blob: d79e1c428b4a03043d7e8dc808f7cfe86b06788d [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"
Andreas Dilger717d50e2007-10-16 18:38:25 -040049#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070050
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040051struct proc_dir_entry *ext4_proc_root;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040052static struct kset *ext4_kset;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040053
Mingming Cao617ba132006-10-11 01:20:53 -070054static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055 unsigned long journal_devnum);
Theodore Ts'oe2d67052009-05-01 00:33:44 -040056static int ext4_commit_super(struct super_block *sb, int sync);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040057static void ext4_mark_recovery_complete(struct super_block *sb,
58 struct ext4_super_block *es);
59static void ext4_clear_journal_err(struct super_block *sb,
60 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070061static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040062static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070063 char nbuf[16]);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040064static int ext4_remount(struct super_block *sb, int *flags, char *data);
65static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080066static int ext4_unfreeze(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040067static void ext4_write_super(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080068static int ext4_freeze(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070069
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070070
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070071ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
72 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070073{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040074 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070075 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040076 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070077}
78
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070079ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
80 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070081{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040082 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070083 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040084 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070085}
86
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070087ext4_fsblk_t ext4_inode_table(struct super_block *sb,
88 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070089{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040090 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070091 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040092 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070093}
94
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -050095__u32 ext4_free_blks_count(struct super_block *sb,
96 struct ext4_group_desc *bg)
97{
98 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
99 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
100 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
101}
102
103__u32 ext4_free_inodes_count(struct super_block *sb,
104 struct ext4_group_desc *bg)
105{
106 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
107 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
108 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
109}
110
111__u32 ext4_used_dirs_count(struct super_block *sb,
112 struct ext4_group_desc *bg)
113{
114 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
115 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
116 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
117}
118
119__u32 ext4_itable_unused_count(struct super_block *sb,
120 struct ext4_group_desc *bg)
121{
122 return le16_to_cpu(bg->bg_itable_unused_lo) |
123 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
124 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
125}
126
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700127void ext4_block_bitmap_set(struct super_block *sb,
128 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700129{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400130 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700131 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
132 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700133}
134
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700135void ext4_inode_bitmap_set(struct super_block *sb,
136 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700137{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400138 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700139 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
140 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700141}
142
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700143void ext4_inode_table_set(struct super_block *sb,
144 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700145{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400146 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700147 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
148 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700149}
150
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500151void ext4_free_blks_set(struct super_block *sb,
152 struct ext4_group_desc *bg, __u32 count)
153{
154 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
155 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
156 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
157}
158
159void ext4_free_inodes_set(struct super_block *sb,
160 struct ext4_group_desc *bg, __u32 count)
161{
162 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
163 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
164 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
165}
166
167void ext4_used_dirs_set(struct super_block *sb,
168 struct ext4_group_desc *bg, __u32 count)
169{
170 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
171 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
172 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
173}
174
175void ext4_itable_unused_set(struct super_block *sb,
176 struct ext4_group_desc *bg, __u32 count)
177{
178 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
179 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
180 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
181}
182
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700183/*
Mingming Caodab291a2006-10-11 01:21:01 -0700184 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 *
186 * The only special thing we need to do here is to make sure that all
187 * journal_end calls result in the superblock being marked dirty, so
188 * that sync() will call the filesystem's write_super callback if
189 * appropriate.
190 */
Mingming Cao617ba132006-10-11 01:20:53 -0700191handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192{
193 journal_t *journal;
194
195 if (sb->s_flags & MS_RDONLY)
196 return ERR_PTR(-EROFS);
197
198 /* Special case here: if the journal has aborted behind our
199 * backs (eg. EIO in the commit thread), then we still need to
200 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700201 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500202 if (journal) {
203 if (is_journal_aborted(journal)) {
204 ext4_abort(sb, __func__,
205 "Detected aborted journal");
206 return ERR_PTR(-EROFS);
207 }
208 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700209 }
Frank Mayhar03901312009-01-07 00:06:22 -0500210 /*
211 * We're not journaling, return the appropriate indication.
212 */
213 current->journal_info = EXT4_NOJOURNAL_HANDLE;
214 return current->journal_info;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215}
216
217/*
218 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700219 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700220 * that sync() will call the filesystem's write_super callback if
221 * appropriate.
222 */
Mingming Cao617ba132006-10-11 01:20:53 -0700223int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
225 struct super_block *sb;
226 int err;
227 int rc;
228
Frank Mayhar03901312009-01-07 00:06:22 -0500229 if (!ext4_handle_valid(handle)) {
230 /*
231 * Do this here since we don't call jbd2_journal_stop() in
232 * no-journal mode.
233 */
234 current->journal_info = NULL;
235 return 0;
236 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237 sb = handle->h_transaction->t_journal->j_private;
238 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700239 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240
241 if (!err)
242 err = rc;
243 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700244 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700245 return err;
246}
247
Mingming Cao617ba132006-10-11 01:20:53 -0700248void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 struct buffer_head *bh, handle_t *handle, int err)
250{
251 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700252 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700253
Frank Mayhar03901312009-01-07 00:06:22 -0500254 BUG_ON(!ext4_handle_valid(handle));
255
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256 if (bh)
257 BUFFER_TRACE(bh, "abort");
258
259 if (!handle->h_err)
260 handle->h_err = err;
261
262 if (is_handle_aborted(handle))
263 return;
264
265 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
266 caller, errstr, err_fn);
267
Mingming Caodab291a2006-10-11 01:21:01 -0700268 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700269}
270
271/* Deal with the reporting of failure conditions on a filesystem such as
272 * inconsistencies detected or read IO failures.
273 *
274 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700275 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276 * write ordering constraints on the superblock which prevent us from
277 * writing it out straight away; and given that the journal is about to
278 * be aborted, we can't rely on the current, or future, transactions to
279 * write out the superblock safely.
280 *
Mingming Caodab291a2006-10-11 01:21:01 -0700281 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 * the journal instead. On recovery, the journal will compain about
283 * that error until we've noted it down and cleared it.
284 */
285
Mingming Cao617ba132006-10-11 01:20:53 -0700286static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700287{
Mingming Cao617ba132006-10-11 01:20:53 -0700288 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700289
Mingming Cao617ba132006-10-11 01:20:53 -0700290 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
291 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292
293 if (sb->s_flags & MS_RDONLY)
294 return;
295
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400296 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700297 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700298
Mingming Cao617ba132006-10-11 01:20:53 -0700299 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700300 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700301 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400303 if (test_opt(sb, ERRORS_RO)) {
304 printk(KERN_CRIT "Remounting filesystem read-only\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 sb->s_flags |= MS_RDONLY;
306 }
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400307 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700308 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700309 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310 sb->s_id);
311}
312
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400313void ext4_error(struct super_block *sb, const char *function,
314 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315{
316 va_list args;
317
318 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400319 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320 vprintk(fmt, args);
321 printk("\n");
322 va_end(args);
323
Mingming Cao617ba132006-10-11 01:20:53 -0700324 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325}
326
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400327static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 char nbuf[16])
329{
330 char *errstr = NULL;
331
332 switch (errno) {
333 case -EIO:
334 errstr = "IO failure";
335 break;
336 case -ENOMEM:
337 errstr = "Out of memory";
338 break;
339 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700340 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341 errstr = "Journal has aborted";
342 else
343 errstr = "Readonly filesystem";
344 break;
345 default:
346 /* If the caller passed in an extra buffer for unknown
347 * errors, textualise them now. Else we just return
348 * NULL. */
349 if (nbuf) {
350 /* Check for truncated error codes... */
351 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
352 errstr = nbuf;
353 }
354 break;
355 }
356
357 return errstr;
358}
359
Mingming Cao617ba132006-10-11 01:20:53 -0700360/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 * automatically and invokes the appropriate error response. */
362
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400363void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700364{
365 char nbuf[16];
366 const char *errstr;
367
368 /* Special case: if the error is EROFS, and we're not already
369 * inside a transaction, then there's really no point in logging
370 * an error. */
371 if (errno == -EROFS && journal_current_handle() == NULL &&
372 (sb->s_flags & MS_RDONLY))
373 return;
374
Mingming Cao617ba132006-10-11 01:20:53 -0700375 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400376 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
377 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700378
Mingming Cao617ba132006-10-11 01:20:53 -0700379 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700380}
381
382/*
Mingming Cao617ba132006-10-11 01:20:53 -0700383 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700384 * abort function may be used to deal with unrecoverable failures such
385 * as journal IO errors or ENOMEM at a critical moment in log management.
386 *
387 * We unconditionally force the filesystem into an ABORT|READONLY state,
388 * unless the error response on the fs has been set to panic in which
389 * case we take the easy way out and panic immediately.
390 */
391
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400392void ext4_abort(struct super_block *sb, const char *function,
393 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700394{
395 va_list args;
396
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400397 printk(KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700398
399 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400400 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 vprintk(fmt, args);
402 printk("\n");
403 va_end(args);
404
405 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700406 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407
408 if (sb->s_flags & MS_RDONLY)
409 return;
410
411 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700412 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700413 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700414 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400415 if (EXT4_SB(sb)->s_journal)
416 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700417}
418
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400419void ext4_warning(struct super_block *sb, const char *function,
420 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421{
422 va_list args;
423
424 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700425 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700426 sb->s_id, function);
427 vprintk(fmt, args);
428 printk("\n");
429 va_end(args);
430}
431
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500432void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
433 const char *function, const char *fmt, ...)
434__releases(bitlock)
435__acquires(bitlock)
436{
437 va_list args;
438 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
439
440 va_start(args, fmt);
441 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
442 vprintk(fmt, args);
443 printk("\n");
444 va_end(args);
445
446 if (test_opt(sb, ERRORS_CONT)) {
447 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
448 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400449 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500450 return;
451 }
452 ext4_unlock_group(sb, grp);
453 ext4_handle_error(sb);
454 /*
455 * We only get here in the ERRORS_RO case; relocking the group
456 * may be dangerous, but nothing bad will happen since the
457 * filesystem will have already been marked read/only and the
458 * journal has been aborted. We return 1 as a hint to callers
459 * who might what to use the return value from
460 * ext4_grp_locked_error() to distinguish beween the
461 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
462 * aggressively from the ext4 function in question, with a
463 * more appropriate error code.
464 */
465 ext4_lock_group(sb, grp);
466 return;
467}
468
469
Mingming Cao617ba132006-10-11 01:20:53 -0700470void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471{
Mingming Cao617ba132006-10-11 01:20:53 -0700472 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473
Mingming Cao617ba132006-10-11 01:20:53 -0700474 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475 return;
476
Harvey Harrison46e665e2008-04-17 10:38:59 -0400477 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478 "updating to rev %d because of new feature flag, "
479 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700480 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481
Mingming Cao617ba132006-10-11 01:20:53 -0700482 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
483 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
484 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485 /* leave es->s_feature_*compat flags alone */
486 /* es->s_uuid will be set by e2fsck if empty */
487
488 /*
489 * The rest of the superblock fields should be zero, and if not it
490 * means they are likely already in use, so leave them alone. We
491 * can leave it up to e2fsck to clean up any inconsistencies there.
492 */
493}
494
495/*
496 * Open the external journal device
497 */
Mingming Cao617ba132006-10-11 01:20:53 -0700498static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499{
500 struct block_device *bdev;
501 char b[BDEVNAME_SIZE];
502
503 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
504 if (IS_ERR(bdev))
505 goto fail;
506 return bdev;
507
508fail:
Theodore Ts'oabda1412009-01-06 00:20:32 -0500509 printk(KERN_ERR "EXT4-fs: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510 __bdevname(dev, b), PTR_ERR(bdev));
511 return NULL;
512}
513
514/*
515 * Release the journal device
516 */
Mingming Cao617ba132006-10-11 01:20:53 -0700517static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518{
519 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500520 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700521}
522
Mingming Cao617ba132006-10-11 01:20:53 -0700523static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700524{
525 struct block_device *bdev;
526 int ret = -ENODEV;
527
528 bdev = sbi->journal_bdev;
529 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700530 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 sbi->journal_bdev = NULL;
532 }
533 return ret;
534}
535
536static inline struct inode *orphan_list_entry(struct list_head *l)
537{
Mingming Cao617ba132006-10-11 01:20:53 -0700538 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700539}
540
Mingming Cao617ba132006-10-11 01:20:53 -0700541static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542{
543 struct list_head *l;
544
545 printk(KERN_ERR "sb orphan head is %d\n",
546 le32_to_cpu(sbi->s_es->s_last_orphan));
547
548 printk(KERN_ERR "sb_info orphan list:\n");
549 list_for_each(l, &sbi->s_orphan) {
550 struct inode *inode = orphan_list_entry(l);
551 printk(KERN_ERR " "
552 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
553 inode->i_sb->s_id, inode->i_ino, inode,
554 inode->i_mode, inode->i_nlink,
555 NEXT_ORPHAN(inode));
556 }
557}
558
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400559static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700560{
Mingming Cao617ba132006-10-11 01:20:53 -0700561 struct ext4_sb_info *sbi = EXT4_SB(sb);
562 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400563 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564
Alex Tomasc9de5602008-01-29 00:19:52 -0500565 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700566 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700567 ext4_xattr_put_super(sb);
Frank Mayhar03901312009-01-07 00:06:22 -0500568 if (sbi->s_journal) {
569 err = jbd2_journal_destroy(sbi->s_journal);
570 sbi->s_journal = NULL;
571 if (err < 0)
572 ext4_abort(sb, __func__,
573 "Couldn't clean up the journal");
574 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700576 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577 es->s_state = cpu_to_le16(sbi->s_mount_state);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400578 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400580 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400581 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400582 }
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400583 kobject_del(&sbi->s_kobj);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700584
585 for (i = 0; i < sbi->s_gdb_count; i++)
586 brelse(sbi->s_group_desc[i]);
587 kfree(sbi->s_group_desc);
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -0400588 if (is_vmalloc_addr(sbi->s_flex_groups))
589 vfree(sbi->s_flex_groups);
590 else
591 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592 percpu_counter_destroy(&sbi->s_freeblocks_counter);
593 percpu_counter_destroy(&sbi->s_freeinodes_counter);
594 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400595 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700596 brelse(sbi->s_sbh);
597#ifdef CONFIG_QUOTA
598 for (i = 0; i < MAXQUOTAS; i++)
599 kfree(sbi->s_qf_names[i]);
600#endif
601
602 /* Debugging code just in case the in-memory inode orphan list
603 * isn't empty. The on-disk one can be non-empty if we've
604 * detected an error and taken the fs readonly, but the
605 * in-memory list had better be clean by this point. */
606 if (!list_empty(&sbi->s_orphan))
607 dump_orphan_list(sb, sbi);
608 J_ASSERT(list_empty(&sbi->s_orphan));
609
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700610 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
612 /*
613 * Invalidate the journal device's buffers. We don't want them
614 * floating about in memory - the physical journal device may
615 * hotswapped, and it breaks the `ro-after' testing code.
616 */
617 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700618 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700619 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620 }
621 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400622 /*
623 * Now that we are completely done shutting down the
624 * superblock, we need to actually destroy the kobject.
625 */
626 unlock_kernel();
627 unlock_super(sb);
628 kobject_put(&sbi->s_kobj);
629 wait_for_completion(&sbi->s_kobj_unregister);
630 lock_super(sb);
631 lock_kernel();
Pekka Enberg705895b2009-02-15 18:07:52 -0500632 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633 kfree(sbi);
634 return;
635}
636
Christoph Lametere18b8902006-12-06 20:33:20 -0800637static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638
639/*
640 * Called inside transaction, so use GFP_NOFS
641 */
Mingming Cao617ba132006-10-11 01:20:53 -0700642static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643{
Mingming Cao617ba132006-10-11 01:20:53 -0700644 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700645
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800646 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700647 if (!ei)
648 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400649#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700650 ei->i_acl = EXT4_ACL_NOT_CACHED;
651 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400654 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700655 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500656 INIT_LIST_HEAD(&ei->i_prealloc_list);
657 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500658 /*
659 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
660 * therefore it can be null here. Don't check it, just initialize
661 * jinode.
662 */
Jan Kara678aaf42008-07-11 19:27:31 -0400663 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400664 ei->i_reserved_data_blocks = 0;
665 ei->i_reserved_meta_blocks = 0;
666 ei->i_allocated_meta_blocks = 0;
667 ei->i_delalloc_reserved_flag = 0;
668 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700669 return &ei->vfs_inode;
670}
671
Mingming Cao617ba132006-10-11 01:20:53 -0700672static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700674 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
675 printk("EXT4 Inode %p: orphan list check failed!\n",
676 EXT4_I(inode));
677 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
678 EXT4_I(inode), sizeof(struct ext4_inode_info),
679 true);
680 dump_stack();
681 }
Mingming Cao617ba132006-10-11 01:20:53 -0700682 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683}
684
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700685static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686{
Mingming Cao617ba132006-10-11 01:20:53 -0700687 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700688
Christoph Lametera35afb82007-05-16 22:10:57 -0700689 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400690#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700691 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500693 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700694 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695}
696
697static int init_inodecache(void)
698{
Mingming Cao617ba132006-10-11 01:20:53 -0700699 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
700 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700701 0, (SLAB_RECLAIM_ACCOUNT|
702 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900703 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700704 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700705 return -ENOMEM;
706 return 0;
707}
708
709static void destroy_inodecache(void)
710{
Mingming Cao617ba132006-10-11 01:20:53 -0700711 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712}
713
Mingming Cao617ba132006-10-11 01:20:53 -0700714static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400716#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700717 if (EXT4_I(inode)->i_acl &&
718 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
719 posix_acl_release(EXT4_I(inode)->i_acl);
720 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721 }
Mingming Cao617ba132006-10-11 01:20:53 -0700722 if (EXT4_I(inode)->i_default_acl &&
723 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
724 posix_acl_release(EXT4_I(inode)->i_default_acl);
725 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726 }
727#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400728 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500729 if (EXT4_JOURNAL(inode))
730 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400731 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732}
733
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400734static inline void ext4_show_quota_options(struct seq_file *seq,
735 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736{
737#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700738 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700739
740 if (sbi->s_jquota_fmt)
741 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400742 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700743
744 if (sbi->s_qf_names[USRQUOTA])
745 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
746
747 if (sbi->s_qf_names[GRPQUOTA])
748 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
749
Mingming Cao617ba132006-10-11 01:20:53 -0700750 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751 seq_puts(seq, ",usrquota");
752
Mingming Cao617ba132006-10-11 01:20:53 -0700753 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 seq_puts(seq, ",grpquota");
755#endif
756}
757
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700758/*
759 * Show an option if
760 * - it's set to a non-default value OR
761 * - if the per-sb default is different from the global default
762 */
Mingming Cao617ba132006-10-11 01:20:53 -0700763static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500765 int def_errors;
766 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700767 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700768 struct ext4_sb_info *sbi = EXT4_SB(sb);
769 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700770
771 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500772 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700773
774 if (sbi->s_sb_block != 1)
775 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
776 if (test_opt(sb, MINIX_DF))
777 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500778 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700779 seq_puts(seq, ",grpid");
780 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
781 seq_puts(seq, ",nogrpid");
782 if (sbi->s_resuid != EXT4_DEF_RESUID ||
783 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
784 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
785 }
786 if (sbi->s_resgid != EXT4_DEF_RESGID ||
787 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
788 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
789 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500790 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700791 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500792 def_errors == EXT4_ERRORS_CONTINUE) {
793 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700794 }
795 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500796 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500797 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500798 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700799 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500800 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700801 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500802 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700803 seq_puts(seq, ",debug");
804 if (test_opt(sb, OLDALLOC))
805 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400806#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500807 if (test_opt(sb, XATTR_USER) &&
808 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700809 seq_puts(seq, ",user_xattr");
810 if (!test_opt(sb, XATTR_USER) &&
811 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
812 seq_puts(seq, ",nouser_xattr");
813 }
814#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400815#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500816 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700817 seq_puts(seq, ",acl");
818 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
819 seq_puts(seq, ",noacl");
820#endif
Theodore Ts'o30773842009-01-03 20:27:38 -0500821 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700822 seq_printf(seq, ",commit=%u",
823 (unsigned) (sbi->s_commit_interval / HZ));
824 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500825 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
826 seq_printf(seq, ",min_batch_time=%u",
827 (unsigned) sbi->s_min_batch_time);
828 }
829 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
830 seq_printf(seq, ",max_batch_time=%u",
831 (unsigned) sbi->s_min_batch_time);
832 }
833
Eric Sandeen571640c2008-05-26 12:29:46 -0400834 /*
835 * We're changing the default of barrier mount option, so
836 * let's always display its mount state so it's clear what its
837 * status is.
838 */
839 seq_puts(seq, ",barrier=");
840 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400841 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
842 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700843 if (test_opt(sb, NOBH))
844 seq_puts(seq, ",nobh");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500845 if (test_opt(sb, I_VERSION))
846 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400847 if (!test_opt(sb, DELALLOC))
848 seq_puts(seq, ",nodelalloc");
849
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500851 if (sbi->s_stripe)
852 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500853 /*
854 * journal mode get enabled in different ways
855 * So just print the value even if we didn't specify it
856 */
Mingming Cao617ba132006-10-11 01:20:53 -0700857 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700858 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700859 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700860 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700861 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862 seq_puts(seq, ",data=writeback");
863
Theodore Ts'o240799c2008-10-09 23:53:47 -0400864 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
865 seq_printf(seq, ",inode_readahead_blks=%u",
866 sbi->s_inode_readahead_blks);
867
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400868 if (test_opt(sb, DATA_ERR_ABORT))
869 seq_puts(seq, ",data_err=abort");
870
Theodore Ts'oafd46722009-03-16 23:12:23 -0400871 if (test_opt(sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o06705bf2009-03-28 10:59:57 -0400872 seq_puts(seq, ",noauto_da_alloc");
Theodore Ts'oafd46722009-03-16 23:12:23 -0400873
Mingming Cao617ba132006-10-11 01:20:53 -0700874 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 return 0;
876}
877
878
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700879static struct inode *ext4_nfs_get_inode(struct super_block *sb,
880 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700882 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700883
Mingming Cao617ba132006-10-11 01:20:53 -0700884 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700885 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700886 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700887 return ERR_PTR(-ESTALE);
888
889 /* iget isn't really right if the inode is currently unallocated!!
890 *
Mingming Cao617ba132006-10-11 01:20:53 -0700891 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700892 * deleted, so we should be safe.
893 *
894 * Currently we don't know the generation for parent directory, so
895 * a generation of 0 means "accept any"
896 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800897 inode = ext4_iget(sb, ino);
898 if (IS_ERR(inode))
899 return ERR_CAST(inode);
900 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901 iput(inode);
902 return ERR_PTR(-ESTALE);
903 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700904
905 return inode;
906}
907
908static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
909 int fh_len, int fh_type)
910{
911 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
912 ext4_nfs_get_inode);
913}
914
915static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
916 int fh_len, int fh_type)
917{
918 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
919 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700920}
921
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500922/*
923 * Try to release metadata pages (indirect blocks, directories) which are
924 * mapped via the block device. Since these pages could have journal heads
925 * which would prevent try_to_free_buffers() from freeing them, we must use
926 * jbd2 layer's try_to_free_buffers() function to release them.
927 */
928static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
929{
930 journal_t *journal = EXT4_SB(sb)->s_journal;
931
932 WARN_ON(PageChecked(page));
933 if (!page_has_buffers(page))
934 return 0;
935 if (journal)
936 return jbd2_journal_try_to_free_buffers(journal, page,
937 wait & ~__GFP_WAIT);
938 return try_to_free_buffers(page);
939}
940
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400942#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400943#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944
Mingming Cao617ba132006-10-11 01:20:53 -0700945static int ext4_write_dquot(struct dquot *dquot);
946static int ext4_acquire_dquot(struct dquot *dquot);
947static int ext4_release_dquot(struct dquot *dquot);
948static int ext4_mark_dquot_dirty(struct dquot *dquot);
949static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700950static int ext4_quota_on(struct super_block *sb, int type, int format_id,
951 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700952static int ext4_quota_on_mount(struct super_block *sb, int type);
953static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700955static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700956 const char *data, size_t len, loff_t off);
957
Mingming Cao617ba132006-10-11 01:20:53 -0700958static struct dquot_operations ext4_quota_operations = {
Jan Karaedf72452009-01-12 19:05:26 +0100959 .initialize = dquot_initialize,
960 .drop = dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961 .alloc_space = dquot_alloc_space,
Mingming Cao60e58e02009-01-22 18:13:05 +0100962 .reserve_space = dquot_reserve_space,
963 .claim_space = dquot_claim_space,
964 .release_rsv = dquot_release_reserved_space,
965 .get_reserved_space = ext4_get_reserved_space,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 .alloc_inode = dquot_alloc_inode,
967 .free_space = dquot_free_space,
968 .free_inode = dquot_free_inode,
969 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700970 .write_dquot = ext4_write_dquot,
971 .acquire_dquot = ext4_acquire_dquot,
972 .release_dquot = ext4_release_dquot,
973 .mark_dirty = ext4_mark_dquot_dirty,
Jan Karaa5b5ee322008-11-25 15:31:35 +0100974 .write_info = ext4_write_info,
975 .alloc_dquot = dquot_alloc,
976 .destroy_dquot = dquot_destroy,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977};
978
Mingming Cao617ba132006-10-11 01:20:53 -0700979static struct quotactl_ops ext4_qctl_operations = {
980 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 .quota_off = vfs_quota_off,
982 .quota_sync = vfs_quota_sync,
983 .get_info = vfs_get_dqinfo,
984 .set_info = vfs_set_dqinfo,
985 .get_dqblk = vfs_get_dqblk,
986 .set_dqblk = vfs_set_dqblk
987};
988#endif
989
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800990static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700991 .alloc_inode = ext4_alloc_inode,
992 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700993 .write_inode = ext4_write_inode,
994 .dirty_inode = ext4_dirty_inode,
995 .delete_inode = ext4_delete_inode,
996 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -0700997 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -0800998 .freeze_fs = ext4_freeze,
999 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001000 .statfs = ext4_statfs,
1001 .remount_fs = ext4_remount,
1002 .clear_inode = ext4_clear_inode,
1003 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001005 .quota_read = ext4_quota_read,
1006 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001007#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001008 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001009};
1010
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001011static const struct super_operations ext4_nojournal_sops = {
1012 .alloc_inode = ext4_alloc_inode,
1013 .destroy_inode = ext4_destroy_inode,
1014 .write_inode = ext4_write_inode,
1015 .dirty_inode = ext4_dirty_inode,
1016 .delete_inode = ext4_delete_inode,
1017 .write_super = ext4_write_super,
1018 .put_super = ext4_put_super,
1019 .statfs = ext4_statfs,
1020 .remount_fs = ext4_remount,
1021 .clear_inode = ext4_clear_inode,
1022 .show_options = ext4_show_options,
1023#ifdef CONFIG_QUOTA
1024 .quota_read = ext4_quota_read,
1025 .quota_write = ext4_quota_write,
1026#endif
1027 .bdev_try_to_free_page = bdev_try_to_free_page,
1028};
1029
Christoph Hellwig39655162007-10-21 16:42:17 -07001030static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001031 .fh_to_dentry = ext4_fh_to_dentry,
1032 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001033 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001034};
1035
1036enum {
1037 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1038 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001039 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001040 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001041 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001042 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001043 Opt_journal_update, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001044 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001045 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001046 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1048 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001049 Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err, Opt_resize,
1050 Opt_usrquota, Opt_grpquota, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001051 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001052 Opt_inode_readahead_blks, Opt_journal_ioprio
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053};
1054
Steven Whitehousea447c092008-10-13 10:46:57 +01001055static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 {Opt_bsd_df, "bsddf"},
1057 {Opt_minix_df, "minixdf"},
1058 {Opt_grpid, "grpid"},
1059 {Opt_grpid, "bsdgroups"},
1060 {Opt_nogrpid, "nogrpid"},
1061 {Opt_nogrpid, "sysvgroups"},
1062 {Opt_resgid, "resgid=%u"},
1063 {Opt_resuid, "resuid=%u"},
1064 {Opt_sb, "sb=%u"},
1065 {Opt_err_cont, "errors=continue"},
1066 {Opt_err_panic, "errors=panic"},
1067 {Opt_err_ro, "errors=remount-ro"},
1068 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 {Opt_debug, "debug"},
1070 {Opt_oldalloc, "oldalloc"},
1071 {Opt_orlov, "orlov"},
1072 {Opt_user_xattr, "user_xattr"},
1073 {Opt_nouser_xattr, "nouser_xattr"},
1074 {Opt_acl, "acl"},
1075 {Opt_noacl, "noacl"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076 {Opt_noload, "noload"},
1077 {Opt_nobh, "nobh"},
1078 {Opt_bh, "bh"},
1079 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001080 {Opt_min_batch_time, "min_batch_time=%u"},
1081 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 {Opt_journal_update, "journal=update"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001083 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001084 {Opt_journal_checksum, "journal_checksum"},
1085 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 {Opt_abort, "abort"},
1087 {Opt_data_journal, "data=journal"},
1088 {Opt_data_ordered, "data=ordered"},
1089 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001090 {Opt_data_err_abort, "data_err=abort"},
1091 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092 {Opt_offusrjquota, "usrjquota="},
1093 {Opt_usrjquota, "usrjquota=%s"},
1094 {Opt_offgrpjquota, "grpjquota="},
1095 {Opt_grpjquota, "grpjquota=%s"},
1096 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1097 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1098 {Opt_grpquota, "grpquota"},
1099 {Opt_noquota, "noquota"},
1100 {Opt_quota, "quota"},
1101 {Opt_usrquota, "usrquota"},
1102 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001103 {Opt_barrier, "barrier"},
1104 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001105 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001106 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001107 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001108 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001109 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001110 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001111 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001112 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001113 {Opt_auto_da_alloc, "auto_da_alloc"},
1114 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001115 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116};
1117
Mingming Cao617ba132006-10-11 01:20:53 -07001118static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119{
Mingming Cao617ba132006-10-11 01:20:53 -07001120 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 char *options = (char *) *data;
1122
1123 if (!options || strncmp(options, "sb=", 3) != 0)
1124 return 1; /* Default location */
1125 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -07001126 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 sb_block = simple_strtoul(options, &options, 0);
1128 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001129 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001130 (char *) *data);
1131 return 1;
1132 }
1133 if (*options == ',')
1134 options++;
1135 *data = (void *) options;
1136 return sb_block;
1137}
1138
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001139#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1140
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001141static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001142 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001143 unsigned int *journal_ioprio,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001144 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145{
Mingming Cao617ba132006-10-11 01:20:53 -07001146 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001147 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 substring_t args[MAX_OPT_ARGS];
1149 int data_opt = 0;
1150 int option;
1151#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001152 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 char *qname;
1154#endif
1155
1156 if (!options)
1157 return 1;
1158
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001159 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001160 int token;
1161 if (!*p)
1162 continue;
1163
1164 token = match_token(p, tokens, args);
1165 switch (token) {
1166 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001167 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 break;
1169 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001170 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 break;
1172 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001173 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 break;
1175 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001176 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 break;
1178 case Opt_resuid:
1179 if (match_int(&args[0], &option))
1180 return 0;
1181 sbi->s_resuid = option;
1182 break;
1183 case Opt_resgid:
1184 if (match_int(&args[0], &option))
1185 return 0;
1186 sbi->s_resgid = option;
1187 break;
1188 case Opt_sb:
1189 /* handled by get_sb_block() instead of here */
1190 /* *sb_block = match_int(&args[0]); */
1191 break;
1192 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001193 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1194 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1195 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001196 break;
1197 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001198 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1199 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1200 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 break;
1202 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001203 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1204 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1205 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 break;
1207 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001208 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001211 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 break;
1213 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001214 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 break;
1216 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001217 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001219#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001220 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001221 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 break;
1223 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001224 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 break;
1226#else
1227 case Opt_user_xattr:
1228 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001229 printk(KERN_ERR "EXT4 (no)user_xattr options "
1230 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231 break;
1232#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001233#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234 case Opt_acl:
1235 set_opt(sbi->s_mount_opt, POSIX_ACL);
1236 break;
1237 case Opt_noacl:
1238 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1239 break;
1240#else
1241 case Opt_acl:
1242 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001243 printk(KERN_ERR "EXT4 (no)acl options "
1244 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001245 break;
1246#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 case Opt_journal_update:
1248 /* @@@ FIXME */
1249 /* Eventually we will want to be able to create
1250 a journal file here. For now, only allow the
1251 user to specify an existing inode to be the
1252 journal file. */
1253 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001254 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 "journal on remount\n");
1256 return 0;
1257 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001258 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001260 case Opt_journal_dev:
1261 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001262 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 "journal on remount\n");
1264 return 0;
1265 }
1266 if (match_int(&args[0], &option))
1267 return 0;
1268 *journal_devnum = option;
1269 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001270 case Opt_journal_checksum:
1271 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1272 break;
1273 case Opt_journal_async_commit:
1274 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1275 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1276 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001277 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001278 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001279 break;
1280 case Opt_commit:
1281 if (match_int(&args[0], &option))
1282 return 0;
1283 if (option < 0)
1284 return 0;
1285 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001286 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001287 sbi->s_commit_interval = HZ * option;
1288 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001289 case Opt_max_batch_time:
1290 if (match_int(&args[0], &option))
1291 return 0;
1292 if (option < 0)
1293 return 0;
1294 if (option == 0)
1295 option = EXT4_DEF_MAX_BATCH_TIME;
1296 sbi->s_max_batch_time = option;
1297 break;
1298 case Opt_min_batch_time:
1299 if (match_int(&args[0], &option))
1300 return 0;
1301 if (option < 0)
1302 return 0;
1303 sbi->s_min_batch_time = option;
1304 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001306 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307 goto datacheck;
1308 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001309 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 goto datacheck;
1311 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001312 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313 datacheck:
1314 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001315 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001316 != data_opt) {
1317 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001318 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001319 "mode on remount\n");
1320 return 0;
1321 }
1322 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001323 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 sbi->s_mount_opt |= data_opt;
1325 }
1326 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001327 case Opt_data_err_abort:
1328 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1329 break;
1330 case Opt_data_err_ignore:
1331 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1332 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001333#ifdef CONFIG_QUOTA
1334 case Opt_usrjquota:
1335 qtype = USRQUOTA;
1336 goto set_qf_name;
1337 case Opt_grpjquota:
1338 qtype = GRPQUOTA;
1339set_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001340 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001341 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001343 "EXT4-fs: Cannot change journaled "
1344 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 return 0;
1346 }
1347 qname = match_strdup(&args[0]);
1348 if (!qname) {
1349 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001350 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001351 "storing quotafile name.\n");
1352 return 0;
1353 }
1354 if (sbi->s_qf_names[qtype] &&
1355 strcmp(sbi->s_qf_names[qtype], qname)) {
1356 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001357 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001358 "specified.\n", QTYPE2NAME(qtype));
1359 kfree(qname);
1360 return 0;
1361 }
1362 sbi->s_qf_names[qtype] = qname;
1363 if (strchr(sbi->s_qf_names[qtype], '/')) {
1364 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001365 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001366 "filesystem root.\n");
1367 kfree(sbi->s_qf_names[qtype]);
1368 sbi->s_qf_names[qtype] = NULL;
1369 return 0;
1370 }
1371 set_opt(sbi->s_mount_opt, QUOTA);
1372 break;
1373 case Opt_offusrjquota:
1374 qtype = USRQUOTA;
1375 goto clear_qf_name;
1376 case Opt_offgrpjquota:
1377 qtype = GRPQUOTA;
1378clear_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001379 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001380 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001381 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001382 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 "quota turned on.\n");
1384 return 0;
1385 }
1386 /*
1387 * The space will be released later when all options
1388 * are confirmed to be correct
1389 */
1390 sbi->s_qf_names[qtype] = NULL;
1391 break;
1392 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001393 qfmt = QFMT_VFS_OLD;
1394 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001396 qfmt = QFMT_VFS_V0;
1397set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001398 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001399 sbi->s_jquota_fmt != qfmt) {
1400 printk(KERN_ERR "EXT4-fs: Cannot change "
1401 "journaled quota options when "
1402 "quota turned on.\n");
1403 return 0;
1404 }
1405 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 break;
1407 case Opt_quota:
1408 case Opt_usrquota:
1409 set_opt(sbi->s_mount_opt, QUOTA);
1410 set_opt(sbi->s_mount_opt, USRQUOTA);
1411 break;
1412 case Opt_grpquota:
1413 set_opt(sbi->s_mount_opt, QUOTA);
1414 set_opt(sbi->s_mount_opt, GRPQUOTA);
1415 break;
1416 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001417 if (sb_any_quota_loaded(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001418 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 "options when quota turned on.\n");
1420 return 0;
1421 }
1422 clear_opt(sbi->s_mount_opt, QUOTA);
1423 clear_opt(sbi->s_mount_opt, USRQUOTA);
1424 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1425 break;
1426#else
1427 case Opt_quota:
1428 case Opt_usrquota:
1429 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001430 printk(KERN_ERR
1431 "EXT4-fs: quota options not supported.\n");
1432 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433 case Opt_usrjquota:
1434 case Opt_grpjquota:
1435 case Opt_offusrjquota:
1436 case Opt_offgrpjquota:
1437 case Opt_jqfmt_vfsold:
1438 case Opt_jqfmt_vfsv0:
1439 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001440 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001441 "supported.\n");
1442 break;
1443 case Opt_noquota:
1444 break;
1445#endif
1446 case Opt_abort:
1447 set_opt(sbi->s_mount_opt, ABORT);
1448 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001449 case Opt_nobarrier:
1450 clear_opt(sbi->s_mount_opt, BARRIER);
1451 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 case Opt_barrier:
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001453 if (match_int(&args[0], &option)) {
1454 set_opt(sbi->s_mount_opt, BARRIER);
1455 break;
1456 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001457 if (option)
1458 set_opt(sbi->s_mount_opt, BARRIER);
1459 else
1460 clear_opt(sbi->s_mount_opt, BARRIER);
1461 break;
1462 case Opt_ignore:
1463 break;
1464 case Opt_resize:
1465 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001466 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 "for remount\n");
1468 return 0;
1469 }
1470 if (match_int(&args[0], &option) != 0)
1471 return 0;
1472 *n_blocks_count = option;
1473 break;
1474 case Opt_nobh:
1475 set_opt(sbi->s_mount_opt, NOBH);
1476 break;
1477 case Opt_bh:
1478 clear_opt(sbi->s_mount_opt, NOBH);
1479 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001480 case Opt_i_version:
1481 set_opt(sbi->s_mount_opt, I_VERSION);
1482 sb->s_flags |= MS_I_VERSION;
1483 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001484 case Opt_nodelalloc:
1485 clear_opt(sbi->s_mount_opt, DELALLOC);
1486 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001487 case Opt_stripe:
1488 if (match_int(&args[0], &option))
1489 return 0;
1490 if (option < 0)
1491 return 0;
1492 sbi->s_stripe = option;
1493 break;
Alex Tomas64769242008-07-11 19:27:31 -04001494 case Opt_delalloc:
1495 set_opt(sbi->s_mount_opt, DELALLOC);
1496 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001497 case Opt_inode_readahead_blks:
1498 if (match_int(&args[0], &option))
1499 return 0;
1500 if (option < 0 || option > (1 << 30))
1501 return 0;
Theodore Ts'of7c43952009-04-24 23:31:59 -04001502 if (!is_power_of_2(option)) {
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001503 printk(KERN_ERR "EXT4-fs: inode_readahead_blks"
1504 " must be a power of 2\n");
1505 return 0;
1506 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04001507 sbi->s_inode_readahead_blks = option;
1508 break;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001509 case Opt_journal_ioprio:
1510 if (match_int(&args[0], &option))
1511 return 0;
1512 if (option < 0 || option > 7)
1513 break;
1514 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1515 option);
1516 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001517 case Opt_noauto_da_alloc:
1518 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1519 break;
Theodore Ts'oafd46722009-03-16 23:12:23 -04001520 case Opt_auto_da_alloc:
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001521 if (match_int(&args[0], &option)) {
1522 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1523 break;
1524 }
Theodore Ts'oafd46722009-03-16 23:12:23 -04001525 if (option)
1526 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1527 else
1528 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1529 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001531 printk(KERN_ERR
1532 "EXT4-fs: Unrecognized mount option \"%s\" "
1533 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 return 0;
1535 }
1536 }
1537#ifdef CONFIG_QUOTA
1538 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001539 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 sbi->s_qf_names[USRQUOTA])
1541 clear_opt(sbi->s_mount_opt, USRQUOTA);
1542
Mingming Cao617ba132006-10-11 01:20:53 -07001543 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001544 sbi->s_qf_names[GRPQUOTA])
1545 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1546
1547 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001548 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001549 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001550 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1551 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 "format mixing.\n");
1553 return 0;
1554 }
1555
1556 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001557 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558 "not specified.\n");
1559 return 0;
1560 }
1561 } else {
1562 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001563 printk(KERN_ERR "EXT4-fs: journaled quota format "
1564 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001565 "enabled.\n");
1566 return 0;
1567 }
1568 }
1569#endif
1570 return 1;
1571}
1572
Mingming Cao617ba132006-10-11 01:20:53 -07001573static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 int read_only)
1575{
Mingming Cao617ba132006-10-11 01:20:53 -07001576 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 int res = 0;
1578
Mingming Cao617ba132006-10-11 01:20:53 -07001579 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001580 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1581 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 res = MS_RDONLY;
1583 }
1584 if (read_only)
1585 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001586 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001587 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1588 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001589 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001590 printk(KERN_WARNING
1591 "EXT4-fs warning: mounting fs with errors, "
1592 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1594 le16_to_cpu(es->s_mnt_count) >=
1595 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001596 printk(KERN_WARNING
1597 "EXT4-fs warning: maximal mount count reached, "
1598 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001599 else if (le32_to_cpu(es->s_checkinterval) &&
1600 (le32_to_cpu(es->s_lastcheck) +
1601 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001602 printk(KERN_WARNING
1603 "EXT4-fs warning: checktime reached, "
1604 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001605 if (!sbi->s_journal)
1606 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001607 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001608 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001609 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001611 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001612 if (sbi->s_journal)
1613 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001614
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001615 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001616 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001617 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1619 sb->s_blocksize,
1620 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001621 EXT4_BLOCKS_PER_GROUP(sb),
1622 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001623 sbi->s_mount_opt);
1624
Frank Mayhar03901312009-01-07 00:06:22 -05001625 if (EXT4_SB(sb)->s_journal) {
1626 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1627 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1628 "external", EXT4_SB(sb)->s_journal->j_devname);
1629 } else {
1630 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1631 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001632 return res;
1633}
1634
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001635static int ext4_fill_flex_info(struct super_block *sb)
1636{
1637 struct ext4_sb_info *sbi = EXT4_SB(sb);
1638 struct ext4_group_desc *gdp = NULL;
1639 struct buffer_head *bh;
1640 ext4_group_t flex_group_count;
1641 ext4_group_t flex_group;
1642 int groups_per_flex = 0;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001643 size_t size;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001644 int i;
1645
1646 if (!sbi->s_es->s_log_groups_per_flex) {
1647 sbi->s_log_groups_per_flex = 0;
1648 return 1;
1649 }
1650
1651 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1652 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1653
Frederic Bohec62a11f2008-09-08 10:20:24 -04001654 /* We allocate both existing and potentially added groups */
1655 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001656 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1657 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001658 size = flex_group_count * sizeof(struct flex_groups);
1659 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1660 if (sbi->s_flex_groups == NULL) {
1661 sbi->s_flex_groups = vmalloc(size);
1662 if (sbi->s_flex_groups)
1663 memset(sbi->s_flex_groups, 0, size);
1664 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001665 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001666 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001667 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001668 goto failed;
1669 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001670
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001671 for (i = 0; i < sbi->s_groups_count; i++) {
1672 gdp = ext4_get_group_desc(sb, i, &bh);
1673
1674 flex_group = ext4_flex_group(sbi, i);
Theodore Ts'o9f24e422009-03-04 19:09:10 -05001675 atomic_set(&sbi->s_flex_groups[flex_group].free_inodes,
1676 ext4_free_inodes_count(sb, gdp));
1677 atomic_set(&sbi->s_flex_groups[flex_group].free_blocks,
1678 ext4_free_blks_count(sb, gdp));
Theodore Ts'o7d39db12009-03-04 19:31:53 -05001679 atomic_set(&sbi->s_flex_groups[flex_group].used_dirs,
1680 ext4_used_dirs_count(sb, gdp));
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001681 }
1682
1683 return 1;
1684failed:
1685 return 0;
1686}
1687
Andreas Dilger717d50e2007-10-16 18:38:25 -04001688__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1689 struct ext4_group_desc *gdp)
1690{
1691 __u16 crc = 0;
1692
1693 if (sbi->s_es->s_feature_ro_compat &
1694 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1695 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1696 __le32 le_group = cpu_to_le32(block_group);
1697
1698 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1699 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1700 crc = crc16(crc, (__u8 *)gdp, offset);
1701 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1702 /* for checksum of struct ext4_group_desc do the rest...*/
1703 if ((sbi->s_es->s_feature_incompat &
1704 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1705 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1706 crc = crc16(crc, (__u8 *)gdp + offset,
1707 le16_to_cpu(sbi->s_es->s_desc_size) -
1708 offset);
1709 }
1710
1711 return cpu_to_le16(crc);
1712}
1713
1714int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1715 struct ext4_group_desc *gdp)
1716{
1717 if ((sbi->s_es->s_feature_ro_compat &
1718 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1719 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1720 return 0;
1721
1722 return 1;
1723}
1724
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001725/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001726static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727{
Mingming Cao617ba132006-10-11 01:20:53 -07001728 struct ext4_sb_info *sbi = EXT4_SB(sb);
1729 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1730 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001731 ext4_fsblk_t block_bitmap;
1732 ext4_fsblk_t inode_bitmap;
1733 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001734 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001735 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736
Jose R. Santosce421582007-10-16 18:38:25 -04001737 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1738 flexbg_flag = 1;
1739
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001740 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001742 for (i = 0; i < sbi->s_groups_count; i++) {
1743 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1744
Jose R. Santosce421582007-10-16 18:38:25 -04001745 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001746 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747 else
1748 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001749 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001751 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001752 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001753 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001754 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001755 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756 return 0;
1757 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001758 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001759 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001760 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001761 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001762 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 return 0;
1764 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001765 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001766 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001767 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001768 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001769 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001770 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 return 0;
1772 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001773 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001774 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001775 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001776 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001777 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1778 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001779 if (!(sb->s_flags & MS_RDONLY)) {
1780 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001781 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001782 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001783 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001784 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001785 if (!flexbg_flag)
1786 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001787 }
1788
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001789 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001790 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791 return 1;
1792}
1793
Mingming Cao617ba132006-10-11 01:20:53 -07001794/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001795 * the superblock) which were deleted from all directories, but held open by
1796 * a process at the time of a crash. We walk the list and try to delete these
1797 * inodes at recovery time (only with a read-write filesystem).
1798 *
1799 * In order to keep the orphan inode chain consistent during traversal (in
1800 * case of crash during recovery), we link each inode into the superblock
1801 * orphan list_head and handle it the same way as an inode deletion during
1802 * normal operation (which journals the operations for us).
1803 *
1804 * We only do an iget() and an iput() on each inode, which is very safe if we
1805 * accidentally point at an in-use or already deleted inode. The worst that
1806 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001807 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001808 * e2fsck was run on this filesystem, and it must have already done the orphan
1809 * inode cleanup for us, so we can safely abort without any further action.
1810 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001811static void ext4_orphan_cleanup(struct super_block *sb,
1812 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813{
1814 unsigned int s_flags = sb->s_flags;
1815 int nr_orphans = 0, nr_truncates = 0;
1816#ifdef CONFIG_QUOTA
1817 int i;
1818#endif
1819 if (!es->s_last_orphan) {
1820 jbd_debug(4, "no orphan inodes to clean up\n");
1821 return;
1822 }
1823
Eric Sandeena8f48a92006-12-06 20:40:13 -08001824 if (bdev_read_only(sb->s_bdev)) {
1825 printk(KERN_ERR "EXT4-fs: write access "
1826 "unavailable, skipping orphan cleanup.\n");
1827 return;
1828 }
1829
Mingming Cao617ba132006-10-11 01:20:53 -07001830 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831 if (es->s_last_orphan)
1832 jbd_debug(1, "Errors on filesystem, "
1833 "clearing orphan list.\n");
1834 es->s_last_orphan = 0;
1835 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1836 return;
1837 }
1838
1839 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001840 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001841 sb->s_id);
1842 sb->s_flags &= ~MS_RDONLY;
1843 }
1844#ifdef CONFIG_QUOTA
1845 /* Needed for iput() to work correctly and not trash data */
1846 sb->s_flags |= MS_ACTIVE;
1847 /* Turn on quotas so that they are updated correctly */
1848 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001849 if (EXT4_SB(sb)->s_qf_names[i]) {
1850 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001851 if (ret < 0)
1852 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001853 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854 "quota: error %d\n", ret);
1855 }
1856 }
1857#endif
1858
1859 while (es->s_last_orphan) {
1860 struct inode *inode;
1861
Josef Bacik97bd42b2008-04-29 22:04:56 -04001862 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1863 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001864 es->s_last_orphan = 0;
1865 break;
1866 }
1867
Mingming Cao617ba132006-10-11 01:20:53 -07001868 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Jan Karaa269eb12009-01-26 17:04:39 +01001869 vfs_dq_init(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001870 if (inode->i_nlink) {
1871 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001872 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001873 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001874 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001875 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001876 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877 nr_truncates++;
1878 } else {
1879 printk(KERN_DEBUG
1880 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001881 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001882 jbd_debug(2, "deleting unreferenced inode %lu\n",
1883 inode->i_ino);
1884 nr_orphans++;
1885 }
1886 iput(inode); /* The delete magic happens here! */
1887 }
1888
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001889#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001890
1891 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001892 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001893 sb->s_id, PLURAL(nr_orphans));
1894 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001895 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 sb->s_id, PLURAL(nr_truncates));
1897#ifdef CONFIG_QUOTA
1898 /* Turn quotas off */
1899 for (i = 0; i < MAXQUOTAS; i++) {
1900 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001901 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001902 }
1903#endif
1904 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1905}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001906/*
1907 * Maximal extent format file size.
1908 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1909 * extent format containers, within a sector_t, and within i_blocks
1910 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1911 * so that won't be a limiting factor.
1912 *
1913 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1914 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001915static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001916{
1917 loff_t res;
1918 loff_t upper_limit = MAX_LFS_FILESIZE;
1919
1920 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001921 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001922 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001923 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001924 * i_block represent total blocks in 512 bytes
1925 * 32 == size of vfs inode i_blocks * 8
1926 */
1927 upper_limit = (1LL << 32) - 1;
1928
1929 /* total blocks in file system block size */
1930 upper_limit >>= (blkbits - 9);
1931 upper_limit <<= blkbits;
1932 }
1933
1934 /* 32-bit extent-start container, ee_block */
1935 res = 1LL << 32;
1936 res <<= blkbits;
1937 res -= 1;
1938
1939 /* Sanity check against vm- & vfs- imposed limits */
1940 if (res > upper_limit)
1941 res = upper_limit;
1942
1943 return res;
1944}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001945
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001947 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001948 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1949 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001950 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001951static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952{
Mingming Cao617ba132006-10-11 01:20:53 -07001953 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001954 int meta_blocks;
1955 loff_t upper_limit;
1956 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001957 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001958 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001959 * does not exceed 2^48 -1
1960 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1961 * total number of 512 bytes blocks of the file
1962 */
1963
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001964 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001965 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001966 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001967 * implies the inode i_block represent total blocks in
1968 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001969 */
1970 upper_limit = (1LL << 32) - 1;
1971
1972 /* total blocks in file system block size */
1973 upper_limit >>= (bits - 9);
1974
1975 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001976 /*
1977 * We use 48 bit ext4_inode i_blocks
1978 * With EXT4_HUGE_FILE_FL set the i_blocks
1979 * represent total number of blocks in
1980 * file system block size
1981 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001982 upper_limit = (1LL << 48) - 1;
1983
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001984 }
1985
1986 /* indirect blocks */
1987 meta_blocks = 1;
1988 /* double indirect blocks */
1989 meta_blocks += 1 + (1LL << (bits-2));
1990 /* tripple indirect blocks */
1991 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1992
1993 upper_limit -= meta_blocks;
1994 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995
1996 res += 1LL << (bits-2);
1997 res += 1LL << (2*(bits-2));
1998 res += 1LL << (3*(bits-2));
1999 res <<= bits;
2000 if (res > upper_limit)
2001 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002002
2003 if (res > MAX_LFS_FILESIZE)
2004 res = MAX_LFS_FILESIZE;
2005
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006 return res;
2007}
2008
Mingming Cao617ba132006-10-11 01:20:53 -07002009static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002010 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011{
Mingming Cao617ba132006-10-11 01:20:53 -07002012 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002013 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002014 int has_super = 0;
2015
2016 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2017
Mingming Cao617ba132006-10-11 01:20:53 -07002018 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002020 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07002022 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002023 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07002024 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025}
2026
Alex Tomasc9de5602008-01-29 00:19:52 -05002027/**
2028 * ext4_get_stripe_size: Get the stripe size.
2029 * @sbi: In memory super block info
2030 *
2031 * If we have specified it via mount option, then
2032 * use the mount option value. If the value specified at mount time is
2033 * greater than the blocks per group use the super block value.
2034 * If the super block value is greater than blocks per group return 0.
2035 * Allocator needs it be less than blocks per group.
2036 *
2037 */
2038static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2039{
2040 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2041 unsigned long stripe_width =
2042 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2043
2044 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2045 return sbi->s_stripe;
2046
2047 if (stripe_width <= sbi->s_blocks_per_group)
2048 return stripe_width;
2049
2050 if (stride <= sbi->s_blocks_per_group)
2051 return stride;
2052
2053 return 0;
2054}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002055
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002056/* sysfs supprt */
2057
2058struct ext4_attr {
2059 struct attribute attr;
2060 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2061 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2062 const char *, size_t);
2063 int offset;
2064};
2065
2066static int parse_strtoul(const char *buf,
2067 unsigned long max, unsigned long *value)
2068{
2069 char *endp;
2070
2071 while (*buf && isspace(*buf))
2072 buf++;
2073 *value = simple_strtoul(buf, &endp, 0);
2074 while (*endp && isspace(*endp))
2075 endp++;
2076 if (*endp || *value > max)
2077 return -EINVAL;
2078
2079 return 0;
2080}
2081
2082static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2083 struct ext4_sb_info *sbi,
2084 char *buf)
2085{
2086 return snprintf(buf, PAGE_SIZE, "%llu\n",
2087 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2088}
2089
2090static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2091 struct ext4_sb_info *sbi, char *buf)
2092{
2093 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2094
2095 return snprintf(buf, PAGE_SIZE, "%lu\n",
2096 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2097 sbi->s_sectors_written_start) >> 1);
2098}
2099
2100static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2101 struct ext4_sb_info *sbi, char *buf)
2102{
2103 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2104
2105 return snprintf(buf, PAGE_SIZE, "%llu\n",
2106 sbi->s_kbytes_written +
2107 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2108 EXT4_SB(sb)->s_sectors_written_start) >> 1));
2109}
2110
2111static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2112 struct ext4_sb_info *sbi,
2113 const char *buf, size_t count)
2114{
2115 unsigned long t;
2116
2117 if (parse_strtoul(buf, 0x40000000, &t))
2118 return -EINVAL;
2119
Theodore Ts'of7c43952009-04-24 23:31:59 -04002120 if (!is_power_of_2(t))
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002121 return -EINVAL;
2122
2123 sbi->s_inode_readahead_blks = t;
2124 return count;
2125}
2126
2127static ssize_t sbi_ui_show(struct ext4_attr *a,
2128 struct ext4_sb_info *sbi, char *buf)
2129{
2130 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2131
2132 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2133}
2134
2135static ssize_t sbi_ui_store(struct ext4_attr *a,
2136 struct ext4_sb_info *sbi,
2137 const char *buf, size_t count)
2138{
2139 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2140 unsigned long t;
2141
2142 if (parse_strtoul(buf, 0xffffffff, &t))
2143 return -EINVAL;
2144 *ui = t;
2145 return count;
2146}
2147
2148#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2149static struct ext4_attr ext4_attr_##_name = { \
2150 .attr = {.name = __stringify(_name), .mode = _mode }, \
2151 .show = _show, \
2152 .store = _store, \
2153 .offset = offsetof(struct ext4_sb_info, _elname), \
2154}
2155#define EXT4_ATTR(name, mode, show, store) \
2156static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2157
2158#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2159#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2160#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2161 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2162#define ATTR_LIST(name) &ext4_attr_##name.attr
2163
2164EXT4_RO_ATTR(delayed_allocation_blocks);
2165EXT4_RO_ATTR(session_write_kbytes);
2166EXT4_RO_ATTR(lifetime_write_kbytes);
2167EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2168 inode_readahead_blks_store, s_inode_readahead_blks);
2169EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2170EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2171EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2172EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2173EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2174EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2175
2176static struct attribute *ext4_attrs[] = {
2177 ATTR_LIST(delayed_allocation_blocks),
2178 ATTR_LIST(session_write_kbytes),
2179 ATTR_LIST(lifetime_write_kbytes),
2180 ATTR_LIST(inode_readahead_blks),
2181 ATTR_LIST(mb_stats),
2182 ATTR_LIST(mb_max_to_scan),
2183 ATTR_LIST(mb_min_to_scan),
2184 ATTR_LIST(mb_order2_req),
2185 ATTR_LIST(mb_stream_req),
2186 ATTR_LIST(mb_group_prealloc),
2187 NULL,
2188};
2189
2190static ssize_t ext4_attr_show(struct kobject *kobj,
2191 struct attribute *attr, char *buf)
2192{
2193 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2194 s_kobj);
2195 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2196
2197 return a->show ? a->show(a, sbi, buf) : 0;
2198}
2199
2200static ssize_t ext4_attr_store(struct kobject *kobj,
2201 struct attribute *attr,
2202 const char *buf, size_t len)
2203{
2204 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2205 s_kobj);
2206 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2207
2208 return a->store ? a->store(a, sbi, buf, len) : 0;
2209}
2210
2211static void ext4_sb_release(struct kobject *kobj)
2212{
2213 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2214 s_kobj);
2215 complete(&sbi->s_kobj_unregister);
2216}
2217
2218
2219static struct sysfs_ops ext4_attr_ops = {
2220 .show = ext4_attr_show,
2221 .store = ext4_attr_store,
2222};
2223
2224static struct kobj_type ext4_ktype = {
2225 .default_attrs = ext4_attrs,
2226 .sysfs_ops = &ext4_attr_ops,
2227 .release = ext4_sb_release,
2228};
2229
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002230static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002231 __releases(kernel_lock)
2232 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002233
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002235 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002236 struct ext4_super_block *es = NULL;
2237 struct ext4_sb_info *sbi;
2238 ext4_fsblk_t block;
2239 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002240 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 unsigned long journal_devnum = 0;
2243 unsigned long def_mount_opts;
2244 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002245 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002246 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002247 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002248 int blocksize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002249 unsigned int db_count;
2250 unsigned int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002251 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002252 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002253 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002254 int err;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002255 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002256
2257 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2258 if (!sbi)
2259 return -ENOMEM;
Pekka Enberg705895b2009-02-15 18:07:52 -05002260
2261 sbi->s_blockgroup_lock =
2262 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2263 if (!sbi->s_blockgroup_lock) {
2264 kfree(sbi);
2265 return -ENOMEM;
2266 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002267 sb->s_fs_info = sbi;
2268 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002269 sbi->s_resuid = EXT4_DEF_RESUID;
2270 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002271 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002272 sbi->s_sb_block = sb_block;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002273 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2274 sectors[1]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002275
2276 unlock_kernel();
2277
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002278 /* Cleanup superblock name */
2279 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2280 *cp = '!';
2281
Mingming Cao617ba132006-10-11 01:20:53 -07002282 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002283 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07002284 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285 goto out_fail;
2286 }
2287
2288 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002289 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002290 * block sizes. We need to calculate the offset from buffer start.
2291 */
Mingming Cao617ba132006-10-11 01:20:53 -07002292 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002293 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2294 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002296 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002297 }
2298
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002299 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002300 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002301 goto out_fail;
2302 }
2303 /*
2304 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002305 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002306 */
Mingming Cao617ba132006-10-11 01:20:53 -07002307 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002308 sbi->s_es = es;
2309 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002310 if (sb->s_magic != EXT4_SUPER_MAGIC)
2311 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002312 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313
2314 /* Set defaults before we parse the mount options */
2315 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002316 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07002318 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07002320 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002321 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002322#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002323 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002324 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002325#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002326#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002327 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002329#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002330 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2331 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2332 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2333 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2334 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2335 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002336
Mingming Cao617ba132006-10-11 01:20:53 -07002337 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002339 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002340 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002341 else
2342 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002343
2344 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2345 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002346 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2347 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2348 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349
Eric Sandeen571640c2008-05-26 12:29:46 -04002350 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002351
Mingming Cao1e2462f2007-07-18 09:00:55 -04002352 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002353 * enable delayed allocation by default
2354 * Use -o nodelalloc to turn it off
2355 */
2356 set_opt(sbi->s_mount_opt, DELALLOC);
2357
2358
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002359 if (!parse_options((char *) data, sb, &journal_devnum,
2360 &journal_ioprio, NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002361 goto failed_mount;
2362
2363 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002364 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002365
Mingming Cao617ba132006-10-11 01:20:53 -07002366 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2367 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2368 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2369 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002370 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002371 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002372 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002373
2374 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375 * Check feature flags regardless of the revision level, since we
2376 * previously didn't change the revision level when setting the flags,
2377 * so there is a chance incompat flags are set on a rev 0 filesystem.
2378 */
Mingming Cao617ba132006-10-11 01:20:53 -07002379 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002380 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002381 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002382 "unsupported optional features (%x).\n", sb->s_id,
2383 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2384 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002385 goto failed_mount;
2386 }
Mingming Cao617ba132006-10-11 01:20:53 -07002387 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002388 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002389 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002390 "unsupported optional features (%x).\n", sb->s_id,
2391 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2392 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002393 goto failed_mount;
2394 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002395 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2396 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2397 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002398 /*
2399 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002400 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002401 */
2402 if (sizeof(root->i_blocks) < sizeof(u64) &&
2403 !(sb->s_flags & MS_RDONLY)) {
2404 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2405 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002406 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002407 goto failed_mount;
2408 }
2409 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002410 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2411
Mingming Cao617ba132006-10-11 01:20:53 -07002412 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2413 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002415 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002416 blocksize, sb->s_id);
2417 goto failed_mount;
2418 }
2419
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002421
2422 /* Validate the filesystem blocksize */
2423 if (!sb_set_blocksize(sb, blocksize)) {
2424 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2425 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 goto failed_mount;
2427 }
2428
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002429 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002430 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2431 offset = do_div(logical_sb_block, blocksize);
2432 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002433 if (!bh) {
2434 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002435 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002436 goto failed_mount;
2437 }
Mingming Cao617ba132006-10-11 01:20:53 -07002438 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002440 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002441 printk(KERN_ERR
2442 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002443 goto failed_mount;
2444 }
2445 }
2446
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002447 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2448 has_huge_files);
2449 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450
Mingming Cao617ba132006-10-11 01:20:53 -07002451 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2452 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2453 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454 } else {
2455 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2456 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002457 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002458 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002459 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002460 printk(KERN_ERR
2461 "EXT4-fs: unsupported inode size: %d\n",
2462 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002463 goto failed_mount;
2464 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002465 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2466 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002467 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002468 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2469 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002470 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002471 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002472 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002473 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002474 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002475 sbi->s_desc_size);
2476 goto failed_mount;
2477 }
2478 } else
2479 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002482 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002483 goto cantfind_ext4;
2484 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002485 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002486 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002487 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2488 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002489 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 sbi->s_sbh = bh;
2491 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002492 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2493 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002494 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002495 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2496 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002497 i = le32_to_cpu(es->s_flags);
2498 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2499 sbi->s_hash_unsigned = 3;
2500 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2501#ifdef __CHAR_UNSIGNED__
2502 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2503 sbi->s_hash_unsigned = 3;
2504#else
2505 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2506#endif
2507 sb->s_dirt = 1;
2508 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002509
2510 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002511 printk(KERN_ERR
2512 "EXT4-fs: #blocks per group too big: %lu\n",
2513 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002514 goto failed_mount;
2515 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002516 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002517 printk(KERN_ERR
2518 "EXT4-fs: #inodes per group too big: %lu\n",
2519 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002520 goto failed_mount;
2521 }
2522
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002523 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002525 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002526 " too large to mount safely\n", sb->s_id);
2527 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002528 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529 "enabled\n");
2530 goto failed_mount;
2531 }
2532
Mingming Cao617ba132006-10-11 01:20:53 -07002533 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2534 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002535
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002536 /* check blocks count against device size */
2537 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2538 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
2539 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu "
2540 "exceeds size of device (%llu blocks)\n",
2541 ext4_blocks_count(es), blocks_count);
2542 goto failed_mount;
2543 }
2544
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002545 /*
2546 * It makes no sense for the first data block to be beyond the end
2547 * of the filesystem.
2548 */
2549 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
2550 printk(KERN_WARNING "EXT4-fs: bad geometry: first data"
2551 "block %u is beyond end of filesystem (%llu)\n",
2552 le32_to_cpu(es->s_first_data_block),
2553 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05002554 goto failed_mount;
2555 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002556 blocks_count = (ext4_blocks_count(es) -
2557 le32_to_cpu(es->s_first_data_block) +
2558 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2559 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002560 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
2561 printk(KERN_WARNING "EXT4-fs: groups count too large: %u "
2562 "(block count %llu, first data block %u, "
2563 "blocks per group %lu)\n", sbi->s_groups_count,
2564 ext4_blocks_count(es),
2565 le32_to_cpu(es->s_first_data_block),
2566 EXT4_BLOCKS_PER_GROUP(sb));
2567 goto failed_mount;
2568 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002569 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002570 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2571 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002572 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002573 GFP_KERNEL);
2574 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002575 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002576 goto failed_mount;
2577 }
2578
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002579#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002580 if (ext4_proc_root)
2581 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002582#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002583
Pekka Enberg705895b2009-02-15 18:07:52 -05002584 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002585
2586 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002587 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588 sbi->s_group_desc[i] = sb_bread(sb, block);
2589 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002590 printk(KERN_ERR "EXT4-fs: "
2591 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002592 db_count = i;
2593 goto failed_mount2;
2594 }
2595 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002596 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002597 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 goto failed_mount2;
2599 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002600 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2601 if (!ext4_fill_flex_info(sb)) {
2602 printk(KERN_ERR
2603 "EXT4-fs: unable to initialize "
2604 "flex_bg meta info!\n");
2605 goto failed_mount2;
2606 }
2607
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002608 sbi->s_gdb_count = db_count;
2609 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2610 spin_lock_init(&sbi->s_next_gen_lock);
2611
Peter Zijlstra833f4072007-10-16 23:25:45 -07002612 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2613 ext4_count_free_blocks(sb));
2614 if (!err) {
2615 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2616 ext4_count_free_inodes(sb));
2617 }
2618 if (!err) {
2619 err = percpu_counter_init(&sbi->s_dirs_counter,
2620 ext4_count_dirs(sb));
2621 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002622 if (!err) {
2623 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2624 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002625 if (err) {
2626 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2627 goto failed_mount3;
2628 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002629
Alex Tomasc9de5602008-01-29 00:19:52 -05002630 sbi->s_stripe = ext4_get_stripe_size(sbi);
2631
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002632 /*
2633 * set up enough so that it can read an inode
2634 */
Theodore Ts'o9ca92382009-05-01 12:52:25 -04002635 if (!test_opt(sb, NOLOAD) &&
2636 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2637 sb->s_op = &ext4_sops;
2638 else
2639 sb->s_op = &ext4_nojournal_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07002640 sb->s_export_op = &ext4_export_ops;
2641 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002643 sb->s_qcop = &ext4_qctl_operations;
2644 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002645#endif
2646 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002647 mutex_init(&sbi->s_orphan_lock);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04002648 mutex_init(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002649
2650 sb->s_root = NULL;
2651
2652 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002653 EXT4_HAS_INCOMPAT_FEATURE(sb,
2654 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002655
2656 /*
2657 * The first inode we look at is the journal inode. Don't try
2658 * root first: it may be modified in the journal!
2659 */
2660 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002661 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2662 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002663 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002664 if (!(sb->s_flags & MS_RDONLY) &&
2665 EXT4_SB(sb)->s_journal->j_failed_commit) {
2666 printk(KERN_CRIT "EXT4-fs error (device %s): "
2667 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002668 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002669 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002670 if (test_opt(sb, ERRORS_RO)) {
2671 printk(KERN_CRIT
2672 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002673 sb->s_flags |= MS_RDONLY;
2674 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2675 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2676 }
2677 if (test_opt(sb, ERRORS_PANIC)) {
2678 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2679 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04002680 ext4_commit_super(sb, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002681 goto failed_mount4;
2682 }
2683 }
Frank Mayhar03901312009-01-07 00:06:22 -05002684 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2685 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2686 printk(KERN_ERR "EXT4-fs: required journal recovery "
2687 "suppressed and not mounted read-only\n");
2688 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002690 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2691 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2692 sbi->s_journal = NULL;
2693 needs_recovery = 0;
2694 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002695 }
2696
Jose R. Santoseb40a092007-07-18 08:37:25 -04002697 if (ext4_blocks_count(es) > 0xffffffffULL &&
2698 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2699 JBD2_FEATURE_INCOMPAT_64BIT)) {
Theodore Ts'oabda1412009-01-06 00:20:32 -05002700 printk(KERN_ERR "EXT4-fs: Failed to set 64-bit journal feature\n");
Jose R. Santoseb40a092007-07-18 08:37:25 -04002701 goto failed_mount4;
2702 }
2703
Girish Shilamkar818d2762008-01-28 23:58:27 -05002704 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2705 jbd2_journal_set_features(sbi->s_journal,
2706 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2707 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2708 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2709 jbd2_journal_set_features(sbi->s_journal,
2710 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2711 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2712 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2713 } else {
2714 jbd2_journal_clear_features(sbi->s_journal,
2715 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2716 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2717 }
2718
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002719 /* We have now updated the journal if required, so we can
2720 * validate the data journaling mode. */
2721 switch (test_opt(sb, DATA_FLAGS)) {
2722 case 0:
2723 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002724 * capabilities: ORDERED_DATA if the journal can
2725 * cope, else JOURNAL_DATA
2726 */
Mingming Caodab291a2006-10-11 01:21:01 -07002727 if (jbd2_journal_check_available_features
2728 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2730 else
2731 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2732 break;
2733
Mingming Cao617ba132006-10-11 01:20:53 -07002734 case EXT4_MOUNT_ORDERED_DATA:
2735 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002736 if (!jbd2_journal_check_available_features
2737 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002738 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002739 "requested data journaling mode\n");
2740 goto failed_mount4;
2741 }
2742 default:
2743 break;
2744 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002745 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002746
Frank Mayhar03901312009-01-07 00:06:22 -05002747no_journal:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748
2749 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002750 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2751 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 "its supported only with writeback mode\n");
2753 clear_opt(sbi->s_mount_opt, NOBH);
2754 }
2755 }
2756 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002757 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002758 * so we can safely mount the rest of the filesystem now.
2759 */
2760
David Howells1d1fe1e2008-02-07 00:15:37 -08002761 root = ext4_iget(sb, EXT4_ROOT_INO);
2762 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002763 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002764 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765 goto failed_mount4;
2766 }
2767 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002768 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002769 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002770 goto failed_mount4;
2771 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002772 sb->s_root = d_alloc_root(root);
2773 if (!sb->s_root) {
2774 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2775 iput(root);
2776 ret = -ENOMEM;
2777 goto failed_mount4;
2778 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002779
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002780 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002781
2782 /* determine the minimum size of new large inodes, if present */
2783 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2784 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2785 EXT4_GOOD_OLD_INODE_SIZE;
2786 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2787 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2788 if (sbi->s_want_extra_isize <
2789 le16_to_cpu(es->s_want_extra_isize))
2790 sbi->s_want_extra_isize =
2791 le16_to_cpu(es->s_want_extra_isize);
2792 if (sbi->s_want_extra_isize <
2793 le16_to_cpu(es->s_min_extra_isize))
2794 sbi->s_want_extra_isize =
2795 le16_to_cpu(es->s_min_extra_isize);
2796 }
2797 }
2798 /* Check if enough inode space is available */
2799 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2800 sbi->s_inode_size) {
2801 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2802 EXT4_GOOD_OLD_INODE_SIZE;
2803 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2804 "available.\n");
2805 }
2806
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002807 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2808 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2809 "requested data journaling mode\n");
2810 clear_opt(sbi->s_mount_opt, DELALLOC);
2811 } else if (test_opt(sb, DELALLOC))
2812 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2813
2814 ext4_ext_init(sb);
2815 err = ext4_mb_init(sb, needs_recovery);
2816 if (err) {
2817 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2818 err);
2819 goto failed_mount4;
2820 }
2821
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002822 sbi->s_kobj.kset = ext4_kset;
2823 init_completion(&sbi->s_kobj_unregister);
2824 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2825 "%s", sb->s_id);
2826 if (err) {
2827 ext4_mb_release(sb);
2828 ext4_ext_release(sb);
2829 goto failed_mount4;
2830 };
2831
Mingming Cao617ba132006-10-11 01:20:53 -07002832 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2833 ext4_orphan_cleanup(sb, es);
2834 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002835 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002836 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002837 ext4_mark_recovery_complete(sb, es);
2838 }
2839 if (EXT4_SB(sb)->s_journal) {
2840 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2841 descr = " journalled data mode";
2842 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2843 descr = " ordered data mode";
2844 else
2845 descr = " writeback data mode";
2846 } else
2847 descr = "out journal";
2848
2849 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2850 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851
2852 lock_kernel();
2853 return 0;
2854
Mingming Cao617ba132006-10-11 01:20:53 -07002855cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002856 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002857 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858 sb->s_id);
2859 goto failed_mount;
2860
2861failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002862 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2863 if (sbi->s_journal) {
2864 jbd2_journal_destroy(sbi->s_journal);
2865 sbi->s_journal = NULL;
2866 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002867failed_mount3:
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04002868 if (sbi->s_flex_groups) {
2869 if (is_vmalloc_addr(sbi->s_flex_groups))
2870 vfree(sbi->s_flex_groups);
2871 else
2872 kfree(sbi->s_flex_groups);
2873 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2875 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2876 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002877 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002878failed_mount2:
2879 for (i = 0; i < db_count; i++)
2880 brelse(sbi->s_group_desc[i]);
2881 kfree(sbi->s_group_desc);
2882failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002883 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002884 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002885 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002886#ifdef CONFIG_QUOTA
2887 for (i = 0; i < MAXQUOTAS; i++)
2888 kfree(sbi->s_qf_names[i]);
2889#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002890 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 brelse(bh);
2892out_fail:
2893 sb->s_fs_info = NULL;
2894 kfree(sbi);
2895 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002896 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002897}
2898
2899/*
2900 * Setup any per-fs journal parameters now. We'll do this both on
2901 * initial mount, once the journal has been initialised but before we've
2902 * done any recovery; and again on any subsequent remount.
2903 */
Mingming Cao617ba132006-10-11 01:20:53 -07002904static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905{
Mingming Cao617ba132006-10-11 01:20:53 -07002906 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907
Theodore Ts'o30773842009-01-03 20:27:38 -05002908 journal->j_commit_interval = sbi->s_commit_interval;
2909 journal->j_min_batch_time = sbi->s_min_batch_time;
2910 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911
2912 spin_lock(&journal->j_state_lock);
2913 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002914 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 else
Mingming Caodab291a2006-10-11 01:21:01 -07002916 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002917 if (test_opt(sb, DATA_ERR_ABORT))
2918 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2919 else
2920 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002921 spin_unlock(&journal->j_state_lock);
2922}
2923
Mingming Cao617ba132006-10-11 01:20:53 -07002924static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925 unsigned int journal_inum)
2926{
2927 struct inode *journal_inode;
2928 journal_t *journal;
2929
Frank Mayhar03901312009-01-07 00:06:22 -05002930 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2931
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002932 /* First, test for the existence of a valid inode on disk. Bad
2933 * things happen if we iget() an unused inode, as the subsequent
2934 * iput() will try to delete it. */
2935
David Howells1d1fe1e2008-02-07 00:15:37 -08002936 journal_inode = ext4_iget(sb, journal_inum);
2937 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002938 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939 return NULL;
2940 }
2941 if (!journal_inode->i_nlink) {
2942 make_bad_inode(journal_inode);
2943 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002944 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945 return NULL;
2946 }
2947
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002948 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002949 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002950 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002951 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952 iput(journal_inode);
2953 return NULL;
2954 }
2955
Mingming Caodab291a2006-10-11 01:21:01 -07002956 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002957 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002958 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002959 iput(journal_inode);
2960 return NULL;
2961 }
2962 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002963 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002964 return journal;
2965}
2966
Mingming Cao617ba132006-10-11 01:20:53 -07002967static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002968 dev_t j_dev)
2969{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002970 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002971 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002972 ext4_fsblk_t start;
2973 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002975 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002976 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002977 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978 struct block_device *bdev;
2979
Frank Mayhar03901312009-01-07 00:06:22 -05002980 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2981
Mingming Cao617ba132006-10-11 01:20:53 -07002982 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983 if (bdev == NULL)
2984 return NULL;
2985
2986 if (bd_claim(bdev, sb)) {
2987 printk(KERN_ERR
Theodore Ts'oabda1412009-01-06 00:20:32 -05002988 "EXT4-fs: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002989 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002990 return NULL;
2991 }
2992
2993 blocksize = sb->s_blocksize;
2994 hblock = bdev_hardsect_size(bdev);
2995 if (blocksize < hblock) {
2996 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002997 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 goto out_bdev;
2999 }
3000
Mingming Cao617ba132006-10-11 01:20:53 -07003001 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3002 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003 set_blocksize(bdev, blocksize);
3004 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07003005 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006 "external journal\n");
3007 goto out_bdev;
3008 }
3009
Mingming Cao617ba132006-10-11 01:20:53 -07003010 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3011 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07003013 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
3014 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003015 "bad superblock\n");
3016 brelse(bh);
3017 goto out_bdev;
3018 }
3019
Mingming Cao617ba132006-10-11 01:20:53 -07003020 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
3021 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003022 brelse(bh);
3023 goto out_bdev;
3024 }
3025
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003026 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027 start = sb_block + 1;
3028 brelse(bh); /* we're done with the superblock */
3029
Mingming Caodab291a2006-10-11 01:21:01 -07003030 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003031 start, len, blocksize);
3032 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07003033 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034 goto out_bdev;
3035 }
3036 journal->j_private = sb;
3037 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3038 wait_on_buffer(journal->j_sb_buffer);
3039 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003040 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041 goto out_journal;
3042 }
3043 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07003044 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003045 "user (unsupported) - %d\n",
3046 be32_to_cpu(journal->j_superblock->s_nr_users));
3047 goto out_journal;
3048 }
Mingming Cao617ba132006-10-11 01:20:53 -07003049 EXT4_SB(sb)->journal_bdev = bdev;
3050 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003051 return journal;
3052out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07003053 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07003055 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003056 return NULL;
3057}
3058
Mingming Cao617ba132006-10-11 01:20:53 -07003059static int ext4_load_journal(struct super_block *sb,
3060 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061 unsigned long journal_devnum)
3062{
3063 journal_t *journal;
3064 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3065 dev_t journal_dev;
3066 int err = 0;
3067 int really_read_only;
3068
Frank Mayhar03901312009-01-07 00:06:22 -05003069 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3070
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 if (journal_devnum &&
3072 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003073 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 "numbers have changed\n");
3075 journal_dev = new_decode_dev(journal_devnum);
3076 } else
3077 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3078
3079 really_read_only = bdev_read_only(sb->s_bdev);
3080
3081 /*
3082 * Are we loading a blank journal or performing recovery after a
3083 * crash? For recovery, we need to check in advance whether we
3084 * can get read-write access to the device.
3085 */
3086
Mingming Cao617ba132006-10-11 01:20:53 -07003087 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003089 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090 "required on readonly filesystem.\n");
3091 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07003092 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 "unavailable, cannot proceed.\n");
3094 return -EROFS;
3095 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003096 printk(KERN_INFO "EXT4-fs: write access will "
3097 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003098 }
3099 }
3100
3101 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07003102 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 "and inode journals!\n");
3104 return -EINVAL;
3105 }
3106
3107 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07003108 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003109 return -EINVAL;
3110 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003111 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 return -EINVAL;
3113 }
3114
Theodore Ts'o4776004f2008-09-08 23:00:52 -04003115 if (journal->j_flags & JBD2_BARRIER)
3116 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
3117 else
3118 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
3119
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07003121 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07003123 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07003124 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003125 return err;
3126 }
3127 }
3128
Mingming Cao617ba132006-10-11 01:20:53 -07003129 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07003130 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003131 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07003132 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003133
3134 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07003135 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07003136 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 return err;
3138 }
3139
Mingming Cao617ba132006-10-11 01:20:53 -07003140 EXT4_SB(sb)->s_journal = journal;
3141 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003142
3143 if (journal_devnum &&
3144 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3145 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003146
3147 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003148 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003149 }
3150
3151 return 0;
3152}
3153
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003154static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003155{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003156 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07003157 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003158 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159
3160 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08003161 return error;
Theodore Ts'o914258b2008-10-06 21:35:40 -04003162 if (buffer_write_io_error(sbh)) {
3163 /*
3164 * Oh, dear. A previous attempt to write the
3165 * superblock failed. This could happen because the
3166 * USB device was yanked out. Or it could happen to
3167 * be a transient write error and maybe the block will
3168 * be remapped. Nothing we can do but to retry the
3169 * write and hope for the best.
3170 */
Theodore Ts'oabda1412009-01-06 00:20:32 -05003171 printk(KERN_ERR "EXT4-fs: previous I/O error to "
Theodore Ts'o914258b2008-10-06 21:35:40 -04003172 "superblock detected for %s.\n", sb->s_id);
3173 clear_buffer_write_io_error(sbh);
3174 set_buffer_uptodate(sbh);
3175 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176 es->s_wtime = cpu_to_le32(get_seconds());
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003177 es->s_kbytes_written =
3178 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3179 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3180 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003181 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3182 &EXT4_SB(sb)->s_freeblocks_counter));
3183 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3184 &EXT4_SB(sb)->s_freeinodes_counter));
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003185 sb->s_dirt = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003186 BUFFER_TRACE(sbh, "marking dirty");
3187 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003188 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08003189 error = sync_dirty_buffer(sbh);
3190 if (error)
3191 return error;
3192
3193 error = buffer_write_io_error(sbh);
3194 if (error) {
Theodore Ts'oabda1412009-01-06 00:20:32 -05003195 printk(KERN_ERR "EXT4-fs: I/O error while writing "
Theodore Ts'o914258b2008-10-06 21:35:40 -04003196 "superblock for %s.\n", sb->s_id);
3197 clear_buffer_write_io_error(sbh);
3198 set_buffer_uptodate(sbh);
3199 }
3200 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08003201 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202}
3203
3204
3205/*
3206 * Have we just finished recovery? If so, and if we are mounting (or
3207 * remounting) the filesystem readonly, then we will end up with a
3208 * consistent fs on disk. Record that fact.
3209 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003210static void ext4_mark_recovery_complete(struct super_block *sb,
3211 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212{
Mingming Cao617ba132006-10-11 01:20:53 -07003213 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003214
Frank Mayhar03901312009-01-07 00:06:22 -05003215 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3216 BUG_ON(journal != NULL);
3217 return;
3218 }
Mingming Caodab291a2006-10-11 01:21:01 -07003219 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003220 if (jbd2_journal_flush(journal) < 0)
3221 goto out;
3222
Mingming Cao617ba132006-10-11 01:20:53 -07003223 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003224 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003225 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003226 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003228
3229out:
Mingming Caodab291a2006-10-11 01:21:01 -07003230 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231}
3232
3233/*
3234 * If we are mounting (or read-write remounting) a filesystem whose journal
3235 * has recorded an error from a previous lifetime, move that error to the
3236 * main filesystem now.
3237 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003238static void ext4_clear_journal_err(struct super_block *sb,
3239 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003240{
3241 journal_t *journal;
3242 int j_errno;
3243 const char *errstr;
3244
Frank Mayhar03901312009-01-07 00:06:22 -05003245 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3246
Mingming Cao617ba132006-10-11 01:20:53 -07003247 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248
3249 /*
3250 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003251 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252 */
3253
Mingming Caodab291a2006-10-11 01:21:01 -07003254 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003255 if (j_errno) {
3256 char nbuf[16];
3257
Mingming Cao617ba132006-10-11 01:20:53 -07003258 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003259 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003261 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003262 "filesystem check.");
3263
Mingming Cao617ba132006-10-11 01:20:53 -07003264 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3265 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003266 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267
Mingming Caodab291a2006-10-11 01:21:01 -07003268 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003269 }
3270}
3271
3272/*
3273 * Force the running and committing transactions to commit,
3274 * and wait on the commit.
3275 */
Mingming Cao617ba132006-10-11 01:20:53 -07003276int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003277{
3278 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003279 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280
3281 if (sb->s_flags & MS_RDONLY)
3282 return 0;
3283
Mingming Cao617ba132006-10-11 01:20:53 -07003284 journal = EXT4_SB(sb)->s_journal;
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003285 if (journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003286 ret = ext4_journal_force_commit(journal);
Frank Mayhar03901312009-01-07 00:06:22 -05003287
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003288 return ret;
3289}
3290
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003291static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003293 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294}
3295
Mingming Cao617ba132006-10-11 01:20:53 -07003296static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003297{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003298 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05003299 tid_t target;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003300
Theodore Ts'oede86cc2008-10-05 20:50:06 -04003301 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003302 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
3303 if (wait)
3304 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Frank Mayhar03901312009-01-07 00:06:22 -05003305 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003306 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307}
3308
3309/*
3310 * LVM calls this function before a (read-only) snapshot is created. This
3311 * gives us a chance to flush the journal completely and mark the fs clean.
3312 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003313static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003314{
Takashi Satoc4be0c12009-01-09 16:40:58 -08003315 int error = 0;
3316 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003317
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003318 if (sb->s_flags & MS_RDONLY)
3319 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003320
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003321 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003322
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003323 /* Now we set up the journal barrier. */
3324 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003325
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003326 /*
3327 * Don't clear the needs_recovery flag if we failed to flush
3328 * the journal.
3329 */
3330 error = jbd2_journal_flush(journal);
3331 if (error < 0) {
3332 out:
3333 jbd2_journal_unlock_updates(journal);
3334 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003335 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003336
3337 /* Journal blocked and flushed, clear needs_recovery flag. */
3338 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3339 error = ext4_commit_super(sb, 1);
3340 if (error)
3341 goto out;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003342 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343}
3344
3345/*
3346 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3347 * flag here, even though the filesystem is not technically dirty yet.
3348 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003349static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003350{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003351 if (sb->s_flags & MS_RDONLY)
3352 return 0;
3353
3354 lock_super(sb);
3355 /* Reset the needs_recovery flag before the fs is unlocked. */
3356 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3357 ext4_commit_super(sb, 1);
3358 unlock_super(sb);
3359 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Takashi Satoc4be0c12009-01-09 16:40:58 -08003360 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003361}
3362
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003363static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003364{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003365 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003366 struct ext4_sb_info *sbi = EXT4_SB(sb);
3367 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003368 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003369 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003370 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003371 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003372 int err;
3373#ifdef CONFIG_QUOTA
3374 int i;
3375#endif
3376
3377 /* Store the original options */
3378 old_sb_flags = sb->s_flags;
3379 old_opts.s_mount_opt = sbi->s_mount_opt;
3380 old_opts.s_resuid = sbi->s_resuid;
3381 old_opts.s_resgid = sbi->s_resgid;
3382 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003383 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3384 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003385#ifdef CONFIG_QUOTA
3386 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3387 for (i = 0; i < MAXQUOTAS; i++)
3388 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3389#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003390 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3391 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392
3393 /*
3394 * Allow the "check" option to be passed as a remount option.
3395 */
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003396 if (!parse_options(data, sb, NULL, &journal_ioprio,
3397 &n_blocks_count, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398 err = -EINVAL;
3399 goto restore_opts;
3400 }
3401
Mingming Cao617ba132006-10-11 01:20:53 -07003402 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003403 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003404
3405 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003406 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003407
3408 es = sbi->s_es;
3409
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003410 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05003411 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003412 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3413 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003414
3415 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003416 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003417 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003418 err = -EROFS;
3419 goto restore_opts;
3420 }
3421
3422 if (*flags & MS_RDONLY) {
3423 /*
3424 * First of all, the unconditional stuff we have to do
3425 * to disable replay of the journal when we next remount
3426 */
3427 sb->s_flags |= MS_RDONLY;
3428
3429 /*
3430 * OK, test if we are remounting a valid rw partition
3431 * readonly, and if so set the rdonly flag and then
3432 * mark the partition as valid again.
3433 */
Mingming Cao617ba132006-10-11 01:20:53 -07003434 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3435 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003436 es->s_state = cpu_to_le16(sbi->s_mount_state);
3437
Theodore Ts'oa63c9eb2009-05-01 01:59:42 -04003438 if (sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003439 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003440 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003441 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003442 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3443 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3444 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003445 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003446 "optional features (%x).\n", sb->s_id,
3447 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3448 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 err = -EROFS;
3450 goto restore_opts;
3451 }
Eric Sandeenead65962007-02-10 01:46:08 -08003452
3453 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003454 * Make sure the group descriptor checksums
3455 * are sane. If they aren't, refuse to
3456 * remount r/w.
3457 */
3458 for (g = 0; g < sbi->s_groups_count; g++) {
3459 struct ext4_group_desc *gdp =
3460 ext4_get_group_desc(sb, g, NULL);
3461
3462 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3463 printk(KERN_ERR
3464 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003465 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003466 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3467 le16_to_cpu(gdp->bg_checksum));
3468 err = -EINVAL;
3469 goto restore_opts;
3470 }
3471 }
3472
3473 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003474 * If we have an unprocessed orphan list hanging
3475 * around from a previously readonly bdev mount,
3476 * require a full umount/remount for now.
3477 */
3478 if (es->s_last_orphan) {
3479 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3480 "remount RDWR because of unprocessed "
3481 "orphan inode list. Please "
3482 "umount/remount instead.\n",
3483 sb->s_id);
3484 err = -EINVAL;
3485 goto restore_opts;
3486 }
3487
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003488 /*
3489 * Mounting a RDONLY partition read-write, so reread
3490 * and store the current valid flag. (It may have
3491 * been changed by e2fsck since we originally mounted
3492 * the partition.)
3493 */
Frank Mayhar03901312009-01-07 00:06:22 -05003494 if (sbi->s_journal)
3495 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003496 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003497 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003498 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003499 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003500 sb->s_flags &= ~MS_RDONLY;
3501 }
3502 }
Frank Mayhar03901312009-01-07 00:06:22 -05003503 if (sbi->s_journal == NULL)
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003504 ext4_commit_super(sb, 1);
Frank Mayhar03901312009-01-07 00:06:22 -05003505
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003506#ifdef CONFIG_QUOTA
3507 /* Release old quota file names */
3508 for (i = 0; i < MAXQUOTAS; i++)
3509 if (old_opts.s_qf_names[i] &&
3510 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3511 kfree(old_opts.s_qf_names[i]);
3512#endif
3513 return 0;
3514restore_opts:
3515 sb->s_flags = old_sb_flags;
3516 sbi->s_mount_opt = old_opts.s_mount_opt;
3517 sbi->s_resuid = old_opts.s_resuid;
3518 sbi->s_resgid = old_opts.s_resgid;
3519 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003520 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3521 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522#ifdef CONFIG_QUOTA
3523 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3524 for (i = 0; i < MAXQUOTAS; i++) {
3525 if (sbi->s_qf_names[i] &&
3526 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3527 kfree(sbi->s_qf_names[i]);
3528 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3529 }
3530#endif
3531 return err;
3532}
3533
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003534static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003535{
3536 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003537 struct ext4_sb_info *sbi = EXT4_SB(sb);
3538 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003539 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003540
Badari Pulavarty5e700302007-07-15 23:42:00 -07003541 if (test_opt(sb, MINIX_DF)) {
3542 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003543 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04003544 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003545 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546
3547 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003548 * Compute the overhead (FS structures). This is constant
3549 * for a given filesystem unless the number of block groups
3550 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003551 */
3552
3553 /*
3554 * All of the blocks before first_data_block are
3555 * overhead
3556 */
3557 overhead = le32_to_cpu(es->s_first_data_block);
3558
3559 /*
3560 * Add the overhead attributed to the superblock and
3561 * block group descriptors. If the sparse superblocks
3562 * feature is turned on, then not all groups have this.
3563 */
3564 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003565 overhead += ext4_bg_has_super(sb, i) +
3566 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003567 cond_resched();
3568 }
3569
3570 /*
3571 * Every block group has an inode bitmap, a block
3572 * bitmap, and an inode table.
3573 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003574 overhead += ngroups * (2 + sbi->s_itb_per_group);
3575 sbi->s_overhead_last = overhead;
3576 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003577 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003578 }
3579
Mingming Cao617ba132006-10-11 01:20:53 -07003580 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003581 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003582 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003583 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3584 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003585 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003586 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3587 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003588 buf->f_bavail = 0;
3589 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003590 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003591 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003592 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003593 fsid = le64_to_cpup((void *)es->s_uuid) ^
3594 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3595 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3596 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003597 return 0;
3598}
3599
3600/* Helper function for writing quotas on sync - we need to start transaction before quota file
3601 * is locked for write. Otherwise the are possible deadlocks:
3602 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003603 * ext4_create() quota_sync()
Jan Karaa269eb12009-01-26 17:04:39 +01003604 * jbd2_journal_start() write_dquot()
3605 * vfs_dq_init() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003606 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003607 *
3608 */
3609
3610#ifdef CONFIG_QUOTA
3611
3612static inline struct inode *dquot_to_inode(struct dquot *dquot)
3613{
3614 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3615}
3616
Mingming Cao617ba132006-10-11 01:20:53 -07003617static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003618{
3619 int ret, err;
3620 handle_t *handle;
3621 struct inode *inode;
3622
3623 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003624 handle = ext4_journal_start(inode,
3625 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003626 if (IS_ERR(handle))
3627 return PTR_ERR(handle);
3628 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003629 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003630 if (!ret)
3631 ret = err;
3632 return ret;
3633}
3634
Mingming Cao617ba132006-10-11 01:20:53 -07003635static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003636{
3637 int ret, err;
3638 handle_t *handle;
3639
Mingming Cao617ba132006-10-11 01:20:53 -07003640 handle = ext4_journal_start(dquot_to_inode(dquot),
3641 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003642 if (IS_ERR(handle))
3643 return PTR_ERR(handle);
3644 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003645 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003646 if (!ret)
3647 ret = err;
3648 return ret;
3649}
3650
Mingming Cao617ba132006-10-11 01:20:53 -07003651static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003652{
3653 int ret, err;
3654 handle_t *handle;
3655
Mingming Cao617ba132006-10-11 01:20:53 -07003656 handle = ext4_journal_start(dquot_to_inode(dquot),
3657 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003658 if (IS_ERR(handle)) {
3659 /* Release dquot anyway to avoid endless cycle in dqput() */
3660 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003661 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003662 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003663 ret = dquot_release(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_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003671{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003672 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003673 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3674 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003675 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003676 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003677 } else {
3678 return dquot_mark_dquot_dirty(dquot);
3679 }
3680}
3681
Mingming Cao617ba132006-10-11 01:20:53 -07003682static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683{
3684 int ret, err;
3685 handle_t *handle;
3686
3687 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003688 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003689 if (IS_ERR(handle))
3690 return PTR_ERR(handle);
3691 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003692 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003693 if (!ret)
3694 ret = err;
3695 return ret;
3696}
3697
3698/*
3699 * Turn on quotas during mount time - we need to find
3700 * the quota file and such...
3701 */
Mingming Cao617ba132006-10-11 01:20:53 -07003702static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003703{
Mingming Cao617ba132006-10-11 01:20:53 -07003704 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3705 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003706}
3707
3708/*
3709 * Standard function to be called on quota_on
3710 */
Mingming Cao617ba132006-10-11 01:20:53 -07003711static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003712 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003713{
3714 int err;
Al Viro82646132008-08-02 00:57:06 -04003715 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003716
3717 if (!test_opt(sb, QUOTA))
3718 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003719 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003720 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003721 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003722
Al Viro82646132008-08-02 00:57:06 -04003723 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003724 if (err)
3725 return err;
Jan Kara06235432008-05-13 19:11:51 -04003726
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003727 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003728 if (path.mnt->mnt_sb != sb) {
3729 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003730 return -EXDEV;
3731 }
Jan Kara06235432008-05-13 19:11:51 -04003732 /* Journaling quota? */
3733 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003734 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003735 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003736 printk(KERN_WARNING
3737 "EXT4-fs: Quota file not on filesystem root. "
3738 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003739 }
Jan Kara06235432008-05-13 19:11:51 -04003740
3741 /*
3742 * When we journal data on quota file, we have to flush journal to see
3743 * all updates to the file when we bypass pagecache...
3744 */
Frank Mayhar03901312009-01-07 00:06:22 -05003745 if (EXT4_SB(sb)->s_journal &&
3746 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003747 /*
3748 * We don't need to lock updates but journal_flush() could
3749 * otherwise be livelocked...
3750 */
3751 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003752 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003753 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003754 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003755 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003756 return err;
3757 }
Jan Kara06235432008-05-13 19:11:51 -04003758 }
3759
Al Viro82646132008-08-02 00:57:06 -04003760 err = vfs_quota_on_path(sb, type, format_id, &path);
3761 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003762 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003763}
3764
3765/* Read data from quotafile - avoid pagecache and such because we cannot afford
3766 * acquiring the locks... As quota files are never truncated and quota code
3767 * itself serializes the operations (and noone else should touch the files)
3768 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003769static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770 size_t len, loff_t off)
3771{
3772 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003773 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003774 int err = 0;
3775 int offset = off & (sb->s_blocksize - 1);
3776 int tocopy;
3777 size_t toread;
3778 struct buffer_head *bh;
3779 loff_t i_size = i_size_read(inode);
3780
3781 if (off > i_size)
3782 return 0;
3783 if (off+len > i_size)
3784 len = i_size-off;
3785 toread = len;
3786 while (toread > 0) {
3787 tocopy = sb->s_blocksize - offset < toread ?
3788 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003789 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003790 if (err)
3791 return err;
3792 if (!bh) /* A hole? */
3793 memset(data, 0, tocopy);
3794 else
3795 memcpy(data, bh->b_data+offset, tocopy);
3796 brelse(bh);
3797 offset = 0;
3798 toread -= tocopy;
3799 data += tocopy;
3800 blk++;
3801 }
3802 return len;
3803}
3804
3805/* Write to quotafile (we know the transaction is already started and has
3806 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003807static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003808 const char *data, size_t len, loff_t off)
3809{
3810 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003811 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003812 int err = 0;
3813 int offset = off & (sb->s_blocksize - 1);
3814 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003815 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003816 size_t towrite = len;
3817 struct buffer_head *bh;
3818 handle_t *handle = journal_current_handle();
3819
Frank Mayhar03901312009-01-07 00:06:22 -05003820 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003821 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003822 " cancelled because transaction is not started.\n",
3823 (unsigned long long)off, (unsigned long long)len);
3824 return -EIO;
3825 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003826 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3827 while (towrite > 0) {
3828 tocopy = sb->s_blocksize - offset < towrite ?
3829 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003830 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003831 if (!bh)
3832 goto out;
3833 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003834 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003835 if (err) {
3836 brelse(bh);
3837 goto out;
3838 }
3839 }
3840 lock_buffer(bh);
3841 memcpy(bh->b_data+offset, data, tocopy);
3842 flush_dcache_page(bh->b_page);
3843 unlock_buffer(bh);
3844 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003845 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003846 else {
3847 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003848 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003849 mark_buffer_dirty(bh);
3850 }
3851 brelse(bh);
3852 if (err)
3853 goto out;
3854 offset = 0;
3855 towrite -= tocopy;
3856 data += tocopy;
3857 blk++;
3858 }
3859out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003860 if (len == towrite) {
3861 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003862 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003863 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003864 if (inode->i_size < off+len-towrite) {
3865 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003866 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003868 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003869 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003870 mutex_unlock(&inode->i_mutex);
3871 return len - towrite;
3872}
3873
3874#endif
3875
Mingming Cao617ba132006-10-11 01:20:53 -07003876static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003877 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3878{
Mingming Cao617ba132006-10-11 01:20:53 -07003879 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003880}
3881
Theodore Ts'o03010a32008-10-10 20:02:48 -04003882static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003883 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003884 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003885 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003886 .kill_sb = kill_block_super,
3887 .fs_flags = FS_REQUIRES_DEV,
3888};
3889
Theodore Ts'o03010a32008-10-10 20:02:48 -04003890#ifdef CONFIG_EXT4DEV_COMPAT
3891static int ext4dev_get_sb(struct file_system_type *fs_type,
3892 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3893{
3894 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3895 "to mount using ext4\n");
3896 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3897 "will go away by 2.6.31\n");
3898 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3899}
3900
3901static struct file_system_type ext4dev_fs_type = {
3902 .owner = THIS_MODULE,
3903 .name = "ext4dev",
3904 .get_sb = ext4dev_get_sb,
3905 .kill_sb = kill_block_super,
3906 .fs_flags = FS_REQUIRES_DEV,
3907};
3908MODULE_ALIAS("ext4dev");
3909#endif
3910
Mingming Cao617ba132006-10-11 01:20:53 -07003911static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912{
Alex Tomasc9de5602008-01-29 00:19:52 -05003913 int err;
3914
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04003915 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
3916 if (!ext4_kset)
3917 return -ENOMEM;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003918 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003919 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003920 if (err)
3921 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003922
3923 err = init_ext4_xattr();
3924 if (err)
3925 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926 err = init_inodecache();
3927 if (err)
3928 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003929 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003930 if (err)
3931 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003932#ifdef CONFIG_EXT4DEV_COMPAT
3933 err = register_filesystem(&ext4dev_fs_type);
3934 if (err) {
3935 unregister_filesystem(&ext4_fs_type);
3936 goto out;
3937 }
3938#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003939 return 0;
3940out:
3941 destroy_inodecache();
3942out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003943 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003944out2:
3945 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 return err;
3947}
3948
Mingming Cao617ba132006-10-11 01:20:53 -07003949static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003950{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003951 unregister_filesystem(&ext4_fs_type);
3952#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003953 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003954#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003955 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003956 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003957 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003958 remove_proc_entry("fs/ext4", NULL);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04003959 kset_unregister(ext4_kset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003960}
3961
3962MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05003963MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003965module_init(init_ext4_fs)
3966module_exit(exit_ext4_fs)