blob: ce84a6ed4a48fed92956bfa73638878335d0fecf [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>
Vignesh Babu13305932007-07-18 09:11:02 -040040#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040041#include <linux/crc16.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070042#include <asm/uaccess.h>
43
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040044#include "ext4.h"
45#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046#include "xattr.h"
47#include "acl.h"
Theodore Ts'o3661d282009-09-14 22:59:50 -040048#include "mballoc.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Theodore Ts'o9bffad12009-06-17 11:48:11 -040050#define CREATE_TRACE_POINTS
51#include <trace/events/ext4.h>
52
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040053struct proc_dir_entry *ext4_proc_root;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040054static struct kset *ext4_kset;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040055
Mingming Cao617ba132006-10-11 01:20:53 -070056static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057 unsigned long journal_devnum);
Theodore Ts'oe2d67052009-05-01 00:33:44 -040058static int ext4_commit_super(struct super_block *sb, int sync);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040059static void ext4_mark_recovery_complete(struct super_block *sb,
60 struct ext4_super_block *es);
61static void ext4_clear_journal_err(struct super_block *sb,
62 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070063static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040064static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070065 char nbuf[16]);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040066static int ext4_remount(struct super_block *sb, int *flags, char *data);
67static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080068static int ext4_unfreeze(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040069static void ext4_write_super(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080070static int ext4_freeze(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070071
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070072
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070073ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
74 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070075{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040076 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070077 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -040078 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070079}
80
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070081ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
82 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070083{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040084 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070085 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -040086 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070087}
88
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070089ext4_fsblk_t ext4_inode_table(struct super_block *sb,
90 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070091{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040092 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070093 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -040094 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070095}
96
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -050097__u32 ext4_free_blks_count(struct super_block *sb,
98 struct ext4_group_desc *bg)
99{
100 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
101 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400102 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500103}
104
105__u32 ext4_free_inodes_count(struct super_block *sb,
106 struct ext4_group_desc *bg)
107{
108 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
109 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400110 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500111}
112
113__u32 ext4_used_dirs_count(struct super_block *sb,
114 struct ext4_group_desc *bg)
115{
116 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
117 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400118 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500119}
120
121__u32 ext4_itable_unused_count(struct super_block *sb,
122 struct ext4_group_desc *bg)
123{
124 return le16_to_cpu(bg->bg_itable_unused_lo) |
125 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400126 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500127}
128
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700129void ext4_block_bitmap_set(struct super_block *sb,
130 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700131{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400132 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700133 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
134 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700135}
136
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700137void ext4_inode_bitmap_set(struct super_block *sb,
138 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700139{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400140 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700141 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
142 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700143}
144
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700145void ext4_inode_table_set(struct super_block *sb,
146 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700147{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400148 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700149 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
150 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700151}
152
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500153void ext4_free_blks_set(struct super_block *sb,
154 struct ext4_group_desc *bg, __u32 count)
155{
156 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
157 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
158 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
159}
160
161void ext4_free_inodes_set(struct super_block *sb,
162 struct ext4_group_desc *bg, __u32 count)
163{
164 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
165 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
166 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
167}
168
169void ext4_used_dirs_set(struct super_block *sb,
170 struct ext4_group_desc *bg, __u32 count)
171{
172 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
173 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
174 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
175}
176
177void ext4_itable_unused_set(struct super_block *sb,
178 struct ext4_group_desc *bg, __u32 count)
179{
180 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
181 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
182 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
183}
184
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400185
186/* Just increment the non-pointer handle value */
187static handle_t *ext4_get_nojournal(void)
188{
189 handle_t *handle = current->journal_info;
190 unsigned long ref_cnt = (unsigned long)handle;
191
192 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
193
194 ref_cnt++;
195 handle = (handle_t *)ref_cnt;
196
197 current->journal_info = handle;
198 return handle;
199}
200
201
202/* Decrement the non-pointer handle value */
203static void ext4_put_nojournal(handle_t *handle)
204{
205 unsigned long ref_cnt = (unsigned long)handle;
206
207 BUG_ON(ref_cnt == 0);
208
209 ref_cnt--;
210 handle = (handle_t *)ref_cnt;
211
212 current->journal_info = handle;
213}
214
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700215/*
Mingming Caodab291a2006-10-11 01:21:01 -0700216 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 *
218 * The only special thing we need to do here is to make sure that all
219 * journal_end calls result in the superblock being marked dirty, so
220 * that sync() will call the filesystem's write_super callback if
221 * appropriate.
222 */
Mingming Cao617ba132006-10-11 01:20:53 -0700223handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700224{
225 journal_t *journal;
226
227 if (sb->s_flags & MS_RDONLY)
228 return ERR_PTR(-EROFS);
229
230 /* Special case here: if the journal has aborted behind our
231 * backs (eg. EIO in the commit thread), then we still need to
232 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700233 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500234 if (journal) {
235 if (is_journal_aborted(journal)) {
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400236 ext4_abort(sb, __func__, "Detected aborted journal");
Frank Mayhar03901312009-01-07 00:06:22 -0500237 return ERR_PTR(-EROFS);
238 }
239 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700240 }
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400241 return ext4_get_nojournal();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700242}
243
244/*
245 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700246 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700247 * that sync() will call the filesystem's write_super callback if
248 * appropriate.
249 */
Mingming Cao617ba132006-10-11 01:20:53 -0700250int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700251{
252 struct super_block *sb;
253 int err;
254 int rc;
255
Frank Mayhar03901312009-01-07 00:06:22 -0500256 if (!ext4_handle_valid(handle)) {
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400257 ext4_put_nojournal(handle);
Frank Mayhar03901312009-01-07 00:06:22 -0500258 return 0;
259 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700260 sb = handle->h_transaction->t_journal->j_private;
261 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700262 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700263
264 if (!err)
265 err = rc;
266 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700267 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700268 return err;
269}
270
Mingming Cao617ba132006-10-11 01:20:53 -0700271void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700272 struct buffer_head *bh, handle_t *handle, int err)
273{
274 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700275 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700276
Frank Mayhar03901312009-01-07 00:06:22 -0500277 BUG_ON(!ext4_handle_valid(handle));
278
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700279 if (bh)
280 BUFFER_TRACE(bh, "abort");
281
282 if (!handle->h_err)
283 handle->h_err = err;
284
285 if (is_handle_aborted(handle))
286 return;
287
288 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
289 caller, errstr, err_fn);
290
Mingming Caodab291a2006-10-11 01:21:01 -0700291 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700292}
293
294/* Deal with the reporting of failure conditions on a filesystem such as
295 * inconsistencies detected or read IO failures.
296 *
297 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700298 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299 * write ordering constraints on the superblock which prevent us from
300 * writing it out straight away; and given that the journal is about to
301 * be aborted, we can't rely on the current, or future, transactions to
302 * write out the superblock safely.
303 *
Mingming Caodab291a2006-10-11 01:21:01 -0700304 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 * the journal instead. On recovery, the journal will compain about
306 * that error until we've noted it down and cleared it.
307 */
308
Mingming Cao617ba132006-10-11 01:20:53 -0700309static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700310{
Mingming Cao617ba132006-10-11 01:20:53 -0700311 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700312
Mingming Cao617ba132006-10-11 01:20:53 -0700313 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
314 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315
316 if (sb->s_flags & MS_RDONLY)
317 return;
318
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400319 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700320 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321
Theodore Ts'o4ab2f152009-06-13 10:09:36 -0400322 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700324 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700325 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400326 if (test_opt(sb, ERRORS_RO)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400327 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328 sb->s_flags |= MS_RDONLY;
329 }
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400330 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700331 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700332 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700333 sb->s_id);
334}
335
Eric Sandeen12062dd2010-02-15 14:19:27 -0500336void __ext4_error(struct super_block *sb, const char *function,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400337 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338{
339 va_list args;
340
341 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400342 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
346
Mingming Cao617ba132006-10-11 01:20:53 -0700347 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700348}
349
Frank Mayhar273df552010-03-02 11:46:09 -0500350void ext4_error_inode(const char *function, struct inode *inode,
351 const char *fmt, ...)
352{
353 va_list args;
354
355 va_start(args, fmt);
356 printk(KERN_CRIT "EXT4-fs error (device %s): %s: inode #%lu: (comm %s) ",
357 inode->i_sb->s_id, function, inode->i_ino, current->comm);
358 vprintk(fmt, args);
359 printk("\n");
360 va_end(args);
361
362 ext4_handle_error(inode->i_sb);
363}
364
365void ext4_error_file(const char *function, struct file *file,
366 const char *fmt, ...)
367{
368 va_list args;
369 struct inode *inode = file->f_dentry->d_inode;
370 char pathname[80], *path;
371
372 va_start(args, fmt);
373 path = d_path(&(file->f_path), pathname, sizeof(pathname));
374 if (!path)
375 path = "(unknown)";
376 printk(KERN_CRIT
377 "EXT4-fs error (device %s): %s: inode #%lu (comm %s path %s): ",
378 inode->i_sb->s_id, function, inode->i_ino, current->comm, path);
379 vprintk(fmt, args);
380 printk("\n");
381 va_end(args);
382
383 ext4_handle_error(inode->i_sb);
384}
385
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400386static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700387 char nbuf[16])
388{
389 char *errstr = NULL;
390
391 switch (errno) {
392 case -EIO:
393 errstr = "IO failure";
394 break;
395 case -ENOMEM:
396 errstr = "Out of memory";
397 break;
398 case -EROFS:
Theodore Ts'o78f1ddb2009-07-27 23:09:47 -0400399 if (!sb || (EXT4_SB(sb)->s_journal &&
400 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401 errstr = "Journal has aborted";
402 else
403 errstr = "Readonly filesystem";
404 break;
405 default:
406 /* If the caller passed in an extra buffer for unknown
407 * errors, textualise them now. Else we just return
408 * NULL. */
409 if (nbuf) {
410 /* Check for truncated error codes... */
411 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
412 errstr = nbuf;
413 }
414 break;
415 }
416
417 return errstr;
418}
419
Mingming Cao617ba132006-10-11 01:20:53 -0700420/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421 * automatically and invokes the appropriate error response. */
422
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400423void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700424{
425 char nbuf[16];
426 const char *errstr;
427
428 /* Special case: if the error is EROFS, and we're not already
429 * inside a transaction, then there's really no point in logging
430 * an error. */
431 if (errno == -EROFS && journal_current_handle() == NULL &&
432 (sb->s_flags & MS_RDONLY))
433 return;
434
Mingming Cao617ba132006-10-11 01:20:53 -0700435 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400436 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
437 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700438
Mingming Cao617ba132006-10-11 01:20:53 -0700439 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700440}
441
442/*
Mingming Cao617ba132006-10-11 01:20:53 -0700443 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700444 * abort function may be used to deal with unrecoverable failures such
445 * as journal IO errors or ENOMEM at a critical moment in log management.
446 *
447 * We unconditionally force the filesystem into an ABORT|READONLY state,
448 * unless the error response on the fs has been set to panic in which
449 * case we take the easy way out and panic immediately.
450 */
451
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400452void ext4_abort(struct super_block *sb, const char *function,
453 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700454{
455 va_list args;
456
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700457 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400458 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700459 vprintk(fmt, args);
460 printk("\n");
461 va_end(args);
462
463 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700464 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465
466 if (sb->s_flags & MS_RDONLY)
467 return;
468
Eric Sandeenb31e1552009-06-04 17:36:36 -0400469 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Mingming Cao617ba132006-10-11 01:20:53 -0700470 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 sb->s_flags |= MS_RDONLY;
Theodore Ts'o4ab2f152009-06-13 10:09:36 -0400472 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400473 if (EXT4_SB(sb)->s_journal)
474 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700475}
476
Eric Sandeenb31e1552009-06-04 17:36:36 -0400477void ext4_msg (struct super_block * sb, const char *prefix,
478 const char *fmt, ...)
479{
480 va_list args;
481
482 va_start(args, fmt);
483 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
484 vprintk(fmt, args);
485 printk("\n");
486 va_end(args);
487}
488
Eric Sandeen12062dd2010-02-15 14:19:27 -0500489void __ext4_warning(struct super_block *sb, const char *function,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400490 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700491{
492 va_list args;
493
494 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700495 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 sb->s_id, function);
497 vprintk(fmt, args);
498 printk("\n");
499 va_end(args);
500}
501
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500502void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400503 const char *function, const char *fmt, ...)
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500504__releases(bitlock)
505__acquires(bitlock)
506{
507 va_list args;
508 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
509
510 va_start(args, fmt);
511 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
512 vprintk(fmt, args);
513 printk("\n");
514 va_end(args);
515
516 if (test_opt(sb, ERRORS_CONT)) {
517 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
518 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400519 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500520 return;
521 }
522 ext4_unlock_group(sb, grp);
523 ext4_handle_error(sb);
524 /*
525 * We only get here in the ERRORS_RO case; relocking the group
526 * may be dangerous, but nothing bad will happen since the
527 * filesystem will have already been marked read/only and the
528 * journal has been aborted. We return 1 as a hint to callers
529 * who might what to use the return value from
530 * ext4_grp_locked_error() to distinguish beween the
531 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
532 * aggressively from the ext4 function in question, with a
533 * more appropriate error code.
534 */
535 ext4_lock_group(sb, grp);
536 return;
537}
538
Mingming Cao617ba132006-10-11 01:20:53 -0700539void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540{
Mingming Cao617ba132006-10-11 01:20:53 -0700541 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700542
Mingming Cao617ba132006-10-11 01:20:53 -0700543 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 return;
545
Eric Sandeen12062dd2010-02-15 14:19:27 -0500546 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 "updating to rev %d because of new feature flag, "
548 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700549 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550
Mingming Cao617ba132006-10-11 01:20:53 -0700551 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
552 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
553 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554 /* leave es->s_feature_*compat flags alone */
555 /* es->s_uuid will be set by e2fsck if empty */
556
557 /*
558 * The rest of the superblock fields should be zero, and if not it
559 * means they are likely already in use, so leave them alone. We
560 * can leave it up to e2fsck to clean up any inconsistencies there.
561 */
562}
563
564/*
565 * Open the external journal device
566 */
Eric Sandeenb31e1552009-06-04 17:36:36 -0400567static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568{
569 struct block_device *bdev;
570 char b[BDEVNAME_SIZE];
571
572 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
573 if (IS_ERR(bdev))
574 goto fail;
575 return bdev;
576
577fail:
Eric Sandeenb31e1552009-06-04 17:36:36 -0400578 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700579 __bdevname(dev, b), PTR_ERR(bdev));
580 return NULL;
581}
582
583/*
584 * Release the journal device
585 */
Mingming Cao617ba132006-10-11 01:20:53 -0700586static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587{
588 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500589 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590}
591
Mingming Cao617ba132006-10-11 01:20:53 -0700592static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
594 struct block_device *bdev;
595 int ret = -ENODEV;
596
597 bdev = sbi->journal_bdev;
598 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700599 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600 sbi->journal_bdev = NULL;
601 }
602 return ret;
603}
604
605static inline struct inode *orphan_list_entry(struct list_head *l)
606{
Mingming Cao617ba132006-10-11 01:20:53 -0700607 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608}
609
Mingming Cao617ba132006-10-11 01:20:53 -0700610static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700611{
612 struct list_head *l;
613
Eric Sandeenb31e1552009-06-04 17:36:36 -0400614 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
615 le32_to_cpu(sbi->s_es->s_last_orphan));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700616
617 printk(KERN_ERR "sb_info orphan list:\n");
618 list_for_each(l, &sbi->s_orphan) {
619 struct inode *inode = orphan_list_entry(l);
620 printk(KERN_ERR " "
621 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
622 inode->i_sb->s_id, inode->i_ino, inode,
623 inode->i_mode, inode->i_nlink,
624 NEXT_ORPHAN(inode));
625 }
626}
627
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400628static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700629{
Mingming Cao617ba132006-10-11 01:20:53 -0700630 struct ext4_sb_info *sbi = EXT4_SB(sb);
631 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400632 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700633
Mingming Cao4c0425f2009-09-28 15:48:41 -0400634 flush_workqueue(sbi->dio_unwritten_wq);
635 destroy_workqueue(sbi->dio_unwritten_wq);
636
Al Viroa9e220f2009-05-05 22:10:44 -0400637 lock_super(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200638 lock_kernel();
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200639 if (sb->s_dirt)
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200640 ext4_commit_super(sb, 1);
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200641
Frank Mayhar03901312009-01-07 00:06:22 -0500642 if (sbi->s_journal) {
643 err = jbd2_journal_destroy(sbi->s_journal);
644 sbi->s_journal = NULL;
645 if (err < 0)
646 ext4_abort(sb, __func__,
647 "Couldn't clean up the journal");
648 }
Josef Bacikd4edac32009-12-08 21:48:58 -0500649
650 ext4_release_system_zone(sb);
651 ext4_mb_release(sb);
652 ext4_ext_release(sb);
653 ext4_xattr_put_super(sb);
654
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700656 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657 es->s_state = cpu_to_le16(sbi->s_mount_state);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400658 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700659 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400660 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400661 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400662 }
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400663 kobject_del(&sbi->s_kobj);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664
665 for (i = 0; i < sbi->s_gdb_count; i++)
666 brelse(sbi->s_group_desc[i]);
667 kfree(sbi->s_group_desc);
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -0400668 if (is_vmalloc_addr(sbi->s_flex_groups))
669 vfree(sbi->s_flex_groups);
670 else
671 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 percpu_counter_destroy(&sbi->s_freeblocks_counter);
673 percpu_counter_destroy(&sbi->s_freeinodes_counter);
674 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400675 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676 brelse(sbi->s_sbh);
677#ifdef CONFIG_QUOTA
678 for (i = 0; i < MAXQUOTAS; i++)
679 kfree(sbi->s_qf_names[i]);
680#endif
681
682 /* Debugging code just in case the in-memory inode orphan list
683 * isn't empty. The on-disk one can be non-empty if we've
684 * detected an error and taken the fs readonly, but the
685 * in-memory list had better be clean by this point. */
686 if (!list_empty(&sbi->s_orphan))
687 dump_orphan_list(sb, sbi);
688 J_ASSERT(list_empty(&sbi->s_orphan));
689
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700690 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
692 /*
693 * Invalidate the journal device's buffers. We don't want them
694 * floating about in memory - the physical journal device may
695 * hotswapped, and it breaks the `ro-after' testing code.
696 */
697 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700698 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700699 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700 }
701 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400702 /*
703 * Now that we are completely done shutting down the
704 * superblock, we need to actually destroy the kobject.
705 */
706 unlock_kernel();
707 unlock_super(sb);
708 kobject_put(&sbi->s_kobj);
709 wait_for_completion(&sbi->s_kobj_unregister);
Pekka Enberg705895b2009-02-15 18:07:52 -0500710 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711 kfree(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700712}
713
Christoph Lametere18b8902006-12-06 20:33:20 -0800714static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700715
716/*
717 * Called inside transaction, so use GFP_NOFS
718 */
Mingming Cao617ba132006-10-11 01:20:53 -0700719static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720{
Mingming Cao617ba132006-10-11 01:20:53 -0700721 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700722
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800723 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700724 if (!ei)
725 return NULL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400726
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400728 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700729 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500730 INIT_LIST_HEAD(&ei->i_prealloc_list);
731 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500732 /*
733 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
734 * therefore it can be null here. Don't check it, just initialize
735 * jinode.
736 */
Jan Kara678aaf42008-07-11 19:27:31 -0400737 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400738 ei->i_reserved_data_blocks = 0;
739 ei->i_reserved_meta_blocks = 0;
740 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500741 ei->i_da_metadata_calc_len = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400742 ei->i_delalloc_reserved_flag = 0;
743 spin_lock_init(&(ei->i_block_reservation_lock));
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300744#ifdef CONFIG_QUOTA
745 ei->i_reserved_quota = 0;
746#endif
Jiaying Zhangc7064ef2010-03-02 13:28:44 -0500747 INIT_LIST_HEAD(&ei->i_completed_io_list);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500748 spin_lock_init(&ei->i_completed_io_lock);
Mingming Cao8d5d02e2009-09-28 15:48:29 -0400749 ei->cur_aio_dio = NULL;
Jan Karab436b9b2009-12-08 23:51:10 -0500750 ei->i_sync_tid = 0;
751 ei->i_datasync_tid = 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400752
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700753 return &ei->vfs_inode;
754}
755
Mingming Cao617ba132006-10-11 01:20:53 -0700756static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700757{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700758 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400759 ext4_msg(inode->i_sb, KERN_ERR,
760 "Inode %lu (%p): orphan list check failed!",
761 inode->i_ino, EXT4_I(inode));
Vasily Averin9f7dd932007-07-15 23:40:45 -0700762 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
763 EXT4_I(inode), sizeof(struct ext4_inode_info),
764 true);
765 dump_stack();
766 }
Mingming Cao617ba132006-10-11 01:20:53 -0700767 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700768}
769
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700770static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700771{
Mingming Cao617ba132006-10-11 01:20:53 -0700772 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773
Christoph Lametera35afb82007-05-16 22:10:57 -0700774 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400775#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700776 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500778 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700779 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780}
781
782static int init_inodecache(void)
783{
Mingming Cao617ba132006-10-11 01:20:53 -0700784 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
785 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 0, (SLAB_RECLAIM_ACCOUNT|
787 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900788 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700789 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 return -ENOMEM;
791 return 0;
792}
793
794static void destroy_inodecache(void)
795{
Mingming Cao617ba132006-10-11 01:20:53 -0700796 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797}
798
Mingming Cao617ba132006-10-11 01:20:53 -0700799static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800{
Christoph Hellwig9f754752010-03-03 09:05:05 -0500801 dquot_drop(inode);
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400802 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500803 if (EXT4_JOURNAL(inode))
804 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400805 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700806}
807
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400808static inline void ext4_show_quota_options(struct seq_file *seq,
809 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810{
811#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700812 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700813
Jan Kara5a20bdf2009-11-30 23:58:32 +0100814 if (sbi->s_jquota_fmt) {
815 char *fmtname = "";
816
817 switch (sbi->s_jquota_fmt) {
818 case QFMT_VFS_OLD:
819 fmtname = "vfsold";
820 break;
821 case QFMT_VFS_V0:
822 fmtname = "vfsv0";
823 break;
824 case QFMT_VFS_V1:
825 fmtname = "vfsv1";
826 break;
827 }
828 seq_printf(seq, ",jqfmt=%s", fmtname);
829 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700830
831 if (sbi->s_qf_names[USRQUOTA])
832 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
833
834 if (sbi->s_qf_names[GRPQUOTA])
835 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
836
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500837 if (test_opt(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838 seq_puts(seq, ",usrquota");
839
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500840 if (test_opt(sb, GRPQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841 seq_puts(seq, ",grpquota");
842#endif
843}
844
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700845/*
846 * Show an option if
847 * - it's set to a non-default value OR
848 * - if the per-sb default is different from the global default
849 */
Mingming Cao617ba132006-10-11 01:20:53 -0700850static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500852 int def_errors;
853 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700855 struct ext4_sb_info *sbi = EXT4_SB(sb);
856 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700857
858 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500859 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700860
861 if (sbi->s_sb_block != 1)
862 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
863 if (test_opt(sb, MINIX_DF))
864 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500865 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700866 seq_puts(seq, ",grpid");
867 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
868 seq_puts(seq, ",nogrpid");
869 if (sbi->s_resuid != EXT4_DEF_RESUID ||
870 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
871 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
872 }
873 if (sbi->s_resgid != EXT4_DEF_RESGID ||
874 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
875 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
876 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500877 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700878 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500879 def_errors == EXT4_ERRORS_CONTINUE) {
880 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700881 }
882 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500883 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500884 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500885 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700886 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500887 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700888 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500889 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700890 seq_puts(seq, ",debug");
891 if (test_opt(sb, OLDALLOC))
892 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400893#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500894 if (test_opt(sb, XATTR_USER) &&
895 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700896 seq_puts(seq, ",user_xattr");
897 if (!test_opt(sb, XATTR_USER) &&
898 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
899 seq_puts(seq, ",nouser_xattr");
900 }
901#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400902#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500903 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700904 seq_puts(seq, ",acl");
905 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
906 seq_puts(seq, ",noacl");
907#endif
Theodore Ts'o30773842009-01-03 20:27:38 -0500908 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700909 seq_printf(seq, ",commit=%u",
910 (unsigned) (sbi->s_commit_interval / HZ));
911 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500912 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
913 seq_printf(seq, ",min_batch_time=%u",
914 (unsigned) sbi->s_min_batch_time);
915 }
916 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
917 seq_printf(seq, ",max_batch_time=%u",
918 (unsigned) sbi->s_min_batch_time);
919 }
920
Eric Sandeen571640c2008-05-26 12:29:46 -0400921 /*
922 * We're changing the default of barrier mount option, so
923 * let's always display its mount state so it's clear what its
924 * status is.
925 */
926 seq_puts(seq, ",barrier=");
927 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400928 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
929 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700930 if (test_opt(sb, NOBH))
931 seq_puts(seq, ",nobh");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500932 if (test_opt(sb, I_VERSION))
933 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400934 if (!test_opt(sb, DELALLOC))
935 seq_puts(seq, ",nodelalloc");
936
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500938 if (sbi->s_stripe)
939 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500940 /*
941 * journal mode get enabled in different ways
942 * So just print the value even if we didn't specify it
943 */
Mingming Cao617ba132006-10-11 01:20:53 -0700944 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700945 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700946 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700948 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949 seq_puts(seq, ",data=writeback");
950
Theodore Ts'o240799c2008-10-09 23:53:47 -0400951 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
952 seq_printf(seq, ",inode_readahead_blks=%u",
953 sbi->s_inode_readahead_blks);
954
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400955 if (test_opt(sb, DATA_ERR_ABORT))
956 seq_puts(seq, ",data_err=abort");
957
Theodore Ts'oafd46722009-03-16 23:12:23 -0400958 if (test_opt(sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o06705bf2009-03-28 10:59:57 -0400959 seq_puts(seq, ",noauto_da_alloc");
Theodore Ts'oafd46722009-03-16 23:12:23 -0400960
Eric Sandeen5328e632009-11-19 14:25:42 -0500961 if (test_opt(sb, DISCARD))
962 seq_puts(seq, ",discard");
963
Eric Sandeene3bb52a2009-11-19 14:28:50 -0500964 if (test_opt(sb, NOLOAD))
965 seq_puts(seq, ",norecovery");
966
Jiaying Zhang744692d2010-03-04 16:14:02 -0500967 if (test_opt(sb, DIOREAD_NOLOCK))
968 seq_puts(seq, ",dioread_nolock");
969
Mingming Cao617ba132006-10-11 01:20:53 -0700970 ext4_show_quota_options(seq, sb);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400971
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972 return 0;
973}
974
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700975static struct inode *ext4_nfs_get_inode(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400976 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700977{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700978 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979
Mingming Cao617ba132006-10-11 01:20:53 -0700980 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700981 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700982 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700983 return ERR_PTR(-ESTALE);
984
985 /* iget isn't really right if the inode is currently unallocated!!
986 *
Mingming Cao617ba132006-10-11 01:20:53 -0700987 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700988 * deleted, so we should be safe.
989 *
990 * Currently we don't know the generation for parent directory, so
991 * a generation of 0 means "accept any"
992 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800993 inode = ext4_iget(sb, ino);
994 if (IS_ERR(inode))
995 return ERR_CAST(inode);
996 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700997 iput(inode);
998 return ERR_PTR(-ESTALE);
999 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001000
1001 return inode;
1002}
1003
1004static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001005 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001006{
1007 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1008 ext4_nfs_get_inode);
1009}
1010
1011static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001012 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001013{
1014 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1015 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016}
1017
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001018/*
1019 * Try to release metadata pages (indirect blocks, directories) which are
1020 * mapped via the block device. Since these pages could have journal heads
1021 * which would prevent try_to_free_buffers() from freeing them, we must use
1022 * jbd2 layer's try_to_free_buffers() function to release them.
1023 */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001024static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1025 gfp_t wait)
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001026{
1027 journal_t *journal = EXT4_SB(sb)->s_journal;
1028
1029 WARN_ON(PageChecked(page));
1030 if (!page_has_buffers(page))
1031 return 0;
1032 if (journal)
1033 return jbd2_journal_try_to_free_buffers(journal, page,
1034 wait & ~__GFP_WAIT);
1035 return try_to_free_buffers(page);
1036}
1037
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001039#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001040#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041
Mingming Cao617ba132006-10-11 01:20:53 -07001042static int ext4_write_dquot(struct dquot *dquot);
1043static int ext4_acquire_dquot(struct dquot *dquot);
1044static int ext4_release_dquot(struct dquot *dquot);
1045static int ext4_mark_dquot_dirty(struct dquot *dquot);
1046static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -07001047static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1048 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -07001049static int ext4_quota_on_mount(struct super_block *sb, int type);
1050static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -07001052static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 const char *data, size_t len, loff_t off);
1054
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001055static const struct dquot_operations ext4_quota_operations = {
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001056#ifdef CONFIG_QUOTA
Mingming Cao60e58e02009-01-22 18:13:05 +01001057 .get_reserved_space = ext4_get_reserved_space,
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001058#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001059 .write_dquot = ext4_write_dquot,
1060 .acquire_dquot = ext4_acquire_dquot,
1061 .release_dquot = ext4_release_dquot,
1062 .mark_dirty = ext4_mark_dquot_dirty,
Jan Karaa5b5ee32008-11-25 15:31:35 +01001063 .write_info = ext4_write_info,
1064 .alloc_dquot = dquot_alloc,
1065 .destroy_dquot = dquot_destroy,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066};
1067
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001068static const struct quotactl_ops ext4_qctl_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07001069 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 .quota_off = vfs_quota_off,
1071 .quota_sync = vfs_quota_sync,
1072 .get_info = vfs_get_dqinfo,
1073 .set_info = vfs_set_dqinfo,
1074 .get_dqblk = vfs_get_dqblk,
1075 .set_dqblk = vfs_set_dqblk
1076};
1077#endif
1078
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001079static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -07001080 .alloc_inode = ext4_alloc_inode,
1081 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001082 .write_inode = ext4_write_inode,
1083 .dirty_inode = ext4_dirty_inode,
1084 .delete_inode = ext4_delete_inode,
1085 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001086 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001087 .freeze_fs = ext4_freeze,
1088 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001089 .statfs = ext4_statfs,
1090 .remount_fs = ext4_remount,
1091 .clear_inode = ext4_clear_inode,
1092 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001093#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001094 .quota_read = ext4_quota_read,
1095 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001097 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098};
1099
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001100static const struct super_operations ext4_nojournal_sops = {
1101 .alloc_inode = ext4_alloc_inode,
1102 .destroy_inode = ext4_destroy_inode,
1103 .write_inode = ext4_write_inode,
1104 .dirty_inode = ext4_dirty_inode,
1105 .delete_inode = ext4_delete_inode,
1106 .write_super = ext4_write_super,
1107 .put_super = ext4_put_super,
1108 .statfs = ext4_statfs,
1109 .remount_fs = ext4_remount,
1110 .clear_inode = ext4_clear_inode,
1111 .show_options = ext4_show_options,
1112#ifdef CONFIG_QUOTA
1113 .quota_read = ext4_quota_read,
1114 .quota_write = ext4_quota_write,
1115#endif
1116 .bdev_try_to_free_page = bdev_try_to_free_page,
1117};
1118
Christoph Hellwig39655162007-10-21 16:42:17 -07001119static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001120 .fh_to_dentry = ext4_fh_to_dentry,
1121 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001122 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123};
1124
1125enum {
1126 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1127 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001128 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001130 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001131 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001132 Opt_journal_update, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001133 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Theodore Ts'o296c3552009-09-30 00:32:42 -04001135 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara5a20bdf2009-11-30 23:58:32 +01001137 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1138 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
1139 Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001140 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001141 Opt_block_validity, Opt_noblock_validity,
Eric Sandeen5328e632009-11-19 14:25:42 -05001142 Opt_inode_readahead_blks, Opt_journal_ioprio,
Jiaying Zhang744692d2010-03-04 16:14:02 -05001143 Opt_dioread_nolock, Opt_dioread_lock,
Eric Sandeen5328e632009-11-19 14:25:42 -05001144 Opt_discard, Opt_nodiscard,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001145};
1146
Steven Whitehousea447c092008-10-13 10:46:57 +01001147static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 {Opt_bsd_df, "bsddf"},
1149 {Opt_minix_df, "minixdf"},
1150 {Opt_grpid, "grpid"},
1151 {Opt_grpid, "bsdgroups"},
1152 {Opt_nogrpid, "nogrpid"},
1153 {Opt_nogrpid, "sysvgroups"},
1154 {Opt_resgid, "resgid=%u"},
1155 {Opt_resuid, "resuid=%u"},
1156 {Opt_sb, "sb=%u"},
1157 {Opt_err_cont, "errors=continue"},
1158 {Opt_err_panic, "errors=panic"},
1159 {Opt_err_ro, "errors=remount-ro"},
1160 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 {Opt_debug, "debug"},
1162 {Opt_oldalloc, "oldalloc"},
1163 {Opt_orlov, "orlov"},
1164 {Opt_user_xattr, "user_xattr"},
1165 {Opt_nouser_xattr, "nouser_xattr"},
1166 {Opt_acl, "acl"},
1167 {Opt_noacl, "noacl"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 {Opt_noload, "noload"},
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001169 {Opt_noload, "norecovery"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001170 {Opt_nobh, "nobh"},
1171 {Opt_bh, "bh"},
1172 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001173 {Opt_min_batch_time, "min_batch_time=%u"},
1174 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 {Opt_journal_update, "journal=update"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001176 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001177 {Opt_journal_checksum, "journal_checksum"},
1178 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 {Opt_abort, "abort"},
1180 {Opt_data_journal, "data=journal"},
1181 {Opt_data_ordered, "data=ordered"},
1182 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001183 {Opt_data_err_abort, "data_err=abort"},
1184 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 {Opt_offusrjquota, "usrjquota="},
1186 {Opt_usrjquota, "usrjquota=%s"},
1187 {Opt_offgrpjquota, "grpjquota="},
1188 {Opt_grpjquota, "grpjquota=%s"},
1189 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1190 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara5a20bdf2009-11-30 23:58:32 +01001191 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192 {Opt_grpquota, "grpquota"},
1193 {Opt_noquota, "noquota"},
1194 {Opt_quota, "quota"},
1195 {Opt_usrquota, "usrquota"},
1196 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001197 {Opt_barrier, "barrier"},
1198 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001199 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001200 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001202 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001203 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001204 {Opt_block_validity, "block_validity"},
1205 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001206 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001207 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001208 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001209 {Opt_auto_da_alloc, "auto_da_alloc"},
1210 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001211 {Opt_dioread_nolock, "dioread_nolock"},
1212 {Opt_dioread_lock, "dioread_lock"},
Eric Sandeen5328e632009-11-19 14:25:42 -05001213 {Opt_discard, "discard"},
1214 {Opt_nodiscard, "nodiscard"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001215 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001216};
1217
Mingming Cao617ba132006-10-11 01:20:53 -07001218static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219{
Mingming Cao617ba132006-10-11 01:20:53 -07001220 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001221 char *options = (char *) *data;
1222
1223 if (!options || strncmp(options, "sb=", 3) != 0)
1224 return 1; /* Default location */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001225
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 options += 3;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001227 /* TODO: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228 sb_block = simple_strtoul(options, &options, 0);
1229 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001230 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231 (char *) *data);
1232 return 1;
1233 }
1234 if (*options == ',')
1235 options++;
1236 *data = (void *) options;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001237
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 return sb_block;
1239}
1240
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001241#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001242static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1243 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001244
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001245#ifdef CONFIG_QUOTA
1246static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1247{
1248 struct ext4_sb_info *sbi = EXT4_SB(sb);
1249 char *qname;
1250
1251 if (sb_any_quota_loaded(sb) &&
1252 !sbi->s_qf_names[qtype]) {
1253 ext4_msg(sb, KERN_ERR,
1254 "Cannot change journaled "
1255 "quota options when quota turned on");
1256 return 0;
1257 }
1258 qname = match_strdup(args);
1259 if (!qname) {
1260 ext4_msg(sb, KERN_ERR,
1261 "Not enough memory for storing quotafile name");
1262 return 0;
1263 }
1264 if (sbi->s_qf_names[qtype] &&
1265 strcmp(sbi->s_qf_names[qtype], qname)) {
1266 ext4_msg(sb, KERN_ERR,
1267 "%s quota file already specified", QTYPE2NAME(qtype));
1268 kfree(qname);
1269 return 0;
1270 }
1271 sbi->s_qf_names[qtype] = qname;
1272 if (strchr(sbi->s_qf_names[qtype], '/')) {
1273 ext4_msg(sb, KERN_ERR,
1274 "quotafile must be on filesystem root");
1275 kfree(sbi->s_qf_names[qtype]);
1276 sbi->s_qf_names[qtype] = NULL;
1277 return 0;
1278 }
1279 set_opt(sbi->s_mount_opt, QUOTA);
1280 return 1;
1281}
1282
1283static int clear_qf_name(struct super_block *sb, int qtype)
1284{
1285
1286 struct ext4_sb_info *sbi = EXT4_SB(sb);
1287
1288 if (sb_any_quota_loaded(sb) &&
1289 sbi->s_qf_names[qtype]) {
1290 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1291 " when quota turned on");
1292 return 0;
1293 }
1294 /*
1295 * The space will be released later when all options are confirmed
1296 * to be correct
1297 */
1298 sbi->s_qf_names[qtype] = NULL;
1299 return 1;
1300}
1301#endif
1302
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001303static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001304 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001305 unsigned int *journal_ioprio,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001306 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001307{
Mingming Cao617ba132006-10-11 01:20:53 -07001308 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001309 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001310 substring_t args[MAX_OPT_ARGS];
1311 int data_opt = 0;
1312 int option;
1313#ifdef CONFIG_QUOTA
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001314 int qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001315#endif
1316
1317 if (!options)
1318 return 1;
1319
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001320 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 int token;
1322 if (!*p)
1323 continue;
1324
Eric Sandeen15121c12010-02-15 20:17:55 -05001325 /*
1326 * Initialize args struct so we know whether arg was
1327 * found; some options take optional arguments.
1328 */
1329 args[0].to = args[0].from = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001330 token = match_token(p, tokens, args);
1331 switch (token) {
1332 case Opt_bsd_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001333 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001334 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001335 break;
1336 case Opt_minix_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001337 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001338 set_opt(sbi->s_mount_opt, MINIX_DF);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001339
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001340 break;
1341 case Opt_grpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001342 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001343 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001344
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 break;
1346 case Opt_nogrpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001347 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001348 clear_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001349
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 break;
1351 case Opt_resuid:
1352 if (match_int(&args[0], &option))
1353 return 0;
1354 sbi->s_resuid = option;
1355 break;
1356 case Opt_resgid:
1357 if (match_int(&args[0], &option))
1358 return 0;
1359 sbi->s_resgid = option;
1360 break;
1361 case Opt_sb:
1362 /* handled by get_sb_block() instead of here */
1363 /* *sb_block = match_int(&args[0]); */
1364 break;
1365 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001366 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1367 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1368 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001369 break;
1370 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001371 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1372 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1373 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374 break;
1375 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001376 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1377 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1378 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 break;
1380 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001381 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001382 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001384 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001385 break;
1386 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001387 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001388 break;
1389 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001390 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001392#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001393 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001394 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 break;
1396 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001397 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398 break;
1399#else
1400 case Opt_user_xattr:
1401 case Opt_nouser_xattr:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001402 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 break;
1404#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001405#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 case Opt_acl:
1407 set_opt(sbi->s_mount_opt, POSIX_ACL);
1408 break;
1409 case Opt_noacl:
1410 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1411 break;
1412#else
1413 case Opt_acl:
1414 case Opt_noacl:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001415 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001416 break;
1417#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 case Opt_journal_update:
1419 /* @@@ FIXME */
1420 /* Eventually we will want to be able to create
1421 a journal file here. For now, only allow the
1422 user to specify an existing inode to be the
1423 journal file. */
1424 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001425 ext4_msg(sb, KERN_ERR,
1426 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 return 0;
1428 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001429 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001430 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 case Opt_journal_dev:
1432 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001433 ext4_msg(sb, KERN_ERR,
1434 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001435 return 0;
1436 }
1437 if (match_int(&args[0], &option))
1438 return 0;
1439 *journal_devnum = option;
1440 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001441 case Opt_journal_checksum:
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001442 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1443 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001444 case Opt_journal_async_commit:
1445 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001446 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
Girish Shilamkar818d2762008-01-28 23:58:27 -05001447 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001449 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 break;
1451 case Opt_commit:
1452 if (match_int(&args[0], &option))
1453 return 0;
1454 if (option < 0)
1455 return 0;
1456 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001457 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001458 sbi->s_commit_interval = HZ * option;
1459 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001460 case Opt_max_batch_time:
1461 if (match_int(&args[0], &option))
1462 return 0;
1463 if (option < 0)
1464 return 0;
1465 if (option == 0)
1466 option = EXT4_DEF_MAX_BATCH_TIME;
1467 sbi->s_max_batch_time = option;
1468 break;
1469 case Opt_min_batch_time:
1470 if (match_int(&args[0], &option))
1471 return 0;
1472 if (option < 0)
1473 return 0;
1474 sbi->s_min_batch_time = option;
1475 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001477 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478 goto datacheck;
1479 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001480 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001481 goto datacheck;
1482 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001483 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001484 datacheck:
1485 if (is_remount) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001486 if (test_opt(sb, DATA_FLAGS) != data_opt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001487 ext4_msg(sb, KERN_ERR,
1488 "Cannot change data mode on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 return 0;
1490 }
1491 } else {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001492 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 sbi->s_mount_opt |= data_opt;
1494 }
1495 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001496 case Opt_data_err_abort:
1497 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1498 break;
1499 case Opt_data_err_ignore:
1500 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1501 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502#ifdef CONFIG_QUOTA
1503 case Opt_usrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001504 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1505 return 0;
1506 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 case Opt_grpjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001508 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 break;
1511 case Opt_offusrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001512 if (!clear_qf_name(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001513 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001514 break;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001515 case Opt_offgrpjquota:
1516 if (!clear_qf_name(sb, GRPQUOTA))
1517 return 0;
1518 break;
1519
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001520 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001521 qfmt = QFMT_VFS_OLD;
1522 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001524 qfmt = QFMT_VFS_V0;
Jan Kara5a20bdf2009-11-30 23:58:32 +01001525 goto set_qf_format;
1526 case Opt_jqfmt_vfsv1:
1527 qfmt = QFMT_VFS_V1;
Jan Karadfc5d032008-05-13 19:11:51 -04001528set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001529 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001530 sbi->s_jquota_fmt != qfmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001531 ext4_msg(sb, KERN_ERR, "Cannot change "
Jan Karadfc5d032008-05-13 19:11:51 -04001532 "journaled quota options when "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001533 "quota turned on");
Jan Karadfc5d032008-05-13 19:11:51 -04001534 return 0;
1535 }
1536 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 break;
1538 case Opt_quota:
1539 case Opt_usrquota:
1540 set_opt(sbi->s_mount_opt, QUOTA);
1541 set_opt(sbi->s_mount_opt, USRQUOTA);
1542 break;
1543 case Opt_grpquota:
1544 set_opt(sbi->s_mount_opt, QUOTA);
1545 set_opt(sbi->s_mount_opt, GRPQUOTA);
1546 break;
1547 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001548 if (sb_any_quota_loaded(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001549 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1550 "options when quota turned on");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 return 0;
1552 }
1553 clear_opt(sbi->s_mount_opt, QUOTA);
1554 clear_opt(sbi->s_mount_opt, USRQUOTA);
1555 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1556 break;
1557#else
1558 case Opt_quota:
1559 case Opt_usrquota:
1560 case Opt_grpquota:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001561 ext4_msg(sb, KERN_ERR,
1562 "quota options not supported");
Jan Karacd59e7b2008-05-13 19:11:51 -04001563 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001564 case Opt_usrjquota:
1565 case Opt_grpjquota:
1566 case Opt_offusrjquota:
1567 case Opt_offgrpjquota:
1568 case Opt_jqfmt_vfsold:
1569 case Opt_jqfmt_vfsv0:
Jan Kara5a20bdf2009-11-30 23:58:32 +01001570 case Opt_jqfmt_vfsv1:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001571 ext4_msg(sb, KERN_ERR,
1572 "journaled quota options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 break;
1574 case Opt_noquota:
1575 break;
1576#endif
1577 case Opt_abort:
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04001578 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001579 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001580 case Opt_nobarrier:
1581 clear_opt(sbi->s_mount_opt, BARRIER);
1582 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 case Opt_barrier:
Eric Sandeen15121c12010-02-15 20:17:55 -05001584 if (args[0].from) {
1585 if (match_int(&args[0], &option))
1586 return 0;
1587 } else
1588 option = 1; /* No argument, default to 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 if (option)
1590 set_opt(sbi->s_mount_opt, BARRIER);
1591 else
1592 clear_opt(sbi->s_mount_opt, BARRIER);
1593 break;
1594 case Opt_ignore:
1595 break;
1596 case Opt_resize:
1597 if (!is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001598 ext4_msg(sb, KERN_ERR,
1599 "resize option only available "
1600 "for remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 return 0;
1602 }
1603 if (match_int(&args[0], &option) != 0)
1604 return 0;
1605 *n_blocks_count = option;
1606 break;
1607 case Opt_nobh:
1608 set_opt(sbi->s_mount_opt, NOBH);
1609 break;
1610 case Opt_bh:
1611 clear_opt(sbi->s_mount_opt, NOBH);
1612 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001613 case Opt_i_version:
1614 set_opt(sbi->s_mount_opt, I_VERSION);
1615 sb->s_flags |= MS_I_VERSION;
1616 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001617 case Opt_nodelalloc:
1618 clear_opt(sbi->s_mount_opt, DELALLOC);
1619 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001620 case Opt_stripe:
1621 if (match_int(&args[0], &option))
1622 return 0;
1623 if (option < 0)
1624 return 0;
1625 sbi->s_stripe = option;
1626 break;
Alex Tomas64769242008-07-11 19:27:31 -04001627 case Opt_delalloc:
1628 set_opt(sbi->s_mount_opt, DELALLOC);
1629 break;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001630 case Opt_block_validity:
1631 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1632 break;
1633 case Opt_noblock_validity:
1634 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1635 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001636 case Opt_inode_readahead_blks:
1637 if (match_int(&args[0], &option))
1638 return 0;
1639 if (option < 0 || option > (1 << 30))
1640 return 0;
Theodore Ts'of7c43952009-04-24 23:31:59 -04001641 if (!is_power_of_2(option)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001642 ext4_msg(sb, KERN_ERR,
1643 "EXT4-fs: inode_readahead_blks"
1644 " must be a power of 2");
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001645 return 0;
1646 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04001647 sbi->s_inode_readahead_blks = option;
1648 break;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001649 case Opt_journal_ioprio:
1650 if (match_int(&args[0], &option))
1651 return 0;
1652 if (option < 0 || option > 7)
1653 break;
1654 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1655 option);
1656 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001657 case Opt_noauto_da_alloc:
1658 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1659 break;
Theodore Ts'oafd46722009-03-16 23:12:23 -04001660 case Opt_auto_da_alloc:
Eric Sandeen15121c12010-02-15 20:17:55 -05001661 if (args[0].from) {
1662 if (match_int(&args[0], &option))
1663 return 0;
1664 } else
1665 option = 1; /* No argument, default to 1 */
Theodore Ts'oafd46722009-03-16 23:12:23 -04001666 if (option)
1667 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1668 else
1669 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1670 break;
Eric Sandeen5328e632009-11-19 14:25:42 -05001671 case Opt_discard:
1672 set_opt(sbi->s_mount_opt, DISCARD);
1673 break;
1674 case Opt_nodiscard:
1675 clear_opt(sbi->s_mount_opt, DISCARD);
1676 break;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001677 case Opt_dioread_nolock:
1678 set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1679 break;
1680 case Opt_dioread_lock:
1681 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1682 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001683 default:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001684 ext4_msg(sb, KERN_ERR,
1685 "Unrecognized mount option \"%s\" "
1686 "or missing value", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 return 0;
1688 }
1689 }
1690#ifdef CONFIG_QUOTA
1691 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001692 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001693 clear_opt(sbi->s_mount_opt, USRQUOTA);
1694
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001695 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1697
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001698 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001699 ext4_msg(sb, KERN_ERR, "old and new quota "
1700 "format mixing");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701 return 0;
1702 }
1703
1704 if (!sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001705 ext4_msg(sb, KERN_ERR, "journaled quota format "
1706 "not specified");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001707 return 0;
1708 }
1709 } else {
1710 if (sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001711 ext4_msg(sb, KERN_ERR, "journaled quota format "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001712 "specified with no journaling "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001713 "enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 return 0;
1715 }
1716 }
1717#endif
1718 return 1;
1719}
1720
Mingming Cao617ba132006-10-11 01:20:53 -07001721static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001722 int read_only)
1723{
Mingming Cao617ba132006-10-11 01:20:53 -07001724 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001725 int res = 0;
1726
Mingming Cao617ba132006-10-11 01:20:53 -07001727 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001728 ext4_msg(sb, KERN_ERR, "revision level too high, "
1729 "forcing read-only mode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 res = MS_RDONLY;
1731 }
1732 if (read_only)
1733 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001734 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001735 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1736 "running e2fsck is recommended");
Mingming Cao617ba132006-10-11 01:20:53 -07001737 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001738 ext4_msg(sb, KERN_WARNING,
1739 "warning: mounting fs with errors, "
1740 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1742 le16_to_cpu(es->s_mnt_count) >=
1743 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001744 ext4_msg(sb, KERN_WARNING,
1745 "warning: maximal mount count reached, "
1746 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001747 else if (le32_to_cpu(es->s_checkinterval) &&
1748 (le32_to_cpu(es->s_lastcheck) +
1749 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001750 ext4_msg(sb, KERN_WARNING,
1751 "warning: checktime reached, "
1752 "running e2fsck is recommended");
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001753 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05001754 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001756 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001757 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001759 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001760 if (sbi->s_journal)
1761 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001762
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001763 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001765 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -04001766 "bpg=%lu, ipg=%lu, mo=%04x]\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001767 sb->s_blocksize,
1768 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001769 EXT4_BLOCKS_PER_GROUP(sb),
1770 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001771 sbi->s_mount_opt);
1772
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 return res;
1774}
1775
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001776static int ext4_fill_flex_info(struct super_block *sb)
1777{
1778 struct ext4_sb_info *sbi = EXT4_SB(sb);
1779 struct ext4_group_desc *gdp = NULL;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001780 ext4_group_t flex_group_count;
1781 ext4_group_t flex_group;
1782 int groups_per_flex = 0;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001783 size_t size;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001784 int i;
1785
Theodore Ts'o503358a2009-11-23 07:24:46 -05001786 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1787 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1788
1789 if (groups_per_flex < 2) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001790 sbi->s_log_groups_per_flex = 0;
1791 return 1;
1792 }
1793
Frederic Bohec62a11f2008-09-08 10:20:24 -04001794 /* We allocate both existing and potentially added groups */
1795 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001796 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1797 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001798 size = flex_group_count * sizeof(struct flex_groups);
1799 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1800 if (sbi->s_flex_groups == NULL) {
1801 sbi->s_flex_groups = vmalloc(size);
1802 if (sbi->s_flex_groups)
1803 memset(sbi->s_flex_groups, 0, size);
1804 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001805 if (sbi->s_flex_groups == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001806 ext4_msg(sb, KERN_ERR, "not enough memory for "
1807 "%u flex groups", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001808 goto failed;
1809 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001810
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001811 for (i = 0; i < sbi->s_groups_count; i++) {
Theodore Ts'o88b6edd2009-05-25 11:50:39 -04001812 gdp = ext4_get_group_desc(sb, i, NULL);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001813
1814 flex_group = ext4_flex_group(sbi, i);
Theodore Ts'o7ad9bb62009-09-11 16:51:28 -04001815 atomic_add(ext4_free_inodes_count(sb, gdp),
1816 &sbi->s_flex_groups[flex_group].free_inodes);
1817 atomic_add(ext4_free_blks_count(sb, gdp),
1818 &sbi->s_flex_groups[flex_group].free_blocks);
1819 atomic_add(ext4_used_dirs_count(sb, gdp),
1820 &sbi->s_flex_groups[flex_group].used_dirs);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001821 }
1822
1823 return 1;
1824failed:
1825 return 0;
1826}
1827
Andreas Dilger717d50e2007-10-16 18:38:25 -04001828__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1829 struct ext4_group_desc *gdp)
1830{
1831 __u16 crc = 0;
1832
1833 if (sbi->s_es->s_feature_ro_compat &
1834 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1835 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1836 __le32 le_group = cpu_to_le32(block_group);
1837
1838 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1839 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1840 crc = crc16(crc, (__u8 *)gdp, offset);
1841 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1842 /* for checksum of struct ext4_group_desc do the rest...*/
1843 if ((sbi->s_es->s_feature_incompat &
1844 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1845 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1846 crc = crc16(crc, (__u8 *)gdp + offset,
1847 le16_to_cpu(sbi->s_es->s_desc_size) -
1848 offset);
1849 }
1850
1851 return cpu_to_le16(crc);
1852}
1853
1854int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1855 struct ext4_group_desc *gdp)
1856{
1857 if ((sbi->s_es->s_feature_ro_compat &
1858 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1859 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1860 return 0;
1861
1862 return 1;
1863}
1864
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001866static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001867{
Mingming Cao617ba132006-10-11 01:20:53 -07001868 struct ext4_sb_info *sbi = EXT4_SB(sb);
1869 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1870 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001871 ext4_fsblk_t block_bitmap;
1872 ext4_fsblk_t inode_bitmap;
1873 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001874 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001875 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876
Jose R. Santosce421582007-10-16 18:38:25 -04001877 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1878 flexbg_flag = 1;
1879
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001880 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001881
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001882 for (i = 0; i < sbi->s_groups_count; i++) {
1883 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1884
Jose R. Santosce421582007-10-16 18:38:25 -04001885 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001886 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001887 else
1888 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001889 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001890
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001891 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001892 if (block_bitmap < first_block || block_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001893 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001894 "Block bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001895 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896 return 0;
1897 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001898 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001899 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001900 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001901 "Inode bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001902 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001903 return 0;
1904 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001905 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001906 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001907 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001908 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001909 "Inode table for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001910 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911 return 0;
1912 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001913 ext4_lock_group(sb, i);
Andreas Dilger717d50e2007-10-16 18:38:25 -04001914 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001915 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1916 "Checksum for group %u failed (%u!=%u)",
1917 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1918 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001919 if (!(sb->s_flags & MS_RDONLY)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001920 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04001921 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001922 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001923 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001924 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04001925 if (!flexbg_flag)
1926 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001927 }
1928
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001929 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001930 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001931 return 1;
1932}
1933
Mingming Cao617ba132006-10-11 01:20:53 -07001934/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001935 * the superblock) which were deleted from all directories, but held open by
1936 * a process at the time of a crash. We walk the list and try to delete these
1937 * inodes at recovery time (only with a read-write filesystem).
1938 *
1939 * In order to keep the orphan inode chain consistent during traversal (in
1940 * case of crash during recovery), we link each inode into the superblock
1941 * orphan list_head and handle it the same way as an inode deletion during
1942 * normal operation (which journals the operations for us).
1943 *
1944 * We only do an iget() and an iput() on each inode, which is very safe if we
1945 * accidentally point at an in-use or already deleted inode. The worst that
1946 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001947 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001948 * e2fsck was run on this filesystem, and it must have already done the orphan
1949 * inode cleanup for us, so we can safely abort without any further action.
1950 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001951static void ext4_orphan_cleanup(struct super_block *sb,
1952 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001953{
1954 unsigned int s_flags = sb->s_flags;
1955 int nr_orphans = 0, nr_truncates = 0;
1956#ifdef CONFIG_QUOTA
1957 int i;
1958#endif
1959 if (!es->s_last_orphan) {
1960 jbd_debug(4, "no orphan inodes to clean up\n");
1961 return;
1962 }
1963
Eric Sandeena8f48a92006-12-06 20:40:13 -08001964 if (bdev_read_only(sb->s_bdev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001965 ext4_msg(sb, KERN_ERR, "write access "
1966 "unavailable, skipping orphan cleanup");
Eric Sandeena8f48a92006-12-06 20:40:13 -08001967 return;
1968 }
1969
Mingming Cao617ba132006-10-11 01:20:53 -07001970 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 if (es->s_last_orphan)
1972 jbd_debug(1, "Errors on filesystem, "
1973 "clearing orphan list.\n");
1974 es->s_last_orphan = 0;
1975 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1976 return;
1977 }
1978
1979 if (s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001980 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 sb->s_flags &= ~MS_RDONLY;
1982 }
1983#ifdef CONFIG_QUOTA
1984 /* Needed for iput() to work correctly and not trash data */
1985 sb->s_flags |= MS_ACTIVE;
1986 /* Turn on quotas so that they are updated correctly */
1987 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001988 if (EXT4_SB(sb)->s_qf_names[i]) {
1989 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001990 if (ret < 0)
Eric Sandeenb31e1552009-06-04 17:36:36 -04001991 ext4_msg(sb, KERN_ERR,
1992 "Cannot turn on journaled "
1993 "quota: error %d", ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 }
1995 }
1996#endif
1997
1998 while (es->s_last_orphan) {
1999 struct inode *inode;
2000
Josef Bacik97bd42b2008-04-29 22:04:56 -04002001 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2002 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 es->s_last_orphan = 0;
2004 break;
2005 }
2006
Mingming Cao617ba132006-10-11 01:20:53 -07002007 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Christoph Hellwig871a2932010-03-03 09:05:07 -05002008 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009 if (inode->i_nlink) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002010 ext4_msg(sb, KERN_DEBUG,
2011 "%s: truncating inode %lu to %lld bytes",
Harvey Harrison46e665e2008-04-17 10:38:59 -04002012 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002013 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002014 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07002015 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002016 nr_truncates++;
2017 } else {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002018 ext4_msg(sb, KERN_DEBUG,
2019 "%s: deleting unreferenced inode %lu",
Harvey Harrison46e665e2008-04-17 10:38:59 -04002020 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002021 jbd_debug(2, "deleting unreferenced inode %lu\n",
2022 inode->i_ino);
2023 nr_orphans++;
2024 }
2025 iput(inode); /* The delete magic happens here! */
2026 }
2027
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002028#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029
2030 if (nr_orphans)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002031 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2032 PLURAL(nr_orphans));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002033 if (nr_truncates)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002034 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2035 PLURAL(nr_truncates));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002036#ifdef CONFIG_QUOTA
2037 /* Turn quotas off */
2038 for (i = 0; i < MAXQUOTAS; i++) {
2039 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07002040 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 }
2042#endif
2043 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2044}
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002045
Eric Sandeencd2291a2008-01-28 23:58:27 -05002046/*
2047 * Maximal extent format file size.
2048 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2049 * extent format containers, within a sector_t, and within i_blocks
2050 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2051 * so that won't be a limiting factor.
2052 *
2053 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2054 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002055static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05002056{
2057 loff_t res;
2058 loff_t upper_limit = MAX_LFS_FILESIZE;
2059
2060 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002061 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05002062 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002063 * CONFIG_LBDAF is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05002064 * i_block represent total blocks in 512 bytes
2065 * 32 == size of vfs inode i_blocks * 8
2066 */
2067 upper_limit = (1LL << 32) - 1;
2068
2069 /* total blocks in file system block size */
2070 upper_limit >>= (blkbits - 9);
2071 upper_limit <<= blkbits;
2072 }
2073
2074 /* 32-bit extent-start container, ee_block */
2075 res = 1LL << 32;
2076 res <<= blkbits;
2077 res -= 1;
2078
2079 /* Sanity check against vm- & vfs- imposed limits */
2080 if (res > upper_limit)
2081 res = upper_limit;
2082
2083 return res;
2084}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002086/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05002087 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002088 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2089 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002091static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092{
Mingming Cao617ba132006-10-11 01:20:53 -07002093 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002094 int meta_blocks;
2095 loff_t upper_limit;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002096 /* This is calculated to be the largest file size for a dense, block
2097 * mapped file such that the file's total number of 512-byte sectors,
2098 * including data and all indirect blocks, does not exceed (2^48 - 1).
2099 *
2100 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2101 * number of 512-byte sectors of the file.
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002102 */
2103
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002104 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002105 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002106 * !has_huge_files or CONFIG_LBDAF not enabled implies that
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002107 * the inode i_block field represents total file blocks in
2108 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002109 */
2110 upper_limit = (1LL << 32) - 1;
2111
2112 /* total blocks in file system block size */
2113 upper_limit >>= (bits - 9);
2114
2115 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05002116 /*
2117 * We use 48 bit ext4_inode i_blocks
2118 * With EXT4_HUGE_FILE_FL set the i_blocks
2119 * represent total number of blocks in
2120 * file system block size
2121 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002122 upper_limit = (1LL << 48) - 1;
2123
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002124 }
2125
2126 /* indirect blocks */
2127 meta_blocks = 1;
2128 /* double indirect blocks */
2129 meta_blocks += 1 + (1LL << (bits-2));
2130 /* tripple indirect blocks */
2131 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2132
2133 upper_limit -= meta_blocks;
2134 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135
2136 res += 1LL << (bits-2);
2137 res += 1LL << (2*(bits-2));
2138 res += 1LL << (3*(bits-2));
2139 res <<= bits;
2140 if (res > upper_limit)
2141 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002142
2143 if (res > MAX_LFS_FILESIZE)
2144 res = MAX_LFS_FILESIZE;
2145
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002146 return res;
2147}
2148
Mingming Cao617ba132006-10-11 01:20:53 -07002149static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002150 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151{
Mingming Cao617ba132006-10-11 01:20:53 -07002152 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002153 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 int has_super = 0;
2155
2156 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2157
Mingming Cao617ba132006-10-11 01:20:53 -07002158 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002160 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07002162 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 has_super = 1;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002164
Mingming Cao617ba132006-10-11 01:20:53 -07002165 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166}
2167
Alex Tomasc9de5602008-01-29 00:19:52 -05002168/**
2169 * ext4_get_stripe_size: Get the stripe size.
2170 * @sbi: In memory super block info
2171 *
2172 * If we have specified it via mount option, then
2173 * use the mount option value. If the value specified at mount time is
2174 * greater than the blocks per group use the super block value.
2175 * If the super block value is greater than blocks per group return 0.
2176 * Allocator needs it be less than blocks per group.
2177 *
2178 */
2179static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2180{
2181 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2182 unsigned long stripe_width =
2183 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2184
2185 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2186 return sbi->s_stripe;
2187
2188 if (stripe_width <= sbi->s_blocks_per_group)
2189 return stripe_width;
2190
2191 if (stride <= sbi->s_blocks_per_group)
2192 return stride;
2193
2194 return 0;
2195}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002196
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002197/* sysfs supprt */
2198
2199struct ext4_attr {
2200 struct attribute attr;
2201 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2202 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2203 const char *, size_t);
2204 int offset;
2205};
2206
2207static int parse_strtoul(const char *buf,
2208 unsigned long max, unsigned long *value)
2209{
2210 char *endp;
2211
André Goddard Rosae7d28602009-12-14 18:01:06 -08002212 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
2213 endp = skip_spaces(endp);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002214 if (*endp || *value > max)
2215 return -EINVAL;
2216
2217 return 0;
2218}
2219
2220static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2221 struct ext4_sb_info *sbi,
2222 char *buf)
2223{
2224 return snprintf(buf, PAGE_SIZE, "%llu\n",
2225 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2226}
2227
2228static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2229 struct ext4_sb_info *sbi, char *buf)
2230{
2231 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2232
2233 return snprintf(buf, PAGE_SIZE, "%lu\n",
2234 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2235 sbi->s_sectors_written_start) >> 1);
2236}
2237
2238static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2239 struct ext4_sb_info *sbi, char *buf)
2240{
2241 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2242
2243 return snprintf(buf, PAGE_SIZE, "%llu\n",
Andrew Mortona6b43e32009-12-23 07:48:08 -05002244 (unsigned long long)(sbi->s_kbytes_written +
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002245 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
Andrew Mortona6b43e32009-12-23 07:48:08 -05002246 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002247}
2248
2249static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2250 struct ext4_sb_info *sbi,
2251 const char *buf, size_t count)
2252{
2253 unsigned long t;
2254
2255 if (parse_strtoul(buf, 0x40000000, &t))
2256 return -EINVAL;
2257
Theodore Ts'of7c43952009-04-24 23:31:59 -04002258 if (!is_power_of_2(t))
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002259 return -EINVAL;
2260
2261 sbi->s_inode_readahead_blks = t;
2262 return count;
2263}
2264
2265static ssize_t sbi_ui_show(struct ext4_attr *a,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002266 struct ext4_sb_info *sbi, char *buf)
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002267{
2268 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2269
2270 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2271}
2272
2273static ssize_t sbi_ui_store(struct ext4_attr *a,
2274 struct ext4_sb_info *sbi,
2275 const char *buf, size_t count)
2276{
2277 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2278 unsigned long t;
2279
2280 if (parse_strtoul(buf, 0xffffffff, &t))
2281 return -EINVAL;
2282 *ui = t;
2283 return count;
2284}
2285
2286#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2287static struct ext4_attr ext4_attr_##_name = { \
2288 .attr = {.name = __stringify(_name), .mode = _mode }, \
2289 .show = _show, \
2290 .store = _store, \
2291 .offset = offsetof(struct ext4_sb_info, _elname), \
2292}
2293#define EXT4_ATTR(name, mode, show, store) \
2294static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2295
2296#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2297#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2298#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2299 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2300#define ATTR_LIST(name) &ext4_attr_##name.attr
2301
2302EXT4_RO_ATTR(delayed_allocation_blocks);
2303EXT4_RO_ATTR(session_write_kbytes);
2304EXT4_RO_ATTR(lifetime_write_kbytes);
2305EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2306 inode_readahead_blks_store, s_inode_readahead_blks);
Andreas Dilger11013912009-06-13 11:45:35 -04002307EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002308EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2309EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2310EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2311EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2312EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2313EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002314EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002315
2316static struct attribute *ext4_attrs[] = {
2317 ATTR_LIST(delayed_allocation_blocks),
2318 ATTR_LIST(session_write_kbytes),
2319 ATTR_LIST(lifetime_write_kbytes),
2320 ATTR_LIST(inode_readahead_blks),
Andreas Dilger11013912009-06-13 11:45:35 -04002321 ATTR_LIST(inode_goal),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002322 ATTR_LIST(mb_stats),
2323 ATTR_LIST(mb_max_to_scan),
2324 ATTR_LIST(mb_min_to_scan),
2325 ATTR_LIST(mb_order2_req),
2326 ATTR_LIST(mb_stream_req),
2327 ATTR_LIST(mb_group_prealloc),
Theodore Ts'o55138e02009-09-29 13:31:31 -04002328 ATTR_LIST(max_writeback_mb_bump),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002329 NULL,
2330};
2331
2332static ssize_t ext4_attr_show(struct kobject *kobj,
2333 struct attribute *attr, char *buf)
2334{
2335 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2336 s_kobj);
2337 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2338
2339 return a->show ? a->show(a, sbi, buf) : 0;
2340}
2341
2342static ssize_t ext4_attr_store(struct kobject *kobj,
2343 struct attribute *attr,
2344 const char *buf, size_t len)
2345{
2346 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2347 s_kobj);
2348 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2349
2350 return a->store ? a->store(a, sbi, buf, len) : 0;
2351}
2352
2353static void ext4_sb_release(struct kobject *kobj)
2354{
2355 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2356 s_kobj);
2357 complete(&sbi->s_kobj_unregister);
2358}
2359
2360
Emese Revfy52cf25d2010-01-19 02:58:23 +01002361static const struct sysfs_ops ext4_attr_ops = {
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002362 .show = ext4_attr_show,
2363 .store = ext4_attr_store,
2364};
2365
2366static struct kobj_type ext4_ktype = {
2367 .default_attrs = ext4_attrs,
2368 .sysfs_ops = &ext4_attr_ops,
2369 .release = ext4_sb_release,
2370};
2371
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002372/*
2373 * Check whether this filesystem can be mounted based on
2374 * the features present and the RDONLY/RDWR mount requested.
2375 * Returns 1 if this filesystem can be mounted as requested,
2376 * 0 if it cannot be.
2377 */
2378static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2379{
2380 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2381 ext4_msg(sb, KERN_ERR,
2382 "Couldn't mount because of "
2383 "unsupported optional features (%x)",
2384 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2385 ~EXT4_FEATURE_INCOMPAT_SUPP));
2386 return 0;
2387 }
2388
2389 if (readonly)
2390 return 1;
2391
2392 /* Check that feature set is OK for a read-write mount */
2393 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2394 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2395 "unsupported optional features (%x)",
2396 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2397 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2398 return 0;
2399 }
2400 /*
2401 * Large file size enabled file system can only be mounted
2402 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2403 */
2404 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2405 if (sizeof(blkcnt_t) < sizeof(u64)) {
2406 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2407 "cannot be mounted RDWR without "
2408 "CONFIG_LBDAF");
2409 return 0;
2410 }
2411 }
2412 return 1;
2413}
2414
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002415static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002416 __releases(kernel_lock)
2417 __acquires(kernel_lock)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002418{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002419 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002420 struct ext4_super_block *es = NULL;
2421 struct ext4_sb_info *sbi;
2422 ext4_fsblk_t block;
2423 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002424 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002425 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426 unsigned long journal_devnum = 0;
2427 unsigned long def_mount_opts;
2428 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002429 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002430 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002431 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002432 int blocksize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002433 unsigned int db_count;
2434 unsigned int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002435 int needs_recovery, has_huge_files;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002436 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002437 int err;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002438 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439
2440 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2441 if (!sbi)
2442 return -ENOMEM;
Pekka Enberg705895b2009-02-15 18:07:52 -05002443
2444 sbi->s_blockgroup_lock =
2445 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2446 if (!sbi->s_blockgroup_lock) {
2447 kfree(sbi);
2448 return -ENOMEM;
2449 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450 sb->s_fs_info = sbi;
2451 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002452 sbi->s_resuid = EXT4_DEF_RESUID;
2453 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002454 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002455 sbi->s_sb_block = sb_block;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002456 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2457 sectors[1]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002458
2459 unlock_kernel();
2460
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002461 /* Cleanup superblock name */
2462 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2463 *cp = '!';
2464
Mingming Cao617ba132006-10-11 01:20:53 -07002465 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002466 if (!blocksize) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002467 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002468 goto out_fail;
2469 }
2470
2471 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002472 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473 * block sizes. We need to calculate the offset from buffer start.
2474 */
Mingming Cao617ba132006-10-11 01:20:53 -07002475 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002476 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2477 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002479 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 }
2481
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002482 if (!(bh = sb_bread(sb, logical_sb_block))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002483 ext4_msg(sb, KERN_ERR, "unable to read superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 goto out_fail;
2485 }
2486 /*
2487 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002488 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002489 */
Mingming Cao617ba132006-10-11 01:20:53 -07002490 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002491 sbi->s_es = es;
2492 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002493 if (sb->s_magic != EXT4_SUPER_MAGIC)
2494 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002495 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002496
2497 /* Set defaults before we parse the mount options */
2498 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002499 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002500 set_opt(sbi->s_mount_opt, DEBUG);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002501 if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
2502 ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
2503 "2.6.38");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002504 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002505 }
Mingming Cao617ba132006-10-11 01:20:53 -07002506 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002507 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002508#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002509 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002510 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002511#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002512#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002513 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002514 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002515#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002516 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002517 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002518 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002519 set_opt(sbi->s_mount_opt, ORDERED_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002520 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002521 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002522
Mingming Cao617ba132006-10-11 01:20:53 -07002523 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002524 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002525 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002526 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002527 else
2528 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529
2530 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2531 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002532 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2533 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2534 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002535
Eric Sandeen571640c2008-05-26 12:29:46 -04002536 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002537
Mingming Cao1e2462f2007-07-18 09:00:55 -04002538 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002539 * enable delayed allocation by default
2540 * Use -o nodelalloc to turn it off
2541 */
2542 set_opt(sbi->s_mount_opt, DELALLOC);
2543
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002544 if (!parse_options((char *) data, sb, &journal_devnum,
2545 &journal_ioprio, NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 goto failed_mount;
2547
2548 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002549 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002550
Mingming Cao617ba132006-10-11 01:20:53 -07002551 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2552 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2553 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2554 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002555 ext4_msg(sb, KERN_WARNING,
2556 "feature flags set on rev 0 fs, "
2557 "running e2fsck is recommended");
Theodore Tso469108f2008-02-10 01:11:44 -05002558
2559 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560 * Check feature flags regardless of the revision level, since we
2561 * previously didn't change the revision level when setting the flags,
2562 * so there is a chance incompat flags are set on a rev 0 filesystem.
2563 */
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002564 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002565 goto failed_mount;
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002566
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2568
Mingming Cao617ba132006-10-11 01:20:53 -07002569 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2570 blocksize > EXT4_MAX_BLOCK_SIZE) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002571 ext4_msg(sb, KERN_ERR,
2572 "Unsupported filesystem blocksize %d", blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002573 goto failed_mount;
2574 }
2575
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002576 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002577 /* Validate the filesystem blocksize */
2578 if (!sb_set_blocksize(sb, blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002579 ext4_msg(sb, KERN_ERR, "bad block size %d",
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002580 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002581 goto failed_mount;
2582 }
2583
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002584 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002585 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2586 offset = do_div(logical_sb_block, blocksize);
2587 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588 if (!bh) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002589 ext4_msg(sb, KERN_ERR,
2590 "Can't read superblock on 2nd try");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002591 goto failed_mount;
2592 }
Mingming Cao617ba132006-10-11 01:20:53 -07002593 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002595 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002596 ext4_msg(sb, KERN_ERR,
2597 "Magic mismatch, very weird!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002598 goto failed_mount;
2599 }
2600 }
2601
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002602 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2603 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002604 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2605 has_huge_files);
2606 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607
Mingming Cao617ba132006-10-11 01:20:53 -07002608 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2609 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2610 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611 } else {
2612 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2613 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002614 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002615 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002616 (sbi->s_inode_size > blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002617 ext4_msg(sb, KERN_ERR,
2618 "unsupported inode size: %d",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002619 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002620 goto failed_mount;
2621 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002622 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2623 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002624 }
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002625
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002626 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2627 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002628 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002629 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002630 !is_power_of_2(sbi->s_desc_size)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002631 ext4_msg(sb, KERN_ERR,
2632 "unsupported descriptor size %lu",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002633 sbi->s_desc_size);
2634 goto failed_mount;
2635 }
2636 } else
2637 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002638
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002639 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002640 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002641 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002642 goto cantfind_ext4;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002643
Mingming Cao617ba132006-10-11 01:20:53 -07002644 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002645 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002646 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002647 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2648 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002649 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002650 sbi->s_sbh = bh;
2651 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002652 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2653 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002654
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002655 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002656 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2657 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002658 i = le32_to_cpu(es->s_flags);
2659 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2660 sbi->s_hash_unsigned = 3;
2661 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2662#ifdef __CHAR_UNSIGNED__
2663 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2664 sbi->s_hash_unsigned = 3;
2665#else
2666 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2667#endif
2668 sb->s_dirt = 1;
2669 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002670
2671 if (sbi->s_blocks_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002672 ext4_msg(sb, KERN_ERR,
2673 "#blocks per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002674 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675 goto failed_mount;
2676 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677 if (sbi->s_inodes_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002678 ext4_msg(sb, KERN_ERR,
2679 "#inodes per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002680 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681 goto failed_mount;
2682 }
2683
Eric Sandeenbf43d842009-08-17 23:48:51 -04002684 /*
2685 * Test whether we have more sectors than will fit in sector_t,
2686 * and whether the max offset is addressable by the page cache.
2687 */
2688 if ((ext4_blocks_count(es) >
2689 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2690 (ext4_blocks_count(es) >
2691 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002692 ext4_msg(sb, KERN_ERR, "filesystem"
Eric Sandeenbf43d842009-08-17 23:48:51 -04002693 " too large to mount safely on this system");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002694 if (sizeof(sector_t) < 8)
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002695 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
Eric Sandeenbf43d842009-08-17 23:48:51 -04002696 ret = -EFBIG;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002697 goto failed_mount;
2698 }
2699
Mingming Cao617ba132006-10-11 01:20:53 -07002700 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2701 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002702
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002703 /* check blocks count against device size */
2704 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2705 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002706 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2707 "exceeds size of device (%llu blocks)",
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002708 ext4_blocks_count(es), blocks_count);
2709 goto failed_mount;
2710 }
2711
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002712 /*
2713 * It makes no sense for the first data block to be beyond the end
2714 * of the filesystem.
2715 */
2716 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002717 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2718 "block %u is beyond end of filesystem (%llu)",
2719 le32_to_cpu(es->s_first_data_block),
2720 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05002721 goto failed_mount;
2722 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002723 blocks_count = (ext4_blocks_count(es) -
2724 le32_to_cpu(es->s_first_data_block) +
2725 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2726 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002727 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002728 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002729 "(block count %llu, first data block %u, "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002730 "blocks per group %lu)", sbi->s_groups_count,
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002731 ext4_blocks_count(es),
2732 le32_to_cpu(es->s_first_data_block),
2733 EXT4_BLOCKS_PER_GROUP(sb));
2734 goto failed_mount;
2735 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002736 sbi->s_groups_count = blocks_count;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002737 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2738 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Mingming Cao617ba132006-10-11 01:20:53 -07002739 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2740 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002741 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002742 GFP_KERNEL);
2743 if (sbi->s_group_desc == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002744 ext4_msg(sb, KERN_ERR, "not enough memory");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002745 goto failed_mount;
2746 }
2747
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002748#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002749 if (ext4_proc_root)
2750 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002751#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002752
Pekka Enberg705895b2009-02-15 18:07:52 -05002753 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002754
2755 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002756 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 sbi->s_group_desc[i] = sb_bread(sb, block);
2758 if (!sbi->s_group_desc[i]) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002759 ext4_msg(sb, KERN_ERR,
2760 "can't read group descriptor %d", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761 db_count = i;
2762 goto failed_mount2;
2763 }
2764 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002765 if (!ext4_check_descriptors(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002766 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002767 goto failed_mount2;
2768 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002769 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2770 if (!ext4_fill_flex_info(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002771 ext4_msg(sb, KERN_ERR,
2772 "unable to initialize "
2773 "flex_bg meta info!");
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002774 goto failed_mount2;
2775 }
2776
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 sbi->s_gdb_count = db_count;
2778 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2779 spin_lock_init(&sbi->s_next_gen_lock);
2780
Peter Zijlstra833f4072007-10-16 23:25:45 -07002781 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2782 ext4_count_free_blocks(sb));
2783 if (!err) {
2784 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2785 ext4_count_free_inodes(sb));
2786 }
2787 if (!err) {
2788 err = percpu_counter_init(&sbi->s_dirs_counter,
2789 ext4_count_dirs(sb));
2790 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002791 if (!err) {
2792 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2793 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002794 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002795 ext4_msg(sb, KERN_ERR, "insufficient memory");
Peter Zijlstra833f4072007-10-16 23:25:45 -07002796 goto failed_mount3;
2797 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002798
Alex Tomasc9de5602008-01-29 00:19:52 -05002799 sbi->s_stripe = ext4_get_stripe_size(sbi);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002800 sbi->s_max_writeback_mb_bump = 128;
Alex Tomasc9de5602008-01-29 00:19:52 -05002801
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002802 /*
2803 * set up enough so that it can read an inode
2804 */
Theodore Ts'o9ca92382009-05-01 12:52:25 -04002805 if (!test_opt(sb, NOLOAD) &&
2806 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2807 sb->s_op = &ext4_sops;
2808 else
2809 sb->s_op = &ext4_nojournal_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07002810 sb->s_export_op = &ext4_export_ops;
2811 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002813 sb->s_qcop = &ext4_qctl_operations;
2814 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815#endif
2816 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002817 mutex_init(&sbi->s_orphan_lock);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04002818 mutex_init(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002819
2820 sb->s_root = NULL;
2821
2822 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002823 EXT4_HAS_INCOMPAT_FEATURE(sb,
2824 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825
2826 /*
2827 * The first inode we look at is the journal inode. Don't try
2828 * root first: it may be modified in the journal!
2829 */
2830 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002831 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2832 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002833 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002834 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2835 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002836 ext4_msg(sb, KERN_ERR, "required journal recovery "
2837 "suppressed and not mounted read-only");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002838 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002840 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2841 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2842 sbi->s_journal = NULL;
2843 needs_recovery = 0;
2844 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845 }
2846
Jose R. Santoseb40a092007-07-18 08:37:25 -04002847 if (ext4_blocks_count(es) > 0xffffffffULL &&
2848 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2849 JBD2_FEATURE_INCOMPAT_64BIT)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002850 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002851 goto failed_mount_wq;
Jose R. Santoseb40a092007-07-18 08:37:25 -04002852 }
2853
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002854 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2855 jbd2_journal_set_features(sbi->s_journal,
2856 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
Girish Shilamkar818d2762008-01-28 23:58:27 -05002857 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002858 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2859 jbd2_journal_set_features(sbi->s_journal,
2860 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
Girish Shilamkar818d2762008-01-28 23:58:27 -05002861 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2862 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002863 } else {
2864 jbd2_journal_clear_features(sbi->s_journal,
2865 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2866 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2867 }
Girish Shilamkar818d2762008-01-28 23:58:27 -05002868
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002869 /* We have now updated the journal if required, so we can
2870 * validate the data journaling mode. */
2871 switch (test_opt(sb, DATA_FLAGS)) {
2872 case 0:
2873 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002874 * capabilities: ORDERED_DATA if the journal can
2875 * cope, else JOURNAL_DATA
2876 */
Mingming Caodab291a2006-10-11 01:21:01 -07002877 if (jbd2_journal_check_available_features
2878 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002879 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2880 else
2881 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2882 break;
2883
Mingming Cao617ba132006-10-11 01:20:53 -07002884 case EXT4_MOUNT_ORDERED_DATA:
2885 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002886 if (!jbd2_journal_check_available_features
2887 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002888 ext4_msg(sb, KERN_ERR, "Journal does not support "
2889 "requested data journaling mode");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002890 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 }
2892 default:
2893 break;
2894 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002895 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896
Frank Mayhar03901312009-01-07 00:06:22 -05002897no_journal:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002898 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002899 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002900 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2901 "its supported only with writeback mode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002902 clear_opt(sbi->s_mount_opt, NOBH);
2903 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05002904 if (test_opt(sb, DIOREAD_NOLOCK)) {
2905 ext4_msg(sb, KERN_WARNING, "dioread_nolock option is "
2906 "not supported with nobh mode");
2907 goto failed_mount_wq;
2908 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002909 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04002910 EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
2911 if (!EXT4_SB(sb)->dio_unwritten_wq) {
2912 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
2913 goto failed_mount_wq;
2914 }
2915
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002917 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002918 * so we can safely mount the rest of the filesystem now.
2919 */
2920
David Howells1d1fe1e2008-02-07 00:15:37 -08002921 root = ext4_iget(sb, EXT4_ROOT_INO);
2922 if (IS_ERR(root)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002923 ext4_msg(sb, KERN_ERR, "get root inode failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08002924 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925 goto failed_mount4;
2926 }
2927 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002928 iput(root);
Eric Sandeenb31e1552009-06-04 17:36:36 -04002929 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002930 goto failed_mount4;
2931 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002932 sb->s_root = d_alloc_root(root);
2933 if (!sb->s_root) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002934 ext4_msg(sb, KERN_ERR, "get root dentry failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08002935 iput(root);
2936 ret = -ENOMEM;
2937 goto failed_mount4;
2938 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002940 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002941
2942 /* determine the minimum size of new large inodes, if present */
2943 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2944 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2945 EXT4_GOOD_OLD_INODE_SIZE;
2946 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2947 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2948 if (sbi->s_want_extra_isize <
2949 le16_to_cpu(es->s_want_extra_isize))
2950 sbi->s_want_extra_isize =
2951 le16_to_cpu(es->s_want_extra_isize);
2952 if (sbi->s_want_extra_isize <
2953 le16_to_cpu(es->s_min_extra_isize))
2954 sbi->s_want_extra_isize =
2955 le16_to_cpu(es->s_min_extra_isize);
2956 }
2957 }
2958 /* Check if enough inode space is available */
2959 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2960 sbi->s_inode_size) {
2961 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2962 EXT4_GOOD_OLD_INODE_SIZE;
Eric Sandeenb31e1552009-06-04 17:36:36 -04002963 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2964 "available");
Kalpak Shahef7f3832007-07-18 09:15:20 -04002965 }
2966
Theodore Ts'o90576c02009-09-29 15:51:30 -04002967 if (test_opt(sb, DELALLOC) &&
2968 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002969 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2970 "requested data journaling mode");
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002971 clear_opt(sbi->s_mount_opt, DELALLOC);
Theodore Ts'o90576c02009-09-29 15:51:30 -04002972 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05002973 if (test_opt(sb, DIOREAD_NOLOCK)) {
2974 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2975 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2976 "option - requested data journaling mode");
2977 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2978 }
2979 if (sb->s_blocksize < PAGE_SIZE) {
2980 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2981 "option - block size is too small");
2982 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2983 }
2984 }
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002985
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002986 err = ext4_setup_system_zone(sb);
2987 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002988 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2989 "zone (%d)\n", err);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002990 goto failed_mount4;
2991 }
2992
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002993 ext4_ext_init(sb);
2994 err = ext4_mb_init(sb, needs_recovery);
2995 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002996 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2997 err);
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002998 goto failed_mount4;
2999 }
3000
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04003001 sbi->s_kobj.kset = ext4_kset;
3002 init_completion(&sbi->s_kobj_unregister);
3003 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
3004 "%s", sb->s_id);
3005 if (err) {
3006 ext4_mb_release(sb);
3007 ext4_ext_release(sb);
3008 goto failed_mount4;
3009 };
3010
Mingming Cao617ba132006-10-11 01:20:53 -07003011 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3012 ext4_orphan_cleanup(sb, es);
3013 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05003014 if (needs_recovery) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003015 ext4_msg(sb, KERN_INFO, "recovery complete");
Frank Mayhar03901312009-01-07 00:06:22 -05003016 ext4_mark_recovery_complete(sb, es);
3017 }
3018 if (EXT4_SB(sb)->s_journal) {
3019 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
3020 descr = " journalled data mode";
3021 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
3022 descr = " ordered data mode";
3023 else
3024 descr = " writeback data mode";
3025 } else
3026 descr = "out journal";
3027
Eric Sandeenb31e1552009-06-04 17:36:36 -04003028 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003029
3030 lock_kernel();
3031 return 0;
3032
Mingming Cao617ba132006-10-11 01:20:53 -07003033cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034 if (!silent)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003035 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 goto failed_mount;
3037
3038failed_mount4:
Eric Sandeenb31e1552009-06-04 17:36:36 -04003039 ext4_msg(sb, KERN_ERR, "mount failed");
Mingming Cao4c0425f2009-09-28 15:48:41 -04003040 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
3041failed_mount_wq:
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003042 ext4_release_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003043 if (sbi->s_journal) {
3044 jbd2_journal_destroy(sbi->s_journal);
3045 sbi->s_journal = NULL;
3046 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047failed_mount3:
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04003048 if (sbi->s_flex_groups) {
3049 if (is_vmalloc_addr(sbi->s_flex_groups))
3050 vfree(sbi->s_flex_groups);
3051 else
3052 kfree(sbi->s_flex_groups);
3053 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054 percpu_counter_destroy(&sbi->s_freeblocks_counter);
3055 percpu_counter_destroy(&sbi->s_freeinodes_counter);
3056 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003057 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058failed_mount2:
3059 for (i = 0; i < db_count; i++)
3060 brelse(sbi->s_group_desc[i]);
3061 kfree(sbi->s_group_desc);
3062failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04003063 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003064 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003065 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003066#ifdef CONFIG_QUOTA
3067 for (i = 0; i < MAXQUOTAS; i++)
3068 kfree(sbi->s_qf_names[i]);
3069#endif
Mingming Cao617ba132006-10-11 01:20:53 -07003070 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 brelse(bh);
3072out_fail:
3073 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04003074 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 kfree(sbi);
3076 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08003077 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003078}
3079
3080/*
3081 * Setup any per-fs journal parameters now. We'll do this both on
3082 * initial mount, once the journal has been initialised but before we've
3083 * done any recovery; and again on any subsequent remount.
3084 */
Mingming Cao617ba132006-10-11 01:20:53 -07003085static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003086{
Mingming Cao617ba132006-10-11 01:20:53 -07003087 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088
Theodore Ts'o30773842009-01-03 20:27:38 -05003089 journal->j_commit_interval = sbi->s_commit_interval;
3090 journal->j_min_batch_time = sbi->s_min_batch_time;
3091 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003092
3093 spin_lock(&journal->j_state_lock);
3094 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07003095 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003096 else
Mingming Caodab291a2006-10-11 01:21:01 -07003097 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04003098 if (test_opt(sb, DATA_ERR_ABORT))
3099 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
3100 else
3101 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003102 spin_unlock(&journal->j_state_lock);
3103}
3104
Mingming Cao617ba132006-10-11 01:20:53 -07003105static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003106 unsigned int journal_inum)
3107{
3108 struct inode *journal_inode;
3109 journal_t *journal;
3110
Frank Mayhar03901312009-01-07 00:06:22 -05003111 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3112
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003113 /* First, test for the existence of a valid inode on disk. Bad
3114 * things happen if we iget() an unused inode, as the subsequent
3115 * iput() will try to delete it. */
3116
David Howells1d1fe1e2008-02-07 00:15:37 -08003117 journal_inode = ext4_iget(sb, journal_inum);
3118 if (IS_ERR(journal_inode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003119 ext4_msg(sb, KERN_ERR, "no journal found");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003120 return NULL;
3121 }
3122 if (!journal_inode->i_nlink) {
3123 make_bad_inode(journal_inode);
3124 iput(journal_inode);
Eric Sandeenb31e1552009-06-04 17:36:36 -04003125 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003126 return NULL;
3127 }
3128
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003129 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08003131 if (!S_ISREG(journal_inode->i_mode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003132 ext4_msg(sb, KERN_ERR, "invalid journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003133 iput(journal_inode);
3134 return NULL;
3135 }
3136
Mingming Caodab291a2006-10-11 01:21:01 -07003137 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003138 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003139 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003140 iput(journal_inode);
3141 return NULL;
3142 }
3143 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003144 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003145 return journal;
3146}
3147
Mingming Cao617ba132006-10-11 01:20:53 -07003148static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003149 dev_t j_dev)
3150{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003151 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07003153 ext4_fsblk_t start;
3154 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003155 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07003156 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003158 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159 struct block_device *bdev;
3160
Frank Mayhar03901312009-01-07 00:06:22 -05003161 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3162
Eric Sandeenb31e1552009-06-04 17:36:36 -04003163 bdev = ext4_blkdev_get(j_dev, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003164 if (bdev == NULL)
3165 return NULL;
3166
3167 if (bd_claim(bdev, sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003168 ext4_msg(sb, KERN_ERR,
3169 "failed to claim external journal device");
Al Viro9a1c3542008-02-22 20:40:24 -05003170 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171 return NULL;
3172 }
3173
3174 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04003175 hblock = bdev_logical_block_size(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176 if (blocksize < hblock) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003177 ext4_msg(sb, KERN_ERR,
3178 "blocksize too small for journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 goto out_bdev;
3180 }
3181
Mingming Cao617ba132006-10-11 01:20:53 -07003182 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3183 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184 set_blocksize(bdev, blocksize);
3185 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003186 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3187 "external journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003188 goto out_bdev;
3189 }
3190
Mingming Cao617ba132006-10-11 01:20:53 -07003191 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3192 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003193 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07003194 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003195 ext4_msg(sb, KERN_ERR, "external journal has "
3196 "bad superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003197 brelse(bh);
3198 goto out_bdev;
3199 }
3200
Mingming Cao617ba132006-10-11 01:20:53 -07003201 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003202 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003203 brelse(bh);
3204 goto out_bdev;
3205 }
3206
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003207 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003208 start = sb_block + 1;
3209 brelse(bh); /* we're done with the superblock */
3210
Mingming Caodab291a2006-10-11 01:21:01 -07003211 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003212 start, len, blocksize);
3213 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003214 ext4_msg(sb, KERN_ERR, "failed to create device journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003215 goto out_bdev;
3216 }
3217 journal->j_private = sb;
3218 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3219 wait_on_buffer(journal->j_sb_buffer);
3220 if (!buffer_uptodate(journal->j_sb_buffer)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003221 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222 goto out_journal;
3223 }
3224 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003225 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3226 "user (unsupported) - %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227 be32_to_cpu(journal->j_superblock->s_nr_users));
3228 goto out_journal;
3229 }
Mingming Cao617ba132006-10-11 01:20:53 -07003230 EXT4_SB(sb)->journal_bdev = bdev;
3231 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003232 return journal;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003233
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003234out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07003235 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07003237 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003238 return NULL;
3239}
3240
Mingming Cao617ba132006-10-11 01:20:53 -07003241static int ext4_load_journal(struct super_block *sb,
3242 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003243 unsigned long journal_devnum)
3244{
3245 journal_t *journal;
3246 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3247 dev_t journal_dev;
3248 int err = 0;
3249 int really_read_only;
3250
Frank Mayhar03901312009-01-07 00:06:22 -05003251 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3252
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003253 if (journal_devnum &&
3254 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003255 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3256 "numbers have changed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003257 journal_dev = new_decode_dev(journal_devnum);
3258 } else
3259 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3260
3261 really_read_only = bdev_read_only(sb->s_bdev);
3262
3263 /*
3264 * Are we loading a blank journal or performing recovery after a
3265 * crash? For recovery, we need to check in advance whether we
3266 * can get read-write access to the device.
3267 */
Mingming Cao617ba132006-10-11 01:20:53 -07003268 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003269 if (sb->s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003270 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3271 "required on readonly filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003272 if (really_read_only) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003273 ext4_msg(sb, KERN_ERR, "write access "
3274 "unavailable, cannot proceed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003275 return -EROFS;
3276 }
Eric Sandeenb31e1552009-06-04 17:36:36 -04003277 ext4_msg(sb, KERN_INFO, "write access will "
3278 "be enabled during recovery");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279 }
3280 }
3281
3282 if (journal_inum && journal_dev) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003283 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3284 "and inode journals!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285 return -EINVAL;
3286 }
3287
3288 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07003289 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 return -EINVAL;
3291 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003292 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003293 return -EINVAL;
3294 }
3295
Theodore Ts'o90576c02009-09-29 15:51:30 -04003296 if (!(journal->j_flags & JBD2_BARRIER))
Eric Sandeenb31e1552009-06-04 17:36:36 -04003297 ext4_msg(sb, KERN_INFO, "barriers disabled");
Theodore Ts'o4776004f2008-09-08 23:00:52 -04003298
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003299 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07003300 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003302 ext4_msg(sb, KERN_ERR, "error updating journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003303 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003304 return err;
3305 }
3306 }
3307
Mingming Cao617ba132006-10-11 01:20:53 -07003308 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07003309 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003310 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07003311 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312
3313 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003314 ext4_msg(sb, KERN_ERR, "error loading journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003315 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003316 return err;
3317 }
3318
Mingming Cao617ba132006-10-11 01:20:53 -07003319 EXT4_SB(sb)->s_journal = journal;
3320 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003321
3322 if (journal_devnum &&
3323 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3324 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003325
3326 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003327 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003328 }
3329
3330 return 0;
3331}
3332
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003333static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003334{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003335 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07003336 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003337 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003338
3339 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08003340 return error;
Theodore Ts'o914258b2008-10-06 21:35:40 -04003341 if (buffer_write_io_error(sbh)) {
3342 /*
3343 * Oh, dear. A previous attempt to write the
3344 * superblock failed. This could happen because the
3345 * USB device was yanked out. Or it could happen to
3346 * be a transient write error and maybe the block will
3347 * be remapped. Nothing we can do but to retry the
3348 * write and hope for the best.
3349 */
Eric Sandeenb31e1552009-06-04 17:36:36 -04003350 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3351 "superblock detected");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003352 clear_buffer_write_io_error(sbh);
3353 set_buffer_uptodate(sbh);
3354 }
Theodore Ts'o71290b32009-09-10 17:31:04 -04003355 /*
3356 * If the file system is mounted read-only, don't update the
3357 * superblock write time. This avoids updating the superblock
3358 * write time when we are mounting the root file system
3359 * read/only but we need to replay the journal; at that point,
3360 * for people who are east of GMT and who make their clock
3361 * tick in localtime for Windows bug-for-bug compatibility,
3362 * the clock is set in the future, and this will cause e2fsck
3363 * to complain and force a full file system check.
3364 */
3365 if (!(sb->s_flags & MS_RDONLY))
3366 es->s_wtime = cpu_to_le32(get_seconds());
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003367 es->s_kbytes_written =
3368 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3369 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3370 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003371 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3372 &EXT4_SB(sb)->s_freeblocks_counter));
3373 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3374 &EXT4_SB(sb)->s_freeinodes_counter));
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003375 sb->s_dirt = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003376 BUFFER_TRACE(sbh, "marking dirty");
3377 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003378 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08003379 error = sync_dirty_buffer(sbh);
3380 if (error)
3381 return error;
3382
3383 error = buffer_write_io_error(sbh);
3384 if (error) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003385 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3386 "superblock");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003387 clear_buffer_write_io_error(sbh);
3388 set_buffer_uptodate(sbh);
3389 }
3390 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08003391 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003392}
3393
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003394/*
3395 * Have we just finished recovery? If so, and if we are mounting (or
3396 * remounting) the filesystem readonly, then we will end up with a
3397 * consistent fs on disk. Record that fact.
3398 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003399static void ext4_mark_recovery_complete(struct super_block *sb,
3400 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003401{
Mingming Cao617ba132006-10-11 01:20:53 -07003402 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003403
Frank Mayhar03901312009-01-07 00:06:22 -05003404 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3405 BUG_ON(journal != NULL);
3406 return;
3407 }
Mingming Caodab291a2006-10-11 01:21:01 -07003408 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003409 if (jbd2_journal_flush(journal) < 0)
3410 goto out;
3411
Mingming Cao617ba132006-10-11 01:20:53 -07003412 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003413 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003414 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003415 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003416 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003417
3418out:
Mingming Caodab291a2006-10-11 01:21:01 -07003419 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003420}
3421
3422/*
3423 * If we are mounting (or read-write remounting) a filesystem whose journal
3424 * has recorded an error from a previous lifetime, move that error to the
3425 * main filesystem now.
3426 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003427static void ext4_clear_journal_err(struct super_block *sb,
3428 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003429{
3430 journal_t *journal;
3431 int j_errno;
3432 const char *errstr;
3433
Frank Mayhar03901312009-01-07 00:06:22 -05003434 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3435
Mingming Cao617ba132006-10-11 01:20:53 -07003436 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003437
3438 /*
3439 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003440 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003441 */
3442
Mingming Caodab291a2006-10-11 01:21:01 -07003443 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003444 if (j_errno) {
3445 char nbuf[16];
3446
Mingming Cao617ba132006-10-11 01:20:53 -07003447 errstr = ext4_decode_error(sb, j_errno, nbuf);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003448 ext4_warning(sb, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 "from previous mount: %s", errstr);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003450 ext4_warning(sb, "Marking fs in need of filesystem check.");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451
Mingming Cao617ba132006-10-11 01:20:53 -07003452 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3453 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003454 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455
Mingming Caodab291a2006-10-11 01:21:01 -07003456 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457 }
3458}
3459
3460/*
3461 * Force the running and committing transactions to commit,
3462 * and wait on the commit.
3463 */
Mingming Cao617ba132006-10-11 01:20:53 -07003464int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003465{
3466 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003467 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003468
3469 if (sb->s_flags & MS_RDONLY)
3470 return 0;
3471
Mingming Cao617ba132006-10-11 01:20:53 -07003472 journal = EXT4_SB(sb)->s_journal;
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003473 if (journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003474 ret = ext4_journal_force_commit(journal);
Frank Mayhar03901312009-01-07 00:06:22 -05003475
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003476 return ret;
3477}
3478
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003479static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003480{
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003481 lock_super(sb);
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003482 ext4_commit_super(sb, 1);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003483 unlock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003484}
3485
Mingming Cao617ba132006-10-11 01:20:53 -07003486static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003487{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003488 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05003489 tid_t target;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003490 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003491
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003492 trace_ext4_sync_fs(sb, wait);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003493 flush_workqueue(sbi->dio_unwritten_wq);
3494 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003495 if (wait)
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003496 jbd2_log_wait_commit(sbi->s_journal, target);
Frank Mayhar03901312009-01-07 00:06:22 -05003497 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003498 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003499}
3500
3501/*
3502 * LVM calls this function before a (read-only) snapshot is created. This
3503 * gives us a chance to flush the journal completely and mark the fs clean.
3504 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003505static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003506{
Takashi Satoc4be0c12009-01-09 16:40:58 -08003507 int error = 0;
3508 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003509
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003510 if (sb->s_flags & MS_RDONLY)
3511 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003512
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003513 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003514
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003515 /* Now we set up the journal barrier. */
3516 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003517
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003518 /*
3519 * Don't clear the needs_recovery flag if we failed to flush
3520 * the journal.
3521 */
3522 error = jbd2_journal_flush(journal);
3523 if (error < 0) {
3524 out:
3525 jbd2_journal_unlock_updates(journal);
3526 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003527 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003528
3529 /* Journal blocked and flushed, clear needs_recovery flag. */
3530 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3531 error = ext4_commit_super(sb, 1);
3532 if (error)
3533 goto out;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003534 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003535}
3536
3537/*
3538 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3539 * flag here, even though the filesystem is not technically dirty yet.
3540 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003541static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003543 if (sb->s_flags & MS_RDONLY)
3544 return 0;
3545
3546 lock_super(sb);
3547 /* Reset the needs_recovery flag before the fs is unlocked. */
3548 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3549 ext4_commit_super(sb, 1);
3550 unlock_super(sb);
3551 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Takashi Satoc4be0c12009-01-09 16:40:58 -08003552 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003553}
3554
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003555static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003556{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003557 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003558 struct ext4_sb_info *sbi = EXT4_SB(sb);
3559 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003560 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003561 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003562 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003563 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003564 int err;
3565#ifdef CONFIG_QUOTA
3566 int i;
3567#endif
3568
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003569 lock_kernel();
3570
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003571 /* Store the original options */
Al Virobbd68512009-05-06 10:43:07 -04003572 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003573 old_sb_flags = sb->s_flags;
3574 old_opts.s_mount_opt = sbi->s_mount_opt;
3575 old_opts.s_resuid = sbi->s_resuid;
3576 old_opts.s_resgid = sbi->s_resgid;
3577 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003578 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3579 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003580#ifdef CONFIG_QUOTA
3581 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3582 for (i = 0; i < MAXQUOTAS; i++)
3583 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3584#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003585 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3586 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003587
3588 /*
3589 * Allow the "check" option to be passed as a remount option.
3590 */
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003591 if (!parse_options(data, sb, NULL, &journal_ioprio,
3592 &n_blocks_count, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003593 err = -EINVAL;
3594 goto restore_opts;
3595 }
3596
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003597 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003598 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003599
3600 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05003601 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003602
3603 es = sbi->s_es;
3604
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003605 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05003606 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003607 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3608 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003609
3610 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003611 n_blocks_count > ext4_blocks_count(es)) {
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003612 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003613 err = -EROFS;
3614 goto restore_opts;
3615 }
3616
3617 if (*flags & MS_RDONLY) {
3618 /*
3619 * First of all, the unconditional stuff we have to do
3620 * to disable replay of the journal when we next remount
3621 */
3622 sb->s_flags |= MS_RDONLY;
3623
3624 /*
3625 * OK, test if we are remounting a valid rw partition
3626 * readonly, and if so set the rdonly flag and then
3627 * mark the partition as valid again.
3628 */
Mingming Cao617ba132006-10-11 01:20:53 -07003629 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3630 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003631 es->s_state = cpu_to_le16(sbi->s_mount_state);
3632
Theodore Ts'oa63c9eb2009-05-01 01:59:42 -04003633 if (sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003634 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003635 } else {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003636 /* Make sure we can mount this feature set readwrite */
3637 if (!ext4_feature_set_ok(sb, 0)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003638 err = -EROFS;
3639 goto restore_opts;
3640 }
Eric Sandeenead65962007-02-10 01:46:08 -08003641 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003642 * Make sure the group descriptor checksums
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003643 * are sane. If they aren't, refuse to remount r/w.
Theodore Ts'o8a266462008-07-26 14:34:21 -04003644 */
3645 for (g = 0; g < sbi->s_groups_count; g++) {
3646 struct ext4_group_desc *gdp =
3647 ext4_get_group_desc(sb, g, NULL);
3648
3649 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003650 ext4_msg(sb, KERN_ERR,
3651 "ext4_remount: Checksum for group %u failed (%u!=%u)",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003652 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3653 le16_to_cpu(gdp->bg_checksum));
3654 err = -EINVAL;
3655 goto restore_opts;
3656 }
3657 }
3658
3659 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003660 * If we have an unprocessed orphan list hanging
3661 * around from a previously readonly bdev mount,
3662 * require a full umount/remount for now.
3663 */
3664 if (es->s_last_orphan) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003665 ext4_msg(sb, KERN_WARNING, "Couldn't "
Eric Sandeenead65962007-02-10 01:46:08 -08003666 "remount RDWR because of unprocessed "
3667 "orphan inode list. Please "
Eric Sandeenb31e1552009-06-04 17:36:36 -04003668 "umount/remount instead");
Eric Sandeenead65962007-02-10 01:46:08 -08003669 err = -EINVAL;
3670 goto restore_opts;
3671 }
3672
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003673 /*
3674 * Mounting a RDONLY partition read-write, so reread
3675 * and store the current valid flag. (It may have
3676 * been changed by e2fsck since we originally mounted
3677 * the partition.)
3678 */
Frank Mayhar03901312009-01-07 00:06:22 -05003679 if (sbi->s_journal)
3680 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003682 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003683 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003684 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685 sb->s_flags &= ~MS_RDONLY;
3686 }
3687 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003688 ext4_setup_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003689 if (sbi->s_journal == NULL)
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003690 ext4_commit_super(sb, 1);
Frank Mayhar03901312009-01-07 00:06:22 -05003691
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003692#ifdef CONFIG_QUOTA
3693 /* Release old quota file names */
3694 for (i = 0; i < MAXQUOTAS; i++)
3695 if (old_opts.s_qf_names[i] &&
3696 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3697 kfree(old_opts.s_qf_names[i]);
3698#endif
Al Virobbd68512009-05-06 10:43:07 -04003699 unlock_super(sb);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003700 unlock_kernel();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003701 return 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003702
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003703restore_opts:
3704 sb->s_flags = old_sb_flags;
3705 sbi->s_mount_opt = old_opts.s_mount_opt;
3706 sbi->s_resuid = old_opts.s_resuid;
3707 sbi->s_resgid = old_opts.s_resgid;
3708 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003709 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3710 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003711#ifdef CONFIG_QUOTA
3712 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3713 for (i = 0; i < MAXQUOTAS; i++) {
3714 if (sbi->s_qf_names[i] &&
3715 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3716 kfree(sbi->s_qf_names[i]);
3717 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3718 }
3719#endif
Al Virobbd68512009-05-06 10:43:07 -04003720 unlock_super(sb);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003721 unlock_kernel();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003722 return err;
3723}
3724
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003725static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726{
3727 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003728 struct ext4_sb_info *sbi = EXT4_SB(sb);
3729 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003730 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003731
Badari Pulavarty5e700302007-07-15 23:42:00 -07003732 if (test_opt(sb, MINIX_DF)) {
3733 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003734 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04003735 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003736 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003737
3738 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003739 * Compute the overhead (FS structures). This is constant
3740 * for a given filesystem unless the number of block groups
3741 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003742 */
3743
3744 /*
3745 * All of the blocks before first_data_block are
3746 * overhead
3747 */
3748 overhead = le32_to_cpu(es->s_first_data_block);
3749
3750 /*
3751 * Add the overhead attributed to the superblock and
3752 * block group descriptors. If the sparse superblocks
3753 * feature is turned on, then not all groups have this.
3754 */
3755 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003756 overhead += ext4_bg_has_super(sb, i) +
3757 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758 cond_resched();
3759 }
3760
3761 /*
3762 * Every block group has an inode bitmap, a block
3763 * bitmap, and an inode table.
3764 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003765 overhead += ngroups * (2 + sbi->s_itb_per_group);
3766 sbi->s_overhead_last = overhead;
3767 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003768 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003769 }
3770
Mingming Cao617ba132006-10-11 01:20:53 -07003771 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003772 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003773 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003774 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3775 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003776 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3777 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003778 buf->f_bavail = 0;
3779 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003780 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07003781 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003782 fsid = le64_to_cpup((void *)es->s_uuid) ^
3783 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3784 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3785 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003786
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003787 return 0;
3788}
3789
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003790/* Helper function for writing quotas on sync - we need to start transaction
3791 * before quota file is locked for write. Otherwise the are possible deadlocks:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003792 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003793 * ext4_create() quota_sync()
Jan Karaa269eb12009-01-26 17:04:39 +01003794 * jbd2_journal_start() write_dquot()
Christoph Hellwig871a2932010-03-03 09:05:07 -05003795 * dquot_initialize() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003796 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003797 *
3798 */
3799
3800#ifdef CONFIG_QUOTA
3801
3802static inline struct inode *dquot_to_inode(struct dquot *dquot)
3803{
3804 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3805}
3806
Mingming Cao617ba132006-10-11 01:20:53 -07003807static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003808{
3809 int ret, err;
3810 handle_t *handle;
3811 struct inode *inode;
3812
3813 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003814 handle = ext4_journal_start(inode,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003815 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003816 if (IS_ERR(handle))
3817 return PTR_ERR(handle);
3818 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003819 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003820 if (!ret)
3821 ret = err;
3822 return ret;
3823}
3824
Mingming Cao617ba132006-10-11 01:20:53 -07003825static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003826{
3827 int ret, err;
3828 handle_t *handle;
3829
Mingming Cao617ba132006-10-11 01:20:53 -07003830 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003831 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832 if (IS_ERR(handle))
3833 return PTR_ERR(handle);
3834 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003835 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836 if (!ret)
3837 ret = err;
3838 return ret;
3839}
3840
Mingming Cao617ba132006-10-11 01:20:53 -07003841static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003842{
3843 int ret, err;
3844 handle_t *handle;
3845
Mingming Cao617ba132006-10-11 01:20:53 -07003846 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003847 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003848 if (IS_ERR(handle)) {
3849 /* Release dquot anyway to avoid endless cycle in dqput() */
3850 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003851 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003852 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003853 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003854 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855 if (!ret)
3856 ret = err;
3857 return ret;
3858}
3859
Mingming Cao617ba132006-10-11 01:20:53 -07003860static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003861{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003862 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003863 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3864 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003865 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003866 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867 } else {
3868 return dquot_mark_dquot_dirty(dquot);
3869 }
3870}
3871
Mingming Cao617ba132006-10-11 01:20:53 -07003872static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003873{
3874 int ret, err;
3875 handle_t *handle;
3876
3877 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003878 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879 if (IS_ERR(handle))
3880 return PTR_ERR(handle);
3881 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003882 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003883 if (!ret)
3884 ret = err;
3885 return ret;
3886}
3887
3888/*
3889 * Turn on quotas during mount time - we need to find
3890 * the quota file and such...
3891 */
Mingming Cao617ba132006-10-11 01:20:53 -07003892static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893{
Mingming Cao617ba132006-10-11 01:20:53 -07003894 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003895 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003896}
3897
3898/*
3899 * Standard function to be called on quota_on
3900 */
Mingming Cao617ba132006-10-11 01:20:53 -07003901static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003902 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003903{
3904 int err;
Al Viro82646132008-08-02 00:57:06 -04003905 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003906
3907 if (!test_opt(sb, QUOTA))
3908 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003909 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003910 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003911 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003912
Al Viro82646132008-08-02 00:57:06 -04003913 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003914 if (err)
3915 return err;
Jan Kara06235432008-05-13 19:11:51 -04003916
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003917 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003918 if (path.mnt->mnt_sb != sb) {
3919 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003920 return -EXDEV;
3921 }
Jan Kara06235432008-05-13 19:11:51 -04003922 /* Journaling quota? */
3923 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003924 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003925 if (path.dentry->d_parent != sb->s_root)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003926 ext4_msg(sb, KERN_WARNING,
3927 "Quota file not on filesystem root. "
3928 "Journaled quota will not work");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003929 }
Jan Kara06235432008-05-13 19:11:51 -04003930
3931 /*
3932 * When we journal data on quota file, we have to flush journal to see
3933 * all updates to the file when we bypass pagecache...
3934 */
Frank Mayhar03901312009-01-07 00:06:22 -05003935 if (EXT4_SB(sb)->s_journal &&
3936 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003937 /*
3938 * We don't need to lock updates but journal_flush() could
3939 * otherwise be livelocked...
3940 */
3941 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003942 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003943 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003944 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003945 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003946 return err;
3947 }
Jan Kara06235432008-05-13 19:11:51 -04003948 }
3949
Al Viro82646132008-08-02 00:57:06 -04003950 err = vfs_quota_on_path(sb, type, format_id, &path);
3951 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003952 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953}
3954
3955/* Read data from quotafile - avoid pagecache and such because we cannot afford
3956 * acquiring the locks... As quota files are never truncated and quota code
3957 * itself serializes the operations (and noone else should touch the files)
3958 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003959static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003960 size_t len, loff_t off)
3961{
3962 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003963 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003964 int err = 0;
3965 int offset = off & (sb->s_blocksize - 1);
3966 int tocopy;
3967 size_t toread;
3968 struct buffer_head *bh;
3969 loff_t i_size = i_size_read(inode);
3970
3971 if (off > i_size)
3972 return 0;
3973 if (off+len > i_size)
3974 len = i_size-off;
3975 toread = len;
3976 while (toread > 0) {
3977 tocopy = sb->s_blocksize - offset < toread ?
3978 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003979 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003980 if (err)
3981 return err;
3982 if (!bh) /* A hole? */
3983 memset(data, 0, tocopy);
3984 else
3985 memcpy(data, bh->b_data+offset, tocopy);
3986 brelse(bh);
3987 offset = 0;
3988 toread -= tocopy;
3989 data += tocopy;
3990 blk++;
3991 }
3992 return len;
3993}
3994
3995/* Write to quotafile (we know the transaction is already started and has
3996 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003997static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003998 const char *data, size_t len, loff_t off)
3999{
4000 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004001 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004002 int err = 0;
4003 int offset = off & (sb->s_blocksize - 1);
Mingming Cao617ba132006-10-11 01:20:53 -07004004 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004005 struct buffer_head *bh;
4006 handle_t *handle = journal_current_handle();
4007
Frank Mayhar03901312009-01-07 00:06:22 -05004008 if (EXT4_SB(sb)->s_journal && !handle) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004009 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4010 " cancelled because transaction is not started",
Jan Kara9c3013e2007-09-11 15:23:29 -07004011 (unsigned long long)off, (unsigned long long)len);
4012 return -EIO;
4013 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004014 /*
4015 * Since we account only one data block in transaction credits,
4016 * then it is impossible to cross a block boundary.
4017 */
4018 if (sb->s_blocksize - offset < len) {
4019 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4020 " cancelled because not block aligned",
4021 (unsigned long long)off, (unsigned long long)len);
4022 return -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004023 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004024
4025 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4026 bh = ext4_bread(handle, inode, blk, 1, &err);
4027 if (!bh)
4028 goto out;
4029 if (journal_quota) {
4030 err = ext4_journal_get_write_access(handle, bh);
4031 if (err) {
4032 brelse(bh);
4033 goto out;
4034 }
4035 }
4036 lock_buffer(bh);
4037 memcpy(bh->b_data+offset, data, len);
4038 flush_dcache_page(bh->b_page);
4039 unlock_buffer(bh);
4040 if (journal_quota)
4041 err = ext4_handle_dirty_metadata(handle, NULL, bh);
4042 else {
4043 /* Always do at least ordered writes for quotas */
4044 err = ext4_jbd2_file_inode(handle, inode);
4045 mark_buffer_dirty(bh);
4046 }
4047 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004048out:
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004049 if (err) {
Jan Kara4d04e4f2008-07-04 09:59:34 -07004050 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004051 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07004052 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004053 if (inode->i_size < off + len) {
4054 i_size_write(inode, off + len);
Mingming Cao617ba132006-10-11 01:20:53 -07004055 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004056 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004057 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004058 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004059 mutex_unlock(&inode->i_mutex);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004060 return len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004061}
4062
4063#endif
4064
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004065static int ext4_get_sb(struct file_system_type *fs_type, int flags,
4066 const char *dev_name, void *data, struct vfsmount *mnt)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004067{
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004068 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004069}
4070
Theodore Ts'oa2142382009-12-09 21:09:58 -05004071#if !defined(CONTIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004072static struct file_system_type ext2_fs_type = {
4073 .owner = THIS_MODULE,
4074 .name = "ext2",
4075 .get_sb = ext4_get_sb,
4076 .kill_sb = kill_block_super,
4077 .fs_flags = FS_REQUIRES_DEV,
4078};
4079
4080static inline void register_as_ext2(void)
4081{
4082 int err = register_filesystem(&ext2_fs_type);
4083 if (err)
4084 printk(KERN_WARNING
4085 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
4086}
4087
4088static inline void unregister_as_ext2(void)
4089{
4090 unregister_filesystem(&ext2_fs_type);
4091}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004092MODULE_ALIAS("ext2");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004093#else
4094static inline void register_as_ext2(void) { }
4095static inline void unregister_as_ext2(void) { }
4096#endif
4097
Theodore Ts'oa2142382009-12-09 21:09:58 -05004098#if !defined(CONTIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004099static struct file_system_type ext3_fs_type = {
4100 .owner = THIS_MODULE,
4101 .name = "ext3",
4102 .get_sb = ext4_get_sb,
4103 .kill_sb = kill_block_super,
4104 .fs_flags = FS_REQUIRES_DEV,
4105};
4106
4107static inline void register_as_ext3(void)
4108{
4109 int err = register_filesystem(&ext3_fs_type);
4110 if (err)
4111 printk(KERN_WARNING
4112 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
4113}
4114
4115static inline void unregister_as_ext3(void)
4116{
4117 unregister_filesystem(&ext3_fs_type);
4118}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004119MODULE_ALIAS("ext3");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004120#else
4121static inline void register_as_ext3(void) { }
4122static inline void unregister_as_ext3(void) { }
4123#endif
4124
Theodore Ts'o03010a32008-10-10 20:02:48 -04004125static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004126 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04004127 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07004128 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004129 .kill_sb = kill_block_super,
4130 .fs_flags = FS_REQUIRES_DEV,
4131};
4132
Mingming Cao617ba132006-10-11 01:20:53 -07004133static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004134{
Alex Tomasc9de5602008-01-29 00:19:52 -05004135 int err;
4136
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004137 err = init_ext4_system_zone();
4138 if (err)
4139 return err;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004140 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4141 if (!ext4_kset)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004142 goto out4;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004143 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05004144 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004145 if (err)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004146 goto out3;
Alex Tomasc9de5602008-01-29 00:19:52 -05004147
4148 err = init_ext4_xattr();
4149 if (err)
4150 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151 err = init_inodecache();
4152 if (err)
4153 goto out1;
Theodore Ts'o24b58422009-12-07 14:08:51 -05004154 register_as_ext2();
4155 register_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004156 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004157 if (err)
4158 goto out;
4159 return 0;
4160out:
Theodore Ts'o24b58422009-12-07 14:08:51 -05004161 unregister_as_ext2();
4162 unregister_as_ext3();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004163 destroy_inodecache();
4164out1:
Mingming Cao617ba132006-10-11 01:20:53 -07004165 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004166out2:
4167 exit_ext4_mballoc();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004168out3:
4169 remove_proc_entry("fs/ext4", NULL);
4170 kset_unregister(ext4_kset);
4171out4:
4172 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004173 return err;
4174}
4175
Mingming Cao617ba132006-10-11 01:20:53 -07004176static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004177{
Theodore Ts'o24b58422009-12-07 14:08:51 -05004178 unregister_as_ext2();
4179 unregister_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004180 unregister_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004181 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07004182 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004183 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004184 remove_proc_entry("fs/ext4", NULL);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004185 kset_unregister(ext4_kset);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004186 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004187}
4188
4189MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05004190MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004191MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07004192module_init(init_ext4_fs)
4193module_exit(exit_ext4_fs)