blob: 5e8f9077b0fcddddbd7c29052e9cfecc962d6d80 [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
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400350static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700351 char nbuf[16])
352{
353 char *errstr = NULL;
354
355 switch (errno) {
356 case -EIO:
357 errstr = "IO failure";
358 break;
359 case -ENOMEM:
360 errstr = "Out of memory";
361 break;
362 case -EROFS:
Theodore Ts'o78f1ddb2009-07-27 23:09:47 -0400363 if (!sb || (EXT4_SB(sb)->s_journal &&
364 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365 errstr = "Journal has aborted";
366 else
367 errstr = "Readonly filesystem";
368 break;
369 default:
370 /* If the caller passed in an extra buffer for unknown
371 * errors, textualise them now. Else we just return
372 * NULL. */
373 if (nbuf) {
374 /* Check for truncated error codes... */
375 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
376 errstr = nbuf;
377 }
378 break;
379 }
380
381 return errstr;
382}
383
Mingming Cao617ba132006-10-11 01:20:53 -0700384/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700385 * automatically and invokes the appropriate error response. */
386
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400387void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388{
389 char nbuf[16];
390 const char *errstr;
391
392 /* Special case: if the error is EROFS, and we're not already
393 * inside a transaction, then there's really no point in logging
394 * an error. */
395 if (errno == -EROFS && journal_current_handle() == NULL &&
396 (sb->s_flags & MS_RDONLY))
397 return;
398
Mingming Cao617ba132006-10-11 01:20:53 -0700399 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400400 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
401 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402
Mingming Cao617ba132006-10-11 01:20:53 -0700403 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404}
405
406/*
Mingming Cao617ba132006-10-11 01:20:53 -0700407 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408 * abort function may be used to deal with unrecoverable failures such
409 * as journal IO errors or ENOMEM at a critical moment in log management.
410 *
411 * We unconditionally force the filesystem into an ABORT|READONLY state,
412 * unless the error response on the fs has been set to panic in which
413 * case we take the easy way out and panic immediately.
414 */
415
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400416void ext4_abort(struct super_block *sb, const char *function,
417 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418{
419 va_list args;
420
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700421 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400422 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700423 vprintk(fmt, args);
424 printk("\n");
425 va_end(args);
426
427 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700428 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700429
430 if (sb->s_flags & MS_RDONLY)
431 return;
432
Eric Sandeenb31e1552009-06-04 17:36:36 -0400433 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Mingming Cao617ba132006-10-11 01:20:53 -0700434 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 sb->s_flags |= MS_RDONLY;
Theodore Ts'o4ab2f152009-06-13 10:09:36 -0400436 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400437 if (EXT4_SB(sb)->s_journal)
438 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439}
440
Eric Sandeenb31e1552009-06-04 17:36:36 -0400441void ext4_msg (struct super_block * sb, const char *prefix,
442 const char *fmt, ...)
443{
444 va_list args;
445
446 va_start(args, fmt);
447 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
448 vprintk(fmt, args);
449 printk("\n");
450 va_end(args);
451}
452
Eric Sandeen12062dd2010-02-15 14:19:27 -0500453void __ext4_warning(struct super_block *sb, const char *function,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400454 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700455{
456 va_list args;
457
458 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700459 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 sb->s_id, function);
461 vprintk(fmt, args);
462 printk("\n");
463 va_end(args);
464}
465
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500466void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400467 const char *function, const char *fmt, ...)
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500468__releases(bitlock)
469__acquires(bitlock)
470{
471 va_list args;
472 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
473
474 va_start(args, fmt);
475 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
476 vprintk(fmt, args);
477 printk("\n");
478 va_end(args);
479
480 if (test_opt(sb, ERRORS_CONT)) {
481 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
482 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400483 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500484 return;
485 }
486 ext4_unlock_group(sb, grp);
487 ext4_handle_error(sb);
488 /*
489 * We only get here in the ERRORS_RO case; relocking the group
490 * may be dangerous, but nothing bad will happen since the
491 * filesystem will have already been marked read/only and the
492 * journal has been aborted. We return 1 as a hint to callers
493 * who might what to use the return value from
494 * ext4_grp_locked_error() to distinguish beween the
495 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
496 * aggressively from the ext4 function in question, with a
497 * more appropriate error code.
498 */
499 ext4_lock_group(sb, grp);
500 return;
501}
502
Mingming Cao617ba132006-10-11 01:20:53 -0700503void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700504{
Mingming Cao617ba132006-10-11 01:20:53 -0700505 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700506
Mingming Cao617ba132006-10-11 01:20:53 -0700507 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700508 return;
509
Eric Sandeen12062dd2010-02-15 14:19:27 -0500510 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 "updating to rev %d because of new feature flag, "
512 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700513 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514
Mingming Cao617ba132006-10-11 01:20:53 -0700515 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
516 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
517 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 /* leave es->s_feature_*compat flags alone */
519 /* es->s_uuid will be set by e2fsck if empty */
520
521 /*
522 * The rest of the superblock fields should be zero, and if not it
523 * means they are likely already in use, so leave them alone. We
524 * can leave it up to e2fsck to clean up any inconsistencies there.
525 */
526}
527
528/*
529 * Open the external journal device
530 */
Eric Sandeenb31e1552009-06-04 17:36:36 -0400531static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532{
533 struct block_device *bdev;
534 char b[BDEVNAME_SIZE];
535
536 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
537 if (IS_ERR(bdev))
538 goto fail;
539 return bdev;
540
541fail:
Eric Sandeenb31e1552009-06-04 17:36:36 -0400542 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543 __bdevname(dev, b), PTR_ERR(bdev));
544 return NULL;
545}
546
547/*
548 * Release the journal device
549 */
Mingming Cao617ba132006-10-11 01:20:53 -0700550static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551{
552 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500553 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554}
555
Mingming Cao617ba132006-10-11 01:20:53 -0700556static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557{
558 struct block_device *bdev;
559 int ret = -ENODEV;
560
561 bdev = sbi->journal_bdev;
562 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700563 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700564 sbi->journal_bdev = NULL;
565 }
566 return ret;
567}
568
569static inline struct inode *orphan_list_entry(struct list_head *l)
570{
Mingming Cao617ba132006-10-11 01:20:53 -0700571 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572}
573
Mingming Cao617ba132006-10-11 01:20:53 -0700574static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700575{
576 struct list_head *l;
577
Eric Sandeenb31e1552009-06-04 17:36:36 -0400578 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
579 le32_to_cpu(sbi->s_es->s_last_orphan));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580
581 printk(KERN_ERR "sb_info orphan list:\n");
582 list_for_each(l, &sbi->s_orphan) {
583 struct inode *inode = orphan_list_entry(l);
584 printk(KERN_ERR " "
585 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
586 inode->i_sb->s_id, inode->i_ino, inode,
587 inode->i_mode, inode->i_nlink,
588 NEXT_ORPHAN(inode));
589 }
590}
591
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400592static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
Mingming Cao617ba132006-10-11 01:20:53 -0700594 struct ext4_sb_info *sbi = EXT4_SB(sb);
595 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400596 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597
Mingming Cao4c0425f2009-09-28 15:48:41 -0400598 flush_workqueue(sbi->dio_unwritten_wq);
599 destroy_workqueue(sbi->dio_unwritten_wq);
600
Al Viroa9e220f2009-05-05 22:10:44 -0400601 lock_super(sb);
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200602 lock_kernel();
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200603 if (sb->s_dirt)
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200604 ext4_commit_super(sb, 1);
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200605
Frank Mayhar03901312009-01-07 00:06:22 -0500606 if (sbi->s_journal) {
607 err = jbd2_journal_destroy(sbi->s_journal);
608 sbi->s_journal = NULL;
609 if (err < 0)
610 ext4_abort(sb, __func__,
611 "Couldn't clean up the journal");
612 }
Josef Bacikd4edac32009-12-08 21:48:58 -0500613
614 ext4_release_system_zone(sb);
615 ext4_mb_release(sb);
616 ext4_ext_release(sb);
617 ext4_xattr_put_super(sb);
618
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700619 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700620 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 es->s_state = cpu_to_le16(sbi->s_mount_state);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400622 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700623 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400624 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400625 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400626 }
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400627 kobject_del(&sbi->s_kobj);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628
629 for (i = 0; i < sbi->s_gdb_count; i++)
630 brelse(sbi->s_group_desc[i]);
631 kfree(sbi->s_group_desc);
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -0400632 if (is_vmalloc_addr(sbi->s_flex_groups))
633 vfree(sbi->s_flex_groups);
634 else
635 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700636 percpu_counter_destroy(&sbi->s_freeblocks_counter);
637 percpu_counter_destroy(&sbi->s_freeinodes_counter);
638 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400639 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 brelse(sbi->s_sbh);
641#ifdef CONFIG_QUOTA
642 for (i = 0; i < MAXQUOTAS; i++)
643 kfree(sbi->s_qf_names[i]);
644#endif
645
646 /* Debugging code just in case the in-memory inode orphan list
647 * isn't empty. The on-disk one can be non-empty if we've
648 * detected an error and taken the fs readonly, but the
649 * in-memory list had better be clean by this point. */
650 if (!list_empty(&sbi->s_orphan))
651 dump_orphan_list(sb, sbi);
652 J_ASSERT(list_empty(&sbi->s_orphan));
653
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700654 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
656 /*
657 * Invalidate the journal device's buffers. We don't want them
658 * floating about in memory - the physical journal device may
659 * hotswapped, and it breaks the `ro-after' testing code.
660 */
661 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700662 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700663 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 }
665 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400666 /*
667 * Now that we are completely done shutting down the
668 * superblock, we need to actually destroy the kobject.
669 */
670 unlock_kernel();
671 unlock_super(sb);
672 kobject_put(&sbi->s_kobj);
673 wait_for_completion(&sbi->s_kobj_unregister);
Pekka Enberg705895b2009-02-15 18:07:52 -0500674 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 kfree(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700676}
677
Christoph Lametere18b8902006-12-06 20:33:20 -0800678static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679
680/*
681 * Called inside transaction, so use GFP_NOFS
682 */
Mingming Cao617ba132006-10-11 01:20:53 -0700683static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700684{
Mingming Cao617ba132006-10-11 01:20:53 -0700685 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800687 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700688 if (!ei)
689 return NULL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400690
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700691 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400692 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700693 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500694 INIT_LIST_HEAD(&ei->i_prealloc_list);
695 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500696 /*
697 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
698 * therefore it can be null here. Don't check it, just initialize
699 * jinode.
700 */
Jan Kara678aaf42008-07-11 19:27:31 -0400701 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400702 ei->i_reserved_data_blocks = 0;
703 ei->i_reserved_meta_blocks = 0;
704 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500705 ei->i_da_metadata_calc_len = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400706 ei->i_delalloc_reserved_flag = 0;
707 spin_lock_init(&(ei->i_block_reservation_lock));
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300708#ifdef CONFIG_QUOTA
709 ei->i_reserved_quota = 0;
710#endif
Jiaying Zhangc7064ef2010-03-02 13:28:44 -0500711 INIT_LIST_HEAD(&ei->i_completed_io_list);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500712 spin_lock_init(&ei->i_completed_io_lock);
Mingming Cao8d5d02e2009-09-28 15:48:29 -0400713 ei->cur_aio_dio = NULL;
Jan Karab436b9b2009-12-08 23:51:10 -0500714 ei->i_sync_tid = 0;
715 ei->i_datasync_tid = 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400716
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700717 return &ei->vfs_inode;
718}
719
Mingming Cao617ba132006-10-11 01:20:53 -0700720static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700722 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400723 ext4_msg(inode->i_sb, KERN_ERR,
724 "Inode %lu (%p): orphan list check failed!",
725 inode->i_ino, EXT4_I(inode));
Vasily Averin9f7dd932007-07-15 23:40:45 -0700726 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
727 EXT4_I(inode), sizeof(struct ext4_inode_info),
728 true);
729 dump_stack();
730 }
Mingming Cao617ba132006-10-11 01:20:53 -0700731 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700732}
733
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700734static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735{
Mingming Cao617ba132006-10-11 01:20:53 -0700736 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700737
Christoph Lametera35afb82007-05-16 22:10:57 -0700738 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400739#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700740 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500742 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700743 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700744}
745
746static int init_inodecache(void)
747{
Mingming Cao617ba132006-10-11 01:20:53 -0700748 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
749 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700750 0, (SLAB_RECLAIM_ACCOUNT|
751 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900752 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700753 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 return -ENOMEM;
755 return 0;
756}
757
758static void destroy_inodecache(void)
759{
Mingming Cao617ba132006-10-11 01:20:53 -0700760 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761}
762
Mingming Cao617ba132006-10-11 01:20:53 -0700763static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764{
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400765 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500766 if (EXT4_JOURNAL(inode))
767 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400768 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700769}
770
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400771static inline void ext4_show_quota_options(struct seq_file *seq,
772 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773{
774#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700775 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776
Jan Kara5a20bdf2009-11-30 23:58:32 +0100777 if (sbi->s_jquota_fmt) {
778 char *fmtname = "";
779
780 switch (sbi->s_jquota_fmt) {
781 case QFMT_VFS_OLD:
782 fmtname = "vfsold";
783 break;
784 case QFMT_VFS_V0:
785 fmtname = "vfsv0";
786 break;
787 case QFMT_VFS_V1:
788 fmtname = "vfsv1";
789 break;
790 }
791 seq_printf(seq, ",jqfmt=%s", fmtname);
792 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793
794 if (sbi->s_qf_names[USRQUOTA])
795 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
796
797 if (sbi->s_qf_names[GRPQUOTA])
798 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
799
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500800 if (test_opt(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700801 seq_puts(seq, ",usrquota");
802
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500803 if (test_opt(sb, GRPQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700804 seq_puts(seq, ",grpquota");
805#endif
806}
807
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700808/*
809 * Show an option if
810 * - it's set to a non-default value OR
811 * - if the per-sb default is different from the global default
812 */
Mingming Cao617ba132006-10-11 01:20:53 -0700813static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700814{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500815 int def_errors;
816 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700817 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700818 struct ext4_sb_info *sbi = EXT4_SB(sb);
819 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700820
821 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500822 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700823
824 if (sbi->s_sb_block != 1)
825 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
826 if (test_opt(sb, MINIX_DF))
827 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500828 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700829 seq_puts(seq, ",grpid");
830 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
831 seq_puts(seq, ",nogrpid");
832 if (sbi->s_resuid != EXT4_DEF_RESUID ||
833 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
834 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
835 }
836 if (sbi->s_resgid != EXT4_DEF_RESGID ||
837 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
838 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
839 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500840 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700841 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500842 def_errors == EXT4_ERRORS_CONTINUE) {
843 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700844 }
845 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500846 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500847 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500848 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700849 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500850 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700851 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500852 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700853 seq_puts(seq, ",debug");
854 if (test_opt(sb, OLDALLOC))
855 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400856#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500857 if (test_opt(sb, XATTR_USER) &&
858 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700859 seq_puts(seq, ",user_xattr");
860 if (!test_opt(sb, XATTR_USER) &&
861 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
862 seq_puts(seq, ",nouser_xattr");
863 }
864#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400865#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500866 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700867 seq_puts(seq, ",acl");
868 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
869 seq_puts(seq, ",noacl");
870#endif
Theodore Ts'o30773842009-01-03 20:27:38 -0500871 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700872 seq_printf(seq, ",commit=%u",
873 (unsigned) (sbi->s_commit_interval / HZ));
874 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500875 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
876 seq_printf(seq, ",min_batch_time=%u",
877 (unsigned) sbi->s_min_batch_time);
878 }
879 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
880 seq_printf(seq, ",max_batch_time=%u",
881 (unsigned) sbi->s_min_batch_time);
882 }
883
Eric Sandeen571640c2008-05-26 12:29:46 -0400884 /*
885 * We're changing the default of barrier mount option, so
886 * let's always display its mount state so it's clear what its
887 * status is.
888 */
889 seq_puts(seq, ",barrier=");
890 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400891 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
892 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700893 if (test_opt(sb, NOBH))
894 seq_puts(seq, ",nobh");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500895 if (test_opt(sb, I_VERSION))
896 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400897 if (!test_opt(sb, DELALLOC))
898 seq_puts(seq, ",nodelalloc");
899
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500901 if (sbi->s_stripe)
902 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500903 /*
904 * journal mode get enabled in different ways
905 * So just print the value even if we didn't specify it
906 */
Mingming Cao617ba132006-10-11 01:20:53 -0700907 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700909 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700911 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 seq_puts(seq, ",data=writeback");
913
Theodore Ts'o240799c2008-10-09 23:53:47 -0400914 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
915 seq_printf(seq, ",inode_readahead_blks=%u",
916 sbi->s_inode_readahead_blks);
917
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400918 if (test_opt(sb, DATA_ERR_ABORT))
919 seq_puts(seq, ",data_err=abort");
920
Theodore Ts'oafd46722009-03-16 23:12:23 -0400921 if (test_opt(sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o06705bf2009-03-28 10:59:57 -0400922 seq_puts(seq, ",noauto_da_alloc");
Theodore Ts'oafd46722009-03-16 23:12:23 -0400923
Eric Sandeen5328e632009-11-19 14:25:42 -0500924 if (test_opt(sb, DISCARD))
925 seq_puts(seq, ",discard");
926
Eric Sandeene3bb52a2009-11-19 14:28:50 -0500927 if (test_opt(sb, NOLOAD))
928 seq_puts(seq, ",norecovery");
929
Jiaying Zhang744692d2010-03-04 16:14:02 -0500930 if (test_opt(sb, DIOREAD_NOLOCK))
931 seq_puts(seq, ",dioread_nolock");
932
Mingming Cao617ba132006-10-11 01:20:53 -0700933 ext4_show_quota_options(seq, sb);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400934
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700935 return 0;
936}
937
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700938static struct inode *ext4_nfs_get_inode(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400939 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700941 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942
Mingming Cao617ba132006-10-11 01:20:53 -0700943 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700945 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 return ERR_PTR(-ESTALE);
947
948 /* iget isn't really right if the inode is currently unallocated!!
949 *
Mingming Cao617ba132006-10-11 01:20:53 -0700950 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 * deleted, so we should be safe.
952 *
953 * Currently we don't know the generation for parent directory, so
954 * a generation of 0 means "accept any"
955 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800956 inode = ext4_iget(sb, ino);
957 if (IS_ERR(inode))
958 return ERR_CAST(inode);
959 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 iput(inode);
961 return ERR_PTR(-ESTALE);
962 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700963
964 return inode;
965}
966
967static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400968 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700969{
970 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
971 ext4_nfs_get_inode);
972}
973
974static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400975 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700976{
977 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
978 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979}
980
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500981/*
982 * Try to release metadata pages (indirect blocks, directories) which are
983 * mapped via the block device. Since these pages could have journal heads
984 * which would prevent try_to_free_buffers() from freeing them, we must use
985 * jbd2 layer's try_to_free_buffers() function to release them.
986 */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400987static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
988 gfp_t wait)
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500989{
990 journal_t *journal = EXT4_SB(sb)->s_journal;
991
992 WARN_ON(PageChecked(page));
993 if (!page_has_buffers(page))
994 return 0;
995 if (journal)
996 return jbd2_journal_try_to_free_buffers(journal, page,
997 wait & ~__GFP_WAIT);
998 return try_to_free_buffers(page);
999}
1000
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001002#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001003#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001004
Mingming Cao617ba132006-10-11 01:20:53 -07001005static int ext4_write_dquot(struct dquot *dquot);
1006static int ext4_acquire_dquot(struct dquot *dquot);
1007static int ext4_release_dquot(struct dquot *dquot);
1008static int ext4_mark_dquot_dirty(struct dquot *dquot);
1009static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -07001010static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1011 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -07001012static int ext4_quota_on_mount(struct super_block *sb, int type);
1013static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -07001015static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 const char *data, size_t len, loff_t off);
1017
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001018static const struct dquot_operations ext4_quota_operations = {
Jan Karaedf72452009-01-12 19:05:26 +01001019 .initialize = dquot_initialize,
1020 .drop = dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 .alloc_space = dquot_alloc_space,
Mingming Cao60e58e02009-01-22 18:13:05 +01001022 .reserve_space = dquot_reserve_space,
1023 .claim_space = dquot_claim_space,
1024 .release_rsv = dquot_release_reserved_space,
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001025#ifdef CONFIG_QUOTA
Mingming Cao60e58e02009-01-22 18:13:05 +01001026 .get_reserved_space = ext4_get_reserved_space,
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001027#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001028 .alloc_inode = dquot_alloc_inode,
1029 .free_space = dquot_free_space,
1030 .free_inode = dquot_free_inode,
1031 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -07001032 .write_dquot = ext4_write_dquot,
1033 .acquire_dquot = ext4_acquire_dquot,
1034 .release_dquot = ext4_release_dquot,
1035 .mark_dirty = ext4_mark_dquot_dirty,
Jan Karaa5b5ee32008-11-25 15:31:35 +01001036 .write_info = ext4_write_info,
1037 .alloc_dquot = dquot_alloc,
1038 .destroy_dquot = dquot_destroy,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039};
1040
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001041static const struct quotactl_ops ext4_qctl_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07001042 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001043 .quota_off = vfs_quota_off,
1044 .quota_sync = vfs_quota_sync,
1045 .get_info = vfs_get_dqinfo,
1046 .set_info = vfs_set_dqinfo,
1047 .get_dqblk = vfs_get_dqblk,
1048 .set_dqblk = vfs_set_dqblk
1049};
1050#endif
1051
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001052static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -07001053 .alloc_inode = ext4_alloc_inode,
1054 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001055 .write_inode = ext4_write_inode,
1056 .dirty_inode = ext4_dirty_inode,
1057 .delete_inode = ext4_delete_inode,
1058 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001059 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001060 .freeze_fs = ext4_freeze,
1061 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001062 .statfs = ext4_statfs,
1063 .remount_fs = ext4_remount,
1064 .clear_inode = ext4_clear_inode,
1065 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001067 .quota_read = ext4_quota_read,
1068 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001070 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071};
1072
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001073static const struct super_operations ext4_nojournal_sops = {
1074 .alloc_inode = ext4_alloc_inode,
1075 .destroy_inode = ext4_destroy_inode,
1076 .write_inode = ext4_write_inode,
1077 .dirty_inode = ext4_dirty_inode,
1078 .delete_inode = ext4_delete_inode,
1079 .write_super = ext4_write_super,
1080 .put_super = ext4_put_super,
1081 .statfs = ext4_statfs,
1082 .remount_fs = ext4_remount,
1083 .clear_inode = ext4_clear_inode,
1084 .show_options = ext4_show_options,
1085#ifdef CONFIG_QUOTA
1086 .quota_read = ext4_quota_read,
1087 .quota_write = ext4_quota_write,
1088#endif
1089 .bdev_try_to_free_page = bdev_try_to_free_page,
1090};
1091
Christoph Hellwig39655162007-10-21 16:42:17 -07001092static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001093 .fh_to_dentry = ext4_fh_to_dentry,
1094 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001095 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001096};
1097
1098enum {
1099 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1100 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001101 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001102 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001103 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001104 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001105 Opt_journal_update, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001106 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001107 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Theodore Ts'o296c3552009-09-30 00:32:42 -04001108 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara5a20bdf2009-11-30 23:58:32 +01001110 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1111 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
1112 Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001113 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001114 Opt_block_validity, Opt_noblock_validity,
Eric Sandeen5328e632009-11-19 14:25:42 -05001115 Opt_inode_readahead_blks, Opt_journal_ioprio,
Jiaying Zhang744692d2010-03-04 16:14:02 -05001116 Opt_dioread_nolock, Opt_dioread_lock,
Eric Sandeen5328e632009-11-19 14:25:42 -05001117 Opt_discard, Opt_nodiscard,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118};
1119
Steven Whitehousea447c092008-10-13 10:46:57 +01001120static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 {Opt_bsd_df, "bsddf"},
1122 {Opt_minix_df, "minixdf"},
1123 {Opt_grpid, "grpid"},
1124 {Opt_grpid, "bsdgroups"},
1125 {Opt_nogrpid, "nogrpid"},
1126 {Opt_nogrpid, "sysvgroups"},
1127 {Opt_resgid, "resgid=%u"},
1128 {Opt_resuid, "resuid=%u"},
1129 {Opt_sb, "sb=%u"},
1130 {Opt_err_cont, "errors=continue"},
1131 {Opt_err_panic, "errors=panic"},
1132 {Opt_err_ro, "errors=remount-ro"},
1133 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 {Opt_debug, "debug"},
1135 {Opt_oldalloc, "oldalloc"},
1136 {Opt_orlov, "orlov"},
1137 {Opt_user_xattr, "user_xattr"},
1138 {Opt_nouser_xattr, "nouser_xattr"},
1139 {Opt_acl, "acl"},
1140 {Opt_noacl, "noacl"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001141 {Opt_noload, "noload"},
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001142 {Opt_noload, "norecovery"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 {Opt_nobh, "nobh"},
1144 {Opt_bh, "bh"},
1145 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001146 {Opt_min_batch_time, "min_batch_time=%u"},
1147 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001148 {Opt_journal_update, "journal=update"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001150 {Opt_journal_checksum, "journal_checksum"},
1151 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 {Opt_abort, "abort"},
1153 {Opt_data_journal, "data=journal"},
1154 {Opt_data_ordered, "data=ordered"},
1155 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001156 {Opt_data_err_abort, "data_err=abort"},
1157 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 {Opt_offusrjquota, "usrjquota="},
1159 {Opt_usrjquota, "usrjquota=%s"},
1160 {Opt_offgrpjquota, "grpjquota="},
1161 {Opt_grpjquota, "grpjquota=%s"},
1162 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1163 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara5a20bdf2009-11-30 23:58:32 +01001164 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 {Opt_grpquota, "grpquota"},
1166 {Opt_noquota, "noquota"},
1167 {Opt_quota, "quota"},
1168 {Opt_usrquota, "usrquota"},
1169 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001170 {Opt_barrier, "barrier"},
1171 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001172 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001173 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001175 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001176 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001177 {Opt_block_validity, "block_validity"},
1178 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001179 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001180 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001181 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001182 {Opt_auto_da_alloc, "auto_da_alloc"},
1183 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001184 {Opt_dioread_nolock, "dioread_nolock"},
1185 {Opt_dioread_lock, "dioread_lock"},
Eric Sandeen5328e632009-11-19 14:25:42 -05001186 {Opt_discard, "discard"},
1187 {Opt_nodiscard, "nodiscard"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001188 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189};
1190
Mingming Cao617ba132006-10-11 01:20:53 -07001191static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001192{
Mingming Cao617ba132006-10-11 01:20:53 -07001193 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 char *options = (char *) *data;
1195
1196 if (!options || strncmp(options, "sb=", 3) != 0)
1197 return 1; /* Default location */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001198
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001199 options += 3;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001200 /* TODO: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 sb_block = simple_strtoul(options, &options, 0);
1202 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001203 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 (char *) *data);
1205 return 1;
1206 }
1207 if (*options == ',')
1208 options++;
1209 *data = (void *) options;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001210
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 return sb_block;
1212}
1213
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001214#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001215static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1216 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001217
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001218#ifdef CONFIG_QUOTA
1219static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1220{
1221 struct ext4_sb_info *sbi = EXT4_SB(sb);
1222 char *qname;
1223
1224 if (sb_any_quota_loaded(sb) &&
1225 !sbi->s_qf_names[qtype]) {
1226 ext4_msg(sb, KERN_ERR,
1227 "Cannot change journaled "
1228 "quota options when quota turned on");
1229 return 0;
1230 }
1231 qname = match_strdup(args);
1232 if (!qname) {
1233 ext4_msg(sb, KERN_ERR,
1234 "Not enough memory for storing quotafile name");
1235 return 0;
1236 }
1237 if (sbi->s_qf_names[qtype] &&
1238 strcmp(sbi->s_qf_names[qtype], qname)) {
1239 ext4_msg(sb, KERN_ERR,
1240 "%s quota file already specified", QTYPE2NAME(qtype));
1241 kfree(qname);
1242 return 0;
1243 }
1244 sbi->s_qf_names[qtype] = qname;
1245 if (strchr(sbi->s_qf_names[qtype], '/')) {
1246 ext4_msg(sb, KERN_ERR,
1247 "quotafile must be on filesystem root");
1248 kfree(sbi->s_qf_names[qtype]);
1249 sbi->s_qf_names[qtype] = NULL;
1250 return 0;
1251 }
1252 set_opt(sbi->s_mount_opt, QUOTA);
1253 return 1;
1254}
1255
1256static int clear_qf_name(struct super_block *sb, int qtype)
1257{
1258
1259 struct ext4_sb_info *sbi = EXT4_SB(sb);
1260
1261 if (sb_any_quota_loaded(sb) &&
1262 sbi->s_qf_names[qtype]) {
1263 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1264 " when quota turned on");
1265 return 0;
1266 }
1267 /*
1268 * The space will be released later when all options are confirmed
1269 * to be correct
1270 */
1271 sbi->s_qf_names[qtype] = NULL;
1272 return 1;
1273}
1274#endif
1275
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001276static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001277 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001278 unsigned int *journal_ioprio,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001279 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280{
Mingming Cao617ba132006-10-11 01:20:53 -07001281 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001282 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 substring_t args[MAX_OPT_ARGS];
1284 int data_opt = 0;
1285 int option;
1286#ifdef CONFIG_QUOTA
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001287 int qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001288#endif
1289
1290 if (!options)
1291 return 1;
1292
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001293 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 int token;
1295 if (!*p)
1296 continue;
1297
Eric Sandeen15121c12010-02-15 20:17:55 -05001298 /*
1299 * Initialize args struct so we know whether arg was
1300 * found; some options take optional arguments.
1301 */
1302 args[0].to = args[0].from = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303 token = match_token(p, tokens, args);
1304 switch (token) {
1305 case Opt_bsd_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001306 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001307 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001308 break;
1309 case Opt_minix_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001310 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001311 set_opt(sbi->s_mount_opt, MINIX_DF);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001312
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001313 break;
1314 case Opt_grpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001315 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001316 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001317
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 break;
1319 case Opt_nogrpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001320 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001321 clear_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001322
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 break;
1324 case Opt_resuid:
1325 if (match_int(&args[0], &option))
1326 return 0;
1327 sbi->s_resuid = option;
1328 break;
1329 case Opt_resgid:
1330 if (match_int(&args[0], &option))
1331 return 0;
1332 sbi->s_resgid = option;
1333 break;
1334 case Opt_sb:
1335 /* handled by get_sb_block() instead of here */
1336 /* *sb_block = match_int(&args[0]); */
1337 break;
1338 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001339 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1340 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1341 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001342 break;
1343 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001344 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1345 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1346 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001347 break;
1348 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001349 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1350 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1351 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001352 break;
1353 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001354 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001355 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001357 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001358 break;
1359 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001360 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001361 break;
1362 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001363 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001365#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001366 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001367 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 break;
1369 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001370 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371 break;
1372#else
1373 case Opt_user_xattr:
1374 case Opt_nouser_xattr:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001375 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376 break;
1377#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001378#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001379 case Opt_acl:
1380 set_opt(sbi->s_mount_opt, POSIX_ACL);
1381 break;
1382 case Opt_noacl:
1383 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1384 break;
1385#else
1386 case Opt_acl:
1387 case Opt_noacl:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001388 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389 break;
1390#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391 case Opt_journal_update:
1392 /* @@@ FIXME */
1393 /* Eventually we will want to be able to create
1394 a journal file here. For now, only allow the
1395 user to specify an existing inode to be the
1396 journal file. */
1397 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001398 ext4_msg(sb, KERN_ERR,
1399 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 return 0;
1401 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001402 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001404 case Opt_journal_dev:
1405 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001406 ext4_msg(sb, KERN_ERR,
1407 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001408 return 0;
1409 }
1410 if (match_int(&args[0], &option))
1411 return 0;
1412 *journal_devnum = option;
1413 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001414 case Opt_journal_checksum:
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001415 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1416 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001417 case Opt_journal_async_commit:
1418 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001419 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
Girish Shilamkar818d2762008-01-28 23:58:27 -05001420 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001422 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 break;
1424 case Opt_commit:
1425 if (match_int(&args[0], &option))
1426 return 0;
1427 if (option < 0)
1428 return 0;
1429 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001430 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 sbi->s_commit_interval = HZ * option;
1432 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001433 case Opt_max_batch_time:
1434 if (match_int(&args[0], &option))
1435 return 0;
1436 if (option < 0)
1437 return 0;
1438 if (option == 0)
1439 option = EXT4_DEF_MAX_BATCH_TIME;
1440 sbi->s_max_batch_time = option;
1441 break;
1442 case Opt_min_batch_time:
1443 if (match_int(&args[0], &option))
1444 return 0;
1445 if (option < 0)
1446 return 0;
1447 sbi->s_min_batch_time = option;
1448 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001449 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001450 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001451 goto datacheck;
1452 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001453 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001454 goto datacheck;
1455 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001456 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001457 datacheck:
1458 if (is_remount) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001459 if (test_opt(sb, DATA_FLAGS) != data_opt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001460 ext4_msg(sb, KERN_ERR,
1461 "Cannot change data mode on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 return 0;
1463 }
1464 } else {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001465 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 sbi->s_mount_opt |= data_opt;
1467 }
1468 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001469 case Opt_data_err_abort:
1470 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1471 break;
1472 case Opt_data_err_ignore:
1473 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1474 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001475#ifdef CONFIG_QUOTA
1476 case Opt_usrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001477 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1478 return 0;
1479 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 case Opt_grpjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001481 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001483 break;
1484 case Opt_offusrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001485 if (!clear_qf_name(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001486 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 break;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001488 case Opt_offgrpjquota:
1489 if (!clear_qf_name(sb, GRPQUOTA))
1490 return 0;
1491 break;
1492
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001494 qfmt = QFMT_VFS_OLD;
1495 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001496 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001497 qfmt = QFMT_VFS_V0;
Jan Kara5a20bdf2009-11-30 23:58:32 +01001498 goto set_qf_format;
1499 case Opt_jqfmt_vfsv1:
1500 qfmt = QFMT_VFS_V1;
Jan Karadfc5d032008-05-13 19:11:51 -04001501set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001502 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001503 sbi->s_jquota_fmt != qfmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001504 ext4_msg(sb, KERN_ERR, "Cannot change "
Jan Karadfc5d032008-05-13 19:11:51 -04001505 "journaled quota options when "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001506 "quota turned on");
Jan Karadfc5d032008-05-13 19:11:51 -04001507 return 0;
1508 }
1509 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 break;
1511 case Opt_quota:
1512 case Opt_usrquota:
1513 set_opt(sbi->s_mount_opt, QUOTA);
1514 set_opt(sbi->s_mount_opt, USRQUOTA);
1515 break;
1516 case Opt_grpquota:
1517 set_opt(sbi->s_mount_opt, QUOTA);
1518 set_opt(sbi->s_mount_opt, GRPQUOTA);
1519 break;
1520 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001521 if (sb_any_quota_loaded(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001522 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1523 "options when quota turned on");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524 return 0;
1525 }
1526 clear_opt(sbi->s_mount_opt, QUOTA);
1527 clear_opt(sbi->s_mount_opt, USRQUOTA);
1528 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1529 break;
1530#else
1531 case Opt_quota:
1532 case Opt_usrquota:
1533 case Opt_grpquota:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001534 ext4_msg(sb, KERN_ERR,
1535 "quota options not supported");
Jan Karacd59e7b2008-05-13 19:11:51 -04001536 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001537 case Opt_usrjquota:
1538 case Opt_grpjquota:
1539 case Opt_offusrjquota:
1540 case Opt_offgrpjquota:
1541 case Opt_jqfmt_vfsold:
1542 case Opt_jqfmt_vfsv0:
Jan Kara5a20bdf2009-11-30 23:58:32 +01001543 case Opt_jqfmt_vfsv1:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001544 ext4_msg(sb, KERN_ERR,
1545 "journaled quota options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 break;
1547 case Opt_noquota:
1548 break;
1549#endif
1550 case Opt_abort:
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04001551 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001553 case Opt_nobarrier:
1554 clear_opt(sbi->s_mount_opt, BARRIER);
1555 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001556 case Opt_barrier:
Eric Sandeen15121c12010-02-15 20:17:55 -05001557 if (args[0].from) {
1558 if (match_int(&args[0], &option))
1559 return 0;
1560 } else
1561 option = 1; /* No argument, default to 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 if (option)
1563 set_opt(sbi->s_mount_opt, BARRIER);
1564 else
1565 clear_opt(sbi->s_mount_opt, BARRIER);
1566 break;
1567 case Opt_ignore:
1568 break;
1569 case Opt_resize:
1570 if (!is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001571 ext4_msg(sb, KERN_ERR,
1572 "resize option only available "
1573 "for remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 return 0;
1575 }
1576 if (match_int(&args[0], &option) != 0)
1577 return 0;
1578 *n_blocks_count = option;
1579 break;
1580 case Opt_nobh:
1581 set_opt(sbi->s_mount_opt, NOBH);
1582 break;
1583 case Opt_bh:
1584 clear_opt(sbi->s_mount_opt, NOBH);
1585 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001586 case Opt_i_version:
1587 set_opt(sbi->s_mount_opt, I_VERSION);
1588 sb->s_flags |= MS_I_VERSION;
1589 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001590 case Opt_nodelalloc:
1591 clear_opt(sbi->s_mount_opt, DELALLOC);
1592 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001593 case Opt_stripe:
1594 if (match_int(&args[0], &option))
1595 return 0;
1596 if (option < 0)
1597 return 0;
1598 sbi->s_stripe = option;
1599 break;
Alex Tomas64769242008-07-11 19:27:31 -04001600 case Opt_delalloc:
1601 set_opt(sbi->s_mount_opt, DELALLOC);
1602 break;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001603 case Opt_block_validity:
1604 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1605 break;
1606 case Opt_noblock_validity:
1607 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1608 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001609 case Opt_inode_readahead_blks:
1610 if (match_int(&args[0], &option))
1611 return 0;
1612 if (option < 0 || option > (1 << 30))
1613 return 0;
Theodore Ts'of7c43952009-04-24 23:31:59 -04001614 if (!is_power_of_2(option)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001615 ext4_msg(sb, KERN_ERR,
1616 "EXT4-fs: inode_readahead_blks"
1617 " must be a power of 2");
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001618 return 0;
1619 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04001620 sbi->s_inode_readahead_blks = option;
1621 break;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001622 case Opt_journal_ioprio:
1623 if (match_int(&args[0], &option))
1624 return 0;
1625 if (option < 0 || option > 7)
1626 break;
1627 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1628 option);
1629 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001630 case Opt_noauto_da_alloc:
1631 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1632 break;
Theodore Ts'oafd46722009-03-16 23:12:23 -04001633 case Opt_auto_da_alloc:
Eric Sandeen15121c12010-02-15 20:17:55 -05001634 if (args[0].from) {
1635 if (match_int(&args[0], &option))
1636 return 0;
1637 } else
1638 option = 1; /* No argument, default to 1 */
Theodore Ts'oafd46722009-03-16 23:12:23 -04001639 if (option)
1640 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1641 else
1642 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1643 break;
Eric Sandeen5328e632009-11-19 14:25:42 -05001644 case Opt_discard:
1645 set_opt(sbi->s_mount_opt, DISCARD);
1646 break;
1647 case Opt_nodiscard:
1648 clear_opt(sbi->s_mount_opt, DISCARD);
1649 break;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001650 case Opt_dioread_nolock:
1651 set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1652 break;
1653 case Opt_dioread_lock:
1654 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1655 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001656 default:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001657 ext4_msg(sb, KERN_ERR,
1658 "Unrecognized mount option \"%s\" "
1659 "or missing value", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001660 return 0;
1661 }
1662 }
1663#ifdef CONFIG_QUOTA
1664 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001665 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001666 clear_opt(sbi->s_mount_opt, USRQUOTA);
1667
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001668 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001669 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1670
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001671 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001672 ext4_msg(sb, KERN_ERR, "old and new quota "
1673 "format mixing");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674 return 0;
1675 }
1676
1677 if (!sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001678 ext4_msg(sb, KERN_ERR, "journaled quota format "
1679 "not specified");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001680 return 0;
1681 }
1682 } else {
1683 if (sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001684 ext4_msg(sb, KERN_ERR, "journaled quota format "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001685 "specified with no journaling "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001686 "enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 return 0;
1688 }
1689 }
1690#endif
1691 return 1;
1692}
1693
Mingming Cao617ba132006-10-11 01:20:53 -07001694static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695 int read_only)
1696{
Mingming Cao617ba132006-10-11 01:20:53 -07001697 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001698 int res = 0;
1699
Mingming Cao617ba132006-10-11 01:20:53 -07001700 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001701 ext4_msg(sb, KERN_ERR, "revision level too high, "
1702 "forcing read-only mode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001703 res = MS_RDONLY;
1704 }
1705 if (read_only)
1706 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001707 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001708 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1709 "running e2fsck is recommended");
Mingming Cao617ba132006-10-11 01:20:53 -07001710 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001711 ext4_msg(sb, KERN_WARNING,
1712 "warning: mounting fs with errors, "
1713 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1715 le16_to_cpu(es->s_mnt_count) >=
1716 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001717 ext4_msg(sb, KERN_WARNING,
1718 "warning: maximal mount count reached, "
1719 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720 else if (le32_to_cpu(es->s_checkinterval) &&
1721 (le32_to_cpu(es->s_lastcheck) +
1722 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001723 ext4_msg(sb, KERN_WARNING,
1724 "warning: checktime reached, "
1725 "running e2fsck is recommended");
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001726 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05001727 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001728 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001729 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001730 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001731 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001732 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001733 if (sbi->s_journal)
1734 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001735
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001736 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001738 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -04001739 "bpg=%lu, ipg=%lu, mo=%04x]\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001740 sb->s_blocksize,
1741 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001742 EXT4_BLOCKS_PER_GROUP(sb),
1743 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744 sbi->s_mount_opt);
1745
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746 return res;
1747}
1748
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001749static int ext4_fill_flex_info(struct super_block *sb)
1750{
1751 struct ext4_sb_info *sbi = EXT4_SB(sb);
1752 struct ext4_group_desc *gdp = NULL;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001753 ext4_group_t flex_group_count;
1754 ext4_group_t flex_group;
1755 int groups_per_flex = 0;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001756 size_t size;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001757 int i;
1758
Theodore Ts'o503358a2009-11-23 07:24:46 -05001759 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1760 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1761
1762 if (groups_per_flex < 2) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001763 sbi->s_log_groups_per_flex = 0;
1764 return 1;
1765 }
1766
Frederic Bohec62a11f2008-09-08 10:20:24 -04001767 /* We allocate both existing and potentially added groups */
1768 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001769 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1770 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001771 size = flex_group_count * sizeof(struct flex_groups);
1772 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1773 if (sbi->s_flex_groups == NULL) {
1774 sbi->s_flex_groups = vmalloc(size);
1775 if (sbi->s_flex_groups)
1776 memset(sbi->s_flex_groups, 0, size);
1777 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001778 if (sbi->s_flex_groups == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001779 ext4_msg(sb, KERN_ERR, "not enough memory for "
1780 "%u flex groups", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001781 goto failed;
1782 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001783
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001784 for (i = 0; i < sbi->s_groups_count; i++) {
Theodore Ts'o88b6edd2009-05-25 11:50:39 -04001785 gdp = ext4_get_group_desc(sb, i, NULL);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001786
1787 flex_group = ext4_flex_group(sbi, i);
Theodore Ts'o7ad9bb62009-09-11 16:51:28 -04001788 atomic_add(ext4_free_inodes_count(sb, gdp),
1789 &sbi->s_flex_groups[flex_group].free_inodes);
1790 atomic_add(ext4_free_blks_count(sb, gdp),
1791 &sbi->s_flex_groups[flex_group].free_blocks);
1792 atomic_add(ext4_used_dirs_count(sb, gdp),
1793 &sbi->s_flex_groups[flex_group].used_dirs);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001794 }
1795
1796 return 1;
1797failed:
1798 return 0;
1799}
1800
Andreas Dilger717d50e2007-10-16 18:38:25 -04001801__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1802 struct ext4_group_desc *gdp)
1803{
1804 __u16 crc = 0;
1805
1806 if (sbi->s_es->s_feature_ro_compat &
1807 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1808 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1809 __le32 le_group = cpu_to_le32(block_group);
1810
1811 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1812 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1813 crc = crc16(crc, (__u8 *)gdp, offset);
1814 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1815 /* for checksum of struct ext4_group_desc do the rest...*/
1816 if ((sbi->s_es->s_feature_incompat &
1817 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1818 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1819 crc = crc16(crc, (__u8 *)gdp + offset,
1820 le16_to_cpu(sbi->s_es->s_desc_size) -
1821 offset);
1822 }
1823
1824 return cpu_to_le16(crc);
1825}
1826
1827int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1828 struct ext4_group_desc *gdp)
1829{
1830 if ((sbi->s_es->s_feature_ro_compat &
1831 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1832 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1833 return 0;
1834
1835 return 1;
1836}
1837
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001839static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840{
Mingming Cao617ba132006-10-11 01:20:53 -07001841 struct ext4_sb_info *sbi = EXT4_SB(sb);
1842 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1843 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001844 ext4_fsblk_t block_bitmap;
1845 ext4_fsblk_t inode_bitmap;
1846 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001847 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001848 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849
Jose R. Santosce421582007-10-16 18:38:25 -04001850 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1851 flexbg_flag = 1;
1852
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001853 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001854
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001855 for (i = 0; i < sbi->s_groups_count; i++) {
1856 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1857
Jose R. Santosce421582007-10-16 18:38:25 -04001858 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001859 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860 else
1861 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001862 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001864 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001865 if (block_bitmap < first_block || block_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001866 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001867 "Block bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001868 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 return 0;
1870 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001871 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001872 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001873 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001874 "Inode bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001875 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 return 0;
1877 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001878 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001879 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001880 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001881 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001882 "Inode table for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001883 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001884 return 0;
1885 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001886 ext4_lock_group(sb, i);
Andreas Dilger717d50e2007-10-16 18:38:25 -04001887 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001888 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1889 "Checksum for group %u failed (%u!=%u)",
1890 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1891 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001892 if (!(sb->s_flags & MS_RDONLY)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001893 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04001894 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001895 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001896 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001897 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04001898 if (!flexbg_flag)
1899 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 }
1901
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001902 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001903 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001904 return 1;
1905}
1906
Mingming Cao617ba132006-10-11 01:20:53 -07001907/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001908 * the superblock) which were deleted from all directories, but held open by
1909 * a process at the time of a crash. We walk the list and try to delete these
1910 * inodes at recovery time (only with a read-write filesystem).
1911 *
1912 * In order to keep the orphan inode chain consistent during traversal (in
1913 * case of crash during recovery), we link each inode into the superblock
1914 * orphan list_head and handle it the same way as an inode deletion during
1915 * normal operation (which journals the operations for us).
1916 *
1917 * We only do an iget() and an iput() on each inode, which is very safe if we
1918 * accidentally point at an in-use or already deleted inode. The worst that
1919 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001920 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001921 * e2fsck was run on this filesystem, and it must have already done the orphan
1922 * inode cleanup for us, so we can safely abort without any further action.
1923 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001924static void ext4_orphan_cleanup(struct super_block *sb,
1925 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926{
1927 unsigned int s_flags = sb->s_flags;
1928 int nr_orphans = 0, nr_truncates = 0;
1929#ifdef CONFIG_QUOTA
1930 int i;
1931#endif
1932 if (!es->s_last_orphan) {
1933 jbd_debug(4, "no orphan inodes to clean up\n");
1934 return;
1935 }
1936
Eric Sandeena8f48a92006-12-06 20:40:13 -08001937 if (bdev_read_only(sb->s_bdev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001938 ext4_msg(sb, KERN_ERR, "write access "
1939 "unavailable, skipping orphan cleanup");
Eric Sandeena8f48a92006-12-06 20:40:13 -08001940 return;
1941 }
1942
Mingming Cao617ba132006-10-11 01:20:53 -07001943 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 if (es->s_last_orphan)
1945 jbd_debug(1, "Errors on filesystem, "
1946 "clearing orphan list.\n");
1947 es->s_last_orphan = 0;
1948 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1949 return;
1950 }
1951
1952 if (s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001953 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001954 sb->s_flags &= ~MS_RDONLY;
1955 }
1956#ifdef CONFIG_QUOTA
1957 /* Needed for iput() to work correctly and not trash data */
1958 sb->s_flags |= MS_ACTIVE;
1959 /* Turn on quotas so that they are updated correctly */
1960 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001961 if (EXT4_SB(sb)->s_qf_names[i]) {
1962 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963 if (ret < 0)
Eric Sandeenb31e1552009-06-04 17:36:36 -04001964 ext4_msg(sb, KERN_ERR,
1965 "Cannot turn on journaled "
1966 "quota: error %d", ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001967 }
1968 }
1969#endif
1970
1971 while (es->s_last_orphan) {
1972 struct inode *inode;
1973
Josef Bacik97bd42b2008-04-29 22:04:56 -04001974 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1975 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976 es->s_last_orphan = 0;
1977 break;
1978 }
1979
Mingming Cao617ba132006-10-11 01:20:53 -07001980 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Jan Karaa269eb12009-01-26 17:04:39 +01001981 vfs_dq_init(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001982 if (inode->i_nlink) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001983 ext4_msg(sb, KERN_DEBUG,
1984 "%s: truncating inode %lu to %lld bytes",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001985 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001986 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001988 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 nr_truncates++;
1990 } else {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001991 ext4_msg(sb, KERN_DEBUG,
1992 "%s: deleting unreferenced inode %lu",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001993 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 jbd_debug(2, "deleting unreferenced inode %lu\n",
1995 inode->i_ino);
1996 nr_orphans++;
1997 }
1998 iput(inode); /* The delete magic happens here! */
1999 }
2000
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002001#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002002
2003 if (nr_orphans)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002004 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2005 PLURAL(nr_orphans));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006 if (nr_truncates)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002007 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2008 PLURAL(nr_truncates));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002009#ifdef CONFIG_QUOTA
2010 /* Turn quotas off */
2011 for (i = 0; i < MAXQUOTAS; i++) {
2012 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07002013 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002014 }
2015#endif
2016 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2017}
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002018
Eric Sandeencd2291a2008-01-28 23:58:27 -05002019/*
2020 * Maximal extent format file size.
2021 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2022 * extent format containers, within a sector_t, and within i_blocks
2023 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2024 * so that won't be a limiting factor.
2025 *
2026 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2027 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002028static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05002029{
2030 loff_t res;
2031 loff_t upper_limit = MAX_LFS_FILESIZE;
2032
2033 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002034 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05002035 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002036 * CONFIG_LBDAF is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05002037 * i_block represent total blocks in 512 bytes
2038 * 32 == size of vfs inode i_blocks * 8
2039 */
2040 upper_limit = (1LL << 32) - 1;
2041
2042 /* total blocks in file system block size */
2043 upper_limit >>= (blkbits - 9);
2044 upper_limit <<= blkbits;
2045 }
2046
2047 /* 32-bit extent-start container, ee_block */
2048 res = 1LL << 32;
2049 res <<= blkbits;
2050 res -= 1;
2051
2052 /* Sanity check against vm- & vfs- imposed limits */
2053 if (res > upper_limit)
2054 res = upper_limit;
2055
2056 return res;
2057}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002058
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002059/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05002060 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002061 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2062 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002063 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002064static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002065{
Mingming Cao617ba132006-10-11 01:20:53 -07002066 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002067 int meta_blocks;
2068 loff_t upper_limit;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002069 /* This is calculated to be the largest file size for a dense, block
2070 * mapped file such that the file's total number of 512-byte sectors,
2071 * including data and all indirect blocks, does not exceed (2^48 - 1).
2072 *
2073 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2074 * number of 512-byte sectors of the file.
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002075 */
2076
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002077 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002078 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002079 * !has_huge_files or CONFIG_LBDAF not enabled implies that
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002080 * the inode i_block field represents total file blocks in
2081 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002082 */
2083 upper_limit = (1LL << 32) - 1;
2084
2085 /* total blocks in file system block size */
2086 upper_limit >>= (bits - 9);
2087
2088 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05002089 /*
2090 * We use 48 bit ext4_inode i_blocks
2091 * With EXT4_HUGE_FILE_FL set the i_blocks
2092 * represent total number of blocks in
2093 * file system block size
2094 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002095 upper_limit = (1LL << 48) - 1;
2096
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002097 }
2098
2099 /* indirect blocks */
2100 meta_blocks = 1;
2101 /* double indirect blocks */
2102 meta_blocks += 1 + (1LL << (bits-2));
2103 /* tripple indirect blocks */
2104 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2105
2106 upper_limit -= meta_blocks;
2107 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108
2109 res += 1LL << (bits-2);
2110 res += 1LL << (2*(bits-2));
2111 res += 1LL << (3*(bits-2));
2112 res <<= bits;
2113 if (res > upper_limit)
2114 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002115
2116 if (res > MAX_LFS_FILESIZE)
2117 res = MAX_LFS_FILESIZE;
2118
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002119 return res;
2120}
2121
Mingming Cao617ba132006-10-11 01:20:53 -07002122static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002123 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002124{
Mingming Cao617ba132006-10-11 01:20:53 -07002125 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002126 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 int has_super = 0;
2128
2129 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2130
Mingming Cao617ba132006-10-11 01:20:53 -07002131 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002132 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002133 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002134 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07002135 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002136 has_super = 1;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002137
Mingming Cao617ba132006-10-11 01:20:53 -07002138 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139}
2140
Alex Tomasc9de5602008-01-29 00:19:52 -05002141/**
2142 * ext4_get_stripe_size: Get the stripe size.
2143 * @sbi: In memory super block info
2144 *
2145 * If we have specified it via mount option, then
2146 * use the mount option value. If the value specified at mount time is
2147 * greater than the blocks per group use the super block value.
2148 * If the super block value is greater than blocks per group return 0.
2149 * Allocator needs it be less than blocks per group.
2150 *
2151 */
2152static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2153{
2154 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2155 unsigned long stripe_width =
2156 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2157
2158 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2159 return sbi->s_stripe;
2160
2161 if (stripe_width <= sbi->s_blocks_per_group)
2162 return stripe_width;
2163
2164 if (stride <= sbi->s_blocks_per_group)
2165 return stride;
2166
2167 return 0;
2168}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002170/* sysfs supprt */
2171
2172struct ext4_attr {
2173 struct attribute attr;
2174 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2175 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2176 const char *, size_t);
2177 int offset;
2178};
2179
2180static int parse_strtoul(const char *buf,
2181 unsigned long max, unsigned long *value)
2182{
2183 char *endp;
2184
André Goddard Rosae7d28602009-12-14 18:01:06 -08002185 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
2186 endp = skip_spaces(endp);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002187 if (*endp || *value > max)
2188 return -EINVAL;
2189
2190 return 0;
2191}
2192
2193static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2194 struct ext4_sb_info *sbi,
2195 char *buf)
2196{
2197 return snprintf(buf, PAGE_SIZE, "%llu\n",
2198 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2199}
2200
2201static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2202 struct ext4_sb_info *sbi, char *buf)
2203{
2204 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2205
2206 return snprintf(buf, PAGE_SIZE, "%lu\n",
2207 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2208 sbi->s_sectors_written_start) >> 1);
2209}
2210
2211static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2212 struct ext4_sb_info *sbi, char *buf)
2213{
2214 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2215
2216 return snprintf(buf, PAGE_SIZE, "%llu\n",
Andrew Mortona6b43e32009-12-23 07:48:08 -05002217 (unsigned long long)(sbi->s_kbytes_written +
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002218 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
Andrew Mortona6b43e32009-12-23 07:48:08 -05002219 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002220}
2221
2222static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2223 struct ext4_sb_info *sbi,
2224 const char *buf, size_t count)
2225{
2226 unsigned long t;
2227
2228 if (parse_strtoul(buf, 0x40000000, &t))
2229 return -EINVAL;
2230
Theodore Ts'of7c43952009-04-24 23:31:59 -04002231 if (!is_power_of_2(t))
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002232 return -EINVAL;
2233
2234 sbi->s_inode_readahead_blks = t;
2235 return count;
2236}
2237
2238static ssize_t sbi_ui_show(struct ext4_attr *a,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002239 struct ext4_sb_info *sbi, char *buf)
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002240{
2241 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2242
2243 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2244}
2245
2246static ssize_t sbi_ui_store(struct ext4_attr *a,
2247 struct ext4_sb_info *sbi,
2248 const char *buf, size_t count)
2249{
2250 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2251 unsigned long t;
2252
2253 if (parse_strtoul(buf, 0xffffffff, &t))
2254 return -EINVAL;
2255 *ui = t;
2256 return count;
2257}
2258
2259#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2260static struct ext4_attr ext4_attr_##_name = { \
2261 .attr = {.name = __stringify(_name), .mode = _mode }, \
2262 .show = _show, \
2263 .store = _store, \
2264 .offset = offsetof(struct ext4_sb_info, _elname), \
2265}
2266#define EXT4_ATTR(name, mode, show, store) \
2267static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2268
2269#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2270#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2271#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2272 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2273#define ATTR_LIST(name) &ext4_attr_##name.attr
2274
2275EXT4_RO_ATTR(delayed_allocation_blocks);
2276EXT4_RO_ATTR(session_write_kbytes);
2277EXT4_RO_ATTR(lifetime_write_kbytes);
2278EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2279 inode_readahead_blks_store, s_inode_readahead_blks);
Andreas Dilger11013912009-06-13 11:45:35 -04002280EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002281EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2282EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2283EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2284EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2285EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2286EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002287EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002288
2289static struct attribute *ext4_attrs[] = {
2290 ATTR_LIST(delayed_allocation_blocks),
2291 ATTR_LIST(session_write_kbytes),
2292 ATTR_LIST(lifetime_write_kbytes),
2293 ATTR_LIST(inode_readahead_blks),
Andreas Dilger11013912009-06-13 11:45:35 -04002294 ATTR_LIST(inode_goal),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002295 ATTR_LIST(mb_stats),
2296 ATTR_LIST(mb_max_to_scan),
2297 ATTR_LIST(mb_min_to_scan),
2298 ATTR_LIST(mb_order2_req),
2299 ATTR_LIST(mb_stream_req),
2300 ATTR_LIST(mb_group_prealloc),
Theodore Ts'o55138e02009-09-29 13:31:31 -04002301 ATTR_LIST(max_writeback_mb_bump),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002302 NULL,
2303};
2304
2305static ssize_t ext4_attr_show(struct kobject *kobj,
2306 struct attribute *attr, char *buf)
2307{
2308 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2309 s_kobj);
2310 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2311
2312 return a->show ? a->show(a, sbi, buf) : 0;
2313}
2314
2315static ssize_t ext4_attr_store(struct kobject *kobj,
2316 struct attribute *attr,
2317 const char *buf, size_t len)
2318{
2319 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2320 s_kobj);
2321 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2322
2323 return a->store ? a->store(a, sbi, buf, len) : 0;
2324}
2325
2326static void ext4_sb_release(struct kobject *kobj)
2327{
2328 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2329 s_kobj);
2330 complete(&sbi->s_kobj_unregister);
2331}
2332
2333
2334static struct sysfs_ops ext4_attr_ops = {
2335 .show = ext4_attr_show,
2336 .store = ext4_attr_store,
2337};
2338
2339static struct kobj_type ext4_ktype = {
2340 .default_attrs = ext4_attrs,
2341 .sysfs_ops = &ext4_attr_ops,
2342 .release = ext4_sb_release,
2343};
2344
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002345/*
2346 * Check whether this filesystem can be mounted based on
2347 * the features present and the RDONLY/RDWR mount requested.
2348 * Returns 1 if this filesystem can be mounted as requested,
2349 * 0 if it cannot be.
2350 */
2351static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2352{
2353 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2354 ext4_msg(sb, KERN_ERR,
2355 "Couldn't mount because of "
2356 "unsupported optional features (%x)",
2357 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2358 ~EXT4_FEATURE_INCOMPAT_SUPP));
2359 return 0;
2360 }
2361
2362 if (readonly)
2363 return 1;
2364
2365 /* Check that feature set is OK for a read-write mount */
2366 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2367 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2368 "unsupported optional features (%x)",
2369 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2370 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2371 return 0;
2372 }
2373 /*
2374 * Large file size enabled file system can only be mounted
2375 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2376 */
2377 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2378 if (sizeof(blkcnt_t) < sizeof(u64)) {
2379 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2380 "cannot be mounted RDWR without "
2381 "CONFIG_LBDAF");
2382 return 0;
2383 }
2384 }
2385 return 1;
2386}
2387
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002388static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002389 __releases(kernel_lock)
2390 __acquires(kernel_lock)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002392 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002393 struct ext4_super_block *es = NULL;
2394 struct ext4_sb_info *sbi;
2395 ext4_fsblk_t block;
2396 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002397 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002398 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002399 unsigned long journal_devnum = 0;
2400 unsigned long def_mount_opts;
2401 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002402 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002403 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002404 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002405 int blocksize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002406 unsigned int db_count;
2407 unsigned int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002408 int needs_recovery, has_huge_files;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002409 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002410 int err;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002411 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002412
2413 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2414 if (!sbi)
2415 return -ENOMEM;
Pekka Enberg705895b2009-02-15 18:07:52 -05002416
2417 sbi->s_blockgroup_lock =
2418 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2419 if (!sbi->s_blockgroup_lock) {
2420 kfree(sbi);
2421 return -ENOMEM;
2422 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423 sb->s_fs_info = sbi;
2424 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002425 sbi->s_resuid = EXT4_DEF_RESUID;
2426 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002427 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002428 sbi->s_sb_block = sb_block;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002429 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2430 sectors[1]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002431
2432 unlock_kernel();
2433
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002434 /* Cleanup superblock name */
2435 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2436 *cp = '!';
2437
Mingming Cao617ba132006-10-11 01:20:53 -07002438 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002439 if (!blocksize) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002440 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002441 goto out_fail;
2442 }
2443
2444 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002445 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002446 * block sizes. We need to calculate the offset from buffer start.
2447 */
Mingming Cao617ba132006-10-11 01:20:53 -07002448 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002449 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2450 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002452 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453 }
2454
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002455 if (!(bh = sb_bread(sb, logical_sb_block))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002456 ext4_msg(sb, KERN_ERR, "unable to read superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457 goto out_fail;
2458 }
2459 /*
2460 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002461 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 */
Mingming Cao617ba132006-10-11 01:20:53 -07002463 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002464 sbi->s_es = es;
2465 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002466 if (sb->s_magic != EXT4_SUPER_MAGIC)
2467 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002468 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002469
2470 /* Set defaults before we parse the mount options */
2471 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002472 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473 set_opt(sbi->s_mount_opt, DEBUG);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002474 if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
2475 ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
2476 "2.6.38");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002478 }
Mingming Cao617ba132006-10-11 01:20:53 -07002479 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002480 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002481#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002482 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002483 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002484#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002485#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002486 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002487 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002488#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002489 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002490 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002491 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002492 set_opt(sbi->s_mount_opt, ORDERED_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002493 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002494 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002495
Mingming Cao617ba132006-10-11 01:20:53 -07002496 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002497 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002498 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002499 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002500 else
2501 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002502
2503 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2504 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002505 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2506 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2507 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002508
Eric Sandeen571640c2008-05-26 12:29:46 -04002509 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002510
Mingming Cao1e2462f2007-07-18 09:00:55 -04002511 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002512 * enable delayed allocation by default
2513 * Use -o nodelalloc to turn it off
2514 */
2515 set_opt(sbi->s_mount_opt, DELALLOC);
2516
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002517 if (!parse_options((char *) data, sb, &journal_devnum,
2518 &journal_ioprio, NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002519 goto failed_mount;
2520
2521 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002522 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002523
Mingming Cao617ba132006-10-11 01:20:53 -07002524 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2525 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2526 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2527 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002528 ext4_msg(sb, KERN_WARNING,
2529 "feature flags set on rev 0 fs, "
2530 "running e2fsck is recommended");
Theodore Tso469108f2008-02-10 01:11:44 -05002531
2532 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002533 * Check feature flags regardless of the revision level, since we
2534 * previously didn't change the revision level when setting the flags,
2535 * so there is a chance incompat flags are set on a rev 0 filesystem.
2536 */
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002537 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002538 goto failed_mount;
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002539
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002540 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2541
Mingming Cao617ba132006-10-11 01:20:53 -07002542 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2543 blocksize > EXT4_MAX_BLOCK_SIZE) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002544 ext4_msg(sb, KERN_ERR,
2545 "Unsupported filesystem blocksize %d", blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 goto failed_mount;
2547 }
2548
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002550 /* Validate the filesystem blocksize */
2551 if (!sb_set_blocksize(sb, blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002552 ext4_msg(sb, KERN_ERR, "bad block size %d",
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002553 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002554 goto failed_mount;
2555 }
2556
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002557 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002558 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2559 offset = do_div(logical_sb_block, blocksize);
2560 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002561 if (!bh) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002562 ext4_msg(sb, KERN_ERR,
2563 "Can't read superblock on 2nd try");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002564 goto failed_mount;
2565 }
Mingming Cao617ba132006-10-11 01:20:53 -07002566 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002568 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002569 ext4_msg(sb, KERN_ERR,
2570 "Magic mismatch, very weird!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002571 goto failed_mount;
2572 }
2573 }
2574
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002575 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2576 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002577 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2578 has_huge_files);
2579 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002580
Mingming Cao617ba132006-10-11 01:20:53 -07002581 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2582 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2583 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002584 } else {
2585 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2586 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002587 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002588 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002589 (sbi->s_inode_size > blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002590 ext4_msg(sb, KERN_ERR,
2591 "unsupported inode size: %d",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002592 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002593 goto failed_mount;
2594 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002595 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2596 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597 }
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002598
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002599 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2600 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002601 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002602 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002603 !is_power_of_2(sbi->s_desc_size)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002604 ext4_msg(sb, KERN_ERR,
2605 "unsupported descriptor size %lu",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002606 sbi->s_desc_size);
2607 goto failed_mount;
2608 }
2609 } else
2610 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002611
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002612 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002613 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002614 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002615 goto cantfind_ext4;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002616
Mingming Cao617ba132006-10-11 01:20:53 -07002617 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002618 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002619 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002620 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2621 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002622 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002623 sbi->s_sbh = bh;
2624 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002625 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2626 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002627
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002628 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002629 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2630 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002631 i = le32_to_cpu(es->s_flags);
2632 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2633 sbi->s_hash_unsigned = 3;
2634 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2635#ifdef __CHAR_UNSIGNED__
2636 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2637 sbi->s_hash_unsigned = 3;
2638#else
2639 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2640#endif
2641 sb->s_dirt = 1;
2642 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002643
2644 if (sbi->s_blocks_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002645 ext4_msg(sb, KERN_ERR,
2646 "#blocks per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002647 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002648 goto failed_mount;
2649 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002650 if (sbi->s_inodes_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002651 ext4_msg(sb, KERN_ERR,
2652 "#inodes per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002653 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654 goto failed_mount;
2655 }
2656
Eric Sandeenbf43d842009-08-17 23:48:51 -04002657 /*
2658 * Test whether we have more sectors than will fit in sector_t,
2659 * and whether the max offset is addressable by the page cache.
2660 */
2661 if ((ext4_blocks_count(es) >
2662 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2663 (ext4_blocks_count(es) >
2664 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002665 ext4_msg(sb, KERN_ERR, "filesystem"
Eric Sandeenbf43d842009-08-17 23:48:51 -04002666 " too large to mount safely on this system");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667 if (sizeof(sector_t) < 8)
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002668 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
Eric Sandeenbf43d842009-08-17 23:48:51 -04002669 ret = -EFBIG;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002670 goto failed_mount;
2671 }
2672
Mingming Cao617ba132006-10-11 01:20:53 -07002673 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2674 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002675
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002676 /* check blocks count against device size */
2677 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2678 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002679 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2680 "exceeds size of device (%llu blocks)",
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002681 ext4_blocks_count(es), blocks_count);
2682 goto failed_mount;
2683 }
2684
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002685 /*
2686 * It makes no sense for the first data block to be beyond the end
2687 * of the filesystem.
2688 */
2689 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002690 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2691 "block %u is beyond end of filesystem (%llu)",
2692 le32_to_cpu(es->s_first_data_block),
2693 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05002694 goto failed_mount;
2695 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002696 blocks_count = (ext4_blocks_count(es) -
2697 le32_to_cpu(es->s_first_data_block) +
2698 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2699 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002700 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002701 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002702 "(block count %llu, first data block %u, "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002703 "blocks per group %lu)", sbi->s_groups_count,
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002704 ext4_blocks_count(es),
2705 le32_to_cpu(es->s_first_data_block),
2706 EXT4_BLOCKS_PER_GROUP(sb));
2707 goto failed_mount;
2708 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002709 sbi->s_groups_count = blocks_count;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002710 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2711 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Mingming Cao617ba132006-10-11 01:20:53 -07002712 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2713 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002714 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002715 GFP_KERNEL);
2716 if (sbi->s_group_desc == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002717 ext4_msg(sb, KERN_ERR, "not enough memory");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 goto failed_mount;
2719 }
2720
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002721#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002722 if (ext4_proc_root)
2723 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002724#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002725
Pekka Enberg705895b2009-02-15 18:07:52 -05002726 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002727
2728 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002729 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002730 sbi->s_group_desc[i] = sb_bread(sb, block);
2731 if (!sbi->s_group_desc[i]) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002732 ext4_msg(sb, KERN_ERR,
2733 "can't read group descriptor %d", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734 db_count = i;
2735 goto failed_mount2;
2736 }
2737 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002738 if (!ext4_check_descriptors(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002739 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002740 goto failed_mount2;
2741 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002742 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2743 if (!ext4_fill_flex_info(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002744 ext4_msg(sb, KERN_ERR,
2745 "unable to initialize "
2746 "flex_bg meta info!");
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002747 goto failed_mount2;
2748 }
2749
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750 sbi->s_gdb_count = db_count;
2751 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2752 spin_lock_init(&sbi->s_next_gen_lock);
2753
Peter Zijlstra833f4072007-10-16 23:25:45 -07002754 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2755 ext4_count_free_blocks(sb));
2756 if (!err) {
2757 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2758 ext4_count_free_inodes(sb));
2759 }
2760 if (!err) {
2761 err = percpu_counter_init(&sbi->s_dirs_counter,
2762 ext4_count_dirs(sb));
2763 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002764 if (!err) {
2765 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2766 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002767 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002768 ext4_msg(sb, KERN_ERR, "insufficient memory");
Peter Zijlstra833f4072007-10-16 23:25:45 -07002769 goto failed_mount3;
2770 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002771
Alex Tomasc9de5602008-01-29 00:19:52 -05002772 sbi->s_stripe = ext4_get_stripe_size(sbi);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002773 sbi->s_max_writeback_mb_bump = 128;
Alex Tomasc9de5602008-01-29 00:19:52 -05002774
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775 /*
2776 * set up enough so that it can read an inode
2777 */
Theodore Ts'o9ca92382009-05-01 12:52:25 -04002778 if (!test_opt(sb, NOLOAD) &&
2779 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2780 sb->s_op = &ext4_sops;
2781 else
2782 sb->s_op = &ext4_nojournal_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07002783 sb->s_export_op = &ext4_export_ops;
2784 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002785#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002786 sb->s_qcop = &ext4_qctl_operations;
2787 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788#endif
2789 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002790 mutex_init(&sbi->s_orphan_lock);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04002791 mutex_init(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002792
2793 sb->s_root = NULL;
2794
2795 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002796 EXT4_HAS_INCOMPAT_FEATURE(sb,
2797 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002798
2799 /*
2800 * The first inode we look at is the journal inode. Don't try
2801 * root first: it may be modified in the journal!
2802 */
2803 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002804 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2805 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002806 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002807 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2808 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002809 ext4_msg(sb, KERN_ERR, "required journal recovery "
2810 "suppressed and not mounted read-only");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002811 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002813 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2814 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2815 sbi->s_journal = NULL;
2816 needs_recovery = 0;
2817 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002818 }
2819
Jose R. Santoseb40a092007-07-18 08:37:25 -04002820 if (ext4_blocks_count(es) > 0xffffffffULL &&
2821 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2822 JBD2_FEATURE_INCOMPAT_64BIT)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002823 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002824 goto failed_mount_wq;
Jose R. Santoseb40a092007-07-18 08:37:25 -04002825 }
2826
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002827 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2828 jbd2_journal_set_features(sbi->s_journal,
2829 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
Girish Shilamkar818d2762008-01-28 23:58:27 -05002830 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002831 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2832 jbd2_journal_set_features(sbi->s_journal,
2833 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
Girish Shilamkar818d2762008-01-28 23:58:27 -05002834 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2835 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002836 } else {
2837 jbd2_journal_clear_features(sbi->s_journal,
2838 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2839 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2840 }
Girish Shilamkar818d2762008-01-28 23:58:27 -05002841
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002842 /* We have now updated the journal if required, so we can
2843 * validate the data journaling mode. */
2844 switch (test_opt(sb, DATA_FLAGS)) {
2845 case 0:
2846 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002847 * capabilities: ORDERED_DATA if the journal can
2848 * cope, else JOURNAL_DATA
2849 */
Mingming Caodab291a2006-10-11 01:21:01 -07002850 if (jbd2_journal_check_available_features
2851 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002852 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2853 else
2854 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2855 break;
2856
Mingming Cao617ba132006-10-11 01:20:53 -07002857 case EXT4_MOUNT_ORDERED_DATA:
2858 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002859 if (!jbd2_journal_check_available_features
2860 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002861 ext4_msg(sb, KERN_ERR, "Journal does not support "
2862 "requested data journaling mode");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002863 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864 }
2865 default:
2866 break;
2867 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002868 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002869
Frank Mayhar03901312009-01-07 00:06:22 -05002870no_journal:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002871 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002872 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002873 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2874 "its supported only with writeback mode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002875 clear_opt(sbi->s_mount_opt, NOBH);
2876 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05002877 if (test_opt(sb, DIOREAD_NOLOCK)) {
2878 ext4_msg(sb, KERN_WARNING, "dioread_nolock option is "
2879 "not supported with nobh mode");
2880 goto failed_mount_wq;
2881 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002882 }
Mingming Cao4c0425f2009-09-28 15:48:41 -04002883 EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
2884 if (!EXT4_SB(sb)->dio_unwritten_wq) {
2885 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
2886 goto failed_mount_wq;
2887 }
2888
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002889 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002890 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 * so we can safely mount the rest of the filesystem now.
2892 */
2893
David Howells1d1fe1e2008-02-07 00:15:37 -08002894 root = ext4_iget(sb, EXT4_ROOT_INO);
2895 if (IS_ERR(root)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002896 ext4_msg(sb, KERN_ERR, "get root inode failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08002897 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002898 goto failed_mount4;
2899 }
2900 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002901 iput(root);
Eric Sandeenb31e1552009-06-04 17:36:36 -04002902 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002903 goto failed_mount4;
2904 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002905 sb->s_root = d_alloc_root(root);
2906 if (!sb->s_root) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002907 ext4_msg(sb, KERN_ERR, "get root dentry failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08002908 iput(root);
2909 ret = -ENOMEM;
2910 goto failed_mount4;
2911 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002913 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002914
2915 /* determine the minimum size of new large inodes, if present */
2916 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2917 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2918 EXT4_GOOD_OLD_INODE_SIZE;
2919 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2920 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2921 if (sbi->s_want_extra_isize <
2922 le16_to_cpu(es->s_want_extra_isize))
2923 sbi->s_want_extra_isize =
2924 le16_to_cpu(es->s_want_extra_isize);
2925 if (sbi->s_want_extra_isize <
2926 le16_to_cpu(es->s_min_extra_isize))
2927 sbi->s_want_extra_isize =
2928 le16_to_cpu(es->s_min_extra_isize);
2929 }
2930 }
2931 /* Check if enough inode space is available */
2932 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2933 sbi->s_inode_size) {
2934 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2935 EXT4_GOOD_OLD_INODE_SIZE;
Eric Sandeenb31e1552009-06-04 17:36:36 -04002936 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2937 "available");
Kalpak Shahef7f3832007-07-18 09:15:20 -04002938 }
2939
Theodore Ts'o90576c02009-09-29 15:51:30 -04002940 if (test_opt(sb, DELALLOC) &&
2941 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002942 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2943 "requested data journaling mode");
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002944 clear_opt(sbi->s_mount_opt, DELALLOC);
Theodore Ts'o90576c02009-09-29 15:51:30 -04002945 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05002946 if (test_opt(sb, DIOREAD_NOLOCK)) {
2947 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2948 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2949 "option - requested data journaling mode");
2950 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2951 }
2952 if (sb->s_blocksize < PAGE_SIZE) {
2953 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2954 "option - block size is too small");
2955 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2956 }
2957 }
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002958
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002959 err = ext4_setup_system_zone(sb);
2960 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002961 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2962 "zone (%d)\n", err);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04002963 goto failed_mount4;
2964 }
2965
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002966 ext4_ext_init(sb);
2967 err = ext4_mb_init(sb, needs_recovery);
2968 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002969 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2970 err);
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002971 goto failed_mount4;
2972 }
2973
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002974 sbi->s_kobj.kset = ext4_kset;
2975 init_completion(&sbi->s_kobj_unregister);
2976 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
2977 "%s", sb->s_id);
2978 if (err) {
2979 ext4_mb_release(sb);
2980 ext4_ext_release(sb);
2981 goto failed_mount4;
2982 };
2983
Mingming Cao617ba132006-10-11 01:20:53 -07002984 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2985 ext4_orphan_cleanup(sb, es);
2986 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002987 if (needs_recovery) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002988 ext4_msg(sb, KERN_INFO, "recovery complete");
Frank Mayhar03901312009-01-07 00:06:22 -05002989 ext4_mark_recovery_complete(sb, es);
2990 }
2991 if (EXT4_SB(sb)->s_journal) {
2992 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2993 descr = " journalled data mode";
2994 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2995 descr = " ordered data mode";
2996 else
2997 descr = " writeback data mode";
2998 } else
2999 descr = "out journal";
3000
Eric Sandeenb31e1552009-06-04 17:36:36 -04003001 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002
3003 lock_kernel();
3004 return 0;
3005
Mingming Cao617ba132006-10-11 01:20:53 -07003006cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003007 if (!silent)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003008 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009 goto failed_mount;
3010
3011failed_mount4:
Eric Sandeenb31e1552009-06-04 17:36:36 -04003012 ext4_msg(sb, KERN_ERR, "mount failed");
Mingming Cao4c0425f2009-09-28 15:48:41 -04003013 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
3014failed_mount_wq:
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003015 ext4_release_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003016 if (sbi->s_journal) {
3017 jbd2_journal_destroy(sbi->s_journal);
3018 sbi->s_journal = NULL;
3019 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003020failed_mount3:
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04003021 if (sbi->s_flex_groups) {
3022 if (is_vmalloc_addr(sbi->s_flex_groups))
3023 vfree(sbi->s_flex_groups);
3024 else
3025 kfree(sbi->s_flex_groups);
3026 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003027 percpu_counter_destroy(&sbi->s_freeblocks_counter);
3028 percpu_counter_destroy(&sbi->s_freeinodes_counter);
3029 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003030 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003031failed_mount2:
3032 for (i = 0; i < db_count; i++)
3033 brelse(sbi->s_group_desc[i]);
3034 kfree(sbi->s_group_desc);
3035failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04003036 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003037 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003038 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039#ifdef CONFIG_QUOTA
3040 for (i = 0; i < MAXQUOTAS; i++)
3041 kfree(sbi->s_qf_names[i]);
3042#endif
Mingming Cao617ba132006-10-11 01:20:53 -07003043 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003044 brelse(bh);
3045out_fail:
3046 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04003047 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003048 kfree(sbi);
3049 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08003050 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003051}
3052
3053/*
3054 * Setup any per-fs journal parameters now. We'll do this both on
3055 * initial mount, once the journal has been initialised but before we've
3056 * done any recovery; and again on any subsequent remount.
3057 */
Mingming Cao617ba132006-10-11 01:20:53 -07003058static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003059{
Mingming Cao617ba132006-10-11 01:20:53 -07003060 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003061
Theodore Ts'o30773842009-01-03 20:27:38 -05003062 journal->j_commit_interval = sbi->s_commit_interval;
3063 journal->j_min_batch_time = sbi->s_min_batch_time;
3064 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065
3066 spin_lock(&journal->j_state_lock);
3067 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07003068 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003069 else
Mingming Caodab291a2006-10-11 01:21:01 -07003070 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04003071 if (test_opt(sb, DATA_ERR_ABORT))
3072 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
3073 else
3074 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003075 spin_unlock(&journal->j_state_lock);
3076}
3077
Mingming Cao617ba132006-10-11 01:20:53 -07003078static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 unsigned int journal_inum)
3080{
3081 struct inode *journal_inode;
3082 journal_t *journal;
3083
Frank Mayhar03901312009-01-07 00:06:22 -05003084 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3085
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003086 /* First, test for the existence of a valid inode on disk. Bad
3087 * things happen if we iget() an unused inode, as the subsequent
3088 * iput() will try to delete it. */
3089
David Howells1d1fe1e2008-02-07 00:15:37 -08003090 journal_inode = ext4_iget(sb, journal_inum);
3091 if (IS_ERR(journal_inode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003092 ext4_msg(sb, KERN_ERR, "no journal found");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003093 return NULL;
3094 }
3095 if (!journal_inode->i_nlink) {
3096 make_bad_inode(journal_inode);
3097 iput(journal_inode);
Eric Sandeenb31e1552009-06-04 17:36:36 -04003098 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003099 return NULL;
3100 }
3101
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003102 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08003104 if (!S_ISREG(journal_inode->i_mode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003105 ext4_msg(sb, KERN_ERR, "invalid journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003106 iput(journal_inode);
3107 return NULL;
3108 }
3109
Mingming Caodab291a2006-10-11 01:21:01 -07003110 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003111 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003112 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003113 iput(journal_inode);
3114 return NULL;
3115 }
3116 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003117 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118 return journal;
3119}
3120
Mingming Cao617ba132006-10-11 01:20:53 -07003121static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122 dev_t j_dev)
3123{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003124 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003125 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07003126 ext4_fsblk_t start;
3127 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07003129 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003131 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 struct block_device *bdev;
3133
Frank Mayhar03901312009-01-07 00:06:22 -05003134 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3135
Eric Sandeenb31e1552009-06-04 17:36:36 -04003136 bdev = ext4_blkdev_get(j_dev, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003137 if (bdev == NULL)
3138 return NULL;
3139
3140 if (bd_claim(bdev, sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003141 ext4_msg(sb, KERN_ERR,
3142 "failed to claim external journal device");
Al Viro9a1c3542008-02-22 20:40:24 -05003143 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144 return NULL;
3145 }
3146
3147 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04003148 hblock = bdev_logical_block_size(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003149 if (blocksize < hblock) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003150 ext4_msg(sb, KERN_ERR,
3151 "blocksize too small for journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003152 goto out_bdev;
3153 }
3154
Mingming Cao617ba132006-10-11 01:20:53 -07003155 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3156 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 set_blocksize(bdev, blocksize);
3158 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003159 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3160 "external journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161 goto out_bdev;
3162 }
3163
Mingming Cao617ba132006-10-11 01:20:53 -07003164 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3165 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07003167 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003168 ext4_msg(sb, KERN_ERR, "external journal has "
3169 "bad superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003170 brelse(bh);
3171 goto out_bdev;
3172 }
3173
Mingming Cao617ba132006-10-11 01:20:53 -07003174 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003175 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003176 brelse(bh);
3177 goto out_bdev;
3178 }
3179
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003180 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003181 start = sb_block + 1;
3182 brelse(bh); /* we're done with the superblock */
3183
Mingming Caodab291a2006-10-11 01:21:01 -07003184 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003185 start, len, blocksize);
3186 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003187 ext4_msg(sb, KERN_ERR, "failed to create device journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003188 goto out_bdev;
3189 }
3190 journal->j_private = sb;
3191 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3192 wait_on_buffer(journal->j_sb_buffer);
3193 if (!buffer_uptodate(journal->j_sb_buffer)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003194 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 goto out_journal;
3196 }
3197 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003198 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3199 "user (unsupported) - %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003200 be32_to_cpu(journal->j_superblock->s_nr_users));
3201 goto out_journal;
3202 }
Mingming Cao617ba132006-10-11 01:20:53 -07003203 EXT4_SB(sb)->journal_bdev = bdev;
3204 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003205 return journal;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003206
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07003208 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003209out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07003210 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211 return NULL;
3212}
3213
Mingming Cao617ba132006-10-11 01:20:53 -07003214static int ext4_load_journal(struct super_block *sb,
3215 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003216 unsigned long journal_devnum)
3217{
3218 journal_t *journal;
3219 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3220 dev_t journal_dev;
3221 int err = 0;
3222 int really_read_only;
3223
Frank Mayhar03901312009-01-07 00:06:22 -05003224 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3225
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003226 if (journal_devnum &&
3227 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003228 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3229 "numbers have changed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003230 journal_dev = new_decode_dev(journal_devnum);
3231 } else
3232 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3233
3234 really_read_only = bdev_read_only(sb->s_bdev);
3235
3236 /*
3237 * Are we loading a blank journal or performing recovery after a
3238 * crash? For recovery, we need to check in advance whether we
3239 * can get read-write access to the device.
3240 */
Mingming Cao617ba132006-10-11 01:20:53 -07003241 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003242 if (sb->s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003243 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3244 "required on readonly filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245 if (really_read_only) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003246 ext4_msg(sb, KERN_ERR, "write access "
3247 "unavailable, cannot proceed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003248 return -EROFS;
3249 }
Eric Sandeenb31e1552009-06-04 17:36:36 -04003250 ext4_msg(sb, KERN_INFO, "write access will "
3251 "be enabled during recovery");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252 }
3253 }
3254
3255 if (journal_inum && journal_dev) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003256 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3257 "and inode journals!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258 return -EINVAL;
3259 }
3260
3261 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07003262 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003263 return -EINVAL;
3264 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003265 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266 return -EINVAL;
3267 }
3268
Theodore Ts'o90576c02009-09-29 15:51:30 -04003269 if (!(journal->j_flags & JBD2_BARRIER))
Eric Sandeenb31e1552009-06-04 17:36:36 -04003270 ext4_msg(sb, KERN_INFO, "barriers disabled");
Theodore Ts'o4776004f2008-09-08 23:00:52 -04003271
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003272 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07003273 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003275 ext4_msg(sb, KERN_ERR, "error updating journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003276 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003277 return err;
3278 }
3279 }
3280
Mingming Cao617ba132006-10-11 01:20:53 -07003281 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07003282 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003283 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07003284 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285
3286 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003287 ext4_msg(sb, KERN_ERR, "error loading journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003288 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003289 return err;
3290 }
3291
Mingming Cao617ba132006-10-11 01:20:53 -07003292 EXT4_SB(sb)->s_journal = journal;
3293 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003294
3295 if (journal_devnum &&
3296 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3297 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298
3299 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003300 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003301 }
3302
3303 return 0;
3304}
3305
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003306static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003307{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003308 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07003309 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003310 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003311
3312 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08003313 return error;
Theodore Ts'o914258b2008-10-06 21:35:40 -04003314 if (buffer_write_io_error(sbh)) {
3315 /*
3316 * Oh, dear. A previous attempt to write the
3317 * superblock failed. This could happen because the
3318 * USB device was yanked out. Or it could happen to
3319 * be a transient write error and maybe the block will
3320 * be remapped. Nothing we can do but to retry the
3321 * write and hope for the best.
3322 */
Eric Sandeenb31e1552009-06-04 17:36:36 -04003323 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3324 "superblock detected");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003325 clear_buffer_write_io_error(sbh);
3326 set_buffer_uptodate(sbh);
3327 }
Theodore Ts'o71290b32009-09-10 17:31:04 -04003328 /*
3329 * If the file system is mounted read-only, don't update the
3330 * superblock write time. This avoids updating the superblock
3331 * write time when we are mounting the root file system
3332 * read/only but we need to replay the journal; at that point,
3333 * for people who are east of GMT and who make their clock
3334 * tick in localtime for Windows bug-for-bug compatibility,
3335 * the clock is set in the future, and this will cause e2fsck
3336 * to complain and force a full file system check.
3337 */
3338 if (!(sb->s_flags & MS_RDONLY))
3339 es->s_wtime = cpu_to_le32(get_seconds());
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003340 es->s_kbytes_written =
3341 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3342 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3343 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003344 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3345 &EXT4_SB(sb)->s_freeblocks_counter));
3346 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3347 &EXT4_SB(sb)->s_freeinodes_counter));
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003348 sb->s_dirt = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349 BUFFER_TRACE(sbh, "marking dirty");
3350 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003351 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08003352 error = sync_dirty_buffer(sbh);
3353 if (error)
3354 return error;
3355
3356 error = buffer_write_io_error(sbh);
3357 if (error) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003358 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3359 "superblock");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003360 clear_buffer_write_io_error(sbh);
3361 set_buffer_uptodate(sbh);
3362 }
3363 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08003364 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365}
3366
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003367/*
3368 * Have we just finished recovery? If so, and if we are mounting (or
3369 * remounting) the filesystem readonly, then we will end up with a
3370 * consistent fs on disk. Record that fact.
3371 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003372static void ext4_mark_recovery_complete(struct super_block *sb,
3373 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003374{
Mingming Cao617ba132006-10-11 01:20:53 -07003375 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003376
Frank Mayhar03901312009-01-07 00:06:22 -05003377 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3378 BUG_ON(journal != NULL);
3379 return;
3380 }
Mingming Caodab291a2006-10-11 01:21:01 -07003381 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003382 if (jbd2_journal_flush(journal) < 0)
3383 goto out;
3384
Mingming Cao617ba132006-10-11 01:20:53 -07003385 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003386 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003387 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003388 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003389 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003390
3391out:
Mingming Caodab291a2006-10-11 01:21:01 -07003392 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003393}
3394
3395/*
3396 * If we are mounting (or read-write remounting) a filesystem whose journal
3397 * has recorded an error from a previous lifetime, move that error to the
3398 * main filesystem now.
3399 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003400static void ext4_clear_journal_err(struct super_block *sb,
3401 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003402{
3403 journal_t *journal;
3404 int j_errno;
3405 const char *errstr;
3406
Frank Mayhar03901312009-01-07 00:06:22 -05003407 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3408
Mingming Cao617ba132006-10-11 01:20:53 -07003409 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003410
3411 /*
3412 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003413 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003414 */
3415
Mingming Caodab291a2006-10-11 01:21:01 -07003416 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003417 if (j_errno) {
3418 char nbuf[16];
3419
Mingming Cao617ba132006-10-11 01:20:53 -07003420 errstr = ext4_decode_error(sb, j_errno, nbuf);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003421 ext4_warning(sb, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003422 "from previous mount: %s", errstr);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003423 ext4_warning(sb, "Marking fs in need of filesystem check.");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003424
Mingming Cao617ba132006-10-11 01:20:53 -07003425 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3426 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003427 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003428
Mingming Caodab291a2006-10-11 01:21:01 -07003429 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003430 }
3431}
3432
3433/*
3434 * Force the running and committing transactions to commit,
3435 * and wait on the commit.
3436 */
Mingming Cao617ba132006-10-11 01:20:53 -07003437int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003438{
3439 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003440 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003441
3442 if (sb->s_flags & MS_RDONLY)
3443 return 0;
3444
Mingming Cao617ba132006-10-11 01:20:53 -07003445 journal = EXT4_SB(sb)->s_journal;
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003446 if (journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003447 ret = ext4_journal_force_commit(journal);
Frank Mayhar03901312009-01-07 00:06:22 -05003448
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 return ret;
3450}
3451
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003452static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003453{
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003454 lock_super(sb);
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003455 ext4_commit_super(sb, 1);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003456 unlock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457}
3458
Mingming Cao617ba132006-10-11 01:20:53 -07003459static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003460{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003461 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05003462 tid_t target;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003463 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003464
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003465 trace_ext4_sync_fs(sb, wait);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003466 flush_workqueue(sbi->dio_unwritten_wq);
3467 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003468 if (wait)
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003469 jbd2_log_wait_commit(sbi->s_journal, target);
Frank Mayhar03901312009-01-07 00:06:22 -05003470 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003471 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003472}
3473
3474/*
3475 * LVM calls this function before a (read-only) snapshot is created. This
3476 * gives us a chance to flush the journal completely and mark the fs clean.
3477 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003478static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003479{
Takashi Satoc4be0c12009-01-09 16:40:58 -08003480 int error = 0;
3481 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003482
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003483 if (sb->s_flags & MS_RDONLY)
3484 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003485
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003486 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003487
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003488 /* Now we set up the journal barrier. */
3489 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003490
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003491 /*
3492 * Don't clear the needs_recovery flag if we failed to flush
3493 * the journal.
3494 */
3495 error = jbd2_journal_flush(journal);
3496 if (error < 0) {
3497 out:
3498 jbd2_journal_unlock_updates(journal);
3499 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003500 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003501
3502 /* Journal blocked and flushed, clear needs_recovery flag. */
3503 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3504 error = ext4_commit_super(sb, 1);
3505 if (error)
3506 goto out;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003507 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003508}
3509
3510/*
3511 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3512 * flag here, even though the filesystem is not technically dirty yet.
3513 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003514static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003515{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003516 if (sb->s_flags & MS_RDONLY)
3517 return 0;
3518
3519 lock_super(sb);
3520 /* Reset the needs_recovery flag before the fs is unlocked. */
3521 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3522 ext4_commit_super(sb, 1);
3523 unlock_super(sb);
3524 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Takashi Satoc4be0c12009-01-09 16:40:58 -08003525 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003526}
3527
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003528static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003529{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003530 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003531 struct ext4_sb_info *sbi = EXT4_SB(sb);
3532 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003533 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003534 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003535 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003536 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003537 int err;
3538#ifdef CONFIG_QUOTA
3539 int i;
3540#endif
3541
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003542 lock_kernel();
3543
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003544 /* Store the original options */
Al Virobbd68512009-05-06 10:43:07 -04003545 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003546 old_sb_flags = sb->s_flags;
3547 old_opts.s_mount_opt = sbi->s_mount_opt;
3548 old_opts.s_resuid = sbi->s_resuid;
3549 old_opts.s_resgid = sbi->s_resgid;
3550 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003551 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3552 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003553#ifdef CONFIG_QUOTA
3554 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3555 for (i = 0; i < MAXQUOTAS; i++)
3556 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3557#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003558 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3559 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003560
3561 /*
3562 * Allow the "check" option to be passed as a remount option.
3563 */
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003564 if (!parse_options(data, sb, NULL, &journal_ioprio,
3565 &n_blocks_count, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566 err = -EINVAL;
3567 goto restore_opts;
3568 }
3569
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003570 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003571 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003572
3573 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05003574 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003575
3576 es = sbi->s_es;
3577
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003578 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05003579 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003580 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3581 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003582
3583 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003584 n_blocks_count > ext4_blocks_count(es)) {
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003585 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 err = -EROFS;
3587 goto restore_opts;
3588 }
3589
3590 if (*flags & MS_RDONLY) {
3591 /*
3592 * First of all, the unconditional stuff we have to do
3593 * to disable replay of the journal when we next remount
3594 */
3595 sb->s_flags |= MS_RDONLY;
3596
3597 /*
3598 * OK, test if we are remounting a valid rw partition
3599 * readonly, and if so set the rdonly flag and then
3600 * mark the partition as valid again.
3601 */
Mingming Cao617ba132006-10-11 01:20:53 -07003602 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3603 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003604 es->s_state = cpu_to_le16(sbi->s_mount_state);
3605
Theodore Ts'oa63c9eb2009-05-01 01:59:42 -04003606 if (sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003607 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003608 } else {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003609 /* Make sure we can mount this feature set readwrite */
3610 if (!ext4_feature_set_ok(sb, 0)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003611 err = -EROFS;
3612 goto restore_opts;
3613 }
Eric Sandeenead65962007-02-10 01:46:08 -08003614 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003615 * Make sure the group descriptor checksums
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003616 * are sane. If they aren't, refuse to remount r/w.
Theodore Ts'o8a266462008-07-26 14:34:21 -04003617 */
3618 for (g = 0; g < sbi->s_groups_count; g++) {
3619 struct ext4_group_desc *gdp =
3620 ext4_get_group_desc(sb, g, NULL);
3621
3622 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003623 ext4_msg(sb, KERN_ERR,
3624 "ext4_remount: Checksum for group %u failed (%u!=%u)",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003625 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3626 le16_to_cpu(gdp->bg_checksum));
3627 err = -EINVAL;
3628 goto restore_opts;
3629 }
3630 }
3631
3632 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003633 * If we have an unprocessed orphan list hanging
3634 * around from a previously readonly bdev mount,
3635 * require a full umount/remount for now.
3636 */
3637 if (es->s_last_orphan) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003638 ext4_msg(sb, KERN_WARNING, "Couldn't "
Eric Sandeenead65962007-02-10 01:46:08 -08003639 "remount RDWR because of unprocessed "
3640 "orphan inode list. Please "
Eric Sandeenb31e1552009-06-04 17:36:36 -04003641 "umount/remount instead");
Eric Sandeenead65962007-02-10 01:46:08 -08003642 err = -EINVAL;
3643 goto restore_opts;
3644 }
3645
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003646 /*
3647 * Mounting a RDONLY partition read-write, so reread
3648 * and store the current valid flag. (It may have
3649 * been changed by e2fsck since we originally mounted
3650 * the partition.)
3651 */
Frank Mayhar03901312009-01-07 00:06:22 -05003652 if (sbi->s_journal)
3653 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003654 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003655 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003656 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003657 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003658 sb->s_flags &= ~MS_RDONLY;
3659 }
3660 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003661 ext4_setup_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003662 if (sbi->s_journal == NULL)
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003663 ext4_commit_super(sb, 1);
Frank Mayhar03901312009-01-07 00:06:22 -05003664
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665#ifdef CONFIG_QUOTA
3666 /* Release old quota file names */
3667 for (i = 0; i < MAXQUOTAS; i++)
3668 if (old_opts.s_qf_names[i] &&
3669 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3670 kfree(old_opts.s_qf_names[i]);
3671#endif
Al Virobbd68512009-05-06 10:43:07 -04003672 unlock_super(sb);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003673 unlock_kernel();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003674 return 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003675
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003676restore_opts:
3677 sb->s_flags = old_sb_flags;
3678 sbi->s_mount_opt = old_opts.s_mount_opt;
3679 sbi->s_resuid = old_opts.s_resuid;
3680 sbi->s_resgid = old_opts.s_resgid;
3681 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003682 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3683 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003684#ifdef CONFIG_QUOTA
3685 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3686 for (i = 0; i < MAXQUOTAS; i++) {
3687 if (sbi->s_qf_names[i] &&
3688 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3689 kfree(sbi->s_qf_names[i]);
3690 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3691 }
3692#endif
Al Virobbd68512009-05-06 10:43:07 -04003693 unlock_super(sb);
Alessio Igor Bogani337eb002009-05-12 15:10:54 +02003694 unlock_kernel();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003695 return err;
3696}
3697
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003698static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003699{
3700 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003701 struct ext4_sb_info *sbi = EXT4_SB(sb);
3702 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003703 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003704
Badari Pulavarty5e700302007-07-15 23:42:00 -07003705 if (test_opt(sb, MINIX_DF)) {
3706 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003707 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04003708 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003709 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003710
3711 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003712 * Compute the overhead (FS structures). This is constant
3713 * for a given filesystem unless the number of block groups
3714 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003715 */
3716
3717 /*
3718 * All of the blocks before first_data_block are
3719 * overhead
3720 */
3721 overhead = le32_to_cpu(es->s_first_data_block);
3722
3723 /*
3724 * Add the overhead attributed to the superblock and
3725 * block group descriptors. If the sparse superblocks
3726 * feature is turned on, then not all groups have this.
3727 */
3728 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003729 overhead += ext4_bg_has_super(sb, i) +
3730 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003731 cond_resched();
3732 }
3733
3734 /*
3735 * Every block group has an inode bitmap, a block
3736 * bitmap, and an inode table.
3737 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003738 overhead += ngroups * (2 + sbi->s_itb_per_group);
3739 sbi->s_overhead_last = overhead;
3740 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003741 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003742 }
3743
Mingming Cao617ba132006-10-11 01:20:53 -07003744 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003745 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003746 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003747 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3748 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003749 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3750 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003751 buf->f_bavail = 0;
3752 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003753 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07003754 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003755 fsid = le64_to_cpup((void *)es->s_uuid) ^
3756 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3757 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3758 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003759
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003760 return 0;
3761}
3762
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003763/* Helper function for writing quotas on sync - we need to start transaction
3764 * before quota file is locked for write. Otherwise the are possible deadlocks:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003765 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003766 * ext4_create() quota_sync()
Jan Karaa269eb12009-01-26 17:04:39 +01003767 * jbd2_journal_start() write_dquot()
3768 * vfs_dq_init() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003769 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003770 *
3771 */
3772
3773#ifdef CONFIG_QUOTA
3774
3775static inline struct inode *dquot_to_inode(struct dquot *dquot)
3776{
3777 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3778}
3779
Mingming Cao617ba132006-10-11 01:20:53 -07003780static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003781{
3782 int ret, err;
3783 handle_t *handle;
3784 struct inode *inode;
3785
3786 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003787 handle = ext4_journal_start(inode,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003788 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003789 if (IS_ERR(handle))
3790 return PTR_ERR(handle);
3791 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003792 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003793 if (!ret)
3794 ret = err;
3795 return ret;
3796}
3797
Mingming Cao617ba132006-10-11 01:20:53 -07003798static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003799{
3800 int ret, err;
3801 handle_t *handle;
3802
Mingming Cao617ba132006-10-11 01:20:53 -07003803 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003804 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 if (IS_ERR(handle))
3806 return PTR_ERR(handle);
3807 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003808 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003809 if (!ret)
3810 ret = err;
3811 return ret;
3812}
3813
Mingming Cao617ba132006-10-11 01:20:53 -07003814static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003815{
3816 int ret, err;
3817 handle_t *handle;
3818
Mingming Cao617ba132006-10-11 01:20:53 -07003819 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003820 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003821 if (IS_ERR(handle)) {
3822 /* Release dquot anyway to avoid endless cycle in dqput() */
3823 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003824 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003825 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003826 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003827 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003828 if (!ret)
3829 ret = err;
3830 return ret;
3831}
3832
Mingming Cao617ba132006-10-11 01:20:53 -07003833static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003835 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003836 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3837 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003838 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003839 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003840 } else {
3841 return dquot_mark_dquot_dirty(dquot);
3842 }
3843}
3844
Mingming Cao617ba132006-10-11 01:20:53 -07003845static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003846{
3847 int ret, err;
3848 handle_t *handle;
3849
3850 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003851 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003852 if (IS_ERR(handle))
3853 return PTR_ERR(handle);
3854 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003855 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003856 if (!ret)
3857 ret = err;
3858 return ret;
3859}
3860
3861/*
3862 * Turn on quotas during mount time - we need to find
3863 * the quota file and such...
3864 */
Mingming Cao617ba132006-10-11 01:20:53 -07003865static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003866{
Mingming Cao617ba132006-10-11 01:20:53 -07003867 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003868 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869}
3870
3871/*
3872 * Standard function to be called on quota_on
3873 */
Mingming Cao617ba132006-10-11 01:20:53 -07003874static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003875 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876{
3877 int err;
Al Viro82646132008-08-02 00:57:06 -04003878 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879
3880 if (!test_opt(sb, QUOTA))
3881 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003882 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003883 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003884 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003885
Al Viro82646132008-08-02 00:57:06 -04003886 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003887 if (err)
3888 return err;
Jan Kara06235432008-05-13 19:11:51 -04003889
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003890 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003891 if (path.mnt->mnt_sb != sb) {
3892 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003893 return -EXDEV;
3894 }
Jan Kara06235432008-05-13 19:11:51 -04003895 /* Journaling quota? */
3896 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003897 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003898 if (path.dentry->d_parent != sb->s_root)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003899 ext4_msg(sb, KERN_WARNING,
3900 "Quota file not on filesystem root. "
3901 "Journaled quota will not work");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003902 }
Jan Kara06235432008-05-13 19:11:51 -04003903
3904 /*
3905 * When we journal data on quota file, we have to flush journal to see
3906 * all updates to the file when we bypass pagecache...
3907 */
Frank Mayhar03901312009-01-07 00:06:22 -05003908 if (EXT4_SB(sb)->s_journal &&
3909 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003910 /*
3911 * We don't need to lock updates but journal_flush() could
3912 * otherwise be livelocked...
3913 */
3914 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003915 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003916 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003917 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003918 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003919 return err;
3920 }
Jan Kara06235432008-05-13 19:11:51 -04003921 }
3922
Al Viro82646132008-08-02 00:57:06 -04003923 err = vfs_quota_on_path(sb, type, format_id, &path);
3924 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003925 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926}
3927
3928/* Read data from quotafile - avoid pagecache and such because we cannot afford
3929 * acquiring the locks... As quota files are never truncated and quota code
3930 * itself serializes the operations (and noone else should touch the files)
3931 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003932static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003933 size_t len, loff_t off)
3934{
3935 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003936 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003937 int err = 0;
3938 int offset = off & (sb->s_blocksize - 1);
3939 int tocopy;
3940 size_t toread;
3941 struct buffer_head *bh;
3942 loff_t i_size = i_size_read(inode);
3943
3944 if (off > i_size)
3945 return 0;
3946 if (off+len > i_size)
3947 len = i_size-off;
3948 toread = len;
3949 while (toread > 0) {
3950 tocopy = sb->s_blocksize - offset < toread ?
3951 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003952 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003953 if (err)
3954 return err;
3955 if (!bh) /* A hole? */
3956 memset(data, 0, tocopy);
3957 else
3958 memcpy(data, bh->b_data+offset, tocopy);
3959 brelse(bh);
3960 offset = 0;
3961 toread -= tocopy;
3962 data += tocopy;
3963 blk++;
3964 }
3965 return len;
3966}
3967
3968/* Write to quotafile (we know the transaction is already started and has
3969 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003970static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003971 const char *data, size_t len, loff_t off)
3972{
3973 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003974 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003975 int err = 0;
3976 int offset = off & (sb->s_blocksize - 1);
3977 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003978 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003979 size_t towrite = len;
3980 struct buffer_head *bh;
3981 handle_t *handle = journal_current_handle();
3982
Frank Mayhar03901312009-01-07 00:06:22 -05003983 if (EXT4_SB(sb)->s_journal && !handle) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003984 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3985 " cancelled because transaction is not started",
Jan Kara9c3013e2007-09-11 15:23:29 -07003986 (unsigned long long)off, (unsigned long long)len);
3987 return -EIO;
3988 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003989 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3990 while (towrite > 0) {
3991 tocopy = sb->s_blocksize - offset < towrite ?
3992 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003993 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003994 if (!bh)
3995 goto out;
3996 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003997 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003998 if (err) {
3999 brelse(bh);
4000 goto out;
4001 }
4002 }
4003 lock_buffer(bh);
4004 memcpy(bh->b_data+offset, data, tocopy);
4005 flush_dcache_page(bh->b_page);
4006 unlock_buffer(bh);
4007 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05004008 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004009 else {
4010 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04004011 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012 mark_buffer_dirty(bh);
4013 }
4014 brelse(bh);
4015 if (err)
4016 goto out;
4017 offset = 0;
4018 towrite -= tocopy;
4019 data += tocopy;
4020 blk++;
4021 }
4022out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07004023 if (len == towrite) {
4024 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004025 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07004026 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027 if (inode->i_size < off+len-towrite) {
4028 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07004029 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004030 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004031 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004032 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004033 mutex_unlock(&inode->i_mutex);
4034 return len - towrite;
4035}
4036
4037#endif
4038
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004039static int ext4_get_sb(struct file_system_type *fs_type, int flags,
4040 const char *dev_name, void *data, struct vfsmount *mnt)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004041{
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004042 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004043}
4044
Theodore Ts'oa2142382009-12-09 21:09:58 -05004045#if !defined(CONTIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004046static struct file_system_type ext2_fs_type = {
4047 .owner = THIS_MODULE,
4048 .name = "ext2",
4049 .get_sb = ext4_get_sb,
4050 .kill_sb = kill_block_super,
4051 .fs_flags = FS_REQUIRES_DEV,
4052};
4053
4054static inline void register_as_ext2(void)
4055{
4056 int err = register_filesystem(&ext2_fs_type);
4057 if (err)
4058 printk(KERN_WARNING
4059 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
4060}
4061
4062static inline void unregister_as_ext2(void)
4063{
4064 unregister_filesystem(&ext2_fs_type);
4065}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004066MODULE_ALIAS("ext2");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004067#else
4068static inline void register_as_ext2(void) { }
4069static inline void unregister_as_ext2(void) { }
4070#endif
4071
Theodore Ts'oa2142382009-12-09 21:09:58 -05004072#if !defined(CONTIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004073static struct file_system_type ext3_fs_type = {
4074 .owner = THIS_MODULE,
4075 .name = "ext3",
4076 .get_sb = ext4_get_sb,
4077 .kill_sb = kill_block_super,
4078 .fs_flags = FS_REQUIRES_DEV,
4079};
4080
4081static inline void register_as_ext3(void)
4082{
4083 int err = register_filesystem(&ext3_fs_type);
4084 if (err)
4085 printk(KERN_WARNING
4086 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
4087}
4088
4089static inline void unregister_as_ext3(void)
4090{
4091 unregister_filesystem(&ext3_fs_type);
4092}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004093MODULE_ALIAS("ext3");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004094#else
4095static inline void register_as_ext3(void) { }
4096static inline void unregister_as_ext3(void) { }
4097#endif
4098
Theodore Ts'o03010a32008-10-10 20:02:48 -04004099static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004100 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04004101 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07004102 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004103 .kill_sb = kill_block_super,
4104 .fs_flags = FS_REQUIRES_DEV,
4105};
4106
Mingming Cao617ba132006-10-11 01:20:53 -07004107static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004108{
Alex Tomasc9de5602008-01-29 00:19:52 -05004109 int err;
4110
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004111 err = init_ext4_system_zone();
4112 if (err)
4113 return err;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004114 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4115 if (!ext4_kset)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004116 goto out4;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004117 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05004118 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004119 if (err)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004120 goto out3;
Alex Tomasc9de5602008-01-29 00:19:52 -05004121
4122 err = init_ext4_xattr();
4123 if (err)
4124 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004125 err = init_inodecache();
4126 if (err)
4127 goto out1;
Theodore Ts'o24b58422009-12-07 14:08:51 -05004128 register_as_ext2();
4129 register_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004130 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004131 if (err)
4132 goto out;
4133 return 0;
4134out:
Theodore Ts'o24b58422009-12-07 14:08:51 -05004135 unregister_as_ext2();
4136 unregister_as_ext3();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004137 destroy_inodecache();
4138out1:
Mingming Cao617ba132006-10-11 01:20:53 -07004139 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004140out2:
4141 exit_ext4_mballoc();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004142out3:
4143 remove_proc_entry("fs/ext4", NULL);
4144 kset_unregister(ext4_kset);
4145out4:
4146 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004147 return err;
4148}
4149
Mingming Cao617ba132006-10-11 01:20:53 -07004150static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004151{
Theodore Ts'o24b58422009-12-07 14:08:51 -05004152 unregister_as_ext2();
4153 unregister_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004154 unregister_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004155 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07004156 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004157 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004158 remove_proc_entry("fs/ext4", NULL);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004159 kset_unregister(ext4_kset);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004160 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004161}
4162
4163MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05004164MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004165MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07004166module_init(init_ext4_fs)
4167module_exit(exit_ext4_fs)