blob: a3321bf22311d8126bc0dd2de334eb2cce0c5b0f [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>
Mingming Caodab291a2006-10-11 01:21:01 -070023#include <linux/jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070024#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/blkdev.h>
27#include <linux/parser.h>
28#include <linux/smp_lock.h>
29#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'oede86cc2008-10-05 20:50:06 -040038#include <linux/marker.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"
47#include "namei.h"
Andreas Dilger717d50e2007-10-16 18:38:25 -040048#include "group.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070049
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040050struct proc_dir_entry *ext4_proc_root;
51
Mingming Cao617ba132006-10-11 01:20:53 -070052static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070054static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070055 unsigned int);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040056static void ext4_commit_super(struct super_block *sb,
57 struct ext4_super_block *es, int sync);
58static 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);
Mingming Cao617ba132006-10-11 01:20:53 -070067static void ext4_unlockfs(struct super_block *sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040068static void ext4_write_super(struct super_block *sb);
Mingming Cao617ba132006-10-11 01:20:53 -070069static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070070
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070071
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070072ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
73 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070074{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040075 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070076 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040077 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070078}
79
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070080ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
81 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070082{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040083 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070084 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040085 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070086}
87
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070088ext4_fsblk_t ext4_inode_table(struct super_block *sb,
89 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070090{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040091 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070092 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -040093 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070094}
95
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -050096__u32 ext4_free_blks_count(struct super_block *sb,
97 struct ext4_group_desc *bg)
98{
99 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
100 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
101 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
102}
103
104__u32 ext4_free_inodes_count(struct super_block *sb,
105 struct ext4_group_desc *bg)
106{
107 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
108 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
109 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
110}
111
112__u32 ext4_used_dirs_count(struct super_block *sb,
113 struct ext4_group_desc *bg)
114{
115 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
116 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
117 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
118}
119
120__u32 ext4_itable_unused_count(struct super_block *sb,
121 struct ext4_group_desc *bg)
122{
123 return le16_to_cpu(bg->bg_itable_unused_lo) |
124 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
125 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
126}
127
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700128void ext4_block_bitmap_set(struct super_block *sb,
129 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700130{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400131 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700132 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
133 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700134}
135
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700136void ext4_inode_bitmap_set(struct super_block *sb,
137 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700138{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400139 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700140 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
141 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700142}
143
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700144void ext4_inode_table_set(struct super_block *sb,
145 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700146{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400147 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700148 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
149 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700150}
151
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500152void ext4_free_blks_set(struct super_block *sb,
153 struct ext4_group_desc *bg, __u32 count)
154{
155 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
156 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
157 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
158}
159
160void ext4_free_inodes_set(struct super_block *sb,
161 struct ext4_group_desc *bg, __u32 count)
162{
163 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
164 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
165 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
166}
167
168void ext4_used_dirs_set(struct super_block *sb,
169 struct ext4_group_desc *bg, __u32 count)
170{
171 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
172 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
173 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
174}
175
176void ext4_itable_unused_set(struct super_block *sb,
177 struct ext4_group_desc *bg, __u32 count)
178{
179 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
180 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
181 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
182}
183
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700184/*
Mingming Caodab291a2006-10-11 01:21:01 -0700185 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700186 *
187 * The only special thing we need to do here is to make sure that all
188 * journal_end calls result in the superblock being marked dirty, so
189 * that sync() will call the filesystem's write_super callback if
190 * appropriate.
191 */
Mingming Cao617ba132006-10-11 01:20:53 -0700192handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700193{
194 journal_t *journal;
195
196 if (sb->s_flags & MS_RDONLY)
197 return ERR_PTR(-EROFS);
198
199 /* Special case here: if the journal has aborted behind our
200 * backs (eg. EIO in the commit thread), then we still need to
201 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700202 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -0500203 if (journal) {
204 if (is_journal_aborted(journal)) {
205 ext4_abort(sb, __func__,
206 "Detected aborted journal");
207 return ERR_PTR(-EROFS);
208 }
209 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210 }
Frank Mayhar03901312009-01-07 00:06:22 -0500211 /*
212 * We're not journaling, return the appropriate indication.
213 */
214 current->journal_info = EXT4_NOJOURNAL_HANDLE;
215 return current->journal_info;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700216}
217
218/*
219 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700220 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221 * that sync() will call the filesystem's write_super callback if
222 * appropriate.
223 */
Mingming Cao617ba132006-10-11 01:20:53 -0700224int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700225{
226 struct super_block *sb;
227 int err;
228 int rc;
229
Frank Mayhar03901312009-01-07 00:06:22 -0500230 if (!ext4_handle_valid(handle)) {
231 /*
232 * Do this here since we don't call jbd2_journal_stop() in
233 * no-journal mode.
234 */
235 current->journal_info = NULL;
236 return 0;
237 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700238 sb = handle->h_transaction->t_journal->j_private;
239 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700240 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241
242 if (!err)
243 err = rc;
244 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700245 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246 return err;
247}
248
Mingming Cao617ba132006-10-11 01:20:53 -0700249void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700250 struct buffer_head *bh, handle_t *handle, int err)
251{
252 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700253 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700254
Frank Mayhar03901312009-01-07 00:06:22 -0500255 BUG_ON(!ext4_handle_valid(handle));
256
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700257 if (bh)
258 BUFFER_TRACE(bh, "abort");
259
260 if (!handle->h_err)
261 handle->h_err = err;
262
263 if (is_handle_aborted(handle))
264 return;
265
266 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
267 caller, errstr, err_fn);
268
Mingming Caodab291a2006-10-11 01:21:01 -0700269 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700270}
271
272/* Deal with the reporting of failure conditions on a filesystem such as
273 * inconsistencies detected or read IO failures.
274 *
275 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700276 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700277 * write ordering constraints on the superblock which prevent us from
278 * writing it out straight away; and given that the journal is about to
279 * be aborted, we can't rely on the current, or future, transactions to
280 * write out the superblock safely.
281 *
Mingming Caodab291a2006-10-11 01:21:01 -0700282 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700283 * the journal instead. On recovery, the journal will compain about
284 * that error until we've noted it down and cleared it.
285 */
286
Mingming Cao617ba132006-10-11 01:20:53 -0700287static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288{
Mingming Cao617ba132006-10-11 01:20:53 -0700289 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700290
Mingming Cao617ba132006-10-11 01:20:53 -0700291 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
292 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700293
294 if (sb->s_flags & MS_RDONLY)
295 return;
296
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400297 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700298 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299
Mingming Cao617ba132006-10-11 01:20:53 -0700300 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700302 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700303 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400304 if (test_opt(sb, ERRORS_RO)) {
305 printk(KERN_CRIT "Remounting filesystem read-only\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700306 sb->s_flags |= MS_RDONLY;
307 }
Mingming Cao617ba132006-10-11 01:20:53 -0700308 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700309 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700310 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311 sb->s_id);
312}
313
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400314void ext4_error(struct super_block *sb, const char *function,
315 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700316{
317 va_list args;
318
319 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400320 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700321 vprintk(fmt, args);
322 printk("\n");
323 va_end(args);
324
Mingming Cao617ba132006-10-11 01:20:53 -0700325 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700326}
327
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400328static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 char nbuf[16])
330{
331 char *errstr = NULL;
332
333 switch (errno) {
334 case -EIO:
335 errstr = "IO failure";
336 break;
337 case -ENOMEM:
338 errstr = "Out of memory";
339 break;
340 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700341 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342 errstr = "Journal has aborted";
343 else
344 errstr = "Readonly filesystem";
345 break;
346 default:
347 /* If the caller passed in an extra buffer for unknown
348 * errors, textualise them now. Else we just return
349 * NULL. */
350 if (nbuf) {
351 /* Check for truncated error codes... */
352 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
353 errstr = nbuf;
354 }
355 break;
356 }
357
358 return errstr;
359}
360
Mingming Cao617ba132006-10-11 01:20:53 -0700361/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700362 * automatically and invokes the appropriate error response. */
363
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400364void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700365{
366 char nbuf[16];
367 const char *errstr;
368
369 /* Special case: if the error is EROFS, and we're not already
370 * inside a transaction, then there's really no point in logging
371 * an error. */
372 if (errno == -EROFS && journal_current_handle() == NULL &&
373 (sb->s_flags & MS_RDONLY))
374 return;
375
Mingming Cao617ba132006-10-11 01:20:53 -0700376 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400377 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
378 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700379
Mingming Cao617ba132006-10-11 01:20:53 -0700380 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700381}
382
383/*
Mingming Cao617ba132006-10-11 01:20:53 -0700384 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700385 * abort function may be used to deal with unrecoverable failures such
386 * as journal IO errors or ENOMEM at a critical moment in log management.
387 *
388 * We unconditionally force the filesystem into an ABORT|READONLY state,
389 * unless the error response on the fs has been set to panic in which
390 * case we take the easy way out and panic immediately.
391 */
392
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400393void ext4_abort(struct super_block *sb, const char *function,
394 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700395{
396 va_list args;
397
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400398 printk(KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700399
400 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400401 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700402 vprintk(fmt, args);
403 printk("\n");
404 va_end(args);
405
406 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700407 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700408
409 if (sb->s_flags & MS_RDONLY)
410 return;
411
412 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700413 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700415 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400416 if (EXT4_SB(sb)->s_journal)
417 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700418}
419
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400420void ext4_warning(struct super_block *sb, const char *function,
421 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422{
423 va_list args;
424
425 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700426 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700427 sb->s_id, function);
428 vprintk(fmt, args);
429 printk("\n");
430 va_end(args);
431}
432
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500433void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
434 const char *function, const char *fmt, ...)
435__releases(bitlock)
436__acquires(bitlock)
437{
438 va_list args;
439 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
440
441 va_start(args, fmt);
442 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
443 vprintk(fmt, args);
444 printk("\n");
445 va_end(args);
446
447 if (test_opt(sb, ERRORS_CONT)) {
448 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
449 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
450 ext4_commit_super(sb, es, 0);
451 return;
452 }
453 ext4_unlock_group(sb, grp);
454 ext4_handle_error(sb);
455 /*
456 * We only get here in the ERRORS_RO case; relocking the group
457 * may be dangerous, but nothing bad will happen since the
458 * filesystem will have already been marked read/only and the
459 * journal has been aborted. We return 1 as a hint to callers
460 * who might what to use the return value from
461 * ext4_grp_locked_error() to distinguish beween the
462 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
463 * aggressively from the ext4 function in question, with a
464 * more appropriate error code.
465 */
466 ext4_lock_group(sb, grp);
467 return;
468}
469
470
Mingming Cao617ba132006-10-11 01:20:53 -0700471void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700472{
Mingming Cao617ba132006-10-11 01:20:53 -0700473 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474
Mingming Cao617ba132006-10-11 01:20:53 -0700475 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700476 return;
477
Harvey Harrison46e665e2008-04-17 10:38:59 -0400478 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700479 "updating to rev %d because of new feature flag, "
480 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700481 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700482
Mingming Cao617ba132006-10-11 01:20:53 -0700483 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
484 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
485 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700486 /* leave es->s_feature_*compat flags alone */
487 /* es->s_uuid will be set by e2fsck if empty */
488
489 /*
490 * The rest of the superblock fields should be zero, and if not it
491 * means they are likely already in use, so leave them alone. We
492 * can leave it up to e2fsck to clean up any inconsistencies there.
493 */
494}
495
496/*
497 * Open the external journal device
498 */
Mingming Cao617ba132006-10-11 01:20:53 -0700499static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500{
501 struct block_device *bdev;
502 char b[BDEVNAME_SIZE];
503
504 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
505 if (IS_ERR(bdev))
506 goto fail;
507 return bdev;
508
509fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700510 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700511 __bdevname(dev, b), PTR_ERR(bdev));
512 return NULL;
513}
514
515/*
516 * Release the journal device
517 */
Mingming Cao617ba132006-10-11 01:20:53 -0700518static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519{
520 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500521 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522}
523
Mingming Cao617ba132006-10-11 01:20:53 -0700524static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700525{
526 struct block_device *bdev;
527 int ret = -ENODEV;
528
529 bdev = sbi->journal_bdev;
530 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700531 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 sbi->journal_bdev = NULL;
533 }
534 return ret;
535}
536
537static inline struct inode *orphan_list_entry(struct list_head *l)
538{
Mingming Cao617ba132006-10-11 01:20:53 -0700539 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540}
541
Mingming Cao617ba132006-10-11 01:20:53 -0700542static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700543{
544 struct list_head *l;
545
546 printk(KERN_ERR "sb orphan head is %d\n",
547 le32_to_cpu(sbi->s_es->s_last_orphan));
548
549 printk(KERN_ERR "sb_info orphan list:\n");
550 list_for_each(l, &sbi->s_orphan) {
551 struct inode *inode = orphan_list_entry(l);
552 printk(KERN_ERR " "
553 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
554 inode->i_sb->s_id, inode->i_ino, inode,
555 inode->i_mode, inode->i_nlink,
556 NEXT_ORPHAN(inode));
557 }
558}
559
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400560static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561{
Mingming Cao617ba132006-10-11 01:20:53 -0700562 struct ext4_sb_info *sbi = EXT4_SB(sb);
563 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400564 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700565
Alex Tomasc9de5602008-01-29 00:19:52 -0500566 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700567 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700568 ext4_xattr_put_super(sb);
Frank Mayhar03901312009-01-07 00:06:22 -0500569 if (sbi->s_journal) {
570 err = jbd2_journal_destroy(sbi->s_journal);
571 sbi->s_journal = NULL;
572 if (err < 0)
573 ext4_abort(sb, __func__,
574 "Couldn't clean up the journal");
575 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700577 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 es->s_state = cpu_to_le16(sbi->s_mount_state);
Mingming Cao617ba132006-10-11 01:20:53 -0700579 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700580 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400581 if (sbi->s_proc) {
582 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400583 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400584 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585
586 for (i = 0; i < sbi->s_gdb_count; i++)
587 brelse(sbi->s_group_desc[i]);
588 kfree(sbi->s_group_desc);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400589 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700590 percpu_counter_destroy(&sbi->s_freeblocks_counter);
591 percpu_counter_destroy(&sbi->s_freeinodes_counter);
592 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400593 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700594 brelse(sbi->s_sbh);
595#ifdef CONFIG_QUOTA
596 for (i = 0; i < MAXQUOTAS; i++)
597 kfree(sbi->s_qf_names[i]);
598#endif
599
600 /* Debugging code just in case the in-memory inode orphan list
601 * isn't empty. The on-disk one can be non-empty if we've
602 * detected an error and taken the fs readonly, but the
603 * in-memory list had better be clean by this point. */
604 if (!list_empty(&sbi->s_orphan))
605 dump_orphan_list(sb, sbi);
606 J_ASSERT(list_empty(&sbi->s_orphan));
607
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700608 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700609 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
610 /*
611 * Invalidate the journal device's buffers. We don't want them
612 * floating about in memory - the physical journal device may
613 * hotswapped, and it breaks the `ro-after' testing code.
614 */
615 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700616 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700617 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700618 }
619 sb->s_fs_info = NULL;
620 kfree(sbi);
621 return;
622}
623
Christoph Lametere18b8902006-12-06 20:33:20 -0800624static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625
626/*
627 * Called inside transaction, so use GFP_NOFS
628 */
Mingming Cao617ba132006-10-11 01:20:53 -0700629static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700630{
Mingming Cao617ba132006-10-11 01:20:53 -0700631 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800633 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700634 if (!ei)
635 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400636#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700637 ei->i_acl = EXT4_ACL_NOT_CACHED;
638 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700639#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700640 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400641 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700642 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500643 INIT_LIST_HEAD(&ei->i_prealloc_list);
644 spin_lock_init(&ei->i_prealloc_lock);
Frank Mayhar03901312009-01-07 00:06:22 -0500645 /*
646 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
647 * therefore it can be null here. Don't check it, just initialize
648 * jinode.
649 */
Jan Kara678aaf42008-07-11 19:27:31 -0400650 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400651 ei->i_reserved_data_blocks = 0;
652 ei->i_reserved_meta_blocks = 0;
653 ei->i_allocated_meta_blocks = 0;
654 ei->i_delalloc_reserved_flag = 0;
655 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700656 return &ei->vfs_inode;
657}
658
Mingming Cao617ba132006-10-11 01:20:53 -0700659static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700660{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700661 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
662 printk("EXT4 Inode %p: orphan list check failed!\n",
663 EXT4_I(inode));
664 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
665 EXT4_I(inode), sizeof(struct ext4_inode_info),
666 true);
667 dump_stack();
668 }
Mingming Cao617ba132006-10-11 01:20:53 -0700669 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670}
671
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700672static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673{
Mingming Cao617ba132006-10-11 01:20:53 -0700674 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675
Christoph Lametera35afb82007-05-16 22:10:57 -0700676 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400677#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700678 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700679#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500680 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700681 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682}
683
684static int init_inodecache(void)
685{
Mingming Cao617ba132006-10-11 01:20:53 -0700686 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
687 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700688 0, (SLAB_RECLAIM_ACCOUNT|
689 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900690 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700691 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700692 return -ENOMEM;
693 return 0;
694}
695
696static void destroy_inodecache(void)
697{
Mingming Cao617ba132006-10-11 01:20:53 -0700698 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700699}
700
Mingming Cao617ba132006-10-11 01:20:53 -0700701static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700702{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400703#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700704 if (EXT4_I(inode)->i_acl &&
705 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
706 posix_acl_release(EXT4_I(inode)->i_acl);
707 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 }
Mingming Cao617ba132006-10-11 01:20:53 -0700709 if (EXT4_I(inode)->i_default_acl &&
710 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
711 posix_acl_release(EXT4_I(inode)->i_default_acl);
712 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700713 }
714#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400715 ext4_discard_preallocations(inode);
Frank Mayhar03901312009-01-07 00:06:22 -0500716 if (EXT4_JOURNAL(inode))
717 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
Jan Kara678aaf42008-07-11 19:27:31 -0400718 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700719}
720
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400721static inline void ext4_show_quota_options(struct seq_file *seq,
722 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723{
724#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700725 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700726
727 if (sbi->s_jquota_fmt)
728 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400729 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700730
731 if (sbi->s_qf_names[USRQUOTA])
732 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
733
734 if (sbi->s_qf_names[GRPQUOTA])
735 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
736
Mingming Cao617ba132006-10-11 01:20:53 -0700737 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 seq_puts(seq, ",usrquota");
739
Mingming Cao617ba132006-10-11 01:20:53 -0700740 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700741 seq_puts(seq, ",grpquota");
742#endif
743}
744
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700745/*
746 * Show an option if
747 * - it's set to a non-default value OR
748 * - if the per-sb default is different from the global default
749 */
Mingming Cao617ba132006-10-11 01:20:53 -0700750static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500752 int def_errors;
753 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700755 struct ext4_sb_info *sbi = EXT4_SB(sb);
756 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700757
758 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500759 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700760
761 if (sbi->s_sb_block != 1)
762 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
763 if (test_opt(sb, MINIX_DF))
764 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500765 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700766 seq_puts(seq, ",grpid");
767 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
768 seq_puts(seq, ",nogrpid");
769 if (sbi->s_resuid != EXT4_DEF_RESUID ||
770 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
771 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
772 }
773 if (sbi->s_resgid != EXT4_DEF_RESGID ||
774 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
775 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
776 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500777 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700778 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500779 def_errors == EXT4_ERRORS_CONTINUE) {
780 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700781 }
782 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500783 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500784 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500785 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700786 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500787 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700788 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500789 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700790 seq_puts(seq, ",debug");
791 if (test_opt(sb, OLDALLOC))
792 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400793#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500794 if (test_opt(sb, XATTR_USER) &&
795 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700796 seq_puts(seq, ",user_xattr");
797 if (!test_opt(sb, XATTR_USER) &&
798 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
799 seq_puts(seq, ",nouser_xattr");
800 }
801#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400802#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500803 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700804 seq_puts(seq, ",acl");
805 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
806 seq_puts(seq, ",noacl");
807#endif
808 if (!test_opt(sb, RESERVATION))
809 seq_puts(seq, ",noreservation");
Theodore Ts'o30773842009-01-03 20:27:38 -0500810 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700811 seq_printf(seq, ",commit=%u",
812 (unsigned) (sbi->s_commit_interval / HZ));
813 }
Theodore Ts'o30773842009-01-03 20:27:38 -0500814 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
815 seq_printf(seq, ",min_batch_time=%u",
816 (unsigned) sbi->s_min_batch_time);
817 }
818 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
819 seq_printf(seq, ",max_batch_time=%u",
820 (unsigned) sbi->s_min_batch_time);
821 }
822
Eric Sandeen571640c2008-05-26 12:29:46 -0400823 /*
824 * We're changing the default of barrier mount option, so
825 * let's always display its mount state so it's clear what its
826 * status is.
827 */
828 seq_puts(seq, ",barrier=");
829 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400830 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
831 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700832 if (test_opt(sb, NOBH))
833 seq_puts(seq, ",nobh");
834 if (!test_opt(sb, EXTENTS))
835 seq_puts(seq, ",noextents");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500836 if (test_opt(sb, I_VERSION))
837 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400838 if (!test_opt(sb, DELALLOC))
839 seq_puts(seq, ",nodelalloc");
840
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700841
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500842 if (sbi->s_stripe)
843 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500844 /*
845 * journal mode get enabled in different ways
846 * So just print the value even if we didn't specify it
847 */
Mingming Cao617ba132006-10-11 01:20:53 -0700848 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700849 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700850 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700852 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700853 seq_puts(seq, ",data=writeback");
854
Theodore Ts'o240799c2008-10-09 23:53:47 -0400855 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
856 seq_printf(seq, ",inode_readahead_blks=%u",
857 sbi->s_inode_readahead_blks);
858
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400859 if (test_opt(sb, DATA_ERR_ABORT))
860 seq_puts(seq, ",data_err=abort");
861
Mingming Cao617ba132006-10-11 01:20:53 -0700862 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863 return 0;
864}
865
866
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700867static struct inode *ext4_nfs_get_inode(struct super_block *sb,
868 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700869{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700870 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700871
Mingming Cao617ba132006-10-11 01:20:53 -0700872 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700873 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700874 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700875 return ERR_PTR(-ESTALE);
876
877 /* iget isn't really right if the inode is currently unallocated!!
878 *
Mingming Cao617ba132006-10-11 01:20:53 -0700879 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880 * deleted, so we should be safe.
881 *
882 * Currently we don't know the generation for parent directory, so
883 * a generation of 0 means "accept any"
884 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800885 inode = ext4_iget(sb, ino);
886 if (IS_ERR(inode))
887 return ERR_CAST(inode);
888 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 iput(inode);
890 return ERR_PTR(-ESTALE);
891 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700892
893 return inode;
894}
895
896static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
897 int fh_len, int fh_type)
898{
899 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
900 ext4_nfs_get_inode);
901}
902
903static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
904 int fh_len, int fh_type)
905{
906 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
907 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908}
909
910#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400911#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400912#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700913
Mingming Cao617ba132006-10-11 01:20:53 -0700914static int ext4_dquot_initialize(struct inode *inode, int type);
915static int ext4_dquot_drop(struct inode *inode);
916static int ext4_write_dquot(struct dquot *dquot);
917static int ext4_acquire_dquot(struct dquot *dquot);
918static int ext4_release_dquot(struct dquot *dquot);
919static int ext4_mark_dquot_dirty(struct dquot *dquot);
920static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700921static int ext4_quota_on(struct super_block *sb, int type, int format_id,
922 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700923static int ext4_quota_on_mount(struct super_block *sb, int type);
924static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700925 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700926static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700927 const char *data, size_t len, loff_t off);
928
Mingming Cao617ba132006-10-11 01:20:53 -0700929static struct dquot_operations ext4_quota_operations = {
930 .initialize = ext4_dquot_initialize,
931 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 .alloc_space = dquot_alloc_space,
933 .alloc_inode = dquot_alloc_inode,
934 .free_space = dquot_free_space,
935 .free_inode = dquot_free_inode,
936 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700937 .write_dquot = ext4_write_dquot,
938 .acquire_dquot = ext4_acquire_dquot,
939 .release_dquot = ext4_release_dquot,
940 .mark_dirty = ext4_mark_dquot_dirty,
941 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700942};
943
Mingming Cao617ba132006-10-11 01:20:53 -0700944static struct quotactl_ops ext4_qctl_operations = {
945 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 .quota_off = vfs_quota_off,
947 .quota_sync = vfs_quota_sync,
948 .get_info = vfs_get_dqinfo,
949 .set_info = vfs_set_dqinfo,
950 .get_dqblk = vfs_get_dqblk,
951 .set_dqblk = vfs_set_dqblk
952};
953#endif
954
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800955static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700956 .alloc_inode = ext4_alloc_inode,
957 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700958 .write_inode = ext4_write_inode,
959 .dirty_inode = ext4_dirty_inode,
960 .delete_inode = ext4_delete_inode,
961 .put_super = ext4_put_super,
962 .write_super = ext4_write_super,
963 .sync_fs = ext4_sync_fs,
964 .write_super_lockfs = ext4_write_super_lockfs,
965 .unlockfs = ext4_unlockfs,
966 .statfs = ext4_statfs,
967 .remount_fs = ext4_remount,
968 .clear_inode = ext4_clear_inode,
969 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700970#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700971 .quota_read = ext4_quota_read,
972 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973#endif
974};
975
Christoph Hellwig39655162007-10-21 16:42:17 -0700976static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700977 .fh_to_dentry = ext4_fh_to_dentry,
978 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700979 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700980};
981
982enum {
983 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
984 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400985 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700986 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
987 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -0500988 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
989 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500990 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400992 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
994 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
995 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500996 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400997 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -0400998 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700999};
1000
Steven Whitehousea447c092008-10-13 10:46:57 +01001001static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001002 {Opt_bsd_df, "bsddf"},
1003 {Opt_minix_df, "minixdf"},
1004 {Opt_grpid, "grpid"},
1005 {Opt_grpid, "bsdgroups"},
1006 {Opt_nogrpid, "nogrpid"},
1007 {Opt_nogrpid, "sysvgroups"},
1008 {Opt_resgid, "resgid=%u"},
1009 {Opt_resuid, "resuid=%u"},
1010 {Opt_sb, "sb=%u"},
1011 {Opt_err_cont, "errors=continue"},
1012 {Opt_err_panic, "errors=panic"},
1013 {Opt_err_ro, "errors=remount-ro"},
1014 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 {Opt_debug, "debug"},
1016 {Opt_oldalloc, "oldalloc"},
1017 {Opt_orlov, "orlov"},
1018 {Opt_user_xattr, "user_xattr"},
1019 {Opt_nouser_xattr, "nouser_xattr"},
1020 {Opt_acl, "acl"},
1021 {Opt_noacl, "noacl"},
1022 {Opt_reservation, "reservation"},
1023 {Opt_noreservation, "noreservation"},
1024 {Opt_noload, "noload"},
1025 {Opt_nobh, "nobh"},
1026 {Opt_bh, "bh"},
1027 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001028 {Opt_min_batch_time, "min_batch_time=%u"},
1029 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 {Opt_journal_update, "journal=update"},
1031 {Opt_journal_inum, "journal=%u"},
1032 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001033 {Opt_journal_checksum, "journal_checksum"},
1034 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 {Opt_abort, "abort"},
1036 {Opt_data_journal, "data=journal"},
1037 {Opt_data_ordered, "data=ordered"},
1038 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001039 {Opt_data_err_abort, "data_err=abort"},
1040 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001041 {Opt_offusrjquota, "usrjquota="},
1042 {Opt_usrjquota, "usrjquota=%s"},
1043 {Opt_offgrpjquota, "grpjquota="},
1044 {Opt_grpjquota, "grpjquota=%s"},
1045 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1046 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1047 {Opt_grpquota, "grpquota"},
1048 {Opt_noquota, "noquota"},
1049 {Opt_quota, "quota"},
1050 {Opt_usrquota, "usrquota"},
1051 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -07001052 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -04001053 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001054 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001055 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001056 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001057 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001058 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001059 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001060 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061};
1062
Mingming Cao617ba132006-10-11 01:20:53 -07001063static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064{
Mingming Cao617ba132006-10-11 01:20:53 -07001065 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 char *options = (char *) *data;
1067
1068 if (!options || strncmp(options, "sb=", 3) != 0)
1069 return 1; /* Default location */
1070 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -07001071 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001072 sb_block = simple_strtoul(options, &options, 0);
1073 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001074 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001075 (char *) *data);
1076 return 1;
1077 }
1078 if (*options == ',')
1079 options++;
1080 *data = (void *) options;
1081 return sb_block;
1082}
1083
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001084static int parse_options(char *options, struct super_block *sb,
1085 unsigned int *inum, unsigned long *journal_devnum,
1086 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087{
Mingming Cao617ba132006-10-11 01:20:53 -07001088 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001089 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001090 substring_t args[MAX_OPT_ARGS];
1091 int data_opt = 0;
1092 int option;
1093#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001094 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095 char *qname;
1096#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001097 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098
1099 if (!options)
1100 return 1;
1101
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001102 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001103 int token;
1104 if (!*p)
1105 continue;
1106
1107 token = match_token(p, tokens, args);
1108 switch (token) {
1109 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001110 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001111 break;
1112 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001113 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001114 break;
1115 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001116 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001117 break;
1118 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001119 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001120 break;
1121 case Opt_resuid:
1122 if (match_int(&args[0], &option))
1123 return 0;
1124 sbi->s_resuid = option;
1125 break;
1126 case Opt_resgid:
1127 if (match_int(&args[0], &option))
1128 return 0;
1129 sbi->s_resgid = option;
1130 break;
1131 case Opt_sb:
1132 /* handled by get_sb_block() instead of here */
1133 /* *sb_block = match_int(&args[0]); */
1134 break;
1135 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001136 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1137 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1138 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 break;
1140 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001141 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1142 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1143 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001144 break;
1145 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001146 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1147 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1148 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 break;
1150 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001151 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001153 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001154 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001155 break;
1156 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001157 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001158 break;
1159 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001160 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001161 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001162#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001164 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 break;
1166 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001167 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001168 break;
1169#else
1170 case Opt_user_xattr:
1171 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001172 printk(KERN_ERR "EXT4 (no)user_xattr options "
1173 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 break;
1175#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001176#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 case Opt_acl:
1178 set_opt(sbi->s_mount_opt, POSIX_ACL);
1179 break;
1180 case Opt_noacl:
1181 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1182 break;
1183#else
1184 case Opt_acl:
1185 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001186 printk(KERN_ERR "EXT4 (no)acl options "
1187 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 break;
1189#endif
1190 case Opt_reservation:
1191 set_opt(sbi->s_mount_opt, RESERVATION);
1192 break;
1193 case Opt_noreservation:
1194 clear_opt(sbi->s_mount_opt, RESERVATION);
1195 break;
1196 case Opt_journal_update:
1197 /* @@@ FIXME */
1198 /* Eventually we will want to be able to create
1199 a journal file here. For now, only allow the
1200 user to specify an existing inode to be the
1201 journal file. */
1202 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001203 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 "journal on remount\n");
1205 return 0;
1206 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001207 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 break;
1209 case Opt_journal_inum:
1210 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001211 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001212 "journal on remount\n");
1213 return 0;
1214 }
1215 if (match_int(&args[0], &option))
1216 return 0;
1217 *inum = option;
1218 break;
1219 case Opt_journal_dev:
1220 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001221 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001222 "journal on remount\n");
1223 return 0;
1224 }
1225 if (match_int(&args[0], &option))
1226 return 0;
1227 *journal_devnum = option;
1228 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001229 case Opt_journal_checksum:
1230 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1231 break;
1232 case Opt_journal_async_commit:
1233 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1234 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1235 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001236 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001237 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 break;
1239 case Opt_commit:
1240 if (match_int(&args[0], &option))
1241 return 0;
1242 if (option < 0)
1243 return 0;
1244 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001245 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 sbi->s_commit_interval = HZ * option;
1247 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001248 case Opt_max_batch_time:
1249 if (match_int(&args[0], &option))
1250 return 0;
1251 if (option < 0)
1252 return 0;
1253 if (option == 0)
1254 option = EXT4_DEF_MAX_BATCH_TIME;
1255 sbi->s_max_batch_time = option;
1256 break;
1257 case Opt_min_batch_time:
1258 if (match_int(&args[0], &option))
1259 return 0;
1260 if (option < 0)
1261 return 0;
1262 sbi->s_min_batch_time = option;
1263 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001264 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001265 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 goto datacheck;
1267 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001268 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001269 goto datacheck;
1270 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001271 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001272 datacheck:
1273 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001274 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001275 != data_opt) {
1276 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001277 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001278 "mode on remount\n");
1279 return 0;
1280 }
1281 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001282 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001283 sbi->s_mount_opt |= data_opt;
1284 }
1285 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001286 case Opt_data_err_abort:
1287 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1288 break;
1289 case Opt_data_err_ignore:
1290 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1291 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292#ifdef CONFIG_QUOTA
1293 case Opt_usrjquota:
1294 qtype = USRQUOTA;
1295 goto set_qf_name;
1296 case Opt_grpjquota:
1297 qtype = GRPQUOTA;
1298set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001299 if ((sb_any_quota_enabled(sb) ||
1300 sb_any_quota_suspended(sb)) &&
1301 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001303 "EXT4-fs: Cannot change journaled "
1304 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001305 return 0;
1306 }
1307 qname = match_strdup(&args[0]);
1308 if (!qname) {
1309 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001310 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311 "storing quotafile name.\n");
1312 return 0;
1313 }
1314 if (sbi->s_qf_names[qtype] &&
1315 strcmp(sbi->s_qf_names[qtype], qname)) {
1316 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001317 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001318 "specified.\n", QTYPE2NAME(qtype));
1319 kfree(qname);
1320 return 0;
1321 }
1322 sbi->s_qf_names[qtype] = qname;
1323 if (strchr(sbi->s_qf_names[qtype], '/')) {
1324 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001325 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001326 "filesystem root.\n");
1327 kfree(sbi->s_qf_names[qtype]);
1328 sbi->s_qf_names[qtype] = NULL;
1329 return 0;
1330 }
1331 set_opt(sbi->s_mount_opt, QUOTA);
1332 break;
1333 case Opt_offusrjquota:
1334 qtype = USRQUOTA;
1335 goto clear_qf_name;
1336 case Opt_offgrpjquota:
1337 qtype = GRPQUOTA;
1338clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001339 if ((sb_any_quota_enabled(sb) ||
1340 sb_any_quota_suspended(sb)) &&
1341 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001342 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001343 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 "quota turned on.\n");
1345 return 0;
1346 }
1347 /*
1348 * The space will be released later when all options
1349 * are confirmed to be correct
1350 */
1351 sbi->s_qf_names[qtype] = NULL;
1352 break;
1353 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001354 qfmt = QFMT_VFS_OLD;
1355 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001356 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001357 qfmt = QFMT_VFS_V0;
1358set_qf_format:
1359 if ((sb_any_quota_enabled(sb) ||
1360 sb_any_quota_suspended(sb)) &&
1361 sbi->s_jquota_fmt != qfmt) {
1362 printk(KERN_ERR "EXT4-fs: Cannot change "
1363 "journaled quota options when "
1364 "quota turned on.\n");
1365 return 0;
1366 }
1367 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001368 break;
1369 case Opt_quota:
1370 case Opt_usrquota:
1371 set_opt(sbi->s_mount_opt, QUOTA);
1372 set_opt(sbi->s_mount_opt, USRQUOTA);
1373 break;
1374 case Opt_grpquota:
1375 set_opt(sbi->s_mount_opt, QUOTA);
1376 set_opt(sbi->s_mount_opt, GRPQUOTA);
1377 break;
1378 case Opt_noquota:
1379 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001380 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001381 "options when quota turned on.\n");
1382 return 0;
1383 }
1384 clear_opt(sbi->s_mount_opt, QUOTA);
1385 clear_opt(sbi->s_mount_opt, USRQUOTA);
1386 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1387 break;
1388#else
1389 case Opt_quota:
1390 case Opt_usrquota:
1391 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001392 printk(KERN_ERR
1393 "EXT4-fs: quota options not supported.\n");
1394 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395 case Opt_usrjquota:
1396 case Opt_grpjquota:
1397 case Opt_offusrjquota:
1398 case Opt_offgrpjquota:
1399 case Opt_jqfmt_vfsold:
1400 case Opt_jqfmt_vfsv0:
1401 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001402 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001403 "supported.\n");
1404 break;
1405 case Opt_noquota:
1406 break;
1407#endif
1408 case Opt_abort:
1409 set_opt(sbi->s_mount_opt, ABORT);
1410 break;
1411 case Opt_barrier:
1412 if (match_int(&args[0], &option))
1413 return 0;
1414 if (option)
1415 set_opt(sbi->s_mount_opt, BARRIER);
1416 else
1417 clear_opt(sbi->s_mount_opt, BARRIER);
1418 break;
1419 case Opt_ignore:
1420 break;
1421 case Opt_resize:
1422 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001423 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 "for remount\n");
1425 return 0;
1426 }
1427 if (match_int(&args[0], &option) != 0)
1428 return 0;
1429 *n_blocks_count = option;
1430 break;
1431 case Opt_nobh:
1432 set_opt(sbi->s_mount_opt, NOBH);
1433 break;
1434 case Opt_bh:
1435 clear_opt(sbi->s_mount_opt, NOBH);
1436 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001437 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001438 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1439 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1440 ext4_warning(sb, __func__,
1441 "extents feature not enabled "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001442 "on this filesystem, use tune2fs");
Eric Sandeene4079a12008-07-11 19:27:31 -04001443 return 0;
1444 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001445 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001446 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001447 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001448 /*
1449 * When e2fsprogs support resizing an already existing
1450 * ext3 file system to greater than 2**32 we need to
1451 * add support to block allocator to handle growing
1452 * already existing block mapped inode so that blocks
1453 * allocated for them fall within 2**32
1454 */
1455 last_block = ext4_blocks_count(sbi->s_es) - 1;
1456 if (last_block > 0xffffffffULL) {
1457 printk(KERN_ERR "EXT4-fs: Filesystem too "
1458 "large to mount with "
1459 "-o noextents options\n");
1460 return 0;
1461 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001462 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001463 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001464 case Opt_i_version:
1465 set_opt(sbi->s_mount_opt, I_VERSION);
1466 sb->s_flags |= MS_I_VERSION;
1467 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001468 case Opt_nodelalloc:
1469 clear_opt(sbi->s_mount_opt, DELALLOC);
1470 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001471 case Opt_stripe:
1472 if (match_int(&args[0], &option))
1473 return 0;
1474 if (option < 0)
1475 return 0;
1476 sbi->s_stripe = option;
1477 break;
Alex Tomas64769242008-07-11 19:27:31 -04001478 case Opt_delalloc:
1479 set_opt(sbi->s_mount_opt, DELALLOC);
1480 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001481 case Opt_inode_readahead_blks:
1482 if (match_int(&args[0], &option))
1483 return 0;
1484 if (option < 0 || option > (1 << 30))
1485 return 0;
1486 sbi->s_inode_readahead_blks = option;
1487 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001488 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001489 printk(KERN_ERR
1490 "EXT4-fs: Unrecognized mount option \"%s\" "
1491 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001492 return 0;
1493 }
1494 }
1495#ifdef CONFIG_QUOTA
1496 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001497 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001498 sbi->s_qf_names[USRQUOTA])
1499 clear_opt(sbi->s_mount_opt, USRQUOTA);
1500
Mingming Cao617ba132006-10-11 01:20:53 -07001501 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001502 sbi->s_qf_names[GRPQUOTA])
1503 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1504
1505 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001506 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001508 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1509 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001510 "format mixing.\n");
1511 return 0;
1512 }
1513
1514 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001515 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 "not specified.\n");
1517 return 0;
1518 }
1519 } else {
1520 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001521 printk(KERN_ERR "EXT4-fs: journaled quota format "
1522 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523 "enabled.\n");
1524 return 0;
1525 }
1526 }
1527#endif
1528 return 1;
1529}
1530
Mingming Cao617ba132006-10-11 01:20:53 -07001531static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001532 int read_only)
1533{
Mingming Cao617ba132006-10-11 01:20:53 -07001534 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001535 int res = 0;
1536
Mingming Cao617ba132006-10-11 01:20:53 -07001537 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001538 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1539 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001540 res = MS_RDONLY;
1541 }
1542 if (read_only)
1543 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001544 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001545 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1546 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001547 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001548 printk(KERN_WARNING
1549 "EXT4-fs warning: mounting fs with errors, "
1550 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001551 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1552 le16_to_cpu(es->s_mnt_count) >=
1553 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001554 printk(KERN_WARNING
1555 "EXT4-fs warning: maximal mount count reached, "
1556 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001557 else if (le32_to_cpu(es->s_checkinterval) &&
1558 (le32_to_cpu(es->s_lastcheck) +
1559 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001560 printk(KERN_WARNING
1561 "EXT4-fs warning: checktime reached, "
1562 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001563 if (!sbi->s_journal)
1564 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001565 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001566 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001567 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001568 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001569 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001570 if (sbi->s_journal)
1571 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001572
Mingming Cao617ba132006-10-11 01:20:53 -07001573 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001575 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001576 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1577 sb->s_blocksize,
1578 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001579 EXT4_BLOCKS_PER_GROUP(sb),
1580 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001581 sbi->s_mount_opt);
1582
Frank Mayhar03901312009-01-07 00:06:22 -05001583 if (EXT4_SB(sb)->s_journal) {
1584 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1585 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1586 "external", EXT4_SB(sb)->s_journal->j_devname);
1587 } else {
1588 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1589 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001590 return res;
1591}
1592
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001593static int ext4_fill_flex_info(struct super_block *sb)
1594{
1595 struct ext4_sb_info *sbi = EXT4_SB(sb);
1596 struct ext4_group_desc *gdp = NULL;
1597 struct buffer_head *bh;
1598 ext4_group_t flex_group_count;
1599 ext4_group_t flex_group;
1600 int groups_per_flex = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001601 int i;
1602
1603 if (!sbi->s_es->s_log_groups_per_flex) {
1604 sbi->s_log_groups_per_flex = 0;
1605 return 1;
1606 }
1607
1608 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1609 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1610
Frederic Bohec62a11f2008-09-08 10:20:24 -04001611 /* We allocate both existing and potentially added groups */
1612 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001613 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1614 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001615 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001616 sizeof(struct flex_groups), GFP_KERNEL);
1617 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001618 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001619 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001620 goto failed;
1621 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001622
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001623 for (i = 0; i < sbi->s_groups_count; i++) {
1624 gdp = ext4_get_group_desc(sb, i, &bh);
1625
1626 flex_group = ext4_flex_group(sbi, i);
1627 sbi->s_flex_groups[flex_group].free_inodes +=
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001628 ext4_free_inodes_count(sb, gdp);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001629 sbi->s_flex_groups[flex_group].free_blocks +=
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001630 ext4_free_blks_count(sb, gdp);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001631 }
1632
1633 return 1;
1634failed:
1635 return 0;
1636}
1637
Andreas Dilger717d50e2007-10-16 18:38:25 -04001638__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1639 struct ext4_group_desc *gdp)
1640{
1641 __u16 crc = 0;
1642
1643 if (sbi->s_es->s_feature_ro_compat &
1644 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1645 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1646 __le32 le_group = cpu_to_le32(block_group);
1647
1648 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1649 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1650 crc = crc16(crc, (__u8 *)gdp, offset);
1651 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1652 /* for checksum of struct ext4_group_desc do the rest...*/
1653 if ((sbi->s_es->s_feature_incompat &
1654 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1655 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1656 crc = crc16(crc, (__u8 *)gdp + offset,
1657 le16_to_cpu(sbi->s_es->s_desc_size) -
1658 offset);
1659 }
1660
1661 return cpu_to_le16(crc);
1662}
1663
1664int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1665 struct ext4_group_desc *gdp)
1666{
1667 if ((sbi->s_es->s_feature_ro_compat &
1668 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1669 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1670 return 0;
1671
1672 return 1;
1673}
1674
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001676static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001677{
Mingming Cao617ba132006-10-11 01:20:53 -07001678 struct ext4_sb_info *sbi = EXT4_SB(sb);
1679 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1680 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001681 ext4_fsblk_t block_bitmap;
1682 ext4_fsblk_t inode_bitmap;
1683 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001684 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001685 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001686
Jose R. Santosce421582007-10-16 18:38:25 -04001687 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1688 flexbg_flag = 1;
1689
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001690 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001692 for (i = 0; i < sbi->s_groups_count; i++) {
1693 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1694
Jose R. Santosce421582007-10-16 18:38:25 -04001695 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001696 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697 else
1698 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001699 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001701 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001702 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001703 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001704 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001705 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 return 0;
1707 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001708 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001709 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001710 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001711 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001712 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713 return 0;
1714 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001715 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001716 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001717 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001718 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001719 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001720 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001721 return 0;
1722 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001723 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001724 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001725 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001726 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001727 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1728 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001729 if (!(sb->s_flags & MS_RDONLY)) {
1730 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001731 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001732 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001733 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001734 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001735 if (!flexbg_flag)
1736 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 }
1738
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001739 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001740 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 return 1;
1742}
1743
Mingming Cao617ba132006-10-11 01:20:53 -07001744/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745 * the superblock) which were deleted from all directories, but held open by
1746 * a process at the time of a crash. We walk the list and try to delete these
1747 * inodes at recovery time (only with a read-write filesystem).
1748 *
1749 * In order to keep the orphan inode chain consistent during traversal (in
1750 * case of crash during recovery), we link each inode into the superblock
1751 * orphan list_head and handle it the same way as an inode deletion during
1752 * normal operation (which journals the operations for us).
1753 *
1754 * We only do an iget() and an iput() on each inode, which is very safe if we
1755 * accidentally point at an in-use or already deleted inode. The worst that
1756 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001757 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001758 * e2fsck was run on this filesystem, and it must have already done the orphan
1759 * inode cleanup for us, so we can safely abort without any further action.
1760 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001761static void ext4_orphan_cleanup(struct super_block *sb,
1762 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001763{
1764 unsigned int s_flags = sb->s_flags;
1765 int nr_orphans = 0, nr_truncates = 0;
1766#ifdef CONFIG_QUOTA
1767 int i;
1768#endif
1769 if (!es->s_last_orphan) {
1770 jbd_debug(4, "no orphan inodes to clean up\n");
1771 return;
1772 }
1773
Eric Sandeena8f48a92006-12-06 20:40:13 -08001774 if (bdev_read_only(sb->s_bdev)) {
1775 printk(KERN_ERR "EXT4-fs: write access "
1776 "unavailable, skipping orphan cleanup.\n");
1777 return;
1778 }
1779
Mingming Cao617ba132006-10-11 01:20:53 -07001780 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001781 if (es->s_last_orphan)
1782 jbd_debug(1, "Errors on filesystem, "
1783 "clearing orphan list.\n");
1784 es->s_last_orphan = 0;
1785 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1786 return;
1787 }
1788
1789 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001790 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001791 sb->s_id);
1792 sb->s_flags &= ~MS_RDONLY;
1793 }
1794#ifdef CONFIG_QUOTA
1795 /* Needed for iput() to work correctly and not trash data */
1796 sb->s_flags |= MS_ACTIVE;
1797 /* Turn on quotas so that they are updated correctly */
1798 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001799 if (EXT4_SB(sb)->s_qf_names[i]) {
1800 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 if (ret < 0)
1802 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001803 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001804 "quota: error %d\n", ret);
1805 }
1806 }
1807#endif
1808
1809 while (es->s_last_orphan) {
1810 struct inode *inode;
1811
Josef Bacik97bd42b2008-04-29 22:04:56 -04001812 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1813 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814 es->s_last_orphan = 0;
1815 break;
1816 }
1817
Mingming Cao617ba132006-10-11 01:20:53 -07001818 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 DQUOT_INIT(inode);
1820 if (inode->i_nlink) {
1821 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001822 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001823 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001824 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001825 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001826 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001827 nr_truncates++;
1828 } else {
1829 printk(KERN_DEBUG
1830 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001831 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001832 jbd_debug(2, "deleting unreferenced inode %lu\n",
1833 inode->i_ino);
1834 nr_orphans++;
1835 }
1836 iput(inode); /* The delete magic happens here! */
1837 }
1838
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001839#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001840
1841 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001842 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001843 sb->s_id, PLURAL(nr_orphans));
1844 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001845 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001846 sb->s_id, PLURAL(nr_truncates));
1847#ifdef CONFIG_QUOTA
1848 /* Turn quotas off */
1849 for (i = 0; i < MAXQUOTAS; i++) {
1850 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001851 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001852 }
1853#endif
1854 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1855}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001856/*
1857 * Maximal extent format file size.
1858 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1859 * extent format containers, within a sector_t, and within i_blocks
1860 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1861 * so that won't be a limiting factor.
1862 *
1863 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1864 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001865static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001866{
1867 loff_t res;
1868 loff_t upper_limit = MAX_LFS_FILESIZE;
1869
1870 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001871 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001872 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001873 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001874 * i_block represent total blocks in 512 bytes
1875 * 32 == size of vfs inode i_blocks * 8
1876 */
1877 upper_limit = (1LL << 32) - 1;
1878
1879 /* total blocks in file system block size */
1880 upper_limit >>= (blkbits - 9);
1881 upper_limit <<= blkbits;
1882 }
1883
1884 /* 32-bit extent-start container, ee_block */
1885 res = 1LL << 32;
1886 res <<= blkbits;
1887 res -= 1;
1888
1889 /* Sanity check against vm- & vfs- imposed limits */
1890 if (res > upper_limit)
1891 res = upper_limit;
1892
1893 return res;
1894}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001895
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001896/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001897 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001898 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1899 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001900 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001901static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001902{
Mingming Cao617ba132006-10-11 01:20:53 -07001903 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001904 int meta_blocks;
1905 loff_t upper_limit;
1906 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001907 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001908 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001909 * does not exceed 2^48 -1
1910 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1911 * total number of 512 bytes blocks of the file
1912 */
1913
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001914 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001915 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001916 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001917 * implies the inode i_block represent total blocks in
1918 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001919 */
1920 upper_limit = (1LL << 32) - 1;
1921
1922 /* total blocks in file system block size */
1923 upper_limit >>= (bits - 9);
1924
1925 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001926 /*
1927 * We use 48 bit ext4_inode i_blocks
1928 * With EXT4_HUGE_FILE_FL set the i_blocks
1929 * represent total number of blocks in
1930 * file system block size
1931 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001932 upper_limit = (1LL << 48) - 1;
1933
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001934 }
1935
1936 /* indirect blocks */
1937 meta_blocks = 1;
1938 /* double indirect blocks */
1939 meta_blocks += 1 + (1LL << (bits-2));
1940 /* tripple indirect blocks */
1941 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1942
1943 upper_limit -= meta_blocks;
1944 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001945
1946 res += 1LL << (bits-2);
1947 res += 1LL << (2*(bits-2));
1948 res += 1LL << (3*(bits-2));
1949 res <<= bits;
1950 if (res > upper_limit)
1951 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001952
1953 if (res > MAX_LFS_FILESIZE)
1954 res = MAX_LFS_FILESIZE;
1955
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956 return res;
1957}
1958
Mingming Cao617ba132006-10-11 01:20:53 -07001959static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001960 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961{
Mingming Cao617ba132006-10-11 01:20:53 -07001962 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001963 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 int has_super = 0;
1965
1966 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1967
Mingming Cao617ba132006-10-11 01:20:53 -07001968 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001970 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001971 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001972 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001973 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001974 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001975}
1976
Alex Tomasc9de5602008-01-29 00:19:52 -05001977/**
1978 * ext4_get_stripe_size: Get the stripe size.
1979 * @sbi: In memory super block info
1980 *
1981 * If we have specified it via mount option, then
1982 * use the mount option value. If the value specified at mount time is
1983 * greater than the blocks per group use the super block value.
1984 * If the super block value is greater than blocks per group return 0.
1985 * Allocator needs it be less than blocks per group.
1986 *
1987 */
1988static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1989{
1990 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1991 unsigned long stripe_width =
1992 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1993
1994 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1995 return sbi->s_stripe;
1996
1997 if (stripe_width <= sbi->s_blocks_per_group)
1998 return stripe_width;
1999
2000 if (stride <= sbi->s_blocks_per_group)
2001 return stride;
2002
2003 return 0;
2004}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002005
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002006static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002007 __releases(kernel_lock)
2008 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002009
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002010{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002011 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002012 struct ext4_super_block *es = NULL;
2013 struct ext4_sb_info *sbi;
2014 ext4_fsblk_t block;
2015 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002016 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002017 unsigned long offset = 0;
2018 unsigned int journal_inum = 0;
2019 unsigned long journal_devnum = 0;
2020 unsigned long def_mount_opts;
2021 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002022 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002023 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002024 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002026 int db_count;
2027 int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002028 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002029 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002030 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002031 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002032
2033 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2034 if (!sbi)
2035 return -ENOMEM;
2036 sb->s_fs_info = sbi;
2037 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002038 sbi->s_resuid = EXT4_DEF_RESUID;
2039 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002040 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002041 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002042
2043 unlock_kernel();
2044
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002045 /* Cleanup superblock name */
2046 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2047 *cp = '!';
2048
Mingming Cao617ba132006-10-11 01:20:53 -07002049 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07002051 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 goto out_fail;
2053 }
2054
2055 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002056 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 * block sizes. We need to calculate the offset from buffer start.
2058 */
Mingming Cao617ba132006-10-11 01:20:53 -07002059 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002060 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2061 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002063 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002064 }
2065
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002066 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002067 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002068 goto out_fail;
2069 }
2070 /*
2071 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002072 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002073 */
Mingming Cao617ba132006-10-11 01:20:53 -07002074 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 sbi->s_es = es;
2076 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002077 if (sb->s_magic != EXT4_SUPER_MAGIC)
2078 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079
2080 /* Set defaults before we parse the mount options */
2081 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002082 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002083 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07002084 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002085 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07002086 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002087 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002088#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002089 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002091#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002092#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002093 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002094 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002095#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002096 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2097 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2098 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2099 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2100 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2101 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102
Mingming Cao617ba132006-10-11 01:20:53 -07002103 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002104 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002105 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002106 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002107 else
2108 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109
2110 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2111 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002112 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2113 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2114 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002115
2116 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04002117 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002118
Mingming Cao1e2462f2007-07-18 09:00:55 -04002119 /*
2120 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04002121 * only if feature flag already set by mkfs or tune2fs.
2122 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04002123 */
Eric Sandeene4079a12008-07-11 19:27:31 -04002124 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2125 set_opt(sbi->s_mount_opt, EXTENTS);
2126 else
2127 ext4_warning(sb, __func__,
2128 "extents feature not enabled on this filesystem, "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002129 "use tune2fs.");
Mingming Cao1e2462f2007-07-18 09:00:55 -04002130
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002131 /*
2132 * enable delayed allocation by default
2133 * Use -o nodelalloc to turn it off
2134 */
2135 set_opt(sbi->s_mount_opt, DELALLOC);
2136
2137
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002138 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2139 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002140 goto failed_mount;
2141
2142 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002143 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144
Mingming Cao617ba132006-10-11 01:20:53 -07002145 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2146 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2147 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2148 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002150 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002151 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002152
2153 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002154 * Check feature flags regardless of the revision level, since we
2155 * previously didn't change the revision level when setting the flags,
2156 * so there is a chance incompat flags are set on a rev 0 filesystem.
2157 */
Mingming Cao617ba132006-10-11 01:20:53 -07002158 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002159 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002160 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002161 "unsupported optional features (%x).\n", sb->s_id,
2162 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2163 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 goto failed_mount;
2165 }
Mingming Cao617ba132006-10-11 01:20:53 -07002166 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002167 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002168 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002169 "unsupported optional features (%x).\n", sb->s_id,
2170 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2171 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 goto failed_mount;
2173 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002174 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2175 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2176 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002177 /*
2178 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002179 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002180 */
2181 if (sizeof(root->i_blocks) < sizeof(u64) &&
2182 !(sb->s_flags & MS_RDONLY)) {
2183 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2184 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002185 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002186 goto failed_mount;
2187 }
2188 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2190
Mingming Cao617ba132006-10-11 01:20:53 -07002191 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2192 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002194 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002195 blocksize, sb->s_id);
2196 goto failed_mount;
2197 }
2198
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002200
2201 /* Validate the filesystem blocksize */
2202 if (!sb_set_blocksize(sb, blocksize)) {
2203 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2204 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205 goto failed_mount;
2206 }
2207
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002208 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002209 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2210 offset = do_div(logical_sb_block, blocksize);
2211 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002212 if (!bh) {
2213 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002214 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215 goto failed_mount;
2216 }
Mingming Cao617ba132006-10-11 01:20:53 -07002217 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002218 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002219 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002220 printk(KERN_ERR
2221 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002222 goto failed_mount;
2223 }
2224 }
2225
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002226 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2227 has_huge_files);
2228 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002229
Mingming Cao617ba132006-10-11 01:20:53 -07002230 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2231 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2232 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233 } else {
2234 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2235 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002236 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002237 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002239 printk(KERN_ERR
2240 "EXT4-fs: unsupported inode size: %d\n",
2241 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 goto failed_mount;
2243 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002244 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2245 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002247 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2248 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002249 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002250 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002251 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002252 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002253 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002254 sbi->s_desc_size);
2255 goto failed_mount;
2256 }
2257 } else
2258 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002259 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002261 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002262 goto cantfind_ext4;
2263 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002265 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2267 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002268 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 sbi->s_sbh = bh;
2270 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002271 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2272 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002273 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002274 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2275 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002276 i = le32_to_cpu(es->s_flags);
2277 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2278 sbi->s_hash_unsigned = 3;
2279 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2280#ifdef __CHAR_UNSIGNED__
2281 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2282 sbi->s_hash_unsigned = 3;
2283#else
2284 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2285#endif
2286 sb->s_dirt = 1;
2287 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288
2289 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002290 printk(KERN_ERR
2291 "EXT4-fs: #blocks per group too big: %lu\n",
2292 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 goto failed_mount;
2294 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002296 printk(KERN_ERR
2297 "EXT4-fs: #inodes per group too big: %lu\n",
2298 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002299 goto failed_mount;
2300 }
2301
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002302 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002303 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002304 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002305 " too large to mount safely\n", sb->s_id);
2306 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002307 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002308 "enabled\n");
2309 goto failed_mount;
2310 }
2311
Mingming Cao617ba132006-10-11 01:20:53 -07002312 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2313 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002314
2315 /* ensure blocks_count calculation below doesn't sign-extend */
2316 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2317 le32_to_cpu(es->s_first_data_block) + 1) {
2318 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2319 "first data block %u, blocks per group %lu\n",
2320 ext4_blocks_count(es),
2321 le32_to_cpu(es->s_first_data_block),
2322 EXT4_BLOCKS_PER_GROUP(sb));
2323 goto failed_mount;
2324 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002325 blocks_count = (ext4_blocks_count(es) -
2326 le32_to_cpu(es->s_first_data_block) +
2327 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2328 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2329 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002330 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2331 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002332 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002333 GFP_KERNEL);
2334 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002335 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002336 goto failed_mount;
2337 }
2338
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002339#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002340 if (ext4_proc_root)
2341 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2342
Theodore Ts'o240799c2008-10-09 23:53:47 -04002343 if (sbi->s_proc)
2344 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2345 &ext4_ui_proc_fops,
2346 &sbi->s_inode_readahead_blks);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002347#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002348
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349 bgl_lock_init(&sbi->s_blockgroup_lock);
2350
2351 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002352 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353 sbi->s_group_desc[i] = sb_bread(sb, block);
2354 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002355 printk(KERN_ERR "EXT4-fs: "
2356 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002357 db_count = i;
2358 goto failed_mount2;
2359 }
2360 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002361 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002362 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002363 goto failed_mount2;
2364 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002365 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2366 if (!ext4_fill_flex_info(sb)) {
2367 printk(KERN_ERR
2368 "EXT4-fs: unable to initialize "
2369 "flex_bg meta info!\n");
2370 goto failed_mount2;
2371 }
2372
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002373 sbi->s_gdb_count = db_count;
2374 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2375 spin_lock_init(&sbi->s_next_gen_lock);
2376
Peter Zijlstra833f4072007-10-16 23:25:45 -07002377 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2378 ext4_count_free_blocks(sb));
2379 if (!err) {
2380 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2381 ext4_count_free_inodes(sb));
2382 }
2383 if (!err) {
2384 err = percpu_counter_init(&sbi->s_dirs_counter,
2385 ext4_count_dirs(sb));
2386 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002387 if (!err) {
2388 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2389 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002390 if (err) {
2391 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2392 goto failed_mount3;
2393 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002394
Alex Tomasc9de5602008-01-29 00:19:52 -05002395 sbi->s_stripe = ext4_get_stripe_size(sbi);
2396
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002397 /*
2398 * set up enough so that it can read an inode
2399 */
Mingming Cao617ba132006-10-11 01:20:53 -07002400 sb->s_op = &ext4_sops;
2401 sb->s_export_op = &ext4_export_ops;
2402 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002403#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002404 sb->s_qcop = &ext4_qctl_operations;
2405 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002406#endif
2407 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2408
2409 sb->s_root = NULL;
2410
2411 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002412 EXT4_HAS_INCOMPAT_FEATURE(sb,
2413 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414
2415 /*
2416 * The first inode we look at is the journal inode. Don't try
2417 * root first: it may be modified in the journal!
2418 */
2419 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002420 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2421 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002422 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002423 if (!(sb->s_flags & MS_RDONLY) &&
2424 EXT4_SB(sb)->s_journal->j_failed_commit) {
2425 printk(KERN_CRIT "EXT4-fs error (device %s): "
2426 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002427 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002428 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002429 if (test_opt(sb, ERRORS_RO)) {
2430 printk(KERN_CRIT
2431 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002432 sb->s_flags |= MS_RDONLY;
2433 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2434 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2435 }
2436 if (test_opt(sb, ERRORS_PANIC)) {
2437 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2438 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2439 ext4_commit_super(sb, es, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002440 goto failed_mount4;
2441 }
2442 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002443 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002444 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002445 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002446 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2447 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2448 printk(KERN_ERR "EXT4-fs: required journal recovery "
2449 "suppressed and not mounted read-only\n");
2450 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002451 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002452 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2453 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2454 sbi->s_journal = NULL;
2455 needs_recovery = 0;
2456 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002457 }
2458
Jose R. Santoseb40a092007-07-18 08:37:25 -04002459 if (ext4_blocks_count(es) > 0xffffffffULL &&
2460 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2461 JBD2_FEATURE_INCOMPAT_64BIT)) {
2462 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2463 goto failed_mount4;
2464 }
2465
Girish Shilamkar818d2762008-01-28 23:58:27 -05002466 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2467 jbd2_journal_set_features(sbi->s_journal,
2468 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2469 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2470 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2471 jbd2_journal_set_features(sbi->s_journal,
2472 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2473 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2474 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2475 } else {
2476 jbd2_journal_clear_features(sbi->s_journal,
2477 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2478 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2479 }
2480
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002481 /* We have now updated the journal if required, so we can
2482 * validate the data journaling mode. */
2483 switch (test_opt(sb, DATA_FLAGS)) {
2484 case 0:
2485 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002486 * capabilities: ORDERED_DATA if the journal can
2487 * cope, else JOURNAL_DATA
2488 */
Mingming Caodab291a2006-10-11 01:21:01 -07002489 if (jbd2_journal_check_available_features
2490 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002491 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2492 else
2493 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2494 break;
2495
Mingming Cao617ba132006-10-11 01:20:53 -07002496 case EXT4_MOUNT_ORDERED_DATA:
2497 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002498 if (!jbd2_journal_check_available_features
2499 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002500 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002501 "requested data journaling mode\n");
2502 goto failed_mount4;
2503 }
2504 default:
2505 break;
2506 }
2507
Frank Mayhar03901312009-01-07 00:06:22 -05002508no_journal:
2509
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002510 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002511 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2512 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 "its supported only with writeback mode\n");
2514 clear_opt(sbi->s_mount_opt, NOBH);
2515 }
2516 }
2517 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002518 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002519 * so we can safely mount the rest of the filesystem now.
2520 */
2521
David Howells1d1fe1e2008-02-07 00:15:37 -08002522 root = ext4_iget(sb, EXT4_ROOT_INO);
2523 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002524 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002525 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002526 goto failed_mount4;
2527 }
2528 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002529 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002530 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002531 goto failed_mount4;
2532 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002533 sb->s_root = d_alloc_root(root);
2534 if (!sb->s_root) {
2535 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2536 iput(root);
2537 ret = -ENOMEM;
2538 goto failed_mount4;
2539 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002540
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002541 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002542
2543 /* determine the minimum size of new large inodes, if present */
2544 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2545 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2546 EXT4_GOOD_OLD_INODE_SIZE;
2547 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2548 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2549 if (sbi->s_want_extra_isize <
2550 le16_to_cpu(es->s_want_extra_isize))
2551 sbi->s_want_extra_isize =
2552 le16_to_cpu(es->s_want_extra_isize);
2553 if (sbi->s_want_extra_isize <
2554 le16_to_cpu(es->s_min_extra_isize))
2555 sbi->s_want_extra_isize =
2556 le16_to_cpu(es->s_min_extra_isize);
2557 }
2558 }
2559 /* Check if enough inode space is available */
2560 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2561 sbi->s_inode_size) {
2562 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2563 EXT4_GOOD_OLD_INODE_SIZE;
2564 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2565 "available.\n");
2566 }
2567
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002568 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2569 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2570 "requested data journaling mode\n");
2571 clear_opt(sbi->s_mount_opt, DELALLOC);
2572 } else if (test_opt(sb, DELALLOC))
2573 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2574
2575 ext4_ext_init(sb);
2576 err = ext4_mb_init(sb, needs_recovery);
2577 if (err) {
2578 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2579 err);
2580 goto failed_mount4;
2581 }
2582
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002583 /*
2584 * akpm: core read_super() calls in here with the superblock locked.
2585 * That deadlocks, because orphan cleanup needs to lock the superblock
2586 * in numerous places. Here we just pop the lock - it's relatively
2587 * harmless, because we are now ready to accept write_super() requests,
2588 * and aviro says that's the only reason for hanging onto the
2589 * superblock lock.
2590 */
Mingming Cao617ba132006-10-11 01:20:53 -07002591 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2592 ext4_orphan_cleanup(sb, es);
2593 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002594 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002595 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002596 ext4_mark_recovery_complete(sb, es);
2597 }
2598 if (EXT4_SB(sb)->s_journal) {
2599 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2600 descr = " journalled data mode";
2601 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2602 descr = " ordered data mode";
2603 else
2604 descr = " writeback data mode";
2605 } else
2606 descr = "out journal";
2607
2608 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2609 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002610
2611 lock_kernel();
2612 return 0;
2613
Mingming Cao617ba132006-10-11 01:20:53 -07002614cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002615 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002616 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 sb->s_id);
2618 goto failed_mount;
2619
2620failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002621 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2622 if (sbi->s_journal) {
2623 jbd2_journal_destroy(sbi->s_journal);
2624 sbi->s_journal = NULL;
2625 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002626failed_mount3:
2627 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2628 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2629 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002630 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002631failed_mount2:
2632 for (i = 0; i < db_count; i++)
2633 brelse(sbi->s_group_desc[i]);
2634 kfree(sbi->s_group_desc);
2635failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002636 if (sbi->s_proc) {
2637 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002638 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002639 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002640#ifdef CONFIG_QUOTA
2641 for (i = 0; i < MAXQUOTAS; i++)
2642 kfree(sbi->s_qf_names[i]);
2643#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002644 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002645 brelse(bh);
2646out_fail:
2647 sb->s_fs_info = NULL;
2648 kfree(sbi);
2649 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002650 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002651}
2652
2653/*
2654 * Setup any per-fs journal parameters now. We'll do this both on
2655 * initial mount, once the journal has been initialised but before we've
2656 * done any recovery; and again on any subsequent remount.
2657 */
Mingming Cao617ba132006-10-11 01:20:53 -07002658static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002659{
Mingming Cao617ba132006-10-11 01:20:53 -07002660 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661
Theodore Ts'o30773842009-01-03 20:27:38 -05002662 journal->j_commit_interval = sbi->s_commit_interval;
2663 journal->j_min_batch_time = sbi->s_min_batch_time;
2664 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002665
2666 spin_lock(&journal->j_state_lock);
2667 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002668 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002669 else
Mingming Caodab291a2006-10-11 01:21:01 -07002670 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002671 if (test_opt(sb, DATA_ERR_ABORT))
2672 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2673 else
2674 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675 spin_unlock(&journal->j_state_lock);
2676}
2677
Mingming Cao617ba132006-10-11 01:20:53 -07002678static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679 unsigned int journal_inum)
2680{
2681 struct inode *journal_inode;
2682 journal_t *journal;
2683
Frank Mayhar03901312009-01-07 00:06:22 -05002684 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2685
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002686 /* First, test for the existence of a valid inode on disk. Bad
2687 * things happen if we iget() an unused inode, as the subsequent
2688 * iput() will try to delete it. */
2689
David Howells1d1fe1e2008-02-07 00:15:37 -08002690 journal_inode = ext4_iget(sb, journal_inum);
2691 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002692 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002693 return NULL;
2694 }
2695 if (!journal_inode->i_nlink) {
2696 make_bad_inode(journal_inode);
2697 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002698 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002699 return NULL;
2700 }
2701
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002702 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002703 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002704 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002705 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706 iput(journal_inode);
2707 return NULL;
2708 }
2709
Mingming Caodab291a2006-10-11 01:21:01 -07002710 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002711 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002712 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002713 iput(journal_inode);
2714 return NULL;
2715 }
2716 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002717 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 return journal;
2719}
2720
Mingming Cao617ba132006-10-11 01:20:53 -07002721static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002722 dev_t j_dev)
2723{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002724 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002726 ext4_fsblk_t start;
2727 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002728 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002729 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002730 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002731 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002732 struct block_device *bdev;
2733
Frank Mayhar03901312009-01-07 00:06:22 -05002734 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2735
Mingming Cao617ba132006-10-11 01:20:53 -07002736 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002737 if (bdev == NULL)
2738 return NULL;
2739
2740 if (bd_claim(bdev, sb)) {
2741 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002742 "EXT4: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002743 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744 return NULL;
2745 }
2746
2747 blocksize = sb->s_blocksize;
2748 hblock = bdev_hardsect_size(bdev);
2749 if (blocksize < hblock) {
2750 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002751 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 goto out_bdev;
2753 }
2754
Mingming Cao617ba132006-10-11 01:20:53 -07002755 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2756 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 set_blocksize(bdev, blocksize);
2758 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002759 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002760 "external journal\n");
2761 goto out_bdev;
2762 }
2763
Mingming Cao617ba132006-10-11 01:20:53 -07002764 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2765 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002766 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002767 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2768 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002769 "bad superblock\n");
2770 brelse(bh);
2771 goto out_bdev;
2772 }
2773
Mingming Cao617ba132006-10-11 01:20:53 -07002774 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2775 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002776 brelse(bh);
2777 goto out_bdev;
2778 }
2779
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002780 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002781 start = sb_block + 1;
2782 brelse(bh); /* we're done with the superblock */
2783
Mingming Caodab291a2006-10-11 01:21:01 -07002784 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002785 start, len, blocksize);
2786 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002787 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 goto out_bdev;
2789 }
2790 journal->j_private = sb;
2791 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2792 wait_on_buffer(journal->j_sb_buffer);
2793 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002794 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795 goto out_journal;
2796 }
2797 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002798 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002799 "user (unsupported) - %d\n",
2800 be32_to_cpu(journal->j_superblock->s_nr_users));
2801 goto out_journal;
2802 }
Mingming Cao617ba132006-10-11 01:20:53 -07002803 EXT4_SB(sb)->journal_bdev = bdev;
2804 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002805 return journal;
2806out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002807 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002808out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002809 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002810 return NULL;
2811}
2812
Mingming Cao617ba132006-10-11 01:20:53 -07002813static int ext4_load_journal(struct super_block *sb,
2814 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815 unsigned long journal_devnum)
2816{
2817 journal_t *journal;
2818 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2819 dev_t journal_dev;
2820 int err = 0;
2821 int really_read_only;
2822
Frank Mayhar03901312009-01-07 00:06:22 -05002823 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2824
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825 if (journal_devnum &&
2826 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002827 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828 "numbers have changed\n");
2829 journal_dev = new_decode_dev(journal_devnum);
2830 } else
2831 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2832
2833 really_read_only = bdev_read_only(sb->s_bdev);
2834
2835 /*
2836 * Are we loading a blank journal or performing recovery after a
2837 * crash? For recovery, we need to check in advance whether we
2838 * can get read-write access to the device.
2839 */
2840
Mingming Cao617ba132006-10-11 01:20:53 -07002841 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002842 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002843 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002844 "required on readonly filesystem.\n");
2845 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002846 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002847 "unavailable, cannot proceed.\n");
2848 return -EROFS;
2849 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002850 printk(KERN_INFO "EXT4-fs: write access will "
2851 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002852 }
2853 }
2854
2855 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002856 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002857 "and inode journals!\n");
2858 return -EINVAL;
2859 }
2860
2861 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002862 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002863 return -EINVAL;
2864 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002865 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866 return -EINVAL;
2867 }
2868
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002869 if (journal->j_flags & JBD2_BARRIER)
2870 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2871 else
2872 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2873
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002874 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002875 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002877 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002878 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002879 return err;
2880 }
2881 }
2882
Mingming Cao617ba132006-10-11 01:20:53 -07002883 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002884 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002885 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002886 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887
2888 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002889 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002890 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002891 return err;
2892 }
2893
Mingming Cao617ba132006-10-11 01:20:53 -07002894 EXT4_SB(sb)->s_journal = journal;
2895 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896
2897 if (journal_devnum &&
2898 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2899 es->s_journal_dev = cpu_to_le32(journal_devnum);
2900 sb->s_dirt = 1;
2901
2902 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002903 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002904 }
2905
2906 return 0;
2907}
2908
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002909static int ext4_create_journal(struct super_block *sb,
2910 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 unsigned int journal_inum)
2912{
2913 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002914 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915
2916 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002917 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002918 "create journal.\n");
2919 return -EROFS;
2920 }
2921
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002922 journal = ext4_get_journal(sb, journal_inum);
2923 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002924 return -EINVAL;
2925
Mingming Cao617ba132006-10-11 01:20:53 -07002926 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927 journal_inum);
2928
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002929 err = jbd2_journal_create(journal);
2930 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002931 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002932 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002933 return -EIO;
2934 }
2935
Mingming Cao617ba132006-10-11 01:20:53 -07002936 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937
Mingming Cao617ba132006-10-11 01:20:53 -07002938 ext4_update_dynamic_rev(sb);
2939 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2940 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002941
2942 es->s_journal_inum = cpu_to_le32(journal_inum);
2943 sb->s_dirt = 1;
2944
2945 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002946 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947
2948 return 0;
2949}
2950
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002951static void ext4_commit_super(struct super_block *sb,
2952 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002953{
Mingming Cao617ba132006-10-11 01:20:53 -07002954 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955
2956 if (!sbh)
2957 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002958 if (buffer_write_io_error(sbh)) {
2959 /*
2960 * Oh, dear. A previous attempt to write the
2961 * superblock failed. This could happen because the
2962 * USB device was yanked out. Or it could happen to
2963 * be a transient write error and maybe the block will
2964 * be remapped. Nothing we can do but to retry the
2965 * write and hope for the best.
2966 */
2967 printk(KERN_ERR "ext4: previous I/O error to "
2968 "superblock detected for %s.\n", sb->s_id);
2969 clear_buffer_write_io_error(sbh);
2970 set_buffer_uptodate(sbh);
2971 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002972 es->s_wtime = cpu_to_le32(get_seconds());
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05002973 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2974 &EXT4_SB(sb)->s_freeblocks_counter));
2975 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2976 &EXT4_SB(sb)->s_freeinodes_counter));
2977
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978 BUFFER_TRACE(sbh, "marking dirty");
2979 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002980 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002981 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002982 if (buffer_write_io_error(sbh)) {
2983 printk(KERN_ERR "ext4: I/O error while writing "
2984 "superblock for %s.\n", sb->s_id);
2985 clear_buffer_write_io_error(sbh);
2986 set_buffer_uptodate(sbh);
2987 }
2988 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002989}
2990
2991
2992/*
2993 * Have we just finished recovery? If so, and if we are mounting (or
2994 * remounting) the filesystem readonly, then we will end up with a
2995 * consistent fs on disk. Record that fact.
2996 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002997static void ext4_mark_recovery_complete(struct super_block *sb,
2998 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002999{
Mingming Cao617ba132006-10-11 01:20:53 -07003000 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001
Frank Mayhar03901312009-01-07 00:06:22 -05003002 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3003 BUG_ON(journal != NULL);
3004 return;
3005 }
Mingming Caodab291a2006-10-11 01:21:01 -07003006 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003007 if (jbd2_journal_flush(journal) < 0)
3008 goto out;
3009
Jan Kara32c37732007-07-15 23:41:09 -07003010 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003011 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003012 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003013 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003015 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003016 }
Jan Kara32c37732007-07-15 23:41:09 -07003017 unlock_super(sb);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003018
3019out:
Mingming Caodab291a2006-10-11 01:21:01 -07003020 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021}
3022
3023/*
3024 * If we are mounting (or read-write remounting) a filesystem whose journal
3025 * has recorded an error from a previous lifetime, move that error to the
3026 * main filesystem now.
3027 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003028static void ext4_clear_journal_err(struct super_block *sb,
3029 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030{
3031 journal_t *journal;
3032 int j_errno;
3033 const char *errstr;
3034
Frank Mayhar03901312009-01-07 00:06:22 -05003035 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3036
Mingming Cao617ba132006-10-11 01:20:53 -07003037 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003038
3039 /*
3040 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003041 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003042 */
3043
Mingming Caodab291a2006-10-11 01:21:01 -07003044 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003045 if (j_errno) {
3046 char nbuf[16];
3047
Mingming Cao617ba132006-10-11 01:20:53 -07003048 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003049 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003050 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003051 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003052 "filesystem check.");
3053
Mingming Cao617ba132006-10-11 01:20:53 -07003054 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3055 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003056 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003057
Mingming Caodab291a2006-10-11 01:21:01 -07003058 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003059 }
3060}
3061
3062/*
3063 * Force the running and committing transactions to commit,
3064 * and wait on the commit.
3065 */
Mingming Cao617ba132006-10-11 01:20:53 -07003066int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003067{
3068 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003069 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070
3071 if (sb->s_flags & MS_RDONLY)
3072 return 0;
3073
Mingming Cao617ba132006-10-11 01:20:53 -07003074 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003075 if (journal) {
3076 sb->s_dirt = 0;
3077 ret = ext4_journal_force_commit(journal);
3078 }
3079
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003080 return ret;
3081}
3082
3083/*
Mingming Cao617ba132006-10-11 01:20:53 -07003084 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003085 * have to propagate any other updates to the superblock on disk at this
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003086 * point. (We can probably nuke this function altogether, and remove
3087 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003088 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003089static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090{
Frank Mayhar03901312009-01-07 00:06:22 -05003091 if (EXT4_SB(sb)->s_journal) {
3092 if (mutex_trylock(&sb->s_lock) != 0)
3093 BUG();
3094 sb->s_dirt = 0;
3095 } else {
3096 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3097 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003098}
3099
Mingming Cao617ba132006-10-11 01:20:53 -07003100static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003101{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003102 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003103
Theodore Ts'oede86cc2008-10-05 20:50:06 -04003104 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003105 sb->s_dirt = 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003106 if (EXT4_SB(sb)->s_journal) {
3107 if (wait)
3108 ret = ext4_force_commit(sb);
3109 else
3110 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3111 } else {
3112 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3113 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003114 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003115}
3116
3117/*
3118 * LVM calls this function before a (read-only) snapshot is created. This
3119 * gives us a chance to flush the journal completely and mark the fs clean.
3120 */
Mingming Cao617ba132006-10-11 01:20:53 -07003121static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122{
3123 sb->s_dirt = 0;
3124
3125 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003126 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003127
Frank Mayhar03901312009-01-07 00:06:22 -05003128 if (journal) {
3129 /* Now we set up the journal barrier. */
3130 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003131
Frank Mayhar03901312009-01-07 00:06:22 -05003132 /*
3133 * We don't want to clear needs_recovery flag when we
3134 * failed to flush the journal.
3135 */
3136 if (jbd2_journal_flush(journal) < 0)
3137 return;
3138 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003139
3140 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07003141 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3142 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143 }
3144}
3145
3146/*
3147 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3148 * flag here, even though the filesystem is not technically dirty yet.
3149 */
Mingming Cao617ba132006-10-11 01:20:53 -07003150static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003151{
Frank Mayhar03901312009-01-07 00:06:22 -05003152 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003153 lock_super(sb);
3154 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07003155 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3156 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003157 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07003158 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159 }
3160}
3161
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003162static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003164 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003165 struct ext4_sb_info *sbi = EXT4_SB(sb);
3166 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003168 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003169 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003170 int err;
3171#ifdef CONFIG_QUOTA
3172 int i;
3173#endif
3174
3175 /* Store the original options */
3176 old_sb_flags = sb->s_flags;
3177 old_opts.s_mount_opt = sbi->s_mount_opt;
3178 old_opts.s_resuid = sbi->s_resuid;
3179 old_opts.s_resgid = sbi->s_resgid;
3180 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003181 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3182 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183#ifdef CONFIG_QUOTA
3184 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3185 for (i = 0; i < MAXQUOTAS; i++)
3186 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3187#endif
3188
3189 /*
3190 * Allow the "check" option to be passed as a remount option.
3191 */
3192 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3193 err = -EINVAL;
3194 goto restore_opts;
3195 }
3196
Mingming Cao617ba132006-10-11 01:20:53 -07003197 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003198 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003199
3200 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003201 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003202
3203 es = sbi->s_es;
3204
Frank Mayhar03901312009-01-07 00:06:22 -05003205 if (sbi->s_journal)
3206 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003207
3208 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003209 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003210 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211 err = -EROFS;
3212 goto restore_opts;
3213 }
3214
3215 if (*flags & MS_RDONLY) {
3216 /*
3217 * First of all, the unconditional stuff we have to do
3218 * to disable replay of the journal when we next remount
3219 */
3220 sb->s_flags |= MS_RDONLY;
3221
3222 /*
3223 * OK, test if we are remounting a valid rw partition
3224 * readonly, and if so set the rdonly flag and then
3225 * mark the partition as valid again.
3226 */
Mingming Cao617ba132006-10-11 01:20:53 -07003227 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3228 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003229 es->s_state = cpu_to_le16(sbi->s_mount_state);
3230
Jan Kara32c37732007-07-15 23:41:09 -07003231 /*
3232 * We have to unlock super so that we can wait for
3233 * transactions.
3234 */
Frank Mayhar03901312009-01-07 00:06:22 -05003235 if (sbi->s_journal) {
3236 unlock_super(sb);
3237 ext4_mark_recovery_complete(sb, es);
3238 lock_super(sb);
3239 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003240 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003241 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003242 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3243 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3244 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003246 "optional features (%x).\n", sb->s_id,
3247 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3248 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003249 err = -EROFS;
3250 goto restore_opts;
3251 }
Eric Sandeenead65962007-02-10 01:46:08 -08003252
3253 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003254 * Make sure the group descriptor checksums
3255 * are sane. If they aren't, refuse to
3256 * remount r/w.
3257 */
3258 for (g = 0; g < sbi->s_groups_count; g++) {
3259 struct ext4_group_desc *gdp =
3260 ext4_get_group_desc(sb, g, NULL);
3261
3262 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3263 printk(KERN_ERR
3264 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003265 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003266 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3267 le16_to_cpu(gdp->bg_checksum));
3268 err = -EINVAL;
3269 goto restore_opts;
3270 }
3271 }
3272
3273 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003274 * If we have an unprocessed orphan list hanging
3275 * around from a previously readonly bdev mount,
3276 * require a full umount/remount for now.
3277 */
3278 if (es->s_last_orphan) {
3279 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3280 "remount RDWR because of unprocessed "
3281 "orphan inode list. Please "
3282 "umount/remount instead.\n",
3283 sb->s_id);
3284 err = -EINVAL;
3285 goto restore_opts;
3286 }
3287
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003288 /*
3289 * Mounting a RDONLY partition read-write, so reread
3290 * and store the current valid flag. (It may have
3291 * been changed by e2fsck since we originally mounted
3292 * the partition.)
3293 */
Frank Mayhar03901312009-01-07 00:06:22 -05003294 if (sbi->s_journal)
3295 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003297 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003299 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003300 sb->s_flags &= ~MS_RDONLY;
3301 }
3302 }
Frank Mayhar03901312009-01-07 00:06:22 -05003303 if (sbi->s_journal == NULL)
3304 ext4_commit_super(sb, es, 1);
3305
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003306#ifdef CONFIG_QUOTA
3307 /* Release old quota file names */
3308 for (i = 0; i < MAXQUOTAS; i++)
3309 if (old_opts.s_qf_names[i] &&
3310 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3311 kfree(old_opts.s_qf_names[i]);
3312#endif
3313 return 0;
3314restore_opts:
3315 sb->s_flags = old_sb_flags;
3316 sbi->s_mount_opt = old_opts.s_mount_opt;
3317 sbi->s_resuid = old_opts.s_resuid;
3318 sbi->s_resgid = old_opts.s_resgid;
3319 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003320 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3321 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003322#ifdef CONFIG_QUOTA
3323 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3324 for (i = 0; i < MAXQUOTAS; i++) {
3325 if (sbi->s_qf_names[i] &&
3326 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3327 kfree(sbi->s_qf_names[i]);
3328 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3329 }
3330#endif
3331 return err;
3332}
3333
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003334static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003335{
3336 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003337 struct ext4_sb_info *sbi = EXT4_SB(sb);
3338 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003339 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003340
Badari Pulavarty5e700302007-07-15 23:42:00 -07003341 if (test_opt(sb, MINIX_DF)) {
3342 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003343 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003344 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003345 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003346 smp_rmb();
3347
3348 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003349 * Compute the overhead (FS structures). This is constant
3350 * for a given filesystem unless the number of block groups
3351 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003352 */
3353
3354 /*
3355 * All of the blocks before first_data_block are
3356 * overhead
3357 */
3358 overhead = le32_to_cpu(es->s_first_data_block);
3359
3360 /*
3361 * Add the overhead attributed to the superblock and
3362 * block group descriptors. If the sparse superblocks
3363 * feature is turned on, then not all groups have this.
3364 */
3365 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003366 overhead += ext4_bg_has_super(sb, i) +
3367 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003368 cond_resched();
3369 }
3370
3371 /*
3372 * Every block group has an inode bitmap, a block
3373 * bitmap, and an inode table.
3374 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003375 overhead += ngroups * (2 + sbi->s_itb_per_group);
3376 sbi->s_overhead_last = overhead;
3377 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003378 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003379 }
3380
Mingming Cao617ba132006-10-11 01:20:53 -07003381 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003382 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003383 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003384 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3385 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003386 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003387 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3388 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003389 buf->f_bavail = 0;
3390 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003391 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003392 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003393 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003394 fsid = le64_to_cpup((void *)es->s_uuid) ^
3395 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3396 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3397 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003398 return 0;
3399}
3400
3401/* Helper function for writing quotas on sync - we need to start transaction before quota file
3402 * is locked for write. Otherwise the are possible deadlocks:
3403 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003404 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003405 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003406 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003407 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003408 *
3409 */
3410
3411#ifdef CONFIG_QUOTA
3412
3413static inline struct inode *dquot_to_inode(struct dquot *dquot)
3414{
3415 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3416}
3417
Mingming Cao617ba132006-10-11 01:20:53 -07003418static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003419{
3420 handle_t *handle;
3421 int ret, err;
3422
3423 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003424 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003425 if (IS_ERR(handle))
3426 return PTR_ERR(handle);
3427 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003428 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003429 if (!ret)
3430 ret = err;
3431 return ret;
3432}
3433
Mingming Cao617ba132006-10-11 01:20:53 -07003434static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003435{
3436 handle_t *handle;
3437 int ret, err;
3438
3439 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003440 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003441 if (IS_ERR(handle)) {
3442 /*
3443 * We call dquot_drop() anyway to at least release references
3444 * to quota structures so that umount does not hang.
3445 */
3446 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003447 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003448 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003450 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451 if (!ret)
3452 ret = err;
3453 return ret;
3454}
3455
Mingming Cao617ba132006-10-11 01:20:53 -07003456static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003457{
3458 int ret, err;
3459 handle_t *handle;
3460 struct inode *inode;
3461
3462 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003463 handle = ext4_journal_start(inode,
3464 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003465 if (IS_ERR(handle))
3466 return PTR_ERR(handle);
3467 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003468 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003469 if (!ret)
3470 ret = err;
3471 return ret;
3472}
3473
Mingming Cao617ba132006-10-11 01:20:53 -07003474static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003475{
3476 int ret, err;
3477 handle_t *handle;
3478
Mingming Cao617ba132006-10-11 01:20:53 -07003479 handle = ext4_journal_start(dquot_to_inode(dquot),
3480 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003481 if (IS_ERR(handle))
3482 return PTR_ERR(handle);
3483 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003484 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003485 if (!ret)
3486 ret = err;
3487 return ret;
3488}
3489
Mingming Cao617ba132006-10-11 01:20:53 -07003490static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003491{
3492 int ret, err;
3493 handle_t *handle;
3494
Mingming Cao617ba132006-10-11 01:20:53 -07003495 handle = ext4_journal_start(dquot_to_inode(dquot),
3496 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003497 if (IS_ERR(handle)) {
3498 /* Release dquot anyway to avoid endless cycle in dqput() */
3499 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003500 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003501 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003502 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003503 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003504 if (!ret)
3505 ret = err;
3506 return ret;
3507}
3508
Mingming Cao617ba132006-10-11 01:20:53 -07003509static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003510{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003511 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003512 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3513 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003514 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003515 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003516 } else {
3517 return dquot_mark_dquot_dirty(dquot);
3518 }
3519}
3520
Mingming Cao617ba132006-10-11 01:20:53 -07003521static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522{
3523 int ret, err;
3524 handle_t *handle;
3525
3526 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003527 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003528 if (IS_ERR(handle))
3529 return PTR_ERR(handle);
3530 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003531 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003532 if (!ret)
3533 ret = err;
3534 return ret;
3535}
3536
3537/*
3538 * Turn on quotas during mount time - we need to find
3539 * the quota file and such...
3540 */
Mingming Cao617ba132006-10-11 01:20:53 -07003541static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542{
Mingming Cao617ba132006-10-11 01:20:53 -07003543 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3544 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003545}
3546
3547/*
3548 * Standard function to be called on quota_on
3549 */
Mingming Cao617ba132006-10-11 01:20:53 -07003550static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003551 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003552{
3553 int err;
Al Viro82646132008-08-02 00:57:06 -04003554 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003555
3556 if (!test_opt(sb, QUOTA))
3557 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003558 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003559 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003560 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003561
Al Viro82646132008-08-02 00:57:06 -04003562 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003563 if (err)
3564 return err;
Jan Kara06235432008-05-13 19:11:51 -04003565
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003566 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003567 if (path.mnt->mnt_sb != sb) {
3568 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003569 return -EXDEV;
3570 }
Jan Kara06235432008-05-13 19:11:51 -04003571 /* Journaling quota? */
3572 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003573 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003574 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003575 printk(KERN_WARNING
3576 "EXT4-fs: Quota file not on filesystem root. "
3577 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003578 }
Jan Kara06235432008-05-13 19:11:51 -04003579
3580 /*
3581 * When we journal data on quota file, we have to flush journal to see
3582 * all updates to the file when we bypass pagecache...
3583 */
Frank Mayhar03901312009-01-07 00:06:22 -05003584 if (EXT4_SB(sb)->s_journal &&
3585 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003586 /*
3587 * We don't need to lock updates but journal_flush() could
3588 * otherwise be livelocked...
3589 */
3590 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003591 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003592 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003593 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003594 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003595 return err;
3596 }
Jan Kara06235432008-05-13 19:11:51 -04003597 }
3598
Al Viro82646132008-08-02 00:57:06 -04003599 err = vfs_quota_on_path(sb, type, format_id, &path);
3600 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003601 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003602}
3603
3604/* Read data from quotafile - avoid pagecache and such because we cannot afford
3605 * acquiring the locks... As quota files are never truncated and quota code
3606 * itself serializes the operations (and noone else should touch the files)
3607 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003608static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003609 size_t len, loff_t off)
3610{
3611 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003612 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003613 int err = 0;
3614 int offset = off & (sb->s_blocksize - 1);
3615 int tocopy;
3616 size_t toread;
3617 struct buffer_head *bh;
3618 loff_t i_size = i_size_read(inode);
3619
3620 if (off > i_size)
3621 return 0;
3622 if (off+len > i_size)
3623 len = i_size-off;
3624 toread = len;
3625 while (toread > 0) {
3626 tocopy = sb->s_blocksize - offset < toread ?
3627 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003628 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003629 if (err)
3630 return err;
3631 if (!bh) /* A hole? */
3632 memset(data, 0, tocopy);
3633 else
3634 memcpy(data, bh->b_data+offset, tocopy);
3635 brelse(bh);
3636 offset = 0;
3637 toread -= tocopy;
3638 data += tocopy;
3639 blk++;
3640 }
3641 return len;
3642}
3643
3644/* Write to quotafile (we know the transaction is already started and has
3645 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003646static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003647 const char *data, size_t len, loff_t off)
3648{
3649 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003650 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003651 int err = 0;
3652 int offset = off & (sb->s_blocksize - 1);
3653 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003654 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003655 size_t towrite = len;
3656 struct buffer_head *bh;
3657 handle_t *handle = journal_current_handle();
3658
Frank Mayhar03901312009-01-07 00:06:22 -05003659 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003660 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003661 " cancelled because transaction is not started.\n",
3662 (unsigned long long)off, (unsigned long long)len);
3663 return -EIO;
3664 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003665 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3666 while (towrite > 0) {
3667 tocopy = sb->s_blocksize - offset < towrite ?
3668 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003669 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003670 if (!bh)
3671 goto out;
3672 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003673 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003674 if (err) {
3675 brelse(bh);
3676 goto out;
3677 }
3678 }
3679 lock_buffer(bh);
3680 memcpy(bh->b_data+offset, data, tocopy);
3681 flush_dcache_page(bh->b_page);
3682 unlock_buffer(bh);
3683 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003684 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685 else {
3686 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003687 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003688 mark_buffer_dirty(bh);
3689 }
3690 brelse(bh);
3691 if (err)
3692 goto out;
3693 offset = 0;
3694 towrite -= tocopy;
3695 data += tocopy;
3696 blk++;
3697 }
3698out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003699 if (len == towrite) {
3700 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003701 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003702 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003703 if (inode->i_size < off+len-towrite) {
3704 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003705 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003706 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003707 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003708 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003709 mutex_unlock(&inode->i_mutex);
3710 return len - towrite;
3711}
3712
3713#endif
3714
Mingming Cao617ba132006-10-11 01:20:53 -07003715static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003716 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3717{
Mingming Cao617ba132006-10-11 01:20:53 -07003718 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003719}
3720
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003721#ifdef CONFIG_PROC_FS
3722static int ext4_ui_proc_show(struct seq_file *m, void *v)
3723{
3724 unsigned int *p = m->private;
3725
3726 seq_printf(m, "%u\n", *p);
3727 return 0;
3728}
3729
3730static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3731{
3732 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3733}
3734
3735static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3736 size_t cnt, loff_t *ppos)
3737{
Roel Kluin23475e22008-11-26 02:23:19 -05003738 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003739 char str[32];
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003740
3741 if (cnt >= sizeof(str))
3742 return -EINVAL;
3743 if (copy_from_user(str, buf, cnt))
3744 return -EFAULT;
Roel Kluin23475e22008-11-26 02:23:19 -05003745
3746 *p = simple_strtoul(str, NULL, 0);
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003747 return cnt;
3748}
3749
3750const struct file_operations ext4_ui_proc_fops = {
3751 .owner = THIS_MODULE,
3752 .open = ext4_ui_proc_open,
3753 .read = seq_read,
3754 .llseek = seq_lseek,
3755 .release = single_release,
3756 .write = ext4_ui_proc_write,
3757};
3758#endif
3759
Theodore Ts'o03010a32008-10-10 20:02:48 -04003760static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003761 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003762 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003763 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003764 .kill_sb = kill_block_super,
3765 .fs_flags = FS_REQUIRES_DEV,
3766};
3767
Theodore Ts'o03010a32008-10-10 20:02:48 -04003768#ifdef CONFIG_EXT4DEV_COMPAT
3769static int ext4dev_get_sb(struct file_system_type *fs_type,
3770 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3771{
3772 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3773 "to mount using ext4\n");
3774 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3775 "will go away by 2.6.31\n");
3776 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3777}
3778
3779static struct file_system_type ext4dev_fs_type = {
3780 .owner = THIS_MODULE,
3781 .name = "ext4dev",
3782 .get_sb = ext4dev_get_sb,
3783 .kill_sb = kill_block_super,
3784 .fs_flags = FS_REQUIRES_DEV,
3785};
3786MODULE_ALIAS("ext4dev");
3787#endif
3788
Mingming Cao617ba132006-10-11 01:20:53 -07003789static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003790{
Alex Tomasc9de5602008-01-29 00:19:52 -05003791 int err;
3792
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003793 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003794 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003795 if (err)
3796 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003797
3798 err = init_ext4_xattr();
3799 if (err)
3800 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003801 err = init_inodecache();
3802 if (err)
3803 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003804 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003805 if (err)
3806 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003807#ifdef CONFIG_EXT4DEV_COMPAT
3808 err = register_filesystem(&ext4dev_fs_type);
3809 if (err) {
3810 unregister_filesystem(&ext4_fs_type);
3811 goto out;
3812 }
3813#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003814 return 0;
3815out:
3816 destroy_inodecache();
3817out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003818 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003819out2:
3820 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003821 return err;
3822}
3823
Mingming Cao617ba132006-10-11 01:20:53 -07003824static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003826 unregister_filesystem(&ext4_fs_type);
3827#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003828 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003829#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003830 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003831 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003832 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003833 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834}
3835
3836MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003837MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003838MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003839module_init(init_ext4_fs)
3840module_exit(exit_ext4_fs)