blob: 9c0214689de024c27098cf59f80399105873ee1d [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;
Mingming Caodab291a2006-10-11 01:21:01 -0700336 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700337}
338
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400339void ext4_warning(struct super_block *sb, const char *function,
340 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700341{
342 va_list args;
343
344 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700345 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700346 sb->s_id, function);
347 vprintk(fmt, args);
348 printk("\n");
349 va_end(args);
350}
351
Mingming Cao617ba132006-10-11 01:20:53 -0700352void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700353{
Mingming Cao617ba132006-10-11 01:20:53 -0700354 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700355
Mingming Cao617ba132006-10-11 01:20:53 -0700356 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700357 return;
358
Harvey Harrison46e665e2008-04-17 10:38:59 -0400359 ext4_warning(sb, __func__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700360 "updating to rev %d because of new feature flag, "
361 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700362 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700363
Mingming Cao617ba132006-10-11 01:20:53 -0700364 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
365 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
366 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700367 /* leave es->s_feature_*compat flags alone */
368 /* es->s_uuid will be set by e2fsck if empty */
369
370 /*
371 * The rest of the superblock fields should be zero, and if not it
372 * means they are likely already in use, so leave them alone. We
373 * can leave it up to e2fsck to clean up any inconsistencies there.
374 */
375}
376
Aneesh Kumar K.V99e6f822008-01-28 23:58:27 -0500377int ext4_update_compat_feature(handle_t *handle,
378 struct super_block *sb, __u32 compat)
379{
380 int err = 0;
381 if (!EXT4_HAS_COMPAT_FEATURE(sb, compat)) {
382 err = ext4_journal_get_write_access(handle,
383 EXT4_SB(sb)->s_sbh);
384 if (err)
385 return err;
386 EXT4_SET_COMPAT_FEATURE(sb, compat);
387 sb->s_dirt = 1;
388 handle->h_sync = 1;
389 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
390 "call ext4_journal_dirty_met adata");
391 err = ext4_journal_dirty_metadata(handle,
392 EXT4_SB(sb)->s_sbh);
393 }
394 return err;
395}
396
397int ext4_update_rocompat_feature(handle_t *handle,
398 struct super_block *sb, __u32 rocompat)
399{
400 int err = 0;
401 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, rocompat)) {
402 err = ext4_journal_get_write_access(handle,
403 EXT4_SB(sb)->s_sbh);
404 if (err)
405 return err;
406 EXT4_SET_RO_COMPAT_FEATURE(sb, rocompat);
407 sb->s_dirt = 1;
408 handle->h_sync = 1;
409 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
410 "call ext4_journal_dirty_met adata");
411 err = ext4_journal_dirty_metadata(handle,
412 EXT4_SB(sb)->s_sbh);
413 }
414 return err;
415}
416
417int ext4_update_incompat_feature(handle_t *handle,
418 struct super_block *sb, __u32 incompat)
419{
420 int err = 0;
421 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, incompat)) {
422 err = ext4_journal_get_write_access(handle,
423 EXT4_SB(sb)->s_sbh);
424 if (err)
425 return err;
426 EXT4_SET_INCOMPAT_FEATURE(sb, incompat);
427 sb->s_dirt = 1;
428 handle->h_sync = 1;
429 BUFFER_TRACE(EXT4_SB(sb)->s_sbh,
430 "call ext4_journal_dirty_met adata");
431 err = ext4_journal_dirty_metadata(handle,
432 EXT4_SB(sb)->s_sbh);
433 }
434 return err;
435}
436
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700437/*
438 * Open the external journal device
439 */
Mingming Cao617ba132006-10-11 01:20:53 -0700440static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700441{
442 struct block_device *bdev;
443 char b[BDEVNAME_SIZE];
444
445 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
446 if (IS_ERR(bdev))
447 goto fail;
448 return bdev;
449
450fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700451 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700452 __bdevname(dev, b), PTR_ERR(bdev));
453 return NULL;
454}
455
456/*
457 * Release the journal device
458 */
Mingming Cao617ba132006-10-11 01:20:53 -0700459static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700460{
461 bd_release(bdev);
462 return blkdev_put(bdev);
463}
464
Mingming Cao617ba132006-10-11 01:20:53 -0700465static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700466{
467 struct block_device *bdev;
468 int ret = -ENODEV;
469
470 bdev = sbi->journal_bdev;
471 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700472 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700473 sbi->journal_bdev = NULL;
474 }
475 return ret;
476}
477
478static inline struct inode *orphan_list_entry(struct list_head *l)
479{
Mingming Cao617ba132006-10-11 01:20:53 -0700480 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700481}
482
Mingming Cao617ba132006-10-11 01:20:53 -0700483static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700484{
485 struct list_head *l;
486
487 printk(KERN_ERR "sb orphan head is %d\n",
488 le32_to_cpu(sbi->s_es->s_last_orphan));
489
490 printk(KERN_ERR "sb_info orphan list:\n");
491 list_for_each(l, &sbi->s_orphan) {
492 struct inode *inode = orphan_list_entry(l);
493 printk(KERN_ERR " "
494 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
495 inode->i_sb->s_id, inode->i_ino, inode,
496 inode->i_mode, inode->i_nlink,
497 NEXT_ORPHAN(inode));
498 }
499}
500
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400501static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502{
Mingming Cao617ba132006-10-11 01:20:53 -0700503 struct ext4_sb_info *sbi = EXT4_SB(sb);
504 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 int i;
506
Alex Tomasc9de5602008-01-29 00:19:52 -0500507 ext4_mb_release(sb);
Alex Tomasa86c6182006-10-11 01:21:03 -0700508 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700509 ext4_xattr_put_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -0700510 jbd2_journal_destroy(sbi->s_journal);
Jan Kara47b4a502008-07-11 19:27:31 -0400511 sbi->s_journal = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700513 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 es->s_state = cpu_to_le16(sbi->s_mount_state);
515 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
516 mark_buffer_dirty(sbi->s_sbh);
Mingming Cao617ba132006-10-11 01:20:53 -0700517 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518 }
Theodore Ts'o240799c2008-10-09 23:53:47 -0400519 if (sbi->s_proc) {
520 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -0400521 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -0400522 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523
524 for (i = 0; i < sbi->s_gdb_count; i++)
525 brelse(sbi->s_group_desc[i]);
526 kfree(sbi->s_group_desc);
Jose R. Santos772cb7c2008-07-11 19:27:31 -0400527 kfree(sbi->s_flex_groups);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700528 percpu_counter_destroy(&sbi->s_freeblocks_counter);
529 percpu_counter_destroy(&sbi->s_freeinodes_counter);
530 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -0400531 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700532 brelse(sbi->s_sbh);
533#ifdef CONFIG_QUOTA
534 for (i = 0; i < MAXQUOTAS; i++)
535 kfree(sbi->s_qf_names[i]);
536#endif
537
538 /* Debugging code just in case the in-memory inode orphan list
539 * isn't empty. The on-disk one can be non-empty if we've
540 * detected an error and taken the fs readonly, but the
541 * in-memory list had better be clean by this point. */
542 if (!list_empty(&sbi->s_orphan))
543 dump_orphan_list(sb, sbi);
544 J_ASSERT(list_empty(&sbi->s_orphan));
545
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700546 invalidate_bdev(sb->s_bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700547 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
548 /*
549 * Invalidate the journal device's buffers. We don't want them
550 * floating about in memory - the physical journal device may
551 * hotswapped, and it breaks the `ro-after' testing code.
552 */
553 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700554 invalidate_bdev(sbi->journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -0700555 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700556 }
557 sb->s_fs_info = NULL;
558 kfree(sbi);
559 return;
560}
561
Christoph Lametere18b8902006-12-06 20:33:20 -0800562static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700563
564/*
565 * Called inside transaction, so use GFP_NOFS
566 */
Mingming Cao617ba132006-10-11 01:20:53 -0700567static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568{
Mingming Cao617ba132006-10-11 01:20:53 -0700569 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700570
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800571 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700572 if (!ei)
573 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700574#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
575 ei->i_acl = EXT4_ACL_NOT_CACHED;
576 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700577#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700578 ei->vfs_inode.i_version = 1;
Aneesh Kumar K.V91246c02008-08-19 21:14:52 -0400579 ei->vfs_inode.i_data.writeback_index = 0;
Alex Tomasa86c6182006-10-11 01:21:03 -0700580 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Alex Tomasc9de5602008-01-29 00:19:52 -0500581 INIT_LIST_HEAD(&ei->i_prealloc_list);
582 spin_lock_init(&ei->i_prealloc_lock);
Jan Kara678aaf42008-07-11 19:27:31 -0400583 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
Mingming Caod2a17632008-07-14 17:52:37 -0400584 ei->i_reserved_data_blocks = 0;
585 ei->i_reserved_meta_blocks = 0;
586 ei->i_allocated_meta_blocks = 0;
587 ei->i_delalloc_reserved_flag = 0;
588 spin_lock_init(&(ei->i_block_reservation_lock));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 return &ei->vfs_inode;
590}
591
Mingming Cao617ba132006-10-11 01:20:53 -0700592static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700593{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700594 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
595 printk("EXT4 Inode %p: orphan list check failed!\n",
596 EXT4_I(inode));
597 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
598 EXT4_I(inode), sizeof(struct ext4_inode_info),
599 true);
600 dump_stack();
601 }
Mingming Cao617ba132006-10-11 01:20:53 -0700602 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700603}
604
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700605static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606{
Mingming Cao617ba132006-10-11 01:20:53 -0700607 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608
Christoph Lametera35afb82007-05-16 22:10:57 -0700609 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700610#ifdef CONFIG_EXT4DEV_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700611 init_rwsem(&ei->xattr_sem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700612#endif
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -0500613 init_rwsem(&ei->i_data_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -0700614 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700615}
616
617static int init_inodecache(void)
618{
Mingming Cao617ba132006-10-11 01:20:53 -0700619 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
620 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700621 0, (SLAB_RECLAIM_ACCOUNT|
622 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900623 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -0700624 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700625 return -ENOMEM;
626 return 0;
627}
628
629static void destroy_inodecache(void)
630{
Mingming Cao617ba132006-10-11 01:20:53 -0700631 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700632}
633
Mingming Cao617ba132006-10-11 01:20:53 -0700634static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700635{
Mingming Cao617ba132006-10-11 01:20:53 -0700636#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
637 if (EXT4_I(inode)->i_acl &&
638 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
639 posix_acl_release(EXT4_I(inode)->i_acl);
640 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700641 }
Mingming Cao617ba132006-10-11 01:20:53 -0700642 if (EXT4_I(inode)->i_default_acl &&
643 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
644 posix_acl_release(EXT4_I(inode)->i_default_acl);
645 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700646 }
647#endif
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -0400648 ext4_discard_preallocations(inode);
Jan Kara678aaf42008-07-11 19:27:31 -0400649 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
650 &EXT4_I(inode)->jinode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651}
652
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400653static inline void ext4_show_quota_options(struct seq_file *seq,
654 struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700655{
656#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700657 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658
659 if (sbi->s_jquota_fmt)
660 seq_printf(seq, ",jqfmt=%s",
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400661 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold" : "vfsv0");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662
663 if (sbi->s_qf_names[USRQUOTA])
664 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
665
666 if (sbi->s_qf_names[GRPQUOTA])
667 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
668
Mingming Cao617ba132006-10-11 01:20:53 -0700669 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700670 seq_puts(seq, ",usrquota");
671
Mingming Cao617ba132006-10-11 01:20:53 -0700672 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700673 seq_puts(seq, ",grpquota");
674#endif
675}
676
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700677/*
678 * Show an option if
679 * - it's set to a non-default value OR
680 * - if the per-sb default is different from the global default
681 */
Mingming Cao617ba132006-10-11 01:20:53 -0700682static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700683{
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500684 int def_errors;
685 unsigned long def_mount_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686 struct super_block *sb = vfs->mnt_sb;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700687 struct ext4_sb_info *sbi = EXT4_SB(sb);
688 struct ext4_super_block *es = sbi->s_es;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700689
690 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500691 def_errors = le16_to_cpu(es->s_errors);
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700692
693 if (sbi->s_sb_block != 1)
694 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
695 if (test_opt(sb, MINIX_DF))
696 seq_puts(seq, ",minixdf");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500697 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700698 seq_puts(seq, ",grpid");
699 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
700 seq_puts(seq, ",nogrpid");
701 if (sbi->s_resuid != EXT4_DEF_RESUID ||
702 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
703 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
704 }
705 if (sbi->s_resgid != EXT4_DEF_RESGID ||
706 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
707 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
708 }
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500709 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700710 if (def_errors == EXT4_ERRORS_PANIC ||
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500711 def_errors == EXT4_ERRORS_CONTINUE) {
712 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700713 }
714 }
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500715 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -0500716 seq_puts(seq, ",errors=continue");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500717 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700718 seq_puts(seq, ",errors=panic");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500719 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700720 seq_puts(seq, ",nouid32");
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500721 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700722 seq_puts(seq, ",debug");
723 if (test_opt(sb, OLDALLOC))
724 seq_puts(seq, ",oldalloc");
Adrian Bunk07620f62008-01-28 23:58:27 -0500725#ifdef CONFIG_EXT4DEV_FS_XATTR
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500726 if (test_opt(sb, XATTR_USER) &&
727 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700728 seq_puts(seq, ",user_xattr");
729 if (!test_opt(sb, XATTR_USER) &&
730 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
731 seq_puts(seq, ",nouser_xattr");
732 }
733#endif
Adrian Bunk07620f62008-01-28 23:58:27 -0500734#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500735 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700736 seq_puts(seq, ",acl");
737 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
738 seq_puts(seq, ",noacl");
739#endif
740 if (!test_opt(sb, RESERVATION))
741 seq_puts(seq, ",noreservation");
742 if (sbi->s_commit_interval) {
743 seq_printf(seq, ",commit=%u",
744 (unsigned) (sbi->s_commit_interval / HZ));
745 }
Eric Sandeen571640c2008-05-26 12:29:46 -0400746 /*
747 * We're changing the default of barrier mount option, so
748 * let's always display its mount state so it's clear what its
749 * status is.
750 */
751 seq_puts(seq, ",barrier=");
752 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Theodore Ts'ocd0b6a32008-05-26 10:28:28 -0400753 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
754 seq_puts(seq, ",journal_async_commit");
Miklos Szeredid9c9bef12007-10-16 23:26:27 -0700755 if (test_opt(sb, NOBH))
756 seq_puts(seq, ",nobh");
757 if (!test_opt(sb, EXTENTS))
758 seq_puts(seq, ",noextents");
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500759 if (test_opt(sb, I_VERSION))
760 seq_puts(seq, ",i_version");
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400761 if (!test_opt(sb, DELALLOC))
762 seq_puts(seq, ",nodelalloc");
763
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700764
Miklos Szeredicb45bbe2008-01-28 23:58:27 -0500765 if (sbi->s_stripe)
766 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
Aneesh Kumar K.Vaa22df22008-01-28 23:58:27 -0500767 /*
768 * journal mode get enabled in different ways
769 * So just print the value even if we didn't specify it
770 */
Mingming Cao617ba132006-10-11 01:20:53 -0700771 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700772 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700773 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700774 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700775 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700776 seq_puts(seq, ",data=writeback");
777
Theodore Ts'o240799c2008-10-09 23:53:47 -0400778 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
779 seq_printf(seq, ",inode_readahead_blks=%u",
780 sbi->s_inode_readahead_blks);
781
Mingming Cao617ba132006-10-11 01:20:53 -0700782 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 return 0;
784}
785
786
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700787static struct inode *ext4_nfs_get_inode(struct super_block *sb,
788 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700789{
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700790 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791
Mingming Cao617ba132006-10-11 01:20:53 -0700792 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700793 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700794 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700795 return ERR_PTR(-ESTALE);
796
797 /* iget isn't really right if the inode is currently unallocated!!
798 *
Mingming Cao617ba132006-10-11 01:20:53 -0700799 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800 * deleted, so we should be safe.
801 *
802 * Currently we don't know the generation for parent directory, so
803 * a generation of 0 means "accept any"
804 */
David Howells1d1fe1e2008-02-07 00:15:37 -0800805 inode = ext4_iget(sb, ino);
806 if (IS_ERR(inode))
807 return ERR_CAST(inode);
808 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700809 iput(inode);
810 return ERR_PTR(-ESTALE);
811 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700812
813 return inode;
814}
815
816static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
817 int fh_len, int fh_type)
818{
819 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
820 ext4_nfs_get_inode);
821}
822
823static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
824 int fh_len, int fh_type)
825{
826 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
827 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700828}
829
830#ifdef CONFIG_QUOTA
Theodore Ts'oaf5bc922008-09-08 22:25:24 -0400831#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400832#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700833
Mingming Cao617ba132006-10-11 01:20:53 -0700834static int ext4_dquot_initialize(struct inode *inode, int type);
835static int ext4_dquot_drop(struct inode *inode);
836static int ext4_write_dquot(struct dquot *dquot);
837static int ext4_acquire_dquot(struct dquot *dquot);
838static int ext4_release_dquot(struct dquot *dquot);
839static int ext4_mark_dquot_dirty(struct dquot *dquot);
840static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -0700841static int ext4_quota_on(struct super_block *sb, int type, int format_id,
842 char *path, int remount);
Mingming Cao617ba132006-10-11 01:20:53 -0700843static int ext4_quota_on_mount(struct super_block *sb, int type);
844static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700845 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700846static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700847 const char *data, size_t len, loff_t off);
848
Mingming Cao617ba132006-10-11 01:20:53 -0700849static struct dquot_operations ext4_quota_operations = {
850 .initialize = ext4_dquot_initialize,
851 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700852 .alloc_space = dquot_alloc_space,
853 .alloc_inode = dquot_alloc_inode,
854 .free_space = dquot_free_space,
855 .free_inode = dquot_free_inode,
856 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700857 .write_dquot = ext4_write_dquot,
858 .acquire_dquot = ext4_acquire_dquot,
859 .release_dquot = ext4_release_dquot,
860 .mark_dirty = ext4_mark_dquot_dirty,
861 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700862};
863
Mingming Cao617ba132006-10-11 01:20:53 -0700864static struct quotactl_ops ext4_qctl_operations = {
865 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700866 .quota_off = vfs_quota_off,
867 .quota_sync = vfs_quota_sync,
868 .get_info = vfs_get_dqinfo,
869 .set_info = vfs_set_dqinfo,
870 .get_dqblk = vfs_get_dqblk,
871 .set_dqblk = vfs_set_dqblk
872};
873#endif
874
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800875static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -0700876 .alloc_inode = ext4_alloc_inode,
877 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -0700878 .write_inode = ext4_write_inode,
879 .dirty_inode = ext4_dirty_inode,
880 .delete_inode = ext4_delete_inode,
881 .put_super = ext4_put_super,
882 .write_super = ext4_write_super,
883 .sync_fs = ext4_sync_fs,
884 .write_super_lockfs = ext4_write_super_lockfs,
885 .unlockfs = ext4_unlockfs,
886 .statfs = ext4_statfs,
887 .remount_fs = ext4_remount,
888 .clear_inode = ext4_clear_inode,
889 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700890#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700891 .quota_read = ext4_quota_read,
892 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700893#endif
894};
895
Christoph Hellwig39655162007-10-21 16:42:17 -0700896static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -0700897 .fh_to_dentry = ext4_fh_to_dentry,
898 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -0700899 .get_parent = ext4_get_parent,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900};
901
902enum {
903 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
904 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
905 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
906 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
907 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
908 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Girish Shilamkar818d2762008-01-28 23:58:27 -0500909 Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
911 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
912 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
913 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500914 Opt_grpquota, Opt_extents, Opt_noextents, Opt_i_version,
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400915 Opt_mballoc, Opt_nomballoc, Opt_stripe, Opt_delalloc, Opt_nodelalloc,
Theodore Ts'o240799c2008-10-09 23:53:47 -0400916 Opt_inode_readahead_blks
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700917};
918
919static match_table_t tokens = {
920 {Opt_bsd_df, "bsddf"},
921 {Opt_minix_df, "minixdf"},
922 {Opt_grpid, "grpid"},
923 {Opt_grpid, "bsdgroups"},
924 {Opt_nogrpid, "nogrpid"},
925 {Opt_nogrpid, "sysvgroups"},
926 {Opt_resgid, "resgid=%u"},
927 {Opt_resuid, "resuid=%u"},
928 {Opt_sb, "sb=%u"},
929 {Opt_err_cont, "errors=continue"},
930 {Opt_err_panic, "errors=panic"},
931 {Opt_err_ro, "errors=remount-ro"},
932 {Opt_nouid32, "nouid32"},
933 {Opt_nocheck, "nocheck"},
934 {Opt_nocheck, "check=none"},
935 {Opt_debug, "debug"},
936 {Opt_oldalloc, "oldalloc"},
937 {Opt_orlov, "orlov"},
938 {Opt_user_xattr, "user_xattr"},
939 {Opt_nouser_xattr, "nouser_xattr"},
940 {Opt_acl, "acl"},
941 {Opt_noacl, "noacl"},
942 {Opt_reservation, "reservation"},
943 {Opt_noreservation, "noreservation"},
944 {Opt_noload, "noload"},
945 {Opt_nobh, "nobh"},
946 {Opt_bh, "bh"},
947 {Opt_commit, "commit=%u"},
948 {Opt_journal_update, "journal=update"},
949 {Opt_journal_inum, "journal=%u"},
950 {Opt_journal_dev, "journal_dev=%u"},
Girish Shilamkar818d2762008-01-28 23:58:27 -0500951 {Opt_journal_checksum, "journal_checksum"},
952 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700953 {Opt_abort, "abort"},
954 {Opt_data_journal, "data=journal"},
955 {Opt_data_ordered, "data=ordered"},
956 {Opt_data_writeback, "data=writeback"},
957 {Opt_offusrjquota, "usrjquota="},
958 {Opt_usrjquota, "usrjquota=%s"},
959 {Opt_offgrpjquota, "grpjquota="},
960 {Opt_grpjquota, "grpjquota=%s"},
961 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
962 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
963 {Opt_grpquota, "grpquota"},
964 {Opt_noquota, "noquota"},
965 {Opt_quota, "quota"},
966 {Opt_usrquota, "usrquota"},
967 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700968 {Opt_extents, "extents"},
Mingming Cao1e2462f2007-07-18 09:00:55 -0400969 {Opt_noextents, "noextents"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -0500970 {Opt_i_version, "i_version"},
Alex Tomasc9de5602008-01-29 00:19:52 -0500971 {Opt_mballoc, "mballoc"},
972 {Opt_nomballoc, "nomballoc"},
973 {Opt_stripe, "stripe=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700974 {Opt_resize, "resize"},
Alex Tomas64769242008-07-11 19:27:31 -0400975 {Opt_delalloc, "delalloc"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -0400976 {Opt_nodelalloc, "nodelalloc"},
Theodore Ts'o240799c2008-10-09 23:53:47 -0400977 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Josef Bacikf3f12fa2008-04-29 22:05:28 -0400978 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700979};
980
Mingming Cao617ba132006-10-11 01:20:53 -0700981static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700982{
Mingming Cao617ba132006-10-11 01:20:53 -0700983 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 char *options = (char *) *data;
985
986 if (!options || strncmp(options, "sb=", 3) != 0)
987 return 1; /* Default location */
988 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700989 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700990 sb_block = simple_strtoul(options, &options, 0);
991 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -0400992 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700993 (char *) *data);
994 return 1;
995 }
996 if (*options == ',')
997 options++;
998 *data = (void *) options;
999 return sb_block;
1000}
1001
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001002static int parse_options(char *options, struct super_block *sb,
1003 unsigned int *inum, unsigned long *journal_devnum,
1004 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001005{
Mingming Cao617ba132006-10-11 01:20:53 -07001006 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001007 char *p;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001008 substring_t args[MAX_OPT_ARGS];
1009 int data_opt = 0;
1010 int option;
1011#ifdef CONFIG_QUOTA
Jan Karadfc5d032008-05-13 19:11:51 -04001012 int qtype, qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001013 char *qname;
1014#endif
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001015 ext4_fsblk_t last_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001016
1017 if (!options)
1018 return 1;
1019
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001020 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 int token;
1022 if (!*p)
1023 continue;
1024
1025 token = match_token(p, tokens, args);
1026 switch (token) {
1027 case Opt_bsd_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001028 clear_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001029 break;
1030 case Opt_minix_df:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001031 set_opt(sbi->s_mount_opt, MINIX_DF);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001032 break;
1033 case Opt_grpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001034 set_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001035 break;
1036 case Opt_nogrpid:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001037 clear_opt(sbi->s_mount_opt, GRPID);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001038 break;
1039 case Opt_resuid:
1040 if (match_int(&args[0], &option))
1041 return 0;
1042 sbi->s_resuid = option;
1043 break;
1044 case Opt_resgid:
1045 if (match_int(&args[0], &option))
1046 return 0;
1047 sbi->s_resgid = option;
1048 break;
1049 case Opt_sb:
1050 /* handled by get_sb_block() instead of here */
1051 /* *sb_block = match_int(&args[0]); */
1052 break;
1053 case Opt_err_panic:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001054 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1055 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1056 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057 break;
1058 case Opt_err_ro:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001059 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1060 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1061 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001062 break;
1063 case Opt_err_cont:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001064 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1065 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1066 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067 break;
1068 case Opt_nouid32:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001069 set_opt(sbi->s_mount_opt, NO_UID32);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001070 break;
1071 case Opt_nocheck:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001072 clear_opt(sbi->s_mount_opt, CHECK);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073 break;
1074 case Opt_debug:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001075 set_opt(sbi->s_mount_opt, DEBUG);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076 break;
1077 case Opt_oldalloc:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001078 set_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001079 break;
1080 case Opt_orlov:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001081 clear_opt(sbi->s_mount_opt, OLDALLOC);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001082 break;
Mingming Cao617ba132006-10-11 01:20:53 -07001083#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001084 case Opt_user_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001085 set_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001086 break;
1087 case Opt_nouser_xattr:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001088 clear_opt(sbi->s_mount_opt, XATTR_USER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 break;
1090#else
1091 case Opt_user_xattr:
1092 case Opt_nouser_xattr:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001093 printk(KERN_ERR "EXT4 (no)user_xattr options "
1094 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001095 break;
1096#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001097#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001098 case Opt_acl:
1099 set_opt(sbi->s_mount_opt, POSIX_ACL);
1100 break;
1101 case Opt_noacl:
1102 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1103 break;
1104#else
1105 case Opt_acl:
1106 case Opt_noacl:
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001107 printk(KERN_ERR "EXT4 (no)acl options "
1108 "not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109 break;
1110#endif
1111 case Opt_reservation:
1112 set_opt(sbi->s_mount_opt, RESERVATION);
1113 break;
1114 case Opt_noreservation:
1115 clear_opt(sbi->s_mount_opt, RESERVATION);
1116 break;
1117 case Opt_journal_update:
1118 /* @@@ FIXME */
1119 /* Eventually we will want to be able to create
1120 a journal file here. For now, only allow the
1121 user to specify an existing inode to be the
1122 journal file. */
1123 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001124 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001125 "journal on remount\n");
1126 return 0;
1127 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001128 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129 break;
1130 case Opt_journal_inum:
1131 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001132 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 "journal on remount\n");
1134 return 0;
1135 }
1136 if (match_int(&args[0], &option))
1137 return 0;
1138 *inum = option;
1139 break;
1140 case Opt_journal_dev:
1141 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001142 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001143 "journal on remount\n");
1144 return 0;
1145 }
1146 if (match_int(&args[0], &option))
1147 return 0;
1148 *journal_devnum = option;
1149 break;
Girish Shilamkar818d2762008-01-28 23:58:27 -05001150 case Opt_journal_checksum:
1151 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1152 break;
1153 case Opt_journal_async_commit:
1154 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
1155 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1156 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001157 case Opt_noload:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001158 set_opt(sbi->s_mount_opt, NOLOAD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001159 break;
1160 case Opt_commit:
1161 if (match_int(&args[0], &option))
1162 return 0;
1163 if (option < 0)
1164 return 0;
1165 if (option == 0)
Mingming Caocd02ff02007-10-16 18:38:25 -04001166 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 sbi->s_commit_interval = HZ * option;
1168 break;
1169 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -07001170 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171 goto datacheck;
1172 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -07001173 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001174 goto datacheck;
1175 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -07001176 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001177 datacheck:
1178 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001179 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001180 != data_opt) {
1181 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001182 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001183 "mode on remount\n");
1184 return 0;
1185 }
1186 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07001187 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001188 sbi->s_mount_opt |= data_opt;
1189 }
1190 break;
1191#ifdef CONFIG_QUOTA
1192 case Opt_usrjquota:
1193 qtype = USRQUOTA;
1194 goto set_qf_name;
1195 case Opt_grpjquota:
1196 qtype = GRPQUOTA;
1197set_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001198 if ((sb_any_quota_enabled(sb) ||
1199 sb_any_quota_suspended(sb)) &&
1200 !sbi->s_qf_names[qtype]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001201 printk(KERN_ERR
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001202 "EXT4-fs: Cannot change journaled "
1203 "quota options when quota turned on.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001204 return 0;
1205 }
1206 qname = match_strdup(&args[0]);
1207 if (!qname) {
1208 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001209 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 "storing quotafile name.\n");
1211 return 0;
1212 }
1213 if (sbi->s_qf_names[qtype] &&
1214 strcmp(sbi->s_qf_names[qtype], qname)) {
1215 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001216 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001217 "specified.\n", QTYPE2NAME(qtype));
1218 kfree(qname);
1219 return 0;
1220 }
1221 sbi->s_qf_names[qtype] = qname;
1222 if (strchr(sbi->s_qf_names[qtype], '/')) {
1223 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001224 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001225 "filesystem root.\n");
1226 kfree(sbi->s_qf_names[qtype]);
1227 sbi->s_qf_names[qtype] = NULL;
1228 return 0;
1229 }
1230 set_opt(sbi->s_mount_opt, QUOTA);
1231 break;
1232 case Opt_offusrjquota:
1233 qtype = USRQUOTA;
1234 goto clear_qf_name;
1235 case Opt_offgrpjquota:
1236 qtype = GRPQUOTA;
1237clear_qf_name:
Jan Karadfc5d032008-05-13 19:11:51 -04001238 if ((sb_any_quota_enabled(sb) ||
1239 sb_any_quota_suspended(sb)) &&
1240 sbi->s_qf_names[qtype]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001241 printk(KERN_ERR "EXT4-fs: Cannot change "
Jan Kara2c8be6b2008-05-13 21:27:55 -04001242 "journaled quota options when "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001243 "quota turned on.\n");
1244 return 0;
1245 }
1246 /*
1247 * The space will be released later when all options
1248 * are confirmed to be correct
1249 */
1250 sbi->s_qf_names[qtype] = NULL;
1251 break;
1252 case Opt_jqfmt_vfsold:
Jan Karadfc5d032008-05-13 19:11:51 -04001253 qfmt = QFMT_VFS_OLD;
1254 goto set_qf_format;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 case Opt_jqfmt_vfsv0:
Jan Karadfc5d032008-05-13 19:11:51 -04001256 qfmt = QFMT_VFS_V0;
1257set_qf_format:
1258 if ((sb_any_quota_enabled(sb) ||
1259 sb_any_quota_suspended(sb)) &&
1260 sbi->s_jquota_fmt != qfmt) {
1261 printk(KERN_ERR "EXT4-fs: Cannot change "
1262 "journaled quota options when "
1263 "quota turned on.\n");
1264 return 0;
1265 }
1266 sbi->s_jquota_fmt = qfmt;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001267 break;
1268 case Opt_quota:
1269 case Opt_usrquota:
1270 set_opt(sbi->s_mount_opt, QUOTA);
1271 set_opt(sbi->s_mount_opt, USRQUOTA);
1272 break;
1273 case Opt_grpquota:
1274 set_opt(sbi->s_mount_opt, QUOTA);
1275 set_opt(sbi->s_mount_opt, GRPQUOTA);
1276 break;
1277 case Opt_noquota:
1278 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001279 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001280 "options when quota turned on.\n");
1281 return 0;
1282 }
1283 clear_opt(sbi->s_mount_opt, QUOTA);
1284 clear_opt(sbi->s_mount_opt, USRQUOTA);
1285 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1286 break;
1287#else
1288 case Opt_quota:
1289 case Opt_usrquota:
1290 case Opt_grpquota:
Jan Karacd59e7b2008-05-13 19:11:51 -04001291 printk(KERN_ERR
1292 "EXT4-fs: quota options not supported.\n");
1293 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 case Opt_usrjquota:
1295 case Opt_grpjquota:
1296 case Opt_offusrjquota:
1297 case Opt_offgrpjquota:
1298 case Opt_jqfmt_vfsold:
1299 case Opt_jqfmt_vfsv0:
1300 printk(KERN_ERR
Jan Karacd59e7b2008-05-13 19:11:51 -04001301 "EXT4-fs: journaled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001302 "supported.\n");
1303 break;
1304 case Opt_noquota:
1305 break;
1306#endif
1307 case Opt_abort:
1308 set_opt(sbi->s_mount_opt, ABORT);
1309 break;
1310 case Opt_barrier:
1311 if (match_int(&args[0], &option))
1312 return 0;
1313 if (option)
1314 set_opt(sbi->s_mount_opt, BARRIER);
1315 else
1316 clear_opt(sbi->s_mount_opt, BARRIER);
1317 break;
1318 case Opt_ignore:
1319 break;
1320 case Opt_resize:
1321 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001322 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001323 "for remount\n");
1324 return 0;
1325 }
1326 if (match_int(&args[0], &option) != 0)
1327 return 0;
1328 *n_blocks_count = option;
1329 break;
1330 case Opt_nobh:
1331 set_opt(sbi->s_mount_opt, NOBH);
1332 break;
1333 case Opt_bh:
1334 clear_opt(sbi->s_mount_opt, NOBH);
1335 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001336 case Opt_extents:
Eric Sandeene4079a12008-07-11 19:27:31 -04001337 if (!EXT4_HAS_INCOMPAT_FEATURE(sb,
1338 EXT4_FEATURE_INCOMPAT_EXTENTS)) {
1339 ext4_warning(sb, __func__,
1340 "extents feature not enabled "
1341 "on this filesystem, use tune2fs\n");
1342 return 0;
1343 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001344 set_opt(sbi->s_mount_opt, EXTENTS);
Alex Tomasa86c6182006-10-11 01:21:03 -07001345 break;
Mingming Cao1e2462f2007-07-18 09:00:55 -04001346 case Opt_noextents:
Aneesh Kumar K.Vc07651b2008-07-11 19:27:31 -04001347 /*
1348 * When e2fsprogs support resizing an already existing
1349 * ext3 file system to greater than 2**32 we need to
1350 * add support to block allocator to handle growing
1351 * already existing block mapped inode so that blocks
1352 * allocated for them fall within 2**32
1353 */
1354 last_block = ext4_blocks_count(sbi->s_es) - 1;
1355 if (last_block > 0xffffffffULL) {
1356 printk(KERN_ERR "EXT4-fs: Filesystem too "
1357 "large to mount with "
1358 "-o noextents options\n");
1359 return 0;
1360 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001361 clear_opt(sbi->s_mount_opt, EXTENTS);
Mingming Cao1e2462f2007-07-18 09:00:55 -04001362 break;
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001363 case Opt_i_version:
1364 set_opt(sbi->s_mount_opt, I_VERSION);
1365 sb->s_flags |= MS_I_VERSION;
1366 break;
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001367 case Opt_nodelalloc:
1368 clear_opt(sbi->s_mount_opt, DELALLOC);
1369 break;
Alex Tomasc9de5602008-01-29 00:19:52 -05001370 case Opt_stripe:
1371 if (match_int(&args[0], &option))
1372 return 0;
1373 if (option < 0)
1374 return 0;
1375 sbi->s_stripe = option;
1376 break;
Alex Tomas64769242008-07-11 19:27:31 -04001377 case Opt_delalloc:
1378 set_opt(sbi->s_mount_opt, DELALLOC);
1379 break;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001380 case Opt_inode_readahead_blks:
1381 if (match_int(&args[0], &option))
1382 return 0;
1383 if (option < 0 || option > (1 << 30))
1384 return 0;
1385 sbi->s_inode_readahead_blks = option;
1386 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001387 default:
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001388 printk(KERN_ERR
1389 "EXT4-fs: Unrecognized mount option \"%s\" "
1390 "or missing value\n", p);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001391 return 0;
1392 }
1393 }
1394#ifdef CONFIG_QUOTA
1395 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001396 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001397 sbi->s_qf_names[USRQUOTA])
1398 clear_opt(sbi->s_mount_opt, USRQUOTA);
1399
Mingming Cao617ba132006-10-11 01:20:53 -07001400 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001401 sbi->s_qf_names[GRPQUOTA])
1402 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1403
1404 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001405 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001406 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001407 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1408 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001409 "format mixing.\n");
1410 return 0;
1411 }
1412
1413 if (!sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001414 printk(KERN_ERR "EXT4-fs: journaled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001415 "not specified.\n");
1416 return 0;
1417 }
1418 } else {
1419 if (sbi->s_jquota_fmt) {
Jan Kara2c8be6b2008-05-13 21:27:55 -04001420 printk(KERN_ERR "EXT4-fs: journaled quota format "
1421 "specified with no journaling "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001422 "enabled.\n");
1423 return 0;
1424 }
1425 }
1426#endif
1427 return 1;
1428}
1429
Mingming Cao617ba132006-10-11 01:20:53 -07001430static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001431 int read_only)
1432{
Mingming Cao617ba132006-10-11 01:20:53 -07001433 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001434 int res = 0;
1435
Mingming Cao617ba132006-10-11 01:20:53 -07001436 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001437 printk(KERN_ERR "EXT4-fs warning: revision level too high, "
1438 "forcing read-only mode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001439 res = MS_RDONLY;
1440 }
1441 if (read_only)
1442 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001443 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001444 printk(KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
1445 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001446 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001447 printk(KERN_WARNING
1448 "EXT4-fs warning: mounting fs with errors, "
1449 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1451 le16_to_cpu(es->s_mnt_count) >=
1452 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001453 printk(KERN_WARNING
1454 "EXT4-fs warning: maximal mount count reached, "
1455 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456 else if (le32_to_cpu(es->s_checkinterval) &&
1457 (le32_to_cpu(es->s_lastcheck) +
1458 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001459 printk(KERN_WARNING
1460 "EXT4-fs warning: checktime reached, "
1461 "running e2fsck is recommended\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001462#if 0
1463 /* @@@ We _will_ want to clear the valid bit if we find
Andrew Morton63f57932006-10-11 01:21:24 -07001464 * inconsistencies, to force a fsck at reboot. But for
1465 * a plain journaled filesystem we can keep it set as
1466 * valid forever! :)
1467 */
Marcin Slusarz216c34b2008-04-17 10:38:59 -04001468 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001469#endif
1470 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001471 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04001472 le16_add_cpu(&es->s_mnt_count, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001473 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001474 ext4_update_dynamic_rev(sb);
1475 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476
Mingming Cao617ba132006-10-11 01:20:53 -07001477 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001478 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001479 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001480 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1481 sb->s_blocksize,
1482 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001483 EXT4_BLOCKS_PER_GROUP(sb),
1484 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485 sbi->s_mount_opt);
1486
Theodore Ts'o05496762008-09-16 14:36:17 -04001487 printk(KERN_INFO "EXT4 FS on %s, %s journal on %s\n",
1488 sb->s_id, EXT4_SB(sb)->s_journal->j_inode ? "internal" :
1489 "external", EXT4_SB(sb)->s_journal->j_devname);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001490 return res;
1491}
1492
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001493static int ext4_fill_flex_info(struct super_block *sb)
1494{
1495 struct ext4_sb_info *sbi = EXT4_SB(sb);
1496 struct ext4_group_desc *gdp = NULL;
1497 struct buffer_head *bh;
1498 ext4_group_t flex_group_count;
1499 ext4_group_t flex_group;
1500 int groups_per_flex = 0;
1501 __u64 block_bitmap = 0;
1502 int i;
1503
1504 if (!sbi->s_es->s_log_groups_per_flex) {
1505 sbi->s_log_groups_per_flex = 0;
1506 return 1;
1507 }
1508
1509 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1510 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1511
Frederic Bohec62a11f2008-09-08 10:20:24 -04001512 /* We allocate both existing and potentially added groups */
1513 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
1514 ((sbi->s_es->s_reserved_gdt_blocks +1 ) <<
1515 EXT4_DESC_PER_BLOCK_BITS(sb))) /
1516 groups_per_flex;
Li Zefanec05e862008-07-24 12:49:59 -04001517 sbi->s_flex_groups = kzalloc(flex_group_count *
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001518 sizeof(struct flex_groups), GFP_KERNEL);
1519 if (sbi->s_flex_groups == NULL) {
Li Zefanec05e862008-07-24 12:49:59 -04001520 printk(KERN_ERR "EXT4-fs: not enough memory for "
1521 "%lu flex groups\n", flex_group_count);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001522 goto failed;
1523 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04001524
1525 gdp = ext4_get_group_desc(sb, 1, &bh);
1526 block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
1527
1528 for (i = 0; i < sbi->s_groups_count; i++) {
1529 gdp = ext4_get_group_desc(sb, i, &bh);
1530
1531 flex_group = ext4_flex_group(sbi, i);
1532 sbi->s_flex_groups[flex_group].free_inodes +=
1533 le16_to_cpu(gdp->bg_free_inodes_count);
1534 sbi->s_flex_groups[flex_group].free_blocks +=
1535 le16_to_cpu(gdp->bg_free_blocks_count);
1536 }
1537
1538 return 1;
1539failed:
1540 return 0;
1541}
1542
Andreas Dilger717d50e2007-10-16 18:38:25 -04001543__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1544 struct ext4_group_desc *gdp)
1545{
1546 __u16 crc = 0;
1547
1548 if (sbi->s_es->s_feature_ro_compat &
1549 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1550 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1551 __le32 le_group = cpu_to_le32(block_group);
1552
1553 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1554 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1555 crc = crc16(crc, (__u8 *)gdp, offset);
1556 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1557 /* for checksum of struct ext4_group_desc do the rest...*/
1558 if ((sbi->s_es->s_feature_incompat &
1559 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1560 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1561 crc = crc16(crc, (__u8 *)gdp + offset,
1562 le16_to_cpu(sbi->s_es->s_desc_size) -
1563 offset);
1564 }
1565
1566 return cpu_to_le16(crc);
1567}
1568
1569int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1570 struct ext4_group_desc *gdp)
1571{
1572 if ((sbi->s_es->s_feature_ro_compat &
1573 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1574 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1575 return 0;
1576
1577 return 1;
1578}
1579
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001580/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001581static int ext4_check_descriptors(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582{
Mingming Cao617ba132006-10-11 01:20:53 -07001583 struct ext4_sb_info *sbi = EXT4_SB(sb);
1584 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1585 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001586 ext4_fsblk_t block_bitmap;
1587 ext4_fsblk_t inode_bitmap;
1588 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04001589 int flexbg_flag = 0;
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001590 ext4_group_t i;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591
Jose R. Santosce421582007-10-16 18:38:25 -04001592 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1593 flexbg_flag = 1;
1594
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04001595 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001597 for (i = 0; i < sbi->s_groups_count; i++) {
1598 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1599
Jose R. Santosce421582007-10-16 18:38:25 -04001600 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001601 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 else
1603 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001604 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001606 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001607 if (block_bitmap < first_block || block_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001608 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1609 "Block bitmap for group %lu not in group "
1610 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001611 return 0;
1612 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001613 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001614 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001615 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1616 "Inode bitmap for group %lu not in group "
1617 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001618 return 0;
1619 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07001620 inode_table = ext4_inode_table(sb, gdp);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001621 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001622 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001623 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1624 "Inode table for group %lu not in group "
1625 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001626 return 0;
1627 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001628 spin_lock(sb_bgl_lock(sbi, i));
Andreas Dilger717d50e2007-10-16 18:38:25 -04001629 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
Josef Bacikc19204b2008-04-29 22:00:28 -04001630 printk(KERN_ERR "EXT4-fs: ext4_check_descriptors: "
1631 "Checksum for group %lu failed (%u!=%u)\n",
1632 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1633 gdp)), le16_to_cpu(gdp->bg_checksum));
Li Zefan7ee1ec42008-09-08 10:47:19 -04001634 if (!(sb->s_flags & MS_RDONLY)) {
1635 spin_unlock(sb_bgl_lock(sbi, i));
Theodore Ts'o8a266462008-07-26 14:34:21 -04001636 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04001637 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04001638 }
Eric Sandeenb5f10ee2008-08-02 21:21:08 -04001639 spin_unlock(sb_bgl_lock(sbi, i));
Jose R. Santosce421582007-10-16 18:38:25 -04001640 if (!flexbg_flag)
1641 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642 }
1643
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001644 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001645 sbi->s_es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001646 return 1;
1647}
1648
Mingming Cao617ba132006-10-11 01:20:53 -07001649/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650 * the superblock) which were deleted from all directories, but held open by
1651 * a process at the time of a crash. We walk the list and try to delete these
1652 * inodes at recovery time (only with a read-write filesystem).
1653 *
1654 * In order to keep the orphan inode chain consistent during traversal (in
1655 * case of crash during recovery), we link each inode into the superblock
1656 * orphan list_head and handle it the same way as an inode deletion during
1657 * normal operation (which journals the operations for us).
1658 *
1659 * We only do an iget() and an iput() on each inode, which is very safe if we
1660 * accidentally point at an in-use or already deleted inode. The worst that
1661 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001662 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001663 * e2fsck was run on this filesystem, and it must have already done the orphan
1664 * inode cleanup for us, so we can safely abort without any further action.
1665 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001666static void ext4_orphan_cleanup(struct super_block *sb,
1667 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001668{
1669 unsigned int s_flags = sb->s_flags;
1670 int nr_orphans = 0, nr_truncates = 0;
1671#ifdef CONFIG_QUOTA
1672 int i;
1673#endif
1674 if (!es->s_last_orphan) {
1675 jbd_debug(4, "no orphan inodes to clean up\n");
1676 return;
1677 }
1678
Eric Sandeena8f48a92006-12-06 20:40:13 -08001679 if (bdev_read_only(sb->s_bdev)) {
1680 printk(KERN_ERR "EXT4-fs: write access "
1681 "unavailable, skipping orphan cleanup.\n");
1682 return;
1683 }
1684
Mingming Cao617ba132006-10-11 01:20:53 -07001685 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001686 if (es->s_last_orphan)
1687 jbd_debug(1, "Errors on filesystem, "
1688 "clearing orphan list.\n");
1689 es->s_last_orphan = 0;
1690 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1691 return;
1692 }
1693
1694 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001695 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001696 sb->s_id);
1697 sb->s_flags &= ~MS_RDONLY;
1698 }
1699#ifdef CONFIG_QUOTA
1700 /* Needed for iput() to work correctly and not trash data */
1701 sb->s_flags |= MS_ACTIVE;
1702 /* Turn on quotas so that they are updated correctly */
1703 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001704 if (EXT4_SB(sb)->s_qf_names[i]) {
1705 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001706 if (ret < 0)
1707 printk(KERN_ERR
Jan Kara2c8be6b2008-05-13 21:27:55 -04001708 "EXT4-fs: Cannot turn on journaled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 "quota: error %d\n", ret);
1710 }
1711 }
1712#endif
1713
1714 while (es->s_last_orphan) {
1715 struct inode *inode;
1716
Josef Bacik97bd42b2008-04-29 22:04:56 -04001717 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1718 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001719 es->s_last_orphan = 0;
1720 break;
1721 }
1722
Mingming Cao617ba132006-10-11 01:20:53 -07001723 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001724 DQUOT_INIT(inode);
1725 if (inode->i_nlink) {
1726 printk(KERN_DEBUG
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001727 "%s: truncating inode %lu to %lld bytes\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001728 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04001729 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001731 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001732 nr_truncates++;
1733 } else {
1734 printk(KERN_DEBUG
1735 "%s: deleting unreferenced inode %lu\n",
Harvey Harrison46e665e2008-04-17 10:38:59 -04001736 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001737 jbd_debug(2, "deleting unreferenced inode %lu\n",
1738 inode->i_ino);
1739 nr_orphans++;
1740 }
1741 iput(inode); /* The delete magic happens here! */
1742 }
1743
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001744#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001745
1746 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001747 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001748 sb->s_id, PLURAL(nr_orphans));
1749 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001750 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001751 sb->s_id, PLURAL(nr_truncates));
1752#ifdef CONFIG_QUOTA
1753 /* Turn quotas off */
1754 for (i = 0; i < MAXQUOTAS; i++) {
1755 if (sb_dqopt(sb)->files[i])
Jan Kara6f28e082008-04-28 02:14:34 -07001756 vfs_quota_off(sb, i, 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001757 }
1758#endif
1759 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1760}
Eric Sandeencd2291a2008-01-28 23:58:27 -05001761/*
1762 * Maximal extent format file size.
1763 * Resulting logical blkno at s_maxbytes must fit in our on-disk
1764 * extent format containers, within a sector_t, and within i_blocks
1765 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
1766 * so that won't be a limiting factor.
1767 *
1768 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
1769 */
1770static loff_t ext4_max_size(int blkbits)
1771{
1772 loff_t res;
1773 loff_t upper_limit = MAX_LFS_FILESIZE;
1774
1775 /* small i_blocks in vfs inode? */
1776 if (sizeof(blkcnt_t) < sizeof(u64)) {
1777 /*
1778 * CONFIG_LSF is not enabled implies the inode
1779 * i_block represent total blocks in 512 bytes
1780 * 32 == size of vfs inode i_blocks * 8
1781 */
1782 upper_limit = (1LL << 32) - 1;
1783
1784 /* total blocks in file system block size */
1785 upper_limit >>= (blkbits - 9);
1786 upper_limit <<= blkbits;
1787 }
1788
1789 /* 32-bit extent-start container, ee_block */
1790 res = 1LL << 32;
1791 res <<= blkbits;
1792 res -= 1;
1793
1794 /* Sanity check against vm- & vfs- imposed limits */
1795 if (res > upper_limit)
1796 res = upper_limit;
1797
1798 return res;
1799}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001800
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001801/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05001802 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001803 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
1804 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001805 */
Eric Sandeencd2291a2008-01-28 23:58:27 -05001806static loff_t ext4_max_bitmap_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001807{
Mingming Cao617ba132006-10-11 01:20:53 -07001808 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001809 int meta_blocks;
1810 loff_t upper_limit;
1811 /* This is calculated to be the largest file size for a
Eric Sandeencd2291a2008-01-28 23:58:27 -05001812 * dense, bitmapped file such that the total number of
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001814 * does not exceed 2^48 -1
1815 * __u32 i_blocks_lo and _u16 i_blocks_high representing the
1816 * total number of 512 bytes blocks of the file
1817 */
1818
1819 if (sizeof(blkcnt_t) < sizeof(u64)) {
1820 /*
1821 * CONFIG_LSF is not enabled implies the inode
1822 * i_block represent total blocks in 512 bytes
1823 * 32 == size of vfs inode i_blocks * 8
1824 */
1825 upper_limit = (1LL << 32) - 1;
1826
1827 /* total blocks in file system block size */
1828 upper_limit >>= (bits - 9);
1829
1830 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05001831 /*
1832 * We use 48 bit ext4_inode i_blocks
1833 * With EXT4_HUGE_FILE_FL set the i_blocks
1834 * represent total number of blocks in
1835 * file system block size
1836 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001837 upper_limit = (1LL << 48) - 1;
1838
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001839 }
1840
1841 /* indirect blocks */
1842 meta_blocks = 1;
1843 /* double indirect blocks */
1844 meta_blocks += 1 + (1LL << (bits-2));
1845 /* tripple indirect blocks */
1846 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1847
1848 upper_limit -= meta_blocks;
1849 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001850
1851 res += 1LL << (bits-2);
1852 res += 1LL << (2*(bits-2));
1853 res += 1LL << (3*(bits-2));
1854 res <<= bits;
1855 if (res > upper_limit)
1856 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05001857
1858 if (res > MAX_LFS_FILESIZE)
1859 res = MAX_LFS_FILESIZE;
1860
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001861 return res;
1862}
1863
Mingming Cao617ba132006-10-11 01:20:53 -07001864static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001865 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001866{
Mingming Cao617ba132006-10-11 01:20:53 -07001867 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05001868 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001869 int has_super = 0;
1870
1871 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1872
Mingming Cao617ba132006-10-11 01:20:53 -07001873 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001874 nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001875 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001876 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001877 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001878 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001879 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001880}
1881
Alex Tomasc9de5602008-01-29 00:19:52 -05001882/**
1883 * ext4_get_stripe_size: Get the stripe size.
1884 * @sbi: In memory super block info
1885 *
1886 * If we have specified it via mount option, then
1887 * use the mount option value. If the value specified at mount time is
1888 * greater than the blocks per group use the super block value.
1889 * If the super block value is greater than blocks per group return 0.
1890 * Allocator needs it be less than blocks per group.
1891 *
1892 */
1893static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
1894{
1895 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
1896 unsigned long stripe_width =
1897 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
1898
1899 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
1900 return sbi->s_stripe;
1901
1902 if (stripe_width <= sbi->s_blocks_per_group)
1903 return stripe_width;
1904
1905 if (stride <= sbi->s_blocks_per_group)
1906 return stride;
1907
1908 return 0;
1909}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001910
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001911static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Aneesh Kumar K.V74778272008-07-11 19:27:31 -04001912 __releases(kernel_lock)
1913 __acquires(kernel_lock)
Aneesh Kumar K.V1d03ec92008-01-28 23:58:27 -05001914
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001915{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001916 struct buffer_head *bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001917 struct ext4_super_block *es = NULL;
1918 struct ext4_sb_info *sbi;
1919 ext4_fsblk_t block;
1920 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001921 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001922 unsigned long offset = 0;
1923 unsigned int journal_inum = 0;
1924 unsigned long journal_devnum = 0;
1925 unsigned long def_mount_opts;
1926 struct inode *root;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001927 char *cp;
David Howells1d1fe1e2008-02-07 00:15:37 -08001928 int ret = -EINVAL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001929 int blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001930 int db_count;
1931 int i;
1932 int needs_recovery;
1933 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001934 __u64 blocks_count;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001935 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936
1937 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1938 if (!sbi)
1939 return -ENOMEM;
1940 sb->s_fs_info = sbi;
1941 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001942 sbi->s_resuid = EXT4_DEF_RESUID;
1943 sbi->s_resgid = EXT4_DEF_RESGID;
Theodore Ts'o240799c2008-10-09 23:53:47 -04001944 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07001945 sbi->s_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946
1947 unlock_kernel();
1948
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04001949 /* Cleanup superblock name */
1950 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
1951 *cp = '!';
1952
Mingming Cao617ba132006-10-11 01:20:53 -07001953 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001954 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001955 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001956 goto out_fail;
1957 }
1958
1959 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001960 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001961 * block sizes. We need to calculate the offset from buffer start.
1962 */
Mingming Cao617ba132006-10-11 01:20:53 -07001963 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001964 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1965 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001966 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001967 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001968 }
1969
Andrew Morton70bbb3e2006-10-11 01:21:20 -07001970 if (!(bh = sb_bread(sb, logical_sb_block))) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001971 printk(KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 goto out_fail;
1973 }
1974 /*
1975 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001976 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001977 */
Mingming Cao617ba132006-10-11 01:20:53 -07001978 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001979 sbi->s_es = es;
1980 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001981 if (sb->s_magic != EXT4_SUPER_MAGIC)
1982 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001983
1984 /* Set defaults before we parse the mount options */
1985 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001986 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001987 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001988 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001989 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001990 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001991 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001992#ifdef CONFIG_EXT4DEV_FS_XATTR
Mingming Cao617ba132006-10-11 01:20:53 -07001993 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001994 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001995#endif
1996#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Mingming Cao617ba132006-10-11 01:20:53 -07001997 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001998 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001999#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002000 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
2001 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
2002 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
2003 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
2004 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
2005 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002006
Mingming Cao617ba132006-10-11 01:20:53 -07002007 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002008 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002009 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Dmitry Mishinceea16b2006-10-11 01:21:21 -07002010 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05002011 else
2012 set_opt(sbi->s_mount_opt, ERRORS_RO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002013
2014 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2015 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
2016
2017 set_opt(sbi->s_mount_opt, RESERVATION);
Eric Sandeen571640c2008-05-26 12:29:46 -04002018 set_opt(sbi->s_mount_opt, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002019
Mingming Cao1e2462f2007-07-18 09:00:55 -04002020 /*
2021 * turn on extents feature by default in ext4 filesystem
Eric Sandeene4079a12008-07-11 19:27:31 -04002022 * only if feature flag already set by mkfs or tune2fs.
2023 * Use -o noextents to turn it off
Mingming Cao1e2462f2007-07-18 09:00:55 -04002024 */
Eric Sandeene4079a12008-07-11 19:27:31 -04002025 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
2026 set_opt(sbi->s_mount_opt, EXTENTS);
2027 else
2028 ext4_warning(sb, __func__,
2029 "extents feature not enabled on this filesystem, "
2030 "use tune2fs.\n");
Mingming Cao1e2462f2007-07-18 09:00:55 -04002031
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002032 /*
2033 * enable delayed allocation by default
2034 * Use -o nodelalloc to turn it off
2035 */
2036 set_opt(sbi->s_mount_opt, DELALLOC);
2037
2038
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002039 if (!parse_options((char *) data, sb, &journal_inum, &journal_devnum,
2040 NULL, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002041 goto failed_mount;
2042
2043 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002044 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002045
Mingming Cao617ba132006-10-11 01:20:53 -07002046 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2047 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2048 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2049 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002050 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002051 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 "running e2fsck is recommended\n");
Theodore Tso469108f2008-02-10 01:11:44 -05002053
2054 /*
2055 * Since ext4 is still considered development code, we require
2056 * that the TEST_FILESYS flag in s->flags be set.
2057 */
2058 if (!(le32_to_cpu(es->s_flags) & EXT2_FLAGS_TEST_FILESYS)) {
2059 printk(KERN_WARNING "EXT4-fs: %s: not marked "
2060 "OK to use with test code.\n", sb->s_id);
2061 goto failed_mount;
2062 }
2063
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002064 /*
2065 * Check feature flags regardless of the revision level, since we
2066 * previously didn't change the revision level when setting the flags,
2067 * so there is a chance incompat flags are set on a rev 0 filesystem.
2068 */
Mingming Cao617ba132006-10-11 01:20:53 -07002069 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002070 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002071 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002072 "unsupported optional features (%x).\n",
2073 sb->s_id, le32_to_cpu(features));
2074 goto failed_mount;
2075 }
Mingming Cao617ba132006-10-11 01:20:53 -07002076 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002077 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07002078 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002079 "unsupported optional features (%x).\n",
2080 sb->s_id, le32_to_cpu(features));
2081 goto failed_mount;
2082 }
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002083 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2084 /*
2085 * Large file size enabled file system can only be
2086 * mount if kernel is build with CONFIG_LSF
2087 */
2088 if (sizeof(root->i_blocks) < sizeof(u64) &&
2089 !(sb->s_flags & MS_RDONLY)) {
2090 printk(KERN_ERR "EXT4-fs: %s: Filesystem with huge "
2091 "files cannot be mounted read-write "
2092 "without CONFIG_LSF.\n", sb->s_id);
2093 goto failed_mount;
2094 }
2095 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002096 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2097
Mingming Cao617ba132006-10-11 01:20:53 -07002098 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2099 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002100 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002101 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002102 blocksize, sb->s_id);
2103 goto failed_mount;
2104 }
2105
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002106 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05002107
2108 /* Validate the filesystem blocksize */
2109 if (!sb_set_blocksize(sb, blocksize)) {
2110 printk(KERN_ERR "EXT4-fs: bad block size %d.\n",
2111 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002112 goto failed_mount;
2113 }
2114
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002115 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002116 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2117 offset = do_div(logical_sb_block, blocksize);
2118 bh = sb_bread(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002119 if (!bh) {
2120 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002121 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 goto failed_mount;
2123 }
Mingming Cao617ba132006-10-11 01:20:53 -07002124 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002125 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07002126 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002127 printk(KERN_ERR
2128 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002129 goto failed_mount;
2130 }
2131 }
2132
Eric Sandeene2b46572008-01-28 23:58:27 -05002133 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits);
Mingming Cao617ba132006-10-11 01:20:53 -07002134 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135
Mingming Cao617ba132006-10-11 01:20:53 -07002136 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2137 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2138 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139 } else {
2140 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2141 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07002142 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Vignesh Babu13305932007-07-18 09:11:02 -04002143 (!is_power_of_2(sbi->s_inode_size)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002144 (sbi->s_inode_size > blocksize)) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002145 printk(KERN_ERR
2146 "EXT4-fs: unsupported inode size: %d\n",
2147 sbi->s_inode_size);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002148 goto failed_mount;
2149 }
Kalpak Shahef7f3832007-07-18 09:15:20 -04002150 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2151 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002152 }
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002153 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2154 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002155 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002156 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07002157 !is_power_of_2(sbi->s_desc_size)) {
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002158 printk(KERN_ERR
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002159 "EXT4-fs: unsupported descriptor size %lu\n",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002160 sbi->s_desc_size);
2161 goto failed_mount;
2162 }
2163 } else
2164 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002165 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002166 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08002167 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002168 goto cantfind_ext4;
2169 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002170 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07002171 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002172 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2173 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07002174 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002175 sbi->s_sbh = bh;
2176 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07002177 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2178 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002179 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2181 sbi->s_def_hash_version = es->s_def_hash_version;
2182
2183 if (sbi->s_blocks_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002184 printk(KERN_ERR
2185 "EXT4-fs: #blocks per group too big: %lu\n",
2186 sbi->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002187 goto failed_mount;
2188 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002189 if (sbi->s_inodes_per_group > blocksize * 8) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002190 printk(KERN_ERR
2191 "EXT4-fs: #inodes per group too big: %lu\n",
2192 sbi->s_inodes_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002193 goto failed_mount;
2194 }
2195
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002196 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002197 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002198 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199 " too large to mount safely\n", sb->s_id);
2200 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07002201 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002202 "enabled\n");
2203 goto failed_mount;
2204 }
2205
Mingming Cao617ba132006-10-11 01:20:53 -07002206 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2207 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05002208
2209 /* ensure blocks_count calculation below doesn't sign-extend */
2210 if (ext4_blocks_count(es) + EXT4_BLOCKS_PER_GROUP(sb) <
2211 le32_to_cpu(es->s_first_data_block) + 1) {
2212 printk(KERN_WARNING "EXT4-fs: bad geometry: block count %llu, "
2213 "first data block %u, blocks per group %lu\n",
2214 ext4_blocks_count(es),
2215 le32_to_cpu(es->s_first_data_block),
2216 EXT4_BLOCKS_PER_GROUP(sb));
2217 goto failed_mount;
2218 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002219 blocks_count = (ext4_blocks_count(es) -
2220 le32_to_cpu(es->s_first_data_block) +
2221 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2222 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
2223 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07002224 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2225 EXT4_DESC_PER_BLOCK(sb);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002226 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002227 GFP_KERNEL);
2228 if (sbi->s_group_desc == NULL) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002229 printk(KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002230 goto failed_mount;
2231 }
2232
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002233 if (ext4_proc_root)
2234 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
2235
Theodore Ts'o240799c2008-10-09 23:53:47 -04002236 if (sbi->s_proc)
2237 proc_create_data("inode_readahead_blks", 0644, sbi->s_proc,
2238 &ext4_ui_proc_fops,
2239 &sbi->s_inode_readahead_blks);
2240
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002241 bgl_lock_init(&sbi->s_blockgroup_lock);
2242
2243 for (i = 0; i < db_count; i++) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07002244 block = descriptor_loc(sb, logical_sb_block, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002245 sbi->s_group_desc[i] = sb_bread(sb, block);
2246 if (!sbi->s_group_desc[i]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002247 printk(KERN_ERR "EXT4-fs: "
2248 "can't read group descriptor %d\n", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 db_count = i;
2250 goto failed_mount2;
2251 }
2252 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002253 if (!ext4_check_descriptors(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002254 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002255 goto failed_mount2;
2256 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002257 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2258 if (!ext4_fill_flex_info(sb)) {
2259 printk(KERN_ERR
2260 "EXT4-fs: unable to initialize "
2261 "flex_bg meta info!\n");
2262 goto failed_mount2;
2263 }
2264
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002265 sbi->s_gdb_count = db_count;
2266 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2267 spin_lock_init(&sbi->s_next_gen_lock);
2268
Peter Zijlstra833f4072007-10-16 23:25:45 -07002269 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2270 ext4_count_free_blocks(sb));
2271 if (!err) {
2272 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2273 ext4_count_free_inodes(sb));
2274 }
2275 if (!err) {
2276 err = percpu_counter_init(&sbi->s_dirs_counter,
2277 ext4_count_dirs(sb));
2278 }
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002279 if (!err) {
2280 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2281 }
Peter Zijlstra833f4072007-10-16 23:25:45 -07002282 if (err) {
2283 printk(KERN_ERR "EXT4-fs: insufficient memory\n");
2284 goto failed_mount3;
2285 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002286
Alex Tomasc9de5602008-01-29 00:19:52 -05002287 sbi->s_stripe = ext4_get_stripe_size(sbi);
2288
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289 /*
2290 * set up enough so that it can read an inode
2291 */
Mingming Cao617ba132006-10-11 01:20:53 -07002292 sb->s_op = &ext4_sops;
2293 sb->s_export_op = &ext4_export_ops;
2294 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002295#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07002296 sb->s_qcop = &ext4_qctl_operations;
2297 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002298#endif
2299 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
2300
2301 sb->s_root = NULL;
2302
2303 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07002304 EXT4_HAS_INCOMPAT_FEATURE(sb,
2305 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002306
2307 /*
2308 * The first inode we look at is the journal inode. Don't try
2309 * root first: it may be modified in the journal!
2310 */
2311 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07002312 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2313 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002314 goto failed_mount3;
Theodore Ts'o624080e2008-06-06 17:50:40 -04002315 if (!(sb->s_flags & MS_RDONLY) &&
2316 EXT4_SB(sb)->s_journal->j_failed_commit) {
2317 printk(KERN_CRIT "EXT4-fs error (device %s): "
2318 "ext4_fill_super: Journal transaction "
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002319 "%u is corrupt\n", sb->s_id,
Theodore Ts'o624080e2008-06-06 17:50:40 -04002320 EXT4_SB(sb)->s_journal->j_failed_commit);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002321 if (test_opt(sb, ERRORS_RO)) {
2322 printk(KERN_CRIT
2323 "Mounting filesystem read-only\n");
Theodore Ts'o624080e2008-06-06 17:50:40 -04002324 sb->s_flags |= MS_RDONLY;
2325 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2326 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2327 }
2328 if (test_opt(sb, ERRORS_PANIC)) {
2329 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2330 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2331 ext4_commit_super(sb, es, 1);
2332 printk(KERN_CRIT
2333 "EXT4-fs (device %s): mount failed\n",
2334 sb->s_id);
2335 goto failed_mount4;
2336 }
2337 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002338 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002339 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002340 goto failed_mount3;
2341 } else {
2342 if (!silent)
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002343 printk(KERN_ERR
2344 "ext4: No journal on filesystem on %s\n",
2345 sb->s_id);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002346 goto failed_mount3;
2347 }
2348
Jose R. Santoseb40a092007-07-18 08:37:25 -04002349 if (ext4_blocks_count(es) > 0xffffffffULL &&
2350 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2351 JBD2_FEATURE_INCOMPAT_64BIT)) {
2352 printk(KERN_ERR "ext4: Failed to set 64-bit journal feature\n");
2353 goto failed_mount4;
2354 }
2355
Girish Shilamkar818d2762008-01-28 23:58:27 -05002356 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2357 jbd2_journal_set_features(sbi->s_journal,
2358 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2359 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2360 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2361 jbd2_journal_set_features(sbi->s_journal,
2362 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
2363 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2364 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2365 } else {
2366 jbd2_journal_clear_features(sbi->s_journal,
2367 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2368 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2369 }
2370
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002371 /* We have now updated the journal if required, so we can
2372 * validate the data journaling mode. */
2373 switch (test_opt(sb, DATA_FLAGS)) {
2374 case 0:
2375 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07002376 * capabilities: ORDERED_DATA if the journal can
2377 * cope, else JOURNAL_DATA
2378 */
Mingming Caodab291a2006-10-11 01:21:01 -07002379 if (jbd2_journal_check_available_features
2380 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002381 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2382 else
2383 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2384 break;
2385
Mingming Cao617ba132006-10-11 01:20:53 -07002386 case EXT4_MOUNT_ORDERED_DATA:
2387 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07002388 if (!jbd2_journal_check_available_features
2389 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002390 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002391 "requested data journaling mode\n");
2392 goto failed_mount4;
2393 }
2394 default:
2395 break;
2396 }
2397
2398 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002399 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
2400 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002401 "its supported only with writeback mode\n");
2402 clear_opt(sbi->s_mount_opt, NOBH);
2403 }
2404 }
2405 /*
Mingming Caodab291a2006-10-11 01:21:01 -07002406 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002407 * so we can safely mount the rest of the filesystem now.
2408 */
2409
David Howells1d1fe1e2008-02-07 00:15:37 -08002410 root = ext4_iget(sb, EXT4_ROOT_INO);
2411 if (IS_ERR(root)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002412 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
David Howells1d1fe1e2008-02-07 00:15:37 -08002413 ret = PTR_ERR(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002414 goto failed_mount4;
2415 }
2416 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells1d1fe1e2008-02-07 00:15:37 -08002417 iput(root);
Mingming Cao617ba132006-10-11 01:20:53 -07002418 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002419 goto failed_mount4;
2420 }
David Howells1d1fe1e2008-02-07 00:15:37 -08002421 sb->s_root = d_alloc_root(root);
2422 if (!sb->s_root) {
2423 printk(KERN_ERR "EXT4-fs: get root dentry failed\n");
2424 iput(root);
2425 ret = -ENOMEM;
2426 goto failed_mount4;
2427 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002428
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002429 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
Kalpak Shahef7f3832007-07-18 09:15:20 -04002430
2431 /* determine the minimum size of new large inodes, if present */
2432 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2433 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2434 EXT4_GOOD_OLD_INODE_SIZE;
2435 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2436 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2437 if (sbi->s_want_extra_isize <
2438 le16_to_cpu(es->s_want_extra_isize))
2439 sbi->s_want_extra_isize =
2440 le16_to_cpu(es->s_want_extra_isize);
2441 if (sbi->s_want_extra_isize <
2442 le16_to_cpu(es->s_min_extra_isize))
2443 sbi->s_want_extra_isize =
2444 le16_to_cpu(es->s_min_extra_isize);
2445 }
2446 }
2447 /* Check if enough inode space is available */
2448 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2449 sbi->s_inode_size) {
2450 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2451 EXT4_GOOD_OLD_INODE_SIZE;
2452 printk(KERN_INFO "EXT4-fs: required extra inode space not"
2453 "available.\n");
2454 }
2455
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002456 /*
2457 * akpm: core read_super() calls in here with the superblock locked.
2458 * That deadlocks, because orphan cleanup needs to lock the superblock
2459 * in numerous places. Here we just pop the lock - it's relatively
2460 * harmless, because we are now ready to accept write_super() requests,
2461 * and aviro says that's the only reason for hanging onto the
2462 * superblock lock.
2463 */
Mingming Cao617ba132006-10-11 01:20:53 -07002464 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
2465 ext4_orphan_cleanup(sb, es);
2466 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002467 if (needs_recovery)
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002468 printk(KERN_INFO "EXT4-fs: recovery complete.\n");
Mingming Cao617ba132006-10-11 01:20:53 -07002469 ext4_mark_recovery_complete(sb, es);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002470 printk(KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
2471 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
2472 test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
2473 "writeback");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002474
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04002475 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2476 printk(KERN_WARNING "EXT4-fs: Ignoring delalloc option - "
2477 "requested data journaling mode\n");
2478 clear_opt(sbi->s_mount_opt, DELALLOC);
2479 } else if (test_opt(sb, DELALLOC))
2480 printk(KERN_INFO "EXT4-fs: delayed allocation enabled\n");
2481
Alex Tomasa86c6182006-10-11 01:21:03 -07002482 ext4_ext_init(sb);
Theodore Ts'oc2ea3fd2008-10-10 09:40:52 -04002483 err = ext4_mb_init(sb, needs_recovery);
2484 if (err) {
2485 printk(KERN_ERR "EXT4-fs: failed to initalize mballoc (%d)\n",
2486 err);
2487 goto failed_mount4;
2488 }
Alex Tomasa86c6182006-10-11 01:21:03 -07002489
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 lock_kernel();
2491 return 0;
2492
Mingming Cao617ba132006-10-11 01:20:53 -07002493cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002494 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07002495 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002496 sb->s_id);
2497 goto failed_mount;
2498
2499failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07002500 jbd2_journal_destroy(sbi->s_journal);
Jan Kara47b4a502008-07-11 19:27:31 -04002501 sbi->s_journal = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002502failed_mount3:
2503 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2504 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2505 percpu_counter_destroy(&sbi->s_dirs_counter);
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04002506 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002507failed_mount2:
2508 for (i = 0; i < db_count; i++)
2509 brelse(sbi->s_group_desc[i]);
2510 kfree(sbi->s_group_desc);
2511failed_mount:
Theodore Ts'o240799c2008-10-09 23:53:47 -04002512 if (sbi->s_proc) {
2513 remove_proc_entry("inode_readahead_blks", sbi->s_proc);
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04002514 remove_proc_entry(sb->s_id, ext4_proc_root);
Theodore Ts'o240799c2008-10-09 23:53:47 -04002515 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002516#ifdef CONFIG_QUOTA
2517 for (i = 0; i < MAXQUOTAS; i++)
2518 kfree(sbi->s_qf_names[i]);
2519#endif
Mingming Cao617ba132006-10-11 01:20:53 -07002520 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002521 brelse(bh);
2522out_fail:
2523 sb->s_fs_info = NULL;
2524 kfree(sbi);
2525 lock_kernel();
David Howells1d1fe1e2008-02-07 00:15:37 -08002526 return ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002527}
2528
2529/*
2530 * Setup any per-fs journal parameters now. We'll do this both on
2531 * initial mount, once the journal has been initialised but before we've
2532 * done any recovery; and again on any subsequent remount.
2533 */
Mingming Cao617ba132006-10-11 01:20:53 -07002534static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002535{
Mingming Cao617ba132006-10-11 01:20:53 -07002536 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002537
2538 if (sbi->s_commit_interval)
2539 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07002540 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002541 * interval here, but for now we'll just fall back to the jbd
2542 * default. */
2543
2544 spin_lock(&journal->j_state_lock);
2545 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07002546 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002547 else
Mingming Caodab291a2006-10-11 01:21:01 -07002548 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002549 spin_unlock(&journal->j_state_lock);
2550}
2551
Mingming Cao617ba132006-10-11 01:20:53 -07002552static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553 unsigned int journal_inum)
2554{
2555 struct inode *journal_inode;
2556 journal_t *journal;
2557
2558 /* First, test for the existence of a valid inode on disk. Bad
2559 * things happen if we iget() an unused inode, as the subsequent
2560 * iput() will try to delete it. */
2561
David Howells1d1fe1e2008-02-07 00:15:37 -08002562 journal_inode = ext4_iget(sb, journal_inum);
2563 if (IS_ERR(journal_inode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002564 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002565 return NULL;
2566 }
2567 if (!journal_inode->i_nlink) {
2568 make_bad_inode(journal_inode);
2569 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002570 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002571 return NULL;
2572 }
2573
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002574 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002575 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08002576 if (!S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002577 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002578 iput(journal_inode);
2579 return NULL;
2580 }
2581
Mingming Caodab291a2006-10-11 01:21:01 -07002582 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002583 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002584 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002585 iput(journal_inode);
2586 return NULL;
2587 }
2588 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002589 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002590 return journal;
2591}
2592
Mingming Cao617ba132006-10-11 01:20:53 -07002593static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002594 dev_t j_dev)
2595{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002596 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07002598 ext4_fsblk_t start;
2599 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002600 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07002601 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002602 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002603 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002604 struct block_device *bdev;
2605
Mingming Cao617ba132006-10-11 01:20:53 -07002606 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607 if (bdev == NULL)
2608 return NULL;
2609
2610 if (bd_claim(bdev, sb)) {
2611 printk(KERN_ERR
Dave Kleikamp8c55e202007-05-24 13:04:54 -04002612 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002613 blkdev_put(bdev);
2614 return NULL;
2615 }
2616
2617 blocksize = sb->s_blocksize;
2618 hblock = bdev_hardsect_size(bdev);
2619 if (blocksize < hblock) {
2620 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07002621 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002622 goto out_bdev;
2623 }
2624
Mingming Cao617ba132006-10-11 01:20:53 -07002625 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
2626 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002627 set_blocksize(bdev, blocksize);
2628 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07002629 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002630 "external journal\n");
2631 goto out_bdev;
2632 }
2633
Mingming Cao617ba132006-10-11 01:20:53 -07002634 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
2635 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002636 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07002637 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
2638 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002639 "bad superblock\n");
2640 brelse(bh);
2641 goto out_bdev;
2642 }
2643
Mingming Cao617ba132006-10-11 01:20:53 -07002644 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
2645 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002646 brelse(bh);
2647 goto out_bdev;
2648 }
2649
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002650 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002651 start = sb_block + 1;
2652 brelse(bh); /* we're done with the superblock */
2653
Mingming Caodab291a2006-10-11 01:21:01 -07002654 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002655 start, len, blocksize);
2656 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07002657 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002658 goto out_bdev;
2659 }
2660 journal->j_private = sb;
2661 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2662 wait_on_buffer(journal->j_sb_buffer);
2663 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002664 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002665 goto out_journal;
2666 }
2667 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002668 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002669 "user (unsupported) - %d\n",
2670 be32_to_cpu(journal->j_superblock->s_nr_users));
2671 goto out_journal;
2672 }
Mingming Cao617ba132006-10-11 01:20:53 -07002673 EXT4_SB(sb)->journal_bdev = bdev;
2674 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002675 return journal;
2676out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002677 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002678out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002679 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002680 return NULL;
2681}
2682
Mingming Cao617ba132006-10-11 01:20:53 -07002683static int ext4_load_journal(struct super_block *sb,
2684 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002685 unsigned long journal_devnum)
2686{
2687 journal_t *journal;
2688 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2689 dev_t journal_dev;
2690 int err = 0;
2691 int really_read_only;
2692
2693 if (journal_devnum &&
2694 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002695 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002696 "numbers have changed\n");
2697 journal_dev = new_decode_dev(journal_devnum);
2698 } else
2699 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2700
2701 really_read_only = bdev_read_only(sb->s_bdev);
2702
2703 /*
2704 * Are we loading a blank journal or performing recovery after a
2705 * crash? For recovery, we need to check in advance whether we
2706 * can get read-write access to the device.
2707 */
2708
Mingming Cao617ba132006-10-11 01:20:53 -07002709 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002710 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002711 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002712 "required on readonly filesystem.\n");
2713 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002714 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002715 "unavailable, cannot proceed.\n");
2716 return -EROFS;
2717 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002718 printk(KERN_INFO "EXT4-fs: write access will "
2719 "be enabled during recovery.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002720 }
2721 }
2722
2723 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002724 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002725 "and inode journals!\n");
2726 return -EINVAL;
2727 }
2728
2729 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002730 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002731 return -EINVAL;
2732 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002733 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002734 return -EINVAL;
2735 }
2736
Theodore Ts'o4776004f2008-09-08 23:00:52 -04002737 if (journal->j_flags & JBD2_BARRIER)
2738 printk(KERN_INFO "EXT4-fs: barriers enabled\n");
2739 else
2740 printk(KERN_INFO "EXT4-fs: barriers disabled\n");
2741
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002742 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002743 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002744 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002745 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002746 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002747 return err;
2748 }
2749 }
2750
Mingming Cao617ba132006-10-11 01:20:53 -07002751 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002752 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002753 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002754 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002755
2756 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002757 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002758 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002759 return err;
2760 }
2761
Mingming Cao617ba132006-10-11 01:20:53 -07002762 EXT4_SB(sb)->s_journal = journal;
2763 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002764
2765 if (journal_devnum &&
2766 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2767 es->s_journal_dev = cpu_to_le32(journal_devnum);
2768 sb->s_dirt = 1;
2769
2770 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002771 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002772 }
2773
2774 return 0;
2775}
2776
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002777static int ext4_create_journal(struct super_block *sb,
2778 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002779 unsigned int journal_inum)
2780{
2781 journal_t *journal;
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002782 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002783
2784 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002785 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002786 "create journal.\n");
2787 return -EROFS;
2788 }
2789
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002790 journal = ext4_get_journal(sb, journal_inum);
2791 if (!journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002792 return -EINVAL;
2793
Mingming Cao617ba132006-10-11 01:20:53 -07002794 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002795 journal_inum);
2796
Borislav Petkov6c675bd2007-07-15 23:41:45 -07002797 err = jbd2_journal_create(journal);
2798 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002799 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002800 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002801 return -EIO;
2802 }
2803
Mingming Cao617ba132006-10-11 01:20:53 -07002804 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002805
Mingming Cao617ba132006-10-11 01:20:53 -07002806 ext4_update_dynamic_rev(sb);
2807 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2808 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002809
2810 es->s_journal_inum = cpu_to_le32(journal_inum);
2811 sb->s_dirt = 1;
2812
2813 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002814 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815
2816 return 0;
2817}
2818
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002819static void ext4_commit_super(struct super_block *sb,
2820 struct ext4_super_block *es, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002821{
Mingming Cao617ba132006-10-11 01:20:53 -07002822 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002823
2824 if (!sbh)
2825 return;
Theodore Ts'o914258b2008-10-06 21:35:40 -04002826 if (buffer_write_io_error(sbh)) {
2827 /*
2828 * Oh, dear. A previous attempt to write the
2829 * superblock failed. This could happen because the
2830 * USB device was yanked out. Or it could happen to
2831 * be a transient write error and maybe the block will
2832 * be remapped. Nothing we can do but to retry the
2833 * write and hope for the best.
2834 */
2835 printk(KERN_ERR "ext4: previous I/O error to "
2836 "superblock detected for %s.\n", sb->s_id);
2837 clear_buffer_write_io_error(sbh);
2838 set_buffer_uptodate(sbh);
2839 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002840 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002841 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002842 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002843 BUFFER_TRACE(sbh, "marking dirty");
2844 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002845 if (sync) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002846 sync_dirty_buffer(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04002847 if (buffer_write_io_error(sbh)) {
2848 printk(KERN_ERR "ext4: I/O error while writing "
2849 "superblock for %s.\n", sb->s_id);
2850 clear_buffer_write_io_error(sbh);
2851 set_buffer_uptodate(sbh);
2852 }
2853 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002854}
2855
2856
2857/*
2858 * Have we just finished recovery? If so, and if we are mounting (or
2859 * remounting) the filesystem readonly, then we will end up with a
2860 * consistent fs on disk. Record that fact.
2861 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002862static void ext4_mark_recovery_complete(struct super_block *sb,
2863 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002864{
Mingming Cao617ba132006-10-11 01:20:53 -07002865 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866
Mingming Caodab291a2006-10-11 01:21:01 -07002867 jbd2_journal_lock_updates(journal);
2868 jbd2_journal_flush(journal);
Jan Kara32c37732007-07-15 23:41:09 -07002869 lock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07002870 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002871 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002872 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002873 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002874 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002875 }
Jan Kara32c37732007-07-15 23:41:09 -07002876 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002877 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002878}
2879
2880/*
2881 * If we are mounting (or read-write remounting) a filesystem whose journal
2882 * has recorded an error from a previous lifetime, move that error to the
2883 * main filesystem now.
2884 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002885static void ext4_clear_journal_err(struct super_block *sb,
2886 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887{
2888 journal_t *journal;
2889 int j_errno;
2890 const char *errstr;
2891
Mingming Cao617ba132006-10-11 01:20:53 -07002892 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002893
2894 /*
2895 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002896 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002897 */
2898
Mingming Caodab291a2006-10-11 01:21:01 -07002899 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 if (j_errno) {
2901 char nbuf[16];
2902
Mingming Cao617ba132006-10-11 01:20:53 -07002903 errstr = ext4_decode_error(sb, j_errno, nbuf);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002904 ext4_warning(sb, __func__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002905 "from previous mount: %s", errstr);
Harvey Harrison46e665e2008-04-17 10:38:59 -04002906 ext4_warning(sb, __func__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002907 "filesystem check.");
2908
Mingming Cao617ba132006-10-11 01:20:53 -07002909 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2910 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002911 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912
Mingming Caodab291a2006-10-11 01:21:01 -07002913 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002914 }
2915}
2916
2917/*
2918 * Force the running and committing transactions to commit,
2919 * and wait on the commit.
2920 */
Mingming Cao617ba132006-10-11 01:20:53 -07002921int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002922{
2923 journal_t *journal;
2924 int ret;
2925
2926 if (sb->s_flags & MS_RDONLY)
2927 return 0;
2928
Mingming Cao617ba132006-10-11 01:20:53 -07002929 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002930 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002931 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002932 return ret;
2933}
2934
2935/*
Mingming Cao617ba132006-10-11 01:20:53 -07002936 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 * have to propagate any other updates to the superblock on disk at this
2938 * point. Just start an async writeback to get the buffers on their way
2939 * to the disk.
2940 *
2941 * This implicitly triggers the writebehind on sync().
2942 */
2943
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002944static void ext4_write_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002945{
2946 if (mutex_trylock(&sb->s_lock) != 0)
2947 BUG();
2948 sb->s_dirt = 0;
2949}
2950
Mingming Cao617ba132006-10-11 01:20:53 -07002951static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002952{
2953 tid_t target;
2954
Theodore Ts'oede86cc2008-10-05 20:50:06 -04002955 trace_mark(ext4_sync_fs, "dev %s wait %d", sb->s_id, wait);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002956 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002957 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002958 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002959 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960 }
2961 return 0;
2962}
2963
2964/*
2965 * LVM calls this function before a (read-only) snapshot is created. This
2966 * gives us a chance to flush the journal completely and mark the fs clean.
2967 */
Mingming Cao617ba132006-10-11 01:20:53 -07002968static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002969{
2970 sb->s_dirt = 0;
2971
2972 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002973 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002974
2975 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002976 jbd2_journal_lock_updates(journal);
2977 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002978
2979 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002980 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2981 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002982 }
2983}
2984
2985/*
2986 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2987 * flag here, even though the filesystem is not technically dirty yet.
2988 */
Mingming Cao617ba132006-10-11 01:20:53 -07002989static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002990{
2991 if (!(sb->s_flags & MS_RDONLY)) {
2992 lock_super(sb);
2993 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002994 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2995 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002997 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002998 }
2999}
3000
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003001static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003002{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003003 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07003004 struct ext4_sb_info *sbi = EXT4_SB(sb);
3005 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07003007 struct ext4_mount_options old_opts;
Theodore Ts'o8a266462008-07-26 14:34:21 -04003008 ext4_group_t g;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003009 int err;
3010#ifdef CONFIG_QUOTA
3011 int i;
3012#endif
3013
3014 /* Store the original options */
3015 old_sb_flags = sb->s_flags;
3016 old_opts.s_mount_opt = sbi->s_mount_opt;
3017 old_opts.s_resuid = sbi->s_resuid;
3018 old_opts.s_resgid = sbi->s_resgid;
3019 old_opts.s_commit_interval = sbi->s_commit_interval;
3020#ifdef CONFIG_QUOTA
3021 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3022 for (i = 0; i < MAXQUOTAS; i++)
3023 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3024#endif
3025
3026 /*
3027 * Allow the "check" option to be passed as a remount option.
3028 */
3029 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
3030 err = -EINVAL;
3031 goto restore_opts;
3032 }
3033
Mingming Cao617ba132006-10-11 01:20:53 -07003034 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
Harvey Harrison46e665e2008-04-17 10:38:59 -04003035 ext4_abort(sb, __func__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003036
3037 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07003038 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003039
3040 es = sbi->s_es;
3041
Mingming Cao617ba132006-10-11 01:20:53 -07003042 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003043
3044 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003045 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07003046 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003047 err = -EROFS;
3048 goto restore_opts;
3049 }
3050
3051 if (*flags & MS_RDONLY) {
3052 /*
3053 * First of all, the unconditional stuff we have to do
3054 * to disable replay of the journal when we next remount
3055 */
3056 sb->s_flags |= MS_RDONLY;
3057
3058 /*
3059 * OK, test if we are remounting a valid rw partition
3060 * readonly, and if so set the rdonly flag and then
3061 * mark the partition as valid again.
3062 */
Mingming Cao617ba132006-10-11 01:20:53 -07003063 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3064 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 es->s_state = cpu_to_le16(sbi->s_mount_state);
3066
Jan Kara32c37732007-07-15 23:41:09 -07003067 /*
3068 * We have to unlock super so that we can wait for
3069 * transactions.
3070 */
3071 unlock_super(sb);
Mingming Cao617ba132006-10-11 01:20:53 -07003072 ext4_mark_recovery_complete(sb, es);
Jan Kara32c37732007-07-15 23:41:09 -07003073 lock_super(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003074 } else {
3075 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07003076 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3077 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
3078 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003079 "remount RDWR because of unsupported "
3080 "optional features (%x).\n",
3081 sb->s_id, le32_to_cpu(ret));
3082 err = -EROFS;
3083 goto restore_opts;
3084 }
Eric Sandeenead65962007-02-10 01:46:08 -08003085
3086 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04003087 * Make sure the group descriptor checksums
3088 * are sane. If they aren't, refuse to
3089 * remount r/w.
3090 */
3091 for (g = 0; g < sbi->s_groups_count; g++) {
3092 struct ext4_group_desc *gdp =
3093 ext4_get_group_desc(sb, g, NULL);
3094
3095 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
3096 printk(KERN_ERR
3097 "EXT4-fs: ext4_remount: "
3098 "Checksum for group %lu failed (%u!=%u)\n",
3099 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3100 le16_to_cpu(gdp->bg_checksum));
3101 err = -EINVAL;
3102 goto restore_opts;
3103 }
3104 }
3105
3106 /*
Eric Sandeenead65962007-02-10 01:46:08 -08003107 * If we have an unprocessed orphan list hanging
3108 * around from a previously readonly bdev mount,
3109 * require a full umount/remount for now.
3110 */
3111 if (es->s_last_orphan) {
3112 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
3113 "remount RDWR because of unprocessed "
3114 "orphan inode list. Please "
3115 "umount/remount instead.\n",
3116 sb->s_id);
3117 err = -EINVAL;
3118 goto restore_opts;
3119 }
3120
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003121 /*
3122 * Mounting a RDONLY partition read-write, so reread
3123 * and store the current valid flag. (It may have
3124 * been changed by e2fsck since we originally mounted
3125 * the partition.)
3126 */
Mingming Cao617ba132006-10-11 01:20:53 -07003127 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003128 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07003129 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 goto restore_opts;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003131 if (!ext4_setup_super(sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003132 sb->s_flags &= ~MS_RDONLY;
3133 }
3134 }
3135#ifdef CONFIG_QUOTA
3136 /* Release old quota file names */
3137 for (i = 0; i < MAXQUOTAS; i++)
3138 if (old_opts.s_qf_names[i] &&
3139 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3140 kfree(old_opts.s_qf_names[i]);
3141#endif
3142 return 0;
3143restore_opts:
3144 sb->s_flags = old_sb_flags;
3145 sbi->s_mount_opt = old_opts.s_mount_opt;
3146 sbi->s_resuid = old_opts.s_resuid;
3147 sbi->s_resgid = old_opts.s_resgid;
3148 sbi->s_commit_interval = old_opts.s_commit_interval;
3149#ifdef CONFIG_QUOTA
3150 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3151 for (i = 0; i < MAXQUOTAS; i++) {
3152 if (sbi->s_qf_names[i] &&
3153 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3154 kfree(sbi->s_qf_names[i]);
3155 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3156 }
3157#endif
3158 return err;
3159}
3160
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003161static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003162{
3163 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07003164 struct ext4_sb_info *sbi = EXT4_SB(sb);
3165 struct ext4_super_block *es = sbi->s_es;
Pekka Enberg960cc392006-12-06 20:35:29 -08003166 u64 fsid;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003167
Badari Pulavarty5e700302007-07-15 23:42:00 -07003168 if (test_opt(sb, MINIX_DF)) {
3169 sbi->s_overhead_last = 0;
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003170 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003171 ext4_group_t ngroups = sbi->s_groups_count, i;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003172 ext4_fsblk_t overhead = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003173 smp_rmb();
3174
3175 /*
Badari Pulavarty5e700302007-07-15 23:42:00 -07003176 * Compute the overhead (FS structures). This is constant
3177 * for a given filesystem unless the number of block groups
3178 * changes so we cache the previous value until it does.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003179 */
3180
3181 /*
3182 * All of the blocks before first_data_block are
3183 * overhead
3184 */
3185 overhead = le32_to_cpu(es->s_first_data_block);
3186
3187 /*
3188 * Add the overhead attributed to the superblock and
3189 * block group descriptors. If the sparse superblocks
3190 * feature is turned on, then not all groups have this.
3191 */
3192 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003193 overhead += ext4_bg_has_super(sb, i) +
3194 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003195 cond_resched();
3196 }
3197
3198 /*
3199 * Every block group has an inode bitmap, a block
3200 * bitmap, and an inode table.
3201 */
Badari Pulavarty5e700302007-07-15 23:42:00 -07003202 overhead += ngroups * (2 + sbi->s_itb_per_group);
3203 sbi->s_overhead_last = overhead;
3204 smp_wmb();
Aneesh Kumar K.V6bc9fef2007-10-16 18:38:25 -04003205 sbi->s_blocks_last = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003206 }
3207
Mingming Cao617ba132006-10-11 01:20:53 -07003208 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003209 buf->f_bsize = sb->s_blocksize;
Badari Pulavarty5e700302007-07-15 23:42:00 -07003210 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
Aneesh Kumar K.V6bc6e632008-10-10 09:39:00 -04003211 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3212 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
Aneesh Kumar K.V308ba3e2007-10-16 18:38:25 -04003213 ext4_free_blocks_count_set(es, buf->f_bfree);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003214 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3215 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003216 buf->f_bavail = 0;
3217 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07003218 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Badari Pulavarty5e700302007-07-15 23:42:00 -07003219 es->s_free_inodes_count = cpu_to_le32(buf->f_ffree);
Mingming Cao617ba132006-10-11 01:20:53 -07003220 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08003221 fsid = le64_to_cpup((void *)es->s_uuid) ^
3222 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3223 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3224 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003225 return 0;
3226}
3227
3228/* Helper function for writing quotas on sync - we need to start transaction before quota file
3229 * is locked for write. Otherwise the are possible deadlocks:
3230 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07003231 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07003232 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003233 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07003234 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003235 *
3236 */
3237
3238#ifdef CONFIG_QUOTA
3239
3240static inline struct inode *dquot_to_inode(struct dquot *dquot)
3241{
3242 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3243}
3244
Mingming Cao617ba132006-10-11 01:20:53 -07003245static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003246{
3247 handle_t *handle;
3248 int ret, err;
3249
3250 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003251 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003252 if (IS_ERR(handle))
3253 return PTR_ERR(handle);
3254 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003255 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003256 if (!ret)
3257 ret = err;
3258 return ret;
3259}
3260
Mingming Cao617ba132006-10-11 01:20:53 -07003261static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003262{
3263 handle_t *handle;
3264 int ret, err;
3265
3266 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07003267 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Jan Kara2887df12008-04-29 22:02:07 -04003268 if (IS_ERR(handle)) {
3269 /*
3270 * We call dquot_drop() anyway to at least release references
3271 * to quota structures so that umount does not hang.
3272 */
3273 dquot_drop(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003274 return PTR_ERR(handle);
Jan Kara2887df12008-04-29 22:02:07 -04003275 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003276 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07003277 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003278 if (!ret)
3279 ret = err;
3280 return ret;
3281}
3282
Mingming Cao617ba132006-10-11 01:20:53 -07003283static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003284{
3285 int ret, err;
3286 handle_t *handle;
3287 struct inode *inode;
3288
3289 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003290 handle = ext4_journal_start(inode,
3291 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003292 if (IS_ERR(handle))
3293 return PTR_ERR(handle);
3294 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003295 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003296 if (!ret)
3297 ret = err;
3298 return ret;
3299}
3300
Mingming Cao617ba132006-10-11 01:20:53 -07003301static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003302{
3303 int ret, err;
3304 handle_t *handle;
3305
Mingming Cao617ba132006-10-11 01:20:53 -07003306 handle = ext4_journal_start(dquot_to_inode(dquot),
3307 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003308 if (IS_ERR(handle))
3309 return PTR_ERR(handle);
3310 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003311 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003312 if (!ret)
3313 ret = err;
3314 return ret;
3315}
3316
Mingming Cao617ba132006-10-11 01:20:53 -07003317static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003318{
3319 int ret, err;
3320 handle_t *handle;
3321
Mingming Cao617ba132006-10-11 01:20:53 -07003322 handle = ext4_journal_start(dquot_to_inode(dquot),
3323 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07003324 if (IS_ERR(handle)) {
3325 /* Release dquot anyway to avoid endless cycle in dqput() */
3326 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003327 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07003328 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003329 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003330 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003331 if (!ret)
3332 ret = err;
3333 return ret;
3334}
3335
Mingming Cao617ba132006-10-11 01:20:53 -07003336static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003337{
Jan Kara2c8be6b2008-05-13 21:27:55 -04003338 /* Are we journaling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07003339 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3340 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003341 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07003342 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003343 } else {
3344 return dquot_mark_dquot_dirty(dquot);
3345 }
3346}
3347
Mingming Cao617ba132006-10-11 01:20:53 -07003348static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003349{
3350 int ret, err;
3351 handle_t *handle;
3352
3353 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07003354 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003355 if (IS_ERR(handle))
3356 return PTR_ERR(handle);
3357 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07003358 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003359 if (!ret)
3360 ret = err;
3361 return ret;
3362}
3363
3364/*
3365 * Turn on quotas during mount time - we need to find
3366 * the quota file and such...
3367 */
Mingming Cao617ba132006-10-11 01:20:53 -07003368static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003369{
Mingming Cao617ba132006-10-11 01:20:53 -07003370 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
3371 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003372}
3373
3374/*
3375 * Standard function to be called on quota_on
3376 */
Mingming Cao617ba132006-10-11 01:20:53 -07003377static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Jan Kara6f28e082008-04-28 02:14:34 -07003378 char *path, int remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003379{
3380 int err;
3381 struct nameidata nd;
3382
3383 if (!test_opt(sb, QUOTA))
3384 return -EINVAL;
Jan Kara06235432008-05-13 19:11:51 -04003385 /* When remounting, no checks are needed and in fact, path is NULL */
3386 if (remount)
Jan Kara6f28e082008-04-28 02:14:34 -07003387 return vfs_quota_on(sb, type, format_id, path, remount);
Jan Kara06235432008-05-13 19:11:51 -04003388
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003389 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
3390 if (err)
3391 return err;
Jan Kara06235432008-05-13 19:11:51 -04003392
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003393 /* Quotafile not on the same filesystem? */
Jan Blunck4ac91372008-02-14 19:34:32 -08003394 if (nd.path.mnt->mnt_sb != sb) {
Jan Blunck1d957f92008-02-14 19:34:35 -08003395 path_put(&nd.path);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003396 return -EXDEV;
3397 }
Jan Kara06235432008-05-13 19:11:51 -04003398 /* Journaling quota? */
3399 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003400 /* Quotafile not in fs root? */
Jan Kara06235432008-05-13 19:11:51 -04003401 if (nd.path.dentry->d_parent->d_inode != sb->s_root->d_inode)
3402 printk(KERN_WARNING
3403 "EXT4-fs: Quota file not on filesystem root. "
3404 "Journaled quota will not work.\n");
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003405 }
Jan Kara06235432008-05-13 19:11:51 -04003406
3407 /*
3408 * When we journal data on quota file, we have to flush journal to see
3409 * all updates to the file when we bypass pagecache...
3410 */
3411 if (ext4_should_journal_data(nd.path.dentry->d_inode)) {
3412 /*
3413 * We don't need to lock updates but journal_flush() could
3414 * otherwise be livelocked...
3415 */
3416 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
3417 jbd2_journal_flush(EXT4_SB(sb)->s_journal);
3418 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
3419 }
3420
Al Viro77e69da2008-08-01 04:29:18 -04003421 err = vfs_quota_on_path(sb, type, format_id, &nd.path);
Jan Blunck1d957f92008-02-14 19:34:35 -08003422 path_put(&nd.path);
Al Viro77e69da2008-08-01 04:29:18 -04003423 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003424}
3425
3426/* Read data from quotafile - avoid pagecache and such because we cannot afford
3427 * acquiring the locks... As quota files are never truncated and quota code
3428 * itself serializes the operations (and noone else should touch the files)
3429 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07003430static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003431 size_t len, loff_t off)
3432{
3433 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003434 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003435 int err = 0;
3436 int offset = off & (sb->s_blocksize - 1);
3437 int tocopy;
3438 size_t toread;
3439 struct buffer_head *bh;
3440 loff_t i_size = i_size_read(inode);
3441
3442 if (off > i_size)
3443 return 0;
3444 if (off+len > i_size)
3445 len = i_size-off;
3446 toread = len;
3447 while (toread > 0) {
3448 tocopy = sb->s_blocksize - offset < toread ?
3449 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07003450 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003451 if (err)
3452 return err;
3453 if (!bh) /* A hole? */
3454 memset(data, 0, tocopy);
3455 else
3456 memcpy(data, bh->b_data+offset, tocopy);
3457 brelse(bh);
3458 offset = 0;
3459 toread -= tocopy;
3460 data += tocopy;
3461 blk++;
3462 }
3463 return len;
3464}
3465
3466/* Write to quotafile (we know the transaction is already started and has
3467 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07003468static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003469 const char *data, size_t len, loff_t off)
3470{
3471 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05003472 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003473 int err = 0;
3474 int offset = off & (sb->s_blocksize - 1);
3475 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07003476 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003477 size_t towrite = len;
3478 struct buffer_head *bh;
3479 handle_t *handle = journal_current_handle();
3480
Jan Kara9c3013e2007-09-11 15:23:29 -07003481 if (!handle) {
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003482 printk(KERN_WARNING "EXT4-fs: Quota write (off=%llu, len=%llu)"
Jan Kara9c3013e2007-09-11 15:23:29 -07003483 " cancelled because transaction is not started.\n",
3484 (unsigned long long)off, (unsigned long long)len);
3485 return -EIO;
3486 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003487 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
3488 while (towrite > 0) {
3489 tocopy = sb->s_blocksize - offset < towrite ?
3490 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07003491 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003492 if (!bh)
3493 goto out;
3494 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07003495 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003496 if (err) {
3497 brelse(bh);
3498 goto out;
3499 }
3500 }
3501 lock_buffer(bh);
3502 memcpy(bh->b_data+offset, data, tocopy);
3503 flush_dcache_page(bh->b_page);
3504 unlock_buffer(bh);
3505 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07003506 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003507 else {
3508 /* Always do at least ordered writes for quotas */
Jan Kara678aaf42008-07-11 19:27:31 -04003509 err = ext4_jbd2_file_inode(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003510 mark_buffer_dirty(bh);
3511 }
3512 brelse(bh);
3513 if (err)
3514 goto out;
3515 offset = 0;
3516 towrite -= tocopy;
3517 data += tocopy;
3518 blk++;
3519 }
3520out:
Jan Kara4d04e4f2008-07-04 09:59:34 -07003521 if (len == towrite) {
3522 mutex_unlock(&inode->i_mutex);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003523 return err;
Jan Kara4d04e4f2008-07-04 09:59:34 -07003524 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003525 if (inode->i_size < off+len-towrite) {
3526 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07003527 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003528 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003529 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07003530 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003531 mutex_unlock(&inode->i_mutex);
3532 return len - towrite;
3533}
3534
3535#endif
3536
Mingming Cao617ba132006-10-11 01:20:53 -07003537static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003538 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
3539{
Mingming Cao617ba132006-10-11 01:20:53 -07003540 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003541}
3542
Theodore Ts'o5e8814f2008-09-23 18:07:35 -04003543#ifdef CONFIG_PROC_FS
3544static int ext4_ui_proc_show(struct seq_file *m, void *v)
3545{
3546 unsigned int *p = m->private;
3547
3548 seq_printf(m, "%u\n", *p);
3549 return 0;
3550}
3551
3552static int ext4_ui_proc_open(struct inode *inode, struct file *file)
3553{
3554 return single_open(file, ext4_ui_proc_show, PDE(inode)->data);
3555}
3556
3557static ssize_t ext4_ui_proc_write(struct file *file, const char __user *buf,
3558 size_t cnt, loff_t *ppos)
3559{
3560 unsigned int *p = PDE(file->f_path.dentry->d_inode)->data;
3561 char str[32];
3562 unsigned long value;
3563
3564 if (cnt >= sizeof(str))
3565 return -EINVAL;
3566 if (copy_from_user(str, buf, cnt))
3567 return -EFAULT;
3568 value = simple_strtol(str, NULL, 0);
3569 if (value < 0)
3570 return -ERANGE;
3571 *p = value;
3572 return cnt;
3573}
3574
3575const struct file_operations ext4_ui_proc_fops = {
3576 .owner = THIS_MODULE,
3577 .open = ext4_ui_proc_open,
3578 .read = seq_read,
3579 .llseek = seq_lseek,
3580 .release = single_release,
3581 .write = ext4_ui_proc_write,
3582};
3583#endif
3584
Mingming Cao617ba132006-10-11 01:20:53 -07003585static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003586 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07003587 .name = "ext4dev",
3588 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003589 .kill_sb = kill_block_super,
3590 .fs_flags = FS_REQUIRES_DEV,
3591};
3592
Mingming Cao617ba132006-10-11 01:20:53 -07003593static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003594{
Alex Tomasc9de5602008-01-29 00:19:52 -05003595 int err;
3596
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003597 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
Alex Tomasc9de5602008-01-29 00:19:52 -05003598 err = init_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003599 if (err)
3600 return err;
Alex Tomasc9de5602008-01-29 00:19:52 -05003601
3602 err = init_ext4_xattr();
3603 if (err)
3604 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003605 err = init_inodecache();
3606 if (err)
3607 goto out1;
Andrew Morton63f57932006-10-11 01:21:24 -07003608 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003609 if (err)
3610 goto out;
3611 return 0;
3612out:
3613 destroy_inodecache();
3614out1:
Mingming Cao617ba132006-10-11 01:20:53 -07003615 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003616out2:
3617 exit_ext4_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003618 return err;
3619}
3620
Mingming Cao617ba132006-10-11 01:20:53 -07003621static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003622{
Mingming Cao617ba132006-10-11 01:20:53 -07003623 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003624 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07003625 exit_ext4_xattr();
Alex Tomasc9de5602008-01-29 00:19:52 -05003626 exit_ext4_mballoc();
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003627 remove_proc_entry("fs/ext4", NULL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003628}
3629
3630MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07003631MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003632MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07003633module_init(init_ext4_fs)
3634module_exit(exit_ext4_fs)