blob: b91dffd7a0312ad2cba35b8fb50b9a1a8d4a3982 [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>
Mingming Cao617ba132006-10-11 01:20:53 -070024#include <linux/ext4_fs.h>
Mingming Caodab291a2006-10-11 01:21:01 -070025#include <linux/ext4_jbd2.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070026#include <linux/slab.h>
27#include <linux/init.h>
28#include <linux/blkdev.h>
29#include <linux/parser.h>
30#include <linux/smp_lock.h>
31#include <linux/buffer_head.h>
32#include <linux/vfs.h>
33#include <linux/random.h>
34#include <linux/mount.h>
35#include <linux/namei.h>
36#include <linux/quotaops.h>
37#include <linux/seq_file.h>
38
39#include <asm/uaccess.h>
40
41#include "xattr.h"
42#include "acl.h"
43#include "namei.h"
44
Mingming Cao617ba132006-10-11 01:20:53 -070045static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070046 unsigned long journal_devnum);
Mingming Cao617ba132006-10-11 01:20:53 -070047static int ext4_create_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070048 unsigned int);
Mingming Cao617ba132006-10-11 01:20:53 -070049static void ext4_commit_super (struct super_block * sb,
50 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070051 int sync);
Mingming Cao617ba132006-10-11 01:20:53 -070052static void ext4_mark_recovery_complete(struct super_block * sb,
53 struct ext4_super_block * es);
54static void ext4_clear_journal_err(struct super_block * sb,
55 struct ext4_super_block * es);
56static int ext4_sync_fs(struct super_block *sb, int wait);
57static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070058 char nbuf[16]);
Mingming Cao617ba132006-10-11 01:20:53 -070059static int ext4_remount (struct super_block * sb, int * flags, char * data);
60static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf);
61static void ext4_unlockfs(struct super_block *sb);
62static void ext4_write_super (struct super_block * sb);
63static void ext4_write_super_lockfs(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070064
Laurent Vivierbd81d8e2006-10-11 01:21:10 -070065
66ext4_fsblk_t ext4_block_bitmap(struct ext4_group_desc *bg)
67{
68 return le32_to_cpu(bg->bg_block_bitmap) |
69 ((ext4_fsblk_t)le16_to_cpu(bg->bg_block_bitmap_hi) << 32);
70}
71
72ext4_fsblk_t ext4_inode_bitmap(struct ext4_group_desc *bg)
73{
74 return le32_to_cpu(bg->bg_inode_bitmap) |
75 ((ext4_fsblk_t)le16_to_cpu(bg->bg_inode_bitmap_hi) << 32);
76}
77
78ext4_fsblk_t ext4_inode_table(struct ext4_group_desc *bg)
79{
80 return le32_to_cpu(bg->bg_inode_table) |
81 ((ext4_fsblk_t)le16_to_cpu(bg->bg_inode_table_hi) << 32);
82}
83
84void ext4_block_bitmap_set(struct ext4_group_desc *bg, ext4_fsblk_t blk)
85{
86 bg->bg_block_bitmap = cpu_to_le32((u32)blk);
87 bg->bg_block_bitmap_hi = cpu_to_le16(blk >> 32);
88}
89
90void ext4_inode_bitmap_set(struct ext4_group_desc *bg, ext4_fsblk_t blk)
91{
92 bg->bg_inode_bitmap = cpu_to_le32((u32)blk);
93 bg->bg_inode_bitmap_hi = cpu_to_le16(blk >> 32);
94}
95
96void ext4_inode_table_set(struct ext4_group_desc *bg, ext4_fsblk_t blk)
97{
98 bg->bg_inode_table = cpu_to_le32((u32)blk);
99 bg->bg_inode_table_hi = cpu_to_le16(blk >> 32);
100}
101
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700102/*
Mingming Caodab291a2006-10-11 01:21:01 -0700103 * Wrappers for jbd2_journal_start/end.
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700104 *
105 * The only special thing we need to do here is to make sure that all
106 * journal_end calls result in the superblock being marked dirty, so
107 * that sync() will call the filesystem's write_super callback if
108 * appropriate.
109 */
Mingming Cao617ba132006-10-11 01:20:53 -0700110handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700111{
112 journal_t *journal;
113
114 if (sb->s_flags & MS_RDONLY)
115 return ERR_PTR(-EROFS);
116
117 /* Special case here: if the journal has aborted behind our
118 * backs (eg. EIO in the commit thread), then we still need to
119 * take the FS itself readonly cleanly. */
Mingming Cao617ba132006-10-11 01:20:53 -0700120 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700121 if (is_journal_aborted(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700122 ext4_abort(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700123 "Detected aborted journal");
124 return ERR_PTR(-EROFS);
125 }
126
Mingming Caodab291a2006-10-11 01:21:01 -0700127 return jbd2_journal_start(journal, nblocks);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700128}
129
130/*
131 * The only special thing we need to do here is to make sure that all
Mingming Caodab291a2006-10-11 01:21:01 -0700132 * jbd2_journal_stop calls result in the superblock being marked dirty, so
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700133 * that sync() will call the filesystem's write_super callback if
134 * appropriate.
135 */
Mingming Cao617ba132006-10-11 01:20:53 -0700136int __ext4_journal_stop(const char *where, handle_t *handle)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700137{
138 struct super_block *sb;
139 int err;
140 int rc;
141
142 sb = handle->h_transaction->t_journal->j_private;
143 err = handle->h_err;
Mingming Caodab291a2006-10-11 01:21:01 -0700144 rc = jbd2_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700145
146 if (!err)
147 err = rc;
148 if (err)
Mingming Cao617ba132006-10-11 01:20:53 -0700149 __ext4_std_error(sb, where, err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700150 return err;
151}
152
Mingming Cao617ba132006-10-11 01:20:53 -0700153void ext4_journal_abort_handle(const char *caller, const char *err_fn,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700154 struct buffer_head *bh, handle_t *handle, int err)
155{
156 char nbuf[16];
Mingming Cao617ba132006-10-11 01:20:53 -0700157 const char *errstr = ext4_decode_error(NULL, err, nbuf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700158
159 if (bh)
160 BUFFER_TRACE(bh, "abort");
161
162 if (!handle->h_err)
163 handle->h_err = err;
164
165 if (is_handle_aborted(handle))
166 return;
167
168 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
169 caller, errstr, err_fn);
170
Mingming Caodab291a2006-10-11 01:21:01 -0700171 jbd2_journal_abort_handle(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700172}
173
174/* Deal with the reporting of failure conditions on a filesystem such as
175 * inconsistencies detected or read IO failures.
176 *
177 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700178 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700179 * write ordering constraints on the superblock which prevent us from
180 * writing it out straight away; and given that the journal is about to
181 * be aborted, we can't rely on the current, or future, transactions to
182 * write out the superblock safely.
183 *
Mingming Caodab291a2006-10-11 01:21:01 -0700184 * We'll just use the jbd2_journal_abort() error code to record an error in
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700185 * the journal instead. On recovery, the journal will compain about
186 * that error until we've noted it down and cleared it.
187 */
188
Mingming Cao617ba132006-10-11 01:20:53 -0700189static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700190{
Mingming Cao617ba132006-10-11 01:20:53 -0700191 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700192
Mingming Cao617ba132006-10-11 01:20:53 -0700193 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
194 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700195
196 if (sb->s_flags & MS_RDONLY)
197 return;
198
199 if (!test_opt (sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700200 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700201
Mingming Cao617ba132006-10-11 01:20:53 -0700202 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700203 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700204 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700205 }
206 if (test_opt (sb, ERRORS_RO)) {
207 printk (KERN_CRIT "Remounting filesystem read-only\n");
208 sb->s_flags |= MS_RDONLY;
209 }
Mingming Cao617ba132006-10-11 01:20:53 -0700210 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700211 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700212 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700213 sb->s_id);
214}
215
Mingming Cao617ba132006-10-11 01:20:53 -0700216void ext4_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700217 const char * fmt, ...)
218{
219 va_list args;
220
221 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700222 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700223 vprintk(fmt, args);
224 printk("\n");
225 va_end(args);
226
Mingming Cao617ba132006-10-11 01:20:53 -0700227 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700228}
229
Mingming Cao617ba132006-10-11 01:20:53 -0700230static const char *ext4_decode_error(struct super_block * sb, int errno,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700231 char nbuf[16])
232{
233 char *errstr = NULL;
234
235 switch (errno) {
236 case -EIO:
237 errstr = "IO failure";
238 break;
239 case -ENOMEM:
240 errstr = "Out of memory";
241 break;
242 case -EROFS:
Mingming Caodab291a2006-10-11 01:21:01 -0700243 if (!sb || EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700244 errstr = "Journal has aborted";
245 else
246 errstr = "Readonly filesystem";
247 break;
248 default:
249 /* If the caller passed in an extra buffer for unknown
250 * errors, textualise them now. Else we just return
251 * NULL. */
252 if (nbuf) {
253 /* Check for truncated error codes... */
254 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
255 errstr = nbuf;
256 }
257 break;
258 }
259
260 return errstr;
261}
262
Mingming Cao617ba132006-10-11 01:20:53 -0700263/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700264 * automatically and invokes the appropriate error response. */
265
Mingming Cao617ba132006-10-11 01:20:53 -0700266void __ext4_std_error (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700267 int errno)
268{
269 char nbuf[16];
270 const char *errstr;
271
272 /* Special case: if the error is EROFS, and we're not already
273 * inside a transaction, then there's really no point in logging
274 * an error. */
275 if (errno == -EROFS && journal_current_handle() == NULL &&
276 (sb->s_flags & MS_RDONLY))
277 return;
278
Mingming Cao617ba132006-10-11 01:20:53 -0700279 errstr = ext4_decode_error(sb, errno, nbuf);
280 printk (KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700281 sb->s_id, function, errstr);
282
Mingming Cao617ba132006-10-11 01:20:53 -0700283 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700284}
285
286/*
Mingming Cao617ba132006-10-11 01:20:53 -0700287 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700288 * abort function may be used to deal with unrecoverable failures such
289 * as journal IO errors or ENOMEM at a critical moment in log management.
290 *
291 * We unconditionally force the filesystem into an ABORT|READONLY state,
292 * unless the error response on the fs has been set to panic in which
293 * case we take the easy way out and panic immediately.
294 */
295
Mingming Cao617ba132006-10-11 01:20:53 -0700296void ext4_abort (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700297 const char * fmt, ...)
298{
299 va_list args;
300
Mingming Cao617ba132006-10-11 01:20:53 -0700301 printk (KERN_CRIT "ext4_abort called.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700302
303 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700304 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ",sb->s_id, function);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700305 vprintk(fmt, args);
306 printk("\n");
307 va_end(args);
308
309 if (test_opt(sb, ERRORS_PANIC))
Mingming Cao617ba132006-10-11 01:20:53 -0700310 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700311
312 if (sb->s_flags & MS_RDONLY)
313 return;
314
315 printk(KERN_CRIT "Remounting filesystem read-only\n");
Mingming Cao617ba132006-10-11 01:20:53 -0700316 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700317 sb->s_flags |= MS_RDONLY;
Mingming Cao617ba132006-10-11 01:20:53 -0700318 EXT4_SB(sb)->s_mount_opt |= EXT4_MOUNT_ABORT;
Mingming Caodab291a2006-10-11 01:21:01 -0700319 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700320}
321
Mingming Cao617ba132006-10-11 01:20:53 -0700322void ext4_warning (struct super_block * sb, const char * function,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700323 const char * fmt, ...)
324{
325 va_list args;
326
327 va_start(args, fmt);
Mingming Cao617ba132006-10-11 01:20:53 -0700328 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700329 sb->s_id, function);
330 vprintk(fmt, args);
331 printk("\n");
332 va_end(args);
333}
334
Mingming Cao617ba132006-10-11 01:20:53 -0700335void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700336{
Mingming Cao617ba132006-10-11 01:20:53 -0700337 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700338
Mingming Cao617ba132006-10-11 01:20:53 -0700339 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700340 return;
341
Mingming Cao617ba132006-10-11 01:20:53 -0700342 ext4_warning(sb, __FUNCTION__,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700343 "updating to rev %d because of new feature flag, "
344 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700345 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700346
Mingming Cao617ba132006-10-11 01:20:53 -0700347 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
348 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
349 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700350 /* leave es->s_feature_*compat flags alone */
351 /* es->s_uuid will be set by e2fsck if empty */
352
353 /*
354 * The rest of the superblock fields should be zero, and if not it
355 * means they are likely already in use, so leave them alone. We
356 * can leave it up to e2fsck to clean up any inconsistencies there.
357 */
358}
359
360/*
361 * Open the external journal device
362 */
Mingming Cao617ba132006-10-11 01:20:53 -0700363static struct block_device *ext4_blkdev_get(dev_t dev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700364{
365 struct block_device *bdev;
366 char b[BDEVNAME_SIZE];
367
368 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
369 if (IS_ERR(bdev))
370 goto fail;
371 return bdev;
372
373fail:
Mingming Cao617ba132006-10-11 01:20:53 -0700374 printk(KERN_ERR "EXT4: failed to open journal device %s: %ld\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700375 __bdevname(dev, b), PTR_ERR(bdev));
376 return NULL;
377}
378
379/*
380 * Release the journal device
381 */
Mingming Cao617ba132006-10-11 01:20:53 -0700382static int ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700383{
384 bd_release(bdev);
385 return blkdev_put(bdev);
386}
387
Mingming Cao617ba132006-10-11 01:20:53 -0700388static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700389{
390 struct block_device *bdev;
391 int ret = -ENODEV;
392
393 bdev = sbi->journal_bdev;
394 if (bdev) {
Mingming Cao617ba132006-10-11 01:20:53 -0700395 ret = ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700396 sbi->journal_bdev = NULL;
397 }
398 return ret;
399}
400
401static inline struct inode *orphan_list_entry(struct list_head *l)
402{
Mingming Cao617ba132006-10-11 01:20:53 -0700403 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700404}
405
Mingming Cao617ba132006-10-11 01:20:53 -0700406static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700407{
408 struct list_head *l;
409
410 printk(KERN_ERR "sb orphan head is %d\n",
411 le32_to_cpu(sbi->s_es->s_last_orphan));
412
413 printk(KERN_ERR "sb_info orphan list:\n");
414 list_for_each(l, &sbi->s_orphan) {
415 struct inode *inode = orphan_list_entry(l);
416 printk(KERN_ERR " "
417 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
418 inode->i_sb->s_id, inode->i_ino, inode,
419 inode->i_mode, inode->i_nlink,
420 NEXT_ORPHAN(inode));
421 }
422}
423
Mingming Cao617ba132006-10-11 01:20:53 -0700424static void ext4_put_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700425{
Mingming Cao617ba132006-10-11 01:20:53 -0700426 struct ext4_sb_info *sbi = EXT4_SB(sb);
427 struct ext4_super_block *es = sbi->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700428 int i;
429
Alex Tomasa86c6182006-10-11 01:21:03 -0700430 ext4_ext_release(sb);
Mingming Cao617ba132006-10-11 01:20:53 -0700431 ext4_xattr_put_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -0700432 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700433 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700434 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700435 es->s_state = cpu_to_le16(sbi->s_mount_state);
436 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
437 mark_buffer_dirty(sbi->s_sbh);
Mingming Cao617ba132006-10-11 01:20:53 -0700438 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700439 }
440
441 for (i = 0; i < sbi->s_gdb_count; i++)
442 brelse(sbi->s_group_desc[i]);
443 kfree(sbi->s_group_desc);
444 percpu_counter_destroy(&sbi->s_freeblocks_counter);
445 percpu_counter_destroy(&sbi->s_freeinodes_counter);
446 percpu_counter_destroy(&sbi->s_dirs_counter);
447 brelse(sbi->s_sbh);
448#ifdef CONFIG_QUOTA
449 for (i = 0; i < MAXQUOTAS; i++)
450 kfree(sbi->s_qf_names[i]);
451#endif
452
453 /* Debugging code just in case the in-memory inode orphan list
454 * isn't empty. The on-disk one can be non-empty if we've
455 * detected an error and taken the fs readonly, but the
456 * in-memory list had better be clean by this point. */
457 if (!list_empty(&sbi->s_orphan))
458 dump_orphan_list(sb, sbi);
459 J_ASSERT(list_empty(&sbi->s_orphan));
460
461 invalidate_bdev(sb->s_bdev, 0);
462 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
463 /*
464 * Invalidate the journal device's buffers. We don't want them
465 * floating about in memory - the physical journal device may
466 * hotswapped, and it breaks the `ro-after' testing code.
467 */
468 sync_blockdev(sbi->journal_bdev);
469 invalidate_bdev(sbi->journal_bdev, 0);
Mingming Cao617ba132006-10-11 01:20:53 -0700470 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700471 }
472 sb->s_fs_info = NULL;
473 kfree(sbi);
474 return;
475}
476
Mingming Cao617ba132006-10-11 01:20:53 -0700477static kmem_cache_t *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700478
479/*
480 * Called inside transaction, so use GFP_NOFS
481 */
Mingming Cao617ba132006-10-11 01:20:53 -0700482static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700483{
Mingming Cao617ba132006-10-11 01:20:53 -0700484 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700485
Mingming Cao617ba132006-10-11 01:20:53 -0700486 ei = kmem_cache_alloc(ext4_inode_cachep, SLAB_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700487 if (!ei)
488 return NULL;
Mingming Cao617ba132006-10-11 01:20:53 -0700489#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
490 ei->i_acl = EXT4_ACL_NOT_CACHED;
491 ei->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700492#endif
493 ei->i_block_alloc_info = NULL;
494 ei->vfs_inode.i_version = 1;
Alex Tomasa86c6182006-10-11 01:21:03 -0700495 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700496 return &ei->vfs_inode;
497}
498
Mingming Cao617ba132006-10-11 01:20:53 -0700499static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700500{
Mingming Cao617ba132006-10-11 01:20:53 -0700501 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700502}
503
504static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
505{
Mingming Cao617ba132006-10-11 01:20:53 -0700506 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700507
508 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
509 SLAB_CTOR_CONSTRUCTOR) {
510 INIT_LIST_HEAD(&ei->i_orphan);
Mingming Cao617ba132006-10-11 01:20:53 -0700511#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700512 init_rwsem(&ei->xattr_sem);
513#endif
514 mutex_init(&ei->truncate_mutex);
515 inode_init_once(&ei->vfs_inode);
516 }
517}
518
519static int init_inodecache(void)
520{
Mingming Cao617ba132006-10-11 01:20:53 -0700521 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
522 sizeof(struct ext4_inode_info),
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700523 0, (SLAB_RECLAIM_ACCOUNT|
524 SLAB_MEM_SPREAD),
525 init_once, NULL);
Mingming Cao617ba132006-10-11 01:20:53 -0700526 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700527 return -ENOMEM;
528 return 0;
529}
530
531static void destroy_inodecache(void)
532{
Mingming Cao617ba132006-10-11 01:20:53 -0700533 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700534}
535
Mingming Cao617ba132006-10-11 01:20:53 -0700536static void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700537{
Mingming Cao617ba132006-10-11 01:20:53 -0700538 struct ext4_block_alloc_info *rsv = EXT4_I(inode)->i_block_alloc_info;
539#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
540 if (EXT4_I(inode)->i_acl &&
541 EXT4_I(inode)->i_acl != EXT4_ACL_NOT_CACHED) {
542 posix_acl_release(EXT4_I(inode)->i_acl);
543 EXT4_I(inode)->i_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700544 }
Mingming Cao617ba132006-10-11 01:20:53 -0700545 if (EXT4_I(inode)->i_default_acl &&
546 EXT4_I(inode)->i_default_acl != EXT4_ACL_NOT_CACHED) {
547 posix_acl_release(EXT4_I(inode)->i_default_acl);
548 EXT4_I(inode)->i_default_acl = EXT4_ACL_NOT_CACHED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549 }
550#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700551 ext4_discard_reservation(inode);
552 EXT4_I(inode)->i_block_alloc_info = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700553 if (unlikely(rsv))
554 kfree(rsv);
555}
556
Mingming Cao617ba132006-10-11 01:20:53 -0700557static inline void ext4_show_quota_options(struct seq_file *seq, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700558{
559#if defined(CONFIG_QUOTA)
Mingming Cao617ba132006-10-11 01:20:53 -0700560 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700561
562 if (sbi->s_jquota_fmt)
563 seq_printf(seq, ",jqfmt=%s",
564 (sbi->s_jquota_fmt == QFMT_VFS_OLD) ? "vfsold": "vfsv0");
565
566 if (sbi->s_qf_names[USRQUOTA])
567 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
568
569 if (sbi->s_qf_names[GRPQUOTA])
570 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
571
Mingming Cao617ba132006-10-11 01:20:53 -0700572 if (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700573 seq_puts(seq, ",usrquota");
574
Mingming Cao617ba132006-10-11 01:20:53 -0700575 if (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700576 seq_puts(seq, ",grpquota");
577#endif
578}
579
Mingming Cao617ba132006-10-11 01:20:53 -0700580static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700581{
582 struct super_block *sb = vfs->mnt_sb;
583
Mingming Cao617ba132006-10-11 01:20:53 -0700584 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700585 seq_puts(seq, ",data=journal");
Mingming Cao617ba132006-10-11 01:20:53 -0700586 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700587 seq_puts(seq, ",data=ordered");
Mingming Cao617ba132006-10-11 01:20:53 -0700588 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700589 seq_puts(seq, ",data=writeback");
590
Mingming Cao617ba132006-10-11 01:20:53 -0700591 ext4_show_quota_options(seq, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700592
593 return 0;
594}
595
596
Mingming Cao617ba132006-10-11 01:20:53 -0700597static struct dentry *ext4_get_dentry(struct super_block *sb, void *vobjp)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700598{
599 __u32 *objp = vobjp;
600 unsigned long ino = objp[0];
601 __u32 generation = objp[1];
602 struct inode *inode;
603 struct dentry *result;
604
Mingming Cao617ba132006-10-11 01:20:53 -0700605 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700606 return ERR_PTR(-ESTALE);
Mingming Cao617ba132006-10-11 01:20:53 -0700607 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700608 return ERR_PTR(-ESTALE);
609
610 /* iget isn't really right if the inode is currently unallocated!!
611 *
Mingming Cao617ba132006-10-11 01:20:53 -0700612 * ext4_read_inode will return a bad_inode if the inode had been
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700613 * deleted, so we should be safe.
614 *
615 * Currently we don't know the generation for parent directory, so
616 * a generation of 0 means "accept any"
617 */
618 inode = iget(sb, ino);
619 if (inode == NULL)
620 return ERR_PTR(-ENOMEM);
621 if (is_bad_inode(inode) ||
622 (generation && inode->i_generation != generation)) {
623 iput(inode);
624 return ERR_PTR(-ESTALE);
625 }
626 /* now to find a dentry.
627 * If possible, get a well-connected one
628 */
629 result = d_alloc_anon(inode);
630 if (!result) {
631 iput(inode);
632 return ERR_PTR(-ENOMEM);
633 }
634 return result;
635}
636
637#ifdef CONFIG_QUOTA
638#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
639#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
640
Mingming Cao617ba132006-10-11 01:20:53 -0700641static int ext4_dquot_initialize(struct inode *inode, int type);
642static int ext4_dquot_drop(struct inode *inode);
643static int ext4_write_dquot(struct dquot *dquot);
644static int ext4_acquire_dquot(struct dquot *dquot);
645static int ext4_release_dquot(struct dquot *dquot);
646static int ext4_mark_dquot_dirty(struct dquot *dquot);
647static int ext4_write_info(struct super_block *sb, int type);
648static int ext4_quota_on(struct super_block *sb, int type, int format_id, char *path);
649static int ext4_quota_on_mount(struct super_block *sb, int type);
650static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700651 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -0700652static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 const char *data, size_t len, loff_t off);
654
Mingming Cao617ba132006-10-11 01:20:53 -0700655static struct dquot_operations ext4_quota_operations = {
656 .initialize = ext4_dquot_initialize,
657 .drop = ext4_dquot_drop,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700658 .alloc_space = dquot_alloc_space,
659 .alloc_inode = dquot_alloc_inode,
660 .free_space = dquot_free_space,
661 .free_inode = dquot_free_inode,
662 .transfer = dquot_transfer,
Mingming Cao617ba132006-10-11 01:20:53 -0700663 .write_dquot = ext4_write_dquot,
664 .acquire_dquot = ext4_acquire_dquot,
665 .release_dquot = ext4_release_dquot,
666 .mark_dirty = ext4_mark_dquot_dirty,
667 .write_info = ext4_write_info
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668};
669
Mingming Cao617ba132006-10-11 01:20:53 -0700670static struct quotactl_ops ext4_qctl_operations = {
671 .quota_on = ext4_quota_on,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700672 .quota_off = vfs_quota_off,
673 .quota_sync = vfs_quota_sync,
674 .get_info = vfs_get_dqinfo,
675 .set_info = vfs_set_dqinfo,
676 .get_dqblk = vfs_get_dqblk,
677 .set_dqblk = vfs_set_dqblk
678};
679#endif
680
Mingming Cao617ba132006-10-11 01:20:53 -0700681static struct super_operations ext4_sops = {
682 .alloc_inode = ext4_alloc_inode,
683 .destroy_inode = ext4_destroy_inode,
684 .read_inode = ext4_read_inode,
685 .write_inode = ext4_write_inode,
686 .dirty_inode = ext4_dirty_inode,
687 .delete_inode = ext4_delete_inode,
688 .put_super = ext4_put_super,
689 .write_super = ext4_write_super,
690 .sync_fs = ext4_sync_fs,
691 .write_super_lockfs = ext4_write_super_lockfs,
692 .unlockfs = ext4_unlockfs,
693 .statfs = ext4_statfs,
694 .remount_fs = ext4_remount,
695 .clear_inode = ext4_clear_inode,
696 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -0700698 .quota_read = ext4_quota_read,
699 .quota_write = ext4_quota_write,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700700#endif
701};
702
Mingming Cao617ba132006-10-11 01:20:53 -0700703static struct export_operations ext4_export_ops = {
704 .get_parent = ext4_get_parent,
705 .get_dentry = ext4_get_dentry,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706};
707
708enum {
709 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
710 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
711 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
712 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
713 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
714 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
715 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
716 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
717 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_quota, Opt_noquota,
718 Opt_ignore, Opt_barrier, Opt_err, Opt_resize, Opt_usrquota,
Alex Tomasa86c6182006-10-11 01:21:03 -0700719 Opt_grpquota, Opt_extents,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700720};
721
722static match_table_t tokens = {
723 {Opt_bsd_df, "bsddf"},
724 {Opt_minix_df, "minixdf"},
725 {Opt_grpid, "grpid"},
726 {Opt_grpid, "bsdgroups"},
727 {Opt_nogrpid, "nogrpid"},
728 {Opt_nogrpid, "sysvgroups"},
729 {Opt_resgid, "resgid=%u"},
730 {Opt_resuid, "resuid=%u"},
731 {Opt_sb, "sb=%u"},
732 {Opt_err_cont, "errors=continue"},
733 {Opt_err_panic, "errors=panic"},
734 {Opt_err_ro, "errors=remount-ro"},
735 {Opt_nouid32, "nouid32"},
736 {Opt_nocheck, "nocheck"},
737 {Opt_nocheck, "check=none"},
738 {Opt_debug, "debug"},
739 {Opt_oldalloc, "oldalloc"},
740 {Opt_orlov, "orlov"},
741 {Opt_user_xattr, "user_xattr"},
742 {Opt_nouser_xattr, "nouser_xattr"},
743 {Opt_acl, "acl"},
744 {Opt_noacl, "noacl"},
745 {Opt_reservation, "reservation"},
746 {Opt_noreservation, "noreservation"},
747 {Opt_noload, "noload"},
748 {Opt_nobh, "nobh"},
749 {Opt_bh, "bh"},
750 {Opt_commit, "commit=%u"},
751 {Opt_journal_update, "journal=update"},
752 {Opt_journal_inum, "journal=%u"},
753 {Opt_journal_dev, "journal_dev=%u"},
754 {Opt_abort, "abort"},
755 {Opt_data_journal, "data=journal"},
756 {Opt_data_ordered, "data=ordered"},
757 {Opt_data_writeback, "data=writeback"},
758 {Opt_offusrjquota, "usrjquota="},
759 {Opt_usrjquota, "usrjquota=%s"},
760 {Opt_offgrpjquota, "grpjquota="},
761 {Opt_grpjquota, "grpjquota=%s"},
762 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
763 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
764 {Opt_grpquota, "grpquota"},
765 {Opt_noquota, "noquota"},
766 {Opt_quota, "quota"},
767 {Opt_usrquota, "usrquota"},
768 {Opt_barrier, "barrier=%u"},
Alex Tomasa86c6182006-10-11 01:21:03 -0700769 {Opt_extents, "extents"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700770 {Opt_err, NULL},
771 {Opt_resize, "resize"},
772};
773
Mingming Cao617ba132006-10-11 01:20:53 -0700774static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700775{
Mingming Cao617ba132006-10-11 01:20:53 -0700776 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700777 char *options = (char *) *data;
778
779 if (!options || strncmp(options, "sb=", 3) != 0)
780 return 1; /* Default location */
781 options += 3;
Mingming Cao617ba132006-10-11 01:20:53 -0700782 /*todo: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700783 sb_block = simple_strtoul(options, &options, 0);
784 if (*options && *options != ',') {
Mingming Cao617ba132006-10-11 01:20:53 -0700785 printk("EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 (char *) *data);
787 return 1;
788 }
789 if (*options == ',')
790 options++;
791 *data = (void *) options;
792 return sb_block;
793}
794
795static int parse_options (char *options, struct super_block *sb,
796 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao617ba132006-10-11 01:20:53 -0700797 ext4_fsblk_t *n_blocks_count, int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700798{
Mingming Cao617ba132006-10-11 01:20:53 -0700799 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800 char * p;
801 substring_t args[MAX_OPT_ARGS];
802 int data_opt = 0;
803 int option;
804#ifdef CONFIG_QUOTA
805 int qtype;
806 char *qname;
807#endif
808
809 if (!options)
810 return 1;
811
812 while ((p = strsep (&options, ",")) != NULL) {
813 int token;
814 if (!*p)
815 continue;
816
817 token = match_token(p, tokens, args);
818 switch (token) {
819 case Opt_bsd_df:
820 clear_opt (sbi->s_mount_opt, MINIX_DF);
821 break;
822 case Opt_minix_df:
823 set_opt (sbi->s_mount_opt, MINIX_DF);
824 break;
825 case Opt_grpid:
826 set_opt (sbi->s_mount_opt, GRPID);
827 break;
828 case Opt_nogrpid:
829 clear_opt (sbi->s_mount_opt, GRPID);
830 break;
831 case Opt_resuid:
832 if (match_int(&args[0], &option))
833 return 0;
834 sbi->s_resuid = option;
835 break;
836 case Opt_resgid:
837 if (match_int(&args[0], &option))
838 return 0;
839 sbi->s_resgid = option;
840 break;
841 case Opt_sb:
842 /* handled by get_sb_block() instead of here */
843 /* *sb_block = match_int(&args[0]); */
844 break;
845 case Opt_err_panic:
846 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
847 clear_opt (sbi->s_mount_opt, ERRORS_RO);
848 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
849 break;
850 case Opt_err_ro:
851 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
852 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
853 set_opt (sbi->s_mount_opt, ERRORS_RO);
854 break;
855 case Opt_err_cont:
856 clear_opt (sbi->s_mount_opt, ERRORS_RO);
857 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
858 set_opt (sbi->s_mount_opt, ERRORS_CONT);
859 break;
860 case Opt_nouid32:
861 set_opt (sbi->s_mount_opt, NO_UID32);
862 break;
863 case Opt_nocheck:
864 clear_opt (sbi->s_mount_opt, CHECK);
865 break;
866 case Opt_debug:
867 set_opt (sbi->s_mount_opt, DEBUG);
868 break;
869 case Opt_oldalloc:
870 set_opt (sbi->s_mount_opt, OLDALLOC);
871 break;
872 case Opt_orlov:
873 clear_opt (sbi->s_mount_opt, OLDALLOC);
874 break;
Mingming Cao617ba132006-10-11 01:20:53 -0700875#ifdef CONFIG_EXT4DEV_FS_XATTR
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700876 case Opt_user_xattr:
877 set_opt (sbi->s_mount_opt, XATTR_USER);
878 break;
879 case Opt_nouser_xattr:
880 clear_opt (sbi->s_mount_opt, XATTR_USER);
881 break;
882#else
883 case Opt_user_xattr:
884 case Opt_nouser_xattr:
Mingming Cao617ba132006-10-11 01:20:53 -0700885 printk("EXT4 (no)user_xattr options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 break;
887#endif
Mingming Cao617ba132006-10-11 01:20:53 -0700888#ifdef CONFIG_EXT4DEV_FS_POSIX_ACL
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700889 case Opt_acl:
890 set_opt(sbi->s_mount_opt, POSIX_ACL);
891 break;
892 case Opt_noacl:
893 clear_opt(sbi->s_mount_opt, POSIX_ACL);
894 break;
895#else
896 case Opt_acl:
897 case Opt_noacl:
Mingming Cao617ba132006-10-11 01:20:53 -0700898 printk("EXT4 (no)acl options not supported\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700899 break;
900#endif
901 case Opt_reservation:
902 set_opt(sbi->s_mount_opt, RESERVATION);
903 break;
904 case Opt_noreservation:
905 clear_opt(sbi->s_mount_opt, RESERVATION);
906 break;
907 case Opt_journal_update:
908 /* @@@ FIXME */
909 /* Eventually we will want to be able to create
910 a journal file here. For now, only allow the
911 user to specify an existing inode to be the
912 journal file. */
913 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700914 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700915 "journal on remount\n");
916 return 0;
917 }
918 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
919 break;
920 case Opt_journal_inum:
921 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700922 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700923 "journal on remount\n");
924 return 0;
925 }
926 if (match_int(&args[0], &option))
927 return 0;
928 *inum = option;
929 break;
930 case Opt_journal_dev:
931 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700932 printk(KERN_ERR "EXT4-fs: cannot specify "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700933 "journal on remount\n");
934 return 0;
935 }
936 if (match_int(&args[0], &option))
937 return 0;
938 *journal_devnum = option;
939 break;
940 case Opt_noload:
941 set_opt (sbi->s_mount_opt, NOLOAD);
942 break;
943 case Opt_commit:
944 if (match_int(&args[0], &option))
945 return 0;
946 if (option < 0)
947 return 0;
948 if (option == 0)
949 option = JBD_DEFAULT_MAX_COMMIT_AGE;
950 sbi->s_commit_interval = HZ * option;
951 break;
952 case Opt_data_journal:
Mingming Cao617ba132006-10-11 01:20:53 -0700953 data_opt = EXT4_MOUNT_JOURNAL_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700954 goto datacheck;
955 case Opt_data_ordered:
Mingming Cao617ba132006-10-11 01:20:53 -0700956 data_opt = EXT4_MOUNT_ORDERED_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700957 goto datacheck;
958 case Opt_data_writeback:
Mingming Cao617ba132006-10-11 01:20:53 -0700959 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700960 datacheck:
961 if (is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -0700962 if ((sbi->s_mount_opt & EXT4_MOUNT_DATA_FLAGS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700963 != data_opt) {
964 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700965 "EXT4-fs: cannot change data "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700966 "mode on remount\n");
967 return 0;
968 }
969 } else {
Mingming Cao617ba132006-10-11 01:20:53 -0700970 sbi->s_mount_opt &= ~EXT4_MOUNT_DATA_FLAGS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700971 sbi->s_mount_opt |= data_opt;
972 }
973 break;
974#ifdef CONFIG_QUOTA
975 case Opt_usrjquota:
976 qtype = USRQUOTA;
977 goto set_qf_name;
978 case Opt_grpjquota:
979 qtype = GRPQUOTA;
980set_qf_name:
981 if (sb_any_quota_enabled(sb)) {
982 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700983 "EXT4-fs: Cannot change journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700984 "quota options when quota turned on.\n");
985 return 0;
986 }
987 qname = match_strdup(&args[0]);
988 if (!qname) {
989 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700990 "EXT4-fs: not enough memory for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700991 "storing quotafile name.\n");
992 return 0;
993 }
994 if (sbi->s_qf_names[qtype] &&
995 strcmp(sbi->s_qf_names[qtype], qname)) {
996 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -0700997 "EXT4-fs: %s quota file already "
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700998 "specified.\n", QTYPE2NAME(qtype));
999 kfree(qname);
1000 return 0;
1001 }
1002 sbi->s_qf_names[qtype] = qname;
1003 if (strchr(sbi->s_qf_names[qtype], '/')) {
1004 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001005 "EXT4-fs: quotafile must be on "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001006 "filesystem root.\n");
1007 kfree(sbi->s_qf_names[qtype]);
1008 sbi->s_qf_names[qtype] = NULL;
1009 return 0;
1010 }
1011 set_opt(sbi->s_mount_opt, QUOTA);
1012 break;
1013 case Opt_offusrjquota:
1014 qtype = USRQUOTA;
1015 goto clear_qf_name;
1016 case Opt_offgrpjquota:
1017 qtype = GRPQUOTA;
1018clear_qf_name:
1019 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001020 printk(KERN_ERR "EXT4-fs: Cannot change "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021 "journalled quota options when "
1022 "quota turned on.\n");
1023 return 0;
1024 }
1025 /*
1026 * The space will be released later when all options
1027 * are confirmed to be correct
1028 */
1029 sbi->s_qf_names[qtype] = NULL;
1030 break;
1031 case Opt_jqfmt_vfsold:
1032 sbi->s_jquota_fmt = QFMT_VFS_OLD;
1033 break;
1034 case Opt_jqfmt_vfsv0:
1035 sbi->s_jquota_fmt = QFMT_VFS_V0;
1036 break;
1037 case Opt_quota:
1038 case Opt_usrquota:
1039 set_opt(sbi->s_mount_opt, QUOTA);
1040 set_opt(sbi->s_mount_opt, USRQUOTA);
1041 break;
1042 case Opt_grpquota:
1043 set_opt(sbi->s_mount_opt, QUOTA);
1044 set_opt(sbi->s_mount_opt, GRPQUOTA);
1045 break;
1046 case Opt_noquota:
1047 if (sb_any_quota_enabled(sb)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001048 printk(KERN_ERR "EXT4-fs: Cannot change quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001049 "options when quota turned on.\n");
1050 return 0;
1051 }
1052 clear_opt(sbi->s_mount_opt, QUOTA);
1053 clear_opt(sbi->s_mount_opt, USRQUOTA);
1054 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1055 break;
1056#else
1057 case Opt_quota:
1058 case Opt_usrquota:
1059 case Opt_grpquota:
1060 case Opt_usrjquota:
1061 case Opt_grpjquota:
1062 case Opt_offusrjquota:
1063 case Opt_offgrpjquota:
1064 case Opt_jqfmt_vfsold:
1065 case Opt_jqfmt_vfsv0:
1066 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001067 "EXT4-fs: journalled quota options not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001068 "supported.\n");
1069 break;
1070 case Opt_noquota:
1071 break;
1072#endif
1073 case Opt_abort:
1074 set_opt(sbi->s_mount_opt, ABORT);
1075 break;
1076 case Opt_barrier:
1077 if (match_int(&args[0], &option))
1078 return 0;
1079 if (option)
1080 set_opt(sbi->s_mount_opt, BARRIER);
1081 else
1082 clear_opt(sbi->s_mount_opt, BARRIER);
1083 break;
1084 case Opt_ignore:
1085 break;
1086 case Opt_resize:
1087 if (!is_remount) {
Mingming Cao617ba132006-10-11 01:20:53 -07001088 printk("EXT4-fs: resize option only available "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 "for remount\n");
1090 return 0;
1091 }
1092 if (match_int(&args[0], &option) != 0)
1093 return 0;
1094 *n_blocks_count = option;
1095 break;
1096 case Opt_nobh:
1097 set_opt(sbi->s_mount_opt, NOBH);
1098 break;
1099 case Opt_bh:
1100 clear_opt(sbi->s_mount_opt, NOBH);
1101 break;
Alex Tomasa86c6182006-10-11 01:21:03 -07001102 case Opt_extents:
1103 set_opt (sbi->s_mount_opt, EXTENTS);
1104 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001105 default:
1106 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001107 "EXT4-fs: Unrecognized mount option \"%s\" "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 "or missing value\n", p);
1109 return 0;
1110 }
1111 }
1112#ifdef CONFIG_QUOTA
1113 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001114 if ((sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 sbi->s_qf_names[USRQUOTA])
1116 clear_opt(sbi->s_mount_opt, USRQUOTA);
1117
Mingming Cao617ba132006-10-11 01:20:53 -07001118 if ((sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001119 sbi->s_qf_names[GRPQUOTA])
1120 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1121
1122 if ((sbi->s_qf_names[USRQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001123 (sbi->s_mount_opt & EXT4_MOUNT_GRPQUOTA)) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124 (sbi->s_qf_names[GRPQUOTA] &&
Mingming Cao617ba132006-10-11 01:20:53 -07001125 (sbi->s_mount_opt & EXT4_MOUNT_USRQUOTA))) {
1126 printk(KERN_ERR "EXT4-fs: old and new quota "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127 "format mixing.\n");
1128 return 0;
1129 }
1130
1131 if (!sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001132 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001133 "not specified.\n");
1134 return 0;
1135 }
1136 } else {
1137 if (sbi->s_jquota_fmt) {
Mingming Cao617ba132006-10-11 01:20:53 -07001138 printk(KERN_ERR "EXT4-fs: journalled quota format "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001139 "specified with no journalling "
1140 "enabled.\n");
1141 return 0;
1142 }
1143 }
1144#endif
1145 return 1;
1146}
1147
Mingming Cao617ba132006-10-11 01:20:53 -07001148static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001149 int read_only)
1150{
Mingming Cao617ba132006-10-11 01:20:53 -07001151 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001152 int res = 0;
1153
Mingming Cao617ba132006-10-11 01:20:53 -07001154 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1155 printk (KERN_ERR "EXT4-fs warning: revision level too high, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001156 "forcing read-only mode\n");
1157 res = MS_RDONLY;
1158 }
1159 if (read_only)
1160 return res;
Mingming Cao617ba132006-10-11 01:20:53 -07001161 if (!(sbi->s_mount_state & EXT4_VALID_FS))
1162 printk (KERN_WARNING "EXT4-fs warning: mounting unchecked fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001163 "running e2fsck is recommended\n");
Mingming Cao617ba132006-10-11 01:20:53 -07001164 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001165 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001166 "EXT4-fs warning: mounting fs with errors, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001167 "running e2fsck is recommended\n");
1168 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1169 le16_to_cpu(es->s_mnt_count) >=
1170 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1171 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001172 "EXT4-fs warning: maximal mount count reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001173 "running e2fsck is recommended\n");
1174 else if (le32_to_cpu(es->s_checkinterval) &&
1175 (le32_to_cpu(es->s_lastcheck) +
1176 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1177 printk (KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001178 "EXT4-fs warning: checktime reached, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001179 "running e2fsck is recommended\n");
1180#if 0
1181 /* @@@ We _will_ want to clear the valid bit if we find
1182 inconsistencies, to force a fsck at reboot. But for
1183 a plain journaled filesystem we can keep it set as
1184 valid forever! :) */
Mingming Cao617ba132006-10-11 01:20:53 -07001185 es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) & ~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001186#endif
1187 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07001188 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001189 es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
1190 es->s_mtime = cpu_to_le32(get_seconds());
Mingming Cao617ba132006-10-11 01:20:53 -07001191 ext4_update_dynamic_rev(sb);
1192 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001193
Mingming Cao617ba132006-10-11 01:20:53 -07001194 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195 if (test_opt(sb, DEBUG))
Mingming Cao617ba132006-10-11 01:20:53 -07001196 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%lu, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001197 "bpg=%lu, ipg=%lu, mo=%04lx]\n",
1198 sb->s_blocksize,
1199 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07001200 EXT4_BLOCKS_PER_GROUP(sb),
1201 EXT4_INODES_PER_GROUP(sb),
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001202 sbi->s_mount_opt);
1203
Mingming Cao617ba132006-10-11 01:20:53 -07001204 printk(KERN_INFO "EXT4 FS on %s, ", sb->s_id);
1205 if (EXT4_SB(sb)->s_journal->j_inode == NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206 char b[BDEVNAME_SIZE];
1207
1208 printk("external journal on %s\n",
Mingming Cao617ba132006-10-11 01:20:53 -07001209 bdevname(EXT4_SB(sb)->s_journal->j_dev, b));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001210 } else {
1211 printk("internal journal\n");
1212 }
1213 return res;
1214}
1215
1216/* Called at mount-time, super-block is locked */
Mingming Cao617ba132006-10-11 01:20:53 -07001217static int ext4_check_descriptors (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218{
Mingming Cao617ba132006-10-11 01:20:53 -07001219 struct ext4_sb_info *sbi = EXT4_SB(sb);
1220 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1221 ext4_fsblk_t last_block;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001222 ext4_fsblk_t block_bitmap;
1223 ext4_fsblk_t inode_bitmap;
1224 ext4_fsblk_t inode_table;
Mingming Cao617ba132006-10-11 01:20:53 -07001225 struct ext4_group_desc * gdp = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 int desc_block = 0;
1227 int i;
1228
Mingming Cao617ba132006-10-11 01:20:53 -07001229 ext4_debug ("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230
1231 for (i = 0; i < sbi->s_groups_count; i++)
1232 {
1233 if (i == sbi->s_groups_count - 1)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001234 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001235 else
1236 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07001237 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001238
Mingming Cao617ba132006-10-11 01:20:53 -07001239 if ((i % EXT4_DESC_PER_BLOCK(sb)) == 0)
1240 gdp = (struct ext4_group_desc *)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001241 sbi->s_group_desc[desc_block++]->b_data;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001242 block_bitmap = ext4_block_bitmap(gdp);
1243 if (block_bitmap < first_block || block_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001244 {
Mingming Cao617ba132006-10-11 01:20:53 -07001245 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001246 "Block bitmap for group %d"
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001247 " not in group (block "E3FSBLK")!",
1248 i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001249 return 0;
1250 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001251 inode_bitmap = ext4_inode_bitmap(gdp);
1252 if (inode_bitmap < first_block || inode_bitmap > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253 {
Mingming Cao617ba132006-10-11 01:20:53 -07001254 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 "Inode bitmap for group %d"
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001256 " not in group (block "E3FSBLK")!",
1257 i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001258 return 0;
1259 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001260 inode_table = ext4_inode_table(gdp);
1261 if (inode_table < first_block ||
1262 inode_table + sbi->s_itb_per_group > last_block)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001263 {
Mingming Cao617ba132006-10-11 01:20:53 -07001264 ext4_error (sb, "ext4_check_descriptors",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001265 "Inode table for group %d"
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001266 " not in group (block "E3FSBLK")!",
1267 i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001268 return 0;
1269 }
Mingming Cao617ba132006-10-11 01:20:53 -07001270 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001271 gdp++;
1272 }
1273
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001274 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07001275 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001276 return 1;
1277}
1278
1279
Mingming Cao617ba132006-10-11 01:20:53 -07001280/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 * the superblock) which were deleted from all directories, but held open by
1282 * a process at the time of a crash. We walk the list and try to delete these
1283 * inodes at recovery time (only with a read-write filesystem).
1284 *
1285 * In order to keep the orphan inode chain consistent during traversal (in
1286 * case of crash during recovery), we link each inode into the superblock
1287 * orphan list_head and handle it the same way as an inode deletion during
1288 * normal operation (which journals the operations for us).
1289 *
1290 * We only do an iget() and an iput() on each inode, which is very safe if we
1291 * accidentally point at an in-use or already deleted inode. The worst that
1292 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07001293 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 * e2fsck was run on this filesystem, and it must have already done the orphan
1295 * inode cleanup for us, so we can safely abort without any further action.
1296 */
Mingming Cao617ba132006-10-11 01:20:53 -07001297static void ext4_orphan_cleanup (struct super_block * sb,
1298 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001299{
1300 unsigned int s_flags = sb->s_flags;
1301 int nr_orphans = 0, nr_truncates = 0;
1302#ifdef CONFIG_QUOTA
1303 int i;
1304#endif
1305 if (!es->s_last_orphan) {
1306 jbd_debug(4, "no orphan inodes to clean up\n");
1307 return;
1308 }
1309
Mingming Cao617ba132006-10-11 01:20:53 -07001310 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001311 if (es->s_last_orphan)
1312 jbd_debug(1, "Errors on filesystem, "
1313 "clearing orphan list.\n");
1314 es->s_last_orphan = 0;
1315 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1316 return;
1317 }
1318
1319 if (s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07001320 printk(KERN_INFO "EXT4-fs: %s: orphan cleanup on readonly fs\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001321 sb->s_id);
1322 sb->s_flags &= ~MS_RDONLY;
1323 }
1324#ifdef CONFIG_QUOTA
1325 /* Needed for iput() to work correctly and not trash data */
1326 sb->s_flags |= MS_ACTIVE;
1327 /* Turn on quotas so that they are updated correctly */
1328 for (i = 0; i < MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07001329 if (EXT4_SB(sb)->s_qf_names[i]) {
1330 int ret = ext4_quota_on_mount(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001331 if (ret < 0)
1332 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001333 "EXT4-fs: Cannot turn on journalled "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001334 "quota: error %d\n", ret);
1335 }
1336 }
1337#endif
1338
1339 while (es->s_last_orphan) {
1340 struct inode *inode;
1341
1342 if (!(inode =
Mingming Cao617ba132006-10-11 01:20:53 -07001343 ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan)))) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001344 es->s_last_orphan = 0;
1345 break;
1346 }
1347
Mingming Cao617ba132006-10-11 01:20:53 -07001348 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349 DQUOT_INIT(inode);
1350 if (inode->i_nlink) {
1351 printk(KERN_DEBUG
1352 "%s: truncating inode %lu to %Ld bytes\n",
1353 __FUNCTION__, inode->i_ino, inode->i_size);
1354 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
1355 inode->i_ino, inode->i_size);
Mingming Cao617ba132006-10-11 01:20:53 -07001356 ext4_truncate(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001357 nr_truncates++;
1358 } else {
1359 printk(KERN_DEBUG
1360 "%s: deleting unreferenced inode %lu\n",
1361 __FUNCTION__, inode->i_ino);
1362 jbd_debug(2, "deleting unreferenced inode %lu\n",
1363 inode->i_ino);
1364 nr_orphans++;
1365 }
1366 iput(inode); /* The delete magic happens here! */
1367 }
1368
1369#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1370
1371 if (nr_orphans)
Mingming Cao617ba132006-10-11 01:20:53 -07001372 printk(KERN_INFO "EXT4-fs: %s: %d orphan inode%s deleted\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001373 sb->s_id, PLURAL(nr_orphans));
1374 if (nr_truncates)
Mingming Cao617ba132006-10-11 01:20:53 -07001375 printk(KERN_INFO "EXT4-fs: %s: %d truncate%s cleaned up\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001376 sb->s_id, PLURAL(nr_truncates));
1377#ifdef CONFIG_QUOTA
1378 /* Turn quotas off */
1379 for (i = 0; i < MAXQUOTAS; i++) {
1380 if (sb_dqopt(sb)->files[i])
1381 vfs_quota_off(sb, i);
1382 }
1383#endif
1384 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1385}
1386
1387#define log2(n) ffz(~(n))
1388
1389/*
1390 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1391 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1392 * We need to be 1 filesystem block less than the 2^32 sector limit.
1393 */
Mingming Cao617ba132006-10-11 01:20:53 -07001394static loff_t ext4_max_size(int bits)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395{
Mingming Cao617ba132006-10-11 01:20:53 -07001396 loff_t res = EXT4_NDIR_BLOCKS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001397 /* This constant is calculated to be the largest file size for a
1398 * dense, 4k-blocksize file such that the total number of
1399 * sectors in the file, including data and all indirect blocks,
1400 * does not exceed 2^32. */
1401 const loff_t upper_limit = 0x1ff7fffd000LL;
1402
1403 res += 1LL << (bits-2);
1404 res += 1LL << (2*(bits-2));
1405 res += 1LL << (3*(bits-2));
1406 res <<= bits;
1407 if (res > upper_limit)
1408 res = upper_limit;
1409 return res;
1410}
1411
Mingming Cao617ba132006-10-11 01:20:53 -07001412static ext4_fsblk_t descriptor_loc(struct super_block *sb,
1413 ext4_fsblk_t logic_sb_block,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001414 int nr)
1415{
Mingming Cao617ba132006-10-11 01:20:53 -07001416 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417 unsigned long bg, first_meta_bg;
1418 int has_super = 0;
1419
1420 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1421
Mingming Cao617ba132006-10-11 01:20:53 -07001422 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001423 nr < first_meta_bg)
1424 return (logic_sb_block + nr + 1);
1425 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07001426 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001427 has_super = 1;
Mingming Cao617ba132006-10-11 01:20:53 -07001428 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001429}
1430
1431
Mingming Cao617ba132006-10-11 01:20:53 -07001432static int ext4_fill_super (struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001433{
1434 struct buffer_head * bh;
Mingming Cao617ba132006-10-11 01:20:53 -07001435 struct ext4_super_block *es = NULL;
1436 struct ext4_sb_info *sbi;
1437 ext4_fsblk_t block;
1438 ext4_fsblk_t sb_block = get_sb_block(&data);
1439 ext4_fsblk_t logic_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440 unsigned long offset = 0;
1441 unsigned int journal_inum = 0;
1442 unsigned long journal_devnum = 0;
1443 unsigned long def_mount_opts;
1444 struct inode *root;
1445 int blocksize;
1446 int hblock;
1447 int db_count;
1448 int i;
1449 int needs_recovery;
1450 __le32 features;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001451 __u64 blocks_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452
1453 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
1454 if (!sbi)
1455 return -ENOMEM;
1456 sb->s_fs_info = sbi;
1457 sbi->s_mount_opt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001458 sbi->s_resuid = EXT4_DEF_RESUID;
1459 sbi->s_resgid = EXT4_DEF_RESGID;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001460
1461 unlock_kernel();
1462
Mingming Cao617ba132006-10-11 01:20:53 -07001463 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001464 if (!blocksize) {
Mingming Cao617ba132006-10-11 01:20:53 -07001465 printk(KERN_ERR "EXT4-fs: unable to set blocksize\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001466 goto out_fail;
1467 }
1468
1469 /*
Mingming Cao617ba132006-10-11 01:20:53 -07001470 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001471 * block sizes. We need to calculate the offset from buffer start.
1472 */
Mingming Cao617ba132006-10-11 01:20:53 -07001473 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Mingming Cao3a5b2ec2006-10-11 01:21:05 -07001474 logic_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1475 offset = sector_div(logic_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001476 } else {
1477 logic_sb_block = sb_block;
1478 }
1479
1480 if (!(bh = sb_bread(sb, logic_sb_block))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001481 printk (KERN_ERR "EXT4-fs: unable to read superblock\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001482 goto out_fail;
1483 }
1484 /*
1485 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07001486 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001487 */
Mingming Cao617ba132006-10-11 01:20:53 -07001488 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001489 sbi->s_es = es;
1490 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07001491 if (sb->s_magic != EXT4_SUPER_MAGIC)
1492 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001493
1494 /* Set defaults before we parse the mount options */
1495 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Mingming Cao617ba132006-10-11 01:20:53 -07001496 if (def_mount_opts & EXT4_DEFM_DEBUG)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001497 set_opt(sbi->s_mount_opt, DEBUG);
Mingming Cao617ba132006-10-11 01:20:53 -07001498 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001499 set_opt(sbi->s_mount_opt, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07001500 if (def_mount_opts & EXT4_DEFM_UID16)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001501 set_opt(sbi->s_mount_opt, NO_UID32);
Mingming Cao617ba132006-10-11 01:20:53 -07001502 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503 set_opt(sbi->s_mount_opt, XATTR_USER);
Mingming Cao617ba132006-10-11 01:20:53 -07001504 if (def_mount_opts & EXT4_DEFM_ACL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001505 set_opt(sbi->s_mount_opt, POSIX_ACL);
Mingming Cao617ba132006-10-11 01:20:53 -07001506 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
1507 sbi->s_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
1508 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
1509 sbi->s_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
1510 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
1511 sbi->s_mount_opt |= EXT4_MOUNT_WRITEBACK_DATA;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001512
Mingming Cao617ba132006-10-11 01:20:53 -07001513 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001514 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Mingming Cao617ba132006-10-11 01:20:53 -07001515 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_RO)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516 set_opt(sbi->s_mount_opt, ERRORS_RO);
1517
1518 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
1519 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
1520
1521 set_opt(sbi->s_mount_opt, RESERVATION);
1522
1523 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1524 NULL, 0))
1525 goto failed_mount;
1526
1527 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07001528 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001529
Mingming Cao617ba132006-10-11 01:20:53 -07001530 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
1531 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
1532 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1533 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001534 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07001535 "EXT4-fs warning: feature flags set on rev 0 fs, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001536 "running e2fsck is recommended\n");
1537 /*
1538 * Check feature flags regardless of the revision level, since we
1539 * previously didn't change the revision level when setting the flags,
1540 * so there is a chance incompat flags are set on a rev 0 filesystem.
1541 */
Mingming Cao617ba132006-10-11 01:20:53 -07001542 features = EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543 if (features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001544 printk(KERN_ERR "EXT4-fs: %s: couldn't mount because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001545 "unsupported optional features (%x).\n",
1546 sb->s_id, le32_to_cpu(features));
1547 goto failed_mount;
1548 }
Mingming Cao617ba132006-10-11 01:20:53 -07001549 features = EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001550 if (!(sb->s_flags & MS_RDONLY) && features) {
Mingming Cao617ba132006-10-11 01:20:53 -07001551 printk(KERN_ERR "EXT4-fs: %s: couldn't mount RDWR because of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001552 "unsupported optional features (%x).\n",
1553 sb->s_id, le32_to_cpu(features));
1554 goto failed_mount;
1555 }
1556 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1557
Mingming Cao617ba132006-10-11 01:20:53 -07001558 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
1559 blocksize > EXT4_MAX_BLOCK_SIZE) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001560 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001561 "EXT4-fs: Unsupported filesystem blocksize %d on %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 blocksize, sb->s_id);
1563 goto failed_mount;
1564 }
1565
1566 hblock = bdev_hardsect_size(sb->s_bdev);
1567 if (sb->s_blocksize != blocksize) {
1568 /*
1569 * Make sure the blocksize for the filesystem is larger
1570 * than the hardware sectorsize for the machine.
1571 */
1572 if (blocksize < hblock) {
Mingming Cao617ba132006-10-11 01:20:53 -07001573 printk(KERN_ERR "EXT4-fs: blocksize %d too small for "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001574 "device blocksize %d.\n", blocksize, hblock);
1575 goto failed_mount;
1576 }
1577
1578 brelse (bh);
1579 sb_set_blocksize(sb, blocksize);
Mingming Cao3a5b2ec2006-10-11 01:21:05 -07001580 logic_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
1581 offset = sector_div(logic_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001582 bh = sb_bread(sb, logic_sb_block);
1583 if (!bh) {
1584 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001585 "EXT4-fs: Can't read superblock on 2nd try.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001586 goto failed_mount;
1587 }
Mingming Cao617ba132006-10-11 01:20:53 -07001588 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001589 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07001590 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001592 "EXT4-fs: Magic mismatch, very weird !\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001593 goto failed_mount;
1594 }
1595 }
1596
Mingming Cao617ba132006-10-11 01:20:53 -07001597 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001598
Mingming Cao617ba132006-10-11 01:20:53 -07001599 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
1600 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
1601 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602 } else {
1603 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1604 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
Mingming Cao617ba132006-10-11 01:20:53 -07001605 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001606 (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
1607 (sbi->s_inode_size > blocksize)) {
1608 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001609 "EXT4-fs: unsupported inode size: %d\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001610 sbi->s_inode_size);
1611 goto failed_mount;
1612 }
1613 }
Mingming Cao617ba132006-10-11 01:20:53 -07001614 sbi->s_frag_size = EXT4_MIN_FRAG_SIZE <<
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 le32_to_cpu(es->s_log_frag_size);
1616 if (blocksize != sbi->s_frag_size) {
1617 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001618 "EXT4-fs: fragsize %lu != blocksize %u (unsupported)\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001619 sbi->s_frag_size, blocksize);
1620 goto failed_mount;
1621 }
1622 sbi->s_frags_per_block = 1;
1623 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1624 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1625 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Mingming Cao617ba132006-10-11 01:20:53 -07001626 if (EXT4_INODE_SIZE(sb) == 0)
1627 goto cantfind_ext4;
1628 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001629 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07001630 goto cantfind_ext4;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001631 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1632 sbi->s_inodes_per_block;
Mingming Cao617ba132006-10-11 01:20:53 -07001633 sbi->s_desc_per_block = blocksize / sizeof(struct ext4_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001634 sbi->s_sbh = bh;
1635 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07001636 sbi->s_addr_per_block_bits = log2(EXT4_ADDR_PER_BLOCK(sb));
1637 sbi->s_desc_per_block_bits = log2(EXT4_DESC_PER_BLOCK(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001638 for (i=0; i < 4; i++)
1639 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1640 sbi->s_def_hash_version = es->s_def_hash_version;
1641
1642 if (sbi->s_blocks_per_group > blocksize * 8) {
1643 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001644 "EXT4-fs: #blocks per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645 sbi->s_blocks_per_group);
1646 goto failed_mount;
1647 }
1648 if (sbi->s_frags_per_group > blocksize * 8) {
1649 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001650 "EXT4-fs: #fragments per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001651 sbi->s_frags_per_group);
1652 goto failed_mount;
1653 }
1654 if (sbi->s_inodes_per_group > blocksize * 8) {
1655 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001656 "EXT4-fs: #inodes per group too big: %lu\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 sbi->s_inodes_per_group);
1658 goto failed_mount;
1659 }
1660
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001661 if (ext4_blocks_count(es) >
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001662 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001663 printk(KERN_ERR "EXT4-fs: filesystem on %s:"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 " too large to mount safely\n", sb->s_id);
1665 if (sizeof(sector_t) < 8)
Mingming Cao617ba132006-10-11 01:20:53 -07001666 printk(KERN_WARNING "EXT4-fs: CONFIG_LBD not "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001667 "enabled\n");
1668 goto failed_mount;
1669 }
1670
Mingming Cao617ba132006-10-11 01:20:53 -07001671 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
1672 goto cantfind_ext4;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001673 blocks_count = (ext4_blocks_count(es) -
1674 le32_to_cpu(es->s_first_data_block) +
1675 EXT4_BLOCKS_PER_GROUP(sb) - 1);
1676 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
1677 sbi->s_groups_count = blocks_count;
Mingming Cao617ba132006-10-11 01:20:53 -07001678 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
1679 EXT4_DESC_PER_BLOCK(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001680 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1681 GFP_KERNEL);
1682 if (sbi->s_group_desc == NULL) {
Mingming Cao617ba132006-10-11 01:20:53 -07001683 printk (KERN_ERR "EXT4-fs: not enough memory\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001684 goto failed_mount;
1685 }
1686
1687 bgl_lock_init(&sbi->s_blockgroup_lock);
1688
1689 for (i = 0; i < db_count; i++) {
1690 block = descriptor_loc(sb, logic_sb_block, i);
1691 sbi->s_group_desc[i] = sb_bread(sb, block);
1692 if (!sbi->s_group_desc[i]) {
Mingming Cao617ba132006-10-11 01:20:53 -07001693 printk (KERN_ERR "EXT4-fs: "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 "can't read group descriptor %d\n", i);
1695 db_count = i;
1696 goto failed_mount2;
1697 }
1698 }
Mingming Cao617ba132006-10-11 01:20:53 -07001699 if (!ext4_check_descriptors (sb)) {
1700 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001701 goto failed_mount2;
1702 }
1703 sbi->s_gdb_count = db_count;
1704 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1705 spin_lock_init(&sbi->s_next_gen_lock);
1706
1707 percpu_counter_init(&sbi->s_freeblocks_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001708 ext4_count_free_blocks(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001709 percpu_counter_init(&sbi->s_freeinodes_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001710 ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711 percpu_counter_init(&sbi->s_dirs_counter,
Mingming Cao617ba132006-10-11 01:20:53 -07001712 ext4_count_dirs(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001713
1714 /* per fileystem reservation list head & lock */
1715 spin_lock_init(&sbi->s_rsv_window_lock);
1716 sbi->s_rsv_window_root = RB_ROOT;
1717 /* Add a single, static dummy reservation to the start of the
1718 * reservation window list --- it gives us a placeholder for
1719 * append-at-start-of-list which makes the allocation logic
1720 * _much_ simpler. */
Mingming Cao617ba132006-10-11 01:20:53 -07001721 sbi->s_rsv_window_head.rsv_start = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
1722 sbi->s_rsv_window_head.rsv_end = EXT4_RESERVE_WINDOW_NOT_ALLOCATED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001723 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1724 sbi->s_rsv_window_head.rsv_goal_size = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07001725 ext4_rsv_window_add(sb, &sbi->s_rsv_window_head);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001726
1727 /*
1728 * set up enough so that it can read an inode
1729 */
Mingming Cao617ba132006-10-11 01:20:53 -07001730 sb->s_op = &ext4_sops;
1731 sb->s_export_op = &ext4_export_ops;
1732 sb->s_xattr = ext4_xattr_handlers;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001733#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001734 sb->s_qcop = &ext4_qctl_operations;
1735 sb->dq_op = &ext4_quota_operations;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001736#endif
1737 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
1738
1739 sb->s_root = NULL;
1740
1741 needs_recovery = (es->s_last_orphan != 0 ||
Mingming Cao617ba132006-10-11 01:20:53 -07001742 EXT4_HAS_INCOMPAT_FEATURE(sb,
1743 EXT4_FEATURE_INCOMPAT_RECOVER));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001744
1745 /*
1746 * The first inode we look at is the journal inode. Don't try
1747 * root first: it may be modified in the journal!
1748 */
1749 if (!test_opt(sb, NOLOAD) &&
Mingming Cao617ba132006-10-11 01:20:53 -07001750 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
1751 if (ext4_load_journal(sb, es, journal_devnum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 goto failed_mount3;
1753 } else if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07001754 if (ext4_create_journal(sb, es, journal_inum))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001755 goto failed_mount3;
1756 } else {
1757 if (!silent)
1758 printk (KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001759 "ext4: No journal on filesystem on %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001760 sb->s_id);
1761 goto failed_mount3;
1762 }
1763
1764 /* We have now updated the journal if required, so we can
1765 * validate the data journaling mode. */
1766 switch (test_opt(sb, DATA_FLAGS)) {
1767 case 0:
1768 /* No mode set, assume a default based on the journal
1769 capabilities: ORDERED_DATA if the journal can
1770 cope, else JOURNAL_DATA */
Mingming Caodab291a2006-10-11 01:21:01 -07001771 if (jbd2_journal_check_available_features
1772 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001773 set_opt(sbi->s_mount_opt, ORDERED_DATA);
1774 else
1775 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
1776 break;
1777
Mingming Cao617ba132006-10-11 01:20:53 -07001778 case EXT4_MOUNT_ORDERED_DATA:
1779 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07001780 if (!jbd2_journal_check_available_features
1781 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001782 printk(KERN_ERR "EXT4-fs: Journal does not support "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001783 "requested data journaling mode\n");
1784 goto failed_mount4;
1785 }
1786 default:
1787 break;
1788 }
1789
1790 if (test_opt(sb, NOBH)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001791 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
1792 printk(KERN_WARNING "EXT4-fs: Ignoring nobh option - "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001793 "its supported only with writeback mode\n");
1794 clear_opt(sbi->s_mount_opt, NOBH);
1795 }
1796 }
1797 /*
Mingming Caodab291a2006-10-11 01:21:01 -07001798 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001799 * so we can safely mount the rest of the filesystem now.
1800 */
1801
Mingming Cao617ba132006-10-11 01:20:53 -07001802 root = iget(sb, EXT4_ROOT_INO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001803 sb->s_root = d_alloc_root(root);
1804 if (!sb->s_root) {
Mingming Cao617ba132006-10-11 01:20:53 -07001805 printk(KERN_ERR "EXT4-fs: get root inode failed\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001806 iput(root);
1807 goto failed_mount4;
1808 }
1809 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1810 dput(sb->s_root);
1811 sb->s_root = NULL;
Mingming Cao617ba132006-10-11 01:20:53 -07001812 printk(KERN_ERR "EXT4-fs: corrupt root inode, run e2fsck\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001813 goto failed_mount4;
1814 }
1815
Mingming Cao617ba132006-10-11 01:20:53 -07001816 ext4_setup_super (sb, es, sb->s_flags & MS_RDONLY);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817 /*
1818 * akpm: core read_super() calls in here with the superblock locked.
1819 * That deadlocks, because orphan cleanup needs to lock the superblock
1820 * in numerous places. Here we just pop the lock - it's relatively
1821 * harmless, because we are now ready to accept write_super() requests,
1822 * and aviro says that's the only reason for hanging onto the
1823 * superblock lock.
1824 */
Mingming Cao617ba132006-10-11 01:20:53 -07001825 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
1826 ext4_orphan_cleanup(sb, es);
1827 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001828 if (needs_recovery)
Mingming Cao617ba132006-10-11 01:20:53 -07001829 printk (KERN_INFO "EXT4-fs: recovery complete.\n");
1830 ext4_mark_recovery_complete(sb, es);
1831 printk (KERN_INFO "EXT4-fs: mounted filesystem with %s data mode.\n",
1832 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ? "journal":
1833 test_opt(sb,DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA ? "ordered":
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001834 "writeback");
1835
Alex Tomasa86c6182006-10-11 01:21:03 -07001836 ext4_ext_init(sb);
1837
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001838 lock_kernel();
1839 return 0;
1840
Mingming Cao617ba132006-10-11 01:20:53 -07001841cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001842 if (!silent)
Mingming Cao617ba132006-10-11 01:20:53 -07001843 printk(KERN_ERR "VFS: Can't find ext4 filesystem on dev %s.\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001844 sb->s_id);
1845 goto failed_mount;
1846
1847failed_mount4:
Mingming Caodab291a2006-10-11 01:21:01 -07001848 jbd2_journal_destroy(sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001849failed_mount3:
1850 percpu_counter_destroy(&sbi->s_freeblocks_counter);
1851 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1852 percpu_counter_destroy(&sbi->s_dirs_counter);
1853failed_mount2:
1854 for (i = 0; i < db_count; i++)
1855 brelse(sbi->s_group_desc[i]);
1856 kfree(sbi->s_group_desc);
1857failed_mount:
1858#ifdef CONFIG_QUOTA
1859 for (i = 0; i < MAXQUOTAS; i++)
1860 kfree(sbi->s_qf_names[i]);
1861#endif
Mingming Cao617ba132006-10-11 01:20:53 -07001862 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001863 brelse(bh);
1864out_fail:
1865 sb->s_fs_info = NULL;
1866 kfree(sbi);
1867 lock_kernel();
1868 return -EINVAL;
1869}
1870
1871/*
1872 * Setup any per-fs journal parameters now. We'll do this both on
1873 * initial mount, once the journal has been initialised but before we've
1874 * done any recovery; and again on any subsequent remount.
1875 */
Mingming Cao617ba132006-10-11 01:20:53 -07001876static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001877{
Mingming Cao617ba132006-10-11 01:20:53 -07001878 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001879
1880 if (sbi->s_commit_interval)
1881 journal->j_commit_interval = sbi->s_commit_interval;
Mingming Cao617ba132006-10-11 01:20:53 -07001882 /* We could also set up an ext4-specific default for the commit
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001883 * interval here, but for now we'll just fall back to the jbd
1884 * default. */
1885
1886 spin_lock(&journal->j_state_lock);
1887 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07001888 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001889 else
Mingming Caodab291a2006-10-11 01:21:01 -07001890 journal->j_flags &= ~JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001891 spin_unlock(&journal->j_state_lock);
1892}
1893
Mingming Cao617ba132006-10-11 01:20:53 -07001894static journal_t *ext4_get_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001895 unsigned int journal_inum)
1896{
1897 struct inode *journal_inode;
1898 journal_t *journal;
1899
1900 /* First, test for the existence of a valid inode on disk. Bad
1901 * things happen if we iget() an unused inode, as the subsequent
1902 * iput() will try to delete it. */
1903
1904 journal_inode = iget(sb, journal_inum);
1905 if (!journal_inode) {
Mingming Cao617ba132006-10-11 01:20:53 -07001906 printk(KERN_ERR "EXT4-fs: no journal found.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001907 return NULL;
1908 }
1909 if (!journal_inode->i_nlink) {
1910 make_bad_inode(journal_inode);
1911 iput(journal_inode);
Mingming Cao617ba132006-10-11 01:20:53 -07001912 printk(KERN_ERR "EXT4-fs: journal inode is deleted.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001913 return NULL;
1914 }
1915
1916 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
1917 journal_inode, journal_inode->i_size);
1918 if (is_bad_inode(journal_inode) || !S_ISREG(journal_inode->i_mode)) {
Mingming Cao617ba132006-10-11 01:20:53 -07001919 printk(KERN_ERR "EXT4-fs: invalid journal inode.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001920 iput(journal_inode);
1921 return NULL;
1922 }
1923
Mingming Caodab291a2006-10-11 01:21:01 -07001924 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001925 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07001926 printk(KERN_ERR "EXT4-fs: Could not load journal inode\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001927 iput(journal_inode);
1928 return NULL;
1929 }
1930 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07001931 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001932 return journal;
1933}
1934
Mingming Cao617ba132006-10-11 01:20:53 -07001935static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001936 dev_t j_dev)
1937{
1938 struct buffer_head * bh;
1939 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07001940 ext4_fsblk_t start;
1941 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001942 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07001943 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001944 unsigned long offset;
Mingming Cao617ba132006-10-11 01:20:53 -07001945 struct ext4_super_block * es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001946 struct block_device *bdev;
1947
Mingming Cao617ba132006-10-11 01:20:53 -07001948 bdev = ext4_blkdev_get(j_dev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001949 if (bdev == NULL)
1950 return NULL;
1951
1952 if (bd_claim(bdev, sb)) {
1953 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001954 "EXT4: failed to claim external journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001955 blkdev_put(bdev);
1956 return NULL;
1957 }
1958
1959 blocksize = sb->s_blocksize;
1960 hblock = bdev_hardsect_size(bdev);
1961 if (blocksize < hblock) {
1962 printk(KERN_ERR
Mingming Cao617ba132006-10-11 01:20:53 -07001963 "EXT4-fs: blocksize too small for journal device.\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001964 goto out_bdev;
1965 }
1966
Mingming Cao617ba132006-10-11 01:20:53 -07001967 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
1968 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001969 set_blocksize(bdev, blocksize);
1970 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Mingming Cao617ba132006-10-11 01:20:53 -07001971 printk(KERN_ERR "EXT4-fs: couldn't read superblock of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001972 "external journal\n");
1973 goto out_bdev;
1974 }
1975
Mingming Cao617ba132006-10-11 01:20:53 -07001976 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
1977 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001978 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07001979 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
1980 printk(KERN_ERR "EXT4-fs: external journal has "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001981 "bad superblock\n");
1982 brelse(bh);
1983 goto out_bdev;
1984 }
1985
Mingming Cao617ba132006-10-11 01:20:53 -07001986 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
1987 printk(KERN_ERR "EXT4-fs: journal UUID does not match\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001988 brelse(bh);
1989 goto out_bdev;
1990 }
1991
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07001992 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001993 start = sb_block + 1;
1994 brelse(bh); /* we're done with the superblock */
1995
Mingming Caodab291a2006-10-11 01:21:01 -07001996 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001997 start, len, blocksize);
1998 if (!journal) {
Mingming Cao617ba132006-10-11 01:20:53 -07001999 printk(KERN_ERR "EXT4-fs: failed to create device journal\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002000 goto out_bdev;
2001 }
2002 journal->j_private = sb;
2003 ll_rw_block(READ, 1, &journal->j_sb_buffer);
2004 wait_on_buffer(journal->j_sb_buffer);
2005 if (!buffer_uptodate(journal->j_sb_buffer)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002006 printk(KERN_ERR "EXT4-fs: I/O error on journal device\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002007 goto out_journal;
2008 }
2009 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Mingming Cao617ba132006-10-11 01:20:53 -07002010 printk(KERN_ERR "EXT4-fs: External journal has more than one "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002011 "user (unsupported) - %d\n",
2012 be32_to_cpu(journal->j_superblock->s_nr_users));
2013 goto out_journal;
2014 }
Mingming Cao617ba132006-10-11 01:20:53 -07002015 EXT4_SB(sb)->journal_bdev = bdev;
2016 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002017 return journal;
2018out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07002019 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002020out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07002021 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002022 return NULL;
2023}
2024
Mingming Cao617ba132006-10-11 01:20:53 -07002025static int ext4_load_journal(struct super_block *sb,
2026 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002027 unsigned long journal_devnum)
2028{
2029 journal_t *journal;
2030 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
2031 dev_t journal_dev;
2032 int err = 0;
2033 int really_read_only;
2034
2035 if (journal_devnum &&
2036 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002037 printk(KERN_INFO "EXT4-fs: external journal device major/minor "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002038 "numbers have changed\n");
2039 journal_dev = new_decode_dev(journal_devnum);
2040 } else
2041 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2042
2043 really_read_only = bdev_read_only(sb->s_bdev);
2044
2045 /*
2046 * Are we loading a blank journal or performing recovery after a
2047 * crash? For recovery, we need to check in advance whether we
2048 * can get read-write access to the device.
2049 */
2050
Mingming Cao617ba132006-10-11 01:20:53 -07002051 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002052 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002053 printk(KERN_INFO "EXT4-fs: INFO: recovery "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002054 "required on readonly filesystem.\n");
2055 if (really_read_only) {
Mingming Cao617ba132006-10-11 01:20:53 -07002056 printk(KERN_ERR "EXT4-fs: write access "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002057 "unavailable, cannot proceed.\n");
2058 return -EROFS;
2059 }
Mingming Cao617ba132006-10-11 01:20:53 -07002060 printk (KERN_INFO "EXT4-fs: write access will "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002061 "be enabled during recovery.\n");
2062 }
2063 }
2064
2065 if (journal_inum && journal_dev) {
Mingming Cao617ba132006-10-11 01:20:53 -07002066 printk(KERN_ERR "EXT4-fs: filesystem has both journal "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002067 "and inode journals!\n");
2068 return -EINVAL;
2069 }
2070
2071 if (journal_inum) {
Mingming Cao617ba132006-10-11 01:20:53 -07002072 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002073 return -EINVAL;
2074 } else {
Mingming Cao617ba132006-10-11 01:20:53 -07002075 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002076 return -EINVAL;
2077 }
2078
2079 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
Mingming Caodab291a2006-10-11 01:21:01 -07002080 err = jbd2_journal_update_format(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002081 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002082 printk(KERN_ERR "EXT4-fs: error updating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002083 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002084 return err;
2085 }
2086 }
2087
Mingming Cao617ba132006-10-11 01:20:53 -07002088 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
Mingming Caodab291a2006-10-11 01:21:01 -07002089 err = jbd2_journal_wipe(journal, !really_read_only);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002090 if (!err)
Mingming Caodab291a2006-10-11 01:21:01 -07002091 err = jbd2_journal_load(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002092
2093 if (err) {
Mingming Cao617ba132006-10-11 01:20:53 -07002094 printk(KERN_ERR "EXT4-fs: error loading journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002095 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002096 return err;
2097 }
2098
Mingming Cao617ba132006-10-11 01:20:53 -07002099 EXT4_SB(sb)->s_journal = journal;
2100 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002101
2102 if (journal_devnum &&
2103 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2104 es->s_journal_dev = cpu_to_le32(journal_devnum);
2105 sb->s_dirt = 1;
2106
2107 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002108 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002109 }
2110
2111 return 0;
2112}
2113
Mingming Cao617ba132006-10-11 01:20:53 -07002114static int ext4_create_journal(struct super_block * sb,
2115 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002116 unsigned int journal_inum)
2117{
2118 journal_t *journal;
2119
2120 if (sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002121 printk(KERN_ERR "EXT4-fs: readonly filesystem when trying to "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002122 "create journal.\n");
2123 return -EROFS;
2124 }
2125
Mingming Cao617ba132006-10-11 01:20:53 -07002126 if (!(journal = ext4_get_journal(sb, journal_inum)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002127 return -EINVAL;
2128
Mingming Cao617ba132006-10-11 01:20:53 -07002129 printk(KERN_INFO "EXT4-fs: creating new journal on inode %u\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002130 journal_inum);
2131
Mingming Caodab291a2006-10-11 01:21:01 -07002132 if (jbd2_journal_create(journal)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002133 printk(KERN_ERR "EXT4-fs: error creating journal.\n");
Mingming Caodab291a2006-10-11 01:21:01 -07002134 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002135 return -EIO;
2136 }
2137
Mingming Cao617ba132006-10-11 01:20:53 -07002138 EXT4_SB(sb)->s_journal = journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002139
Mingming Cao617ba132006-10-11 01:20:53 -07002140 ext4_update_dynamic_rev(sb);
2141 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2142 EXT4_SET_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002143
2144 es->s_journal_inum = cpu_to_le32(journal_inum);
2145 sb->s_dirt = 1;
2146
2147 /* Make sure we flush the recovery flag to disk. */
Mingming Cao617ba132006-10-11 01:20:53 -07002148 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002149
2150 return 0;
2151}
2152
Mingming Cao617ba132006-10-11 01:20:53 -07002153static void ext4_commit_super (struct super_block * sb,
2154 struct ext4_super_block * es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002155 int sync)
2156{
Mingming Cao617ba132006-10-11 01:20:53 -07002157 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002158
2159 if (!sbh)
2160 return;
2161 es->s_wtime = cpu_to_le32(get_seconds());
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002162 ext4_free_blocks_count_set(es, ext4_count_free_blocks(sb));
Mingming Cao617ba132006-10-11 01:20:53 -07002163 es->s_free_inodes_count = cpu_to_le32(ext4_count_free_inodes(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002164 BUFFER_TRACE(sbh, "marking dirty");
2165 mark_buffer_dirty(sbh);
2166 if (sync)
2167 sync_dirty_buffer(sbh);
2168}
2169
2170
2171/*
2172 * Have we just finished recovery? If so, and if we are mounting (or
2173 * remounting) the filesystem readonly, then we will end up with a
2174 * consistent fs on disk. Record that fact.
2175 */
Mingming Cao617ba132006-10-11 01:20:53 -07002176static void ext4_mark_recovery_complete(struct super_block * sb,
2177 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002178{
Mingming Cao617ba132006-10-11 01:20:53 -07002179 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002180
Mingming Caodab291a2006-10-11 01:21:01 -07002181 jbd2_journal_lock_updates(journal);
2182 jbd2_journal_flush(journal);
Mingming Cao617ba132006-10-11 01:20:53 -07002183 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002184 sb->s_flags & MS_RDONLY) {
Mingming Cao617ba132006-10-11 01:20:53 -07002185 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002186 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002187 ext4_commit_super(sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002188 }
Mingming Caodab291a2006-10-11 01:21:01 -07002189 jbd2_journal_unlock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002190}
2191
2192/*
2193 * If we are mounting (or read-write remounting) a filesystem whose journal
2194 * has recorded an error from a previous lifetime, move that error to the
2195 * main filesystem now.
2196 */
Mingming Cao617ba132006-10-11 01:20:53 -07002197static void ext4_clear_journal_err(struct super_block * sb,
2198 struct ext4_super_block * es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002199{
2200 journal_t *journal;
2201 int j_errno;
2202 const char *errstr;
2203
Mingming Cao617ba132006-10-11 01:20:53 -07002204 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002205
2206 /*
2207 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07002208 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002209 */
2210
Mingming Caodab291a2006-10-11 01:21:01 -07002211 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002212 if (j_errno) {
2213 char nbuf[16];
2214
Mingming Cao617ba132006-10-11 01:20:53 -07002215 errstr = ext4_decode_error(sb, j_errno, nbuf);
2216 ext4_warning(sb, __FUNCTION__, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002217 "from previous mount: %s", errstr);
Mingming Cao617ba132006-10-11 01:20:53 -07002218 ext4_warning(sb, __FUNCTION__, "Marking fs in need of "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002219 "filesystem check.");
2220
Mingming Cao617ba132006-10-11 01:20:53 -07002221 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2222 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
2223 ext4_commit_super (sb, es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002224
Mingming Caodab291a2006-10-11 01:21:01 -07002225 jbd2_journal_clear_err(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002226 }
2227}
2228
2229/*
2230 * Force the running and committing transactions to commit,
2231 * and wait on the commit.
2232 */
Mingming Cao617ba132006-10-11 01:20:53 -07002233int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002234{
2235 journal_t *journal;
2236 int ret;
2237
2238 if (sb->s_flags & MS_RDONLY)
2239 return 0;
2240
Mingming Cao617ba132006-10-11 01:20:53 -07002241 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002242 sb->s_dirt = 0;
Mingming Cao617ba132006-10-11 01:20:53 -07002243 ret = ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002244 return ret;
2245}
2246
2247/*
Mingming Cao617ba132006-10-11 01:20:53 -07002248 * Ext4 always journals updates to the superblock itself, so we don't
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002249 * have to propagate any other updates to the superblock on disk at this
2250 * point. Just start an async writeback to get the buffers on their way
2251 * to the disk.
2252 *
2253 * This implicitly triggers the writebehind on sync().
2254 */
2255
Mingming Cao617ba132006-10-11 01:20:53 -07002256static void ext4_write_super (struct super_block * sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002257{
2258 if (mutex_trylock(&sb->s_lock) != 0)
2259 BUG();
2260 sb->s_dirt = 0;
2261}
2262
Mingming Cao617ba132006-10-11 01:20:53 -07002263static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264{
2265 tid_t target;
2266
2267 sb->s_dirt = 0;
Mingming Caodab291a2006-10-11 01:21:01 -07002268 if (jbd2_journal_start_commit(EXT4_SB(sb)->s_journal, &target)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002269 if (wait)
Mingming Caodab291a2006-10-11 01:21:01 -07002270 jbd2_log_wait_commit(EXT4_SB(sb)->s_journal, target);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002271 }
2272 return 0;
2273}
2274
2275/*
2276 * LVM calls this function before a (read-only) snapshot is created. This
2277 * gives us a chance to flush the journal completely and mark the fs clean.
2278 */
Mingming Cao617ba132006-10-11 01:20:53 -07002279static void ext4_write_super_lockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002280{
2281 sb->s_dirt = 0;
2282
2283 if (!(sb->s_flags & MS_RDONLY)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002284 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002285
2286 /* Now we set up the journal barrier. */
Mingming Caodab291a2006-10-11 01:21:01 -07002287 jbd2_journal_lock_updates(journal);
2288 jbd2_journal_flush(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002289
2290 /* Journal blocked and flushed, clear needs_recovery flag. */
Mingming Cao617ba132006-10-11 01:20:53 -07002291 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2292 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002293 }
2294}
2295
2296/*
2297 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2298 * flag here, even though the filesystem is not technically dirty yet.
2299 */
Mingming Cao617ba132006-10-11 01:20:53 -07002300static void ext4_unlockfs(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002301{
2302 if (!(sb->s_flags & MS_RDONLY)) {
2303 lock_super(sb);
2304 /* Reser the needs_recovery flag before the fs is unlocked. */
Mingming Cao617ba132006-10-11 01:20:53 -07002305 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
2306 ext4_commit_super(sb, EXT4_SB(sb)->s_es, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002307 unlock_super(sb);
Mingming Caodab291a2006-10-11 01:21:01 -07002308 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002309 }
2310}
2311
Mingming Cao617ba132006-10-11 01:20:53 -07002312static int ext4_remount (struct super_block * sb, int * flags, char * data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002313{
Mingming Cao617ba132006-10-11 01:20:53 -07002314 struct ext4_super_block * es;
2315 struct ext4_sb_info *sbi = EXT4_SB(sb);
2316 ext4_fsblk_t n_blocks_count = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002317 unsigned long old_sb_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07002318 struct ext4_mount_options old_opts;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002319 int err;
2320#ifdef CONFIG_QUOTA
2321 int i;
2322#endif
2323
2324 /* Store the original options */
2325 old_sb_flags = sb->s_flags;
2326 old_opts.s_mount_opt = sbi->s_mount_opt;
2327 old_opts.s_resuid = sbi->s_resuid;
2328 old_opts.s_resgid = sbi->s_resgid;
2329 old_opts.s_commit_interval = sbi->s_commit_interval;
2330#ifdef CONFIG_QUOTA
2331 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2332 for (i = 0; i < MAXQUOTAS; i++)
2333 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
2334#endif
2335
2336 /*
2337 * Allow the "check" option to be passed as a remount option.
2338 */
2339 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
2340 err = -EINVAL;
2341 goto restore_opts;
2342 }
2343
Mingming Cao617ba132006-10-11 01:20:53 -07002344 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT)
2345 ext4_abort(sb, __FUNCTION__, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002346
2347 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Mingming Cao617ba132006-10-11 01:20:53 -07002348 ((sbi->s_mount_opt & EXT4_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002349
2350 es = sbi->s_es;
2351
Mingming Cao617ba132006-10-11 01:20:53 -07002352 ext4_init_journal_params(sb, sbi->s_journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002353
2354 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002355 n_blocks_count > ext4_blocks_count(es)) {
Mingming Cao617ba132006-10-11 01:20:53 -07002356 if (sbi->s_mount_opt & EXT4_MOUNT_ABORT) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002357 err = -EROFS;
2358 goto restore_opts;
2359 }
2360
2361 if (*flags & MS_RDONLY) {
2362 /*
2363 * First of all, the unconditional stuff we have to do
2364 * to disable replay of the journal when we next remount
2365 */
2366 sb->s_flags |= MS_RDONLY;
2367
2368 /*
2369 * OK, test if we are remounting a valid rw partition
2370 * readonly, and if so set the rdonly flag and then
2371 * mark the partition as valid again.
2372 */
Mingming Cao617ba132006-10-11 01:20:53 -07002373 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
2374 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002375 es->s_state = cpu_to_le16(sbi->s_mount_state);
2376
Mingming Cao617ba132006-10-11 01:20:53 -07002377 ext4_mark_recovery_complete(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002378 } else {
2379 __le32 ret;
Mingming Cao617ba132006-10-11 01:20:53 -07002380 if ((ret = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2381 ~EXT4_FEATURE_RO_COMPAT_SUPP))) {
2382 printk(KERN_WARNING "EXT4-fs: %s: couldn't "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002383 "remount RDWR because of unsupported "
2384 "optional features (%x).\n",
2385 sb->s_id, le32_to_cpu(ret));
2386 err = -EROFS;
2387 goto restore_opts;
2388 }
2389 /*
2390 * Mounting a RDONLY partition read-write, so reread
2391 * and store the current valid flag. (It may have
2392 * been changed by e2fsck since we originally mounted
2393 * the partition.)
2394 */
Mingming Cao617ba132006-10-11 01:20:53 -07002395 ext4_clear_journal_err(sb, es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002396 sbi->s_mount_state = le16_to_cpu(es->s_state);
Mingming Cao617ba132006-10-11 01:20:53 -07002397 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002398 goto restore_opts;
Mingming Cao617ba132006-10-11 01:20:53 -07002399 if (!ext4_setup_super (sb, es, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002400 sb->s_flags &= ~MS_RDONLY;
2401 }
2402 }
2403#ifdef CONFIG_QUOTA
2404 /* Release old quota file names */
2405 for (i = 0; i < MAXQUOTAS; i++)
2406 if (old_opts.s_qf_names[i] &&
2407 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2408 kfree(old_opts.s_qf_names[i]);
2409#endif
2410 return 0;
2411restore_opts:
2412 sb->s_flags = old_sb_flags;
2413 sbi->s_mount_opt = old_opts.s_mount_opt;
2414 sbi->s_resuid = old_opts.s_resuid;
2415 sbi->s_resgid = old_opts.s_resgid;
2416 sbi->s_commit_interval = old_opts.s_commit_interval;
2417#ifdef CONFIG_QUOTA
2418 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2419 for (i = 0; i < MAXQUOTAS; i++) {
2420 if (sbi->s_qf_names[i] &&
2421 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
2422 kfree(sbi->s_qf_names[i]);
2423 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2424 }
2425#endif
2426 return err;
2427}
2428
Mingming Cao617ba132006-10-11 01:20:53 -07002429static int ext4_statfs (struct dentry * dentry, struct kstatfs * buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002430{
2431 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07002432 struct ext4_sb_info *sbi = EXT4_SB(sb);
2433 struct ext4_super_block *es = sbi->s_es;
2434 ext4_fsblk_t overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002435 int i;
2436
2437 if (test_opt (sb, MINIX_DF))
2438 overhead = 0;
2439 else {
2440 unsigned long ngroups;
Mingming Cao617ba132006-10-11 01:20:53 -07002441 ngroups = EXT4_SB(sb)->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002442 smp_rmb();
2443
2444 /*
2445 * Compute the overhead (FS structures)
2446 */
2447
2448 /*
2449 * All of the blocks before first_data_block are
2450 * overhead
2451 */
2452 overhead = le32_to_cpu(es->s_first_data_block);
2453
2454 /*
2455 * Add the overhead attributed to the superblock and
2456 * block group descriptors. If the sparse superblocks
2457 * feature is turned on, then not all groups have this.
2458 */
2459 for (i = 0; i < ngroups; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002460 overhead += ext4_bg_has_super(sb, i) +
2461 ext4_bg_num_gdb(sb, i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002462 cond_resched();
2463 }
2464
2465 /*
2466 * Every block group has an inode bitmap, a block
2467 * bitmap, and an inode table.
2468 */
Mingming Cao617ba132006-10-11 01:20:53 -07002469 overhead += (ngroups * (2 + EXT4_SB(sb)->s_itb_per_group));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002470 }
2471
Mingming Cao617ba132006-10-11 01:20:53 -07002472 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002473 buf->f_bsize = sb->s_blocksize;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002474 buf->f_blocks = ext4_blocks_count(es) - overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002475 buf->f_bfree = percpu_counter_sum(&sbi->s_freeblocks_counter);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002476 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
2477 if (buf->f_bfree < ext4_r_blocks_count(es))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478 buf->f_bavail = 0;
2479 buf->f_files = le32_to_cpu(es->s_inodes_count);
2480 buf->f_ffree = percpu_counter_sum(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07002481 buf->f_namelen = EXT4_NAME_LEN;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482 return 0;
2483}
2484
2485/* Helper function for writing quotas on sync - we need to start transaction before quota file
2486 * is locked for write. Otherwise the are possible deadlocks:
2487 * Process 1 Process 2
Mingming Cao617ba132006-10-11 01:20:53 -07002488 * ext4_create() quota_sync()
Mingming Caodab291a2006-10-11 01:21:01 -07002489 * jbd2_journal_start() write_dquot()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002490 * DQUOT_INIT() down(dqio_mutex)
Mingming Caodab291a2006-10-11 01:21:01 -07002491 * down(dqio_mutex) jbd2_journal_start()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002492 *
2493 */
2494
2495#ifdef CONFIG_QUOTA
2496
2497static inline struct inode *dquot_to_inode(struct dquot *dquot)
2498{
2499 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
2500}
2501
Mingming Cao617ba132006-10-11 01:20:53 -07002502static int ext4_dquot_initialize(struct inode *inode, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002503{
2504 handle_t *handle;
2505 int ret, err;
2506
2507 /* We may create quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002508 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_INIT_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002509 if (IS_ERR(handle))
2510 return PTR_ERR(handle);
2511 ret = dquot_initialize(inode, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002512 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002513 if (!ret)
2514 ret = err;
2515 return ret;
2516}
2517
Mingming Cao617ba132006-10-11 01:20:53 -07002518static int ext4_dquot_drop(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002519{
2520 handle_t *handle;
2521 int ret, err;
2522
2523 /* We may delete quota structure so we need to reserve enough blocks */
Mingming Cao617ba132006-10-11 01:20:53 -07002524 handle = ext4_journal_start(inode, 2*EXT4_QUOTA_DEL_BLOCKS(inode->i_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002525 if (IS_ERR(handle))
2526 return PTR_ERR(handle);
2527 ret = dquot_drop(inode);
Mingming Cao617ba132006-10-11 01:20:53 -07002528 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002529 if (!ret)
2530 ret = err;
2531 return ret;
2532}
2533
Mingming Cao617ba132006-10-11 01:20:53 -07002534static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002535{
2536 int ret, err;
2537 handle_t *handle;
2538 struct inode *inode;
2539
2540 inode = dquot_to_inode(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002541 handle = ext4_journal_start(inode,
2542 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002543 if (IS_ERR(handle))
2544 return PTR_ERR(handle);
2545 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002546 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002547 if (!ret)
2548 ret = err;
2549 return ret;
2550}
2551
Mingming Cao617ba132006-10-11 01:20:53 -07002552static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002553{
2554 int ret, err;
2555 handle_t *handle;
2556
Mingming Cao617ba132006-10-11 01:20:53 -07002557 handle = ext4_journal_start(dquot_to_inode(dquot),
2558 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002559 if (IS_ERR(handle))
2560 return PTR_ERR(handle);
2561 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002562 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002563 if (!ret)
2564 ret = err;
2565 return ret;
2566}
2567
Mingming Cao617ba132006-10-11 01:20:53 -07002568static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002569{
2570 int ret, err;
2571 handle_t *handle;
2572
Mingming Cao617ba132006-10-11 01:20:53 -07002573 handle = ext4_journal_start(dquot_to_inode(dquot),
2574 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002575 if (IS_ERR(handle))
2576 return PTR_ERR(handle);
2577 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002578 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002579 if (!ret)
2580 ret = err;
2581 return ret;
2582}
2583
Mingming Cao617ba132006-10-11 01:20:53 -07002584static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002585{
2586 /* Are we journalling quotas? */
Mingming Cao617ba132006-10-11 01:20:53 -07002587 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2588 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002589 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07002590 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002591 } else {
2592 return dquot_mark_dquot_dirty(dquot);
2593 }
2594}
2595
Mingming Cao617ba132006-10-11 01:20:53 -07002596static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002597{
2598 int ret, err;
2599 handle_t *handle;
2600
2601 /* Data block + inode block */
Mingming Cao617ba132006-10-11 01:20:53 -07002602 handle = ext4_journal_start(sb->s_root->d_inode, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002603 if (IS_ERR(handle))
2604 return PTR_ERR(handle);
2605 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07002606 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002607 if (!ret)
2608 ret = err;
2609 return ret;
2610}
2611
2612/*
2613 * Turn on quotas during mount time - we need to find
2614 * the quota file and such...
2615 */
Mingming Cao617ba132006-10-11 01:20:53 -07002616static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002617{
Mingming Cao617ba132006-10-11 01:20:53 -07002618 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
2619 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002620}
2621
2622/*
2623 * Standard function to be called on quota_on
2624 */
Mingming Cao617ba132006-10-11 01:20:53 -07002625static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002626 char *path)
2627{
2628 int err;
2629 struct nameidata nd;
2630
2631 if (!test_opt(sb, QUOTA))
2632 return -EINVAL;
2633 /* Not journalling quota? */
Mingming Cao617ba132006-10-11 01:20:53 -07002634 if (!EXT4_SB(sb)->s_qf_names[USRQUOTA] &&
2635 !EXT4_SB(sb)->s_qf_names[GRPQUOTA])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002636 return vfs_quota_on(sb, type, format_id, path);
2637 err = path_lookup(path, LOOKUP_FOLLOW, &nd);
2638 if (err)
2639 return err;
2640 /* Quotafile not on the same filesystem? */
2641 if (nd.mnt->mnt_sb != sb) {
2642 path_release(&nd);
2643 return -EXDEV;
2644 }
2645 /* Quotafile not of fs root? */
2646 if (nd.dentry->d_parent->d_inode != sb->s_root->d_inode)
2647 printk(KERN_WARNING
Mingming Cao617ba132006-10-11 01:20:53 -07002648 "EXT4-fs: Quota file not on filesystem root. "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002649 "Journalled quota will not work.\n");
2650 path_release(&nd);
2651 return vfs_quota_on(sb, type, format_id, path);
2652}
2653
2654/* Read data from quotafile - avoid pagecache and such because we cannot afford
2655 * acquiring the locks... As quota files are never truncated and quota code
2656 * itself serializes the operations (and noone else should touch the files)
2657 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07002658static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002659 size_t len, loff_t off)
2660{
2661 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002662 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002663 int err = 0;
2664 int offset = off & (sb->s_blocksize - 1);
2665 int tocopy;
2666 size_t toread;
2667 struct buffer_head *bh;
2668 loff_t i_size = i_size_read(inode);
2669
2670 if (off > i_size)
2671 return 0;
2672 if (off+len > i_size)
2673 len = i_size-off;
2674 toread = len;
2675 while (toread > 0) {
2676 tocopy = sb->s_blocksize - offset < toread ?
2677 sb->s_blocksize - offset : toread;
Mingming Cao617ba132006-10-11 01:20:53 -07002678 bh = ext4_bread(NULL, inode, blk, 0, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002679 if (err)
2680 return err;
2681 if (!bh) /* A hole? */
2682 memset(data, 0, tocopy);
2683 else
2684 memcpy(data, bh->b_data+offset, tocopy);
2685 brelse(bh);
2686 offset = 0;
2687 toread -= tocopy;
2688 data += tocopy;
2689 blk++;
2690 }
2691 return len;
2692}
2693
2694/* Write to quotafile (we know the transaction is already started and has
2695 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07002696static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002697 const char *data, size_t len, loff_t off)
2698{
2699 struct inode *inode = sb_dqopt(sb)->files[type];
Mingming Cao617ba132006-10-11 01:20:53 -07002700 sector_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701 int err = 0;
2702 int offset = off & (sb->s_blocksize - 1);
2703 int tocopy;
Mingming Cao617ba132006-10-11 01:20:53 -07002704 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002705 size_t towrite = len;
2706 struct buffer_head *bh;
2707 handle_t *handle = journal_current_handle();
2708
2709 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
2710 while (towrite > 0) {
2711 tocopy = sb->s_blocksize - offset < towrite ?
2712 sb->s_blocksize - offset : towrite;
Mingming Cao617ba132006-10-11 01:20:53 -07002713 bh = ext4_bread(handle, inode, blk, 1, &err);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002714 if (!bh)
2715 goto out;
2716 if (journal_quota) {
Mingming Cao617ba132006-10-11 01:20:53 -07002717 err = ext4_journal_get_write_access(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002718 if (err) {
2719 brelse(bh);
2720 goto out;
2721 }
2722 }
2723 lock_buffer(bh);
2724 memcpy(bh->b_data+offset, data, tocopy);
2725 flush_dcache_page(bh->b_page);
2726 unlock_buffer(bh);
2727 if (journal_quota)
Mingming Cao617ba132006-10-11 01:20:53 -07002728 err = ext4_journal_dirty_metadata(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002729 else {
2730 /* Always do at least ordered writes for quotas */
Mingming Cao617ba132006-10-11 01:20:53 -07002731 err = ext4_journal_dirty_data(handle, bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002732 mark_buffer_dirty(bh);
2733 }
2734 brelse(bh);
2735 if (err)
2736 goto out;
2737 offset = 0;
2738 towrite -= tocopy;
2739 data += tocopy;
2740 blk++;
2741 }
2742out:
2743 if (len == towrite)
2744 return err;
2745 if (inode->i_size < off+len-towrite) {
2746 i_size_write(inode, off+len-towrite);
Mingming Cao617ba132006-10-11 01:20:53 -07002747 EXT4_I(inode)->i_disksize = inode->i_size;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748 }
2749 inode->i_version++;
2750 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
Mingming Cao617ba132006-10-11 01:20:53 -07002751 ext4_mark_inode_dirty(handle, inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002752 mutex_unlock(&inode->i_mutex);
2753 return len - towrite;
2754}
2755
2756#endif
2757
Mingming Cao617ba132006-10-11 01:20:53 -07002758static int ext4_get_sb(struct file_system_type *fs_type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002759 int flags, const char *dev_name, void *data, struct vfsmount *mnt)
2760{
Mingming Cao617ba132006-10-11 01:20:53 -07002761 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super, mnt);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002762}
2763
Mingming Cao617ba132006-10-11 01:20:53 -07002764static struct file_system_type ext4dev_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002765 .owner = THIS_MODULE,
Mingming Cao617ba132006-10-11 01:20:53 -07002766 .name = "ext4dev",
2767 .get_sb = ext4_get_sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002768 .kill_sb = kill_block_super,
2769 .fs_flags = FS_REQUIRES_DEV,
2770};
2771
Mingming Cao617ba132006-10-11 01:20:53 -07002772static int __init init_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002773{
Mingming Cao617ba132006-10-11 01:20:53 -07002774 int err = init_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002775 if (err)
2776 return err;
2777 err = init_inodecache();
2778 if (err)
2779 goto out1;
Mingming Cao617ba132006-10-11 01:20:53 -07002780 err = register_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002781 if (err)
2782 goto out;
2783 return 0;
2784out:
2785 destroy_inodecache();
2786out1:
Mingming Cao617ba132006-10-11 01:20:53 -07002787 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002788 return err;
2789}
2790
Mingming Cao617ba132006-10-11 01:20:53 -07002791static void __exit exit_ext4_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002792{
Mingming Cao617ba132006-10-11 01:20:53 -07002793 unregister_filesystem(&ext4dev_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002794 destroy_inodecache();
Mingming Cao617ba132006-10-11 01:20:53 -07002795 exit_ext4_xattr();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002796}
2797
2798MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Mingming Cao617ba132006-10-11 01:20:53 -07002799MODULE_DESCRIPTION("Fourth Extended Filesystem with extents");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002800MODULE_LICENSE("GPL");
Mingming Cao617ba132006-10-11 01:20:53 -07002801module_init(init_ext4_fs)
2802module_exit(exit_ext4_fs)