blob: 49fcf8864e7655a012a500bc6cedf3824f3532a2 [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
Alexandre Ratchov8fadc142006-10-11 01:21:15 -070096void ext4_block_bitmap_set(struct super_block *sb,
97 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070098{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -040099 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700100 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
101 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700102}
103
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700104void ext4_inode_bitmap_set(struct super_block *sb,
105 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700106{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400107 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700108 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
109 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700110}
111
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700112void ext4_inode_table_set(struct super_block *sb,
113 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700114{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400115 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700116 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
117 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700118}
119
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700120/*
Mingming Caodab291a2006-10-11 01:21:01 -0700121 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700122 *
123 * The only special thing we need to do here is to make sure that all
124 * journal_end calls result in the superblock being marked dirty, so
125 * that sync() will call the filesystem's write_super callback if
126 * appropriate.
127 */
Mingming Cao617ba132006-10-11 01:20:53 -0700128handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700129{
130 journal_t *journal;
131
132 if (sb->s_flags & MS_RDONLY)
133 return ERR_PTR(-EROFS);
134
135 /* Special case here: if the journal has aborted behind our
136 * backs (eg. EIO in the commit thread), then we still need to
137 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700138 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700139 if (is_journal_aborted(journal)) {
Harvey Harrison46e665e2008-04-17 10:38:59 -0400140 ext4_abort(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700141 "Detected aborted journal");
142 return ERR_PTR(-EROFS);
143 }
144
Mingming Caodab291a2006-10-11 01:21:01 -0700145 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700146}
147
148/*
149 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700150 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700151 * that sync() will call the filesystem's write_super callback if
152 * appropriate.
153 */
Mingming Cao617ba132006-10-11 01:20:53 -0700154int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700155{
156 struct super_block *sb;
157 int err;
158 int rc;
159
160 sb = handle->h_transaction->t_journal->j_private;
161 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700162 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700163
164 if (!err)
165 err = rc;
166 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700167 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700168 return err;
169}
170
Mingming Cao617ba132006-10-11 01:20:53 -0700171void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172 struct buffer_head *bh, handle_t *handle, int err)
173{
174 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700175 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700176
177 if (bh)
178 BUFFER_TRACE(bh, "abort");
179
180 if (!handle->h_err)
181 handle->h_err = err;
182
183 if (is_handle_aborted(handle))
184 return;
185
186 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
187 caller, errstr, err_fn);
188
Mingming Caodab291a2006-10-11 01:21:01 -0700189 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190}
191
192/* Deal with the reporting of failure conditions on a filesystem such as
193 * inconsistencies detected or read IO failures.
194 *
195 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700196 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700197 * write ordering constraints on the superblock which prevent us from
198 * writing it out straight away; and given that the journal is about to
199 * be aborted, we can't rely on the current, or future, transactions to
200 * write out the superblock safely.
201 *
Mingming Caodab291a2006-10-11 01:21:01 -0700202 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 * the journal instead. On recovery, the journal will compain about
204 * that error until we've noted it down and cleared it.
205 */
206
Mingming Cao617ba132006-10-11 01:20:53 -0700207static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700208{
Mingming Cao617ba132006-10-11 01:20:53 -0700209 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700210
Mingming Cao617ba132006-10-11 01:20:53 -0700211 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
212 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213
214 if (sb->s_flags & MS_RDONLY)
215 return;
216
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400217 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700218 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700219
Mingming Cao617ba132006-10-11 01:20:53 -0700220 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700221 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700222 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400224 if (test_opt(sb, ERRORS_RO)) {
225 printk(KERN_CRIT "Remounting filesystem read-only\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700226 sb->s_flags |= MS_RDONLY;
227 }
Mingming Cao617ba132006-10-11 01:20:53 -0700228 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700229 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700230 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 sb->s_id);
232}
233
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400234void ext4_error(struct super_block *sb, const char *function,
235 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700236{
237 va_list args;
238
239 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400240 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700241 vprintk(fmt, args);
242 printk("\n");
243 va_end(args);
244
Mingming Cao617ba132006-10-11 01:20:53 -0700245 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700246}
247
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400248static const char *ext4_decode_error(struct super_block *sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700249 char nbuf[16])
250{
251 char *errstr = NULL;
252
253 switch (errno) {
254 case -EIO:
255 errstr = "IO failure";
256 break;
257 case -ENOMEM:
258 errstr = "Out of memory";
259 break;
260 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700261 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700262 errstr = "Journal has aborted";
263 else
264 errstr = "Readonly filesystem";
265 break;
266 default:
267 /* If the caller passed in an extra buffer for unknown
268 * errors, textualise them now. Else we just return
269 * NULL. */
270 if (nbuf) {
271 /* Check for truncated error codes... */
272 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
273 errstr = nbuf;
274 }
275 break;
276 }
277
278 return errstr;
279}
280
Mingming Cao617ba132006-10-11 01:20:53 -0700281/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700282 * automatically and invokes the appropriate error response. */
283
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400284void __ext4_std_error(struct super_block *sb, const char *function, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700285{
286 char nbuf[16];
287 const char *errstr;
288
289 /* Special case: if the error is EROFS, and we're not already
290 * inside a transaction, then there's really no point in logging
291 * an error. */
292 if (errno == -EROFS && journal_current_handle() == NULL &&
293 (sb->s_flags & MS_RDONLY))
294 return;
295
Mingming Cao617ba132006-10-11 01:20:53 -0700296 errstr = ext4_decode_error(sb, errno, nbuf);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400297 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
298 sb->s_id, function, errstr);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700299
Mingming Cao617ba132006-10-11 01:20:53 -0700300 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700301}
302
303/*
Mingming Cao617ba132006-10-11 01:20:53 -0700304 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 * abort function may be used to deal with unrecoverable failures such
306 * as journal IO errors or ENOMEM at a critical moment in log management.
307 *
308 * We unconditionally force the filesystem into an ABORT|READONLY state,
309 * unless the error response on the fs has been set to panic in which
310 * case we take the easy way out and panic immediately.
311 */
312
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400313void ext4_abort(struct super_block *sb, const char *function,
314 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700315{
316 va_list args;
317
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400318 printk(KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700319
320 va_start(args, fmt);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400321 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700322 vprintk(fmt, args);
323 printk("\n");
324 va_end(args);
325
326 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700327 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700328
329 if (sb->s_flags & MS_RDONLY)
330 return;
331
332 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700333 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700334 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700335 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400336 if (EXT4_SB(sb)->s_journal)
337 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338}
339
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400340void ext4_warning(struct super_block *sb, const char *function,
341 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700342{
343 va_list args;
344
345 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700346 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700347 sb->s_id, function);
348 vprintk(fmt, args);
349 printk("\n");
350 va_end(args);
351}
352
Mingming Cao617ba132006-10-11 01:20:53 -0700353void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700354{
Mingming Cao617ba132006-10-11 01:20:53 -0700355 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700356
Mingming Cao617ba132006-10-11 01:20:53 -0700357 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700358 return;
359
Harvey Harrison46e665e2008-04-17 10:38:59 -0400360 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700361 "updating to rev %d because of new feature flag, "
362 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700363 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700364
Mingming Cao617ba132006-10-11 01:20:53 -0700365 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
366 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
367 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700368 /* leave es->s_feature_*compat flags alone */
369 /* es->s_uuid will be set by e2fsck if empty */
370
371 /*
372 * The rest of the superblock fields should be zero, and if not it
373 * means they are likely already in use, so leave them alone. We
374 * can leave it up to e2fsck to clean up any inconsistencies there.
375 */
376}
377
378/*
379 * Open the external journal device
380 */
Mingming Cao617ba132006-10-11 01:20:53 -0700381static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700382{
383 struct block_device *bdev;
384 char b[BDEVNAME_SIZE];
385
386 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
387 if (IS_ERR(bdev))
388 goto fail;
389 return bdev;
390
391fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700392 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700393 __bdevname(dev, b), PTR_ERR(bdev));
394 return NULL;
395}
396
397/*
398 * Release the journal device
399 */
Mingming Cao617ba132006-10-11 01:20:53 -0700400static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700401{
402 bd_release(bdev);
Al Viro9a1c3542008-02-22 20:40:24 -0500403 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404}
405
Mingming Cao617ba132006-10-11 01:20:53 -0700406static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407{
408 struct block_device *bdev;
409 int ret = -ENODEV;
410
411 bdev = sbi->journal_bdev;
412 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700413 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700414 sbi->journal_bdev = NULL;
415 }
416 return ret;
417}
418
419static inline struct inode *orphan_list_entry(struct list_head *l)
420{
Mingming Cao617ba132006-10-11 01:20:53 -0700421 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700422}
423
Mingming Cao617ba132006-10-11 01:20:53 -0700424static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425{
426 struct list_head *l;
427
428 printk(KERN_ERR "sb orphan head is %d\n",
429 le32_to_cpu(sbi->s_es->s_last_orphan));
430
431 printk(KERN_ERR "sb_info orphan list:\n");
432 list_for_each(l, &sbi->s_orphan) {
433 struct inode *inode = orphan_list_entry(l);
434 printk(KERN_ERR " "
435 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
436 inode->i_sb->s_id, inode->i_ino, inode,
437 inode->i_mode, inode->i_nlink,
438 NEXT_ORPHAN(inode));
439 }
440}
441
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400442static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700443{
Mingming Cao617ba132006-10-11 01:20:53 -0700444 struct ext4_sb_info *sbi = EXT4_SB(sb);
445 struct ext4_super_block *es = sbi->s_es;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400446 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700447
Alex Tomasc9de5602008-01-29 00:19:52 -0500448 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700449 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700450 ext4_xattr_put_super(sb);
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400451 err = jbd2_journal_destroy(sbi->s_journal);
Jan Kara47b4a502008-07-11 19:27:31 -0400452 sbi->s_journal = NULL;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -0400453 if (err < 0)
454 ext4_abort(sb, __func__, "Couldn't clean up the journal");
455
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700456 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700457 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700458 es->s_state = cpu_to_le16(sbi->s_mount_state);
Mingming Cao617ba132006-10-11 01:20:53 -0700459 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400461 if (sbi->s_proc) {
462 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400463 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400464 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700465
466 for (i = 0; i < sbi->s_gdb_count; i++)
467 brelse(sbi->s_group_desc[i]);
468 kfree(sbi->s_group_desc);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400469 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700470 percpu_counter_destroy(&sbi->s_freeblocks_counter);
471 percpu_counter_destroy(&sbi->s_freeinodes_counter);
472 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400473 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700474 brelse(sbi->s_sbh);
475#ifdef CONFIG_QUOTA
476 for (i = 0; i < MAXQUOTAS; i++)
477 kfree(sbi->s_qf_names[i]);
478#endif
479
480 /* Debugging code just in case the in-memory inode orphan list
481 * isn't empty. The on-disk one can be non-empty if we've
482 * detected an error and taken the fs readonly, but the
483 * in-memory list had better be clean by this point. */
484 if (!list_empty(&sbi->s_orphan))
485 dump_orphan_list(sb, sbi);
486 J_ASSERT(list_empty(&sbi->s_orphan));
487
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700488 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700489 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
490 /*
491 * Invalidate the journal device's buffers. We don't want them
492 * floating about in memory - the physical journal device may
493 * hotswapped, and it breaks the `ro-after' testing code.
494 */
495 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700496 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700497 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 }
499 sb->s_fs_info = NULL;
500 kfree(sbi);
501 return;
502}
503
Christoph Lametere18b8902006-12-06 20:33:20 -0800504static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505
506/*
507 * Called inside transaction, so use GFP_NOFS
508 */
Mingming Cao617ba132006-10-11 01:20:53 -0700509static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700510{
Mingming Cao617ba132006-10-11 01:20:53 -0700511 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800513 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 if (!ei)
515 return NULL;
Theodore Ts'o03010a32008-10-10 20:02:48 -0400516#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700517 ei->i_acl = EXT4_ACL_NOT_CACHED;
518 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700519#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400521 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700522 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500523 INIT_LIST_HEAD(&ei->i_prealloc_list);
524 spin_lock_init(&ei->i_prealloc_lock);
Jan Kara678aaf42008-07-11 19:27:31 -0400525 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400526 ei->i_reserved_data_blocks = 0;
527 ei->i_reserved_meta_blocks = 0;
528 ei->i_allocated_meta_blocks = 0;
529 ei->i_delalloc_reserved_flag = 0;
530 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700531 return &ei->vfs_inode;
532}
533
Mingming Cao617ba132006-10-11 01:20:53 -0700534static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700535{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700536 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
537 printk("EXT4 Inode %p: orphan list check failed!\n",
538 EXT4_I(inode));
539 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
540 EXT4_I(inode), sizeof(struct ext4_inode_info),
541 true);
542 dump_stack();
543 }
Mingming Cao617ba132006-10-11 01:20:53 -0700544 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700545}
546
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700547static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700548{
Mingming Cao617ba132006-10-11 01:20:53 -0700549 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700550
Christoph Lametera35afb82007-05-16 22:10:57 -0700551 INIT_LIST_HEAD(&ei->i_orphan);
Theodore Ts'o03010a32008-10-10 20:02:48 -0400552#ifdef CONFIG_EXT4_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700553 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700554#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500555 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700556 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700557}
558
559static int init_inodecache(void)
560{
Mingming Cao617ba132006-10-11 01:20:53 -0700561 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
562 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563 0, (SLAB_RECLAIM_ACCOUNT|
564 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900565 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700566 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700567 return -ENOMEM;
568 return 0;
569}
570
571static void destroy_inodecache(void)
572{
Mingming Cao617ba132006-10-11 01:20:53 -0700573 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700574}
575
Mingming Cao617ba132006-10-11 01:20:53 -0700576static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577{
Theodore Ts'o03010a32008-10-10 20:02:48 -0400578#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -0700579 if (EXT4_I(inode)->i_acl &&
580 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
581 posix_acl_release(EXT4_I(inode)->i_acl);
582 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700583 }
Mingming Cao617ba132006-10-11 01:20:53 -0700584 if (EXT4_I(inode)->i_default_acl &&
585 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
586 posix_acl_release(EXT4_I(inode)->i_default_acl);
587 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700588 }
589#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400590 ext4_discard_preallocations(inode);
Jan Kara678aaf42008-07-11 19:27:31 -0400591 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
592 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593}
594
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400595static inline void ext4_show_quota_options(struct seq_file *seq,
596 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700597{
598#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700599 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700600
601 if (sbi->s_jquota_fmt)
602 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400603 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700604
605 if (sbi->s_qf_names[USRQUOTA])
606 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
607
608 if (sbi->s_qf_names[GRPQUOTA])
609 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
610
Mingming Cao617ba132006-10-11 01:20:53 -0700611 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612 seq_puts(seq, ",usrquota");
613
Mingming Cao617ba132006-10-11 01:20:53 -0700614 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615 seq_puts(seq, ",grpquota");
616#endif
617}
618
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700619/*
620 * Show an option if
621 * - it's set to a non-default value OR
622 * - if the per-sb default is different from the global default
623 */
Mingming Cao617ba132006-10-11 01:20:53 -0700624static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500626 int def_errors;
627 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700628 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700629 struct ext4_sb_info *sbi = EXT4_SB(sb);
630 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700631
632 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500633 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700634
635 if (sbi->s_sb_block != 1)
636 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
637 if (test_opt(sb, MINIX_DF))
638 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500639 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700640 seq_puts(seq, ",grpid");
641 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
642 seq_puts(seq, ",nogrpid");
643 if (sbi->s_resuid != EXT4_DEF_RESUID ||
644 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
645 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
646 }
647 if (sbi->s_resgid != EXT4_DEF_RESGID ||
648 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
649 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
650 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500651 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700652 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500653 def_errors == EXT4_ERRORS_CONTINUE) {
654 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700655 }
656 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500657 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500658 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500659 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700660 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500661 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700662 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500663 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700664 seq_puts(seq, ",debug");
665 if (test_opt(sb, OLDALLOC))
666 seq_puts(seq, ",oldalloc");
Theodore Ts'o03010a32008-10-10 20:02:48 -0400667#ifdef CONFIG_EXT4_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500668 if (test_opt(sb, XATTR_USER) &&
669 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700670 seq_puts(seq, ",user_xattr");
671 if (!test_opt(sb, XATTR_USER) &&
672 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
673 seq_puts(seq, ",nouser_xattr");
674 }
675#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -0400676#ifdef CONFIG_EXT4_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500677 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700678 seq_puts(seq, ",acl");
679 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
680 seq_puts(seq, ",noacl");
681#endif
682 if (!test_opt(sb, RESERVATION))
683 seq_puts(seq, ",noreservation");
684 if (sbi->s_commit_interval) {
685 seq_printf(seq, ",commit=%u",
686 (unsigned) (sbi->s_commit_interval / HZ));
687 }
Eric Sandeen571640c2008-05-26 12:29:46 -0400688 /*
689 * We're changing the default of barrier mount option, so
690 * let's always display its mount state so it's clear what its
691 * status is.
692 */
693 seq_puts(seq, ",barrier=");
694 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400695 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
696 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700697 if (test_opt(sb, NOBH))
698 seq_puts(seq, ",nobh");
699 if (!test_opt(sb, EXTENTS))
700 seq_puts(seq, ",noextents");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500701 if (test_opt(sb, I_VERSION))
702 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400703 if (!test_opt(sb, DELALLOC))
704 seq_puts(seq, ",nodelalloc");
705
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500707 if (sbi->s_stripe)
708 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500709 /*
710 * journal mode get enabled in different ways
711 * So just print the value even if we didn't specify it
712 */
Mingming Cao617ba132006-10-11 01:20:53 -0700713 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700714 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700715 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700716 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700717 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700718 seq_puts(seq, ",data=writeback");
719
Theodore Ts'o240799c2008-10-09 23:53:47 -0400720 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
721 seq_printf(seq, ",inode_readahead_blks=%u",
722 sbi->s_inode_readahead_blks);
723
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400724 if (test_opt(sb, DATA_ERR_ABORT))
725 seq_puts(seq, ",data_err=abort");
726
Mingming Cao617ba132006-10-11 01:20:53 -0700727 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700728 return 0;
729}
730
731
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700732static struct inode *ext4_nfs_get_inode(struct super_block *sb,
733 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700735 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700736
Mingming Cao617ba132006-10-11 01:20:53 -0700737 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700738 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700739 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700740 return ERR_PTR(-ESTALE);
741
742 /* iget isn't really right if the inode is currently unallocated!!
743 *
Mingming Cao617ba132006-10-11 01:20:53 -0700744 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700745 * deleted, so we should be safe.
746 *
747 * Currently we don't know the generation for parent directory, so
748 * a generation of 0 means "accept any"
749 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800750 inode = ext4_iget(sb, ino);
751 if (IS_ERR(inode))
752 return ERR_CAST(inode);
753 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700754 iput(inode);
755 return ERR_PTR(-ESTALE);
756 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700757
758 return inode;
759}
760
761static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
762 int fh_len, int fh_type)
763{
764 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
765 ext4_nfs_get_inode);
766}
767
768static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
769 int fh_len, int fh_type)
770{
771 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
772 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700773}
774
775#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400776#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400777#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700778
Mingming Cao617ba132006-10-11 01:20:53 -0700779static int ext4_dquot_initialize(struct inode *inode, int type);
780static int ext4_dquot_drop(struct inode *inode);
781static int ext4_write_dquot(struct dquot *dquot);
782static int ext4_acquire_dquot(struct dquot *dquot);
783static int ext4_release_dquot(struct dquot *dquot);
784static int ext4_mark_dquot_dirty(struct dquot *dquot);
785static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700786static int ext4_quota_on(struct super_block *sb, int type, int format_id,
787 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700788static int ext4_quota_on_mount(struct super_block *sb, int type);
789static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700791static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700792 const char *data, size_t len, loff_t off);
793
Mingming Cao617ba132006-10-11 01:20:53 -0700794static struct dquot_operations ext4_quota_operations = {
795 .initialize = ext4_dquot_initialize,
796 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700797 .alloc_space = dquot_alloc_space,
798 .alloc_inode = dquot_alloc_inode,
799 .free_space = dquot_free_space,
800 .free_inode = dquot_free_inode,
801 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700802 .write_dquot = ext4_write_dquot,
803 .acquire_dquot = ext4_acquire_dquot,
804 .release_dquot = ext4_release_dquot,
805 .mark_dirty = ext4_mark_dquot_dirty,
806 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700807};
808
Mingming Cao617ba132006-10-11 01:20:53 -0700809static struct quotactl_ops ext4_qctl_operations = {
810 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700811 .quota_off = vfs_quota_off,
812 .quota_sync = vfs_quota_sync,
813 .get_info = vfs_get_dqinfo,
814 .set_info = vfs_set_dqinfo,
815 .get_dqblk = vfs_get_dqblk,
816 .set_dqblk = vfs_set_dqblk
817};
818#endif
819
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800820static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700821 .alloc_inode = ext4_alloc_inode,
822 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700823 .write_inode = ext4_write_inode,
824 .dirty_inode = ext4_dirty_inode,
825 .delete_inode = ext4_delete_inode,
826 .put_super = ext4_put_super,
827 .write_super = ext4_write_super,
828 .sync_fs = ext4_sync_fs,
829 .write_super_lockfs = ext4_write_super_lockfs,
830 .unlockfs = ext4_unlockfs,
831 .statfs = ext4_statfs,
832 .remount_fs = ext4_remount,
833 .clear_inode = ext4_clear_inode,
834 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700835#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700836 .quota_read = ext4_quota_read,
837 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700838#endif
839};
840
Christoph Hellwig39655162007-10-21 16:42:17 -0700841static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700842 .fh_to_dentry = ext4_fh_to_dentry,
843 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700844 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845};
846
847enum {
848 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
849 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400850 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700851 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
852 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
853 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500854 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700855 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400856 Opt_data_err_abort, Opt_data_err_ignore,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700857 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
858 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
859 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500860 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Theodore Ts'o01436ef2008-10-17 07:22:35 -0400861 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -0400862 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700863};
864
Steven Whitehousea447c092008-10-13 10:46:57 +0100865static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 {Opt_bsd_df, "bsddf"},
867 {Opt_minix_df, "minixdf"},
868 {Opt_grpid, "grpid"},
869 {Opt_grpid, "bsdgroups"},
870 {Opt_nogrpid, "nogrpid"},
871 {Opt_nogrpid, "sysvgroups"},
872 {Opt_resgid, "resgid=%u"},
873 {Opt_resuid, "resuid=%u"},
874 {Opt_sb, "sb=%u"},
875 {Opt_err_cont, "errors=continue"},
876 {Opt_err_panic, "errors=panic"},
877 {Opt_err_ro, "errors=remount-ro"},
878 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700879 {Opt_debug, "debug"},
880 {Opt_oldalloc, "oldalloc"},
881 {Opt_orlov, "orlov"},
882 {Opt_user_xattr, "user_xattr"},
883 {Opt_nouser_xattr, "nouser_xattr"},
884 {Opt_acl, "acl"},
885 {Opt_noacl, "noacl"},
886 {Opt_reservation, "reservation"},
887 {Opt_noreservation, "noreservation"},
888 {Opt_noload, "noload"},
889 {Opt_nobh, "nobh"},
890 {Opt_bh, "bh"},
891 {Opt_commit, "commit=%u"},
892 {Opt_journal_update, "journal=update"},
893 {Opt_journal_inum, "journal=%u"},
894 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -0500895 {Opt_journal_checksum, "journal_checksum"},
896 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700897 {Opt_abort, "abort"},
898 {Opt_data_journal, "data=journal"},
899 {Opt_data_ordered, "data=ordered"},
900 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -0400901 {Opt_data_err_abort, "data_err=abort"},
902 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700903 {Opt_offusrjquota, "usrjquota="},
904 {Opt_usrjquota, "usrjquota=%s"},
905 {Opt_offgrpjquota, "grpjquota="},
906 {Opt_grpjquota, "grpjquota=%s"},
907 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
908 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
909 {Opt_grpquota, "grpquota"},
910 {Opt_noquota, "noquota"},
911 {Opt_quota, "quota"},
912 {Opt_usrquota, "usrquota"},
913 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700914 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400915 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500916 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -0500917 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700918 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -0400919 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400920 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -0400921 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -0400922 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923};
924
Mingming Cao617ba132006-10-11 01:20:53 -0700925static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700926{
Mingming Cao617ba132006-10-11 01:20:53 -0700927 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928 char *options = (char *) *data;
929
930 if (!options || strncmp(options, "sb=", 3) != 0)
931 return 1; /* Default location */
932 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700933 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700934 sb_block = simple_strtoul(options, &options, 0);
935 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400936 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700937 (char *) *data);
938 return 1;
939 }
940 if (*options == ',')
941 options++;
942 *data = (void *) options;
943 return sb_block;
944}
945
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400946static int parse_options(char *options, struct super_block *sb,
947 unsigned int *inum, unsigned long *journal_devnum,
948 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949{
Mingming Cao617ba132006-10-11 01:20:53 -0700950 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400951 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952 substring_t args[MAX_OPT_ARGS];
953 int data_opt = 0;
954 int option;
955#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -0400956 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 char *qname;
958#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -0400959 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960
961 if (!options)
962 return 1;
963
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400964 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700965 int token;
966 if (!*p)
967 continue;
968
969 token = match_token(p, tokens, args);
970 switch (token) {
971 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400972 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700973 break;
974 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400975 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700976 break;
977 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400978 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979 break;
980 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400981 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982 break;
983 case Opt_resuid:
984 if (match_int(&args[0], &option))
985 return 0;
986 sbi->s_resuid = option;
987 break;
988 case Opt_resgid:
989 if (match_int(&args[0], &option))
990 return 0;
991 sbi->s_resgid = option;
992 break;
993 case Opt_sb:
994 /* handled by get_sb_block() instead of here */
995 /* *sb_block = match_int(&args[0]); */
996 break;
997 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400998 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
999 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1000 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001001 break;
1002 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001003 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1004 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1005 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 break;
1007 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001008 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1009 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1010 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001011 break;
1012 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001013 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001015 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001016 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001017 break;
1018 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001019 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001020 break;
1021 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001022 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001023 break;
Theodore Ts'o03010a32008-10-10 20:02:48 -04001024#ifdef CONFIG_EXT4_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001025 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001026 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001027 break;
1028 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001029 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001030 break;
1031#else
1032 case Opt_user_xattr:
1033 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001034 printk(KERN_ERR "EXT4 (no)user_xattr options "
1035 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001036 break;
1037#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001038#ifdef CONFIG_EXT4_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001039 case Opt_acl:
1040 set_opt(sbi->s_mount_opt, POSIX_ACL);
1041 break;
1042 case Opt_noacl:
1043 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1044 break;
1045#else
1046 case Opt_acl:
1047 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001048 printk(KERN_ERR "EXT4 (no)acl options "
1049 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001050 break;
1051#endif
1052 case Opt_reservation:
1053 set_opt(sbi->s_mount_opt, RESERVATION);
1054 break;
1055 case Opt_noreservation:
1056 clear_opt(sbi->s_mount_opt, RESERVATION);
1057 break;
1058 case Opt_journal_update:
1059 /* @@@ FIXME */
1060 /* Eventually we will want to be able to create
1061 a journal file here. For now, only allow the
1062 user to specify an existing inode to be the
1063 journal file. */
1064 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001065 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001066 "journal on remount\n");
1067 return 0;
1068 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001069 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 break;
1071 case Opt_journal_inum:
1072 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001073 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001074 "journal on remount\n");
1075 return 0;
1076 }
1077 if (match_int(&args[0], &option))
1078 return 0;
1079 *inum = option;
1080 break;
1081 case Opt_journal_dev:
1082 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001083 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 "journal on remount\n");
1085 return 0;
1086 }
1087 if (match_int(&args[0], &option))
1088 return 0;
1089 *journal_devnum = option;
1090 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001091 case Opt_journal_checksum:
1092 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1093 break;
1094 case Opt_journal_async_commit:
1095 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1096 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1097 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001099 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001100 break;
1101 case Opt_commit:
1102 if (match_int(&args[0], &option))
1103 return 0;
1104 if (option < 0)
1105 return 0;
1106 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001107 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 sbi->s_commit_interval = HZ * option;
1109 break;
1110 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001111 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112 goto datacheck;
1113 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001114 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 goto datacheck;
1116 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001117 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001118 datacheck:
1119 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001120 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121 != data_opt) {
1122 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001123 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 "mode on remount\n");
1125 return 0;
1126 }
1127 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001128 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 sbi->s_mount_opt |= data_opt;
1130 }
1131 break;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001132 case Opt_data_err_abort:
1133 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1134 break;
1135 case Opt_data_err_ignore:
1136 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1137 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001138#ifdef CONFIG_QUOTA
1139 case Opt_usrjquota:
1140 qtype = USRQUOTA;
1141 goto set_qf_name;
1142 case Opt_grpjquota:
1143 qtype = GRPQUOTA;
1144set_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001145 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001146 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001147 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001148 "EXT4-fs: Cannot change journaled "
1149 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001150 return 0;
1151 }
1152 qname = match_strdup(&args[0]);
1153 if (!qname) {
1154 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001155 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 "storing quotafile name.\n");
1157 return 0;
1158 }
1159 if (sbi->s_qf_names[qtype] &&
1160 strcmp(sbi->s_qf_names[qtype], qname)) {
1161 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001162 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 "specified.\n", QTYPE2NAME(qtype));
1164 kfree(qname);
1165 return 0;
1166 }
1167 sbi->s_qf_names[qtype] = qname;
1168 if (strchr(sbi->s_qf_names[qtype], '/')) {
1169 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001170 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 "filesystem root.\n");
1172 kfree(sbi->s_qf_names[qtype]);
1173 sbi->s_qf_names[qtype] = NULL;
1174 return 0;
1175 }
1176 set_opt(sbi->s_mount_opt, QUOTA);
1177 break;
1178 case Opt_offusrjquota:
1179 qtype = USRQUOTA;
1180 goto clear_qf_name;
1181 case Opt_offgrpjquota:
1182 qtype = GRPQUOTA;
1183clear_qf_name:
Jan Kara17bd13b2008-08-20 18:14:35 +02001184 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001185 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001186 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001187 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 "quota turned on.\n");
1189 return 0;
1190 }
1191 /*
1192 * The space will be released later when all options
1193 * are confirmed to be correct
1194 */
1195 sbi->s_qf_names[qtype] = NULL;
1196 break;
1197 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001198 qfmt = QFMT_VFS_OLD;
1199 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001201 qfmt = QFMT_VFS_V0;
1202set_qf_format:
Jan Kara17bd13b2008-08-20 18:14:35 +02001203 if (sb_any_quota_loaded(sb) &&
Jan Karadfc5d032008-05-13 19:11:51 -04001204 sbi->s_jquota_fmt != qfmt) {
1205 printk(KERN_ERR "EXT4-fs: Cannot change "
1206 "journaled quota options when "
1207 "quota turned on.\n");
1208 return 0;
1209 }
1210 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001211 break;
1212 case Opt_quota:
1213 case Opt_usrquota:
1214 set_opt(sbi->s_mount_opt, QUOTA);
1215 set_opt(sbi->s_mount_opt, USRQUOTA);
1216 break;
1217 case Opt_grpquota:
1218 set_opt(sbi->s_mount_opt, QUOTA);
1219 set_opt(sbi->s_mount_opt, GRPQUOTA);
1220 break;
1221 case Opt_noquota:
Jan Kara17bd13b2008-08-20 18:14:35 +02001222 if (sb_any_quota_loaded(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001223 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001224 "options when quota turned on.\n");
1225 return 0;
1226 }
1227 clear_opt(sbi->s_mount_opt, QUOTA);
1228 clear_opt(sbi->s_mount_opt, USRQUOTA);
1229 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1230 break;
1231#else
1232 case Opt_quota:
1233 case Opt_usrquota:
1234 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001235 printk(KERN_ERR
1236 "EXT4-fs: quota options not supported.\n");
1237 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238 case Opt_usrjquota:
1239 case Opt_grpjquota:
1240 case Opt_offusrjquota:
1241 case Opt_offgrpjquota:
1242 case Opt_jqfmt_vfsold:
1243 case Opt_jqfmt_vfsv0:
1244 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001245 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 "supported.\n");
1247 break;
1248 case Opt_noquota:
1249 break;
1250#endif
1251 case Opt_abort:
1252 set_opt(sbi->s_mount_opt, ABORT);
1253 break;
1254 case Opt_barrier:
1255 if (match_int(&args[0], &option))
1256 return 0;
1257 if (option)
1258 set_opt(sbi->s_mount_opt, BARRIER);
1259 else
1260 clear_opt(sbi->s_mount_opt, BARRIER);
1261 break;
1262 case Opt_ignore:
1263 break;
1264 case Opt_resize:
1265 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001266 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267 "for remount\n");
1268 return 0;
1269 }
1270 if (match_int(&args[0], &option) != 0)
1271 return 0;
1272 *n_blocks_count = option;
1273 break;
1274 case Opt_nobh:
1275 set_opt(sbi->s_mount_opt, NOBH);
1276 break;
1277 case Opt_bh:
1278 clear_opt(sbi->s_mount_opt, NOBH);
1279 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001280 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001281 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1282 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1283 ext4_warning(sb, __func__,
1284 "extents feature not enabled "
1285 "on this filesystem, use tune2fs\n");
1286 return 0;
1287 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001288 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001289 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001290 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001291 /*
1292 * When e2fsprogs support resizing an already existing
1293 * ext3 file system to greater than 2**32 we need to
1294 * add support to block allocator to handle growing
1295 * already existing block mapped inode so that blocks
1296 * allocated for them fall within 2**32
1297 */
1298 last_block = ext4_blocks_count(sbi->s_es) - 1;
1299 if (last_block > 0xffffffffULL) {
1300 printk(KERN_ERR "EXT4-fs: Filesystem too "
1301 "large to mount with "
1302 "-o noextents options\n");
1303 return 0;
1304 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001305 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001306 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001307 case Opt_i_version:
1308 set_opt(sbi->s_mount_opt, I_VERSION);
1309 sb->s_flags |= MS_I_VERSION;
1310 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001311 case Opt_nodelalloc:
1312 clear_opt(sbi->s_mount_opt, DELALLOC);
1313 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001314 case Opt_stripe:
1315 if (match_int(&args[0], &option))
1316 return 0;
1317 if (option < 0)
1318 return 0;
1319 sbi->s_stripe = option;
1320 break;
Alex Tomas64769242008-07-11 19:27:31 -04001321 case Opt_delalloc:
1322 set_opt(sbi->s_mount_opt, DELALLOC);
1323 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001324 case Opt_inode_readahead_blks:
1325 if (match_int(&args[0], &option))
1326 return 0;
1327 if (option < 0 || option > (1 << 30))
1328 return 0;
1329 sbi->s_inode_readahead_blks = option;
1330 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001332 printk(KERN_ERR
1333 "EXT4-fs: Unrecognized mount option \"%s\" "
1334 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001335 return 0;
1336 }
1337 }
1338#ifdef CONFIG_QUOTA
1339 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001340 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001341 sbi->s_qf_names[USRQUOTA])
1342 clear_opt(sbi->s_mount_opt, USRQUOTA);
1343
Mingming Cao617ba132006-10-11 01:20:53 -07001344 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001345 sbi->s_qf_names[GRPQUOTA])
1346 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1347
1348 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001349 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001350 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001351 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1352 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001353 "format mixing.\n");
1354 return 0;
1355 }
1356
1357 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001358 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001359 "not specified.\n");
1360 return 0;
1361 }
1362 } else {
1363 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001364 printk(KERN_ERR "EXT4-fs: journaled quota format "
1365 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001366 "enabled.\n");
1367 return 0;
1368 }
1369 }
1370#endif
1371 return 1;
1372}
1373
Mingming Cao617ba132006-10-11 01:20:53 -07001374static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001375 int read_only)
1376{
Mingming Cao617ba132006-10-11 01:20:53 -07001377 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001378 int res = 0;
1379
Mingming Cao617ba132006-10-11 01:20:53 -07001380 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001381 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1382 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 res = MS_RDONLY;
1384 }
1385 if (read_only)
1386 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001387 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001388 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1389 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001390 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001391 printk(KERN_WARNING
1392 "EXT4-fs warning: mounting fs with errors, "
1393 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001394 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1395 le16_to_cpu(es->s_mnt_count) >=
1396 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001397 printk(KERN_WARNING
1398 "EXT4-fs warning: maximal mount count reached, "
1399 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001400 else if (le32_to_cpu(es->s_checkinterval) &&
1401 (le32_to_cpu(es->s_lastcheck) +
1402 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001403 printk(KERN_WARNING
1404 "EXT4-fs warning: checktime reached, "
1405 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406#if 0
1407 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001408 * inconsistencies, to force a fsck at reboot. But for
1409 * a plain journaled filesystem we can keep it set as
1410 * valid forever! :)
1411 */
Marcin Slusarz216c34b2008-04-17 10:38:59 -04001412 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413#endif
1414 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001415 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001416 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001418 ext4_update_dynamic_rev(sb);
1419 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001420
Mingming Cao617ba132006-10-11 01:20:53 -07001421 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001422 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001423 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001424 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1425 sb->s_blocksize,
1426 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001427 EXT4_BLOCKS_PER_GROUP(sb),
1428 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429 sbi->s_mount_opt);
1430
Theodore Ts'o05496762008-09-16 14:36:17 -04001431 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1432 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1433 "external", EXT4_SB(sb)->s_journal->j_devname);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 return res;
1435}
1436
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001437static int ext4_fill_flex_info(struct super_block *sb)
1438{
1439 struct ext4_sb_info *sbi = EXT4_SB(sb);
1440 struct ext4_group_desc *gdp = NULL;
1441 struct buffer_head *bh;
1442 ext4_group_t flex_group_count;
1443 ext4_group_t flex_group;
1444 int groups_per_flex = 0;
1445 __u64 block_bitmap = 0;
1446 int i;
1447
1448 if (!sbi->s_es->s_log_groups_per_flex) {
1449 sbi->s_log_groups_per_flex = 0;
1450 return 1;
1451 }
1452
1453 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1454 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1455
Frederic Bohec62a11f2008-09-08 10:20:24 -04001456 /* We allocate both existing and potentially added groups */
1457 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
Aneesh Kumar K.Vd94e99a2008-11-04 09:11:26 -05001458 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1459 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001460 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001461 sizeof(struct flex_groups), GFP_KERNEL);
1462 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001463 printk(KERN_ERR "EXT4-fs: not enough memory for "
1464 "%lu flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001465 goto failed;
1466 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001467
1468 gdp = ext4_get_group_desc(sb, 1, &bh);
1469 block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1470
1471 for (i = 0; i < sbi->s_groups_count; i++) {
1472 gdp = ext4_get_group_desc(sb, i, &bh);
1473
1474 flex_group = ext4_flex_group(sbi, i);
1475 sbi->s_flex_groups[flex_group].free_inodes +=
1476 le16_to_cpu(gdp->bg_free_inodes_count);
1477 sbi->s_flex_groups[flex_group].free_blocks +=
1478 le16_to_cpu(gdp->bg_free_blocks_count);
1479 }
1480
1481 return 1;
1482failed:
1483 return 0;
1484}
1485
Andreas Dilger717d50e2007-10-16 18:38:25 -04001486__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1487 struct ext4_group_desc *gdp)
1488{
1489 __u16 crc = 0;
1490
1491 if (sbi->s_es->s_feature_ro_compat &
1492 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1493 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1494 __le32 le_group = cpu_to_le32(block_group);
1495
1496 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1497 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1498 crc = crc16(crc, (__u8 *)gdp, offset);
1499 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1500 /* for checksum of struct ext4_group_desc do the rest...*/
1501 if ((sbi->s_es->s_feature_incompat &
1502 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1503 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1504 crc = crc16(crc, (__u8 *)gdp + offset,
1505 le16_to_cpu(sbi->s_es->s_desc_size) -
1506 offset);
1507 }
1508
1509 return cpu_to_le16(crc);
1510}
1511
1512int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1513 struct ext4_group_desc *gdp)
1514{
1515 if ((sbi->s_es->s_feature_ro_compat &
1516 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1517 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1518 return 0;
1519
1520 return 1;
1521}
1522
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001523/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001524static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001525{
Mingming Cao617ba132006-10-11 01:20:53 -07001526 struct ext4_sb_info *sbi = EXT4_SB(sb);
1527 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1528 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001529 ext4_fsblk_t block_bitmap;
1530 ext4_fsblk_t inode_bitmap;
1531 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001532 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001533 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534
Jose R. Santosce421582007-10-16 18:38:25 -04001535 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1536 flexbg_flag = 1;
1537
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001538 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001539
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001540 for (i = 0; i < sbi->s_groups_count; i++) {
1541 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1542
Jose R. Santosce421582007-10-16 18:38:25 -04001543 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001544 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 else
1546 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001547 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001548
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001549 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001550 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001551 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1552 "Block bitmap for group %lu not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001553 "(block %llu)!\n", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001554 return 0;
1555 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001556 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001557 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001558 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1559 "Inode bitmap for group %lu not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001560 "(block %llu)!\n", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001561 return 0;
1562 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001563 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001564 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001565 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001566 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1567 "Inode table for group %lu not in group "
Eric Sesterhenn51282732008-10-17 09:16:19 -04001568 "(block %llu)!\n", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001569 return 0;
1570 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001571 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001572 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001573 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1574 "Checksum for group %lu failed (%u!=%u)\n",
1575 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1576 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001577 if (!(sb->s_flags & MS_RDONLY)) {
1578 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001579 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001580 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001581 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001582 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001583 if (!flexbg_flag)
1584 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001585 }
1586
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001587 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001588 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 return 1;
1590}
1591
Mingming Cao617ba132006-10-11 01:20:53 -07001592/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 * the superblock) which were deleted from all directories, but held open by
1594 * a process at the time of a crash. We walk the list and try to delete these
1595 * inodes at recovery time (only with a read-write filesystem).
1596 *
1597 * In order to keep the orphan inode chain consistent during traversal (in
1598 * case of crash during recovery), we link each inode into the superblock
1599 * orphan list_head and handle it the same way as an inode deletion during
1600 * normal operation (which journals the operations for us).
1601 *
1602 * We only do an iget() and an iput() on each inode, which is very safe if we
1603 * accidentally point at an in-use or already deleted inode. The worst that
1604 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001605 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 * e2fsck was run on this filesystem, and it must have already done the orphan
1607 * inode cleanup for us, so we can safely abort without any further action.
1608 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001609static void ext4_orphan_cleanup(struct super_block *sb,
1610 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611{
1612 unsigned int s_flags = sb->s_flags;
1613 int nr_orphans = 0, nr_truncates = 0;
1614#ifdef CONFIG_QUOTA
1615 int i;
1616#endif
1617 if (!es->s_last_orphan) {
1618 jbd_debug(4, "no orphan inodes to clean up\n");
1619 return;
1620 }
1621
Eric Sandeena8f48a92006-12-06 20:40:13 -08001622 if (bdev_read_only(sb->s_bdev)) {
1623 printk(KERN_ERR "EXT4-fs: write access "
1624 "unavailable, skipping orphan cleanup.\n");
1625 return;
1626 }
1627
Mingming Cao617ba132006-10-11 01:20:53 -07001628 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 if (es->s_last_orphan)
1630 jbd_debug(1, "Errors on filesystem, "
1631 "clearing orphan list.\n");
1632 es->s_last_orphan = 0;
1633 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1634 return;
1635 }
1636
1637 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001638 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639 sb->s_id);
1640 sb->s_flags &= ~MS_RDONLY;
1641 }
1642#ifdef CONFIG_QUOTA
1643 /* Needed for iput() to work correctly and not trash data */
1644 sb->s_flags |= MS_ACTIVE;
1645 /* Turn on quotas so that they are updated correctly */
1646 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001647 if (EXT4_SB(sb)->s_qf_names[i]) {
1648 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001649 if (ret < 0)
1650 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001651 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 "quota: error %d\n", ret);
1653 }
1654 }
1655#endif
1656
1657 while (es->s_last_orphan) {
1658 struct inode *inode;
1659
Josef Bacik97bd42b2008-04-29 22:04:56 -04001660 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1661 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 es->s_last_orphan = 0;
1663 break;
1664 }
1665
Mingming Cao617ba132006-10-11 01:20:53 -07001666 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 DQUOT_INIT(inode);
1668 if (inode->i_nlink) {
1669 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001670 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001671 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001672 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001673 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001674 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001675 nr_truncates++;
1676 } else {
1677 printk(KERN_DEBUG
1678 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001679 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001680 jbd_debug(2, "deleting unreferenced inode %lu\n",
1681 inode->i_ino);
1682 nr_orphans++;
1683 }
1684 iput(inode); /* The delete magic happens here! */
1685 }
1686
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001687#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001688
1689 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001690 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 sb->s_id, PLURAL(nr_orphans));
1692 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001693 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 sb->s_id, PLURAL(nr_truncates));
1695#ifdef CONFIG_QUOTA
1696 /* Turn quotas off */
1697 for (i = 0; i < MAXQUOTAS; i++) {
1698 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001699 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001700 }
1701#endif
1702 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1703}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001704/*
1705 * Maximal extent format file size.
1706 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1707 * extent format containers, within a sector_t, and within i_blocks
1708 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1709 * so that won't be a limiting factor.
1710 *
1711 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1712 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001713static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05001714{
1715 loff_t res;
1716 loff_t upper_limit = MAX_LFS_FILESIZE;
1717
1718 /* small i_blocks in vfs inode? */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001719 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05001720 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001721 * CONFIG_LBD is not enabled implies the inode
Eric Sandeencd2291a2008-01-28 23:58:27 -05001722 * i_block represent total blocks in 512 bytes
1723 * 32 == size of vfs inode i_blocks * 8
1724 */
1725 upper_limit = (1LL << 32) - 1;
1726
1727 /* total blocks in file system block size */
1728 upper_limit >>= (blkbits - 9);
1729 upper_limit <<= blkbits;
1730 }
1731
1732 /* 32-bit extent-start container, ee_block */
1733 res = 1LL << 32;
1734 res <<= blkbits;
1735 res -= 1;
1736
1737 /* Sanity check against vm- & vfs- imposed limits */
1738 if (res > upper_limit)
1739 res = upper_limit;
1740
1741 return res;
1742}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001743
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001745 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001746 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1747 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001749static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001750{
Mingming Cao617ba132006-10-11 01:20:53 -07001751 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001752 int meta_blocks;
1753 loff_t upper_limit;
1754 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001755 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001756 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001757 * does not exceed 2^48 -1
1758 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1759 * total number of 512 bytes blocks of the file
1760 */
1761
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001762 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001763 /*
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01001764 * !has_huge_files or CONFIG_LBD is not enabled
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001765 * implies the inode i_block represent total blocks in
1766 * 512 bytes 32 == size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001767 */
1768 upper_limit = (1LL << 32) - 1;
1769
1770 /* total blocks in file system block size */
1771 upper_limit >>= (bits - 9);
1772
1773 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001774 /*
1775 * We use 48 bit ext4_inode i_blocks
1776 * With EXT4_HUGE_FILE_FL set the i_blocks
1777 * represent total number of blocks in
1778 * file system block size
1779 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001780 upper_limit = (1LL << 48) - 1;
1781
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001782 }
1783
1784 /* indirect blocks */
1785 meta_blocks = 1;
1786 /* double indirect blocks */
1787 meta_blocks += 1 + (1LL << (bits-2));
1788 /* tripple indirect blocks */
1789 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1790
1791 upper_limit -= meta_blocks;
1792 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793
1794 res += 1LL << (bits-2);
1795 res += 1LL << (2*(bits-2));
1796 res += 1LL << (3*(bits-2));
1797 res <<= bits;
1798 if (res > upper_limit)
1799 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001800
1801 if (res > MAX_LFS_FILESIZE)
1802 res = MAX_LFS_FILESIZE;
1803
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001804 return res;
1805}
1806
Mingming Cao617ba132006-10-11 01:20:53 -07001807static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001808 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001809{
Mingming Cao617ba132006-10-11 01:20:53 -07001810 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001811 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001812 int has_super = 0;
1813
1814 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1815
Mingming Cao617ba132006-10-11 01:20:53 -07001816 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001818 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001820 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001821 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001822 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001823}
1824
Alex Tomasc9de5602008-01-29 00:19:52 -05001825/**
1826 * ext4_get_stripe_size: Get the stripe size.
1827 * @sbi: In memory super block info
1828 *
1829 * If we have specified it via mount option, then
1830 * use the mount option value. If the value specified at mount time is
1831 * greater than the blocks per group use the super block value.
1832 * If the super block value is greater than blocks per group return 0.
1833 * Allocator needs it be less than blocks per group.
1834 *
1835 */
1836static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1837{
1838 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1839 unsigned long stripe_width =
1840 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1841
1842 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1843 return sbi->s_stripe;
1844
1845 if (stripe_width <= sbi->s_blocks_per_group)
1846 return stripe_width;
1847
1848 if (stride <= sbi->s_blocks_per_group)
1849 return stride;
1850
1851 return 0;
1852}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001853
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001854static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04001855 __releases(kernel_lock)
1856 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001857
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001858{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001859 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001860 struct ext4_super_block *es = NULL;
1861 struct ext4_sb_info *sbi;
1862 ext4_fsblk_t block;
1863 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001864 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001865 unsigned long offset = 0;
1866 unsigned int journal_inum = 0;
1867 unsigned long journal_devnum = 0;
1868 unsigned long def_mount_opts;
1869 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001870 char *cp;
David Howells1d1fe1e2008-02-07 00:15:37 -08001871 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001872 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001873 int db_count;
1874 int i;
Theodore Ts'of287a1a2008-10-16 22:50:48 -04001875 int needs_recovery, has_huge_files;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001877 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001878 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879
1880 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1881 if (!sbi)
1882 return -ENOMEM;
1883 sb->s_fs_info = sbi;
1884 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001885 sbi->s_resuid = EXT4_DEF_RESUID;
1886 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001887 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07001888 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001889
1890 unlock_kernel();
1891
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001892 /* Cleanup superblock name */
1893 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1894 *cp = '!';
1895
Mingming Cao617ba132006-10-11 01:20:53 -07001896 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001897 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001898 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001899 goto out_fail;
1900 }
1901
1902 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001903 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001904 * block sizes. We need to calculate the offset from buffer start.
1905 */
Mingming Cao617ba132006-10-11 01:20:53 -07001906 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001907 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1908 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001909 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001910 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001911 }
1912
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001913 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001914 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001915 goto out_fail;
1916 }
1917 /*
1918 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001919 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 */
Mingming Cao617ba132006-10-11 01:20:53 -07001921 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 sbi->s_es = es;
1923 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001924 if (sb->s_magic != EXT4_SUPER_MAGIC)
1925 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001926
1927 /* Set defaults before we parse the mount options */
1928 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001929 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001931 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001933 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001934 set_opt(sbi->s_mount_opt, NO_UID32);
Theodore Ts'o03010a32008-10-10 20:02:48 -04001935#ifdef CONFIG_EXT4_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001936 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001937 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001938#endif
Theodore Ts'o03010a32008-10-10 20:02:48 -04001939#ifdef CONFIG_EXT4_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001940 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001941 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001942#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001943 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1944 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1945 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1946 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1947 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1948 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001949
Mingming Cao617ba132006-10-11 01:20:53 -07001950 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001951 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05001952 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07001953 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05001954 else
1955 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956
1957 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1958 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1959
1960 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04001961 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001962
Mingming Cao1e2462f2007-07-18 09:00:55 -04001963 /*
1964 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04001965 * only if feature flag already set by mkfs or tune2fs.
1966 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04001967 */
Eric Sandeene4079a12008-07-11 19:27:31 -04001968 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
1969 set_opt(sbi->s_mount_opt, EXTENTS);
1970 else
1971 ext4_warning(sb, __func__,
1972 "extents feature not enabled on this filesystem, "
1973 "use tune2fs.\n");
Mingming Cao1e2462f2007-07-18 09:00:55 -04001974
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001975 /*
1976 * enable delayed allocation by default
1977 * Use -o nodelalloc to turn it off
1978 */
1979 set_opt(sbi->s_mount_opt, DELALLOC);
1980
1981
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001982 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
1983 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001984 goto failed_mount;
1985
1986 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001987 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988
Mingming Cao617ba132006-10-11 01:20:53 -07001989 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1990 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1991 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1992 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001994 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001995 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05001996
1997 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 * Check feature flags regardless of the revision level, since we
1999 * previously didn't change the revision level when setting the flags,
2000 * so there is a chance incompat flags are set on a rev 0 filesystem.
2001 */
Mingming Cao617ba132006-10-11 01:20:53 -07002002 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002003 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002004 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002005 "unsupported optional features (%x).\n",
2006 sb->s_id, le32_to_cpu(features));
2007 goto failed_mount;
2008 }
Mingming Cao617ba132006-10-11 01:20:53 -07002009 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002010 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002011 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002012 "unsupported optional features (%x).\n",
2013 sb->s_id, le32_to_cpu(features));
2014 goto failed_mount;
2015 }
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002016 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2017 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
2018 if (has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002019 /*
2020 * Large file size enabled file system can only be
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002021 * mount if kernel is build with CONFIG_LBD
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002022 */
2023 if (sizeof(root->i_blocks) < sizeof(u64) &&
2024 !(sb->s_flags & MS_RDONLY)) {
2025 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2026 "files cannot be mounted read-write "
Jens Axboeb3a6ffe2008-12-12 09:51:16 +01002027 "without CONFIG_LBD.\n", sb->s_id);
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002028 goto failed_mount;
2029 }
2030 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002031 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2032
Mingming Cao617ba132006-10-11 01:20:53 -07002033 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2034 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002035 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002036 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002037 blocksize, sb->s_id);
2038 goto failed_mount;
2039 }
2040
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002042
2043 /* Validate the filesystem blocksize */
2044 if (!sb_set_blocksize(sb, blocksize)) {
2045 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2046 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002047 goto failed_mount;
2048 }
2049
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002050 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002051 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2052 offset = do_div(logical_sb_block, blocksize);
2053 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 if (!bh) {
2055 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002056 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 goto failed_mount;
2058 }
Mingming Cao617ba132006-10-11 01:20:53 -07002059 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002060 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002061 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002062 printk(KERN_ERR
2063 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002064 goto failed_mount;
2065 }
2066 }
2067
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002068 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2069 has_huge_files);
2070 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002071
Mingming Cao617ba132006-10-11 01:20:53 -07002072 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2073 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2074 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002075 } else {
2076 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2077 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002078 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002079 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002080 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002081 printk(KERN_ERR
2082 "EXT4-fs: unsupported inode size: %d\n",
2083 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 goto failed_mount;
2085 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002086 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2087 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002088 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002089 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2090 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002091 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002092 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002093 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002094 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002095 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002096 sbi->s_desc_size);
2097 goto failed_mount;
2098 }
2099 } else
2100 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002103 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002104 goto cantfind_ext4;
2105 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002106 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002107 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002108 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2109 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002110 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002111 sbi->s_sbh = bh;
2112 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002113 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2114 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002115 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002116 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2117 sbi->s_def_hash_version = es->s_def_hash_version;
2118
2119 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002120 printk(KERN_ERR
2121 "EXT4-fs: #blocks per group too big: %lu\n",
2122 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002123 goto failed_mount;
2124 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002126 printk(KERN_ERR
2127 "EXT4-fs: #inodes per group too big: %lu\n",
2128 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 goto failed_mount;
2130 }
2131
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002132 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002133 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002134 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 " too large to mount safely\n", sb->s_id);
2136 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002137 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002138 "enabled\n");
2139 goto failed_mount;
2140 }
2141
Mingming Cao617ba132006-10-11 01:20:53 -07002142 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2143 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002144
2145 /* ensure blocks_count calculation below doesn't sign-extend */
2146 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2147 le32_to_cpu(es->s_first_data_block) + 1) {
2148 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2149 "first data block %u, blocks per group %lu\n",
2150 ext4_blocks_count(es),
2151 le32_to_cpu(es->s_first_data_block),
2152 EXT4_BLOCKS_PER_GROUP(sb));
2153 goto failed_mount;
2154 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002155 blocks_count = (ext4_blocks_count(es) -
2156 le32_to_cpu(es->s_first_data_block) +
2157 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2158 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2159 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002160 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2161 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002162 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002163 GFP_KERNEL);
2164 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002165 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 goto failed_mount;
2167 }
2168
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002169#ifdef CONFIG_PROC_FS
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002170 if (ext4_proc_root)
2171 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2172
Theodore Ts'o240799c2008-10-09 23:53:47 -04002173 if (sbi->s_proc)
2174 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2175 &ext4_ui_proc_fops,
2176 &sbi->s_inode_readahead_blks);
Alexander Beregalov3244fcb2008-10-12 17:27:49 -04002177#endif
Theodore Ts'o240799c2008-10-09 23:53:47 -04002178
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002179 bgl_lock_init(&sbi->s_blockgroup_lock);
2180
2181 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002182 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002183 sbi->s_group_desc[i] = sb_bread(sb, block);
2184 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002185 printk(KERN_ERR "EXT4-fs: "
2186 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187 db_count = i;
2188 goto failed_mount2;
2189 }
2190 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002191 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002192 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 goto failed_mount2;
2194 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002195 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2196 if (!ext4_fill_flex_info(sb)) {
2197 printk(KERN_ERR
2198 "EXT4-fs: unable to initialize "
2199 "flex_bg meta info!\n");
2200 goto failed_mount2;
2201 }
2202
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002203 sbi->s_gdb_count = db_count;
2204 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2205 spin_lock_init(&sbi->s_next_gen_lock);
2206
Peter Zijlstra833f4072007-10-16 23:25:45 -07002207 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2208 ext4_count_free_blocks(sb));
2209 if (!err) {
2210 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2211 ext4_count_free_inodes(sb));
2212 }
2213 if (!err) {
2214 err = percpu_counter_init(&sbi->s_dirs_counter,
2215 ext4_count_dirs(sb));
2216 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002217 if (!err) {
2218 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2219 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002220 if (err) {
2221 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2222 goto failed_mount3;
2223 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224
Alex Tomasc9de5602008-01-29 00:19:52 -05002225 sbi->s_stripe = ext4_get_stripe_size(sbi);
2226
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227 /*
2228 * set up enough so that it can read an inode
2229 */
Mingming Cao617ba132006-10-11 01:20:53 -07002230 sb->s_op = &ext4_sops;
2231 sb->s_export_op = &ext4_export_ops;
2232 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002233#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002234 sb->s_qcop = &ext4_qctl_operations;
2235 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002236#endif
2237 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2238
2239 sb->s_root = NULL;
2240
2241 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002242 EXT4_HAS_INCOMPAT_FEATURE(sb,
2243 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002244
2245 /*
2246 * The first inode we look at is the journal inode. Don't try
2247 * root first: it may be modified in the journal!
2248 */
2249 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002250 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2251 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002252 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002253 if (!(sb->s_flags & MS_RDONLY) &&
2254 EXT4_SB(sb)->s_journal->j_failed_commit) {
2255 printk(KERN_CRIT "EXT4-fs error (device %s): "
2256 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002257 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002258 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002259 if (test_opt(sb, ERRORS_RO)) {
2260 printk(KERN_CRIT
2261 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002262 sb->s_flags |= MS_RDONLY;
2263 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2264 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2265 }
2266 if (test_opt(sb, ERRORS_PANIC)) {
2267 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2268 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2269 ext4_commit_super(sb, es, 1);
2270 printk(KERN_CRIT
2271 "EXT4-fs (device %s): mount failed\n",
2272 sb->s_id);
2273 goto failed_mount4;
2274 }
2275 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002276 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002277 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002278 goto failed_mount3;
2279 } else {
2280 if (!silent)
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002281 printk(KERN_ERR
2282 "ext4: No journal on filesystem on %s\n",
2283 sb->s_id);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284 goto failed_mount3;
2285 }
2286
Jose R. Santoseb40a092007-07-18 08:37:25 -04002287 if (ext4_blocks_count(es) > 0xffffffffULL &&
2288 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2289 JBD2_FEATURE_INCOMPAT_64BIT)) {
2290 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2291 goto failed_mount4;
2292 }
2293
Girish Shilamkar818d2762008-01-28 23:58:27 -05002294 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2295 jbd2_journal_set_features(sbi->s_journal,
2296 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2297 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2298 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2299 jbd2_journal_set_features(sbi->s_journal,
2300 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2301 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2302 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2303 } else {
2304 jbd2_journal_clear_features(sbi->s_journal,
2305 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2306 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2307 }
2308
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 /* We have now updated the journal if required, so we can
2310 * validate the data journaling mode. */
2311 switch (test_opt(sb, DATA_FLAGS)) {
2312 case 0:
2313 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002314 * capabilities: ORDERED_DATA if the journal can
2315 * cope, else JOURNAL_DATA
2316 */
Mingming Caodab291a2006-10-11 01:21:01 -07002317 if (jbd2_journal_check_available_features
2318 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2320 else
2321 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2322 break;
2323
Mingming Cao617ba132006-10-11 01:20:53 -07002324 case EXT4_MOUNT_ORDERED_DATA:
2325 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002326 if (!jbd2_journal_check_available_features
2327 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002328 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002329 "requested data journaling mode\n");
2330 goto failed_mount4;
2331 }
2332 default:
2333 break;
2334 }
2335
2336 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002337 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2338 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002339 "its supported only with writeback mode\n");
2340 clear_opt(sbi->s_mount_opt, NOBH);
2341 }
2342 }
2343 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002344 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002345 * so we can safely mount the rest of the filesystem now.
2346 */
2347
David Howells1d1fe1e2008-02-07 00:15:37 -08002348 root = ext4_iget(sb, EXT4_ROOT_INO);
2349 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002350 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002351 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002352 goto failed_mount4;
2353 }
2354 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002355 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002356 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002357 goto failed_mount4;
2358 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002359 sb->s_root = d_alloc_root(root);
2360 if (!sb->s_root) {
2361 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2362 iput(root);
2363 ret = -ENOMEM;
2364 goto failed_mount4;
2365 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002366
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002367 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002368
2369 /* determine the minimum size of new large inodes, if present */
2370 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2371 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2372 EXT4_GOOD_OLD_INODE_SIZE;
2373 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2374 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2375 if (sbi->s_want_extra_isize <
2376 le16_to_cpu(es->s_want_extra_isize))
2377 sbi->s_want_extra_isize =
2378 le16_to_cpu(es->s_want_extra_isize);
2379 if (sbi->s_want_extra_isize <
2380 le16_to_cpu(es->s_min_extra_isize))
2381 sbi->s_want_extra_isize =
2382 le16_to_cpu(es->s_min_extra_isize);
2383 }
2384 }
2385 /* Check if enough inode space is available */
2386 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2387 sbi->s_inode_size) {
2388 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2389 EXT4_GOOD_OLD_INODE_SIZE;
2390 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2391 "available.\n");
2392 }
2393
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04002394 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2395 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2396 "requested data journaling mode\n");
2397 clear_opt(sbi->s_mount_opt, DELALLOC);
2398 } else if (test_opt(sb, DELALLOC))
2399 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2400
2401 ext4_ext_init(sb);
2402 err = ext4_mb_init(sb, needs_recovery);
2403 if (err) {
2404 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2405 err);
2406 goto failed_mount4;
2407 }
2408
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002409 /*
2410 * akpm: core read_super() calls in here with the superblock locked.
2411 * That deadlocks, because orphan cleanup needs to lock the superblock
2412 * in numerous places. Here we just pop the lock - it's relatively
2413 * harmless, because we are now ready to accept write_super() requests,
2414 * and aviro says that's the only reason for hanging onto the
2415 * superblock lock.
2416 */
Mingming Cao617ba132006-10-11 01:20:53 -07002417 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2418 ext4_orphan_cleanup(sb, es);
2419 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002420 if (needs_recovery)
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002421 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Mingming Cao617ba132006-10-11 01:20:53 -07002422 ext4_mark_recovery_complete(sb, es);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002423 printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2424 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2425 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2426 "writeback");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002427
2428 lock_kernel();
2429 return 0;
2430
Mingming Cao617ba132006-10-11 01:20:53 -07002431cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002432 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002433 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002434 sb->s_id);
2435 goto failed_mount;
2436
2437failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07002438 jbd2_journal_destroy(sbi->s_journal);
Jan Kara47b4a502008-07-11 19:27:31 -04002439 sbi->s_journal = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002440failed_mount3:
2441 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2442 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2443 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002444 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002445failed_mount2:
2446 for (i = 0; i < db_count; i++)
2447 brelse(sbi->s_group_desc[i]);
2448 kfree(sbi->s_group_desc);
2449failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002450 if (sbi->s_proc) {
2451 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002452 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002453 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454#ifdef CONFIG_QUOTA
2455 for (i = 0; i < MAXQUOTAS; i++)
2456 kfree(sbi->s_qf_names[i]);
2457#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002458 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002459 brelse(bh);
2460out_fail:
2461 sb->s_fs_info = NULL;
2462 kfree(sbi);
2463 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002464 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002465}
2466
2467/*
2468 * Setup any per-fs journal parameters now. We'll do this both on
2469 * initial mount, once the journal has been initialised but before we've
2470 * done any recovery; and again on any subsequent remount.
2471 */
Mingming Cao617ba132006-10-11 01:20:53 -07002472static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473{
Mingming Cao617ba132006-10-11 01:20:53 -07002474 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475
2476 if (sbi->s_commit_interval)
2477 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07002478 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002479 * interval here, but for now we'll just fall back to the jbd
2480 * default. */
2481
2482 spin_lock(&journal->j_state_lock);
2483 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002484 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002485 else
Mingming Caodab291a2006-10-11 01:21:01 -07002486 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04002487 if (test_opt(sb, DATA_ERR_ABORT))
2488 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
2489 else
2490 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002491 spin_unlock(&journal->j_state_lock);
2492}
2493
Mingming Cao617ba132006-10-11 01:20:53 -07002494static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002495 unsigned int journal_inum)
2496{
2497 struct inode *journal_inode;
2498 journal_t *journal;
2499
2500 /* First, test for the existence of a valid inode on disk. Bad
2501 * things happen if we iget() an unused inode, as the subsequent
2502 * iput() will try to delete it. */
2503
David Howells1d1fe1e2008-02-07 00:15:37 -08002504 journal_inode = ext4_iget(sb, journal_inum);
2505 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002506 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002507 return NULL;
2508 }
2509 if (!journal_inode->i_nlink) {
2510 make_bad_inode(journal_inode);
2511 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002512 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 return NULL;
2514 }
2515
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002516 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002517 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002518 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002519 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002520 iput(journal_inode);
2521 return NULL;
2522 }
2523
Mingming Caodab291a2006-10-11 01:21:01 -07002524 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002525 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002526 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002527 iput(journal_inode);
2528 return NULL;
2529 }
2530 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002531 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002532 return journal;
2533}
2534
Mingming Cao617ba132006-10-11 01:20:53 -07002535static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002536 dev_t j_dev)
2537{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002538 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002539 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002540 ext4_fsblk_t start;
2541 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002542 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002543 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002544 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002545 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002546 struct block_device *bdev;
2547
Mingming Cao617ba132006-10-11 01:20:53 -07002548 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549 if (bdev == NULL)
2550 return NULL;
2551
2552 if (bd_claim(bdev, sb)) {
2553 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002554 "EXT4: failed to claim external journal device.\n");
Al Viro9a1c3542008-02-22 20:40:24 -05002555 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002556 return NULL;
2557 }
2558
2559 blocksize = sb->s_blocksize;
2560 hblock = bdev_hardsect_size(bdev);
2561 if (blocksize < hblock) {
2562 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002563 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002564 goto out_bdev;
2565 }
2566
Mingming Cao617ba132006-10-11 01:20:53 -07002567 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2568 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002569 set_blocksize(bdev, blocksize);
2570 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002571 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002572 "external journal\n");
2573 goto out_bdev;
2574 }
2575
Mingming Cao617ba132006-10-11 01:20:53 -07002576 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2577 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002579 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2580 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002581 "bad superblock\n");
2582 brelse(bh);
2583 goto out_bdev;
2584 }
2585
Mingming Cao617ba132006-10-11 01:20:53 -07002586 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2587 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002588 brelse(bh);
2589 goto out_bdev;
2590 }
2591
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002592 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002593 start = sb_block + 1;
2594 brelse(bh); /* we're done with the superblock */
2595
Mingming Caodab291a2006-10-11 01:21:01 -07002596 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597 start, len, blocksize);
2598 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002599 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002600 goto out_bdev;
2601 }
2602 journal->j_private = sb;
2603 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2604 wait_on_buffer(journal->j_sb_buffer);
2605 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002606 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607 goto out_journal;
2608 }
2609 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002610 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002611 "user (unsupported) - %d\n",
2612 be32_to_cpu(journal->j_superblock->s_nr_users));
2613 goto out_journal;
2614 }
Mingming Cao617ba132006-10-11 01:20:53 -07002615 EXT4_SB(sb)->journal_bdev = bdev;
2616 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617 return journal;
2618out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002619 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002620out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002621 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 return NULL;
2623}
2624
Mingming Cao617ba132006-10-11 01:20:53 -07002625static int ext4_load_journal(struct super_block *sb,
2626 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002627 unsigned long journal_devnum)
2628{
2629 journal_t *journal;
2630 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2631 dev_t journal_dev;
2632 int err = 0;
2633 int really_read_only;
2634
2635 if (journal_devnum &&
2636 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002637 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002638 "numbers have changed\n");
2639 journal_dev = new_decode_dev(journal_devnum);
2640 } else
2641 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2642
2643 really_read_only = bdev_read_only(sb->s_bdev);
2644
2645 /*
2646 * Are we loading a blank journal or performing recovery after a
2647 * crash? For recovery, we need to check in advance whether we
2648 * can get read-write access to the device.
2649 */
2650
Mingming Cao617ba132006-10-11 01:20:53 -07002651 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002652 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002653 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002654 "required on readonly filesystem.\n");
2655 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002656 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002657 "unavailable, cannot proceed.\n");
2658 return -EROFS;
2659 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002660 printk(KERN_INFO "EXT4-fs: write access will "
2661 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002662 }
2663 }
2664
2665 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002666 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002667 "and inode journals!\n");
2668 return -EINVAL;
2669 }
2670
2671 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002672 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002673 return -EINVAL;
2674 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002675 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002676 return -EINVAL;
2677 }
2678
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002679 if (journal->j_flags & JBD2_BARRIER)
2680 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2681 else
2682 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2683
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002684 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002685 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002686 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002687 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002688 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002689 return err;
2690 }
2691 }
2692
Mingming Cao617ba132006-10-11 01:20:53 -07002693 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002694 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002695 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002696 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002697
2698 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002699 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002700 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701 return err;
2702 }
2703
Mingming Cao617ba132006-10-11 01:20:53 -07002704 EXT4_SB(sb)->s_journal = journal;
2705 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002706
2707 if (journal_devnum &&
2708 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2709 es->s_journal_dev = cpu_to_le32(journal_devnum);
2710 sb->s_dirt = 1;
2711
2712 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002713 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 }
2715
2716 return 0;
2717}
2718
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002719static int ext4_create_journal(struct super_block *sb,
2720 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002721 unsigned int journal_inum)
2722{
2723 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002724 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725
2726 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002727 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002728 "create journal.\n");
2729 return -EROFS;
2730 }
2731
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002732 journal = ext4_get_journal(sb, journal_inum);
2733 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734 return -EINVAL;
2735
Mingming Cao617ba132006-10-11 01:20:53 -07002736 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002737 journal_inum);
2738
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002739 err = jbd2_journal_create(journal);
2740 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002741 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002742 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002743 return -EIO;
2744 }
2745
Mingming Cao617ba132006-10-11 01:20:53 -07002746 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002747
Mingming Cao617ba132006-10-11 01:20:53 -07002748 ext4_update_dynamic_rev(sb);
2749 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2750 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002751
2752 es->s_journal_inum = cpu_to_le32(journal_inum);
2753 sb->s_dirt = 1;
2754
2755 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002756 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002757
2758 return 0;
2759}
2760
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002761static void ext4_commit_super(struct super_block *sb,
2762 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002763{
Mingming Cao617ba132006-10-11 01:20:53 -07002764 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765
2766 if (!sbh)
2767 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002768 if (buffer_write_io_error(sbh)) {
2769 /*
2770 * Oh, dear. A previous attempt to write the
2771 * superblock failed. This could happen because the
2772 * USB device was yanked out. Or it could happen to
2773 * be a transient write error and maybe the block will
2774 * be remapped. Nothing we can do but to retry the
2775 * write and hope for the best.
2776 */
2777 printk(KERN_ERR "ext4: previous I/O error to "
2778 "superblock detected for %s.\n", sb->s_id);
2779 clear_buffer_write_io_error(sbh);
2780 set_buffer_uptodate(sbh);
2781 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002782 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002783 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002784 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002785 BUFFER_TRACE(sbh, "marking dirty");
2786 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002787 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002789 if (buffer_write_io_error(sbh)) {
2790 printk(KERN_ERR "ext4: I/O error while writing "
2791 "superblock for %s.\n", sb->s_id);
2792 clear_buffer_write_io_error(sbh);
2793 set_buffer_uptodate(sbh);
2794 }
2795 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002796}
2797
2798
2799/*
2800 * Have we just finished recovery? If so, and if we are mounting (or
2801 * remounting) the filesystem readonly, then we will end up with a
2802 * consistent fs on disk. Record that fact.
2803 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002804static void ext4_mark_recovery_complete(struct super_block *sb,
2805 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002806{
Mingming Cao617ba132006-10-11 01:20:53 -07002807 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002808
Mingming Caodab291a2006-10-11 01:21:01 -07002809 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002810 if (jbd2_journal_flush(journal) < 0)
2811 goto out;
2812
Jan Kara32c37732007-07-15 23:41:09 -07002813 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002814 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002816 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002818 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002819 }
Jan Kara32c37732007-07-15 23:41:09 -07002820 unlock_super(sb);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002821
2822out:
Mingming Caodab291a2006-10-11 01:21:01 -07002823 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002824}
2825
2826/*
2827 * If we are mounting (or read-write remounting) a filesystem whose journal
2828 * has recorded an error from a previous lifetime, move that error to the
2829 * main filesystem now.
2830 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002831static void ext4_clear_journal_err(struct super_block *sb,
2832 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002833{
2834 journal_t *journal;
2835 int j_errno;
2836 const char *errstr;
2837
Mingming Cao617ba132006-10-11 01:20:53 -07002838 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002839
2840 /*
2841 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002842 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002843 */
2844
Mingming Caodab291a2006-10-11 01:21:01 -07002845 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002846 if (j_errno) {
2847 char nbuf[16];
2848
Mingming Cao617ba132006-10-11 01:20:53 -07002849 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002850 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002852 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002853 "filesystem check.");
2854
Mingming Cao617ba132006-10-11 01:20:53 -07002855 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2856 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002857 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002858
Mingming Caodab291a2006-10-11 01:21:01 -07002859 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002860 }
2861}
2862
2863/*
2864 * Force the running and committing transactions to commit,
2865 * and wait on the commit.
2866 */
Mingming Cao617ba132006-10-11 01:20:53 -07002867int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002868{
2869 journal_t *journal;
2870 int ret;
2871
2872 if (sb->s_flags & MS_RDONLY)
2873 return 0;
2874
Mingming Cao617ba132006-10-11 01:20:53 -07002875 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002877 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002878 return ret;
2879}
2880
2881/*
Mingming Cao617ba132006-10-11 01:20:53 -07002882 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002883 * have to propagate any other updates to the superblock on disk at this
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05002884 * point. (We can probably nuke this function altogether, and remove
2885 * any mention to sb->s_dirt in all of fs/ext4; eventual cleanup...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002886 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002887static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002888{
2889 if (mutex_trylock(&sb->s_lock) != 0)
2890 BUG();
2891 sb->s_dirt = 0;
2892}
2893
Mingming Cao617ba132006-10-11 01:20:53 -07002894static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002895{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05002896 int ret = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002897
Theodore Ts'oede86cc2008-10-05 20:50:06 -04002898 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002899 sb->s_dirt = 0;
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05002900 if (wait)
2901 ret = ext4_force_commit(sb);
2902 else
2903 jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, NULL);
2904 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905}
2906
2907/*
2908 * LVM calls this function before a (read-only) snapshot is created. This
2909 * gives us a chance to flush the journal completely and mark the fs clean.
2910 */
Mingming Cao617ba132006-10-11 01:20:53 -07002911static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912{
2913 sb->s_dirt = 0;
2914
2915 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002916 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002917
2918 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002919 jbd2_journal_lock_updates(journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04002920
2921 /*
2922 * We don't want to clear needs_recovery flag when we failed
2923 * to flush the journal.
2924 */
2925 if (jbd2_journal_flush(journal) < 0)
2926 return;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002927
2928 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002929 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2930 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002931 }
2932}
2933
2934/*
2935 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2936 * flag here, even though the filesystem is not technically dirty yet.
2937 */
Mingming Cao617ba132006-10-11 01:20:53 -07002938static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002939{
2940 if (!(sb->s_flags & MS_RDONLY)) {
2941 lock_super(sb);
2942 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002943 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2944 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002946 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 }
2948}
2949
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002950static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002951{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002952 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07002953 struct ext4_sb_info *sbi = EXT4_SB(sb);
2954 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002955 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002956 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04002957 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958 int err;
2959#ifdef CONFIG_QUOTA
2960 int i;
2961#endif
2962
2963 /* Store the original options */
2964 old_sb_flags = sb->s_flags;
2965 old_opts.s_mount_opt = sbi->s_mount_opt;
2966 old_opts.s_resuid = sbi->s_resuid;
2967 old_opts.s_resgid = sbi->s_resgid;
2968 old_opts.s_commit_interval = sbi->s_commit_interval;
2969#ifdef CONFIG_QUOTA
2970 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2971 for (i = 0; i < MAXQUOTAS; i++)
2972 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2973#endif
2974
2975 /*
2976 * Allow the "check" option to be passed as a remount option.
2977 */
2978 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2979 err = -EINVAL;
2980 goto restore_opts;
2981 }
2982
Mingming Cao617ba132006-10-11 01:20:53 -07002983 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04002984 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002985
2986 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002987 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002988
2989 es = sbi->s_es;
2990
Mingming Cao617ba132006-10-11 01:20:53 -07002991 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002992
2993 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002994 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002995 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996 err = -EROFS;
2997 goto restore_opts;
2998 }
2999
3000 if (*flags & MS_RDONLY) {
3001 /*
3002 * First of all, the unconditional stuff we have to do
3003 * to disable replay of the journal when we next remount
3004 */
3005 sb->s_flags |= MS_RDONLY;
3006
3007 /*
3008 * OK, test if we are remounting a valid rw partition
3009 * readonly, and if so set the rdonly flag and then
3010 * mark the partition as valid again.
3011 */
Mingming Cao617ba132006-10-11 01:20:53 -07003012 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3013 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003014 es->s_state = cpu_to_le16(sbi->s_mount_state);
3015
Jan Kara32c37732007-07-15 23:41:09 -07003016 /*
3017 * We have to unlock super so that we can wait for
3018 * transactions.
3019 */
3020 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003021 ext4_mark_recovery_complete(sb, es);
Jan Kara32c37732007-07-15 23:41:09 -07003022 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003023 } else {
3024 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003025 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3026 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3027 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003028 "remount RDWR because of unsupported "
3029 "optional features (%x).\n",
3030 sb->s_id, le32_to_cpu(ret));
3031 err = -EROFS;
3032 goto restore_opts;
3033 }
Eric Sandeenead65962007-02-10 01:46:08 -08003034
3035 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003036 * Make sure the group descriptor checksums
3037 * are sane. If they aren't, refuse to
3038 * remount r/w.
3039 */
3040 for (g = 0; g < sbi->s_groups_count; g++) {
3041 struct ext4_group_desc *gdp =
3042 ext4_get_group_desc(sb, g, NULL);
3043
3044 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3045 printk(KERN_ERR
3046 "EXT4-fs: ext4_remount: "
3047 "Checksum for group %lu failed (%u!=%u)\n",
3048 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3049 le16_to_cpu(gdp->bg_checksum));
3050 err = -EINVAL;
3051 goto restore_opts;
3052 }
3053 }
3054
3055 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003056 * If we have an unprocessed orphan list hanging
3057 * around from a previously readonly bdev mount,
3058 * require a full umount/remount for now.
3059 */
3060 if (es->s_last_orphan) {
3061 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3062 "remount RDWR because of unprocessed "
3063 "orphan inode list. Please "
3064 "umount/remount instead.\n",
3065 sb->s_id);
3066 err = -EINVAL;
3067 goto restore_opts;
3068 }
3069
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003070 /*
3071 * Mounting a RDONLY partition read-write, so reread
3072 * and store the current valid flag. (It may have
3073 * been changed by e2fsck since we originally mounted
3074 * the partition.)
3075 */
Mingming Cao617ba132006-10-11 01:20:53 -07003076 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003077 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003078 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003080 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003081 sb->s_flags &= ~MS_RDONLY;
3082 }
3083 }
3084#ifdef CONFIG_QUOTA
3085 /* Release old quota file names */
3086 for (i = 0; i < MAXQUOTAS; i++)
3087 if (old_opts.s_qf_names[i] &&
3088 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3089 kfree(old_opts.s_qf_names[i]);
3090#endif
3091 return 0;
3092restore_opts:
3093 sb->s_flags = old_sb_flags;
3094 sbi->s_mount_opt = old_opts.s_mount_opt;
3095 sbi->s_resuid = old_opts.s_resuid;
3096 sbi->s_resgid = old_opts.s_resgid;
3097 sbi->s_commit_interval = old_opts.s_commit_interval;
3098#ifdef CONFIG_QUOTA
3099 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3100 for (i = 0; i < MAXQUOTAS; i++) {
3101 if (sbi->s_qf_names[i] &&
3102 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3103 kfree(sbi->s_qf_names[i]);
3104 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3105 }
3106#endif
3107 return err;
3108}
3109
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003110static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003111{
3112 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003113 struct ext4_sb_info *sbi = EXT4_SB(sb);
3114 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003115 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003116
Badari Pulavarty5e700302007-07-15 23:42:00 -07003117 if (test_opt(sb, MINIX_DF)) {
3118 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003119 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003120 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003121 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003122 smp_rmb();
3123
3124 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003125 * Compute the overhead (FS structures). This is constant
3126 * for a given filesystem unless the number of block groups
3127 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 */
3129
3130 /*
3131 * All of the blocks before first_data_block are
3132 * overhead
3133 */
3134 overhead = le32_to_cpu(es->s_first_data_block);
3135
3136 /*
3137 * Add the overhead attributed to the superblock and
3138 * block group descriptors. If the sparse superblocks
3139 * feature is turned on, then not all groups have this.
3140 */
3141 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003142 overhead += ext4_bg_has_super(sb, i) +
3143 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003144 cond_resched();
3145 }
3146
3147 /*
3148 * Every block group has an inode bitmap, a block
3149 * bitmap, and an inode table.
3150 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003151 overhead += ngroups * (2 + sbi->s_itb_per_group);
3152 sbi->s_overhead_last = overhead;
3153 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003154 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003155 }
3156
Mingming Cao617ba132006-10-11 01:20:53 -07003157 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003158 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003159 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003160 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3161 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003162 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003163 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3164 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003165 buf->f_bavail = 0;
3166 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003167 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003168 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003169 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003170 fsid = le64_to_cpup((void *)es->s_uuid) ^
3171 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3172 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3173 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003174 return 0;
3175}
3176
3177/* Helper function for writing quotas on sync - we need to start transaction before quota file
3178 * is locked for write. Otherwise the are possible deadlocks:
3179 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003180 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003181 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003182 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003183 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003184 *
3185 */
3186
3187#ifdef CONFIG_QUOTA
3188
3189static inline struct inode *dquot_to_inode(struct dquot *dquot)
3190{
3191 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3192}
3193
Mingming Cao617ba132006-10-11 01:20:53 -07003194static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195{
3196 handle_t *handle;
3197 int ret, err;
3198
3199 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003200 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003201 if (IS_ERR(handle))
3202 return PTR_ERR(handle);
3203 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003204 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003205 if (!ret)
3206 ret = err;
3207 return ret;
3208}
3209
Mingming Cao617ba132006-10-11 01:20:53 -07003210static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003211{
3212 handle_t *handle;
3213 int ret, err;
3214
3215 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003216 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003217 if (IS_ERR(handle)) {
3218 /*
3219 * We call dquot_drop() anyway to at least release references
3220 * to quota structures so that umount does not hang.
3221 */
3222 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003223 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003224 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003226 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003227 if (!ret)
3228 ret = err;
3229 return ret;
3230}
3231
Mingming Cao617ba132006-10-11 01:20:53 -07003232static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003233{
3234 int ret, err;
3235 handle_t *handle;
3236 struct inode *inode;
3237
3238 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003239 handle = ext4_journal_start(inode,
3240 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003241 if (IS_ERR(handle))
3242 return PTR_ERR(handle);
3243 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003244 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003245 if (!ret)
3246 ret = err;
3247 return ret;
3248}
3249
Mingming Cao617ba132006-10-11 01:20:53 -07003250static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003251{
3252 int ret, err;
3253 handle_t *handle;
3254
Mingming Cao617ba132006-10-11 01:20:53 -07003255 handle = ext4_journal_start(dquot_to_inode(dquot),
3256 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003257 if (IS_ERR(handle))
3258 return PTR_ERR(handle);
3259 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003260 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003261 if (!ret)
3262 ret = err;
3263 return ret;
3264}
3265
Mingming Cao617ba132006-10-11 01:20:53 -07003266static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003267{
3268 int ret, err;
3269 handle_t *handle;
3270
Mingming Cao617ba132006-10-11 01:20:53 -07003271 handle = ext4_journal_start(dquot_to_inode(dquot),
3272 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003273 if (IS_ERR(handle)) {
3274 /* Release dquot anyway to avoid endless cycle in dqput() */
3275 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003277 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003278 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003279 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003280 if (!ret)
3281 ret = err;
3282 return ret;
3283}
3284
Mingming Cao617ba132006-10-11 01:20:53 -07003285static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003286{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003287 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003288 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3289 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003290 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003291 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 } else {
3293 return dquot_mark_dquot_dirty(dquot);
3294 }
3295}
3296
Mingming Cao617ba132006-10-11 01:20:53 -07003297static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003298{
3299 int ret, err;
3300 handle_t *handle;
3301
3302 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003303 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003304 if (IS_ERR(handle))
3305 return PTR_ERR(handle);
3306 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003307 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308 if (!ret)
3309 ret = err;
3310 return ret;
3311}
3312
3313/*
3314 * Turn on quotas during mount time - we need to find
3315 * the quota file and such...
3316 */
Mingming Cao617ba132006-10-11 01:20:53 -07003317static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318{
Mingming Cao617ba132006-10-11 01:20:53 -07003319 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3320 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003321}
3322
3323/*
3324 * Standard function to be called on quota_on
3325 */
Mingming Cao617ba132006-10-11 01:20:53 -07003326static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro82646132008-08-02 00:57:06 -04003327 char *name, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003328{
3329 int err;
Al Viro82646132008-08-02 00:57:06 -04003330 struct path path;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003331
3332 if (!test_opt(sb, QUOTA))
3333 return -EINVAL;
Al Viro82646132008-08-02 00:57:06 -04003334 /* When remounting, no checks are needed and in fact, name is NULL */
Jan Kara06235432008-05-13 19:11:51 -04003335 if (remount)
Al Viro82646132008-08-02 00:57:06 -04003336 return vfs_quota_on(sb, type, format_id, name, remount);
Jan Kara06235432008-05-13 19:11:51 -04003337
Al Viro82646132008-08-02 00:57:06 -04003338 err = kern_path(name, LOOKUP_FOLLOW, &path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003339 if (err)
3340 return err;
Jan Kara06235432008-05-13 19:11:51 -04003341
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003342 /* Quotafile not on the same filesystem? */
Al Viro82646132008-08-02 00:57:06 -04003343 if (path.mnt->mnt_sb != sb) {
3344 path_put(&path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003345 return -EXDEV;
3346 }
Jan Kara06235432008-05-13 19:11:51 -04003347 /* Journaling quota? */
3348 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003349 /* Quotafile not in fs root? */
Al Viro82646132008-08-02 00:57:06 -04003350 if (path.dentry->d_parent != sb->s_root)
Jan Kara06235432008-05-13 19:11:51 -04003351 printk(KERN_WARNING
3352 "EXT4-fs: Quota file not on filesystem root. "
3353 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003354 }
Jan Kara06235432008-05-13 19:11:51 -04003355
3356 /*
3357 * When we journal data on quota file, we have to flush journal to see
3358 * all updates to the file when we bypass pagecache...
3359 */
Al Viro82646132008-08-02 00:57:06 -04003360 if (ext4_should_journal_data(path.dentry->d_inode)) {
Jan Kara06235432008-05-13 19:11:51 -04003361 /*
3362 * We don't need to lock updates but journal_flush() could
3363 * otherwise be livelocked...
3364 */
3365 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003366 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04003367 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003368 if (err) {
Al Viro82646132008-08-02 00:57:06 -04003369 path_put(&path);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04003370 return err;
3371 }
Jan Kara06235432008-05-13 19:11:51 -04003372 }
3373
Al Viro82646132008-08-02 00:57:06 -04003374 err = vfs_quota_on_path(sb, type, format_id, &path);
3375 path_put(&path);
Al Viro77e69da2008-08-01 04:29:18 -04003376 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003377}
3378
3379/* Read data from quotafile - avoid pagecache and such because we cannot afford
3380 * acquiring the locks... As quota files are never truncated and quota code
3381 * itself serializes the operations (and noone else should touch the files)
3382 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003383static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003384 size_t len, loff_t off)
3385{
3386 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003387 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003388 int err = 0;
3389 int offset = off & (sb->s_blocksize - 1);
3390 int tocopy;
3391 size_t toread;
3392 struct buffer_head *bh;
3393 loff_t i_size = i_size_read(inode);
3394
3395 if (off > i_size)
3396 return 0;
3397 if (off+len > i_size)
3398 len = i_size-off;
3399 toread = len;
3400 while (toread > 0) {
3401 tocopy = sb->s_blocksize - offset < toread ?
3402 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003403 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003404 if (err)
3405 return err;
3406 if (!bh) /* A hole? */
3407 memset(data, 0, tocopy);
3408 else
3409 memcpy(data, bh->b_data+offset, tocopy);
3410 brelse(bh);
3411 offset = 0;
3412 toread -= tocopy;
3413 data += tocopy;
3414 blk++;
3415 }
3416 return len;
3417}
3418
3419/* Write to quotafile (we know the transaction is already started and has
3420 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003421static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003422 const char *data, size_t len, loff_t off)
3423{
3424 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003425 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003426 int err = 0;
3427 int offset = off & (sb->s_blocksize - 1);
3428 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003429 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003430 size_t towrite = len;
3431 struct buffer_head *bh;
3432 handle_t *handle = journal_current_handle();
3433
Jan Kara9c3013e2007-09-11 15:23:29 -07003434 if (!handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003435 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003436 " cancelled because transaction is not started.\n",
3437 (unsigned long long)off, (unsigned long long)len);
3438 return -EIO;
3439 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003440 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3441 while (towrite > 0) {
3442 tocopy = sb->s_blocksize - offset < towrite ?
3443 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003444 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003445 if (!bh)
3446 goto out;
3447 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003448 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003449 if (err) {
3450 brelse(bh);
3451 goto out;
3452 }
3453 }
3454 lock_buffer(bh);
3455 memcpy(bh->b_data+offset, data, tocopy);
3456 flush_dcache_page(bh->b_page);
3457 unlock_buffer(bh);
3458 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07003459 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003460 else {
3461 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003462 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003463 mark_buffer_dirty(bh);
3464 }
3465 brelse(bh);
3466 if (err)
3467 goto out;
3468 offset = 0;
3469 towrite -= tocopy;
3470 data += tocopy;
3471 blk++;
3472 }
3473out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003474 if (len == towrite) {
3475 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003476 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003477 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003478 if (inode->i_size < off+len-towrite) {
3479 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003480 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003481 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003482 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003483 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003484 mutex_unlock(&inode->i_mutex);
3485 return len - towrite;
3486}
3487
3488#endif
3489
Mingming Cao617ba132006-10-11 01:20:53 -07003490static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003491 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3492{
Mingming Cao617ba132006-10-11 01:20:53 -07003493 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003494}
3495
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003496#ifdef CONFIG_PROC_FS
3497static int ext4_ui_proc_show(struct seq_file *m, void *v)
3498{
3499 unsigned int *p = m->private;
3500
3501 seq_printf(m, "%u\n", *p);
3502 return 0;
3503}
3504
3505static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3506{
3507 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3508}
3509
3510static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3511 size_t cnt, loff_t *ppos)
3512{
3513 unsigned int *p = PDE(file->f_path.dentry->d_inode)->data;
3514 char str[32];
3515 unsigned long value;
3516
3517 if (cnt >= sizeof(str))
3518 return -EINVAL;
3519 if (copy_from_user(str, buf, cnt))
3520 return -EFAULT;
3521 value = simple_strtol(str, NULL, 0);
3522 if (value < 0)
3523 return -ERANGE;
3524 *p = value;
3525 return cnt;
3526}
3527
3528const struct file_operations ext4_ui_proc_fops = {
3529 .owner = THIS_MODULE,
3530 .open = ext4_ui_proc_open,
3531 .read = seq_read,
3532 .llseek = seq_lseek,
3533 .release = single_release,
3534 .write = ext4_ui_proc_write,
3535};
3536#endif
3537
Theodore Ts'o03010a32008-10-10 20:02:48 -04003538static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003539 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04003540 .name = "ext4",
Mingming Cao617ba132006-10-11 01:20:53 -07003541 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003542 .kill_sb = kill_block_super,
3543 .fs_flags = FS_REQUIRES_DEV,
3544};
3545
Theodore Ts'o03010a32008-10-10 20:02:48 -04003546#ifdef CONFIG_EXT4DEV_COMPAT
3547static int ext4dev_get_sb(struct file_system_type *fs_type,
3548 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3549{
3550 printk(KERN_WARNING "EXT4-fs: Update your userspace programs "
3551 "to mount using ext4\n");
3552 printk(KERN_WARNING "EXT4-fs: ext4dev backwards compatibility "
3553 "will go away by 2.6.31\n");
3554 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
3555}
3556
3557static struct file_system_type ext4dev_fs_type = {
3558 .owner = THIS_MODULE,
3559 .name = "ext4dev",
3560 .get_sb = ext4dev_get_sb,
3561 .kill_sb = kill_block_super,
3562 .fs_flags = FS_REQUIRES_DEV,
3563};
3564MODULE_ALIAS("ext4dev");
3565#endif
3566
Mingming Cao617ba132006-10-11 01:20:53 -07003567static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003568{
Alex Tomasc9de5602008-01-29 00:19:52 -05003569 int err;
3570
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003571 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003572 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003573 if (err)
3574 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003575
3576 err = init_ext4_xattr();
3577 if (err)
3578 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003579 err = init_inodecache();
3580 if (err)
3581 goto out1;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003582 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003583 if (err)
3584 goto out;
Theodore Ts'o03010a32008-10-10 20:02:48 -04003585#ifdef CONFIG_EXT4DEV_COMPAT
3586 err = register_filesystem(&ext4dev_fs_type);
3587 if (err) {
3588 unregister_filesystem(&ext4_fs_type);
3589 goto out;
3590 }
3591#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003592 return 0;
3593out:
3594 destroy_inodecache();
3595out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003596 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003597out2:
3598 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003599 return err;
3600}
3601
Mingming Cao617ba132006-10-11 01:20:53 -07003602static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003603{
Theodore Ts'o03010a32008-10-10 20:02:48 -04003604 unregister_filesystem(&ext4_fs_type);
3605#ifdef CONFIG_EXT4DEV_COMPAT
Mingming Cao617ba132006-10-11 01:20:53 -07003606 unregister_filesystem(&ext4dev_fs_type);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003607#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003608 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003609 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003610 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003611 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003612}
3613
3614MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003615MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003616MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003617module_init(init_ext4_fs)
3618module_exit(exit_ext4_fs)