blob: 8ecc1e590303841b0c5f13d07568082230acdf4d [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>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070030#include <linux/exportfs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070031#include <linux/vfs.h>
32#include <linux/random.h>
33#include <linux/mount.h>
34#include <linux/namei.h>
35#include <linux/quotaops.h>
36#include <linux/seq_file.h>
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040037#include <linux/proc_fs.h>
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040038#include <linux/ctype.h>
Vignesh Babu13305932007-07-18 09:11:02 -040039#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040040#include <linux/crc16.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070041#include <asm/uaccess.h>
42
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040043#include "ext4.h"
44#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070045#include "xattr.h"
46#include "acl.h"
Theodore Ts'o3661d282009-09-14 22:59:50 -040047#include "mballoc.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048
Theodore Ts'o9bffad12009-06-17 11:48:11 -040049#define CREATE_TRACE_POINTS
50#include <trace/events/ext4.h>
51
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040052struct proc_dir_entry *ext4_proc_root;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040053static struct kset *ext4_kset;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040054
Mingming Cao617ba132006-10-11 01:20:53 -070055static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070056 unsigned long journal_devnum);
Theodore Ts'oe2d67052009-05-01 00:33:44 -040057static int ext4_commit_super(struct super_block *sb, int sync);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040058static void ext4_mark_recovery_complete(struct super_block *sb,
59 struct ext4_super_block *es);
60static void ext4_clear_journal_err(struct super_block *sb,
61 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070062static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040063static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064 char nbuf[16]);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040065static int ext4_remount(struct super_block *sb, int *flags, char *data);
66static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080067static int ext4_unfreeze(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040068static void ext4_write_super(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080069static int ext4_freeze(struct super_block *sb);
Jan Karaba69f9a2010-03-24 20:18:37 -040070static int ext4_get_sb(struct file_system_type *fs_type, int flags,
71 const char *dev_name, void *data, struct vfsmount *mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070072
Jan Karaba69f9a2010-03-24 20:18:37 -040073#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
74static struct file_system_type ext3_fs_type = {
75 .owner = THIS_MODULE,
76 .name = "ext3",
77 .get_sb = ext4_get_sb,
78 .kill_sb = kill_block_super,
79 .fs_flags = FS_REQUIRES_DEV,
80};
81#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
82#else
83#define IS_EXT3_SB(sb) (0)
84#endif
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070085
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070086ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
87 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070088{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040089 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070090 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -040091 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070092}
93
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070094ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
95 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070096{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040097 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070098 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -040099 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700100}
101
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700102ext4_fsblk_t ext4_inode_table(struct super_block *sb,
103 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700104{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400105 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700106 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400107 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700108}
109
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500110__u32 ext4_free_blks_count(struct super_block *sb,
111 struct ext4_group_desc *bg)
112{
113 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
114 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400115 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500116}
117
118__u32 ext4_free_inodes_count(struct super_block *sb,
119 struct ext4_group_desc *bg)
120{
121 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
122 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400123 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500124}
125
126__u32 ext4_used_dirs_count(struct super_block *sb,
127 struct ext4_group_desc *bg)
128{
129 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
130 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400131 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500132}
133
134__u32 ext4_itable_unused_count(struct super_block *sb,
135 struct ext4_group_desc *bg)
136{
137 return le16_to_cpu(bg->bg_itable_unused_lo) |
138 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400139 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500140}
141
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700142void ext4_block_bitmap_set(struct super_block *sb,
143 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700144{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400145 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700146 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
147 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700148}
149
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700150void ext4_inode_bitmap_set(struct super_block *sb,
151 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700152{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400153 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700154 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
155 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700156}
157
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700158void ext4_inode_table_set(struct super_block *sb,
159 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700160{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400161 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700162 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
163 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700164}
165
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500166void ext4_free_blks_set(struct super_block *sb,
167 struct ext4_group_desc *bg, __u32 count)
168{
169 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
170 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
171 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
172}
173
174void ext4_free_inodes_set(struct super_block *sb,
175 struct ext4_group_desc *bg, __u32 count)
176{
177 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
178 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
179 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
180}
181
182void ext4_used_dirs_set(struct super_block *sb,
183 struct ext4_group_desc *bg, __u32 count)
184{
185 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
186 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
187 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
188}
189
190void ext4_itable_unused_set(struct super_block *sb,
191 struct ext4_group_desc *bg, __u32 count)
192{
193 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
194 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
195 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
196}
197
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400198
199/* Just increment the non-pointer handle value */
200static handle_t *ext4_get_nojournal(void)
201{
202 handle_t *handle = current->journal_info;
203 unsigned long ref_cnt = (unsigned long)handle;
204
205 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
206
207 ref_cnt++;
208 handle = (handle_t *)ref_cnt;
209
210 current->journal_info = handle;
211 return handle;
212}
213
214
215/* Decrement the non-pointer handle value */
216static void ext4_put_nojournal(handle_t *handle)
217{
218 unsigned long ref_cnt = (unsigned long)handle;
219
220 BUG_ON(ref_cnt == 0);
221
222 ref_cnt--;
223 handle = (handle_t *)ref_cnt;
224
225 current->journal_info = handle;
226}
227
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228/*
Mingming Caodab291a2006-10-11 01:21:01 -0700229 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700230 *
231 * The only special thing we need to do here is to make sure that all
232 * journal_end calls result in the superblock being marked dirty, so
233 * that sync() will call the filesystem's write_super callback if
234 * appropriate.
235 */
Mingming Cao617ba132006-10-11 01:20:53 -0700236handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700237{
238 journal_t *journal;
239
240 if (sb->s_flags & MS_RDONLY)
241 return ERR_PTR(-EROFS);
242
Eric Sandeen437f88c2010-08-01 17:33:29 -0400243 vfs_check_frozen(sb, SB_FREEZE_TRANS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 /* Special case here: if the journal has aborted behind our
245 * backs (eg. EIO in the commit thread), then we still need to
246 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700247 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500248 if (journal) {
249 if (is_journal_aborted(journal)) {
Theodore Ts'oc67d8592010-06-29 11:07:07 -0400250 ext4_abort(sb, "Detected aborted journal");
Frank Mayhar03901312009-01-07 00:06:22 -0500251 return ERR_PTR(-EROFS);
252 }
253 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254 }
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400255 return ext4_get_nojournal();
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700256}
257
258/*
259 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700260 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700261 * that sync() will call the filesystem's write_super callback if
262 * appropriate.
263 */
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400264int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700265{
266 struct super_block *sb;
267 int err;
268 int rc;
269
Frank Mayhar03901312009-01-07 00:06:22 -0500270 if (!ext4_handle_valid(handle)) {
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400271 ext4_put_nojournal(handle);
Frank Mayhar03901312009-01-07 00:06:22 -0500272 return 0;
273 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700274 sb = handle->h_transaction->t_journal->j_private;
275 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700276 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277
278 if (!err)
279 err = rc;
280 if (err)
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400281 __ext4_std_error(sb, where, line, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 return err;
283}
284
Theodore Ts'o90c72012010-06-29 14:53:24 -0400285void ext4_journal_abort_handle(const char *caller, unsigned int line,
286 const char *err_fn, struct buffer_head *bh,
287 handle_t *handle, int err)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288{
289 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700290 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700291
Frank Mayhar03901312009-01-07 00:06:22 -0500292 BUG_ON(!ext4_handle_valid(handle));
293
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700294 if (bh)
295 BUFFER_TRACE(bh, "abort");
296
297 if (!handle->h_err)
298 handle->h_err = err;
299
300 if (is_handle_aborted(handle))
301 return;
302
Theodore Ts'o90c72012010-06-29 14:53:24 -0400303 printk(KERN_ERR "%s:%d: aborting transaction: %s in %s\n",
304 caller, line, errstr, err_fn);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305
Mingming Caodab291a2006-10-11 01:21:01 -0700306 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700307}
308
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400309static void __save_error_info(struct super_block *sb, const char *func,
310 unsigned int line)
311{
312 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
313
314 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
315 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
316 es->s_last_error_time = cpu_to_le32(get_seconds());
317 strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
318 es->s_last_error_line = cpu_to_le32(line);
319 if (!es->s_first_error_time) {
320 es->s_first_error_time = es->s_last_error_time;
321 strncpy(es->s_first_error_func, func,
322 sizeof(es->s_first_error_func));
323 es->s_first_error_line = cpu_to_le32(line);
324 es->s_first_error_ino = es->s_last_error_ino;
325 es->s_first_error_block = es->s_last_error_block;
326 }
Theodore Ts'o66e61a92010-07-27 11:56:04 -0400327 /*
328 * Start the daily error reporting function if it hasn't been
329 * started already
330 */
331 if (!es->s_error_count)
332 mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400333 es->s_error_count = cpu_to_le32(le32_to_cpu(es->s_error_count) + 1);
334}
335
336static void save_error_info(struct super_block *sb, const char *func,
337 unsigned int line)
338{
339 __save_error_info(sb, func, line);
340 ext4_commit_super(sb, 1);
341}
342
343
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700344/* Deal with the reporting of failure conditions on a filesystem such as
345 * inconsistencies detected or read IO failures.
346 *
347 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700348 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700349 * write ordering constraints on the superblock which prevent us from
350 * writing it out straight away; and given that the journal is about to
351 * be aborted, we can't rely on the current, or future, transactions to
352 * write out the superblock safely.
353 *
Mingming Caodab291a2006-10-11 01:21:01 -0700354 * We'll just use the jbd2_journal_abort() error code to record an error in
Thadeu Lima de Souza Cascardod6b198b2010-01-17 19:10:07 -0200355 * the journal instead. On recovery, the journal will complain about
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356 * that error until we've noted it down and cleared it.
357 */
358
Mingming Cao617ba132006-10-11 01:20:53 -0700359static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 if (sb->s_flags & MS_RDONLY)
362 return;
363
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400364 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700365 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700366
Theodore Ts'o4ab2f152009-06-13 10:09:36 -0400367 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700369 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700370 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400371 if (test_opt(sb, ERRORS_RO)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400372 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700373 sb->s_flags |= MS_RDONLY;
374 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700376 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700377 sb->s_id);
378}
379
Eric Sandeen12062dd2010-02-15 14:19:27 -0500380void __ext4_error(struct super_block *sb, const char *function,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400381 unsigned int line, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382{
383 va_list args;
384
385 va_start(args, fmt);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400386 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: ",
387 sb->s_id, function, line, current->comm);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700388 vprintk(fmt, args);
389 printk("\n");
390 va_end(args);
391
Mingming Cao617ba132006-10-11 01:20:53 -0700392 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393}
394
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400395void ext4_error_inode(struct inode *inode, const char *function,
396 unsigned int line, ext4_fsblk_t block,
Frank Mayhar273df552010-03-02 11:46:09 -0500397 const char *fmt, ...)
398{
399 va_list args;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400400 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
Frank Mayhar273df552010-03-02 11:46:09 -0500401
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400402 es->s_last_error_ino = cpu_to_le32(inode->i_ino);
403 es->s_last_error_block = cpu_to_le64(block);
404 save_error_info(inode->i_sb, function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500405 va_start(args, fmt);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400406 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: inode #%lu: ",
407 inode->i_sb->s_id, function, line, inode->i_ino);
408 if (block)
409 printk("block %llu: ", block);
410 printk("comm %s: ", current->comm);
Frank Mayhar273df552010-03-02 11:46:09 -0500411 vprintk(fmt, args);
412 printk("\n");
413 va_end(args);
414
415 ext4_handle_error(inode->i_sb);
416}
417
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400418void ext4_error_file(struct file *file, const char *function,
419 unsigned int line, const char *fmt, ...)
Frank Mayhar273df552010-03-02 11:46:09 -0500420{
421 va_list args;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400422 struct ext4_super_block *es;
Frank Mayhar273df552010-03-02 11:46:09 -0500423 struct inode *inode = file->f_dentry->d_inode;
424 char pathname[80], *path;
425
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400426 es = EXT4_SB(inode->i_sb)->s_es;
427 es->s_last_error_ino = cpu_to_le32(inode->i_ino);
428 save_error_info(inode->i_sb, function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500429 va_start(args, fmt);
430 path = d_path(&(file->f_path), pathname, sizeof(pathname));
431 if (!path)
432 path = "(unknown)";
433 printk(KERN_CRIT
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400434 "EXT4-fs error (device %s): %s:%d: inode #%lu "
435 "(comm %s path %s): ",
436 inode->i_sb->s_id, function, line, inode->i_ino,
437 current->comm, path);
Frank Mayhar273df552010-03-02 11:46:09 -0500438 vprintk(fmt, args);
439 printk("\n");
440 va_end(args);
441
442 ext4_handle_error(inode->i_sb);
443}
444
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400445static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700446 char nbuf[16])
447{
448 char *errstr = NULL;
449
450 switch (errno) {
451 case -EIO:
452 errstr = "IO failure";
453 break;
454 case -ENOMEM:
455 errstr = "Out of memory";
456 break;
457 case -EROFS:
Theodore Ts'o78f1ddb2009-07-27 23:09:47 -0400458 if (!sb || (EXT4_SB(sb)->s_journal &&
459 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 errstr = "Journal has aborted";
461 else
462 errstr = "Readonly filesystem";
463 break;
464 default:
465 /* If the caller passed in an extra buffer for unknown
466 * errors, textualise them now. Else we just return
467 * NULL. */
468 if (nbuf) {
469 /* Check for truncated error codes... */
470 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
471 errstr = nbuf;
472 }
473 break;
474 }
475
476 return errstr;
477}
478
Mingming Cao617ba132006-10-11 01:20:53 -0700479/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700480 * automatically and invokes the appropriate error response. */
481
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400482void __ext4_std_error(struct super_block *sb, const char *function,
483 unsigned int line, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484{
485 char nbuf[16];
486 const char *errstr;
487
488 /* Special case: if the error is EROFS, and we're not already
489 * inside a transaction, then there's really no point in logging
490 * an error. */
491 if (errno == -EROFS && journal_current_handle() == NULL &&
492 (sb->s_flags & MS_RDONLY))
493 return;
494
Mingming Cao617ba132006-10-11 01:20:53 -0700495 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400496 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
497 sb->s_id, function, line, errstr);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400498 save_error_info(sb, function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700499
Mingming Cao617ba132006-10-11 01:20:53 -0700500 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700501}
502
503/*
Mingming Cao617ba132006-10-11 01:20:53 -0700504 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 * abort function may be used to deal with unrecoverable failures such
506 * as journal IO errors or ENOMEM at a critical moment in log management.
507 *
508 * We unconditionally force the filesystem into an ABORT|READONLY state,
509 * unless the error response on the fs has been set to panic in which
510 * case we take the easy way out and panic immediately.
511 */
512
Theodore Ts'oc67d8592010-06-29 11:07:07 -0400513void __ext4_abort(struct super_block *sb, const char *function,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400514 unsigned int line, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700515{
516 va_list args;
517
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400518 save_error_info(sb, function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519 va_start(args, fmt);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400520 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
521 function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 vprintk(fmt, args);
523 printk("\n");
524 va_end(args);
525
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400526 if ((sb->s_flags & MS_RDONLY) == 0) {
527 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
528 sb->s_flags |= MS_RDONLY;
529 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
530 if (EXT4_SB(sb)->s_journal)
531 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
532 save_error_info(sb, function, line);
533 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700535 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700536}
537
Eric Sandeenb31e1552009-06-04 17:36:36 -0400538void ext4_msg (struct super_block * sb, const char *prefix,
539 const char *fmt, ...)
540{
541 va_list args;
542
543 va_start(args, fmt);
544 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
545 vprintk(fmt, args);
546 printk("\n");
547 va_end(args);
548}
549
Eric Sandeen12062dd2010-02-15 14:19:27 -0500550void __ext4_warning(struct super_block *sb, const char *function,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400551 unsigned int line, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700552{
553 va_list args;
554
555 va_start(args, fmt);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400556 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: ",
557 sb->s_id, function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558 vprintk(fmt, args);
559 printk("\n");
560 va_end(args);
561}
562
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400563void __ext4_grp_locked_error(const char *function, unsigned int line,
564 struct super_block *sb, ext4_group_t grp,
565 unsigned long ino, ext4_fsblk_t block,
566 const char *fmt, ...)
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500567__releases(bitlock)
568__acquires(bitlock)
569{
570 va_list args;
571 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
572
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400573 es->s_last_error_ino = cpu_to_le32(ino);
574 es->s_last_error_block = cpu_to_le64(block);
575 __save_error_info(sb, function, line);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500576 va_start(args, fmt);
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400577 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u",
578 sb->s_id, function, line, grp);
579 if (ino)
580 printk("inode %lu: ", ino);
581 if (block)
582 printk("block %llu:", (unsigned long long) block);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500583 vprintk(fmt, args);
584 printk("\n");
585 va_end(args);
586
587 if (test_opt(sb, ERRORS_CONT)) {
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400588 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500589 return;
590 }
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400591
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500592 ext4_unlock_group(sb, grp);
593 ext4_handle_error(sb);
594 /*
595 * We only get here in the ERRORS_RO case; relocking the group
596 * may be dangerous, but nothing bad will happen since the
597 * filesystem will have already been marked read/only and the
598 * journal has been aborted. We return 1 as a hint to callers
599 * who might what to use the return value from
600 * ext4_grp_locked_error() to distinguish beween the
601 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
602 * aggressively from the ext4 function in question, with a
603 * more appropriate error code.
604 */
605 ext4_lock_group(sb, grp);
606 return;
607}
608
Mingming Cao617ba132006-10-11 01:20:53 -0700609void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700610{
Mingming Cao617ba132006-10-11 01:20:53 -0700611 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612
Mingming Cao617ba132006-10-11 01:20:53 -0700613 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700614 return;
615
Eric Sandeen12062dd2010-02-15 14:19:27 -0500616 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700617 "updating to rev %d because of new feature flag, "
618 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700619 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700620
Mingming Cao617ba132006-10-11 01:20:53 -0700621 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
622 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
623 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700624 /* leave es->s_feature_*compat flags alone */
625 /* es->s_uuid will be set by e2fsck if empty */
626
627 /*
628 * The rest of the superblock fields should be zero, and if not it
629 * means they are likely already in use, so leave them alone. We
630 * can leave it up to e2fsck to clean up any inconsistencies there.
631 */
632}
633
634/*
635 * Open the external journal device
636 */
Eric Sandeenb31e1552009-06-04 17:36:36 -0400637static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700638{
639 struct block_device *bdev;
640 char b[BDEVNAME_SIZE];
641
642 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
643 if (IS_ERR(bdev))
644 goto fail;
645 return bdev;
646
647fail:
Eric Sandeenb31e1552009-06-04 17:36:36 -0400648 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700649 __bdevname(dev, b), PTR_ERR(bdev));
650 return NULL;
651}
652
653/*
654 * Release the journal device
655 */
Mingming Cao617ba132006-10-11 01:20:53 -0700656static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657{
658 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500659 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660}
661
Mingming Cao617ba132006-10-11 01:20:53 -0700662static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700663{
664 struct block_device *bdev;
665 int ret = -ENODEV;
666
667 bdev = sbi->journal_bdev;
668 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700669 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 sbi->journal_bdev = NULL;
671 }
672 return ret;
673}
674
675static inline struct inode *orphan_list_entry(struct list_head *l)
676{
Mingming Cao617ba132006-10-11 01:20:53 -0700677 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700678}
679
Mingming Cao617ba132006-10-11 01:20:53 -0700680static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700681{
682 struct list_head *l;
683
Eric Sandeenb31e1552009-06-04 17:36:36 -0400684 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
685 le32_to_cpu(sbi->s_es->s_last_orphan));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686
687 printk(KERN_ERR "sb_info orphan list:\n");
688 list_for_each(l, &sbi->s_orphan) {
689 struct inode *inode = orphan_list_entry(l);
690 printk(KERN_ERR " "
691 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
692 inode->i_sb->s_id, inode->i_ino, inode,
693 inode->i_mode, inode->i_nlink,
694 NEXT_ORPHAN(inode));
695 }
696}
697
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400698static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699{
Mingming Cao617ba132006-10-11 01:20:53 -0700700 struct ext4_sb_info *sbi = EXT4_SB(sb);
701 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400702 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700703
Christoph Hellwige0ccfd92010-05-19 07:16:42 -0400704 dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
705
Mingming Cao4c0425f2009-09-28 15:48:41 -0400706 flush_workqueue(sbi->dio_unwritten_wq);
707 destroy_workqueue(sbi->dio_unwritten_wq);
708
Al Viroa9e220f2009-05-05 22:10:44 -0400709 lock_super(sb);
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200710 if (sb->s_dirt)
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200711 ext4_commit_super(sb, 1);
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200712
Frank Mayhar03901312009-01-07 00:06:22 -0500713 if (sbi->s_journal) {
714 err = jbd2_journal_destroy(sbi->s_journal);
715 sbi->s_journal = NULL;
716 if (err < 0)
Theodore Ts'oc67d8592010-06-29 11:07:07 -0400717 ext4_abort(sb, "Couldn't clean up the journal");
Frank Mayhar03901312009-01-07 00:06:22 -0500718 }
Josef Bacikd4edac32009-12-08 21:48:58 -0500719
720 ext4_release_system_zone(sb);
721 ext4_mb_release(sb);
722 ext4_ext_release(sb);
723 ext4_xattr_put_super(sb);
724
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700726 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700727 es->s_state = cpu_to_le16(sbi->s_mount_state);
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400728 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400730 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400731 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400732 }
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400733 kobject_del(&sbi->s_kobj);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734
735 for (i = 0; i < sbi->s_gdb_count; i++)
736 brelse(sbi->s_group_desc[i]);
737 kfree(sbi->s_group_desc);
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -0400738 if (is_vmalloc_addr(sbi->s_flex_groups))
739 vfree(sbi->s_flex_groups);
740 else
741 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700742 percpu_counter_destroy(&sbi->s_freeblocks_counter);
743 percpu_counter_destroy(&sbi->s_freeinodes_counter);
744 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400745 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700746 brelse(sbi->s_sbh);
747#ifdef CONFIG_QUOTA
748 for (i = 0; i < MAXQUOTAS; i++)
749 kfree(sbi->s_qf_names[i]);
750#endif
751
752 /* Debugging code just in case the in-memory inode orphan list
753 * isn't empty. The on-disk one can be non-empty if we've
754 * detected an error and taken the fs readonly, but the
755 * in-memory list had better be clean by this point. */
756 if (!list_empty(&sbi->s_orphan))
757 dump_orphan_list(sb, sbi);
758 J_ASSERT(list_empty(&sbi->s_orphan));
759
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700760 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700761 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
762 /*
763 * Invalidate the journal device's buffers. We don't want them
764 * floating about in memory - the physical journal device may
765 * hotswapped, and it breaks the `ro-after' testing code.
766 */
767 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700768 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700769 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 }
771 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400772 /*
773 * Now that we are completely done shutting down the
774 * superblock, we need to actually destroy the kobject.
775 */
Theodore Ts'o3197ebd2009-03-31 09:10:09 -0400776 unlock_super(sb);
777 kobject_put(&sbi->s_kobj);
778 wait_for_completion(&sbi->s_kobj_unregister);
Pekka Enberg705895b2009-02-15 18:07:52 -0500779 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700780 kfree(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781}
782
Christoph Lametere18b8902006-12-06 20:33:20 -0800783static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700784
785/*
786 * Called inside transaction, so use GFP_NOFS
787 */
Mingming Cao617ba132006-10-11 01:20:53 -0700788static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789{
Mingming Cao617ba132006-10-11 01:20:53 -0700790 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800792 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 if (!ei)
794 return NULL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400795
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700796 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400797 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700798 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500799 INIT_LIST_HEAD(&ei->i_prealloc_list);
800 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500801 /*
802 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
803 * therefore it can be null here. Don't check it, just initialize
804 * jinode.
805 */
Jan Kara678aaf42008-07-11 19:27:31 -0400806 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400807 ei->i_reserved_data_blocks = 0;
808 ei->i_reserved_meta_blocks = 0;
809 ei->i_allocated_meta_blocks = 0;
Theodore Ts'o9d0be502010-01-01 02:41:30 -0500810 ei->i_da_metadata_calc_len = 0;
Mingming Caod2a17632008-07-14 17:52:37 -0400811 ei->i_delalloc_reserved_flag = 0;
812 spin_lock_init(&(ei->i_block_reservation_lock));
Dmitry Monakhova9e7f442009-12-14 15:21:14 +0300813#ifdef CONFIG_QUOTA
814 ei->i_reserved_quota = 0;
815#endif
Jiaying Zhangc7064ef2010-03-02 13:28:44 -0500816 INIT_LIST_HEAD(&ei->i_completed_io_list);
Jiaying Zhang744692d2010-03-04 16:14:02 -0500817 spin_lock_init(&ei->i_completed_io_lock);
Mingming Cao8d5d02e2009-09-28 15:48:29 -0400818 ei->cur_aio_dio = NULL;
Jan Karab436b9b2009-12-08 23:51:10 -0500819 ei->i_sync_tid = 0;
820 ei->i_datasync_tid = 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400821
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700822 return &ei->vfs_inode;
823}
824
Mingming Cao617ba132006-10-11 01:20:53 -0700825static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700826{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700827 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400828 ext4_msg(inode->i_sb, KERN_ERR,
829 "Inode %lu (%p): orphan list check failed!",
830 inode->i_ino, EXT4_I(inode));
Vasily Averin9f7dd932007-07-15 23:40:45 -0700831 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
832 EXT4_I(inode), sizeof(struct ext4_inode_info),
833 true);
834 dump_stack();
835 }
Mingming Cao617ba132006-10-11 01:20:53 -0700836 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700837}
838
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700839static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700840{
Mingming Cao617ba132006-10-11 01:20:53 -0700841 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700842
Christoph Lametera35afb82007-05-16 22:10:57 -0700843 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400844#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700845 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700846#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500847 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700848 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849}
850
851static int init_inodecache(void)
852{
Mingming Cao617ba132006-10-11 01:20:53 -0700853 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
854 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 0, (SLAB_RECLAIM_ACCOUNT|
856 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900857 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700858 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700859 return -ENOMEM;
860 return 0;
861}
862
863static void destroy_inodecache(void)
864{
Mingming Cao617ba132006-10-11 01:20:53 -0700865 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866}
867
Al Viro0930fcc2010-06-07 13:16:22 -0400868void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869{
Al Viro0930fcc2010-06-07 13:16:22 -0400870 invalidate_inode_buffers(inode);
871 end_writeback(inode);
Christoph Hellwig9f754752010-03-03 09:05:05 -0500872 dquot_drop(inode);
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400873 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500874 if (EXT4_JOURNAL(inode))
875 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400876 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700877}
878
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400879static inline void ext4_show_quota_options(struct seq_file *seq,
880 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700881{
882#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700883 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700884
Jan Kara5a20bdf2009-11-30 23:58:32 +0100885 if (sbi->s_jquota_fmt) {
886 char *fmtname = "";
887
888 switch (sbi->s_jquota_fmt) {
889 case QFMT_VFS_OLD:
890 fmtname = "vfsold";
891 break;
892 case QFMT_VFS_V0:
893 fmtname = "vfsv0";
894 break;
895 case QFMT_VFS_V1:
896 fmtname = "vfsv1";
897 break;
898 }
899 seq_printf(seq, ",jqfmt=%s", fmtname);
900 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700901
902 if (sbi->s_qf_names[USRQUOTA])
903 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
904
905 if (sbi->s_qf_names[GRPQUOTA])
906 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
907
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500908 if (test_opt(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700909 seq_puts(seq, ",usrquota");
910
Dmitry Monakhov482a7422010-02-24 11:35:32 -0500911 if (test_opt(sb, GRPQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700912 seq_puts(seq, ",grpquota");
913#endif
914}
915
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700916/*
917 * Show an option if
918 * - it's set to a non-default value OR
919 * - if the per-sb default is different from the global default
920 */
Mingming Cao617ba132006-10-11 01:20:53 -0700921static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700922{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500923 int def_errors;
924 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700926 struct ext4_sb_info *sbi = EXT4_SB(sb);
927 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700928
929 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500930 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700931
932 if (sbi->s_sb_block != 1)
933 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
934 if (test_opt(sb, MINIX_DF))
935 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500936 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700937 seq_puts(seq, ",grpid");
938 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
939 seq_puts(seq, ",nogrpid");
940 if (sbi->s_resuid != EXT4_DEF_RESUID ||
941 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
942 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
943 }
944 if (sbi->s_resgid != EXT4_DEF_RESGID ||
945 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
946 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
947 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500948 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700949 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500950 def_errors == EXT4_ERRORS_CONTINUE) {
951 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700952 }
953 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500954 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500955 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500956 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700957 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500958 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700959 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500960 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700961 seq_puts(seq, ",debug");
962 if (test_opt(sb, OLDALLOC))
963 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400964#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500965 if (test_opt(sb, XATTR_USER) &&
966 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700967 seq_puts(seq, ",user_xattr");
968 if (!test_opt(sb, XATTR_USER) &&
969 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
970 seq_puts(seq, ",nouser_xattr");
971 }
972#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400973#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500974 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700975 seq_puts(seq, ",acl");
976 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
977 seq_puts(seq, ",noacl");
978#endif
Theodore Ts'o30773842009-01-03 20:27:38 -0500979 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef2007-10-16 23:26:27 -0700980 seq_printf(seq, ",commit=%u",
981 (unsigned) (sbi->s_commit_interval / HZ));
982 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500983 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
984 seq_printf(seq, ",min_batch_time=%u",
985 (unsigned) sbi->s_min_batch_time);
986 }
987 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
988 seq_printf(seq, ",max_batch_time=%u",
989 (unsigned) sbi->s_min_batch_time);
990 }
991
Eric Sandeen571640c2008-05-26 12:29:46 -0400992 /*
993 * We're changing the default of barrier mount option, so
994 * let's always display its mount state so it's clear what its
995 * status is.
996 */
997 seq_puts(seq, ",barrier=");
998 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400999 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
1000 seq_puts(seq, ",journal_async_commit");
Jan Kara39a4bad2010-05-16 17:00:00 -04001001 else if (test_opt(sb, JOURNAL_CHECKSUM))
1002 seq_puts(seq, ",journal_checksum");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001003 if (test_opt(sb, I_VERSION))
1004 seq_puts(seq, ",i_version");
Theodore Ts'o8b67f042010-08-01 23:14:20 -04001005 if (!test_opt(sb, DELALLOC) &&
1006 !(def_mount_opts & EXT4_DEFM_NODELALLOC))
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001007 seq_puts(seq, ",nodelalloc");
1008
Miklos Szeredicb45bbe2008-01-28 23:58:27 -05001009 if (sbi->s_stripe)
1010 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -05001011 /*
1012 * journal mode get enabled in different ways
1013 * So just print the value even if we didn't specify it
1014 */
Mingming Cao617ba132006-10-11 01:20:53 -07001015 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -07001017 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001018 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -07001019 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 seq_puts(seq, ",data=writeback");
1021
Theodore Ts'o240799c2008-10-09 23:53:47 -04001022 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
1023 seq_printf(seq, ",inode_readahead_blks=%u",
1024 sbi->s_inode_readahead_blks);
1025
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001026 if (test_opt(sb, DATA_ERR_ABORT))
1027 seq_puts(seq, ",data_err=abort");
1028
Theodore Ts'oafd46722009-03-16 23:12:23 -04001029 if (test_opt(sb, NO_AUTO_DA_ALLOC))
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001030 seq_puts(seq, ",noauto_da_alloc");
Theodore Ts'oafd46722009-03-16 23:12:23 -04001031
Theodore Ts'o8b67f042010-08-01 23:14:20 -04001032 if (test_opt(sb, DISCARD) && !(def_mount_opts & EXT4_DEFM_DISCARD))
Eric Sandeen5328e632009-11-19 14:25:42 -05001033 seq_puts(seq, ",discard");
1034
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001035 if (test_opt(sb, NOLOAD))
1036 seq_puts(seq, ",norecovery");
1037
Jiaying Zhang744692d2010-03-04 16:14:02 -05001038 if (test_opt(sb, DIOREAD_NOLOCK))
1039 seq_puts(seq, ",dioread_nolock");
1040
Theodore Ts'o8b67f042010-08-01 23:14:20 -04001041 if (test_opt(sb, BLOCK_VALIDITY) &&
1042 !(def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY))
1043 seq_puts(seq, ",block_validity");
1044
Mingming Cao617ba132006-10-11 01:20:53 -07001045 ext4_show_quota_options(seq, sb);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001046
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001047 return 0;
1048}
1049
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001050static struct inode *ext4_nfs_get_inode(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001051 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001053 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001054
Mingming Cao617ba132006-10-11 01:20:53 -07001055 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -07001057 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 return ERR_PTR(-ESTALE);
1059
1060 /* iget isn't really right if the inode is currently unallocated!!
1061 *
Mingming Cao617ba132006-10-11 01:20:53 -07001062 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001063 * deleted, so we should be safe.
1064 *
1065 * Currently we don't know the generation for parent directory, so
1066 * a generation of 0 means "accept any"
1067 */
David Howells1d1fe1e2008-02-07 00:15:37 -08001068 inode = ext4_iget(sb, ino);
1069 if (IS_ERR(inode))
1070 return ERR_CAST(inode);
1071 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 iput(inode);
1073 return ERR_PTR(-ESTALE);
1074 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001075
1076 return inode;
1077}
1078
1079static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001080 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001081{
1082 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1083 ext4_nfs_get_inode);
1084}
1085
1086static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001087 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001088{
1089 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1090 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001091}
1092
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001093/*
1094 * Try to release metadata pages (indirect blocks, directories) which are
1095 * mapped via the block device. Since these pages could have journal heads
1096 * which would prevent try_to_free_buffers() from freeing them, we must use
1097 * jbd2 layer's try_to_free_buffers() function to release them.
1098 */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001099static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1100 gfp_t wait)
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001101{
1102 journal_t *journal = EXT4_SB(sb)->s_journal;
1103
1104 WARN_ON(PageChecked(page));
1105 if (!page_has_buffers(page))
1106 return 0;
1107 if (journal)
1108 return jbd2_journal_try_to_free_buffers(journal, page,
1109 wait & ~__GFP_WAIT);
1110 return try_to_free_buffers(page);
1111}
1112
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001113#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001114#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001115#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116
Mingming Cao617ba132006-10-11 01:20:53 -07001117static int ext4_write_dquot(struct dquot *dquot);
1118static int ext4_acquire_dquot(struct dquot *dquot);
1119static int ext4_release_dquot(struct dquot *dquot);
1120static int ext4_mark_dquot_dirty(struct dquot *dquot);
1121static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -07001122static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Christoph Hellwig307ae182010-05-19 07:16:43 -04001123 char *path);
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04001124static int ext4_quota_off(struct super_block *sb, int type);
Mingming Cao617ba132006-10-11 01:20:53 -07001125static int ext4_quota_on_mount(struct super_block *sb, int type);
1126static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -07001128static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 const char *data, size_t len, loff_t off);
1130
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001131static const struct dquot_operations ext4_quota_operations = {
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001132#ifdef CONFIG_QUOTA
Mingming Cao60e58e02009-01-22 18:13:05 +01001133 .get_reserved_space = ext4_get_reserved_space,
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001134#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001135 .write_dquot = ext4_write_dquot,
1136 .acquire_dquot = ext4_acquire_dquot,
1137 .release_dquot = ext4_release_dquot,
1138 .mark_dirty = ext4_mark_dquot_dirty,
Jan Karaa5b5ee32008-11-25 15:31:35 +01001139 .write_info = ext4_write_info,
1140 .alloc_dquot = dquot_alloc,
1141 .destroy_dquot = dquot_destroy,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001142};
1143
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001144static const struct quotactl_ops ext4_qctl_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07001145 .quota_on = ext4_quota_on,
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04001146 .quota_off = ext4_quota_off,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001147 .quota_sync = dquot_quota_sync,
1148 .get_info = dquot_get_dqinfo,
1149 .set_info = dquot_set_dqinfo,
1150 .get_dqblk = dquot_get_dqblk,
1151 .set_dqblk = dquot_set_dqblk
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152};
1153#endif
1154
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001155static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -07001156 .alloc_inode = ext4_alloc_inode,
1157 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001158 .write_inode = ext4_write_inode,
1159 .dirty_inode = ext4_dirty_inode,
Al Viro0930fcc2010-06-07 13:16:22 -04001160 .evict_inode = ext4_evict_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001161 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001162 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001163 .freeze_fs = ext4_freeze,
1164 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001165 .statfs = ext4_statfs,
1166 .remount_fs = ext4_remount,
Mingming Cao617ba132006-10-11 01:20:53 -07001167 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001169 .quota_read = ext4_quota_read,
1170 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001172 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173};
1174
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001175static const struct super_operations ext4_nojournal_sops = {
1176 .alloc_inode = ext4_alloc_inode,
1177 .destroy_inode = ext4_destroy_inode,
1178 .write_inode = ext4_write_inode,
1179 .dirty_inode = ext4_dirty_inode,
Al Viro0930fcc2010-06-07 13:16:22 -04001180 .evict_inode = ext4_evict_inode,
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001181 .write_super = ext4_write_super,
1182 .put_super = ext4_put_super,
1183 .statfs = ext4_statfs,
1184 .remount_fs = ext4_remount,
Theodore Ts'o9ca92382009-05-01 12:52:25 -04001185 .show_options = ext4_show_options,
1186#ifdef CONFIG_QUOTA
1187 .quota_read = ext4_quota_read,
1188 .quota_write = ext4_quota_write,
1189#endif
1190 .bdev_try_to_free_page = bdev_try_to_free_page,
1191};
1192
Christoph Hellwig39655162007-10-21 16:42:17 -07001193static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001194 .fh_to_dentry = ext4_fh_to_dentry,
1195 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001196 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197};
1198
1199enum {
1200 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1201 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001202 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001203 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001204 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001205 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001206 Opt_journal_update, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001207 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Theodore Ts'o296c3552009-09-30 00:32:42 -04001209 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara5a20bdf2009-11-30 23:58:32 +01001211 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1212 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
1213 Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001214 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001215 Opt_block_validity, Opt_noblock_validity,
Eric Sandeen5328e632009-11-19 14:25:42 -05001216 Opt_inode_readahead_blks, Opt_journal_ioprio,
Jiaying Zhang744692d2010-03-04 16:14:02 -05001217 Opt_dioread_nolock, Opt_dioread_lock,
Eric Sandeen5328e632009-11-19 14:25:42 -05001218 Opt_discard, Opt_nodiscard,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001219};
1220
Steven Whitehousea447c092008-10-13 10:46:57 +01001221static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 {Opt_bsd_df, "bsddf"},
1223 {Opt_minix_df, "minixdf"},
1224 {Opt_grpid, "grpid"},
1225 {Opt_grpid, "bsdgroups"},
1226 {Opt_nogrpid, "nogrpid"},
1227 {Opt_nogrpid, "sysvgroups"},
1228 {Opt_resgid, "resgid=%u"},
1229 {Opt_resuid, "resuid=%u"},
1230 {Opt_sb, "sb=%u"},
1231 {Opt_err_cont, "errors=continue"},
1232 {Opt_err_panic, "errors=panic"},
1233 {Opt_err_ro, "errors=remount-ro"},
1234 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 {Opt_debug, "debug"},
1236 {Opt_oldalloc, "oldalloc"},
1237 {Opt_orlov, "orlov"},
1238 {Opt_user_xattr, "user_xattr"},
1239 {Opt_nouser_xattr, "nouser_xattr"},
1240 {Opt_acl, "acl"},
1241 {Opt_noacl, "noacl"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 {Opt_noload, "noload"},
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001243 {Opt_noload, "norecovery"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244 {Opt_nobh, "nobh"},
1245 {Opt_bh, "bh"},
1246 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001247 {Opt_min_batch_time, "min_batch_time=%u"},
1248 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249 {Opt_journal_update, "journal=update"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001250 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001251 {Opt_journal_checksum, "journal_checksum"},
1252 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253 {Opt_abort, "abort"},
1254 {Opt_data_journal, "data=journal"},
1255 {Opt_data_ordered, "data=ordered"},
1256 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001257 {Opt_data_err_abort, "data_err=abort"},
1258 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001259 {Opt_offusrjquota, "usrjquota="},
1260 {Opt_usrjquota, "usrjquota=%s"},
1261 {Opt_offgrpjquota, "grpjquota="},
1262 {Opt_grpjquota, "grpjquota=%s"},
1263 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1264 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara5a20bdf2009-11-30 23:58:32 +01001265 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 {Opt_grpquota, "grpquota"},
1267 {Opt_noquota, "noquota"},
1268 {Opt_quota, "quota"},
1269 {Opt_usrquota, "usrquota"},
1270 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001271 {Opt_barrier, "barrier"},
1272 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001273 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001274 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001275 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001276 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001277 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001278 {Opt_block_validity, "block_validity"},
1279 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001280 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001281 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001282 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001283 {Opt_auto_da_alloc, "auto_da_alloc"},
1284 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001285 {Opt_dioread_nolock, "dioread_nolock"},
1286 {Opt_dioread_lock, "dioread_lock"},
Eric Sandeen5328e632009-11-19 14:25:42 -05001287 {Opt_discard, "discard"},
1288 {Opt_nodiscard, "nodiscard"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001289 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290};
1291
Mingming Cao617ba132006-10-11 01:20:53 -07001292static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001293{
Mingming Cao617ba132006-10-11 01:20:53 -07001294 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 char *options = (char *) *data;
1296
1297 if (!options || strncmp(options, "sb=", 3) != 0)
1298 return 1; /* Default location */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001299
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001300 options += 3;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001301 /* TODO: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 sb_block = simple_strtoul(options, &options, 0);
1303 if (*options && *options != ',') {
Theodore Ts'o47760042008-09-08 23:00:52 -04001304 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 (char *) *data);
1306 return 1;
1307 }
1308 if (*options == ',')
1309 options++;
1310 *data = (void *) options;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001311
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001312 return sb_block;
1313}
1314
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001315#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001316static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1317 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001318
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001319#ifdef CONFIG_QUOTA
1320static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1321{
1322 struct ext4_sb_info *sbi = EXT4_SB(sb);
1323 char *qname;
1324
1325 if (sb_any_quota_loaded(sb) &&
1326 !sbi->s_qf_names[qtype]) {
1327 ext4_msg(sb, KERN_ERR,
1328 "Cannot change journaled "
1329 "quota options when quota turned on");
1330 return 0;
1331 }
1332 qname = match_strdup(args);
1333 if (!qname) {
1334 ext4_msg(sb, KERN_ERR,
1335 "Not enough memory for storing quotafile name");
1336 return 0;
1337 }
1338 if (sbi->s_qf_names[qtype] &&
1339 strcmp(sbi->s_qf_names[qtype], qname)) {
1340 ext4_msg(sb, KERN_ERR,
1341 "%s quota file already specified", QTYPE2NAME(qtype));
1342 kfree(qname);
1343 return 0;
1344 }
1345 sbi->s_qf_names[qtype] = qname;
1346 if (strchr(sbi->s_qf_names[qtype], '/')) {
1347 ext4_msg(sb, KERN_ERR,
1348 "quotafile must be on filesystem root");
1349 kfree(sbi->s_qf_names[qtype]);
1350 sbi->s_qf_names[qtype] = NULL;
1351 return 0;
1352 }
1353 set_opt(sbi->s_mount_opt, QUOTA);
1354 return 1;
1355}
1356
1357static int clear_qf_name(struct super_block *sb, int qtype)
1358{
1359
1360 struct ext4_sb_info *sbi = EXT4_SB(sb);
1361
1362 if (sb_any_quota_loaded(sb) &&
1363 sbi->s_qf_names[qtype]) {
1364 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1365 " when quota turned on");
1366 return 0;
1367 }
1368 /*
1369 * The space will be released later when all options are confirmed
1370 * to be correct
1371 */
1372 sbi->s_qf_names[qtype] = NULL;
1373 return 1;
1374}
1375#endif
1376
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001377static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05001378 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001379 unsigned int *journal_ioprio,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001380 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381{
Mingming Cao617ba132006-10-11 01:20:53 -07001382 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001383 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001384 substring_t args[MAX_OPT_ARGS];
1385 int data_opt = 0;
1386 int option;
1387#ifdef CONFIG_QUOTA
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001388 int qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001389#endif
1390
1391 if (!options)
1392 return 1;
1393
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001394 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 int token;
1396 if (!*p)
1397 continue;
1398
Eric Sandeen15121c12010-02-15 20:17:55 -05001399 /*
1400 * Initialize args struct so we know whether arg was
1401 * found; some options take optional arguments.
1402 */
1403 args[0].to = args[0].from = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001404 token = match_token(p, tokens, args);
1405 switch (token) {
1406 case Opt_bsd_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001407 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001408 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 break;
1410 case Opt_minix_df:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001411 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001412 set_opt(sbi->s_mount_opt, MINIX_DF);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001413
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 break;
1415 case Opt_grpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001416 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001417 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001418
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419 break;
1420 case Opt_nogrpid:
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001421 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001422 clear_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001423
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 break;
1425 case Opt_resuid:
1426 if (match_int(&args[0], &option))
1427 return 0;
1428 sbi->s_resuid = option;
1429 break;
1430 case Opt_resgid:
1431 if (match_int(&args[0], &option))
1432 return 0;
1433 sbi->s_resgid = option;
1434 break;
1435 case Opt_sb:
1436 /* handled by get_sb_block() instead of here */
1437 /* *sb_block = match_int(&args[0]); */
1438 break;
1439 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001440 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1441 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1442 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001443 break;
1444 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001445 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1446 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1447 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001448 break;
1449 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001450 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1451 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1452 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001453 break;
1454 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001455 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001457 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001458 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001459 break;
1460 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001461 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462 break;
1463 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001464 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001465 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001466#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001467 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001468 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469 break;
1470 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001471 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001472 break;
1473#else
1474 case Opt_user_xattr:
1475 case Opt_nouser_xattr:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001476 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001477 break;
1478#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001479#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 case Opt_acl:
1481 set_opt(sbi->s_mount_opt, POSIX_ACL);
1482 break;
1483 case Opt_noacl:
1484 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1485 break;
1486#else
1487 case Opt_acl:
1488 case Opt_noacl:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001489 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 break;
1491#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 case Opt_journal_update:
1493 /* @@@ FIXME */
1494 /* Eventually we will want to be able to create
1495 a journal file here. For now, only allow the
1496 user to specify an existing inode to be the
1497 journal file. */
1498 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001499 ext4_msg(sb, KERN_ERR,
1500 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 return 0;
1502 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001503 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001504 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 case Opt_journal_dev:
1506 if (is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001507 ext4_msg(sb, KERN_ERR,
1508 "Cannot specify journal on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509 return 0;
1510 }
1511 if (match_int(&args[0], &option))
1512 return 0;
1513 *journal_devnum = option;
1514 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001515 case Opt_journal_checksum:
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001516 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1517 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001518 case Opt_journal_async_commit:
1519 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08001520 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
Girish Shilamkar818d2762008-01-28 23:58:27 -05001521 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001523 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001524 break;
1525 case Opt_commit:
1526 if (match_int(&args[0], &option))
1527 return 0;
1528 if (option < 0)
1529 return 0;
1530 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001531 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 sbi->s_commit_interval = HZ * option;
1533 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001534 case Opt_max_batch_time:
1535 if (match_int(&args[0], &option))
1536 return 0;
1537 if (option < 0)
1538 return 0;
1539 if (option == 0)
1540 option = EXT4_DEF_MAX_BATCH_TIME;
1541 sbi->s_max_batch_time = option;
1542 break;
1543 case Opt_min_batch_time:
1544 if (match_int(&args[0], &option))
1545 return 0;
1546 if (option < 0)
1547 return 0;
1548 sbi->s_min_batch_time = option;
1549 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001551 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 goto datacheck;
1553 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001554 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 goto datacheck;
1556 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001557 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001558 datacheck:
1559 if (is_remount) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001560 if (test_opt(sb, DATA_FLAGS) != data_opt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001561 ext4_msg(sb, KERN_ERR,
1562 "Cannot change data mode on remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001563 return 0;
1564 }
1565 } else {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001566 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001567 sbi->s_mount_opt |= data_opt;
1568 }
1569 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001570 case Opt_data_err_abort:
1571 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1572 break;
1573 case Opt_data_err_ignore:
1574 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1575 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001576#ifdef CONFIG_QUOTA
1577 case Opt_usrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001578 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1579 return 0;
1580 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001581 case Opt_grpjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001582 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001583 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 break;
1585 case Opt_offusrjquota:
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001586 if (!clear_qf_name(sb, USRQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001587 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 break;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001589 case Opt_offgrpjquota:
1590 if (!clear_qf_name(sb, GRPQUOTA))
1591 return 0;
1592 break;
1593
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001595 qfmt = QFMT_VFS_OLD;
1596 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001597 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001598 qfmt = QFMT_VFS_V0;
Jan Kara5a20bdf2009-11-30 23:58:32 +01001599 goto set_qf_format;
1600 case Opt_jqfmt_vfsv1:
1601 qfmt = QFMT_VFS_V1;
Jan Karadfc5d032008-05-13 19:11:51 -04001602set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001603 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001604 sbi->s_jquota_fmt != qfmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001605 ext4_msg(sb, KERN_ERR, "Cannot change "
Jan Karadfc5d032008-05-13 19:11:51 -04001606 "journaled quota options when "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001607 "quota turned on");
Jan Karadfc5d032008-05-13 19:11:51 -04001608 return 0;
1609 }
1610 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 break;
1612 case Opt_quota:
1613 case Opt_usrquota:
1614 set_opt(sbi->s_mount_opt, QUOTA);
1615 set_opt(sbi->s_mount_opt, USRQUOTA);
1616 break;
1617 case Opt_grpquota:
1618 set_opt(sbi->s_mount_opt, QUOTA);
1619 set_opt(sbi->s_mount_opt, GRPQUOTA);
1620 break;
1621 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001622 if (sb_any_quota_loaded(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001623 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1624 "options when quota turned on");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001625 return 0;
1626 }
1627 clear_opt(sbi->s_mount_opt, QUOTA);
1628 clear_opt(sbi->s_mount_opt, USRQUOTA);
1629 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1630 break;
1631#else
1632 case Opt_quota:
1633 case Opt_usrquota:
1634 case Opt_grpquota:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001635 ext4_msg(sb, KERN_ERR,
1636 "quota options not supported");
Jan Karacd59e7b2008-05-13 19:11:51 -04001637 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 case Opt_usrjquota:
1639 case Opt_grpjquota:
1640 case Opt_offusrjquota:
1641 case Opt_offgrpjquota:
1642 case Opt_jqfmt_vfsold:
1643 case Opt_jqfmt_vfsv0:
Jan Kara5a20bdf2009-11-30 23:58:32 +01001644 case Opt_jqfmt_vfsv1:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001645 ext4_msg(sb, KERN_ERR,
1646 "journaled quota options not supported");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 break;
1648 case Opt_noquota:
1649 break;
1650#endif
1651 case Opt_abort:
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04001652 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001653 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001654 case Opt_nobarrier:
1655 clear_opt(sbi->s_mount_opt, BARRIER);
1656 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 case Opt_barrier:
Eric Sandeen15121c12010-02-15 20:17:55 -05001658 if (args[0].from) {
1659 if (match_int(&args[0], &option))
1660 return 0;
1661 } else
1662 option = 1; /* No argument, default to 1 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001663 if (option)
1664 set_opt(sbi->s_mount_opt, BARRIER);
1665 else
1666 clear_opt(sbi->s_mount_opt, BARRIER);
1667 break;
1668 case Opt_ignore:
1669 break;
1670 case Opt_resize:
1671 if (!is_remount) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001672 ext4_msg(sb, KERN_ERR,
1673 "resize option only available "
1674 "for remount");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675 return 0;
1676 }
1677 if (match_int(&args[0], &option) != 0)
1678 return 0;
1679 *n_blocks_count = option;
1680 break;
1681 case Opt_nobh:
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04001682 ext4_msg(sb, KERN_WARNING,
1683 "Ignoring deprecated nobh option");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 break;
1685 case Opt_bh:
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04001686 ext4_msg(sb, KERN_WARNING,
1687 "Ignoring deprecated bh option");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001689 case Opt_i_version:
1690 set_opt(sbi->s_mount_opt, I_VERSION);
1691 sb->s_flags |= MS_I_VERSION;
1692 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001693 case Opt_nodelalloc:
1694 clear_opt(sbi->s_mount_opt, DELALLOC);
1695 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001696 case Opt_stripe:
1697 if (match_int(&args[0], &option))
1698 return 0;
1699 if (option < 0)
1700 return 0;
1701 sbi->s_stripe = option;
1702 break;
Alex Tomas64769242008-07-11 19:27:31 -04001703 case Opt_delalloc:
1704 set_opt(sbi->s_mount_opt, DELALLOC);
1705 break;
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001706 case Opt_block_validity:
1707 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1708 break;
1709 case Opt_noblock_validity:
1710 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1711 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001712 case Opt_inode_readahead_blks:
1713 if (match_int(&args[0], &option))
1714 return 0;
1715 if (option < 0 || option > (1 << 30))
1716 return 0;
Theodore Ts'of7c43952009-04-24 23:31:59 -04001717 if (!is_power_of_2(option)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001718 ext4_msg(sb, KERN_ERR,
1719 "EXT4-fs: inode_readahead_blks"
1720 " must be a power of 2");
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001721 return 0;
1722 }
Theodore Ts'o240799c2008-10-09 23:53:47 -04001723 sbi->s_inode_readahead_blks = option;
1724 break;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001725 case Opt_journal_ioprio:
1726 if (match_int(&args[0], &option))
1727 return 0;
1728 if (option < 0 || option > 7)
1729 break;
1730 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1731 option);
1732 break;
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001733 case Opt_noauto_da_alloc:
1734 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1735 break;
Theodore Ts'oafd46722009-03-16 23:12:23 -04001736 case Opt_auto_da_alloc:
Eric Sandeen15121c12010-02-15 20:17:55 -05001737 if (args[0].from) {
1738 if (match_int(&args[0], &option))
1739 return 0;
1740 } else
1741 option = 1; /* No argument, default to 1 */
Theodore Ts'oafd46722009-03-16 23:12:23 -04001742 if (option)
1743 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1744 else
1745 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1746 break;
Eric Sandeen5328e632009-11-19 14:25:42 -05001747 case Opt_discard:
1748 set_opt(sbi->s_mount_opt, DISCARD);
1749 break;
1750 case Opt_nodiscard:
1751 clear_opt(sbi->s_mount_opt, DISCARD);
1752 break;
Jiaying Zhang744692d2010-03-04 16:14:02 -05001753 case Opt_dioread_nolock:
1754 set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1755 break;
1756 case Opt_dioread_lock:
1757 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1758 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759 default:
Eric Sandeenb31e1552009-06-04 17:36:36 -04001760 ext4_msg(sb, KERN_ERR,
1761 "Unrecognized mount option \"%s\" "
1762 "or missing value", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763 return 0;
1764 }
1765 }
1766#ifdef CONFIG_QUOTA
1767 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001768 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001769 clear_opt(sbi->s_mount_opt, USRQUOTA);
1770
Dmitry Monakhov482a7422010-02-24 11:35:32 -05001771 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001772 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1773
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001774 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001775 ext4_msg(sb, KERN_ERR, "old and new quota "
1776 "format mixing");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001777 return 0;
1778 }
1779
1780 if (!sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001781 ext4_msg(sb, KERN_ERR, "journaled quota format "
1782 "not specified");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783 return 0;
1784 }
1785 } else {
1786 if (sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001787 ext4_msg(sb, KERN_ERR, "journaled quota format "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001788 "specified with no journaling "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001789 "enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001790 return 0;
1791 }
1792 }
1793#endif
1794 return 1;
1795}
1796
Mingming Cao617ba132006-10-11 01:20:53 -07001797static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001798 int read_only)
1799{
Mingming Cao617ba132006-10-11 01:20:53 -07001800 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 int res = 0;
1802
Mingming Cao617ba132006-10-11 01:20:53 -07001803 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001804 ext4_msg(sb, KERN_ERR, "revision level too high, "
1805 "forcing read-only mode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806 res = MS_RDONLY;
1807 }
1808 if (read_only)
1809 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001810 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001811 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1812 "running e2fsck is recommended");
Mingming Cao617ba132006-10-11 01:20:53 -07001813 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001814 ext4_msg(sb, KERN_WARNING,
1815 "warning: mounting fs with errors, "
1816 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1818 le16_to_cpu(es->s_mnt_count) >=
1819 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001820 ext4_msg(sb, KERN_WARNING,
1821 "warning: maximal mount count reached, "
1822 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823 else if (le32_to_cpu(es->s_checkinterval) &&
1824 (le32_to_cpu(es->s_lastcheck) +
1825 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Eric Sandeenb31e1552009-06-04 17:36:36 -04001826 ext4_msg(sb, KERN_WARNING,
1827 "warning: checktime reached, "
1828 "running e2fsck is recommended");
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001829 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05001830 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001831 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001832 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001833 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001835 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001836 if (sbi->s_journal)
1837 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001839 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001841 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Theodore Ts'o7f4520c2009-06-13 10:09:41 -04001842 "bpg=%lu, ipg=%lu, mo=%04x]\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843 sb->s_blocksize,
1844 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001845 EXT4_BLOCKS_PER_GROUP(sb),
1846 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847 sbi->s_mount_opt);
1848
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849 return res;
1850}
1851
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001852static int ext4_fill_flex_info(struct super_block *sb)
1853{
1854 struct ext4_sb_info *sbi = EXT4_SB(sb);
1855 struct ext4_group_desc *gdp = NULL;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001856 ext4_group_t flex_group_count;
1857 ext4_group_t flex_group;
1858 int groups_per_flex = 0;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001859 size_t size;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001860 int i;
1861
Theodore Ts'o503358a2009-11-23 07:24:46 -05001862 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1863 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1864
1865 if (groups_per_flex < 2) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001866 sbi->s_log_groups_per_flex = 0;
1867 return 1;
1868 }
1869
Frederic Bohec62a11f2008-09-08 10:20:24 -04001870 /* We allocate both existing and potentially added groups */
1871 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001872 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1873 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04001874 size = flex_group_count * sizeof(struct flex_groups);
1875 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1876 if (sbi->s_flex_groups == NULL) {
1877 sbi->s_flex_groups = vmalloc(size);
1878 if (sbi->s_flex_groups)
1879 memset(sbi->s_flex_groups, 0, size);
1880 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001881 if (sbi->s_flex_groups == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001882 ext4_msg(sb, KERN_ERR, "not enough memory for "
1883 "%u flex groups", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001884 goto failed;
1885 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001886
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001887 for (i = 0; i < sbi->s_groups_count; i++) {
Theodore Ts'o88b6edd2009-05-25 11:50:39 -04001888 gdp = ext4_get_group_desc(sb, i, NULL);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001889
1890 flex_group = ext4_flex_group(sbi, i);
Theodore Ts'o7ad9bb62009-09-11 16:51:28 -04001891 atomic_add(ext4_free_inodes_count(sb, gdp),
1892 &sbi->s_flex_groups[flex_group].free_inodes);
1893 atomic_add(ext4_free_blks_count(sb, gdp),
1894 &sbi->s_flex_groups[flex_group].free_blocks);
1895 atomic_add(ext4_used_dirs_count(sb, gdp),
1896 &sbi->s_flex_groups[flex_group].used_dirs);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001897 }
1898
1899 return 1;
1900failed:
1901 return 0;
1902}
1903
Andreas Dilger717d50e2007-10-16 18:38:25 -04001904__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1905 struct ext4_group_desc *gdp)
1906{
1907 __u16 crc = 0;
1908
1909 if (sbi->s_es->s_feature_ro_compat &
1910 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1911 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1912 __le32 le_group = cpu_to_le32(block_group);
1913
1914 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1915 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1916 crc = crc16(crc, (__u8 *)gdp, offset);
1917 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1918 /* for checksum of struct ext4_group_desc do the rest...*/
1919 if ((sbi->s_es->s_feature_incompat &
1920 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1921 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1922 crc = crc16(crc, (__u8 *)gdp + offset,
1923 le16_to_cpu(sbi->s_es->s_desc_size) -
1924 offset);
1925 }
1926
1927 return cpu_to_le16(crc);
1928}
1929
1930int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1931 struct ext4_group_desc *gdp)
1932{
1933 if ((sbi->s_es->s_feature_ro_compat &
1934 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1935 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1936 return 0;
1937
1938 return 1;
1939}
1940
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001942static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001943{
Mingming Cao617ba132006-10-11 01:20:53 -07001944 struct ext4_sb_info *sbi = EXT4_SB(sb);
1945 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1946 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001947 ext4_fsblk_t block_bitmap;
1948 ext4_fsblk_t inode_bitmap;
1949 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001950 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001951 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001952
Jose R. Santosce421582007-10-16 18:38:25 -04001953 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1954 flexbg_flag = 1;
1955
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001956 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001957
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001958 for (i = 0; i < sbi->s_groups_count; i++) {
1959 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1960
Jose R. Santosce421582007-10-16 18:38:25 -04001961 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001962 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001963 else
1964 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001965 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001967 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001968 if (block_bitmap < first_block || block_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001969 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001970 "Block bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001971 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 return 0;
1973 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001974 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001975 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001976 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001977 "Inode bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001978 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979 return 0;
1980 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001981 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001982 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001983 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001984 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001985 "Inode table for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04001986 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 return 0;
1988 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001989 ext4_lock_group(sb, i);
Andreas Dilger717d50e2007-10-16 18:38:25 -04001990 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001991 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1992 "Checksum for group %u failed (%u!=%u)",
1993 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1994 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001995 if (!(sb->s_flags & MS_RDONLY)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04001996 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04001997 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001998 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001999 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002000 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04002001 if (!flexbg_flag)
2002 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 }
2004
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002005 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002006 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 return 1;
2008}
2009
Mingming Cao617ba132006-10-11 01:20:53 -07002010/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 * the superblock) which were deleted from all directories, but held open by
2012 * a process at the time of a crash. We walk the list and try to delete these
2013 * inodes at recovery time (only with a read-write filesystem).
2014 *
2015 * In order to keep the orphan inode chain consistent during traversal (in
2016 * case of crash during recovery), we link each inode into the superblock
2017 * orphan list_head and handle it the same way as an inode deletion during
2018 * normal operation (which journals the operations for us).
2019 *
2020 * We only do an iget() and an iput() on each inode, which is very safe if we
2021 * accidentally point at an in-use or already deleted inode. The worst that
2022 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07002023 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002024 * e2fsck was run on this filesystem, and it must have already done the orphan
2025 * inode cleanup for us, so we can safely abort without any further action.
2026 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002027static void ext4_orphan_cleanup(struct super_block *sb,
2028 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002029{
2030 unsigned int s_flags = sb->s_flags;
2031 int nr_orphans = 0, nr_truncates = 0;
2032#ifdef CONFIG_QUOTA
2033 int i;
2034#endif
2035 if (!es->s_last_orphan) {
2036 jbd_debug(4, "no orphan inodes to clean up\n");
2037 return;
2038 }
2039
Eric Sandeena8f48a92006-12-06 20:40:13 -08002040 if (bdev_read_only(sb->s_bdev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002041 ext4_msg(sb, KERN_ERR, "write access "
2042 "unavailable, skipping orphan cleanup");
Eric Sandeena8f48a92006-12-06 20:40:13 -08002043 return;
2044 }
2045
Mingming Cao617ba132006-10-11 01:20:53 -07002046 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 if (es->s_last_orphan)
2048 jbd_debug(1, "Errors on filesystem, "
2049 "clearing orphan list.\n");
2050 es->s_last_orphan = 0;
2051 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2052 return;
2053 }
2054
2055 if (s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002056 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 sb->s_flags &= ~MS_RDONLY;
2058 }
2059#ifdef CONFIG_QUOTA
2060 /* Needed for iput() to work correctly and not trash data */
2061 sb->s_flags |= MS_ACTIVE;
2062 /* Turn on quotas so that they are updated correctly */
2063 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002064 if (EXT4_SB(sb)->s_qf_names[i]) {
2065 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002066 if (ret < 0)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002067 ext4_msg(sb, KERN_ERR,
2068 "Cannot turn on journaled "
2069 "quota: error %d", ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070 }
2071 }
2072#endif
2073
2074 while (es->s_last_orphan) {
2075 struct inode *inode;
2076
Josef Bacik97bd42b2008-04-29 22:04:56 -04002077 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2078 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079 es->s_last_orphan = 0;
2080 break;
2081 }
2082
Mingming Cao617ba132006-10-11 01:20:53 -07002083 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Christoph Hellwig871a2932010-03-03 09:05:07 -05002084 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 if (inode->i_nlink) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002086 ext4_msg(sb, KERN_DEBUG,
2087 "%s: truncating inode %lu to %lld bytes",
Harvey Harrison46e665e2008-04-17 10:38:59 -04002088 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab2008-09-08 22:25:04 -04002089 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07002091 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092 nr_truncates++;
2093 } else {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002094 ext4_msg(sb, KERN_DEBUG,
2095 "%s: deleting unreferenced inode %lu",
Harvey Harrison46e665e2008-04-17 10:38:59 -04002096 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002097 jbd_debug(2, "deleting unreferenced inode %lu\n",
2098 inode->i_ino);
2099 nr_orphans++;
2100 }
2101 iput(inode); /* The delete magic happens here! */
2102 }
2103
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002104#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105
2106 if (nr_orphans)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002107 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2108 PLURAL(nr_orphans));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109 if (nr_truncates)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002110 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2111 PLURAL(nr_truncates));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112#ifdef CONFIG_QUOTA
2113 /* Turn quotas off */
2114 for (i = 0; i < MAXQUOTAS; i++) {
2115 if (sb_dqopt(sb)->files[i])
Christoph Hellwig287a8092010-05-19 07:16:45 -04002116 dquot_quota_off(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002117 }
2118#endif
2119 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2120}
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002121
Eric Sandeencd2291a2008-01-28 23:58:27 -05002122/*
2123 * Maximal extent format file size.
2124 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2125 * extent format containers, within a sector_t, and within i_blocks
2126 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2127 * so that won't be a limiting factor.
2128 *
2129 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2130 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002131static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05002132{
2133 loff_t res;
2134 loff_t upper_limit = MAX_LFS_FILESIZE;
2135
2136 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002137 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05002138 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002139 * CONFIG_LBDAF is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05002140 * i_block represent total blocks in 512 bytes
2141 * 32 == size of vfs inode i_blocks * 8
2142 */
2143 upper_limit = (1LL << 32) - 1;
2144
2145 /* total blocks in file system block size */
2146 upper_limit >>= (blkbits - 9);
2147 upper_limit <<= blkbits;
2148 }
2149
2150 /* 32-bit extent-start container, ee_block */
2151 res = 1LL << 32;
2152 res <<= blkbits;
2153 res -= 1;
2154
2155 /* Sanity check against vm- & vfs- imposed limits */
2156 if (res > upper_limit)
2157 res = upper_limit;
2158
2159 return res;
2160}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002161
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002162/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05002163 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002164 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2165 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002167static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002168{
Mingming Cao617ba132006-10-11 01:20:53 -07002169 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002170 int meta_blocks;
2171 loff_t upper_limit;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002172 /* This is calculated to be the largest file size for a dense, block
2173 * mapped file such that the file's total number of 512-byte sectors,
2174 * including data and all indirect blocks, does not exceed (2^48 - 1).
2175 *
2176 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2177 * number of 512-byte sectors of the file.
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002178 */
2179
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002180 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002181 /*
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002182 * !has_huge_files or CONFIG_LBDAF not enabled implies that
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002183 * the inode i_block field represents total file blocks in
2184 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002185 */
2186 upper_limit = (1LL << 32) - 1;
2187
2188 /* total blocks in file system block size */
2189 upper_limit >>= (bits - 9);
2190
2191 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05002192 /*
2193 * We use 48 bit ext4_inode i_blocks
2194 * With EXT4_HUGE_FILE_FL set the i_blocks
2195 * represent total number of blocks in
2196 * file system block size
2197 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002198 upper_limit = (1LL << 48) - 1;
2199
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002200 }
2201
2202 /* indirect blocks */
2203 meta_blocks = 1;
2204 /* double indirect blocks */
2205 meta_blocks += 1 + (1LL << (bits-2));
2206 /* tripple indirect blocks */
2207 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2208
2209 upper_limit -= meta_blocks;
2210 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002211
2212 res += 1LL << (bits-2);
2213 res += 1LL << (2*(bits-2));
2214 res += 1LL << (3*(bits-2));
2215 res <<= bits;
2216 if (res > upper_limit)
2217 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002218
2219 if (res > MAX_LFS_FILESIZE)
2220 res = MAX_LFS_FILESIZE;
2221
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002222 return res;
2223}
2224
Mingming Cao617ba132006-10-11 01:20:53 -07002225static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002226 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227{
Mingming Cao617ba132006-10-11 01:20:53 -07002228 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05002229 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 int has_super = 0;
2231
2232 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2233
Mingming Cao617ba132006-10-11 01:20:53 -07002234 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002236 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002237 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07002238 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002239 has_super = 1;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002240
Mingming Cao617ba132006-10-11 01:20:53 -07002241 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242}
2243
Alex Tomasc9de5602008-01-29 00:19:52 -05002244/**
2245 * ext4_get_stripe_size: Get the stripe size.
2246 * @sbi: In memory super block info
2247 *
2248 * If we have specified it via mount option, then
2249 * use the mount option value. If the value specified at mount time is
2250 * greater than the blocks per group use the super block value.
2251 * If the super block value is greater than blocks per group return 0.
2252 * Allocator needs it be less than blocks per group.
2253 *
2254 */
2255static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2256{
2257 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2258 unsigned long stripe_width =
2259 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2260
2261 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2262 return sbi->s_stripe;
2263
2264 if (stripe_width <= sbi->s_blocks_per_group)
2265 return stripe_width;
2266
2267 if (stride <= sbi->s_blocks_per_group)
2268 return stride;
2269
2270 return 0;
2271}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002272
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002273/* sysfs supprt */
2274
2275struct ext4_attr {
2276 struct attribute attr;
2277 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
Theodore Ts'o60e66792010-05-17 07:00:00 -04002278 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002279 const char *, size_t);
2280 int offset;
2281};
2282
2283static int parse_strtoul(const char *buf,
2284 unsigned long max, unsigned long *value)
2285{
2286 char *endp;
2287
André Goddard Rosae7d28602009-12-14 18:01:06 -08002288 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
2289 endp = skip_spaces(endp);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002290 if (*endp || *value > max)
2291 return -EINVAL;
2292
2293 return 0;
2294}
2295
2296static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2297 struct ext4_sb_info *sbi,
2298 char *buf)
2299{
2300 return snprintf(buf, PAGE_SIZE, "%llu\n",
2301 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2302}
2303
2304static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2305 struct ext4_sb_info *sbi, char *buf)
2306{
2307 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2308
Theodore Ts'of613dfc2010-07-27 11:56:08 -04002309 if (!sb->s_bdev->bd_part)
2310 return snprintf(buf, PAGE_SIZE, "0\n");
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002311 return snprintf(buf, PAGE_SIZE, "%lu\n",
2312 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2313 sbi->s_sectors_written_start) >> 1);
2314}
2315
2316static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2317 struct ext4_sb_info *sbi, char *buf)
2318{
2319 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2320
Theodore Ts'of613dfc2010-07-27 11:56:08 -04002321 if (!sb->s_bdev->bd_part)
2322 return snprintf(buf, PAGE_SIZE, "0\n");
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002323 return snprintf(buf, PAGE_SIZE, "%llu\n",
Andrew Mortona6b43e32009-12-23 07:48:08 -05002324 (unsigned long long)(sbi->s_kbytes_written +
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002325 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
Andrew Mortona6b43e32009-12-23 07:48:08 -05002326 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002327}
2328
2329static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2330 struct ext4_sb_info *sbi,
2331 const char *buf, size_t count)
2332{
2333 unsigned long t;
2334
2335 if (parse_strtoul(buf, 0x40000000, &t))
2336 return -EINVAL;
2337
Theodore Ts'of7c43952009-04-24 23:31:59 -04002338 if (!is_power_of_2(t))
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002339 return -EINVAL;
2340
2341 sbi->s_inode_readahead_blks = t;
2342 return count;
2343}
2344
2345static ssize_t sbi_ui_show(struct ext4_attr *a,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002346 struct ext4_sb_info *sbi, char *buf)
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002347{
2348 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2349
2350 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2351}
2352
2353static ssize_t sbi_ui_store(struct ext4_attr *a,
2354 struct ext4_sb_info *sbi,
2355 const char *buf, size_t count)
2356{
2357 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2358 unsigned long t;
2359
2360 if (parse_strtoul(buf, 0xffffffff, &t))
2361 return -EINVAL;
2362 *ui = t;
2363 return count;
2364}
2365
2366#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2367static struct ext4_attr ext4_attr_##_name = { \
2368 .attr = {.name = __stringify(_name), .mode = _mode }, \
2369 .show = _show, \
2370 .store = _store, \
2371 .offset = offsetof(struct ext4_sb_info, _elname), \
2372}
2373#define EXT4_ATTR(name, mode, show, store) \
2374static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2375
2376#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2377#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2378#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2379 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2380#define ATTR_LIST(name) &ext4_attr_##name.attr
2381
2382EXT4_RO_ATTR(delayed_allocation_blocks);
2383EXT4_RO_ATTR(session_write_kbytes);
2384EXT4_RO_ATTR(lifetime_write_kbytes);
2385EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2386 inode_readahead_blks_store, s_inode_readahead_blks);
Andreas Dilger11013912009-06-13 11:45:35 -04002387EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002388EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2389EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2390EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2391EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2392EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2393EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002394EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002395
2396static struct attribute *ext4_attrs[] = {
2397 ATTR_LIST(delayed_allocation_blocks),
2398 ATTR_LIST(session_write_kbytes),
2399 ATTR_LIST(lifetime_write_kbytes),
2400 ATTR_LIST(inode_readahead_blks),
Andreas Dilger11013912009-06-13 11:45:35 -04002401 ATTR_LIST(inode_goal),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002402 ATTR_LIST(mb_stats),
2403 ATTR_LIST(mb_max_to_scan),
2404 ATTR_LIST(mb_min_to_scan),
2405 ATTR_LIST(mb_order2_req),
2406 ATTR_LIST(mb_stream_req),
2407 ATTR_LIST(mb_group_prealloc),
Theodore Ts'o55138e02009-09-29 13:31:31 -04002408 ATTR_LIST(max_writeback_mb_bump),
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002409 NULL,
2410};
2411
2412static ssize_t ext4_attr_show(struct kobject *kobj,
2413 struct attribute *attr, char *buf)
2414{
2415 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2416 s_kobj);
2417 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2418
2419 return a->show ? a->show(a, sbi, buf) : 0;
2420}
2421
2422static ssize_t ext4_attr_store(struct kobject *kobj,
2423 struct attribute *attr,
2424 const char *buf, size_t len)
2425{
2426 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2427 s_kobj);
2428 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2429
2430 return a->store ? a->store(a, sbi, buf, len) : 0;
2431}
2432
2433static void ext4_sb_release(struct kobject *kobj)
2434{
2435 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2436 s_kobj);
2437 complete(&sbi->s_kobj_unregister);
2438}
2439
2440
Emese Revfy52cf25d2010-01-19 02:58:23 +01002441static const struct sysfs_ops ext4_attr_ops = {
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04002442 .show = ext4_attr_show,
2443 .store = ext4_attr_store,
2444};
2445
2446static struct kobj_type ext4_ktype = {
2447 .default_attrs = ext4_attrs,
2448 .sysfs_ops = &ext4_attr_ops,
2449 .release = ext4_sb_release,
2450};
2451
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002452/*
2453 * Check whether this filesystem can be mounted based on
2454 * the features present and the RDONLY/RDWR mount requested.
2455 * Returns 1 if this filesystem can be mounted as requested,
2456 * 0 if it cannot be.
2457 */
2458static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2459{
2460 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2461 ext4_msg(sb, KERN_ERR,
2462 "Couldn't mount because of "
2463 "unsupported optional features (%x)",
2464 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2465 ~EXT4_FEATURE_INCOMPAT_SUPP));
2466 return 0;
2467 }
2468
2469 if (readonly)
2470 return 1;
2471
2472 /* Check that feature set is OK for a read-write mount */
2473 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2474 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2475 "unsupported optional features (%x)",
2476 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2477 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2478 return 0;
2479 }
2480 /*
2481 * Large file size enabled file system can only be mounted
2482 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2483 */
2484 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2485 if (sizeof(blkcnt_t) < sizeof(u64)) {
2486 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2487 "cannot be mounted RDWR without "
2488 "CONFIG_LBDAF");
2489 return 0;
2490 }
2491 }
2492 return 1;
2493}
2494
Theodore Ts'o66e61a92010-07-27 11:56:04 -04002495/*
2496 * This function is called once a day if we have errors logged
2497 * on the file system
2498 */
2499static void print_daily_error_info(unsigned long arg)
2500{
2501 struct super_block *sb = (struct super_block *) arg;
2502 struct ext4_sb_info *sbi;
2503 struct ext4_super_block *es;
2504
2505 sbi = EXT4_SB(sb);
2506 es = sbi->s_es;
2507
2508 if (es->s_error_count)
2509 ext4_msg(sb, KERN_NOTICE, "error count: %u",
2510 le32_to_cpu(es->s_error_count));
2511 if (es->s_first_error_time) {
2512 printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
2513 sb->s_id, le32_to_cpu(es->s_first_error_time),
2514 (int) sizeof(es->s_first_error_func),
2515 es->s_first_error_func,
2516 le32_to_cpu(es->s_first_error_line));
2517 if (es->s_first_error_ino)
2518 printk(": inode %u",
2519 le32_to_cpu(es->s_first_error_ino));
2520 if (es->s_first_error_block)
2521 printk(": block %llu", (unsigned long long)
2522 le64_to_cpu(es->s_first_error_block));
2523 printk("\n");
2524 }
2525 if (es->s_last_error_time) {
2526 printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
2527 sb->s_id, le32_to_cpu(es->s_last_error_time),
2528 (int) sizeof(es->s_last_error_func),
2529 es->s_last_error_func,
2530 le32_to_cpu(es->s_last_error_line));
2531 if (es->s_last_error_ino)
2532 printk(": inode %u",
2533 le32_to_cpu(es->s_last_error_ino));
2534 if (es->s_last_error_block)
2535 printk(": block %llu", (unsigned long long)
2536 le64_to_cpu(es->s_last_error_block));
2537 printk("\n");
2538 }
2539 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
2540}
2541
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002542static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002543 __releases(kernel_lock)
2544 __acquires(kernel_lock)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002545{
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04002546 char *orig_data = kstrdup(data, GFP_KERNEL);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002547 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002548 struct ext4_super_block *es = NULL;
2549 struct ext4_sb_info *sbi;
2550 ext4_fsblk_t block;
2551 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002552 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002554 unsigned long journal_devnum = 0;
2555 unsigned long def_mount_opts;
2556 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002557 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002558 const char *descr;
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04002559 int ret = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560 int blocksize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002561 unsigned int db_count;
2562 unsigned int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002563 int needs_recovery, has_huge_files;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002564 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002565 int err;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002566 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002567
2568 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2569 if (!sbi)
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04002570 goto out_free_orig;
Pekka Enberg705895b2009-02-15 18:07:52 -05002571
2572 sbi->s_blockgroup_lock =
2573 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2574 if (!sbi->s_blockgroup_lock) {
2575 kfree(sbi);
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04002576 goto out_free_orig;
Pekka Enberg705895b2009-02-15 18:07:52 -05002577 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 sb->s_fs_info = sbi;
2579 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002580 sbi->s_resuid = EXT4_DEF_RESUID;
2581 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002582 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef2007-10-16 23:26:27 -07002583 sbi->s_sb_block = sb_block;
Theodore Ts'of613dfc2010-07-27 11:56:08 -04002584 if (sb->s_bdev->bd_part)
2585 sbi->s_sectors_written_start =
2586 part_stat_read(sb->s_bdev->bd_part, sectors[1]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002587
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002588 /* Cleanup superblock name */
2589 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2590 *cp = '!';
2591
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04002592 ret = -EINVAL;
Mingming Cao617ba132006-10-11 01:20:53 -07002593 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594 if (!blocksize) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002595 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002596 goto out_fail;
2597 }
2598
2599 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002600 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002601 * block sizes. We need to calculate the offset from buffer start.
2602 */
Mingming Cao617ba132006-10-11 01:20:53 -07002603 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002604 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2605 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002606 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002607 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002608 }
2609
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002610 if (!(bh = sb_bread(sb, logical_sb_block))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002611 ext4_msg(sb, KERN_ERR, "unable to read superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002612 goto out_fail;
2613 }
2614 /*
2615 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002616 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 */
Mingming Cao617ba132006-10-11 01:20:53 -07002618 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002619 sbi->s_es = es;
2620 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002621 if (sb->s_magic != EXT4_SUPER_MAGIC)
2622 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05002623 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002624
2625 /* Set defaults before we parse the mount options */
2626 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002627 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002628 set_opt(sbi->s_mount_opt, DEBUG);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002629 if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
2630 ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
2631 "2.6.38");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002632 set_opt(sbi->s_mount_opt, GRPID);
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05002633 }
Mingming Cao617ba132006-10-11 01:20:53 -07002634 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002636#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002637 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002638 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002639#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002640#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002641 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002642 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002643#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002644 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002645 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002646 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002647 set_opt(sbi->s_mount_opt, ORDERED_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07002648 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002649 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002650
Mingming Cao617ba132006-10-11 01:20:53 -07002651 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002652 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002653 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002654 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002655 else
2656 set_opt(sbi->s_mount_opt, ERRORS_RO);
Theodore Ts'o8b67f042010-08-01 23:14:20 -04002657 if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
2658 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
2659 if (def_mount_opts & EXT4_DEFM_DISCARD)
2660 set_opt(sbi->s_mount_opt, DISCARD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661
2662 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2663 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002664 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2665 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2666 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667
Theodore Ts'o8b67f042010-08-01 23:14:20 -04002668 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
2669 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002670
Mingming Cao1e2462f2007-07-18 09:00:55 -04002671 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002672 * enable delayed allocation by default
2673 * Use -o nodelalloc to turn it off
2674 */
Theodore Ts'o8b67f042010-08-01 23:14:20 -04002675 if (!IS_EXT3_SB(sb) &&
2676 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
Jan Karaba69f9a2010-03-24 20:18:37 -04002677 set_opt(sbi->s_mount_opt, DELALLOC);
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002678
Theodore Ts'o8b67f042010-08-01 23:14:20 -04002679 if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
2680 &journal_devnum, &journal_ioprio, NULL, 0)) {
2681 ext4_msg(sb, KERN_WARNING,
2682 "failed to parse options in superblock: %s",
2683 sbi->s_es->s_mount_opts);
2684 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002685 if (!parse_options((char *) data, sb, &journal_devnum,
2686 &journal_ioprio, NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002687 goto failed_mount;
2688
2689 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05002690 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002691
Mingming Cao617ba132006-10-11 01:20:53 -07002692 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2693 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2694 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2695 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002696 ext4_msg(sb, KERN_WARNING,
2697 "feature flags set on rev 0 fs, "
2698 "running e2fsck is recommended");
Theodore Tso469108f2008-02-10 01:11:44 -05002699
2700 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701 * Check feature flags regardless of the revision level, since we
2702 * previously didn't change the revision level when setting the flags,
2703 * so there is a chance incompat flags are set on a rev 0 filesystem.
2704 */
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002705 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706 goto failed_mount;
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002707
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002708 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2709
Mingming Cao617ba132006-10-11 01:20:53 -07002710 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2711 blocksize > EXT4_MAX_BLOCK_SIZE) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002712 ext4_msg(sb, KERN_ERR,
2713 "Unsupported filesystem blocksize %d", blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 goto failed_mount;
2715 }
2716
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002717 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002718 /* Validate the filesystem blocksize */
2719 if (!sb_set_blocksize(sb, blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002720 ext4_msg(sb, KERN_ERR, "bad block size %d",
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002721 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722 goto failed_mount;
2723 }
2724
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002725 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002726 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2727 offset = do_div(logical_sb_block, blocksize);
2728 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 if (!bh) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002730 ext4_msg(sb, KERN_ERR,
2731 "Can't read superblock on 2nd try");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002732 goto failed_mount;
2733 }
Mingming Cao617ba132006-10-11 01:20:53 -07002734 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002735 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002736 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002737 ext4_msg(sb, KERN_ERR,
2738 "Magic mismatch, very weird!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002739 goto failed_mount;
2740 }
2741 }
2742
Eric Sandeena13fb1a2009-08-18 00:20:23 -04002743 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2744 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002745 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2746 has_huge_files);
2747 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748
Mingming Cao617ba132006-10-11 01:20:53 -07002749 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2750 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2751 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 } else {
2753 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2754 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002755 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002756 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 (sbi->s_inode_size > blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002758 ext4_msg(sb, KERN_ERR,
2759 "unsupported inode size: %d",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002760 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002761 goto failed_mount;
2762 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002763 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2764 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765 }
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002766
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002767 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2768 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002769 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002770 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002771 !is_power_of_2(sbi->s_desc_size)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002772 ext4_msg(sb, KERN_ERR,
2773 "unsupported descriptor size %lu",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002774 sbi->s_desc_size);
2775 goto failed_mount;
2776 }
2777 } else
2778 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002779
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002781 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002782 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002783 goto cantfind_ext4;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002784
Mingming Cao617ba132006-10-11 01:20:53 -07002785 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002787 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2789 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002790 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002791 sbi->s_sbh = bh;
2792 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002793 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2794 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002795
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002796 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002797 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2798 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002799 i = le32_to_cpu(es->s_flags);
2800 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2801 sbi->s_hash_unsigned = 3;
2802 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2803#ifdef __CHAR_UNSIGNED__
2804 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2805 sbi->s_hash_unsigned = 3;
2806#else
2807 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2808#endif
2809 sb->s_dirt = 1;
2810 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002811
2812 if (sbi->s_blocks_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002813 ext4_msg(sb, KERN_ERR,
2814 "#blocks per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002815 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002816 goto failed_mount;
2817 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002818 if (sbi->s_inodes_per_group > blocksize * 8) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002819 ext4_msg(sb, KERN_ERR,
2820 "#inodes per group too big: %lu",
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002821 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002822 goto failed_mount;
2823 }
2824
Eric Sandeenbf43d842009-08-17 23:48:51 -04002825 /*
2826 * Test whether we have more sectors than will fit in sector_t,
2827 * and whether the max offset is addressable by the page cache.
2828 */
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07002829 ret = generic_check_addressable(sb->s_blocksize_bits,
2830 ext4_blocks_count(es));
2831 if (ret) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002832 ext4_msg(sb, KERN_ERR, "filesystem"
Eric Sandeenbf43d842009-08-17 23:48:51 -04002833 " too large to mount safely on this system");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002834 if (sizeof(sector_t) < 8)
Bartlomiej Zolnierkiewicz90c699a2009-06-19 08:08:50 +02002835 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002836 goto failed_mount;
2837 }
2838
Mingming Cao617ba132006-10-11 01:20:53 -07002839 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2840 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002841
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002842 /* check blocks count against device size */
2843 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2844 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002845 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2846 "exceeds size of device (%llu blocks)",
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04002847 ext4_blocks_count(es), blocks_count);
2848 goto failed_mount;
2849 }
2850
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002851 /*
2852 * It makes no sense for the first data block to be beyond the end
2853 * of the filesystem.
2854 */
2855 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002856 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2857 "block %u is beyond end of filesystem (%llu)",
2858 le32_to_cpu(es->s_first_data_block),
2859 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05002860 goto failed_mount;
2861 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002862 blocks_count = (ext4_blocks_count(es) -
2863 le32_to_cpu(es->s_first_data_block) +
2864 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2865 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002866 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002867 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002868 "(block count %llu, first data block %u, "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002869 "blocks per group %lu)", sbi->s_groups_count,
Theodore Ts'o4ec11022009-01-06 14:53:26 -05002870 ext4_blocks_count(es),
2871 le32_to_cpu(es->s_first_data_block),
2872 EXT4_BLOCKS_PER_GROUP(sb));
2873 goto failed_mount;
2874 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002875 sbi->s_groups_count = blocks_count;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04002876 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2877 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Mingming Cao617ba132006-10-11 01:20:53 -07002878 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2879 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002880 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881 GFP_KERNEL);
2882 if (sbi->s_group_desc == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002883 ext4_msg(sb, KERN_ERR, "not enough memory");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002884 goto failed_mount;
2885 }
2886
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002887#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002888 if (ext4_proc_root)
2889 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002890#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002891
Pekka Enberg705895b2009-02-15 18:07:52 -05002892 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893
2894 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002895 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896 sbi->s_group_desc[i] = sb_bread(sb, block);
2897 if (!sbi->s_group_desc[i]) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002898 ext4_msg(sb, KERN_ERR,
2899 "can't read group descriptor %d", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 db_count = i;
2901 goto failed_mount2;
2902 }
2903 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002904 if (!ext4_check_descriptors(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002905 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906 goto failed_mount2;
2907 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002908 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2909 if (!ext4_fill_flex_info(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002910 ext4_msg(sb, KERN_ERR,
2911 "unable to initialize "
2912 "flex_bg meta info!");
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002913 goto failed_mount2;
2914 }
2915
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916 sbi->s_gdb_count = db_count;
2917 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2918 spin_lock_init(&sbi->s_next_gen_lock);
2919
Alex Tomasc9de5602008-01-29 00:19:52 -05002920 sbi->s_stripe = ext4_get_stripe_size(sbi);
Theodore Ts'o55138e02009-09-29 13:31:31 -04002921 sbi->s_max_writeback_mb_bump = 128;
Alex Tomasc9de5602008-01-29 00:19:52 -05002922
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002923 /*
2924 * set up enough so that it can read an inode
2925 */
Theodore Ts'o9ca92382009-05-01 12:52:25 -04002926 if (!test_opt(sb, NOLOAD) &&
2927 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2928 sb->s_op = &ext4_sops;
2929 else
2930 sb->s_op = &ext4_nojournal_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07002931 sb->s_export_op = &ext4_export_ops;
2932 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002933#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002934 sb->s_qcop = &ext4_qctl_operations;
2935 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002936#endif
2937 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04002938 mutex_init(&sbi->s_orphan_lock);
Theodore Ts'o32ed5052009-04-25 22:53:39 -04002939 mutex_init(&sbi->s_resize_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002940
2941 sb->s_root = NULL;
2942
2943 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002944 EXT4_HAS_INCOMPAT_FEATURE(sb,
2945 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002946
2947 /*
2948 * The first inode we look at is the journal inode. Don't try
2949 * root first: it may be modified in the journal!
2950 */
2951 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002952 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2953 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002954 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002955 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2956 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002957 ext4_msg(sb, KERN_ERR, "required journal recovery "
2958 "suppressed and not mounted read-only");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002959 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002961 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2962 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2963 sbi->s_journal = NULL;
2964 needs_recovery = 0;
2965 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002966 }
2967
Jose R. Santoseb40a092007-07-18 08:37:25 -04002968 if (ext4_blocks_count(es) > 0xffffffffULL &&
2969 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2970 JBD2_FEATURE_INCOMPAT_64BIT)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002971 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
Jiaying Zhang744692d2010-03-04 16:14:02 -05002972 goto failed_mount_wq;
Jose R. Santoseb40a092007-07-18 08:37:25 -04002973 }
2974
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002975 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2976 jbd2_journal_set_features(sbi->s_journal,
2977 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
Girish Shilamkar818d2762008-01-28 23:58:27 -05002978 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002979 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2980 jbd2_journal_set_features(sbi->s_journal,
2981 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
Girish Shilamkar818d2762008-01-28 23:58:27 -05002982 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2983 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08002984 } else {
2985 jbd2_journal_clear_features(sbi->s_journal,
2986 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2987 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2988 }
Girish Shilamkar818d2762008-01-28 23:58:27 -05002989
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002990 /* We have now updated the journal if required, so we can
2991 * validate the data journaling mode. */
2992 switch (test_opt(sb, DATA_FLAGS)) {
2993 case 0:
2994 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002995 * capabilities: ORDERED_DATA if the journal can
2996 * cope, else JOURNAL_DATA
2997 */
Mingming Caodab291a2006-10-11 01:21:01 -07002998 if (jbd2_journal_check_available_features
2999 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003000 set_opt(sbi->s_mount_opt, ORDERED_DATA);
3001 else
3002 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
3003 break;
3004
Mingming Cao617ba132006-10-11 01:20:53 -07003005 case EXT4_MOUNT_ORDERED_DATA:
3006 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07003007 if (!jbd2_journal_check_available_features
3008 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003009 ext4_msg(sb, KERN_ERR, "Journal does not support "
3010 "requested data journaling mode");
Jiaying Zhang744692d2010-03-04 16:14:02 -05003011 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 }
3013 default:
3014 break;
3015 }
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003016 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003017
Frank Mayhar03901312009-01-07 00:06:22 -05003018no_journal:
Dmitry Monakhov84061e02010-05-16 08:00:00 -04003019 err = percpu_counter_init(&sbi->s_freeblocks_counter,
3020 ext4_count_free_blocks(sb));
3021 if (!err)
3022 err = percpu_counter_init(&sbi->s_freeinodes_counter,
3023 ext4_count_free_inodes(sb));
3024 if (!err)
3025 err = percpu_counter_init(&sbi->s_dirs_counter,
3026 ext4_count_dirs(sb));
3027 if (!err)
3028 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
3029 if (err) {
3030 ext4_msg(sb, KERN_ERR, "insufficient memory");
3031 goto failed_mount_wq;
3032 }
Christoph Hellwig206f7ab2010-06-14 14:42:49 -04003033
Mingming Cao4c0425f2009-09-28 15:48:41 -04003034 EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
3035 if (!EXT4_SB(sb)->dio_unwritten_wq) {
3036 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
3037 goto failed_mount_wq;
3038 }
3039
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003040 /*
Mingming Caodab291a2006-10-11 01:21:01 -07003041 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003042 * so we can safely mount the rest of the filesystem now.
3043 */
3044
David Howells1d1fe1e2008-02-07 00:15:37 -08003045 root = ext4_iget(sb, EXT4_ROOT_INO);
3046 if (IS_ERR(root)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003047 ext4_msg(sb, KERN_ERR, "get root inode failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08003048 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003049 goto failed_mount4;
3050 }
3051 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08003052 iput(root);
Eric Sandeenb31e1552009-06-04 17:36:36 -04003053 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003054 goto failed_mount4;
3055 }
David Howells1d1fe1e2008-02-07 00:15:37 -08003056 sb->s_root = d_alloc_root(root);
3057 if (!sb->s_root) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003058 ext4_msg(sb, KERN_ERR, "get root dentry failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08003059 iput(root);
3060 ret = -ENOMEM;
3061 goto failed_mount4;
3062 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003063
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003064 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04003065
3066 /* determine the minimum size of new large inodes, if present */
3067 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3068 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3069 EXT4_GOOD_OLD_INODE_SIZE;
3070 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3071 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3072 if (sbi->s_want_extra_isize <
3073 le16_to_cpu(es->s_want_extra_isize))
3074 sbi->s_want_extra_isize =
3075 le16_to_cpu(es->s_want_extra_isize);
3076 if (sbi->s_want_extra_isize <
3077 le16_to_cpu(es->s_min_extra_isize))
3078 sbi->s_want_extra_isize =
3079 le16_to_cpu(es->s_min_extra_isize);
3080 }
3081 }
3082 /* Check if enough inode space is available */
3083 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3084 sbi->s_inode_size) {
3085 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3086 EXT4_GOOD_OLD_INODE_SIZE;
Eric Sandeenb31e1552009-06-04 17:36:36 -04003087 ext4_msg(sb, KERN_INFO, "required extra inode space not"
3088 "available");
Kalpak Shahef7f3832007-07-18 09:15:20 -04003089 }
3090
Theodore Ts'o90576c02009-09-29 15:51:30 -04003091 if (test_opt(sb, DELALLOC) &&
3092 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003093 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
3094 "requested data journaling mode");
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04003095 clear_opt(sbi->s_mount_opt, DELALLOC);
Theodore Ts'o90576c02009-09-29 15:51:30 -04003096 }
Jiaying Zhang744692d2010-03-04 16:14:02 -05003097 if (test_opt(sb, DIOREAD_NOLOCK)) {
3098 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3099 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3100 "option - requested data journaling mode");
3101 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
3102 }
3103 if (sb->s_blocksize < PAGE_SIZE) {
3104 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
3105 "option - block size is too small");
3106 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
3107 }
3108 }
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04003109
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003110 err = ext4_setup_system_zone(sb);
3111 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003112 ext4_msg(sb, KERN_ERR, "failed to initialize system "
Curt Wohlgemuthfbe845d2010-05-16 13:00:00 -04003113 "zone (%d)", err);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003114 goto failed_mount4;
3115 }
3116
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04003117 ext4_ext_init(sb);
3118 err = ext4_mb_init(sb, needs_recovery);
3119 if (err) {
Uwe Kleine-König421f91d2010-06-11 12:17:00 +02003120 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
Eric Sandeenb31e1552009-06-04 17:36:36 -04003121 err);
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04003122 goto failed_mount4;
3123 }
3124
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04003125 sbi->s_kobj.kset = ext4_kset;
3126 init_completion(&sbi->s_kobj_unregister);
3127 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
3128 "%s", sb->s_id);
3129 if (err) {
3130 ext4_mb_release(sb);
3131 ext4_ext_release(sb);
3132 goto failed_mount4;
3133 };
3134
Mingming Cao617ba132006-10-11 01:20:53 -07003135 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3136 ext4_orphan_cleanup(sb, es);
3137 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05003138 if (needs_recovery) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003139 ext4_msg(sb, KERN_INFO, "recovery complete");
Frank Mayhar03901312009-01-07 00:06:22 -05003140 ext4_mark_recovery_complete(sb, es);
3141 }
3142 if (EXT4_SB(sb)->s_journal) {
3143 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
3144 descr = " journalled data mode";
3145 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
3146 descr = " ordered data mode";
3147 else
3148 descr = " writeback data mode";
3149 } else
3150 descr = "out journal";
3151
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003152 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
Theodore Ts'o8b67f042010-08-01 23:14:20 -04003153 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
3154 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003155
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003156 init_timer(&sbi->s_err_report);
3157 sbi->s_err_report.function = print_daily_error_info;
3158 sbi->s_err_report.data = (unsigned long) sb;
3159 if (es->s_error_count)
3160 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003161
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003162 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163 return 0;
3164
Mingming Cao617ba132006-10-11 01:20:53 -07003165cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003166 if (!silent)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003167 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003168 goto failed_mount;
3169
3170failed_mount4:
Eric Sandeenb31e1552009-06-04 17:36:36 -04003171 ext4_msg(sb, KERN_ERR, "mount failed");
Mingming Cao4c0425f2009-09-28 15:48:41 -04003172 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
3173failed_mount_wq:
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003174 ext4_release_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003175 if (sbi->s_journal) {
3176 jbd2_journal_destroy(sbi->s_journal);
3177 sbi->s_journal = NULL;
3178 }
Dmitry Monakhov84061e02010-05-16 08:00:00 -04003179 percpu_counter_destroy(&sbi->s_freeblocks_counter);
3180 percpu_counter_destroy(&sbi->s_freeinodes_counter);
3181 percpu_counter_destroy(&sbi->s_dirs_counter);
3182 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183failed_mount3:
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -04003184 if (sbi->s_flex_groups) {
3185 if (is_vmalloc_addr(sbi->s_flex_groups))
3186 vfree(sbi->s_flex_groups);
3187 else
3188 kfree(sbi->s_flex_groups);
3189 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190failed_mount2:
3191 for (i = 0; i < db_count; i++)
3192 brelse(sbi->s_group_desc[i]);
3193 kfree(sbi->s_group_desc);
3194failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04003195 if (sbi->s_proc) {
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003196 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04003197 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003198#ifdef CONFIG_QUOTA
3199 for (i = 0; i < MAXQUOTAS; i++)
3200 kfree(sbi->s_qf_names[i]);
3201#endif
Mingming Cao617ba132006-10-11 01:20:53 -07003202 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003203 brelse(bh);
3204out_fail:
3205 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04003206 kfree(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207 kfree(sbi);
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04003208out_free_orig:
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003209 kfree(orig_data);
David Howells1d1fe1e2008-02-07 00:15:37 -08003210 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211}
3212
3213/*
3214 * Setup any per-fs journal parameters now. We'll do this both on
3215 * initial mount, once the journal has been initialised but before we've
3216 * done any recovery; and again on any subsequent remount.
3217 */
Mingming Cao617ba132006-10-11 01:20:53 -07003218static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219{
Mingming Cao617ba132006-10-11 01:20:53 -07003220 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003221
Theodore Ts'o30773842009-01-03 20:27:38 -05003222 journal->j_commit_interval = sbi->s_commit_interval;
3223 journal->j_min_batch_time = sbi->s_min_batch_time;
3224 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225
Theodore Ts'oa931da62010-08-03 21:35:12 -04003226 write_lock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07003228 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 else
Mingming Caodab291a2006-10-11 01:21:01 -07003230 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04003231 if (test_opt(sb, DATA_ERR_ABORT))
3232 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
3233 else
3234 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04003235 write_unlock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003236}
3237
Mingming Cao617ba132006-10-11 01:20:53 -07003238static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239 unsigned int journal_inum)
3240{
3241 struct inode *journal_inode;
3242 journal_t *journal;
3243
Frank Mayhar03901312009-01-07 00:06:22 -05003244 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3245
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003246 /* First, test for the existence of a valid inode on disk. Bad
3247 * things happen if we iget() an unused inode, as the subsequent
3248 * iput() will try to delete it. */
3249
David Howells1d1fe1e2008-02-07 00:15:37 -08003250 journal_inode = ext4_iget(sb, journal_inum);
3251 if (IS_ERR(journal_inode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003252 ext4_msg(sb, KERN_ERR, "no journal found");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003253 return NULL;
3254 }
3255 if (!journal_inode->i_nlink) {
3256 make_bad_inode(journal_inode);
3257 iput(journal_inode);
Eric Sandeenb31e1552009-06-04 17:36:36 -04003258 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003259 return NULL;
3260 }
3261
Theodore Ts'oe5f8eab2008-09-08 22:25:04 -04003262 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003263 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08003264 if (!S_ISREG(journal_inode->i_mode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003265 ext4_msg(sb, KERN_ERR, "invalid journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266 iput(journal_inode);
3267 return NULL;
3268 }
3269
Mingming Caodab291a2006-10-11 01:21:01 -07003270 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003271 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003272 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003273 iput(journal_inode);
3274 return NULL;
3275 }
3276 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003277 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003278 return journal;
3279}
3280
Mingming Cao617ba132006-10-11 01:20:53 -07003281static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003282 dev_t j_dev)
3283{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003284 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003285 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07003286 ext4_fsblk_t start;
3287 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003288 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07003289 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003291 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 struct block_device *bdev;
3293
Frank Mayhar03901312009-01-07 00:06:22 -05003294 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3295
Eric Sandeenb31e1552009-06-04 17:36:36 -04003296 bdev = ext4_blkdev_get(j_dev, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003297 if (bdev == NULL)
3298 return NULL;
3299
3300 if (bd_claim(bdev, sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003301 ext4_msg(sb, KERN_ERR,
3302 "failed to claim external journal device");
Al Viro9a1c3542008-02-22 20:40:24 -05003303 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003304 return NULL;
3305 }
3306
3307 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04003308 hblock = bdev_logical_block_size(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003309 if (blocksize < hblock) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003310 ext4_msg(sb, KERN_ERR,
3311 "blocksize too small for journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312 goto out_bdev;
3313 }
3314
Mingming Cao617ba132006-10-11 01:20:53 -07003315 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3316 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003317 set_blocksize(bdev, blocksize);
3318 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003319 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3320 "external journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003321 goto out_bdev;
3322 }
3323
Mingming Cao617ba132006-10-11 01:20:53 -07003324 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3325 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003326 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07003327 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003328 ext4_msg(sb, KERN_ERR, "external journal has "
3329 "bad superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003330 brelse(bh);
3331 goto out_bdev;
3332 }
3333
Mingming Cao617ba132006-10-11 01:20:53 -07003334 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003335 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003336 brelse(bh);
3337 goto out_bdev;
3338 }
3339
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003340 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003341 start = sb_block + 1;
3342 brelse(bh); /* we're done with the superblock */
3343
Mingming Caodab291a2006-10-11 01:21:01 -07003344 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345 start, len, blocksize);
3346 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003347 ext4_msg(sb, KERN_ERR, "failed to create device journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003348 goto out_bdev;
3349 }
3350 journal->j_private = sb;
3351 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3352 wait_on_buffer(journal->j_sb_buffer);
3353 if (!buffer_uptodate(journal->j_sb_buffer)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003354 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355 goto out_journal;
3356 }
3357 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003358 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3359 "user (unsupported) - %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003360 be32_to_cpu(journal->j_superblock->s_nr_users));
3361 goto out_journal;
3362 }
Mingming Cao617ba132006-10-11 01:20:53 -07003363 EXT4_SB(sb)->journal_bdev = bdev;
3364 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003365 return journal;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003366
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003367out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07003368 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003369out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07003370 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003371 return NULL;
3372}
3373
Mingming Cao617ba132006-10-11 01:20:53 -07003374static int ext4_load_journal(struct super_block *sb,
3375 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003376 unsigned long journal_devnum)
3377{
3378 journal_t *journal;
3379 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3380 dev_t journal_dev;
3381 int err = 0;
3382 int really_read_only;
3383
Frank Mayhar03901312009-01-07 00:06:22 -05003384 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3385
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003386 if (journal_devnum &&
3387 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003388 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3389 "numbers have changed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003390 journal_dev = new_decode_dev(journal_devnum);
3391 } else
3392 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3393
3394 really_read_only = bdev_read_only(sb->s_bdev);
3395
3396 /*
3397 * Are we loading a blank journal or performing recovery after a
3398 * crash? For recovery, we need to check in advance whether we
3399 * can get read-write access to the device.
3400 */
Mingming Cao617ba132006-10-11 01:20:53 -07003401 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003402 if (sb->s_flags & MS_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003403 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3404 "required on readonly filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003405 if (really_read_only) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003406 ext4_msg(sb, KERN_ERR, "write access "
3407 "unavailable, cannot proceed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003408 return -EROFS;
3409 }
Eric Sandeenb31e1552009-06-04 17:36:36 -04003410 ext4_msg(sb, KERN_INFO, "write access will "
3411 "be enabled during recovery");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003412 }
3413 }
3414
3415 if (journal_inum && journal_dev) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003416 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3417 "and inode journals!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003418 return -EINVAL;
3419 }
3420
3421 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07003422 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003423 return -EINVAL;
3424 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07003425 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426 return -EINVAL;
3427 }
3428
Theodore Ts'o90576c02009-09-29 15:51:30 -04003429 if (!(journal->j_flags & JBD2_BARRIER))
Eric Sandeenb31e1552009-06-04 17:36:36 -04003430 ext4_msg(sb, KERN_INFO, "barriers disabled");
Theodore Ts'o47760042008-09-08 23:00:52 -04003431
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003432 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07003433 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003434 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003435 ext4_msg(sb, KERN_ERR, "error updating journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003436 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003437 return err;
3438 }
3439 }
3440
Mingming Cao617ba132006-10-11 01:20:53 -07003441 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07003442 err = jbd2_journal_wipe(journal, !really_read_only);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04003443 if (!err) {
3444 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
3445 if (save)
3446 memcpy(save, ((char *) es) +
3447 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
Mingming Caodab291a2006-10-11 01:21:01 -07003448 err = jbd2_journal_load(journal);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04003449 if (save)
3450 memcpy(((char *) es) + EXT4_S_ERR_START,
3451 save, EXT4_S_ERR_LEN);
3452 kfree(save);
3453 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003454
3455 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003456 ext4_msg(sb, KERN_ERR, "error loading journal");
Mingming Caodab291a2006-10-11 01:21:01 -07003457 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003458 return err;
3459 }
3460
Mingming Cao617ba132006-10-11 01:20:53 -07003461 EXT4_SB(sb)->s_journal = journal;
3462 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003463
3464 if (journal_devnum &&
3465 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3466 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003467
3468 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003469 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003470 }
3471
3472 return 0;
3473}
3474
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003475static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003476{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003477 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07003478 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08003479 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003480
3481 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08003482 return error;
Theodore Ts'o914258b2008-10-06 21:35:40 -04003483 if (buffer_write_io_error(sbh)) {
3484 /*
3485 * Oh, dear. A previous attempt to write the
3486 * superblock failed. This could happen because the
3487 * USB device was yanked out. Or it could happen to
3488 * be a transient write error and maybe the block will
3489 * be remapped. Nothing we can do but to retry the
3490 * write and hope for the best.
3491 */
Eric Sandeenb31e1552009-06-04 17:36:36 -04003492 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3493 "superblock detected");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003494 clear_buffer_write_io_error(sbh);
3495 set_buffer_uptodate(sbh);
3496 }
Theodore Ts'o71290b32009-09-10 17:31:04 -04003497 /*
3498 * If the file system is mounted read-only, don't update the
3499 * superblock write time. This avoids updating the superblock
3500 * write time when we are mounting the root file system
3501 * read/only but we need to replay the journal; at that point,
3502 * for people who are east of GMT and who make their clock
3503 * tick in localtime for Windows bug-for-bug compatibility,
3504 * the clock is set in the future, and this will cause e2fsck
3505 * to complain and force a full file system check.
3506 */
3507 if (!(sb->s_flags & MS_RDONLY))
3508 es->s_wtime = cpu_to_le32(get_seconds());
Theodore Ts'of613dfc2010-07-27 11:56:08 -04003509 if (sb->s_bdev->bd_part)
3510 es->s_kbytes_written =
3511 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003512 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3513 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Theodore Ts'of613dfc2010-07-27 11:56:08 -04003514 else
3515 es->s_kbytes_written =
3516 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05003517 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3518 &EXT4_SB(sb)->s_freeblocks_counter));
3519 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3520 &EXT4_SB(sb)->s_freeinodes_counter));
Theodore Ts'o7234ab22009-04-30 21:24:04 -04003521 sb->s_dirt = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522 BUFFER_TRACE(sbh, "marking dirty");
3523 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003524 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08003525 error = sync_dirty_buffer(sbh);
3526 if (error)
3527 return error;
3528
3529 error = buffer_write_io_error(sbh);
3530 if (error) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003531 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3532 "superblock");
Theodore Ts'o914258b2008-10-06 21:35:40 -04003533 clear_buffer_write_io_error(sbh);
3534 set_buffer_uptodate(sbh);
3535 }
3536 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08003537 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538}
3539
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003540/*
3541 * Have we just finished recovery? If so, and if we are mounting (or
3542 * remounting) the filesystem readonly, then we will end up with a
3543 * consistent fs on disk. Record that fact.
3544 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003545static void ext4_mark_recovery_complete(struct super_block *sb,
3546 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003547{
Mingming Cao617ba132006-10-11 01:20:53 -07003548 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003549
Frank Mayhar03901312009-01-07 00:06:22 -05003550 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3551 BUG_ON(journal != NULL);
3552 return;
3553 }
Mingming Caodab291a2006-10-11 01:21:01 -07003554 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003555 if (jbd2_journal_flush(journal) < 0)
3556 goto out;
3557
Mingming Cao617ba132006-10-11 01:20:53 -07003558 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003559 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003560 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003561 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003562 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003563
3564out:
Mingming Caodab291a2006-10-11 01:21:01 -07003565 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566}
3567
3568/*
3569 * If we are mounting (or read-write remounting) a filesystem whose journal
3570 * has recorded an error from a previous lifetime, move that error to the
3571 * main filesystem now.
3572 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003573static void ext4_clear_journal_err(struct super_block *sb,
3574 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003575{
3576 journal_t *journal;
3577 int j_errno;
3578 const char *errstr;
3579
Frank Mayhar03901312009-01-07 00:06:22 -05003580 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3581
Mingming Cao617ba132006-10-11 01:20:53 -07003582 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003583
3584 /*
3585 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003586 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003587 */
3588
Mingming Caodab291a2006-10-11 01:21:01 -07003589 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003590 if (j_errno) {
3591 char nbuf[16];
3592
Mingming Cao617ba132006-10-11 01:20:53 -07003593 errstr = ext4_decode_error(sb, j_errno, nbuf);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003594 ext4_warning(sb, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003595 "from previous mount: %s", errstr);
Eric Sandeen12062dd2010-02-15 14:19:27 -05003596 ext4_warning(sb, "Marking fs in need of filesystem check.");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003597
Mingming Cao617ba132006-10-11 01:20:53 -07003598 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3599 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003600 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003601
Mingming Caodab291a2006-10-11 01:21:01 -07003602 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003603 }
3604}
3605
3606/*
3607 * Force the running and committing transactions to commit,
3608 * and wait on the commit.
3609 */
Mingming Cao617ba132006-10-11 01:20:53 -07003610int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003611{
3612 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003613 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003614
3615 if (sb->s_flags & MS_RDONLY)
3616 return 0;
3617
Mingming Cao617ba132006-10-11 01:20:53 -07003618 journal = EXT4_SB(sb)->s_journal;
Eric Sandeen6b0310f2010-05-16 02:00:00 -04003619 if (journal) {
Eric Sandeen437f88c2010-08-01 17:33:29 -04003620 vfs_check_frozen(sb, SB_FREEZE_TRANS);
Frank Mayhar03901312009-01-07 00:06:22 -05003621 ret = ext4_journal_force_commit(journal);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04003622 }
Frank Mayhar03901312009-01-07 00:06:22 -05003623
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003624 return ret;
3625}
3626
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003627static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003628{
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003629 lock_super(sb);
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003630 ext4_commit_super(sb, 1);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +02003631 unlock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003632}
3633
Mingming Cao617ba132006-10-11 01:20:53 -07003634static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003635{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003636 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05003637 tid_t target;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003638 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003639
Theodore Ts'o9bffad12009-06-17 11:48:11 -04003640 trace_ext4_sync_fs(sb, wait);
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003641 flush_workqueue(sbi->dio_unwritten_wq);
3642 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003643 if (wait)
Mingming Cao8d5d02e2009-09-28 15:48:29 -04003644 jbd2_log_wait_commit(sbi->s_journal, target);
Frank Mayhar03901312009-01-07 00:06:22 -05003645 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003646 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003647}
3648
3649/*
3650 * LVM calls this function before a (read-only) snapshot is created. This
3651 * gives us a chance to flush the journal completely and mark the fs clean.
3652 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003653static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003654{
Takashi Satoc4be0c12009-01-09 16:40:58 -08003655 int error = 0;
3656 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003657
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003658 if (sb->s_flags & MS_RDONLY)
3659 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003660
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003661 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003662
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003663 /* Now we set up the journal barrier. */
3664 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003666 /*
3667 * Don't clear the needs_recovery flag if we failed to flush
3668 * the journal.
3669 */
3670 error = jbd2_journal_flush(journal);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04003671 if (error < 0)
3672 goto out;
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003673
3674 /* Journal blocked and flushed, clear needs_recovery flag. */
3675 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3676 error = ext4_commit_super(sb, 1);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04003677out:
3678 /* we rely on s_frozen to stop further updates */
3679 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3680 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003681}
3682
3683/*
3684 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3685 * flag here, even though the filesystem is not technically dirty yet.
3686 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08003687static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688{
Theodore Ts'o9ca92382009-05-01 12:52:25 -04003689 if (sb->s_flags & MS_RDONLY)
3690 return 0;
3691
3692 lock_super(sb);
3693 /* Reset the needs_recovery flag before the fs is unlocked. */
3694 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3695 ext4_commit_super(sb, 1);
3696 unlock_super(sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -08003697 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003698}
3699
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003700static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003701{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003702 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003703 struct ext4_sb_info *sbi = EXT4_SB(sb);
3704 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003705 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003706 struct ext4_mount_options old_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04003707 int enable_quota = 0;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003708 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003709 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003710 int err;
3711#ifdef CONFIG_QUOTA
3712 int i;
3713#endif
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003714 char *orig_data = kstrdup(data, GFP_KERNEL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003715
3716 /* Store the original options */
Al Virobbd68512009-05-06 10:43:07 -04003717 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003718 old_sb_flags = sb->s_flags;
3719 old_opts.s_mount_opt = sbi->s_mount_opt;
3720 old_opts.s_resuid = sbi->s_resuid;
3721 old_opts.s_resgid = sbi->s_resgid;
3722 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003723 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3724 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003725#ifdef CONFIG_QUOTA
3726 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3727 for (i = 0; i < MAXQUOTAS; i++)
3728 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3729#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003730 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3731 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003732
3733 /*
3734 * Allow the "check" option to be passed as a remount option.
3735 */
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003736 if (!parse_options(data, sb, NULL, &journal_ioprio,
3737 &n_blocks_count, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003738 err = -EINVAL;
3739 goto restore_opts;
3740 }
3741
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003742 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'oc67d8592010-06-29 11:07:07 -04003743 ext4_abort(sb, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003744
3745 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhov482a7422010-02-24 11:35:32 -05003746 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003747
3748 es = sbi->s_es;
3749
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003750 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05003751 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003752 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3753 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003754
3755 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003756 n_blocks_count > ext4_blocks_count(es)) {
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04003757 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003758 err = -EROFS;
3759 goto restore_opts;
3760 }
3761
3762 if (*flags & MS_RDONLY) {
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04003763 err = dquot_suspend(sb, -1);
3764 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04003765 goto restore_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04003766
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003767 /*
3768 * First of all, the unconditional stuff we have to do
3769 * to disable replay of the journal when we next remount
3770 */
3771 sb->s_flags |= MS_RDONLY;
3772
3773 /*
3774 * OK, test if we are remounting a valid rw partition
3775 * readonly, and if so set the rdonly flag and then
3776 * mark the partition as valid again.
3777 */
Mingming Cao617ba132006-10-11 01:20:53 -07003778 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3779 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003780 es->s_state = cpu_to_le16(sbi->s_mount_state);
3781
Theodore Ts'oa63c9eb2009-05-01 01:59:42 -04003782 if (sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05003783 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003784 } else {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003785 /* Make sure we can mount this feature set readwrite */
3786 if (!ext4_feature_set_ok(sb, 0)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003787 err = -EROFS;
3788 goto restore_opts;
3789 }
Eric Sandeenead65962007-02-10 01:46:08 -08003790 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003791 * Make sure the group descriptor checksums
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003792 * are sane. If they aren't, refuse to remount r/w.
Theodore Ts'o8a266462008-07-26 14:34:21 -04003793 */
3794 for (g = 0; g < sbi->s_groups_count; g++) {
3795 struct ext4_group_desc *gdp =
3796 ext4_get_group_desc(sb, g, NULL);
3797
3798 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003799 ext4_msg(sb, KERN_ERR,
3800 "ext4_remount: Checksum for group %u failed (%u!=%u)",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003801 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3802 le16_to_cpu(gdp->bg_checksum));
3803 err = -EINVAL;
3804 goto restore_opts;
3805 }
3806 }
3807
3808 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003809 * If we have an unprocessed orphan list hanging
3810 * around from a previously readonly bdev mount,
3811 * require a full umount/remount for now.
3812 */
3813 if (es->s_last_orphan) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003814 ext4_msg(sb, KERN_WARNING, "Couldn't "
Eric Sandeenead65962007-02-10 01:46:08 -08003815 "remount RDWR because of unprocessed "
3816 "orphan inode list. Please "
Eric Sandeenb31e1552009-06-04 17:36:36 -04003817 "umount/remount instead");
Eric Sandeenead65962007-02-10 01:46:08 -08003818 err = -EINVAL;
3819 goto restore_opts;
3820 }
3821
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003822 /*
3823 * Mounting a RDONLY partition read-write, so reread
3824 * and store the current valid flag. (It may have
3825 * been changed by e2fsck since we originally mounted
3826 * the partition.)
3827 */
Frank Mayhar03901312009-01-07 00:06:22 -05003828 if (sbi->s_journal)
3829 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003830 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003831 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003832 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003833 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 sb->s_flags &= ~MS_RDONLY;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04003835 enable_quota = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003836 }
3837 }
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04003838 ext4_setup_system_zone(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05003839 if (sbi->s_journal == NULL)
Theodore Ts'oe2d67052009-05-01 00:33:44 -04003840 ext4_commit_super(sb, 1);
Frank Mayhar03901312009-01-07 00:06:22 -05003841
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003842#ifdef CONFIG_QUOTA
3843 /* Release old quota file names */
3844 for (i = 0; i < MAXQUOTAS; i++)
3845 if (old_opts.s_qf_names[i] &&
3846 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3847 kfree(old_opts.s_qf_names[i]);
3848#endif
Al Virobbd68512009-05-06 10:43:07 -04003849 unlock_super(sb);
Christoph Hellwigc79d9672010-05-19 07:16:40 -04003850 if (enable_quota)
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04003851 dquot_resume(sb, -1);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003852
3853 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
3854 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003855 return 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003856
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003857restore_opts:
3858 sb->s_flags = old_sb_flags;
3859 sbi->s_mount_opt = old_opts.s_mount_opt;
3860 sbi->s_resuid = old_opts.s_resuid;
3861 sbi->s_resgid = old_opts.s_resgid;
3862 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003863 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3864 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003865#ifdef CONFIG_QUOTA
3866 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3867 for (i = 0; i < MAXQUOTAS; i++) {
3868 if (sbi->s_qf_names[i] &&
3869 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3870 kfree(sbi->s_qf_names[i]);
3871 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3872 }
3873#endif
Al Virobbd68512009-05-06 10:43:07 -04003874 unlock_super(sb);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003875 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003876 return err;
3877}
3878
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003879static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003880{
3881 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003882 struct ext4_sb_info *sbi = EXT4_SB(sb);
3883 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003884 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003885
Badari Pulavarty5e700302007-07-15 23:42:00 -07003886 if (test_opt(sb, MINIX_DF)) {
3887 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003888 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Theodore Ts'o8df96752009-05-01 08:50:38 -04003889 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003890 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003891
3892 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003893 * Compute the overhead (FS structures). This is constant
3894 * for a given filesystem unless the number of block groups
3895 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003896 */
3897
3898 /*
3899 * All of the blocks before first_data_block are
3900 * overhead
3901 */
3902 overhead = le32_to_cpu(es->s_first_data_block);
3903
3904 /*
3905 * Add the overhead attributed to the superblock and
3906 * block group descriptors. If the sparse superblocks
3907 * feature is turned on, then not all groups have this.
3908 */
3909 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003910 overhead += ext4_bg_has_super(sb, i) +
3911 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003912 cond_resched();
3913 }
3914
3915 /*
3916 * Every block group has an inode bitmap, a block
3917 * bitmap, and an inode table.
3918 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003919 overhead += ngroups * (2 + sbi->s_itb_per_group);
3920 sbi->s_overhead_last = overhead;
3921 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003922 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003923 }
3924
Mingming Cao617ba132006-10-11 01:20:53 -07003925 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003926 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003927 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003928 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3929 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003930 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3931 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003932 buf->f_bavail = 0;
3933 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003934 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07003935 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003936 fsid = le64_to_cpup((void *)es->s_uuid) ^
3937 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3938 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3939 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003940
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003941 return 0;
3942}
3943
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003944/* Helper function for writing quotas on sync - we need to start transaction
3945 * before quota file is locked for write. Otherwise the are possible deadlocks:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003946 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003947 * ext4_create() quota_sync()
Jan Karaa269eb12009-01-26 17:04:39 +01003948 * jbd2_journal_start() write_dquot()
Christoph Hellwig871a2932010-03-03 09:05:07 -05003949 * dquot_initialize() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003950 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003951 *
3952 */
3953
3954#ifdef CONFIG_QUOTA
3955
3956static inline struct inode *dquot_to_inode(struct dquot *dquot)
3957{
3958 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3959}
3960
Mingming Cao617ba132006-10-11 01:20:53 -07003961static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003962{
3963 int ret, err;
3964 handle_t *handle;
3965 struct inode *inode;
3966
3967 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003968 handle = ext4_journal_start(inode,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003969 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003970 if (IS_ERR(handle))
3971 return PTR_ERR(handle);
3972 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003973 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003974 if (!ret)
3975 ret = err;
3976 return ret;
3977}
3978
Mingming Cao617ba132006-10-11 01:20:53 -07003979static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003980{
3981 int ret, err;
3982 handle_t *handle;
3983
Mingming Cao617ba132006-10-11 01:20:53 -07003984 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003985 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003986 if (IS_ERR(handle))
3987 return PTR_ERR(handle);
3988 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003989 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003990 if (!ret)
3991 ret = err;
3992 return ret;
3993}
3994
Mingming Cao617ba132006-10-11 01:20:53 -07003995static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003996{
3997 int ret, err;
3998 handle_t *handle;
3999
Mingming Cao617ba132006-10-11 01:20:53 -07004000 handle = ext4_journal_start(dquot_to_inode(dquot),
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004001 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07004002 if (IS_ERR(handle)) {
4003 /* Release dquot anyway to avoid endless cycle in dqput() */
4004 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004005 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07004006 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004007 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07004008 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004009 if (!ret)
4010 ret = err;
4011 return ret;
4012}
4013
Mingming Cao617ba132006-10-11 01:20:53 -07004014static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004015{
Jan Kara2c8be6b2008-05-13 21:27:55 -04004016 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07004017 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4018 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004019 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07004020 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004021 } else {
4022 return dquot_mark_dquot_dirty(dquot);
4023 }
4024}
4025
Mingming Cao617ba132006-10-11 01:20:53 -07004026static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004027{
4028 int ret, err;
4029 handle_t *handle;
4030
4031 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07004032 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004033 if (IS_ERR(handle))
4034 return PTR_ERR(handle);
4035 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07004036 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004037 if (!ret)
4038 ret = err;
4039 return ret;
4040}
4041
4042/*
4043 * Turn on quotas during mount time - we need to find
4044 * the quota file and such...
4045 */
Mingming Cao617ba132006-10-11 01:20:53 -07004046static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004047{
Christoph Hellwig287a8092010-05-19 07:16:45 -04004048 return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4049 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004050}
4051
4052/*
4053 * Standard function to be called on quota_on
4054 */
Mingming Cao617ba132006-10-11 01:20:53 -07004055static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Christoph Hellwig307ae182010-05-19 07:16:43 -04004056 char *name)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004057{
4058 int err;
Al Viro82646132008-08-02 00:57:06 -04004059 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004060
4061 if (!test_opt(sb, QUOTA))
4062 return -EINVAL;
Jan Kara06235432008-05-13 19:11:51 -04004063
Al Viro82646132008-08-02 00:57:06 -04004064 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004065 if (err)
4066 return err;
Jan Kara06235432008-05-13 19:11:51 -04004067
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004068 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04004069 if (path.mnt->mnt_sb != sb) {
4070 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004071 return -EXDEV;
4072 }
Jan Kara06235432008-05-13 19:11:51 -04004073 /* Journaling quota? */
4074 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004075 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04004076 if (path.dentry->d_parent != sb->s_root)
Eric Sandeenb31e1552009-06-04 17:36:36 -04004077 ext4_msg(sb, KERN_WARNING,
4078 "Quota file not on filesystem root. "
4079 "Journaled quota will not work");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004080 }
Jan Kara06235432008-05-13 19:11:51 -04004081
4082 /*
4083 * When we journal data on quota file, we have to flush journal to see
4084 * all updates to the file when we bypass pagecache...
4085 */
Frank Mayhar03901312009-01-07 00:06:22 -05004086 if (EXT4_SB(sb)->s_journal &&
4087 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04004088 /*
4089 * We don't need to lock updates but journal_flush() could
4090 * otherwise be livelocked...
4091 */
4092 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04004093 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04004094 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04004095 if (err) {
Al Viro82646132008-08-02 00:57:06 -04004096 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04004097 return err;
4098 }
Jan Kara06235432008-05-13 19:11:51 -04004099 }
4100
Christoph Hellwig287a8092010-05-19 07:16:45 -04004101 err = dquot_quota_on_path(sb, type, format_id, &path);
Al Viro82646132008-08-02 00:57:06 -04004102 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04004103 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004104}
4105
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04004106static int ext4_quota_off(struct super_block *sb, int type)
4107{
4108 /* Force all delayed allocation blocks to be allocated */
4109 if (test_opt(sb, DELALLOC)) {
4110 down_read(&sb->s_umount);
4111 sync_filesystem(sb);
4112 up_read(&sb->s_umount);
4113 }
4114
4115 return dquot_quota_off(sb, type);
4116}
4117
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004118/* Read data from quotafile - avoid pagecache and such because we cannot afford
4119 * acquiring the locks... As quota files are never truncated and quota code
4120 * itself serializes the operations (and noone else should touch the files)
4121 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07004122static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004123 size_t len, loff_t off)
4124{
4125 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004126 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004127 int err = 0;
4128 int offset = off & (sb->s_blocksize - 1);
4129 int tocopy;
4130 size_t toread;
4131 struct buffer_head *bh;
4132 loff_t i_size = i_size_read(inode);
4133
4134 if (off > i_size)
4135 return 0;
4136 if (off+len > i_size)
4137 len = i_size-off;
4138 toread = len;
4139 while (toread > 0) {
4140 tocopy = sb->s_blocksize - offset < toread ?
4141 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07004142 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004143 if (err)
4144 return err;
4145 if (!bh) /* A hole? */
4146 memset(data, 0, tocopy);
4147 else
4148 memcpy(data, bh->b_data+offset, tocopy);
4149 brelse(bh);
4150 offset = 0;
4151 toread -= tocopy;
4152 data += tocopy;
4153 blk++;
4154 }
4155 return len;
4156}
4157
4158/* Write to quotafile (we know the transaction is already started and has
4159 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07004160static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004161 const char *data, size_t len, loff_t off)
4162{
4163 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05004164 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004165 int err = 0;
4166 int offset = off & (sb->s_blocksize - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004167 struct buffer_head *bh;
4168 handle_t *handle = journal_current_handle();
4169
Frank Mayhar03901312009-01-07 00:06:22 -05004170 if (EXT4_SB(sb)->s_journal && !handle) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004171 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4172 " cancelled because transaction is not started",
Jan Kara9c3013e2007-09-11 15:23:29 -07004173 (unsigned long long)off, (unsigned long long)len);
4174 return -EIO;
4175 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004176 /*
4177 * Since we account only one data block in transaction credits,
4178 * then it is impossible to cross a block boundary.
4179 */
4180 if (sb->s_blocksize - offset < len) {
4181 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4182 " cancelled because not block aligned",
4183 (unsigned long long)off, (unsigned long long)len);
4184 return -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004185 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004186
4187 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
4188 bh = ext4_bread(handle, inode, blk, 1, &err);
4189 if (!bh)
4190 goto out;
Jan Kara62d2b5f2010-07-27 11:56:07 -04004191 err = ext4_journal_get_write_access(handle, bh);
4192 if (err) {
4193 brelse(bh);
4194 goto out;
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004195 }
4196 lock_buffer(bh);
4197 memcpy(bh->b_data+offset, data, len);
4198 flush_dcache_page(bh->b_page);
4199 unlock_buffer(bh);
Jan Kara62d2b5f2010-07-27 11:56:07 -04004200 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004201 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004202out:
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004203 if (err) {
Jan Kara4d04e4f2008-07-04 09:59:34 -07004204 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004205 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07004206 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004207 if (inode->i_size < off + len) {
4208 i_size_write(inode, off + len);
Mingming Cao617ba132006-10-11 01:20:53 -07004209 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004210 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004211 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07004212 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004213 mutex_unlock(&inode->i_mutex);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05004214 return len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004215}
4216
4217#endif
4218
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004219static int ext4_get_sb(struct file_system_type *fs_type, int flags,
4220 const char *dev_name, void *data, struct vfsmount *mnt)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004221{
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004222 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004223}
4224
Theodore Ts'o37f328e2010-03-24 20:06:41 -04004225#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004226static struct file_system_type ext2_fs_type = {
4227 .owner = THIS_MODULE,
4228 .name = "ext2",
4229 .get_sb = ext4_get_sb,
4230 .kill_sb = kill_block_super,
4231 .fs_flags = FS_REQUIRES_DEV,
4232};
4233
4234static inline void register_as_ext2(void)
4235{
4236 int err = register_filesystem(&ext2_fs_type);
4237 if (err)
4238 printk(KERN_WARNING
4239 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
4240}
4241
4242static inline void unregister_as_ext2(void)
4243{
4244 unregister_filesystem(&ext2_fs_type);
4245}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004246MODULE_ALIAS("ext2");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004247#else
4248static inline void register_as_ext2(void) { }
4249static inline void unregister_as_ext2(void) { }
4250#endif
4251
Theodore Ts'o37f328e2010-03-24 20:06:41 -04004252#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
Theodore Ts'o24b58422009-12-07 14:08:51 -05004253static inline void register_as_ext3(void)
4254{
4255 int err = register_filesystem(&ext3_fs_type);
4256 if (err)
4257 printk(KERN_WARNING
4258 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
4259}
4260
4261static inline void unregister_as_ext3(void)
4262{
4263 unregister_filesystem(&ext3_fs_type);
4264}
Theodore Ts'o51b7e3c2009-12-21 10:56:09 -05004265MODULE_ALIAS("ext3");
Theodore Ts'o24b58422009-12-07 14:08:51 -05004266#else
4267static inline void register_as_ext3(void) { }
4268static inline void unregister_as_ext3(void) { }
4269#endif
4270
Theodore Ts'o03010a32008-10-10 20:02:48 -04004271static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004272 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04004273 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07004274 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004275 .kill_sb = kill_block_super,
4276 .fs_flags = FS_REQUIRES_DEV,
4277};
4278
Mingming Cao617ba132006-10-11 01:20:53 -07004279static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004280{
Alex Tomasc9de5602008-01-29 00:19:52 -05004281 int err;
4282
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04004283 ext4_check_flag_values();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004284 err = init_ext4_system_zone();
4285 if (err)
4286 return err;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004287 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4288 if (!ext4_kset)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004289 goto out4;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004290 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05004291 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004292 if (err)
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004293 goto out3;
Alex Tomasc9de5602008-01-29 00:19:52 -05004294
4295 err = init_ext4_xattr();
4296 if (err)
4297 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004298 err = init_inodecache();
4299 if (err)
4300 goto out1;
Theodore Ts'o24b58422009-12-07 14:08:51 -05004301 register_as_ext2();
4302 register_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004303 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004304 if (err)
4305 goto out;
4306 return 0;
4307out:
Theodore Ts'o24b58422009-12-07 14:08:51 -05004308 unregister_as_ext2();
4309 unregister_as_ext3();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004310 destroy_inodecache();
4311out1:
Mingming Cao617ba132006-10-11 01:20:53 -07004312 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004313out2:
4314 exit_ext4_mballoc();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004315out3:
4316 remove_proc_entry("fs/ext4", NULL);
4317 kset_unregister(ext4_kset);
4318out4:
4319 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004320 return err;
4321}
4322
Mingming Cao617ba132006-10-11 01:20:53 -07004323static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004324{
Theodore Ts'o24b58422009-12-07 14:08:51 -05004325 unregister_as_ext2();
4326 unregister_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04004327 unregister_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07004329 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05004330 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004331 remove_proc_entry("fs/ext4", NULL);
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004332 kset_unregister(ext4_kset);
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04004333 exit_ext4_system_zone();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004334}
4335
4336MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05004337MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004338MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07004339module_init(init_ext4_fs)
4340module_exit(exit_ext4_fs)