blob: e5ab520724da47fe2dbda0d5f5f4d7eaeedc5df7 [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
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500910/*
911 * Try to release metadata pages (indirect blocks, directories) which are
912 * mapped via the block device. Since these pages could have journal heads
913 * which would prevent try_to_free_buffers() from freeing them, we must use
914 * jbd2 layer's try_to_free_buffers() function to release them.
915 */
916static int bdev_try_to_free_page(struct super_block *sb, struct page *page, gfp_t wait)
917{
918 journal_t *journal = EXT4_SB(sb)->s_journal;
919
920 WARN_ON(PageChecked(page));
921 if (!page_has_buffers(page))
922 return 0;
923 if (journal)
924 return jbd2_journal_try_to_free_buffers(journal, page,
925 wait & ~__GFP_WAIT);
926 return try_to_free_buffers(page);
927}
928
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700929#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400930#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400931#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932
Mingming Cao617ba132006-10-11 01:20:53 -0700933static int ext4_dquot_initialize(struct inode *inode, int type);
934static int ext4_dquot_drop(struct inode *inode);
935static int ext4_write_dquot(struct dquot *dquot);
936static int ext4_acquire_dquot(struct dquot *dquot);
937static int ext4_release_dquot(struct dquot *dquot);
938static int ext4_mark_dquot_dirty(struct dquot *dquot);
939static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700940static int ext4_quota_on(struct super_block *sb, int type, int format_id,
941 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700942static int ext4_quota_on_mount(struct super_block *sb, int type);
943static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700944 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700945static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700946 const char *data, size_t len, loff_t off);
947
Mingming Cao617ba132006-10-11 01:20:53 -0700948static struct dquot_operations ext4_quota_operations = {
949 .initialize = ext4_dquot_initialize,
950 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700951 .alloc_space = dquot_alloc_space,
952 .alloc_inode = dquot_alloc_inode,
953 .free_space = dquot_free_space,
954 .free_inode = dquot_free_inode,
955 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700956 .write_dquot = ext4_write_dquot,
957 .acquire_dquot = ext4_acquire_dquot,
958 .release_dquot = ext4_release_dquot,
959 .mark_dirty = ext4_mark_dquot_dirty,
960 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700961};
962
Mingming Cao617ba132006-10-11 01:20:53 -0700963static struct quotactl_ops ext4_qctl_operations = {
964 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 .quota_off = vfs_quota_off,
966 .quota_sync = vfs_quota_sync,
967 .get_info = vfs_get_dqinfo,
968 .set_info = vfs_set_dqinfo,
969 .get_dqblk = vfs_get_dqblk,
970 .set_dqblk = vfs_set_dqblk
971};
972#endif
973
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800974static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700975 .alloc_inode = ext4_alloc_inode,
976 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700977 .write_inode = ext4_write_inode,
978 .dirty_inode = ext4_dirty_inode,
979 .delete_inode = ext4_delete_inode,
980 .put_super = ext4_put_super,
981 .write_super = ext4_write_super,
982 .sync_fs = ext4_sync_fs,
983 .write_super_lockfs = ext4_write_super_lockfs,
984 .unlockfs = ext4_unlockfs,
985 .statfs = ext4_statfs,
986 .remount_fs = ext4_remount,
987 .clear_inode = ext4_clear_inode,
988 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700989#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700990 .quota_read = ext4_quota_read,
991 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700992#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -0500993 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700994};
995
Christoph Hellwig39655162007-10-21 16:42:17 -0700996static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700997 .fh_to_dentry = ext4_fh_to_dentry,
998 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700999 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001000};
1001
1002enum {
1003 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1004 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001005 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1007 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
Theodore Ts'o30773842009-01-03 20:27:38 -05001008 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
1009 Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -05001010 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001012 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1014 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
1015 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001016 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -04001017 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -04001018 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001019};
1020
Steven Whitehousea447c092008-10-13 10:46:57 +01001021static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001022 {Opt_bsd_df, "bsddf"},
1023 {Opt_minix_df, "minixdf"},
1024 {Opt_grpid, "grpid"},
1025 {Opt_grpid, "bsdgroups"},
1026 {Opt_nogrpid, "nogrpid"},
1027 {Opt_nogrpid, "sysvgroups"},
1028 {Opt_resgid, "resgid=%u"},
1029 {Opt_resuid, "resuid=%u"},
1030 {Opt_sb, "sb=%u"},
1031 {Opt_err_cont, "errors=continue"},
1032 {Opt_err_panic, "errors=panic"},
1033 {Opt_err_ro, "errors=remount-ro"},
1034 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 {Opt_debug, "debug"},
1036 {Opt_oldalloc, "oldalloc"},
1037 {Opt_orlov, "orlov"},
1038 {Opt_user_xattr, "user_xattr"},
1039 {Opt_nouser_xattr, "nouser_xattr"},
1040 {Opt_acl, "acl"},
1041 {Opt_noacl, "noacl"},
1042 {Opt_reservation, "reservation"},
1043 {Opt_noreservation, "noreservation"},
1044 {Opt_noload, "noload"},
1045 {Opt_nobh, "nobh"},
1046 {Opt_bh, "bh"},
1047 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001048 {Opt_min_batch_time, "min_batch_time=%u"},
1049 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 {Opt_journal_update, "journal=update"},
1051 {Opt_journal_inum, "journal=%u"},
1052 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001053 {Opt_journal_checksum, "journal_checksum"},
1054 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001055 {Opt_abort, "abort"},
1056 {Opt_data_journal, "data=journal"},
1057 {Opt_data_ordered, "data=ordered"},
1058 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001059 {Opt_data_err_abort, "data_err=abort"},
1060 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 {Opt_offusrjquota, "usrjquota="},
1062 {Opt_usrjquota, "usrjquota=%s"},
1063 {Opt_offgrpjquota, "grpjquota="},
1064 {Opt_grpjquota, "grpjquota=%s"},
1065 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1066 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1067 {Opt_grpquota, "grpquota"},
1068 {Opt_noquota, "noquota"},
1069 {Opt_quota, "quota"},
1070 {Opt_usrquota, "usrquota"},
1071 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -07001072 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -04001073 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001074 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001075 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -04001077 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001078 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001079 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001080 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001081};
1082
Mingming Cao617ba132006-10-11 01:20:53 -07001083static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084{
Mingming Cao617ba132006-10-11 01:20:53 -07001085 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 char *options = (char *) *data;
1087
1088 if (!options || strncmp(options, "sb=", 3) != 0)
1089 return 1; /* Default location */
1090 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -07001091 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001092 sb_block = simple_strtoul(options, &options, 0);
1093 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001094 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095 (char *) *data);
1096 return 1;
1097 }
1098 if (*options == ',')
1099 options++;
1100 *data = (void *) options;
1101 return sb_block;
1102}
1103
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001104static int parse_options(char *options, struct super_block *sb,
1105 unsigned int *inum, unsigned long *journal_devnum,
1106 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001107{
Mingming Cao617ba132006-10-11 01:20:53 -07001108 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001109 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001110 substring_t args[MAX_OPT_ARGS];
1111 int data_opt = 0;
1112 int option;
1113#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001114 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 char *qname;
1116#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001117 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118
1119 if (!options)
1120 return 1;
1121
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001122 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 int token;
1124 if (!*p)
1125 continue;
1126
1127 token = match_token(p, tokens, args);
1128 switch (token) {
1129 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001130 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001131 break;
1132 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001133 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134 break;
1135 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001136 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001137 break;
1138 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001139 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001140 break;
1141 case Opt_resuid:
1142 if (match_int(&args[0], &option))
1143 return 0;
1144 sbi->s_resuid = option;
1145 break;
1146 case Opt_resgid:
1147 if (match_int(&args[0], &option))
1148 return 0;
1149 sbi->s_resgid = option;
1150 break;
1151 case Opt_sb:
1152 /* handled by get_sb_block() instead of here */
1153 /* *sb_block = match_int(&args[0]); */
1154 break;
1155 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001156 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1157 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1158 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 break;
1160 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001161 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1162 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1163 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 break;
1165 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001166 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1167 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1168 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001169 break;
1170 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001171 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001172 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001174 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001175 break;
1176 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001177 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178 break;
1179 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001180 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001181 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001182#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001184 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185 break;
1186 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001187 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 break;
1189#else
1190 case Opt_user_xattr:
1191 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001192 printk(KERN_ERR "EXT4 (no)user_xattr options "
1193 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001194 break;
1195#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001196#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 case Opt_acl:
1198 set_opt(sbi->s_mount_opt, POSIX_ACL);
1199 break;
1200 case Opt_noacl:
1201 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1202 break;
1203#else
1204 case Opt_acl:
1205 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001206 printk(KERN_ERR "EXT4 (no)acl options "
1207 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 break;
1209#endif
1210 case Opt_reservation:
1211 set_opt(sbi->s_mount_opt, RESERVATION);
1212 break;
1213 case Opt_noreservation:
1214 clear_opt(sbi->s_mount_opt, RESERVATION);
1215 break;
1216 case Opt_journal_update:
1217 /* @@@ FIXME */
1218 /* Eventually we will want to be able to create
1219 a journal file here. For now, only allow the
1220 user to specify an existing inode to be the
1221 journal file. */
1222 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001223 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224 "journal on remount\n");
1225 return 0;
1226 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001227 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001228 break;
1229 case Opt_journal_inum:
1230 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001231 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001232 "journal on remount\n");
1233 return 0;
1234 }
1235 if (match_int(&args[0], &option))
1236 return 0;
1237 *inum = option;
1238 break;
1239 case Opt_journal_dev:
1240 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001241 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001242 "journal on remount\n");
1243 return 0;
1244 }
1245 if (match_int(&args[0], &option))
1246 return 0;
1247 *journal_devnum = option;
1248 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001249 case Opt_journal_checksum:
1250 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1251 break;
1252 case Opt_journal_async_commit:
1253 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1254 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1255 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001256 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001257 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 break;
1259 case Opt_commit:
1260 if (match_int(&args[0], &option))
1261 return 0;
1262 if (option < 0)
1263 return 0;
1264 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001265 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 sbi->s_commit_interval = HZ * option;
1267 break;
Theodore Ts'o30773842009-01-03 20:27:38 -05001268 case Opt_max_batch_time:
1269 if (match_int(&args[0], &option))
1270 return 0;
1271 if (option < 0)
1272 return 0;
1273 if (option == 0)
1274 option = EXT4_DEF_MAX_BATCH_TIME;
1275 sbi->s_max_batch_time = option;
1276 break;
1277 case Opt_min_batch_time:
1278 if (match_int(&args[0], &option))
1279 return 0;
1280 if (option < 0)
1281 return 0;
1282 sbi->s_min_batch_time = option;
1283 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001284 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001285 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001286 goto datacheck;
1287 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001288 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001289 goto datacheck;
1290 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001291 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292 datacheck:
1293 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001294 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001295 != data_opt) {
1296 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001297 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001298 "mode on remount\n");
1299 return 0;
1300 }
1301 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001302 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001303 sbi->s_mount_opt |= data_opt;
1304 }
1305 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001306 case Opt_data_err_abort:
1307 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1308 break;
1309 case Opt_data_err_ignore:
1310 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1311 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001312#ifdef CONFIG_QUOTA
1313 case Opt_usrjquota:
1314 qtype = USRQUOTA;
1315 goto set_qf_name;
1316 case Opt_grpjquota:
1317 qtype = GRPQUOTA;
1318set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001319 if ((sb_any_quota_enabled(sb) ||
1320 sb_any_quota_suspended(sb)) &&
1321 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001322 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001323 "EXT4-fs: Cannot change journaled "
1324 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001325 return 0;
1326 }
1327 qname = match_strdup(&args[0]);
1328 if (!qname) {
1329 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001330 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 "storing quotafile name.\n");
1332 return 0;
1333 }
1334 if (sbi->s_qf_names[qtype] &&
1335 strcmp(sbi->s_qf_names[qtype], qname)) {
1336 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001337 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001338 "specified.\n", QTYPE2NAME(qtype));
1339 kfree(qname);
1340 return 0;
1341 }
1342 sbi->s_qf_names[qtype] = qname;
1343 if (strchr(sbi->s_qf_names[qtype], '/')) {
1344 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001345 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001346 "filesystem root.\n");
1347 kfree(sbi->s_qf_names[qtype]);
1348 sbi->s_qf_names[qtype] = NULL;
1349 return 0;
1350 }
1351 set_opt(sbi->s_mount_opt, QUOTA);
1352 break;
1353 case Opt_offusrjquota:
1354 qtype = USRQUOTA;
1355 goto clear_qf_name;
1356 case Opt_offgrpjquota:
1357 qtype = GRPQUOTA;
1358clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001359 if ((sb_any_quota_enabled(sb) ||
1360 sb_any_quota_suspended(sb)) &&
1361 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001362 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001363 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364 "quota turned on.\n");
1365 return 0;
1366 }
1367 /*
1368 * The space will be released later when all options
1369 * are confirmed to be correct
1370 */
1371 sbi->s_qf_names[qtype] = NULL;
1372 break;
1373 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001374 qfmt = QFMT_VFS_OLD;
1375 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001377 qfmt = QFMT_VFS_V0;
1378set_qf_format:
1379 if ((sb_any_quota_enabled(sb) ||
1380 sb_any_quota_suspended(sb)) &&
1381 sbi->s_jquota_fmt != qfmt) {
1382 printk(KERN_ERR "EXT4-fs: Cannot change "
1383 "journaled quota options when "
1384 "quota turned on.\n");
1385 return 0;
1386 }
1387 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001388 break;
1389 case Opt_quota:
1390 case Opt_usrquota:
1391 set_opt(sbi->s_mount_opt, QUOTA);
1392 set_opt(sbi->s_mount_opt, USRQUOTA);
1393 break;
1394 case Opt_grpquota:
1395 set_opt(sbi->s_mount_opt, QUOTA);
1396 set_opt(sbi->s_mount_opt, GRPQUOTA);
1397 break;
1398 case Opt_noquota:
1399 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001400 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 "options when quota turned on.\n");
1402 return 0;
1403 }
1404 clear_opt(sbi->s_mount_opt, QUOTA);
1405 clear_opt(sbi->s_mount_opt, USRQUOTA);
1406 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1407 break;
1408#else
1409 case Opt_quota:
1410 case Opt_usrquota:
1411 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001412 printk(KERN_ERR
1413 "EXT4-fs: quota options not supported.\n");
1414 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 case Opt_usrjquota:
1416 case Opt_grpjquota:
1417 case Opt_offusrjquota:
1418 case Opt_offgrpjquota:
1419 case Opt_jqfmt_vfsold:
1420 case Opt_jqfmt_vfsv0:
1421 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001422 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 "supported.\n");
1424 break;
1425 case Opt_noquota:
1426 break;
1427#endif
1428 case Opt_abort:
1429 set_opt(sbi->s_mount_opt, ABORT);
1430 break;
1431 case Opt_barrier:
1432 if (match_int(&args[0], &option))
1433 return 0;
1434 if (option)
1435 set_opt(sbi->s_mount_opt, BARRIER);
1436 else
1437 clear_opt(sbi->s_mount_opt, BARRIER);
1438 break;
1439 case Opt_ignore:
1440 break;
1441 case Opt_resize:
1442 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001443 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001444 "for remount\n");
1445 return 0;
1446 }
1447 if (match_int(&args[0], &option) != 0)
1448 return 0;
1449 *n_blocks_count = option;
1450 break;
1451 case Opt_nobh:
1452 set_opt(sbi->s_mount_opt, NOBH);
1453 break;
1454 case Opt_bh:
1455 clear_opt(sbi->s_mount_opt, NOBH);
1456 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001457 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001458 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1459 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1460 ext4_warning(sb, __func__,
1461 "extents feature not enabled "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05001462 "on this filesystem, use tune2fs");
Eric Sandeene4079a12008-07-11 19:27:31 -04001463 return 0;
1464 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001465 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001466 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001467 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001468 /*
1469 * When e2fsprogs support resizing an already existing
1470 * ext3 file system to greater than 2**32 we need to
1471 * add support to block allocator to handle growing
1472 * already existing block mapped inode so that blocks
1473 * allocated for them fall within 2**32
1474 */
1475 last_block = ext4_blocks_count(sbi->s_es) - 1;
1476 if (last_block > 0xffffffffULL) {
1477 printk(KERN_ERR "EXT4-fs: Filesystem too "
1478 "large to mount with "
1479 "-o noextents options\n");
1480 return 0;
1481 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001482 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001483 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001484 case Opt_i_version:
1485 set_opt(sbi->s_mount_opt, I_VERSION);
1486 sb->s_flags |= MS_I_VERSION;
1487 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001488 case Opt_nodelalloc:
1489 clear_opt(sbi->s_mount_opt, DELALLOC);
1490 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001491 case Opt_stripe:
1492 if (match_int(&args[0], &option))
1493 return 0;
1494 if (option < 0)
1495 return 0;
1496 sbi->s_stripe = option;
1497 break;
Alex Tomas64769242008-07-11 19:27:31 -04001498 case Opt_delalloc:
1499 set_opt(sbi->s_mount_opt, DELALLOC);
1500 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001501 case Opt_inode_readahead_blks:
1502 if (match_int(&args[0], &option))
1503 return 0;
1504 if (option < 0 || option > (1 << 30))
1505 return 0;
1506 sbi->s_inode_readahead_blks = option;
1507 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001508 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001509 printk(KERN_ERR
1510 "EXT4-fs: Unrecognized mount option \"%s\" "
1511 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512 return 0;
1513 }
1514 }
1515#ifdef CONFIG_QUOTA
1516 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001517 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001518 sbi->s_qf_names[USRQUOTA])
1519 clear_opt(sbi->s_mount_opt, USRQUOTA);
1520
Mingming Cao617ba132006-10-11 01:20:53 -07001521 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 sbi->s_qf_names[GRPQUOTA])
1523 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1524
1525 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001526 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001527 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001528 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1529 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001530 "format mixing.\n");
1531 return 0;
1532 }
1533
1534 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001535 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 "not specified.\n");
1537 return 0;
1538 }
1539 } else {
1540 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001541 printk(KERN_ERR "EXT4-fs: journaled quota format "
1542 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 "enabled.\n");
1544 return 0;
1545 }
1546 }
1547#endif
1548 return 1;
1549}
1550
Mingming Cao617ba132006-10-11 01:20:53 -07001551static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 int read_only)
1553{
Mingming Cao617ba132006-10-11 01:20:53 -07001554 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001555 int res = 0;
1556
Mingming Cao617ba132006-10-11 01:20:53 -07001557 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001558 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1559 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 res = MS_RDONLY;
1561 }
1562 if (read_only)
1563 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001564 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001565 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1566 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001567 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001568 printk(KERN_WARNING
1569 "EXT4-fs warning: mounting fs with errors, "
1570 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001571 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1572 le16_to_cpu(es->s_mnt_count) >=
1573 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001574 printk(KERN_WARNING
1575 "EXT4-fs warning: maximal mount count reached, "
1576 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001577 else if (le32_to_cpu(es->s_checkinterval) &&
1578 (le32_to_cpu(es->s_lastcheck) +
1579 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001580 printk(KERN_WARNING
1581 "EXT4-fs warning: checktime reached, "
1582 "running e2fsck is recommended\n");
Frank Mayhar03901312009-01-07 00:06:22 -05001583 if (!sbi->s_journal)
1584 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001586 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001587 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001588 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001589 ext4_update_dynamic_rev(sb);
Frank Mayhar03901312009-01-07 00:06:22 -05001590 if (sbi->s_journal)
1591 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001592
Mingming Cao617ba132006-10-11 01:20:53 -07001593 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001594 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001595 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1597 sb->s_blocksize,
1598 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001599 EXT4_BLOCKS_PER_GROUP(sb),
1600 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001601 sbi->s_mount_opt);
1602
Frank Mayhar03901312009-01-07 00:06:22 -05001603 if (EXT4_SB(sb)->s_journal) {
1604 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1605 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1606 "external", EXT4_SB(sb)->s_journal->j_devname);
1607 } else {
1608 printk(KERN_INFO "EXT4 FS on %s, no journal\n", sb->s_id);
1609 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 return res;
1611}
1612
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001613static int ext4_fill_flex_info(struct super_block *sb)
1614{
1615 struct ext4_sb_info *sbi = EXT4_SB(sb);
1616 struct ext4_group_desc *gdp = NULL;
1617 struct buffer_head *bh;
1618 ext4_group_t flex_group_count;
1619 ext4_group_t flex_group;
1620 int groups_per_flex = 0;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001621 int i;
1622
1623 if (!sbi->s_es->s_log_groups_per_flex) {
1624 sbi->s_log_groups_per_flex = 0;
1625 return 1;
1626 }
1627
1628 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1629 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1630
Frederic Bohec62a11f2008-09-08 10:20:24 -04001631 /* We allocate both existing and potentially added groups */
1632 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001633 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1634 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001635 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001636 sizeof(struct flex_groups), GFP_KERNEL);
1637 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001638 printk(KERN_ERR "EXT4-fs: not enough memory for "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001639 "%u flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001640 goto failed;
1641 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001642
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001643 for (i = 0; i < sbi->s_groups_count; i++) {
1644 gdp = ext4_get_group_desc(sb, i, &bh);
1645
1646 flex_group = ext4_flex_group(sbi, i);
1647 sbi->s_flex_groups[flex_group].free_inodes +=
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001648 ext4_free_inodes_count(sb, gdp);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001649 sbi->s_flex_groups[flex_group].free_blocks +=
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -05001650 ext4_free_blks_count(sb, gdp);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001651 }
1652
1653 return 1;
1654failed:
1655 return 0;
1656}
1657
Andreas Dilger717d50e2007-10-16 18:38:25 -04001658__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1659 struct ext4_group_desc *gdp)
1660{
1661 __u16 crc = 0;
1662
1663 if (sbi->s_es->s_feature_ro_compat &
1664 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1665 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1666 __le32 le_group = cpu_to_le32(block_group);
1667
1668 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1669 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1670 crc = crc16(crc, (__u8 *)gdp, offset);
1671 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1672 /* for checksum of struct ext4_group_desc do the rest...*/
1673 if ((sbi->s_es->s_feature_incompat &
1674 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1675 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1676 crc = crc16(crc, (__u8 *)gdp + offset,
1677 le16_to_cpu(sbi->s_es->s_desc_size) -
1678 offset);
1679 }
1680
1681 return cpu_to_le16(crc);
1682}
1683
1684int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1685 struct ext4_group_desc *gdp)
1686{
1687 if ((sbi->s_es->s_feature_ro_compat &
1688 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1689 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1690 return 0;
1691
1692 return 1;
1693}
1694
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001695/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001696static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001697{
Mingming Cao617ba132006-10-11 01:20:53 -07001698 struct ext4_sb_info *sbi = EXT4_SB(sb);
1699 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1700 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001701 ext4_fsblk_t block_bitmap;
1702 ext4_fsblk_t inode_bitmap;
1703 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001704 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001705 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706
Jose R. Santosce421582007-10-16 18:38:25 -04001707 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1708 flexbg_flag = 1;
1709
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001710 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001712 for (i = 0; i < sbi->s_groups_count; i++) {
1713 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1714
Jose R. Santosce421582007-10-16 18:38:25 -04001715 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001716 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001717 else
1718 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001719 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001720
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001721 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001722 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001723 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001724 "Block bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001725 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001726 return 0;
1727 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001728 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001729 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001730 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001731 "Inode bitmap for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001732 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733 return 0;
1734 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001735 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001736 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001737 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001738 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001739 "Inode table for group %u not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001740 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 return 0;
1742 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001743 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001744 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001745 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05001746 "Checksum for group %u failed (%u!=%u)\n",
Josef Bacikc19204b2008-04-29 22:00:28 -04001747 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1748 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001749 if (!(sb->s_flags & MS_RDONLY)) {
1750 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001751 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001752 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001753 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001754 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001755 if (!flexbg_flag)
1756 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001757 }
1758
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001759 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001760 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001761 return 1;
1762}
1763
Mingming Cao617ba132006-10-11 01:20:53 -07001764/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001765 * the superblock) which were deleted from all directories, but held open by
1766 * a process at the time of a crash. We walk the list and try to delete these
1767 * inodes at recovery time (only with a read-write filesystem).
1768 *
1769 * In order to keep the orphan inode chain consistent during traversal (in
1770 * case of crash during recovery), we link each inode into the superblock
1771 * orphan list_head and handle it the same way as an inode deletion during
1772 * normal operation (which journals the operations for us).
1773 *
1774 * We only do an iget() and an iput() on each inode, which is very safe if we
1775 * accidentally point at an in-use or already deleted inode. The worst that
1776 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001777 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001778 * e2fsck was run on this filesystem, and it must have already done the orphan
1779 * inode cleanup for us, so we can safely abort without any further action.
1780 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001781static void ext4_orphan_cleanup(struct super_block *sb,
1782 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783{
1784 unsigned int s_flags = sb->s_flags;
1785 int nr_orphans = 0, nr_truncates = 0;
1786#ifdef CONFIG_QUOTA
1787 int i;
1788#endif
1789 if (!es->s_last_orphan) {
1790 jbd_debug(4, "no orphan inodes to clean up\n");
1791 return;
1792 }
1793
Eric Sandeena8f48a92006-12-06 20:40:13 -08001794 if (bdev_read_only(sb->s_bdev)) {
1795 printk(KERN_ERR "EXT4-fs: write access "
1796 "unavailable, skipping orphan cleanup.\n");
1797 return;
1798 }
1799
Mingming Cao617ba132006-10-11 01:20:53 -07001800 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801 if (es->s_last_orphan)
1802 jbd_debug(1, "Errors on filesystem, "
1803 "clearing orphan list.\n");
1804 es->s_last_orphan = 0;
1805 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1806 return;
1807 }
1808
1809 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001810 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001811 sb->s_id);
1812 sb->s_flags &= ~MS_RDONLY;
1813 }
1814#ifdef CONFIG_QUOTA
1815 /* Needed for iput() to work correctly and not trash data */
1816 sb->s_flags |= MS_ACTIVE;
1817 /* Turn on quotas so that they are updated correctly */
1818 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001819 if (EXT4_SB(sb)->s_qf_names[i]) {
1820 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821 if (ret < 0)
1822 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001823 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001824 "quota: error %d\n", ret);
1825 }
1826 }
1827#endif
1828
1829 while (es->s_last_orphan) {
1830 struct inode *inode;
1831
Josef Bacik97bd42b2008-04-29 22:04:56 -04001832 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1833 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 es->s_last_orphan = 0;
1835 break;
1836 }
1837
Mingming Cao617ba132006-10-11 01:20:53 -07001838 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001839 DQUOT_INIT(inode);
1840 if (inode->i_nlink) {
1841 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001842 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001843 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001844 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001845 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001846 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001847 nr_truncates++;
1848 } else {
1849 printk(KERN_DEBUG
1850 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001851 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001852 jbd_debug(2, "deleting unreferenced inode %lu\n",
1853 inode->i_ino);
1854 nr_orphans++;
1855 }
1856 iput(inode); /* The delete magic happens here! */
1857 }
1858
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001859#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001860
1861 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001862 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 sb->s_id, PLURAL(nr_orphans));
1864 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001865 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866 sb->s_id, PLURAL(nr_truncates));
1867#ifdef CONFIG_QUOTA
1868 /* Turn quotas off */
1869 for (i = 0; i < MAXQUOTAS; i++) {
1870 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001871 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 }
1873#endif
1874 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1875}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001876/*
1877 * Maximal extent format file size.
1878 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1879 * extent format containers, within a sector_t, and within i_blocks
1880 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1881 * so that won't be a limiting factor.
1882 *
1883 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1884 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001885static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001886{
1887 loff_t res;
1888 loff_t upper_limit = MAX_LFS_FILESIZE;
1889
1890 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001891 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001892 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001893 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001894 * i_block represent total blocks in 512 bytes
1895 * 32 == size of vfs inode i_blocks * 8
1896 */
1897 upper_limit = (1LL << 32) - 1;
1898
1899 /* total blocks in file system block size */
1900 upper_limit >>= (blkbits - 9);
1901 upper_limit <<= blkbits;
1902 }
1903
1904 /* 32-bit extent-start container, ee_block */
1905 res = 1LL << 32;
1906 res <<= blkbits;
1907 res -= 1;
1908
1909 /* Sanity check against vm- & vfs- imposed limits */
1910 if (res > upper_limit)
1911 res = upper_limit;
1912
1913 return res;
1914}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001915
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001916/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001917 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001918 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1919 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001921static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922{
Mingming Cao617ba132006-10-11 01:20:53 -07001923 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001924 int meta_blocks;
1925 loff_t upper_limit;
1926 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001927 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001928 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001929 * does not exceed 2^48 -1
1930 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1931 * total number of 512 bytes blocks of the file
1932 */
1933
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001934 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001935 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001936 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001937 * implies the inode i_block represent total blocks in
1938 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001939 */
1940 upper_limit = (1LL << 32) - 1;
1941
1942 /* total blocks in file system block size */
1943 upper_limit >>= (bits - 9);
1944
1945 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001946 /*
1947 * We use 48 bit ext4_inode i_blocks
1948 * With EXT4_HUGE_FILE_FL set the i_blocks
1949 * represent total number of blocks in
1950 * file system block size
1951 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001952 upper_limit = (1LL << 48) - 1;
1953
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001954 }
1955
1956 /* indirect blocks */
1957 meta_blocks = 1;
1958 /* double indirect blocks */
1959 meta_blocks += 1 + (1LL << (bits-2));
1960 /* tripple indirect blocks */
1961 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1962
1963 upper_limit -= meta_blocks;
1964 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001965
1966 res += 1LL << (bits-2);
1967 res += 1LL << (2*(bits-2));
1968 res += 1LL << (3*(bits-2));
1969 res <<= bits;
1970 if (res > upper_limit)
1971 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001972
1973 if (res > MAX_LFS_FILESIZE)
1974 res = MAX_LFS_FILESIZE;
1975
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001976 return res;
1977}
1978
Mingming Cao617ba132006-10-11 01:20:53 -07001979static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001980 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981{
Mingming Cao617ba132006-10-11 01:20:53 -07001982 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001983 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001984 int has_super = 0;
1985
1986 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1987
Mingming Cao617ba132006-10-11 01:20:53 -07001988 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001990 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001992 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001994 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995}
1996
Alex Tomasc9de5602008-01-29 00:19:52 -05001997/**
1998 * ext4_get_stripe_size: Get the stripe size.
1999 * @sbi: In memory super block info
2000 *
2001 * If we have specified it via mount option, then
2002 * use the mount option value. If the value specified at mount time is
2003 * greater than the blocks per group use the super block value.
2004 * If the super block value is greater than blocks per group return 0.
2005 * Allocator needs it be less than blocks per group.
2006 *
2007 */
2008static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2009{
2010 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2011 unsigned long stripe_width =
2012 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2013
2014 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2015 return sbi->s_stripe;
2016
2017 if (stripe_width <= sbi->s_blocks_per_group)
2018 return stripe_width;
2019
2020 if (stride <= sbi->s_blocks_per_group)
2021 return stride;
2022
2023 return 0;
2024}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002025
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002026static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04002027 __releases(kernel_lock)
2028 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05002029
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002030{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002031 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07002032 struct ext4_super_block *es = NULL;
2033 struct ext4_sb_info *sbi;
2034 ext4_fsblk_t block;
2035 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002036 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 unsigned long offset = 0;
2038 unsigned int journal_inum = 0;
2039 unsigned long journal_devnum = 0;
2040 unsigned long def_mount_opts;
2041 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002042 char *cp;
Frank Mayhar03901312009-01-07 00:06:22 -05002043 const char *descr;
David Howells1d1fe1e2008-02-07 00:15:37 -08002044 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002046 int db_count;
2047 int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002048 int needs_recovery, has_huge_files;
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002049 int features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002050 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07002051 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052
2053 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2054 if (!sbi)
2055 return -ENOMEM;
2056 sb->s_fs_info = sbi;
2057 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002058 sbi->s_resuid = EXT4_DEF_RESUID;
2059 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04002060 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07002061 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002062
2063 unlock_kernel();
2064
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002065 /* Cleanup superblock name */
2066 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2067 *cp = '!';
2068
Mingming Cao617ba132006-10-11 01:20:53 -07002069 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07002071 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002072 goto out_fail;
2073 }
2074
2075 /*
Mingming Cao617ba132006-10-11 01:20:53 -07002076 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 * block sizes. We need to calculate the offset from buffer start.
2078 */
Mingming Cao617ba132006-10-11 01:20:53 -07002079 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002080 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2081 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002082 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002083 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 }
2085
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002086 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002087 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 goto out_fail;
2089 }
2090 /*
2091 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07002092 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002093 */
Mingming Cao617ba132006-10-11 01:20:53 -07002094 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002095 sbi->s_es = es;
2096 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07002097 if (sb->s_magic != EXT4_SUPER_MAGIC)
2098 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002099
2100 /* Set defaults before we parse the mount options */
2101 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07002102 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002103 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07002104 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002105 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07002106 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002107 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04002108#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07002109 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002110 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002111#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04002112#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07002113 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002114 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08002115#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002116 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2117 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2118 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2119 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2120 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2121 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122
Mingming Cao617ba132006-10-11 01:20:53 -07002123 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002124 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002125 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002126 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002127 else
2128 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129
2130 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2131 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
Theodore Ts'o30773842009-01-03 20:27:38 -05002132 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2133 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2134 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135
2136 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04002137 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138
Mingming Cao1e2462f2007-07-18 09:00:55 -04002139 /*
2140 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04002141 * only if feature flag already set by mkfs or tune2fs.
2142 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04002143 */
Eric Sandeene4079a12008-07-11 19:27:31 -04002144 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2145 set_opt(sbi->s_mount_opt, EXTENTS);
2146 else
2147 ext4_warning(sb, __func__,
2148 "extents feature not enabled on this filesystem, "
Theodore Ts'ofde4d952009-01-05 22:17:35 -05002149 "use tune2fs.");
Mingming Cao1e2462f2007-07-18 09:00:55 -04002150
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002151 /*
2152 * enable delayed allocation by default
2153 * Use -o nodelalloc to turn it off
2154 */
2155 set_opt(sbi->s_mount_opt, DELALLOC);
2156
2157
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002158 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2159 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002160 goto failed_mount;
2161
2162 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002163 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164
Mingming Cao617ba132006-10-11 01:20:53 -07002165 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2166 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2167 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2168 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002169 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002170 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002171 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002172
2173 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002174 * Check feature flags regardless of the revision level, since we
2175 * previously didn't change the revision level when setting the flags,
2176 * so there is a chance incompat flags are set on a rev 0 filesystem.
2177 */
Mingming Cao617ba132006-10-11 01:20:53 -07002178 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002180 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002181 "unsupported optional features (%x).\n", sb->s_id,
2182 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2183 ~EXT4_FEATURE_INCOMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184 goto failed_mount;
2185 }
Mingming Cao617ba132006-10-11 01:20:53 -07002186 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002188 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05002189 "unsupported optional features (%x).\n", sb->s_id,
2190 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2191 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002192 goto failed_mount;
2193 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002194 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2195 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2196 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002197 /*
2198 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002199 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002200 */
2201 if (sizeof(root->i_blocks) < sizeof(u64) &&
2202 !(sb->s_flags & MS_RDONLY)) {
2203 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2204 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002205 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002206 goto failed_mount;
2207 }
2208 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002209 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2210
Mingming Cao617ba132006-10-11 01:20:53 -07002211 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2212 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002213 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002214 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002215 blocksize, sb->s_id);
2216 goto failed_mount;
2217 }
2218
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002219 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002220
2221 /* Validate the filesystem blocksize */
2222 if (!sb_set_blocksize(sb, blocksize)) {
2223 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2224 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002225 goto failed_mount;
2226 }
2227
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002228 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002229 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2230 offset = do_div(logical_sb_block, blocksize);
2231 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002232 if (!bh) {
2233 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002234 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002235 goto failed_mount;
2236 }
Mingming Cao617ba132006-10-11 01:20:53 -07002237 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002238 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002239 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002240 printk(KERN_ERR
2241 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 goto failed_mount;
2243 }
2244 }
2245
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002246 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2247 has_huge_files);
2248 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249
Mingming Cao617ba132006-10-11 01:20:53 -07002250 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2251 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2252 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 } else {
2254 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2255 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002256 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002257 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002258 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002259 printk(KERN_ERR
2260 "EXT4-fs: unsupported inode size: %d\n",
2261 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002262 goto failed_mount;
2263 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002264 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2265 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002266 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002267 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2268 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002269 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002270 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002271 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002272 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002273 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002274 sbi->s_desc_size);
2275 goto failed_mount;
2276 }
2277 } else
2278 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002279 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002281 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002282 goto cantfind_ext4;
2283 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002285 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2287 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002288 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289 sbi->s_sbh = bh;
2290 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002291 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2292 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002293 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002294 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2295 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'of99b2582008-10-28 13:21:44 -04002296 i = le32_to_cpu(es->s_flags);
2297 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2298 sbi->s_hash_unsigned = 3;
2299 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2300#ifdef __CHAR_UNSIGNED__
2301 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2302 sbi->s_hash_unsigned = 3;
2303#else
2304 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2305#endif
2306 sb->s_dirt = 1;
2307 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002308
2309 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002310 printk(KERN_ERR
2311 "EXT4-fs: #blocks per group too big: %lu\n",
2312 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313 goto failed_mount;
2314 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002315 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002316 printk(KERN_ERR
2317 "EXT4-fs: #inodes per group too big: %lu\n",
2318 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 goto failed_mount;
2320 }
2321
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002322 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002323 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002324 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002325 " too large to mount safely\n", sb->s_id);
2326 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002327 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002328 "enabled\n");
2329 goto failed_mount;
2330 }
2331
Mingming Cao617ba132006-10-11 01:20:53 -07002332 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2333 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002334
2335 /* ensure blocks_count calculation below doesn't sign-extend */
2336 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2337 le32_to_cpu(es->s_first_data_block) + 1) {
2338 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2339 "first data block %u, blocks per group %lu\n",
2340 ext4_blocks_count(es),
2341 le32_to_cpu(es->s_first_data_block),
2342 EXT4_BLOCKS_PER_GROUP(sb));
2343 goto failed_mount;
2344 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002345 blocks_count = (ext4_blocks_count(es) -
2346 le32_to_cpu(es->s_first_data_block) +
2347 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2348 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2349 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002350 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2351 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002352 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353 GFP_KERNEL);
2354 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002355 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002356 goto failed_mount;
2357 }
2358
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002359#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002360 if (ext4_proc_root)
2361 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2362
Theodore Ts'o240799c2008-10-09 23:53:47 -04002363 if (sbi->s_proc)
2364 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2365 &ext4_ui_proc_fops,
2366 &sbi->s_inode_readahead_blks);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002367#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002368
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002369 bgl_lock_init(&sbi->s_blockgroup_lock);
2370
2371 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002372 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002373 sbi->s_group_desc[i] = sb_bread(sb, block);
2374 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002375 printk(KERN_ERR "EXT4-fs: "
2376 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002377 db_count = i;
2378 goto failed_mount2;
2379 }
2380 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002381 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002382 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 goto failed_mount2;
2384 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002385 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2386 if (!ext4_fill_flex_info(sb)) {
2387 printk(KERN_ERR
2388 "EXT4-fs: unable to initialize "
2389 "flex_bg meta info!\n");
2390 goto failed_mount2;
2391 }
2392
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002393 sbi->s_gdb_count = db_count;
2394 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2395 spin_lock_init(&sbi->s_next_gen_lock);
2396
Peter Zijlstra833f4072007-10-16 23:25:45 -07002397 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2398 ext4_count_free_blocks(sb));
2399 if (!err) {
2400 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2401 ext4_count_free_inodes(sb));
2402 }
2403 if (!err) {
2404 err = percpu_counter_init(&sbi->s_dirs_counter,
2405 ext4_count_dirs(sb));
2406 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002407 if (!err) {
2408 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2409 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002410 if (err) {
2411 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2412 goto failed_mount3;
2413 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414
Alex Tomasc9de5602008-01-29 00:19:52 -05002415 sbi->s_stripe = ext4_get_stripe_size(sbi);
2416
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002417 /*
2418 * set up enough so that it can read an inode
2419 */
Mingming Cao617ba132006-10-11 01:20:53 -07002420 sb->s_op = &ext4_sops;
2421 sb->s_export_op = &ext4_export_ops;
2422 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002423#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002424 sb->s_qcop = &ext4_qctl_operations;
2425 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002426#endif
2427 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2428
2429 sb->s_root = NULL;
2430
2431 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002432 EXT4_HAS_INCOMPAT_FEATURE(sb,
2433 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002434
2435 /*
2436 * The first inode we look at is the journal inode. Don't try
2437 * root first: it may be modified in the journal!
2438 */
2439 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002440 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2441 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002443 if (!(sb->s_flags & MS_RDONLY) &&
2444 EXT4_SB(sb)->s_journal->j_failed_commit) {
2445 printk(KERN_CRIT "EXT4-fs error (device %s): "
2446 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002447 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002448 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002449 if (test_opt(sb, ERRORS_RO)) {
2450 printk(KERN_CRIT
2451 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002452 sb->s_flags |= MS_RDONLY;
2453 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2454 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2455 }
2456 if (test_opt(sb, ERRORS_PANIC)) {
2457 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2458 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2459 ext4_commit_super(sb, es, 1);
Theodore Ts'o624080e2008-06-06 17:50:40 -04002460 goto failed_mount4;
2461 }
2462 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002463 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002464 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002465 goto failed_mount3;
Frank Mayhar03901312009-01-07 00:06:22 -05002466 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2467 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
2468 printk(KERN_ERR "EXT4-fs: required journal recovery "
2469 "suppressed and not mounted read-only\n");
2470 goto failed_mount4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471 } else {
Frank Mayhar03901312009-01-07 00:06:22 -05002472 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2473 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2474 sbi->s_journal = NULL;
2475 needs_recovery = 0;
2476 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002477 }
2478
Jose R. Santoseb40a092007-07-18 08:37:25 -04002479 if (ext4_blocks_count(es) > 0xffffffffULL &&
2480 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2481 JBD2_FEATURE_INCOMPAT_64BIT)) {
2482 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2483 goto failed_mount4;
2484 }
2485
Girish Shilamkar818d2762008-01-28 23:58:27 -05002486 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2487 jbd2_journal_set_features(sbi->s_journal,
2488 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2489 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2490 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2491 jbd2_journal_set_features(sbi->s_journal,
2492 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2493 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2494 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2495 } else {
2496 jbd2_journal_clear_features(sbi->s_journal,
2497 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2498 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2499 }
2500
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002501 /* We have now updated the journal if required, so we can
2502 * validate the data journaling mode. */
2503 switch (test_opt(sb, DATA_FLAGS)) {
2504 case 0:
2505 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002506 * capabilities: ORDERED_DATA if the journal can
2507 * cope, else JOURNAL_DATA
2508 */
Mingming Caodab291a2006-10-11 01:21:01 -07002509 if (jbd2_journal_check_available_features
2510 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002511 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2512 else
2513 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2514 break;
2515
Mingming Cao617ba132006-10-11 01:20:53 -07002516 case EXT4_MOUNT_ORDERED_DATA:
2517 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002518 if (!jbd2_journal_check_available_features
2519 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002520 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002521 "requested data journaling mode\n");
2522 goto failed_mount4;
2523 }
2524 default:
2525 break;
2526 }
2527
Frank Mayhar03901312009-01-07 00:06:22 -05002528no_journal:
2529
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002530 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002531 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2532 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002533 "its supported only with writeback mode\n");
2534 clear_opt(sbi->s_mount_opt, NOBH);
2535 }
2536 }
2537 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002538 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002539 * so we can safely mount the rest of the filesystem now.
2540 */
2541
David Howells1d1fe1e2008-02-07 00:15:37 -08002542 root = ext4_iget(sb, EXT4_ROOT_INO);
2543 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002544 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002545 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 goto failed_mount4;
2547 }
2548 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002549 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002550 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002551 goto failed_mount4;
2552 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002553 sb->s_root = d_alloc_root(root);
2554 if (!sb->s_root) {
2555 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2556 iput(root);
2557 ret = -ENOMEM;
2558 goto failed_mount4;
2559 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002560
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002561 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002562
2563 /* determine the minimum size of new large inodes, if present */
2564 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2565 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2566 EXT4_GOOD_OLD_INODE_SIZE;
2567 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2568 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2569 if (sbi->s_want_extra_isize <
2570 le16_to_cpu(es->s_want_extra_isize))
2571 sbi->s_want_extra_isize =
2572 le16_to_cpu(es->s_want_extra_isize);
2573 if (sbi->s_want_extra_isize <
2574 le16_to_cpu(es->s_min_extra_isize))
2575 sbi->s_want_extra_isize =
2576 le16_to_cpu(es->s_min_extra_isize);
2577 }
2578 }
2579 /* Check if enough inode space is available */
2580 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2581 sbi->s_inode_size) {
2582 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2583 EXT4_GOOD_OLD_INODE_SIZE;
2584 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2585 "available.\n");
2586 }
2587
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002588 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2589 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2590 "requested data journaling mode\n");
2591 clear_opt(sbi->s_mount_opt, DELALLOC);
2592 } else if (test_opt(sb, DELALLOC))
2593 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2594
2595 ext4_ext_init(sb);
2596 err = ext4_mb_init(sb, needs_recovery);
2597 if (err) {
2598 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2599 err);
2600 goto failed_mount4;
2601 }
2602
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002603 /*
2604 * akpm: core read_super() calls in here with the superblock locked.
2605 * That deadlocks, because orphan cleanup needs to lock the superblock
2606 * in numerous places. Here we just pop the lock - it's relatively
2607 * harmless, because we are now ready to accept write_super() requests,
2608 * and aviro says that's the only reason for hanging onto the
2609 * superblock lock.
2610 */
Mingming Cao617ba132006-10-11 01:20:53 -07002611 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2612 ext4_orphan_cleanup(sb, es);
2613 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05002614 if (needs_recovery) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002615 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Frank Mayhar03901312009-01-07 00:06:22 -05002616 ext4_mark_recovery_complete(sb, es);
2617 }
2618 if (EXT4_SB(sb)->s_journal) {
2619 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2620 descr = " journalled data mode";
2621 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2622 descr = " ordered data mode";
2623 else
2624 descr = " writeback data mode";
2625 } else
2626 descr = "out journal";
2627
2628 printk(KERN_INFO "EXT4-fs: mounted filesystem %s with%s\n",
2629 sb->s_id, descr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002630
2631 lock_kernel();
2632 return 0;
2633
Mingming Cao617ba132006-10-11 01:20:53 -07002634cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002635 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002636 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002637 sb->s_id);
2638 goto failed_mount;
2639
2640failed_mount4:
Frank Mayhar03901312009-01-07 00:06:22 -05002641 printk(KERN_ERR "EXT4-fs (device %s): mount failed\n", sb->s_id);
2642 if (sbi->s_journal) {
2643 jbd2_journal_destroy(sbi->s_journal);
2644 sbi->s_journal = NULL;
2645 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002646failed_mount3:
2647 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2648 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2649 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002650 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002651failed_mount2:
2652 for (i = 0; i < db_count; i++)
2653 brelse(sbi->s_group_desc[i]);
2654 kfree(sbi->s_group_desc);
2655failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002656 if (sbi->s_proc) {
2657 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002658 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002659 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002660#ifdef CONFIG_QUOTA
2661 for (i = 0; i < MAXQUOTAS; i++)
2662 kfree(sbi->s_qf_names[i]);
2663#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002664 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002665 brelse(bh);
2666out_fail:
2667 sb->s_fs_info = NULL;
2668 kfree(sbi);
2669 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002670 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002671}
2672
2673/*
2674 * Setup any per-fs journal parameters now. We'll do this both on
2675 * initial mount, once the journal has been initialised but before we've
2676 * done any recovery; and again on any subsequent remount.
2677 */
Mingming Cao617ba132006-10-11 01:20:53 -07002678static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679{
Mingming Cao617ba132006-10-11 01:20:53 -07002680 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002681
Theodore Ts'o30773842009-01-03 20:27:38 -05002682 journal->j_commit_interval = sbi->s_commit_interval;
2683 journal->j_min_batch_time = sbi->s_min_batch_time;
2684 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685
2686 spin_lock(&journal->j_state_lock);
2687 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002688 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689 else
Mingming Caodab291a2006-10-11 01:21:01 -07002690 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002691 if (test_opt(sb, DATA_ERR_ABORT))
2692 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2693 else
2694 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002695 spin_unlock(&journal->j_state_lock);
2696}
2697
Mingming Cao617ba132006-10-11 01:20:53 -07002698static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002699 unsigned int journal_inum)
2700{
2701 struct inode *journal_inode;
2702 journal_t *journal;
2703
Frank Mayhar03901312009-01-07 00:06:22 -05002704 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2705
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706 /* First, test for the existence of a valid inode on disk. Bad
2707 * things happen if we iget() an unused inode, as the subsequent
2708 * iput() will try to delete it. */
2709
David Howells1d1fe1e2008-02-07 00:15:37 -08002710 journal_inode = ext4_iget(sb, journal_inum);
2711 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002712 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002713 return NULL;
2714 }
2715 if (!journal_inode->i_nlink) {
2716 make_bad_inode(journal_inode);
2717 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002718 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002719 return NULL;
2720 }
2721
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002722 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002723 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002724 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002725 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002726 iput(journal_inode);
2727 return NULL;
2728 }
2729
Mingming Caodab291a2006-10-11 01:21:01 -07002730 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002732 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002733 iput(journal_inode);
2734 return NULL;
2735 }
2736 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002737 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002738 return journal;
2739}
2740
Mingming Cao617ba132006-10-11 01:20:53 -07002741static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002742 dev_t j_dev)
2743{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002744 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002745 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002746 ext4_fsblk_t start;
2747 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002749 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002750 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002751 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 struct block_device *bdev;
2753
Frank Mayhar03901312009-01-07 00:06:22 -05002754 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2755
Mingming Cao617ba132006-10-11 01:20:53 -07002756 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757 if (bdev == NULL)
2758 return NULL;
2759
2760 if (bd_claim(bdev, sb)) {
2761 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002762 "EXT4: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002763 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764 return NULL;
2765 }
2766
2767 blocksize = sb->s_blocksize;
2768 hblock = bdev_hardsect_size(bdev);
2769 if (blocksize < hblock) {
2770 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002771 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772 goto out_bdev;
2773 }
2774
Mingming Cao617ba132006-10-11 01:20:53 -07002775 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2776 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002777 set_blocksize(bdev, blocksize);
2778 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002779 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002780 "external journal\n");
2781 goto out_bdev;
2782 }
2783
Mingming Cao617ba132006-10-11 01:20:53 -07002784 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2785 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002787 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2788 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002789 "bad superblock\n");
2790 brelse(bh);
2791 goto out_bdev;
2792 }
2793
Mingming Cao617ba132006-10-11 01:20:53 -07002794 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2795 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002796 brelse(bh);
2797 goto out_bdev;
2798 }
2799
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002800 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002801 start = sb_block + 1;
2802 brelse(bh); /* we're done with the superblock */
2803
Mingming Caodab291a2006-10-11 01:21:01 -07002804 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002805 start, len, blocksize);
2806 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002807 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002808 goto out_bdev;
2809 }
2810 journal->j_private = sb;
2811 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2812 wait_on_buffer(journal->j_sb_buffer);
2813 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002814 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815 goto out_journal;
2816 }
2817 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002818 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002819 "user (unsupported) - %d\n",
2820 be32_to_cpu(journal->j_superblock->s_nr_users));
2821 goto out_journal;
2822 }
Mingming Cao617ba132006-10-11 01:20:53 -07002823 EXT4_SB(sb)->journal_bdev = bdev;
2824 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002825 return journal;
2826out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002827 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002828out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002829 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002830 return NULL;
2831}
2832
Mingming Cao617ba132006-10-11 01:20:53 -07002833static int ext4_load_journal(struct super_block *sb,
2834 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002835 unsigned long journal_devnum)
2836{
2837 journal_t *journal;
2838 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2839 dev_t journal_dev;
2840 int err = 0;
2841 int really_read_only;
2842
Frank Mayhar03901312009-01-07 00:06:22 -05002843 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
2844
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002845 if (journal_devnum &&
2846 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002847 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002848 "numbers have changed\n");
2849 journal_dev = new_decode_dev(journal_devnum);
2850 } else
2851 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2852
2853 really_read_only = bdev_read_only(sb->s_bdev);
2854
2855 /*
2856 * Are we loading a blank journal or performing recovery after a
2857 * crash? For recovery, we need to check in advance whether we
2858 * can get read-write access to the device.
2859 */
2860
Mingming Cao617ba132006-10-11 01:20:53 -07002861 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002862 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002863 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864 "required on readonly filesystem.\n");
2865 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002866 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002867 "unavailable, cannot proceed.\n");
2868 return -EROFS;
2869 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002870 printk(KERN_INFO "EXT4-fs: write access will "
2871 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002872 }
2873 }
2874
2875 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002876 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002877 "and inode journals!\n");
2878 return -EINVAL;
2879 }
2880
2881 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002882 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883 return -EINVAL;
2884 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002885 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002886 return -EINVAL;
2887 }
2888
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002889 if (journal->j_flags & JBD2_BARRIER)
2890 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2891 else
2892 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2893
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002894 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002895 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002896 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002897 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002898 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002899 return err;
2900 }
2901 }
2902
Mingming Cao617ba132006-10-11 01:20:53 -07002903 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002904 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002906 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907
2908 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002909 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002910 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002911 return err;
2912 }
2913
Mingming Cao617ba132006-10-11 01:20:53 -07002914 EXT4_SB(sb)->s_journal = journal;
2915 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002916
2917 if (journal_devnum &&
2918 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2919 es->s_journal_dev = cpu_to_le32(journal_devnum);
2920 sb->s_dirt = 1;
2921
2922 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002923 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002924 }
2925
2926 return 0;
2927}
2928
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002929static int ext4_create_journal(struct super_block *sb,
2930 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931 unsigned int journal_inum)
2932{
2933 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002934 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002935
2936 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002937 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002938 "create journal.\n");
2939 return -EROFS;
2940 }
2941
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002942 journal = ext4_get_journal(sb, journal_inum);
2943 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002944 return -EINVAL;
2945
Mingming Cao617ba132006-10-11 01:20:53 -07002946 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 journal_inum);
2948
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002949 err = jbd2_journal_create(journal);
2950 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002951 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002952 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002953 return -EIO;
2954 }
2955
Mingming Cao617ba132006-10-11 01:20:53 -07002956 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002957
Mingming Cao617ba132006-10-11 01:20:53 -07002958 ext4_update_dynamic_rev(sb);
2959 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2960 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002961
2962 es->s_journal_inum = cpu_to_le32(journal_inum);
2963 sb->s_dirt = 1;
2964
2965 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002966 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002967
2968 return 0;
2969}
2970
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002971static void ext4_commit_super(struct super_block *sb,
2972 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002973{
Mingming Cao617ba132006-10-11 01:20:53 -07002974 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002975
2976 if (!sbh)
2977 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002978 if (buffer_write_io_error(sbh)) {
2979 /*
2980 * Oh, dear. A previous attempt to write the
2981 * superblock failed. This could happen because the
2982 * USB device was yanked out. Or it could happen to
2983 * be a transient write error and maybe the block will
2984 * be remapped. Nothing we can do but to retry the
2985 * write and hope for the best.
2986 */
2987 printk(KERN_ERR "ext4: previous I/O error to "
2988 "superblock detected for %s.\n", sb->s_id);
2989 clear_buffer_write_io_error(sbh);
2990 set_buffer_uptodate(sbh);
2991 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002992 es->s_wtime = cpu_to_le32(get_seconds());
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05002993 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
2994 &EXT4_SB(sb)->s_freeblocks_counter));
2995 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
2996 &EXT4_SB(sb)->s_freeinodes_counter));
2997
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 BUFFER_TRACE(sbh, "marking dirty");
2999 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003000 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04003002 if (buffer_write_io_error(sbh)) {
3003 printk(KERN_ERR "ext4: I/O error while writing "
3004 "superblock for %s.\n", sb->s_id);
3005 clear_buffer_write_io_error(sbh);
3006 set_buffer_uptodate(sbh);
3007 }
3008 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009}
3010
3011
3012/*
3013 * Have we just finished recovery? If so, and if we are mounting (or
3014 * remounting) the filesystem readonly, then we will end up with a
3015 * consistent fs on disk. Record that fact.
3016 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003017static void ext4_mark_recovery_complete(struct super_block *sb,
3018 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003019{
Mingming Cao617ba132006-10-11 01:20:53 -07003020 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003021
Frank Mayhar03901312009-01-07 00:06:22 -05003022 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3023 BUG_ON(journal != NULL);
3024 return;
3025 }
Mingming Caodab291a2006-10-11 01:21:01 -07003026 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003027 if (jbd2_journal_flush(journal) < 0)
3028 goto out;
3029
Jan Kara32c37732007-07-15 23:41:09 -07003030 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003031 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003032 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07003033 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003034 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07003035 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036 }
Jan Kara32c37732007-07-15 23:41:09 -07003037 unlock_super(sb);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003038
3039out:
Mingming Caodab291a2006-10-11 01:21:01 -07003040 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003041}
3042
3043/*
3044 * If we are mounting (or read-write remounting) a filesystem whose journal
3045 * has recorded an error from a previous lifetime, move that error to the
3046 * main filesystem now.
3047 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003048static void ext4_clear_journal_err(struct super_block *sb,
3049 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003050{
3051 journal_t *journal;
3052 int j_errno;
3053 const char *errstr;
3054
Frank Mayhar03901312009-01-07 00:06:22 -05003055 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3056
Mingming Cao617ba132006-10-11 01:20:53 -07003057 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003058
3059 /*
3060 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07003061 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062 */
3063
Mingming Caodab291a2006-10-11 01:21:01 -07003064 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 if (j_errno) {
3066 char nbuf[16];
3067
Mingming Cao617ba132006-10-11 01:20:53 -07003068 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003069 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04003071 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003072 "filesystem check.");
3073
Mingming Cao617ba132006-10-11 01:20:53 -07003074 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3075 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003076 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003077
Mingming Caodab291a2006-10-11 01:21:01 -07003078 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 }
3080}
3081
3082/*
3083 * Force the running and committing transactions to commit,
3084 * and wait on the commit.
3085 */
Mingming Cao617ba132006-10-11 01:20:53 -07003086int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003087{
3088 journal_t *journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003089 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003090
3091 if (sb->s_flags & MS_RDONLY)
3092 return 0;
3093
Mingming Cao617ba132006-10-11 01:20:53 -07003094 journal = EXT4_SB(sb)->s_journal;
Frank Mayhar03901312009-01-07 00:06:22 -05003095 if (journal) {
3096 sb->s_dirt = 0;
3097 ret = ext4_journal_force_commit(journal);
3098 }
3099
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003100 return ret;
3101}
3102
3103/*
Mingming Cao617ba132006-10-11 01:20:53 -07003104 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003105 * have to propagate any other updates to the superblock on disk at this
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003106 * point. (We can probably nuke this function altogether, and remove
3107 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003108 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003109static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003110{
Frank Mayhar03901312009-01-07 00:06:22 -05003111 if (EXT4_SB(sb)->s_journal) {
3112 if (mutex_trylock(&sb->s_lock) != 0)
3113 BUG();
3114 sb->s_dirt = 0;
3115 } else {
3116 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
3117 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118}
3119
Mingming Cao617ba132006-10-11 01:20:53 -07003120static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003121{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003122 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003123
Theodore Ts'oede86cc2008-10-05 20:50:06 -04003124 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003125 sb->s_dirt = 0;
Frank Mayhar03901312009-01-07 00:06:22 -05003126 if (EXT4_SB(sb)->s_journal) {
3127 if (wait)
3128 ret = ext4_force_commit(sb);
3129 else
3130 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
3131 } else {
3132 ext4_commit_super(sb, EXT4_SB(sb)->s_es, wait);
3133 }
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05003134 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003135}
3136
3137/*
3138 * LVM calls this function before a (read-only) snapshot is created. This
3139 * gives us a chance to flush the journal completely and mark the fs clean.
3140 */
Mingming Cao617ba132006-10-11 01:20:53 -07003141static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003142{
3143 sb->s_dirt = 0;
3144
3145 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003146 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003147
Frank Mayhar03901312009-01-07 00:06:22 -05003148 if (journal) {
3149 /* Now we set up the journal barrier. */
3150 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003151
Frank Mayhar03901312009-01-07 00:06:22 -05003152 /*
3153 * We don't want to clear needs_recovery flag when we
3154 * failed to flush the journal.
3155 */
3156 if (jbd2_journal_flush(journal) < 0)
3157 return;
3158 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003159
3160 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07003161 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3162 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003163 }
3164}
3165
3166/*
3167 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3168 * flag here, even though the filesystem is not technically dirty yet.
3169 */
Mingming Cao617ba132006-10-11 01:20:53 -07003170static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003171{
Frank Mayhar03901312009-01-07 00:06:22 -05003172 if (EXT4_SB(sb)->s_journal && !(sb->s_flags & MS_RDONLY)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003173 lock_super(sb);
3174 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07003175 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3176 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003177 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07003178 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 }
3180}
3181
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003182static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003183{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003184 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003185 struct ext4_sb_info *sbi = EXT4_SB(sb);
3186 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003187 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003188 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003189 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190 int err;
3191#ifdef CONFIG_QUOTA
3192 int i;
3193#endif
3194
3195 /* Store the original options */
3196 old_sb_flags = sb->s_flags;
3197 old_opts.s_mount_opt = sbi->s_mount_opt;
3198 old_opts.s_resuid = sbi->s_resuid;
3199 old_opts.s_resgid = sbi->s_resgid;
3200 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003201 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3202 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003203#ifdef CONFIG_QUOTA
3204 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3205 for (i = 0; i < MAXQUOTAS; i++)
3206 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3207#endif
3208
3209 /*
3210 * Allow the "check" option to be passed as a remount option.
3211 */
3212 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3213 err = -EINVAL;
3214 goto restore_opts;
3215 }
3216
Mingming Cao617ba132006-10-11 01:20:53 -07003217 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003218 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003219
3220 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003221 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003222
3223 es = sbi->s_es;
3224
Frank Mayhar03901312009-01-07 00:06:22 -05003225 if (sbi->s_journal)
3226 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227
3228 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003229 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003230 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003231 err = -EROFS;
3232 goto restore_opts;
3233 }
3234
3235 if (*flags & MS_RDONLY) {
3236 /*
3237 * First of all, the unconditional stuff we have to do
3238 * to disable replay of the journal when we next remount
3239 */
3240 sb->s_flags |= MS_RDONLY;
3241
3242 /*
3243 * OK, test if we are remounting a valid rw partition
3244 * readonly, and if so set the rdonly flag and then
3245 * mark the partition as valid again.
3246 */
Mingming Cao617ba132006-10-11 01:20:53 -07003247 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3248 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003249 es->s_state = cpu_to_le16(sbi->s_mount_state);
3250
Jan Kara32c37732007-07-15 23:41:09 -07003251 /*
3252 * We have to unlock super so that we can wait for
3253 * transactions.
3254 */
Frank Mayhar03901312009-01-07 00:06:22 -05003255 if (sbi->s_journal) {
3256 unlock_super(sb);
3257 ext4_mark_recovery_complete(sb, es);
3258 lock_super(sb);
3259 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003260 } else {
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003261 int ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003262 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3263 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3264 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003265 "remount RDWR because of unsupported "
Aneesh Kumar K.V3a06d772008-11-22 15:04:59 -05003266 "optional features (%x).\n", sb->s_id,
3267 (le32_to_cpu(sbi->s_es->s_feature_ro_compat) &
3268 ~EXT4_FEATURE_RO_COMPAT_SUPP));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003269 err = -EROFS;
3270 goto restore_opts;
3271 }
Eric Sandeenead65962007-02-10 01:46:08 -08003272
3273 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003274 * Make sure the group descriptor checksums
3275 * are sane. If they aren't, refuse to
3276 * remount r/w.
3277 */
3278 for (g = 0; g < sbi->s_groups_count; g++) {
3279 struct ext4_group_desc *gdp =
3280 ext4_get_group_desc(sb, g, NULL);
3281
3282 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3283 printk(KERN_ERR
3284 "EXT4-fs: ext4_remount: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05003285 "Checksum for group %u failed (%u!=%u)\n",
Theodore Ts'o8a266462008-07-26 14:34:21 -04003286 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3287 le16_to_cpu(gdp->bg_checksum));
3288 err = -EINVAL;
3289 goto restore_opts;
3290 }
3291 }
3292
3293 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003294 * If we have an unprocessed orphan list hanging
3295 * around from a previously readonly bdev mount,
3296 * require a full umount/remount for now.
3297 */
3298 if (es->s_last_orphan) {
3299 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3300 "remount RDWR because of unprocessed "
3301 "orphan inode list. Please "
3302 "umount/remount instead.\n",
3303 sb->s_id);
3304 err = -EINVAL;
3305 goto restore_opts;
3306 }
3307
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308 /*
3309 * Mounting a RDONLY partition read-write, so reread
3310 * and store the current valid flag. (It may have
3311 * been changed by e2fsck since we originally mounted
3312 * the partition.)
3313 */
Frank Mayhar03901312009-01-07 00:06:22 -05003314 if (sbi->s_journal)
3315 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003316 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003317 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003319 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003320 sb->s_flags &= ~MS_RDONLY;
3321 }
3322 }
Frank Mayhar03901312009-01-07 00:06:22 -05003323 if (sbi->s_journal == NULL)
3324 ext4_commit_super(sb, es, 1);
3325
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003326#ifdef CONFIG_QUOTA
3327 /* Release old quota file names */
3328 for (i = 0; i < MAXQUOTAS; i++)
3329 if (old_opts.s_qf_names[i] &&
3330 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3331 kfree(old_opts.s_qf_names[i]);
3332#endif
3333 return 0;
3334restore_opts:
3335 sb->s_flags = old_sb_flags;
3336 sbi->s_mount_opt = old_opts.s_mount_opt;
3337 sbi->s_resuid = old_opts.s_resuid;
3338 sbi->s_resgid = old_opts.s_resgid;
3339 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05003340 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3341 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342#ifdef CONFIG_QUOTA
3343 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3344 for (i = 0; i < MAXQUOTAS; i++) {
3345 if (sbi->s_qf_names[i] &&
3346 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3347 kfree(sbi->s_qf_names[i]);
3348 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3349 }
3350#endif
3351 return err;
3352}
3353
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003354static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355{
3356 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003357 struct ext4_sb_info *sbi = EXT4_SB(sb);
3358 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003359 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003360
Badari Pulavarty5e700302007-07-15 23:42:00 -07003361 if (test_opt(sb, MINIX_DF)) {
3362 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003363 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003364 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003365 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003366 smp_rmb();
3367
3368 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003369 * Compute the overhead (FS structures). This is constant
3370 * for a given filesystem unless the number of block groups
3371 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003372 */
3373
3374 /*
3375 * All of the blocks before first_data_block are
3376 * overhead
3377 */
3378 overhead = le32_to_cpu(es->s_first_data_block);
3379
3380 /*
3381 * Add the overhead attributed to the superblock and
3382 * block group descriptors. If the sparse superblocks
3383 * feature is turned on, then not all groups have this.
3384 */
3385 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003386 overhead += ext4_bg_has_super(sb, i) +
3387 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003388 cond_resched();
3389 }
3390
3391 /*
3392 * Every block group has an inode bitmap, a block
3393 * bitmap, and an inode table.
3394 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003395 overhead += ngroups * (2 + sbi->s_itb_per_group);
3396 sbi->s_overhead_last = overhead;
3397 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003398 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003399 }
3400
Mingming Cao617ba132006-10-11 01:20:53 -07003401 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003402 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003403 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003404 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3405 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003406 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003407 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3408 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003409 buf->f_bavail = 0;
3410 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003411 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003412 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003413 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003414 fsid = le64_to_cpup((void *)es->s_uuid) ^
3415 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3416 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3417 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003418 return 0;
3419}
3420
3421/* Helper function for writing quotas on sync - we need to start transaction before quota file
3422 * is locked for write. Otherwise the are possible deadlocks:
3423 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003424 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003425 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003427 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003428 *
3429 */
3430
3431#ifdef CONFIG_QUOTA
3432
3433static inline struct inode *dquot_to_inode(struct dquot *dquot)
3434{
3435 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3436}
3437
Mingming Cao617ba132006-10-11 01:20:53 -07003438static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003439{
3440 handle_t *handle;
3441 int ret, err;
3442
3443 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003444 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003445 if (IS_ERR(handle))
3446 return PTR_ERR(handle);
3447 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003448 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 if (!ret)
3450 ret = err;
3451 return ret;
3452}
3453
Mingming Cao617ba132006-10-11 01:20:53 -07003454static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003455{
3456 handle_t *handle;
3457 int ret, err;
3458
3459 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003460 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003461 if (IS_ERR(handle)) {
3462 /*
3463 * We call dquot_drop() anyway to at least release references
3464 * to quota structures so that umount does not hang.
3465 */
3466 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003467 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003468 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003469 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003470 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003471 if (!ret)
3472 ret = err;
3473 return ret;
3474}
3475
Mingming Cao617ba132006-10-11 01:20:53 -07003476static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003477{
3478 int ret, err;
3479 handle_t *handle;
3480 struct inode *inode;
3481
3482 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003483 handle = ext4_journal_start(inode,
3484 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003485 if (IS_ERR(handle))
3486 return PTR_ERR(handle);
3487 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003488 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003489 if (!ret)
3490 ret = err;
3491 return ret;
3492}
3493
Mingming Cao617ba132006-10-11 01:20:53 -07003494static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003495{
3496 int ret, err;
3497 handle_t *handle;
3498
Mingming Cao617ba132006-10-11 01:20:53 -07003499 handle = ext4_journal_start(dquot_to_inode(dquot),
3500 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003501 if (IS_ERR(handle))
3502 return PTR_ERR(handle);
3503 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003504 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003505 if (!ret)
3506 ret = err;
3507 return ret;
3508}
3509
Mingming Cao617ba132006-10-11 01:20:53 -07003510static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003511{
3512 int ret, err;
3513 handle_t *handle;
3514
Mingming Cao617ba132006-10-11 01:20:53 -07003515 handle = ext4_journal_start(dquot_to_inode(dquot),
3516 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003517 if (IS_ERR(handle)) {
3518 /* Release dquot anyway to avoid endless cycle in dqput() */
3519 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003520 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003521 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003522 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003523 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003524 if (!ret)
3525 ret = err;
3526 return ret;
3527}
3528
Mingming Cao617ba132006-10-11 01:20:53 -07003529static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003530{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003531 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003532 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3533 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003534 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003535 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003536 } else {
3537 return dquot_mark_dquot_dirty(dquot);
3538 }
3539}
3540
Mingming Cao617ba132006-10-11 01:20:53 -07003541static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542{
3543 int ret, err;
3544 handle_t *handle;
3545
3546 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003547 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003548 if (IS_ERR(handle))
3549 return PTR_ERR(handle);
3550 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003551 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003552 if (!ret)
3553 ret = err;
3554 return ret;
3555}
3556
3557/*
3558 * Turn on quotas during mount time - we need to find
3559 * the quota file and such...
3560 */
Mingming Cao617ba132006-10-11 01:20:53 -07003561static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003562{
Mingming Cao617ba132006-10-11 01:20:53 -07003563 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3564 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003565}
3566
3567/*
3568 * Standard function to be called on quota_on
3569 */
Mingming Cao617ba132006-10-11 01:20:53 -07003570static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003571 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003572{
3573 int err;
Al Viro82646132008-08-02 00:57:06 -04003574 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003575
3576 if (!test_opt(sb, QUOTA))
3577 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003578 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003579 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003580 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003581
Al Viro82646132008-08-02 00:57:06 -04003582 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003583 if (err)
3584 return err;
Jan Kara06235432008-05-13 19:11:51 -04003585
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003587 if (path.mnt->mnt_sb != sb) {
3588 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003589 return -EXDEV;
3590 }
Jan Kara06235432008-05-13 19:11:51 -04003591 /* Journaling quota? */
3592 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003593 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003594 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003595 printk(KERN_WARNING
3596 "EXT4-fs: Quota file not on filesystem root. "
3597 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003598 }
Jan Kara06235432008-05-13 19:11:51 -04003599
3600 /*
3601 * When we journal data on quota file, we have to flush journal to see
3602 * all updates to the file when we bypass pagecache...
3603 */
Frank Mayhar03901312009-01-07 00:06:22 -05003604 if (EXT4_SB(sb)->s_journal &&
3605 ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003606 /*
3607 * We don't need to lock updates but journal_flush() could
3608 * otherwise be livelocked...
3609 */
3610 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003611 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003612 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003613 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003614 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003615 return err;
3616 }
Jan Kara06235432008-05-13 19:11:51 -04003617 }
3618
Al Viro82646132008-08-02 00:57:06 -04003619 err = vfs_quota_on_path(sb, type, format_id, &path);
3620 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003621 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003622}
3623
3624/* Read data from quotafile - avoid pagecache and such because we cannot afford
3625 * acquiring the locks... As quota files are never truncated and quota code
3626 * itself serializes the operations (and noone else should touch the files)
3627 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003628static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003629 size_t len, loff_t off)
3630{
3631 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003632 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003633 int err = 0;
3634 int offset = off & (sb->s_blocksize - 1);
3635 int tocopy;
3636 size_t toread;
3637 struct buffer_head *bh;
3638 loff_t i_size = i_size_read(inode);
3639
3640 if (off > i_size)
3641 return 0;
3642 if (off+len > i_size)
3643 len = i_size-off;
3644 toread = len;
3645 while (toread > 0) {
3646 tocopy = sb->s_blocksize - offset < toread ?
3647 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003648 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003649 if (err)
3650 return err;
3651 if (!bh) /* A hole? */
3652 memset(data, 0, tocopy);
3653 else
3654 memcpy(data, bh->b_data+offset, tocopy);
3655 brelse(bh);
3656 offset = 0;
3657 toread -= tocopy;
3658 data += tocopy;
3659 blk++;
3660 }
3661 return len;
3662}
3663
3664/* Write to quotafile (we know the transaction is already started and has
3665 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003666static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003667 const char *data, size_t len, loff_t off)
3668{
3669 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003670 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003671 int err = 0;
3672 int offset = off & (sb->s_blocksize - 1);
3673 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003674 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003675 size_t towrite = len;
3676 struct buffer_head *bh;
3677 handle_t *handle = journal_current_handle();
3678
Frank Mayhar03901312009-01-07 00:06:22 -05003679 if (EXT4_SB(sb)->s_journal && !handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003680 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003681 " cancelled because transaction is not started.\n",
3682 (unsigned long long)off, (unsigned long long)len);
3683 return -EIO;
3684 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003685 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3686 while (towrite > 0) {
3687 tocopy = sb->s_blocksize - offset < towrite ?
3688 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003689 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003690 if (!bh)
3691 goto out;
3692 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003693 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003694 if (err) {
3695 brelse(bh);
3696 goto out;
3697 }
3698 }
3699 lock_buffer(bh);
3700 memcpy(bh->b_data+offset, data, tocopy);
3701 flush_dcache_page(bh->b_page);
3702 unlock_buffer(bh);
3703 if (journal_quota)
Frank Mayhar03901312009-01-07 00:06:22 -05003704 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003705 else {
3706 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003707 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003708 mark_buffer_dirty(bh);
3709 }
3710 brelse(bh);
3711 if (err)
3712 goto out;
3713 offset = 0;
3714 towrite -= tocopy;
3715 data += tocopy;
3716 blk++;
3717 }
3718out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003719 if (len == towrite) {
3720 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003721 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003722 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003723 if (inode->i_size < off+len-towrite) {
3724 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003725 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003726 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003727 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003728 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003729 mutex_unlock(&inode->i_mutex);
3730 return len - towrite;
3731}
3732
3733#endif
3734
Mingming Cao617ba132006-10-11 01:20:53 -07003735static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003736 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3737{
Mingming Cao617ba132006-10-11 01:20:53 -07003738 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003739}
3740
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003741#ifdef CONFIG_PROC_FS
3742static int ext4_ui_proc_show(struct seq_file *m, void *v)
3743{
3744 unsigned int *p = m->private;
3745
3746 seq_printf(m, "%u\n", *p);
3747 return 0;
3748}
3749
3750static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3751{
3752 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3753}
3754
3755static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3756 size_t cnt, loff_t *ppos)
3757{
Roel Kluin23475e22008-11-26 02:23:19 -05003758 unsigned long *p = PDE(file->f_path.dentry->d_inode)->data;
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003759 char str[32];
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003760
3761 if (cnt >= sizeof(str))
3762 return -EINVAL;
3763 if (copy_from_user(str, buf, cnt))
3764 return -EFAULT;
Roel Kluin23475e22008-11-26 02:23:19 -05003765
3766 *p = simple_strtoul(str, NULL, 0);
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003767 return cnt;
3768}
3769
3770const struct file_operations ext4_ui_proc_fops = {
3771 .owner = THIS_MODULE,
3772 .open = ext4_ui_proc_open,
3773 .read = seq_read,
3774 .llseek = seq_lseek,
3775 .release = single_release,
3776 .write = ext4_ui_proc_write,
3777};
3778#endif
3779
Theodore Ts'o03010a32008-10-10 20:02:48 -04003780static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003781 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003782 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003783 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003784 .kill_sb = kill_block_super,
3785 .fs_flags = FS_REQUIRES_DEV,
3786};
3787
Theodore Ts'o03010a32008-10-10 20:02:48 -04003788#ifdef CONFIG_EXT4DEV_COMPAT
3789static int ext4dev_get_sb(struct file_system_type *fs_type,
3790 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3791{
3792 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3793 "to mount using ext4\n");
3794 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3795 "will go away by 2.6.31\n");
3796 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3797}
3798
3799static struct file_system_type ext4dev_fs_type = {
3800 .owner = THIS_MODULE,
3801 .name = "ext4dev",
3802 .get_sb = ext4dev_get_sb,
3803 .kill_sb = kill_block_super,
3804 .fs_flags = FS_REQUIRES_DEV,
3805};
3806MODULE_ALIAS("ext4dev");
3807#endif
3808
Mingming Cao617ba132006-10-11 01:20:53 -07003809static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003810{
Alex Tomasc9de5602008-01-29 00:19:52 -05003811 int err;
3812
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003813 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003814 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003815 if (err)
3816 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003817
3818 err = init_ext4_xattr();
3819 if (err)
3820 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003821 err = init_inodecache();
3822 if (err)
3823 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003824 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003825 if (err)
3826 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003827#ifdef CONFIG_EXT4DEV_COMPAT
3828 err = register_filesystem(&ext4dev_fs_type);
3829 if (err) {
3830 unregister_filesystem(&ext4_fs_type);
3831 goto out;
3832 }
3833#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003834 return 0;
3835out:
3836 destroy_inodecache();
3837out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003838 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003839out2:
3840 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003841 return err;
3842}
3843
Mingming Cao617ba132006-10-11 01:20:53 -07003844static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003845{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003846 unregister_filesystem(&ext4_fs_type);
3847#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003848 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003849#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003850 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003851 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003852 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003853 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003854}
3855
3856MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003857MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003858MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003859module_init(init_ext4_fs)
3860module_exit(exit_ext4_fs)