blob: c47f147507227fda66d6b1a59e6922ab60e36ff9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/ext3/super.c
3 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/blkdev.h>
21#include <linux/parser.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070022#include <linux/exportfs.h>
Al Viro4613ad12012-03-29 22:30:07 -040023#include <linux/statfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/random.h>
25#include <linux/mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/quotaops.h>
Mark Bellon8fc27512005-09-06 15:16:54 -070027#include <linux/seq_file.h>
vignesh babu3fc74262007-07-15 23:41:17 -070028#include <linux/log2.h>
Dan Magenheimerd71bc6d2011-05-26 10:01:49 -060029#include <linux/cleancache.h>
Ben Dooks381be252005-10-30 15:02:56 -080030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/uaccess.h>
Ben Dooks381be252005-10-30 15:02:56 -080032
Al Viro4613ad12012-03-29 22:30:07 -040033#define CREATE_TRACE_POINTS
34
35#include "ext3.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "xattr.h"
37#include "acl.h"
Ben Dooks381be252005-10-30 15:02:56 -080038#include "namei.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Linus Torvaldsbbae8bc2009-04-06 17:16:47 -070040#ifdef CONFIG_EXT3_DEFAULTS_TO_ORDERED
41 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_ORDERED_DATA
42#else
43 #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_WRITEBACK_DATA
44#endif
45
Johann Lombardi71b96252006-01-08 01:03:20 -080046static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
47 unsigned long journal_devnum);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
Eric Sandeeneee194e2006-09-27 01:49:30 -070049 unsigned int);
Takashi Satoc4be0c12009-01-09 16:40:58 -080050static int ext3_commit_super(struct super_block *sb,
51 struct ext3_super_block *es,
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 int sync);
53static void ext3_mark_recovery_complete(struct super_block * sb,
54 struct ext3_super_block * es);
55static void ext3_clear_journal_err(struct super_block * sb,
56 struct ext3_super_block * es);
57static int ext3_sync_fs(struct super_block *sb, int wait);
58static const char *ext3_decode_error(struct super_block * sb, int errno,
59 char nbuf[16]);
60static int ext3_remount (struct super_block * sb, int * flags, char * data);
David Howells726c3342006-06-23 02:02:58 -070061static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080062static int ext3_unfreeze(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080063static int ext3_freeze(struct super_block *sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Mingming Caoae6ddcc2006-09-27 01:49:27 -070065/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * Wrappers for journal_start/end.
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 */
68handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
69{
70 journal_t *journal;
71
72 if (sb->s_flags & MS_RDONLY)
73 return ERR_PTR(-EROFS);
74
75 /* Special case here: if the journal has aborted behind our
76 * backs (eg. EIO in the commit thread), then we still need to
77 * take the FS itself readonly cleanly. */
78 journal = EXT3_SB(sb)->s_journal;
79 if (is_journal_aborted(journal)) {
Harvey Harrisone05b6b52008-04-28 02:16:15 -070080 ext3_abort(sb, __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 "Detected aborted journal");
82 return ERR_PTR(-EROFS);
83 }
84
85 return journal_start(journal, nblocks);
86}
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088int __ext3_journal_stop(const char *where, handle_t *handle)
89{
90 struct super_block *sb;
91 int err;
92 int rc;
93
94 sb = handle->h_transaction->t_journal->j_private;
95 err = handle->h_err;
96 rc = journal_stop(handle);
97
98 if (!err)
99 err = rc;
100 if (err)
101 __ext3_std_error(sb, where, err);
102 return err;
103}
104
105void ext3_journal_abort_handle(const char *caller, const char *err_fn,
106 struct buffer_head *bh, handle_t *handle, int err)
107{
108 char nbuf[16];
109 const char *errstr = ext3_decode_error(NULL, err, nbuf);
110
111 if (bh)
112 BUFFER_TRACE(bh, "abort");
113
114 if (!handle->h_err)
115 handle->h_err = err;
116
117 if (is_handle_aborted(handle))
118 return;
119
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100120 printk(KERN_ERR "EXT3-fs: %s: aborting transaction: %s in %s\n",
121 caller, errstr, err_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 journal_abort_handle(handle);
124}
125
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100126void ext3_msg(struct super_block *sb, const char *prefix,
127 const char *fmt, ...)
128{
Joe Perches99fbb1e2010-11-09 10:18:05 -0800129 struct va_format vaf;
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100130 va_list args;
131
132 va_start(args, fmt);
Joe Perches99fbb1e2010-11-09 10:18:05 -0800133
134 vaf.fmt = fmt;
135 vaf.va = &args;
136
137 printk("%sEXT3-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
138
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100139 va_end(args);
140}
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Deal with the reporting of failure conditions on a filesystem such as
143 * inconsistencies detected or read IO failures.
144 *
145 * On ext2, we can store the error state of the filesystem in the
146 * superblock. That is not possible on ext3, because we may have other
147 * write ordering constraints on the superblock which prevent us from
148 * writing it out straight away; and given that the journal is about to
149 * be aborted, we can't rely on the current, or future, transactions to
150 * write out the superblock safely.
151 *
152 * We'll just use the journal_abort() error code to record an error in
Thadeu Lima de Souza Cascardod6b198b2010-01-17 19:10:07 -0200153 * the journal instead. On recovery, the journal will complain about
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * that error until we've noted it down and cleared it.
155 */
156
157static void ext3_handle_error(struct super_block *sb)
158{
159 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
160
161 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
162 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
163
164 if (sb->s_flags & MS_RDONLY)
165 return;
166
Vasily Averin7543fc72006-09-27 01:49:33 -0700167 if (!test_opt (sb, ERRORS_CONT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 journal_t *journal = EXT3_SB(sb)->s_journal;
169
Dmitry Monakhove3c96432010-02-02 16:05:51 +0300170 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (journal)
172 journal_abort(journal, -EIO);
173 }
Vasily Averin7543fc72006-09-27 01:49:33 -0700174 if (test_opt (sb, ERRORS_RO)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100175 ext3_msg(sb, KERN_CRIT,
176 "error: remounting filesystem read-only");
Dmitry Monakhov3df32192013-05-28 13:19:01 +0400177 /*
178 * Make sure updated value of ->s_mount_state will be visible
179 * before ->s_flags update.
180 */
181 smp_wmb();
Vasily Averin7543fc72006-09-27 01:49:33 -0700182 sb->s_flags |= MS_RDONLY;
183 }
184 ext3_commit_super(sb, es, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 if (test_opt(sb, ERRORS_PANIC))
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100186 panic("EXT3-fs (%s): panic forced after error\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 sb->s_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Joe Perches99fbb1e2010-11-09 10:18:05 -0800190void ext3_error(struct super_block *sb, const char *function,
191 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192{
Joe Perches99fbb1e2010-11-09 10:18:05 -0800193 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 va_list args;
195
196 va_start(args, fmt);
Joe Perches99fbb1e2010-11-09 10:18:05 -0800197
198 vaf.fmt = fmt;
199 vaf.va = &args;
200
201 printk(KERN_CRIT "EXT3-fs error (device %s): %s: %pV\n",
202 sb->s_id, function, &vaf);
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 va_end(args);
205
206 ext3_handle_error(sb);
207}
208
209static const char *ext3_decode_error(struct super_block * sb, int errno,
210 char nbuf[16])
211{
212 char *errstr = NULL;
213
214 switch (errno) {
215 case -EIO:
216 errstr = "IO failure";
217 break;
218 case -ENOMEM:
219 errstr = "Out of memory";
220 break;
221 case -EROFS:
222 if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
223 errstr = "Journal has aborted";
224 else
225 errstr = "Readonly filesystem";
226 break;
227 default:
228 /* If the caller passed in an extra buffer for unknown
229 * errors, textualise them now. Else we just return
230 * NULL. */
231 if (nbuf) {
232 /* Check for truncated error codes... */
233 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
234 errstr = nbuf;
235 }
236 break;
237 }
238
239 return errstr;
240}
241
242/* __ext3_std_error decodes expected errors from journaling functions
243 * automatically and invokes the appropriate error response. */
244
245void __ext3_std_error (struct super_block * sb, const char * function,
246 int errno)
247{
248 char nbuf[16];
Stephen Tweedie30121622005-05-18 11:47:17 -0400249 const char *errstr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Stephen Tweedie30121622005-05-18 11:47:17 -0400251 /* Special case: if the error is EROFS, and we're not already
252 * inside a transaction, then there's really no point in logging
253 * an error. */
254 if (errno == -EROFS && journal_current_handle() == NULL &&
255 (sb->s_flags & MS_RDONLY))
256 return;
257
258 errstr = ext3_decode_error(sb, errno, nbuf);
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100259 ext3_msg(sb, KERN_CRIT, "error in %s: %s", function, errstr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 ext3_handle_error(sb);
262}
263
264/*
265 * ext3_abort is a much stronger failure handler than ext3_error. The
266 * abort function may be used to deal with unrecoverable failures such
267 * as journal IO errors or ENOMEM at a critical moment in log management.
268 *
269 * We unconditionally force the filesystem into an ABORT|READONLY state,
270 * unless the error response on the fs has been set to panic in which
271 * case we take the easy way out and panic immediately.
272 */
273
Joe Perches99fbb1e2010-11-09 10:18:05 -0800274void ext3_abort(struct super_block *sb, const char *function,
275 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Joe Perches99fbb1e2010-11-09 10:18:05 -0800277 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 va_list args;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 va_start(args, fmt);
Joe Perches99fbb1e2010-11-09 10:18:05 -0800281
282 vaf.fmt = fmt;
283 vaf.va = &args;
284
285 printk(KERN_CRIT "EXT3-fs (%s): error: %s: %pV\n",
286 sb->s_id, function, &vaf);
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 va_end(args);
289
290 if (test_opt(sb, ERRORS_PANIC))
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100291 panic("EXT3-fs: panic from previous error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 if (sb->s_flags & MS_RDONLY)
294 return;
295
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100296 ext3_msg(sb, KERN_CRIT,
297 "error: remounting filesystem read-only");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
Dmitry Monakhove3c96432010-02-02 16:05:51 +0300299 set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
Dmitry Monakhov3df32192013-05-28 13:19:01 +0400300 /*
301 * Make sure updated value of ->s_mount_state will be visible
302 * before ->s_flags update.
303 */
304 smp_wmb();
305 sb->s_flags |= MS_RDONLY;
306
Hidehiro Kawai44d6f782008-10-27 22:51:46 -0400307 if (EXT3_SB(sb)->s_journal)
308 journal_abort(EXT3_SB(sb)->s_journal, -EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Joe Perches99fbb1e2010-11-09 10:18:05 -0800311void ext3_warning(struct super_block *sb, const char *function,
312 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
Joe Perches99fbb1e2010-11-09 10:18:05 -0800314 struct va_format vaf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 va_list args;
316
317 va_start(args, fmt);
Joe Perches99fbb1e2010-11-09 10:18:05 -0800318
319 vaf.fmt = fmt;
320 vaf.va = &args;
321
322 printk(KERN_WARNING "EXT3-fs (%s): warning: %s: %pV\n",
323 sb->s_id, function, &vaf);
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 va_end(args);
326}
327
328void ext3_update_dynamic_rev(struct super_block *sb)
329{
330 struct ext3_super_block *es = EXT3_SB(sb)->s_es;
331
332 if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
333 return;
334
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100335 ext3_msg(sb, KERN_WARNING,
336 "warning: updating to rev %d because of "
337 "new feature flag, running e2fsck is recommended",
338 EXT3_DYNAMIC_REV);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340 es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
341 es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
342 es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
343 /* leave es->s_feature_*compat flags alone */
344 /* es->s_uuid will be set by e2fsck if empty */
345
346 /*
347 * The rest of the superblock fields should be zero, and if not it
348 * means they are likely already in use, so leave them alone. We
349 * can leave it up to e2fsck to clean up any inconsistencies there.
350 */
351}
352
353/*
354 * Open the external journal device
355 */
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100356static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
358 struct block_device *bdev;
359 char b[BDEVNAME_SIZE];
360
Tejun Heod4d77622010-11-13 11:55:18 +0100361 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (IS_ERR(bdev))
363 goto fail;
364 return bdev;
365
366fail:
Lars-Peter Clausen8d0c2d12013-03-09 15:28:44 +0100367 ext3_msg(sb, KERN_ERR, "error: failed to open journal device %s: %ld",
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100368 __bdevname(dev, b), PTR_ERR(bdev));
369
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return NULL;
371}
372
373/*
374 * Release the journal device
375 */
Al Viro4385bab2013-05-05 22:11:03 -0400376static void ext3_blkdev_put(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Al Viro4385bab2013-05-05 22:11:03 -0400378 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
Al Viro4385bab2013-05-05 22:11:03 -0400381static void ext3_blkdev_remove(struct ext3_sb_info *sbi)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 bdev = sbi->journal_bdev;
385 if (bdev) {
Al Viro4385bab2013-05-05 22:11:03 -0400386 ext3_blkdev_put(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 sbi->journal_bdev = NULL;
388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391static inline struct inode *orphan_list_entry(struct list_head *l)
392{
393 return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
394}
395
396static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
397{
398 struct list_head *l;
399
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100400 ext3_msg(sb, KERN_ERR, "error: sb orphan head is %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 le32_to_cpu(sbi->s_es->s_last_orphan));
402
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100403 ext3_msg(sb, KERN_ERR, "sb_info orphan list:");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 list_for_each(l, &sbi->s_orphan) {
405 struct inode *inode = orphan_list_entry(l);
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100406 ext3_msg(sb, KERN_ERR, " "
Eric Sandeeneee194e2006-09-27 01:49:30 -0700407 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 inode->i_sb->s_id, inode->i_ino, inode,
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700409 inode->i_mode, inode->i_nlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 NEXT_ORPHAN(inode));
411 }
412}
413
414static void ext3_put_super (struct super_block * sb)
415{
416 struct ext3_sb_info *sbi = EXT3_SB(sb);
417 struct ext3_super_block *es = sbi->s_es;
Hidehiro Kawai44d6f782008-10-27 22:51:46 -0400418 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Christoph Hellwige0ccfd92010-05-19 07:16:42 -0400420 dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 ext3_xattr_put_super(sb);
Hidehiro Kawai44d6f782008-10-27 22:51:46 -0400422 err = journal_destroy(sbi->s_journal);
423 sbi->s_journal = NULL;
424 if (err < 0)
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -0700425 ext3_abort(sb, __func__, "Couldn't clean up the journal");
Hidehiro Kawai44d6f782008-10-27 22:51:46 -0400426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 if (!(sb->s_flags & MS_RDONLY)) {
428 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
429 es->s_state = cpu_to_le16(sbi->s_mount_state);
430 BUFFER_TRACE(sbi->s_sbh, "marking dirty");
431 mark_buffer_dirty(sbi->s_sbh);
432 ext3_commit_super(sb, es, 1);
433 }
434
435 for (i = 0; i < sbi->s_gdb_count; i++)
436 brelse(sbi->s_group_desc[i]);
437 kfree(sbi->s_group_desc);
438 percpu_counter_destroy(&sbi->s_freeblocks_counter);
439 percpu_counter_destroy(&sbi->s_freeinodes_counter);
440 percpu_counter_destroy(&sbi->s_dirs_counter);
441 brelse(sbi->s_sbh);
442#ifdef CONFIG_QUOTA
443 for (i = 0; i < MAXQUOTAS; i++)
444 kfree(sbi->s_qf_names[i]);
445#endif
446
447 /* Debugging code just in case the in-memory inode orphan list
448 * isn't empty. The on-disk one can be non-empty if we've
449 * detected an error and taken the fs readonly, but the
450 * in-memory list had better be clean by this point. */
451 if (!list_empty(&sbi->s_orphan))
452 dump_orphan_list(sb, sbi);
453 J_ASSERT(list_empty(&sbi->s_orphan));
454
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700455 invalidate_bdev(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
457 /*
458 * Invalidate the journal device's buffers. We don't want them
459 * floating about in memory - the physical journal device may
460 * hotswapped, and it breaks the `ro-after' testing code.
461 */
462 sync_blockdev(sbi->journal_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -0700463 invalidate_bdev(sbi->journal_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ext3_blkdev_remove(sbi);
465 }
466 sb->s_fs_info = NULL;
Pekka Enberg5df096d2009-01-07 18:07:25 -0800467 kfree(sbi->s_blockgroup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 kfree(sbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
Christoph Lametere18b8902006-12-06 20:33:20 -0800471static struct kmem_cache *ext3_inode_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473/*
474 * Called inside transaction, so use GFP_NOFS
475 */
476static struct inode *ext3_alloc_inode(struct super_block *sb)
477{
478 struct ext3_inode_info *ei;
479
Christoph Lametere6b4f8d2006-12-06 20:33:14 -0800480 ei = kmem_cache_alloc(ext3_inode_cachep, GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (!ei)
482 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ei->i_block_alloc_info = NULL;
484 ei->vfs_inode.i_version = 1;
Jan Karafe8bc912009-10-16 19:26:15 +0200485 atomic_set(&ei->i_datasync_tid, 0);
486 atomic_set(&ei->i_sync_tid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return &ei->vfs_inode;
488}
489
Lukas Czerner785c4bc2011-05-23 18:33:01 +0200490static int ext3_drop_inode(struct inode *inode)
491{
492 int drop = generic_drop_inode(inode);
493
494 trace_ext3_drop_inode(inode, drop);
495 return drop;
496}
497
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100498static void ext3_i_callback(struct rcu_head *head)
499{
500 struct inode *inode = container_of(head, struct inode, i_rcu);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100501 kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
502}
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504static void ext3_destroy_inode(struct inode *inode)
505{
Vasily Averin9f7dd932007-07-15 23:40:45 -0700506 if (!list_empty(&(EXT3_I(inode)->i_orphan))) {
507 printk("EXT3 Inode %p: orphan list check failed!\n",
508 EXT3_I(inode));
509 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
510 EXT3_I(inode), sizeof(struct ext3_inode_info),
511 false);
512 dump_stack();
513 }
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100514 call_rcu(&inode->i_rcu, ext3_i_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700517static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
520
Christoph Lametera35afb82007-05-16 22:10:57 -0700521 INIT_LIST_HEAD(&ei->i_orphan);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522#ifdef CONFIG_EXT3_FS_XATTR
Christoph Lametera35afb82007-05-16 22:10:57 -0700523 init_rwsem(&ei->xattr_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524#endif
Christoph Lametera35afb82007-05-16 22:10:57 -0700525 mutex_init(&ei->truncate_mutex);
526 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
Mingming Caoae6ddcc2006-09-27 01:49:27 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529static int init_inodecache(void)
530{
531 ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
532 sizeof(struct ext3_inode_info),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800533 0, (SLAB_RECLAIM_ACCOUNT|
534 SLAB_MEM_SPREAD),
Paul Mundt20c2df82007-07-20 10:11:58 +0900535 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (ext3_inode_cachep == NULL)
537 return -ENOMEM;
538 return 0;
539}
540
541static void destroy_inodecache(void)
542{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +1000543 /*
544 * Make sure all delayed rcu free inodes are flushed before we
545 * destroy cache.
546 */
547 rcu_barrier();
Alexey Dobriyan1a1d92c2006-09-27 01:49:40 -0700548 kmem_cache_destroy(ext3_inode_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549}
550
Peter Osterlund6cd37cd2005-10-28 20:23:39 +0200551static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
Mark Bellon8fc27512005-09-06 15:16:54 -0700552{
Peter Osterlund6cd37cd2005-10-28 20:23:39 +0200553#if defined(CONFIG_QUOTA)
OGAWA Hirofumi275abf52005-09-22 21:44:03 -0700554 struct ext3_sb_info *sbi = EXT3_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Jan Kara1aeec432009-11-30 22:22:41 +0100556 if (sbi->s_jquota_fmt) {
557 char *fmtname = "";
558
559 switch (sbi->s_jquota_fmt) {
560 case QFMT_VFS_OLD:
561 fmtname = "vfsold";
562 break;
563 case QFMT_VFS_V0:
564 fmtname = "vfsv0";
565 break;
566 case QFMT_VFS_V1:
567 fmtname = "vfsv1";
568 break;
569 }
570 seq_printf(seq, ",jqfmt=%s", fmtname);
571 }
Mark Bellon8fc27512005-09-06 15:16:54 -0700572
573 if (sbi->s_qf_names[USRQUOTA])
574 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
575
576 if (sbi->s_qf_names[GRPQUOTA])
577 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
578
Dmitry Monakhove3c96432010-02-02 16:05:51 +0300579 if (test_opt(sb, USRQUOTA))
Mark Bellon8fc27512005-09-06 15:16:54 -0700580 seq_puts(seq, ",usrquota");
581
Dmitry Monakhove3c96432010-02-02 16:05:51 +0300582 if (test_opt(sb, GRPQUOTA))
Mark Bellon8fc27512005-09-06 15:16:54 -0700583 seq_puts(seq, ",grpquota");
584#endif
Peter Osterlund6cd37cd2005-10-28 20:23:39 +0200585}
586
Jan Kara3c4cec62009-08-24 16:38:43 +0200587static char *data_mode_string(unsigned long mode)
588{
589 switch (mode) {
590 case EXT3_MOUNT_JOURNAL_DATA:
591 return "journal";
592 case EXT3_MOUNT_ORDERED_DATA:
593 return "ordered";
594 case EXT3_MOUNT_WRITEBACK_DATA:
595 return "writeback";
596 }
597 return "unknown";
598}
599
Miklos Szeredi571beed2007-10-16 23:26:26 -0700600/*
601 * Show an option if
602 * - it's set to a non-default value OR
603 * - if the per-sb default is different from the global default
604 */
Al Viro34c80b12011-12-08 21:32:45 -0500605static int ext3_show_options(struct seq_file *seq, struct dentry *root)
Peter Osterlund6cd37cd2005-10-28 20:23:39 +0200606{
Al Viro34c80b12011-12-08 21:32:45 -0500607 struct super_block *sb = root->d_sb;
Miklos Szeredi571beed2007-10-16 23:26:26 -0700608 struct ext3_sb_info *sbi = EXT3_SB(sb);
609 struct ext3_super_block *es = sbi->s_es;
610 unsigned long def_mount_opts;
611
612 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
613
614 if (sbi->s_sb_block != 1)
615 seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
616 if (test_opt(sb, MINIX_DF))
617 seq_puts(seq, ",minixdf");
618 if (test_opt(sb, GRPID))
619 seq_puts(seq, ",grpid");
620 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT3_DEFM_BSDGROUPS))
621 seq_puts(seq, ",nogrpid");
Eric W. Biederman15232992012-02-07 15:41:24 -0800622 if (!uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT3_DEF_RESUID)) ||
Miklos Szeredi571beed2007-10-16 23:26:26 -0700623 le16_to_cpu(es->s_def_resuid) != EXT3_DEF_RESUID) {
Eric W. Biederman15232992012-02-07 15:41:24 -0800624 seq_printf(seq, ",resuid=%u",
625 from_kuid_munged(&init_user_ns, sbi->s_resuid));
Miklos Szeredi571beed2007-10-16 23:26:26 -0700626 }
Eric W. Biederman15232992012-02-07 15:41:24 -0800627 if (!gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT3_DEF_RESGID)) ||
Miklos Szeredi571beed2007-10-16 23:26:26 -0700628 le16_to_cpu(es->s_def_resgid) != EXT3_DEF_RESGID) {
Eric W. Biederman15232992012-02-07 15:41:24 -0800629 seq_printf(seq, ",resgid=%u",
630 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Miklos Szeredi571beed2007-10-16 23:26:26 -0700631 }
Aneesh Kumar K.V1eca93f2008-02-06 01:40:14 -0800632 if (test_opt(sb, ERRORS_RO)) {
Miklos Szeredi571beed2007-10-16 23:26:26 -0700633 int def_errors = le16_to_cpu(es->s_errors);
634
635 if (def_errors == EXT3_ERRORS_PANIC ||
Aneesh Kumar K.V1eca93f2008-02-06 01:40:14 -0800636 def_errors == EXT3_ERRORS_CONTINUE) {
637 seq_puts(seq, ",errors=remount-ro");
Miklos Szeredi571beed2007-10-16 23:26:26 -0700638 }
639 }
Aneesh Kumar K.V1eca93f2008-02-06 01:40:14 -0800640 if (test_opt(sb, ERRORS_CONT))
641 seq_puts(seq, ",errors=continue");
Miklos Szeredi571beed2007-10-16 23:26:26 -0700642 if (test_opt(sb, ERRORS_PANIC))
643 seq_puts(seq, ",errors=panic");
644 if (test_opt(sb, NO_UID32))
645 seq_puts(seq, ",nouid32");
646 if (test_opt(sb, DEBUG))
647 seq_puts(seq, ",debug");
Miklos Szeredi571beed2007-10-16 23:26:26 -0700648#ifdef CONFIG_EXT3_FS_XATTR
649 if (test_opt(sb, XATTR_USER))
650 seq_puts(seq, ",user_xattr");
651 if (!test_opt(sb, XATTR_USER) &&
652 (def_mount_opts & EXT3_DEFM_XATTR_USER)) {
653 seq_puts(seq, ",nouser_xattr");
654 }
655#endif
656#ifdef CONFIG_EXT3_FS_POSIX_ACL
657 if (test_opt(sb, POSIX_ACL))
658 seq_puts(seq, ",acl");
659 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT3_DEFM_ACL))
660 seq_puts(seq, ",noacl");
661#endif
662 if (!test_opt(sb, RESERVATION))
663 seq_puts(seq, ",noreservation");
664 if (sbi->s_commit_interval) {
665 seq_printf(seq, ",commit=%u",
666 (unsigned) (sbi->s_commit_interval / HZ));
667 }
Eric Sandeen0636c732010-04-30 11:09:34 -0500668
669 /*
670 * Always display barrier state so it's clear what the status is.
671 */
672 seq_puts(seq, ",barrier=");
673 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
Dmitry Monakhove3c96432010-02-02 16:05:51 +0300674 seq_printf(seq, ",data=%s", data_mode_string(test_opt(sb, DATA_FLAGS)));
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -0700675 if (test_opt(sb, DATA_ERR_ABORT))
676 seq_puts(seq, ",data_err=abort");
677
Eric Sandeendee1d3b2009-11-16 16:50:49 -0600678 if (test_opt(sb, NOLOAD))
679 seq_puts(seq, ",norecovery");
680
Peter Osterlund6cd37cd2005-10-28 20:23:39 +0200681 ext3_show_quota_options(seq, sb);
Mark Bellon8fc27512005-09-06 15:16:54 -0700682
683 return 0;
684}
685
NeilBrownfdb36672006-09-16 12:15:38 -0700686
Christoph Hellwig74af0ba2007-10-21 16:42:07 -0700687static struct inode *ext3_nfs_get_inode(struct super_block *sb,
688 u64 ino, u32 generation)
NeilBrownfdb36672006-09-16 12:15:38 -0700689{
NeilBrownfdb36672006-09-16 12:15:38 -0700690 struct inode *inode;
NeilBrownfdb36672006-09-16 12:15:38 -0700691
692 if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
693 return ERR_PTR(-ESTALE);
694 if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
695 return ERR_PTR(-ESTALE);
696
697 /* iget isn't really right if the inode is currently unallocated!!
698 *
699 * ext3_read_inode will return a bad_inode if the inode had been
700 * deleted, so we should be safe.
701 *
702 * Currently we don't know the generation for parent directory, so
703 * a generation of 0 means "accept any"
704 */
David Howells473043d2008-02-07 00:15:36 -0800705 inode = ext3_iget(sb, ino);
706 if (IS_ERR(inode))
707 return ERR_CAST(inode);
708 if (generation && inode->i_generation != generation) {
NeilBrownfdb36672006-09-16 12:15:38 -0700709 iput(inode);
710 return ERR_PTR(-ESTALE);
711 }
Christoph Hellwig74af0ba2007-10-21 16:42:07 -0700712
713 return inode;
714}
715
716static struct dentry *ext3_fh_to_dentry(struct super_block *sb, struct fid *fid,
717 int fh_len, int fh_type)
718{
719 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
720 ext3_nfs_get_inode);
721}
722
723static struct dentry *ext3_fh_to_parent(struct super_block *sb, struct fid *fid,
724 int fh_len, int fh_type)
725{
726 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
727 ext3_nfs_get_inode);
NeilBrownfdb36672006-09-16 12:15:38 -0700728}
729
Toshiyuki Okajima6b082b52009-01-05 22:38:14 -0500730/*
731 * Try to release metadata pages (indirect blocks, directories) which are
732 * mapped via the block device. Since these pages could have journal heads
733 * which would prevent try_to_free_buffers() from freeing them, we must use
734 * jbd layer's try_to_free_buffers() function to release them.
735 */
736static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
737 gfp_t wait)
738{
739 journal_t *journal = EXT3_SB(sb)->s_journal;
740
741 WARN_ON(PageChecked(page));
742 if (!page_has_buffers(page))
743 return 0;
744 if (journal)
745 return journal_try_to_free_buffers(journal, page,
746 wait & ~__GFP_WAIT);
747 return try_to_free_buffers(page);
748}
749
Mark Bellon8fc27512005-09-06 15:16:54 -0700750#ifdef CONFIG_QUOTA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751#define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
752#define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754static int ext3_write_dquot(struct dquot *dquot);
755static int ext3_acquire_dquot(struct dquot *dquot);
756static int ext3_release_dquot(struct dquot *dquot);
757static int ext3_mark_dquot_dirty(struct dquot *dquot);
758static int ext3_write_info(struct super_block *sb, int type);
Jan Kara2fd83a42008-04-28 02:14:34 -0700759static int ext3_quota_on(struct super_block *sb, int type, int format_id,
Jan Karaf00c9e42010-09-15 17:38:58 +0200760 struct path *path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761static int ext3_quota_on_mount(struct super_block *sb, int type);
762static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
763 size_t len, loff_t off);
764static ssize_t ext3_quota_write(struct super_block *sb, int type,
765 const char *data, size_t len, loff_t off);
766
Alexey Dobriyan61e225d2009-09-21 17:01:08 -0700767static const struct dquot_operations ext3_quota_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 .write_dquot = ext3_write_dquot,
769 .acquire_dquot = ext3_acquire_dquot,
770 .release_dquot = ext3_release_dquot,
771 .mark_dirty = ext3_mark_dquot_dirty,
Jan Kara157091a2008-11-25 15:31:34 +0100772 .write_info = ext3_write_info,
773 .alloc_dquot = dquot_alloc,
774 .destroy_dquot = dquot_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775};
776
Alexey Dobriyan0d54b212009-09-21 17:01:09 -0700777static const struct quotactl_ops ext3_qctl_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 .quota_on = ext3_quota_on,
Christoph Hellwig287a8092010-05-19 07:16:45 -0400779 .quota_off = dquot_quota_off,
780 .quota_sync = dquot_quota_sync,
781 .get_info = dquot_get_dqinfo,
782 .set_info = dquot_set_dqinfo,
783 .get_dqblk = dquot_get_dqblk,
784 .set_dqblk = dquot_set_dqblk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785};
786#endif
787
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800788static const struct super_operations ext3_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 .alloc_inode = ext3_alloc_inode,
790 .destroy_inode = ext3_destroy_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 .write_inode = ext3_write_inode,
792 .dirty_inode = ext3_dirty_inode,
Lukas Czerner785c4bc2011-05-23 18:33:01 +0200793 .drop_inode = ext3_drop_inode,
Al Viroac14a952010-06-06 07:08:19 -0400794 .evict_inode = ext3_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .put_super = ext3_put_super,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 .sync_fs = ext3_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -0800797 .freeze_fs = ext3_freeze,
798 .unfreeze_fs = ext3_unfreeze,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 .statfs = ext3_statfs,
800 .remount_fs = ext3_remount,
Mark Bellon8fc27512005-09-06 15:16:54 -0700801 .show_options = ext3_show_options,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802#ifdef CONFIG_QUOTA
803 .quota_read = ext3_quota_read,
804 .quota_write = ext3_quota_write,
805#endif
Toshiyuki Okajima6b082b52009-01-05 22:38:14 -0500806 .bdev_try_to_free_page = bdev_try_to_free_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807};
808
Christoph Hellwig39655162007-10-21 16:42:17 -0700809static const struct export_operations ext3_export_ops = {
Christoph Hellwig74af0ba2007-10-21 16:42:07 -0700810 .fh_to_dentry = ext3_fh_to_dentry,
811 .fh_to_parent = ext3_fh_to_parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 .get_parent = ext3_get_parent,
813};
814
815enum {
816 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
817 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Adrian Bunk2860b732005-11-08 21:34:59 -0800818 Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Badari Pulavartyade1a292006-06-26 00:25:04 -0700820 Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
Johann Lombardi71b96252006-01-08 01:03:20 -0800821 Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -0700823 Opt_data_err_abort, Opt_data_err_ignore,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara1aeec432009-11-30 22:22:41 +0100825 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
Eric Sandeen0636c732010-04-30 11:09:34 -0500826 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
827 Opt_resize, Opt_usrquota, Opt_grpquota
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828};
829
Steven Whitehousea447c092008-10-13 10:46:57 +0100830static const match_table_t tokens = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 {Opt_bsd_df, "bsddf"},
832 {Opt_minix_df, "minixdf"},
833 {Opt_grpid, "grpid"},
834 {Opt_grpid, "bsdgroups"},
835 {Opt_nogrpid, "nogrpid"},
836 {Opt_nogrpid, "sysvgroups"},
837 {Opt_resgid, "resgid=%u"},
838 {Opt_resuid, "resuid=%u"},
839 {Opt_sb, "sb=%u"},
840 {Opt_err_cont, "errors=continue"},
841 {Opt_err_panic, "errors=panic"},
842 {Opt_err_ro, "errors=remount-ro"},
843 {Opt_nouid32, "nouid32"},
844 {Opt_nocheck, "nocheck"},
845 {Opt_nocheck, "check=none"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 {Opt_debug, "debug"},
847 {Opt_oldalloc, "oldalloc"},
848 {Opt_orlov, "orlov"},
849 {Opt_user_xattr, "user_xattr"},
850 {Opt_nouser_xattr, "nouser_xattr"},
851 {Opt_acl, "acl"},
852 {Opt_noacl, "noacl"},
853 {Opt_reservation, "reservation"},
854 {Opt_noreservation, "noreservation"},
855 {Opt_noload, "noload"},
Eric Sandeendee1d3b2009-11-16 16:50:49 -0600856 {Opt_noload, "norecovery"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 {Opt_nobh, "nobh"},
Badari Pulavartyade1a292006-06-26 00:25:04 -0700858 {Opt_bh, "bh"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 {Opt_commit, "commit=%u"},
860 {Opt_journal_update, "journal=update"},
861 {Opt_journal_inum, "journal=%u"},
Johann Lombardi71b96252006-01-08 01:03:20 -0800862 {Opt_journal_dev, "journal_dev=%u"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 {Opt_abort, "abort"},
864 {Opt_data_journal, "data=journal"},
865 {Opt_data_ordered, "data=ordered"},
866 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -0700867 {Opt_data_err_abort, "data_err=abort"},
868 {Opt_data_err_ignore, "data_err=ignore"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 {Opt_offusrjquota, "usrjquota="},
870 {Opt_usrjquota, "usrjquota=%s"},
871 {Opt_offgrpjquota, "grpjquota="},
872 {Opt_grpjquota, "grpjquota=%s"},
873 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
874 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara1aeec432009-11-30 22:22:41 +0100875 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Mark Bellon8fc27512005-09-06 15:16:54 -0700876 {Opt_grpquota, "grpquota"},
Jan Kara1f545872005-06-23 22:01:04 -0700877 {Opt_noquota, "noquota"},
878 {Opt_quota, "quota"},
Mark Bellon8fc27512005-09-06 15:16:54 -0700879 {Opt_usrquota, "usrquota"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 {Opt_barrier, "barrier=%u"},
Eric Sandeen0636c732010-04-30 11:09:34 -0500881 {Opt_barrier, "barrier"},
882 {Opt_nobarrier, "nobarrier"},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 {Opt_resize, "resize"},
Josef Bacik925872162008-03-04 14:29:43 -0800884 {Opt_err, NULL},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885};
886
Alexey Fisher4cf46b62009-11-22 20:38:55 +0100887static ext3_fsblk_t get_sb_block(void **data, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Dave Kleikampe9ad5622006-09-27 01:49:35 -0700889 ext3_fsblk_t sb_block;
890 char *options = (char *) *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 if (!options || strncmp(options, "sb=", 3) != 0)
893 return 1; /* Default location */
894 options += 3;
Mingming Cao43d23f92006-06-25 05:48:07 -0700895 /*todo: use simple_strtoll with >32bit ext3 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 sb_block = simple_strtoul(options, &options, 0);
897 if (*options && *options != ',') {
Lars-Peter Clausen8d0c2d12013-03-09 15:28:44 +0100898 ext3_msg(sb, KERN_ERR, "error: invalid sb specification: %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 (char *) *data);
900 return 1;
901 }
902 if (*options == ',')
903 options++;
904 *data = (void *) options;
905 return sb_block;
906}
907
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300908#ifdef CONFIG_QUOTA
909static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
910{
911 struct ext3_sb_info *sbi = EXT3_SB(sb);
912 char *qname;
913
914 if (sb_any_quota_loaded(sb) &&
915 !sbi->s_qf_names[qtype]) {
916 ext3_msg(sb, KERN_ERR,
917 "Cannot change journaled "
918 "quota options when quota turned on");
919 return 0;
920 }
921 qname = match_strdup(args);
922 if (!qname) {
923 ext3_msg(sb, KERN_ERR,
924 "Not enough memory for storing quotafile name");
925 return 0;
926 }
Jan Karaf56426a2012-12-31 12:38:36 +0100927 if (sbi->s_qf_names[qtype]) {
928 int same = !strcmp(sbi->s_qf_names[qtype], qname);
929
930 kfree(qname);
931 if (!same) {
932 ext3_msg(sb, KERN_ERR,
933 "%s quota file already specified",
934 QTYPE2NAME(qtype));
935 }
936 return same;
937 }
938 if (strchr(qname, '/')) {
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300939 ext3_msg(sb, KERN_ERR,
Jan Karaf56426a2012-12-31 12:38:36 +0100940 "quotafile must be on filesystem root");
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300941 kfree(qname);
942 return 0;
943 }
944 sbi->s_qf_names[qtype] = qname;
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300945 set_opt(sbi->s_mount_opt, QUOTA);
946 return 1;
947}
948
949static int clear_qf_name(struct super_block *sb, int qtype) {
950
951 struct ext3_sb_info *sbi = EXT3_SB(sb);
952
953 if (sb_any_quota_loaded(sb) &&
954 sbi->s_qf_names[qtype]) {
955 ext3_msg(sb, KERN_ERR, "Cannot change journaled quota options"
956 " when quota turned on");
957 return 0;
958 }
Jan Karaf56426a2012-12-31 12:38:36 +0100959 if (sbi->s_qf_names[qtype]) {
960 kfree(sbi->s_qf_names[qtype]);
961 sbi->s_qf_names[qtype] = NULL;
962 }
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300963 return 1;
964}
965#endif
966
Johann Lombardi71b96252006-01-08 01:03:20 -0800967static int parse_options (char *options, struct super_block *sb,
Eric Sandeeneee194e2006-09-27 01:49:30 -0700968 unsigned int *inum, unsigned long *journal_devnum,
Mingming Cao43d23f92006-06-25 05:48:07 -0700969 ext3_fsblk_t *n_blocks_count, int is_remount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct ext3_sb_info *sbi = EXT3_SB(sb);
972 char * p;
973 substring_t args[MAX_OPT_ARGS];
974 int data_opt = 0;
975 int option;
Eric W. Biederman15232992012-02-07 15:41:24 -0800976 kuid_t uid;
977 kgid_t gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978#ifdef CONFIG_QUOTA
Dmitry Monakhove1f5c672010-02-02 16:05:53 +0300979 int qfmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980#endif
981
982 if (!options)
983 return 1;
984
985 while ((p = strsep (&options, ",")) != NULL) {
986 int token;
987 if (!*p)
988 continue;
Eric Sandeen0636c732010-04-30 11:09:34 -0500989 /*
990 * Initialize args struct so we know whether arg was
991 * found; some options take optional arguments.
992 */
Sachin Kamat76f59b32012-09-04 13:42:00 +0530993 args[0].to = args[0].from = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 token = match_token(p, tokens, args);
995 switch (token) {
996 case Opt_bsd_df:
997 clear_opt (sbi->s_mount_opt, MINIX_DF);
998 break;
999 case Opt_minix_df:
1000 set_opt (sbi->s_mount_opt, MINIX_DF);
1001 break;
1002 case Opt_grpid:
1003 set_opt (sbi->s_mount_opt, GRPID);
1004 break;
1005 case Opt_nogrpid:
1006 clear_opt (sbi->s_mount_opt, GRPID);
1007 break;
1008 case Opt_resuid:
1009 if (match_int(&args[0], &option))
1010 return 0;
Eric W. Biederman15232992012-02-07 15:41:24 -08001011 uid = make_kuid(current_user_ns(), option);
1012 if (!uid_valid(uid)) {
1013 ext3_msg(sb, KERN_ERR, "Invalid uid value %d", option);
Zhao Hongjiang66415f62012-10-09 13:48:47 +08001014 return 0;
Eric W. Biederman15232992012-02-07 15:41:24 -08001015
1016 }
1017 sbi->s_resuid = uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 break;
1019 case Opt_resgid:
1020 if (match_int(&args[0], &option))
1021 return 0;
Eric W. Biederman15232992012-02-07 15:41:24 -08001022 gid = make_kgid(current_user_ns(), option);
1023 if (!gid_valid(gid)) {
1024 ext3_msg(sb, KERN_ERR, "Invalid gid value %d", option);
Zhao Hongjiang66415f62012-10-09 13:48:47 +08001025 return 0;
Eric W. Biederman15232992012-02-07 15:41:24 -08001026 }
1027 sbi->s_resgid = gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 break;
1029 case Opt_sb:
1030 /* handled by get_sb_block() instead of here */
1031 /* *sb_block = match_int(&args[0]); */
1032 break;
1033 case Opt_err_panic:
1034 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1035 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1036 set_opt (sbi->s_mount_opt, ERRORS_PANIC);
1037 break;
1038 case Opt_err_ro:
1039 clear_opt (sbi->s_mount_opt, ERRORS_CONT);
1040 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1041 set_opt (sbi->s_mount_opt, ERRORS_RO);
1042 break;
1043 case Opt_err_cont:
1044 clear_opt (sbi->s_mount_opt, ERRORS_RO);
1045 clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
1046 set_opt (sbi->s_mount_opt, ERRORS_CONT);
1047 break;
1048 case Opt_nouid32:
1049 set_opt (sbi->s_mount_opt, NO_UID32);
1050 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 case Opt_nocheck:
1052 clear_opt (sbi->s_mount_opt, CHECK);
1053 break;
1054 case Opt_debug:
1055 set_opt (sbi->s_mount_opt, DEBUG);
1056 break;
1057 case Opt_oldalloc:
Lukas Czernerfbc85402011-08-16 18:08:06 +02001058 ext3_msg(sb, KERN_WARNING,
1059 "Ignoring deprecated oldalloc option");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 break;
1061 case Opt_orlov:
Lukas Czernerfbc85402011-08-16 18:08:06 +02001062 ext3_msg(sb, KERN_WARNING,
1063 "Ignoring deprecated orlov option");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 break;
1065#ifdef CONFIG_EXT3_FS_XATTR
1066 case Opt_user_xattr:
1067 set_opt (sbi->s_mount_opt, XATTR_USER);
1068 break;
1069 case Opt_nouser_xattr:
1070 clear_opt (sbi->s_mount_opt, XATTR_USER);
1071 break;
1072#else
1073 case Opt_user_xattr:
1074 case Opt_nouser_xattr:
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001075 ext3_msg(sb, KERN_INFO,
1076 "(no)user_xattr options not supported");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 break;
1078#endif
1079#ifdef CONFIG_EXT3_FS_POSIX_ACL
1080 case Opt_acl:
1081 set_opt(sbi->s_mount_opt, POSIX_ACL);
1082 break;
1083 case Opt_noacl:
1084 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1085 break;
1086#else
1087 case Opt_acl:
1088 case Opt_noacl:
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001089 ext3_msg(sb, KERN_INFO,
1090 "(no)acl options not supported");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 break;
1092#endif
1093 case Opt_reservation:
1094 set_opt(sbi->s_mount_opt, RESERVATION);
1095 break;
1096 case Opt_noreservation:
1097 clear_opt(sbi->s_mount_opt, RESERVATION);
1098 break;
1099 case Opt_journal_update:
1100 /* @@@ FIXME */
1101 /* Eventually we will want to be able to create
1102 a journal file here. For now, only allow the
1103 user to specify an existing inode to be the
1104 journal file. */
1105 if (is_remount) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001106 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1107 "journal on remount");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109 }
1110 set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
1111 break;
1112 case Opt_journal_inum:
1113 if (is_remount) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001114 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1115 "journal on remount");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 return 0;
1117 }
1118 if (match_int(&args[0], &option))
1119 return 0;
1120 *inum = option;
1121 break;
Johann Lombardi71b96252006-01-08 01:03:20 -08001122 case Opt_journal_dev:
1123 if (is_remount) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001124 ext3_msg(sb, KERN_ERR, "error: cannot specify "
1125 "journal on remount");
Johann Lombardi71b96252006-01-08 01:03:20 -08001126 return 0;
1127 }
1128 if (match_int(&args[0], &option))
1129 return 0;
1130 *journal_devnum = option;
1131 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 case Opt_noload:
1133 set_opt (sbi->s_mount_opt, NOLOAD);
1134 break;
1135 case Opt_commit:
1136 if (match_int(&args[0], &option))
1137 return 0;
1138 if (option < 0)
1139 return 0;
1140 if (option == 0)
1141 option = JBD_DEFAULT_MAX_COMMIT_AGE;
1142 sbi->s_commit_interval = HZ * option;
1143 break;
1144 case Opt_data_journal:
1145 data_opt = EXT3_MOUNT_JOURNAL_DATA;
1146 goto datacheck;
1147 case Opt_data_ordered:
1148 data_opt = EXT3_MOUNT_ORDERED_DATA;
1149 goto datacheck;
1150 case Opt_data_writeback:
1151 data_opt = EXT3_MOUNT_WRITEBACK_DATA;
1152 datacheck:
1153 if (is_remount) {
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001154 if (test_opt(sb, DATA_FLAGS) == data_opt)
Jan Kara3c4cec62009-08-24 16:38:43 +02001155 break;
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001156 ext3_msg(sb, KERN_ERR,
1157 "error: cannot change "
Jan Kara3c4cec62009-08-24 16:38:43 +02001158 "data mode on remount. The filesystem "
1159 "is mounted in data=%s mode and you "
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001160 "try to remount it in data=%s mode.",
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001161 data_mode_string(test_opt(sb,
1162 DATA_FLAGS)),
Jan Kara3c4cec62009-08-24 16:38:43 +02001163 data_mode_string(data_opt));
1164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 } else {
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001166 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 sbi->s_mount_opt |= data_opt;
1168 }
1169 break;
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -07001170 case Opt_data_err_abort:
1171 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1172 break;
1173 case Opt_data_err_ignore:
1174 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1175 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176#ifdef CONFIG_QUOTA
1177 case Opt_usrjquota:
Dmitry Monakhove1f5c672010-02-02 16:05:53 +03001178 if (!set_qf_name(sb, USRQUOTA, &args[0]))
1179 return 0;
1180 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 case Opt_grpjquota:
Dmitry Monakhove1f5c672010-02-02 16:05:53 +03001182 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 break;
1185 case Opt_offusrjquota:
Dmitry Monakhove1f5c672010-02-02 16:05:53 +03001186 if (!clear_qf_name(sb, USRQUOTA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return 0;
Dmitry Monakhove1f5c672010-02-02 16:05:53 +03001188 break;
1189 case Opt_offgrpjquota:
1190 if (!clear_qf_name(sb, GRPQUOTA))
1191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 break;
1193 case Opt_jqfmt_vfsold:
Jan Karad06bf1d2008-07-25 01:46:18 -07001194 qfmt = QFMT_VFS_OLD;
1195 goto set_qf_format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 case Opt_jqfmt_vfsv0:
Jan Karad06bf1d2008-07-25 01:46:18 -07001197 qfmt = QFMT_VFS_V0;
Jan Kara1aeec432009-11-30 22:22:41 +01001198 goto set_qf_format;
1199 case Opt_jqfmt_vfsv1:
1200 qfmt = QFMT_VFS_V1;
Jan Karad06bf1d2008-07-25 01:46:18 -07001201set_qf_format:
Jan Karaee0d5ff2008-08-20 18:11:50 +02001202 if (sb_any_quota_loaded(sb) &&
Jan Karad06bf1d2008-07-25 01:46:18 -07001203 sbi->s_jquota_fmt != qfmt) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001204 ext3_msg(sb, KERN_ERR, "error: cannot change "
Jan Karad06bf1d2008-07-25 01:46:18 -07001205 "journaled quota options when "
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001206 "quota turned on.");
Jan Karad06bf1d2008-07-25 01:46:18 -07001207 return 0;
1208 }
1209 sbi->s_jquota_fmt = qfmt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 break;
Jan Kara1f545872005-06-23 22:01:04 -07001211 case Opt_quota:
Mark Bellon8fc27512005-09-06 15:16:54 -07001212 case Opt_usrquota:
Jan Kara1f545872005-06-23 22:01:04 -07001213 set_opt(sbi->s_mount_opt, QUOTA);
Mark Bellon8fc27512005-09-06 15:16:54 -07001214 set_opt(sbi->s_mount_opt, USRQUOTA);
1215 break;
1216 case Opt_grpquota:
1217 set_opt(sbi->s_mount_opt, QUOTA);
1218 set_opt(sbi->s_mount_opt, GRPQUOTA);
Jan Kara1f545872005-06-23 22:01:04 -07001219 break;
1220 case Opt_noquota:
Jan Karaee0d5ff2008-08-20 18:11:50 +02001221 if (sb_any_quota_loaded(sb)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001222 ext3_msg(sb, KERN_ERR, "error: cannot change "
1223 "quota options when quota turned on.");
Jan Kara1f545872005-06-23 22:01:04 -07001224 return 0;
1225 }
1226 clear_opt(sbi->s_mount_opt, QUOTA);
Mark Bellon8fc27512005-09-06 15:16:54 -07001227 clear_opt(sbi->s_mount_opt, USRQUOTA);
1228 clear_opt(sbi->s_mount_opt, GRPQUOTA);
Jan Kara1f545872005-06-23 22:01:04 -07001229 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230#else
Mark Bellon8fc27512005-09-06 15:16:54 -07001231 case Opt_quota:
1232 case Opt_usrquota:
1233 case Opt_grpquota:
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001234 ext3_msg(sb, KERN_ERR,
1235 "error: quota options not supported.");
Jan Karafa1ff1e2008-04-28 02:16:14 -07001236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 case Opt_usrjquota:
1238 case Opt_grpjquota:
1239 case Opt_offusrjquota:
1240 case Opt_offgrpjquota:
1241 case Opt_jqfmt_vfsold:
1242 case Opt_jqfmt_vfsv0:
Jan Kara1aeec432009-11-30 22:22:41 +01001243 case Opt_jqfmt_vfsv1:
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001244 ext3_msg(sb, KERN_ERR,
1245 "error: journaled quota options not "
1246 "supported.");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 break;
Jan Kara1f545872005-06-23 22:01:04 -07001248 case Opt_noquota:
1249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250#endif
1251 case Opt_abort:
1252 set_opt(sbi->s_mount_opt, ABORT);
1253 break;
Eric Sandeen0636c732010-04-30 11:09:34 -05001254 case Opt_nobarrier:
1255 clear_opt(sbi->s_mount_opt, BARRIER);
1256 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 case Opt_barrier:
Eric Sandeen0636c732010-04-30 11:09:34 -05001258 if (args[0].from) {
1259 if (match_int(&args[0], &option))
1260 return 0;
1261 } else
1262 option = 1; /* No argument, default to 1 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (option)
1264 set_opt(sbi->s_mount_opt, BARRIER);
1265 else
1266 clear_opt(sbi->s_mount_opt, BARRIER);
1267 break;
1268 case Opt_ignore:
1269 break;
1270 case Opt_resize:
Jan Kara08c6a962005-07-12 13:58:28 -07001271 if (!is_remount) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001272 ext3_msg(sb, KERN_ERR,
1273 "error: resize option only available "
1274 "for remount");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276 }
KAMBAROV, ZAURc7f17212005-06-28 20:45:11 -07001277 if (match_int(&args[0], &option) != 0)
1278 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 *n_blocks_count = option;
1280 break;
1281 case Opt_nobh:
Christoph Hellwig4c4d3902010-06-07 10:20:39 +02001282 ext3_msg(sb, KERN_WARNING,
1283 "warning: ignoring deprecated nobh option");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 break;
Badari Pulavartyade1a292006-06-26 00:25:04 -07001285 case Opt_bh:
Christoph Hellwig4c4d3902010-06-07 10:20:39 +02001286 ext3_msg(sb, KERN_WARNING,
1287 "warning: ignoring deprecated bh option");
Badari Pulavartyade1a292006-06-26 00:25:04 -07001288 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 default:
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001290 ext3_msg(sb, KERN_ERR,
1291 "error: unrecognized mount option \"%s\" "
1292 "or missing value", p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 return 0;
1294 }
1295 }
1296#ifdef CONFIG_QUOTA
Mark Bellon8fc27512005-09-06 15:16:54 -07001297 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001298 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
Mark Bellon8fc27512005-09-06 15:16:54 -07001299 clear_opt(sbi->s_mount_opt, USRQUOTA);
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001300 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
Mark Bellon8fc27512005-09-06 15:16:54 -07001301 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1302
Dmitry Monakhove1f5c672010-02-02 16:05:53 +03001303 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001304 ext3_msg(sb, KERN_ERR, "error: old and new quota "
1305 "format mixing.");
Mark Bellon8fc27512005-09-06 15:16:54 -07001306 return 0;
1307 }
1308
1309 if (!sbi->s_jquota_fmt) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001310 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
1311 "not specified.");
Mark Bellon8fc27512005-09-06 15:16:54 -07001312 return 0;
1313 }
1314 } else {
1315 if (sbi->s_jquota_fmt) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001316 ext3_msg(sb, KERN_ERR, "error: journaled quota format "
Jan Kara99aeaf62008-07-25 01:46:17 -07001317 "specified with no journaling "
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001318 "enabled.");
Mark Bellon8fc27512005-09-06 15:16:54 -07001319 return 0;
1320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 }
1322#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 1;
1324}
1325
1326static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
1327 int read_only)
1328{
1329 struct ext3_sb_info *sbi = EXT3_SB(sb);
1330 int res = 0;
1331
1332 if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001333 ext3_msg(sb, KERN_ERR,
1334 "error: revision level too high, "
1335 "forcing read-only mode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 res = MS_RDONLY;
1337 }
1338 if (read_only)
1339 return res;
1340 if (!(sbi->s_mount_state & EXT3_VALID_FS))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001341 ext3_msg(sb, KERN_WARNING,
1342 "warning: mounting unchecked fs, "
1343 "running e2fsck is recommended");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 else if ((sbi->s_mount_state & EXT3_ERROR_FS))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001345 ext3_msg(sb, KERN_WARNING,
1346 "warning: mounting fs with errors, "
1347 "running e2fsck is recommended");
Namhyung Kimdf0d6b82010-10-10 21:36:59 +09001348 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 le16_to_cpu(es->s_mnt_count) >=
Namhyung Kimdf0d6b82010-10-10 21:36:59 +09001350 le16_to_cpu(es->s_max_mnt_count))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001351 ext3_msg(sb, KERN_WARNING,
1352 "warning: maximal mount count reached, "
1353 "running e2fsck is recommended");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 else if (le32_to_cpu(es->s_checkinterval) &&
1355 (le32_to_cpu(es->s_lastcheck) +
1356 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001357 ext3_msg(sb, KERN_WARNING,
1358 "warning: checktime reached, "
1359 "running e2fsck is recommended");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360#if 0
1361 /* @@@ We _will_ want to clear the valid bit if we find
1362 inconsistencies, to force a fsck at reboot. But for
1363 a plain journaled filesystem we can keep it set as
1364 valid forever! :) */
Marcin Slusarze7f23eb2008-04-28 02:16:06 -07001365 es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366#endif
Namhyung Kimdf0d6b82010-10-10 21:36:59 +09001367 if (!le16_to_cpu(es->s_max_mnt_count))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
Marcin Slusarz50e8a282008-02-08 04:20:13 -08001369 le16_add_cpu(&es->s_mnt_count, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 es->s_mtime = cpu_to_le32(get_seconds());
1371 ext3_update_dynamic_rev(sb);
1372 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
1373
1374 ext3_commit_super(sb, es, 1);
1375 if (test_opt(sb, DEBUG))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001376 ext3_msg(sb, KERN_INFO, "[bs=%lu, gc=%lu, "
1377 "bpg=%lu, ipg=%lu, mo=%04lx]",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 sb->s_blocksize,
1379 sbi->s_groups_count,
1380 EXT3_BLOCKS_PER_GROUP(sb),
1381 EXT3_INODES_PER_GROUP(sb),
1382 sbi->s_mount_opt);
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
1385 char b[BDEVNAME_SIZE];
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001386 ext3_msg(sb, KERN_INFO, "using external journal on %s",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
1388 } else {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001389 ext3_msg(sb, KERN_INFO, "using internal journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Dan Magenheimerd71bc6d2011-05-26 10:01:49 -06001391 cleancache_init_fs(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return res;
1393}
1394
1395/* Called at mount-time, super-block is locked */
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001396static int ext3_check_descriptors(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
1398 struct ext3_sb_info *sbi = EXT3_SB(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 int i;
1400
1401 ext3_debug ("Checking group descriptors");
1402
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001403 for (i = 0; i < sbi->s_groups_count; i++) {
1404 struct ext3_group_desc *gdp = ext3_get_group_desc(sb, i, NULL);
Akinobu Mita1eaafea2008-04-28 02:16:07 -07001405 ext3_fsblk_t first_block = ext3_group_first_block_no(sb, i);
1406 ext3_fsblk_t last_block;
Akinobu Mita197cd65a2008-02-06 01:40:16 -08001407
Eric Sandeen855565e2006-09-27 01:49:29 -07001408 if (i == sbi->s_groups_count - 1)
1409 last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
1410 else
1411 last_block = first_block +
1412 (EXT3_BLOCKS_PER_GROUP(sb) - 1);
1413
Eric Sandeen855565e2006-09-27 01:49:29 -07001414 if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
1415 le32_to_cpu(gdp->bg_block_bitmap) > last_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 {
1417 ext3_error (sb, "ext3_check_descriptors",
1418 "Block bitmap for group %d"
1419 " not in group (block %lu)!",
1420 i, (unsigned long)
1421 le32_to_cpu(gdp->bg_block_bitmap));
1422 return 0;
1423 }
Eric Sandeen855565e2006-09-27 01:49:29 -07001424 if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
1425 le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 {
1427 ext3_error (sb, "ext3_check_descriptors",
1428 "Inode bitmap for group %d"
1429 " not in group (block %lu)!",
1430 i, (unsigned long)
1431 le32_to_cpu(gdp->bg_inode_bitmap));
1432 return 0;
1433 }
Eric Sandeen855565e2006-09-27 01:49:29 -07001434 if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
Eric Sandeen780dcdb2007-07-26 10:41:11 -07001435 le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
Eric Sandeen855565e2006-09-27 01:49:29 -07001436 last_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 {
1438 ext3_error (sb, "ext3_check_descriptors",
1439 "Inode table for group %d"
1440 " not in group (block %lu)!",
1441 i, (unsigned long)
1442 le32_to_cpu(gdp->bg_inode_table));
1443 return 0;
1444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 }
1446
1447 sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
1448 sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
1449 return 1;
1450}
1451
1452
1453/* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
1454 * the superblock) which were deleted from all directories, but held open by
1455 * a process at the time of a crash. We walk the list and try to delete these
1456 * inodes at recovery time (only with a read-write filesystem).
1457 *
1458 * In order to keep the orphan inode chain consistent during traversal (in
1459 * case of crash during recovery), we link each inode into the superblock
1460 * orphan list_head and handle it the same way as an inode deletion during
1461 * normal operation (which journals the operations for us).
1462 *
1463 * We only do an iget() and an iput() on each inode, which is very safe if we
1464 * accidentally point at an in-use or already deleted inode. The worst that
1465 * can happen in this case is that we get a "bit already cleared" message from
1466 * ext3_free_inode(). The only reason we would point at a wrong inode is if
1467 * e2fsck was run on this filesystem, and it must have already done the orphan
1468 * inode cleanup for us, so we can safely abort without any further action.
1469 */
1470static void ext3_orphan_cleanup (struct super_block * sb,
1471 struct ext3_super_block * es)
1472{
1473 unsigned int s_flags = sb->s_flags;
1474 int nr_orphans = 0, nr_truncates = 0;
1475#ifdef CONFIG_QUOTA
1476 int i;
1477#endif
1478 if (!es->s_last_orphan) {
1479 jbd_debug(4, "no orphan inodes to clean up\n");
1480 return;
1481 }
1482
Eric Sandeena8f48a92006-12-06 20:40:13 -08001483 if (bdev_read_only(sb->s_bdev)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001484 ext3_msg(sb, KERN_ERR, "error: write access "
1485 "unavailable, skipping orphan cleanup.");
Eric Sandeena8f48a92006-12-06 20:40:13 -08001486 return;
1487 }
1488
Amir Goldsteince654b32011-02-26 22:40:19 +02001489 /* Check if feature set allows readwrite operations */
1490 if (EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP)) {
1491 ext3_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
1492 "unknown ROCOMPAT features");
1493 return;
1494 }
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
Eric Sandeene4230102012-08-27 14:30:40 -05001497 /* don't clear list on RO mount w/ errors */
1498 if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 jbd_debug(1, "Errors on filesystem, "
1500 "clearing orphan list.\n");
Eric Sandeene4230102012-08-27 14:30:40 -05001501 es->s_last_orphan = 0;
1502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1504 return;
1505 }
1506
1507 if (s_flags & MS_RDONLY) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001508 ext3_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 sb->s_flags &= ~MS_RDONLY;
1510 }
1511#ifdef CONFIG_QUOTA
1512 /* Needed for iput() to work correctly and not trash data */
1513 sb->s_flags |= MS_ACTIVE;
1514 /* Turn on quotas so that they are updated correctly */
1515 for (i = 0; i < MAXQUOTAS; i++) {
1516 if (EXT3_SB(sb)->s_qf_names[i]) {
1517 int ret = ext3_quota_on_mount(sb, i);
1518 if (ret < 0)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001519 ext3_msg(sb, KERN_ERR,
1520 "error: cannot turn on journaled "
1521 "quota: %d", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 }
1524#endif
1525
1526 while (es->s_last_orphan) {
1527 struct inode *inode;
1528
David Howells473043d2008-02-07 00:15:36 -08001529 inode = ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
1530 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 es->s_last_orphan = 0;
1532 break;
1533 }
1534
1535 list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
Christoph Hellwig871a2932010-03-03 09:05:07 -05001536 dquot_initialize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (inode->i_nlink) {
1538 printk(KERN_DEBUG
Eric Sandeeneee194e2006-09-27 01:49:30 -07001539 "%s: truncating inode %lu to %Ld bytes\n",
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001540 __func__, inode->i_ino, inode->i_size);
Eric Sandeeneee194e2006-09-27 01:49:30 -07001541 jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 inode->i_ino, inode->i_size);
1543 ext3_truncate(inode);
1544 nr_truncates++;
1545 } else {
1546 printk(KERN_DEBUG
Eric Sandeeneee194e2006-09-27 01:49:30 -07001547 "%s: deleting unreferenced inode %lu\n",
Harvey Harrisone05b6b52008-04-28 02:16:15 -07001548 __func__, inode->i_ino);
Eric Sandeeneee194e2006-09-27 01:49:30 -07001549 jbd_debug(2, "deleting unreferenced inode %lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 inode->i_ino);
1551 nr_orphans++;
1552 }
1553 iput(inode); /* The delete magic happens here! */
1554 }
1555
1556#define PLURAL(x) (x), ((x)==1) ? "" : "s"
1557
1558 if (nr_orphans)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001559 ext3_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
1560 PLURAL(nr_orphans));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (nr_truncates)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001562 ext3_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
1563 PLURAL(nr_truncates));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564#ifdef CONFIG_QUOTA
1565 /* Turn quotas off */
1566 for (i = 0; i < MAXQUOTAS; i++) {
1567 if (sb_dqopt(sb)->files[i])
Christoph Hellwig287a8092010-05-19 07:16:45 -04001568 dquot_quota_off(sb, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570#endif
1571 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
1572}
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574/*
1575 * Maximal file size. There is a direct, and {,double-,triple-}indirect
1576 * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
1577 * We need to be 1 filesystem block less than the 2^32 sector limit.
1578 */
1579static loff_t ext3_max_size(int bits)
1580{
1581 loff_t res = EXT3_NDIR_BLOCKS;
Aneesh Kumar K.Vfe7fdc32008-01-28 23:58:26 -05001582 int meta_blocks;
1583 loff_t upper_limit;
1584
1585 /* This is calculated to be the largest file size for a
1586 * dense, file such that the total number of
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 * sectors in the file, including data and all indirect blocks,
Aneesh Kumar K.Vfe7fdc32008-01-28 23:58:26 -05001588 * does not exceed 2^32 -1
1589 * __u32 i_blocks representing the total number of
1590 * 512 bytes blocks of the file
1591 */
1592 upper_limit = (1LL << 32) - 1;
1593
1594 /* total blocks in file system block size */
1595 upper_limit >>= (bits - 9);
1596
1597
1598 /* indirect blocks */
1599 meta_blocks = 1;
1600 /* double indirect blocks */
1601 meta_blocks += 1 + (1LL << (bits-2));
1602 /* tripple indirect blocks */
1603 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
1604
1605 upper_limit -= meta_blocks;
1606 upper_limit <<= bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
1608 res += 1LL << (bits-2);
1609 res += 1LL << (2*(bits-2));
1610 res += 1LL << (3*(bits-2));
1611 res <<= bits;
1612 if (res > upper_limit)
1613 res = upper_limit;
Aneesh Kumar K.Vfe7fdc32008-01-28 23:58:26 -05001614
1615 if (res > MAX_LFS_FILESIZE)
1616 res = MAX_LFS_FILESIZE;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 return res;
1619}
1620
Mingming Cao43d23f92006-06-25 05:48:07 -07001621static ext3_fsblk_t descriptor_loc(struct super_block *sb,
1622 ext3_fsblk_t logic_sb_block,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 int nr)
1624{
1625 struct ext3_sb_info *sbi = EXT3_SB(sb);
Mingming Cao43d23f92006-06-25 05:48:07 -07001626 unsigned long bg, first_meta_bg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 int has_super = 0;
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
1630
1631 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
1632 nr < first_meta_bg)
1633 return (logic_sb_block + nr + 1);
1634 bg = sbi->s_desc_per_block * nr;
1635 if (ext3_bg_has_super(sb, bg))
1636 has_super = 1;
Mingming Cao43d23f92006-06-25 05:48:07 -07001637 return (has_super + ext3_group_first_block_no(sb, bg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
1640
1641static int ext3_fill_super (struct super_block *sb, void *data, int silent)
1642{
1643 struct buffer_head * bh;
1644 struct ext3_super_block *es = NULL;
1645 struct ext3_sb_info *sbi;
Mingming Cao43d23f92006-06-25 05:48:07 -07001646 ext3_fsblk_t block;
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001647 ext3_fsblk_t sb_block = get_sb_block(&data, sb);
Mingming Cao43d23f92006-06-25 05:48:07 -07001648 ext3_fsblk_t logic_sb_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 unsigned long offset = 0;
Eric Sandeeneee194e2006-09-27 01:49:30 -07001650 unsigned int journal_inum = 0;
Johann Lombardi71b96252006-01-08 01:03:20 -08001651 unsigned long journal_devnum = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 unsigned long def_mount_opts;
1653 struct inode *root;
1654 int blocksize;
1655 int hblock;
1656 int db_count;
1657 int i;
1658 int needs_recovery;
David Howells473043d2008-02-07 00:15:36 -08001659 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 __le32 features;
Peter Zijlstra833f4072007-10-16 23:25:45 -07001661 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Panagiotis Issarisf8314dc2006-09-27 01:49:37 -07001663 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (!sbi)
1665 return -ENOMEM;
Pekka Enberg5df096d2009-01-07 18:07:25 -08001666
1667 sbi->s_blockgroup_lock =
1668 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
1669 if (!sbi->s_blockgroup_lock) {
1670 kfree(sbi);
1671 return -ENOMEM;
1672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 sb->s_fs_info = sbi;
Miklos Szeredi571beed2007-10-16 23:26:26 -07001674 sbi->s_sb_block = sb_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
1677 if (!blocksize) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001678 ext3_msg(sb, KERN_ERR, "error: unable to set blocksize");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 goto out_fail;
1680 }
1681
1682 /*
1683 * The ext3 superblock will not be buffer aligned for other than 1kB
1684 * block sizes. We need to calculate the offset from buffer start.
1685 */
1686 if (blocksize != EXT3_MIN_BLOCK_SIZE) {
1687 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1688 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1689 } else {
1690 logic_sb_block = sb_block;
1691 }
1692
1693 if (!(bh = sb_bread(sb, logic_sb_block))) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001694 ext3_msg(sb, KERN_ERR, "error: unable to read superblock");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 goto out_fail;
1696 }
1697 /*
1698 * Note: s_es must be initialized as soon as possible because
1699 * some ext3 macro-instructions depend on its value
1700 */
Namhyung Kim57e94d82010-10-11 02:10:45 +09001701 es = (struct ext3_super_block *) (bh->b_data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 sbi->s_es = es;
1703 sb->s_magic = le16_to_cpu(es->s_magic);
1704 if (sb->s_magic != EXT3_SUPER_MAGIC)
1705 goto cantfind_ext3;
1706
1707 /* Set defaults before we parse the mount options */
1708 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
1709 if (def_mount_opts & EXT3_DEFM_DEBUG)
1710 set_opt(sbi->s_mount_opt, DEBUG);
1711 if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
1712 set_opt(sbi->s_mount_opt, GRPID);
1713 if (def_mount_opts & EXT3_DEFM_UID16)
1714 set_opt(sbi->s_mount_opt, NO_UID32);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001715#ifdef CONFIG_EXT3_FS_XATTR
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (def_mount_opts & EXT3_DEFM_XATTR_USER)
1717 set_opt(sbi->s_mount_opt, XATTR_USER);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001718#endif
1719#ifdef CONFIG_EXT3_FS_POSIX_ACL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (def_mount_opts & EXT3_DEFM_ACL)
1721 set_opt(sbi->s_mount_opt, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08001722#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001724 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001726 set_opt(sbi->s_mount_opt, ORDERED_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001728 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
1731 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
Aneesh Kumar K.V1eca93f2008-02-06 01:40:14 -08001732 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_CONTINUE)
Dmitry Mishin2245d7c2006-10-11 01:21:49 -07001733 set_opt(sbi->s_mount_opt, ERRORS_CONT);
Aneesh Kumar K.V1eca93f2008-02-06 01:40:14 -08001734 else
1735 set_opt(sbi->s_mount_opt, ERRORS_RO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Eric W. Biederman15232992012-02-07 15:41:24 -08001737 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
1738 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
Christoph Hellwig00eacd62011-07-16 16:46:50 -04001740 /* enable barriers by default */
1741 set_opt(sbi->s_mount_opt, BARRIER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 set_opt(sbi->s_mount_opt, RESERVATION);
1743
Johann Lombardi71b96252006-01-08 01:03:20 -08001744 if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
1745 NULL, 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 goto failed_mount;
1747
1748 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhove3c96432010-02-02 16:05:51 +03001749 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
1751 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
1752 (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
1753 EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
1754 EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001755 ext3_msg(sb, KERN_WARNING,
1756 "warning: feature flags set on rev 0 fs, "
1757 "running e2fsck is recommended");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 /*
1759 * Check feature flags regardless of the revision level, since we
1760 * previously didn't change the revision level when setting the flags,
1761 * so there is a chance incompat flags are set on a rev 0 filesystem.
1762 */
1763 features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
1764 if (features) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001765 ext3_msg(sb, KERN_ERR,
1766 "error: couldn't mount because of unsupported "
1767 "optional features (%x)", le32_to_cpu(features));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 goto failed_mount;
1769 }
1770 features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
1771 if (!(sb->s_flags & MS_RDONLY) && features) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001772 ext3_msg(sb, KERN_ERR,
1773 "error: couldn't mount RDWR because of unsupported "
1774 "optional features (%x)", le32_to_cpu(features));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 goto failed_mount;
1776 }
1777 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
1778
1779 if (blocksize < EXT3_MIN_BLOCK_SIZE ||
1780 blocksize > EXT3_MAX_BLOCK_SIZE) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001781 ext3_msg(sb, KERN_ERR,
1782 "error: couldn't mount because of unsupported "
1783 "filesystem blocksize %d", blocksize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 goto failed_mount;
1785 }
1786
Martin K. Petersene1defc42009-05-22 17:17:49 -04001787 hblock = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 if (sb->s_blocksize != blocksize) {
1789 /*
1790 * Make sure the blocksize for the filesystem is larger
1791 * than the hardware sectorsize for the machine.
1792 */
1793 if (blocksize < hblock) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001794 ext3_msg(sb, KERN_ERR,
1795 "error: fsblocksize %d too small for "
1796 "hardware sectorsize %d", blocksize, hblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 goto failed_mount;
1798 }
1799
1800 brelse (bh);
Takashi Sato0f0a89e2007-10-18 03:06:56 -07001801 if (!sb_set_blocksize(sb, blocksize)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001802 ext3_msg(sb, KERN_ERR,
1803 "error: bad blocksize %d", blocksize);
Takashi Sato0f0a89e2007-10-18 03:06:56 -07001804 goto out_fail;
1805 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
1807 offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
1808 bh = sb_bread(sb, logic_sb_block);
1809 if (!bh) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001810 ext3_msg(sb, KERN_ERR,
1811 "error: can't read superblock on 2nd try");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 goto failed_mount;
1813 }
Namhyung Kim57e94d82010-10-11 02:10:45 +09001814 es = (struct ext3_super_block *)(bh->b_data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 sbi->s_es = es;
1816 if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001817 ext3_msg(sb, KERN_ERR,
1818 "error: magic mismatch");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 goto failed_mount;
1820 }
1821 }
1822
1823 sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
1824
1825 if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
1826 sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
1827 sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
1828 } else {
1829 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
1830 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
1831 if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
vignesh babu3fc74262007-07-15 23:41:17 -07001832 (!is_power_of_2(sbi->s_inode_size)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 (sbi->s_inode_size > blocksize)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001834 ext3_msg(sb, KERN_ERR,
1835 "error: unsupported inode size: %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 sbi->s_inode_size);
1837 goto failed_mount;
1838 }
1839 }
1840 sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
1841 le32_to_cpu(es->s_log_frag_size);
1842 if (blocksize != sbi->s_frag_size) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001843 ext3_msg(sb, KERN_ERR,
1844 "error: fragsize %lu != blocksize %u (unsupported)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 sbi->s_frag_size, blocksize);
1846 goto failed_mount;
1847 }
1848 sbi->s_frags_per_block = 1;
1849 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
1850 sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
1851 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andries E. Brouwerb47b6f32007-12-17 16:19:55 -08001852 if (EXT3_INODE_SIZE(sb) == 0 || EXT3_INODES_PER_GROUP(sb) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto cantfind_ext3;
1854 sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
1855 if (sbi->s_inodes_per_block == 0)
1856 goto cantfind_ext3;
1857 sbi->s_itb_per_group = sbi->s_inodes_per_group /
1858 sbi->s_inodes_per_block;
1859 sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
1860 sbi->s_sbh = bh;
1861 sbi->s_mount_state = le16_to_cpu(es->s_state);
David Howellsf0d1b0b2006-12-08 02:37:49 -08001862 sbi->s_addr_per_block_bits = ilog2(EXT3_ADDR_PER_BLOCK(sb));
1863 sbi->s_desc_per_block_bits = ilog2(EXT3_DESC_PER_BLOCK(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 for (i=0; i < 4; i++)
1865 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
1866 sbi->s_def_hash_version = es->s_def_hash_version;
Theodore Ts'o5e1f8c92008-10-28 13:21:55 -04001867 i = le32_to_cpu(es->s_flags);
1868 if (i & EXT2_FLAGS_UNSIGNED_HASH)
1869 sbi->s_hash_unsigned = 3;
1870 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
1871#ifdef __CHAR_UNSIGNED__
1872 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
1873 sbi->s_hash_unsigned = 3;
1874#else
1875 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
1876#endif
Theodore Ts'o5e1f8c92008-10-28 13:21:55 -04001877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 if (sbi->s_blocks_per_group > blocksize * 8) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001880 ext3_msg(sb, KERN_ERR,
1881 "#blocks per group too big: %lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 sbi->s_blocks_per_group);
1883 goto failed_mount;
1884 }
1885 if (sbi->s_frags_per_group > blocksize * 8) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001886 ext3_msg(sb, KERN_ERR,
1887 "error: #fragments per group too big: %lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 sbi->s_frags_per_group);
1889 goto failed_mount;
1890 }
1891 if (sbi->s_inodes_per_group > blocksize * 8) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001892 ext3_msg(sb, KERN_ERR,
1893 "error: #inodes per group too big: %lu",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 sbi->s_inodes_per_group);
1895 goto failed_mount;
1896 }
1897
Darrick J. Wongad692bf2010-11-16 22:57:44 +00001898 err = generic_check_addressable(sb->s_blocksize_bits,
1899 le32_to_cpu(es->s_blocks_count));
1900 if (err) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001901 ext3_msg(sb, KERN_ERR,
1902 "error: filesystem is too large to mount safely");
Mingming Caofcd5df32006-06-25 05:47:50 -07001903 if (sizeof(sector_t) < 8)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001904 ext3_msg(sb, KERN_ERR,
1905 "error: CONFIG_LBDAF not enabled");
Darrick J. Wongad692bf2010-11-16 22:57:44 +00001906 ret = err;
Mingming Caofcd5df32006-06-25 05:47:50 -07001907 goto failed_mount;
1908 }
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
1911 goto cantfind_ext3;
Eric Sandeen855565e2006-09-27 01:49:29 -07001912 sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
1913 le32_to_cpu(es->s_first_data_block) - 1)
1914 / EXT3_BLOCKS_PER_GROUP(sb)) + 1;
Namhyung Kim81a4e322010-10-11 19:38:39 +09001915 db_count = DIV_ROUND_UP(sbi->s_groups_count, EXT3_DESC_PER_BLOCK(sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
1917 GFP_KERNEL);
1918 if (sbi->s_group_desc == NULL) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001919 ext3_msg(sb, KERN_ERR,
1920 "error: not enough memory");
Namhyung Kim4569cd12010-10-11 19:08:06 +09001921 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto failed_mount;
1923 }
1924
Pekka Enberg5df096d2009-01-07 18:07:25 -08001925 bgl_lock_init(sbi->s_blockgroup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
1927 for (i = 0; i < db_count; i++) {
1928 block = descriptor_loc(sb, logic_sb_block, i);
1929 sbi->s_group_desc[i] = sb_bread(sb, block);
1930 if (!sbi->s_group_desc[i]) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001931 ext3_msg(sb, KERN_ERR,
1932 "error: can't read group descriptor %d", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 db_count = i;
1934 goto failed_mount2;
1935 }
1936 }
1937 if (!ext3_check_descriptors (sb)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001938 ext3_msg(sb, KERN_ERR,
1939 "error: group descriptors corrupted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto failed_mount2;
1941 }
1942 sbi->s_gdb_count = db_count;
1943 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
1944 spin_lock_init(&sbi->s_next_gen_lock);
Mingming Cao0216bfc2006-06-23 02:05:41 -07001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /* per fileystem reservation list head & lock */
1947 spin_lock_init(&sbi->s_rsv_window_lock);
1948 sbi->s_rsv_window_root = RB_ROOT;
1949 /* Add a single, static dummy reservation to the start of the
1950 * reservation window list --- it gives us a placeholder for
1951 * append-at-start-of-list which makes the allocation logic
1952 * _much_ simpler. */
1953 sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1954 sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
1955 sbi->s_rsv_window_head.rsv_alloc_hit = 0;
1956 sbi->s_rsv_window_head.rsv_goal_size = 0;
1957 ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
1958
1959 /*
1960 * set up enough so that it can read an inode
1961 */
1962 sb->s_op = &ext3_sops;
1963 sb->s_export_op = &ext3_export_ops;
1964 sb->s_xattr = ext3_xattr_handlers;
1965#ifdef CONFIG_QUOTA
1966 sb->s_qcop = &ext3_qctl_operations;
1967 sb->dq_op = &ext3_quota_operations;
1968#endif
Aneesh Kumar K.V03cb5f02011-01-29 18:43:39 +05301969 memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Eric Sandeenb8a052d2009-12-14 13:00:30 -06001971 mutex_init(&sbi->s_orphan_lock);
Eric Sandeen96d2a492009-12-14 13:01:05 -06001972 mutex_init(&sbi->s_resize_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
1974 sb->s_root = NULL;
1975
1976 needs_recovery = (es->s_last_orphan != 0 ||
1977 EXT3_HAS_INCOMPAT_FEATURE(sb,
1978 EXT3_FEATURE_INCOMPAT_RECOVER));
1979
1980 /*
1981 * The first inode we look at is the journal inode. Don't try
1982 * root first: it may be modified in the journal!
1983 */
1984 if (!test_opt(sb, NOLOAD) &&
1985 EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
Johann Lombardi71b96252006-01-08 01:03:20 -08001986 if (ext3_load_journal(sb, es, journal_devnum))
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04001987 goto failed_mount2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } else if (journal_inum) {
1989 if (ext3_create_journal(sb, es, journal_inum))
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04001990 goto failed_mount2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 } else {
1992 if (!silent)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01001993 ext3_msg(sb, KERN_ERR,
1994 "error: no journal found. "
1995 "mounting ext3 over ext2?");
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04001996 goto failed_mount2;
1997 }
1998 err = percpu_counter_init(&sbi->s_freeblocks_counter,
1999 ext3_count_free_blocks(sb));
2000 if (!err) {
2001 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2002 ext3_count_free_inodes(sb));
2003 }
2004 if (!err) {
2005 err = percpu_counter_init(&sbi->s_dirs_counter,
2006 ext3_count_dirs(sb));
2007 }
2008 if (err) {
2009 ext3_msg(sb, KERN_ERR, "error: insufficient memory");
Namhyung Kim4569cd12010-10-11 19:08:06 +09002010 ret = err;
Mingming Cao0216bfc2006-06-23 02:05:41 -07002011 goto failed_mount3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
2014 /* We have now updated the journal if required, so we can
2015 * validate the data journaling mode. */
2016 switch (test_opt(sb, DATA_FLAGS)) {
2017 case 0:
2018 /* No mode set, assume a default based on the journal
2019 capabilities: ORDERED_DATA if the journal can
2020 cope, else JOURNAL_DATA */
2021 if (journal_check_available_features
2022 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
Linus Torvaldsbbae8bc2009-04-06 17:16:47 -07002023 set_opt(sbi->s_mount_opt, DEFAULT_DATA_MODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 else
2025 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2026 break;
2027
2028 case EXT3_MOUNT_ORDERED_DATA:
2029 case EXT3_MOUNT_WRITEBACK_DATA:
2030 if (!journal_check_available_features
2031 (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002032 ext3_msg(sb, KERN_ERR,
2033 "error: journal does not support "
2034 "requested data journaling mode");
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04002035 goto failed_mount3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 default:
2038 break;
2039 }
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 /*
2042 * The journal_load will have done any necessary log recovery,
2043 * so we can safely mount the rest of the filesystem now.
2044 */
2045
David Howells473043d2008-02-07 00:15:36 -08002046 root = ext3_iget(sb, EXT3_ROOT_INO);
2047 if (IS_ERR(root)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002048 ext3_msg(sb, KERN_ERR, "error: get root inode failed");
David Howells473043d2008-02-07 00:15:36 -08002049 ret = PTR_ERR(root);
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04002050 goto failed_mount3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
David Howells473043d2008-02-07 00:15:36 -08002053 iput(root);
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002054 ext3_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04002055 goto failed_mount3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Al Viro48fde702012-01-08 22:15:13 -05002057 sb->s_root = d_make_root(root);
David Howells473043d2008-02-07 00:15:36 -08002058 if (!sb->s_root) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002059 ext3_msg(sb, KERN_ERR, "error: get root dentry failed");
David Howells473043d2008-02-07 00:15:36 -08002060 ret = -ENOMEM;
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04002061 goto failed_mount3;
David Howells473043d2008-02-07 00:15:36 -08002062 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Eric Sandeenf007dbf2012-05-24 12:00:37 -05002064 if (ext3_setup_super(sb, es, sb->s_flags & MS_RDONLY))
2065 sb->s_flags |= MS_RDONLY;
Eric Sandeened505ee2009-12-14 12:59:18 -06002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
2068 ext3_orphan_cleanup(sb, es);
2069 EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
Eryu Guan63894ab2011-11-01 10:06:19 +08002070 if (needs_recovery) {
2071 ext3_mark_recovery_complete(sb, es);
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002072 ext3_msg(sb, KERN_INFO, "recovery complete");
Eryu Guan63894ab2011-11-01 10:06:19 +08002073 }
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002074 ext3_msg(sb, KERN_INFO, "mounted filesystem with %s data mode",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
2076 test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
2077 "writeback");
2078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 return 0;
2080
2081cantfind_ext3:
2082 if (!silent)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002083 ext3_msg(sb, KERN_INFO,
2084 "error: can't find ext3 filesystem on dev %s.",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 sb->s_id);
2086 goto failed_mount;
2087
Mingming Cao0216bfc2006-06-23 02:05:41 -07002088failed_mount3:
2089 percpu_counter_destroy(&sbi->s_freeblocks_counter);
2090 percpu_counter_destroy(&sbi->s_freeinodes_counter);
2091 percpu_counter_destroy(&sbi->s_dirs_counter);
Dmitry Monakhov41d1a632010-04-12 23:46:00 +04002092 journal_destroy(sbi->s_journal);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093failed_mount2:
2094 for (i = 0; i < db_count; i++)
2095 brelse(sbi->s_group_desc[i]);
2096 kfree(sbi->s_group_desc);
2097failed_mount:
2098#ifdef CONFIG_QUOTA
2099 for (i = 0; i < MAXQUOTAS; i++)
2100 kfree(sbi->s_qf_names[i]);
2101#endif
2102 ext3_blkdev_remove(sbi);
2103 brelse(bh);
2104out_fail:
2105 sb->s_fs_info = NULL;
Manish Katiyarde5ce032009-05-17 23:52:47 -04002106 kfree(sbi->s_blockgroup_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 kfree(sbi);
David Howells473043d2008-02-07 00:15:36 -08002108 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109}
2110
2111/*
2112 * Setup any per-fs journal parameters now. We'll do this both on
2113 * initial mount, once the journal has been initialised but before we've
Mingming Caoae6ddcc2006-09-27 01:49:27 -07002114 * done any recovery; and again on any subsequent remount.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
2116static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
2117{
2118 struct ext3_sb_info *sbi = EXT3_SB(sb);
2119
2120 if (sbi->s_commit_interval)
2121 journal->j_commit_interval = sbi->s_commit_interval;
2122 /* We could also set up an ext3-specific default for the commit
2123 * interval here, but for now we'll just fall back to the jbd
2124 * default. */
2125
2126 spin_lock(&journal->j_state_lock);
2127 if (test_opt(sb, BARRIER))
2128 journal->j_flags |= JFS_BARRIER;
2129 else
2130 journal->j_flags &= ~JFS_BARRIER;
Hidehiro Kawai0e4fb5e2008-10-18 20:27:57 -07002131 if (test_opt(sb, DATA_ERR_ABORT))
2132 journal->j_flags |= JFS_ABORT_ON_SYNCDATA_ERR;
2133 else
2134 journal->j_flags &= ~JFS_ABORT_ON_SYNCDATA_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 spin_unlock(&journal->j_state_lock);
2136}
2137
Eric Sandeeneee194e2006-09-27 01:49:30 -07002138static journal_t *ext3_get_journal(struct super_block *sb,
2139 unsigned int journal_inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140{
2141 struct inode *journal_inode;
2142 journal_t *journal;
2143
2144 /* First, test for the existence of a valid inode on disk. Bad
2145 * things happen if we iget() an unused inode, as the subsequent
2146 * iput() will try to delete it. */
2147
David Howells473043d2008-02-07 00:15:36 -08002148 journal_inode = ext3_iget(sb, journal_inum);
2149 if (IS_ERR(journal_inode)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002150 ext3_msg(sb, KERN_ERR, "error: no journal found");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 return NULL;
2152 }
2153 if (!journal_inode->i_nlink) {
2154 make_bad_inode(journal_inode);
2155 iput(journal_inode);
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002156 ext3_msg(sb, KERN_ERR, "error: journal inode is deleted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 return NULL;
2158 }
2159
2160 jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
2161 journal_inode, journal_inode->i_size);
David Howells473043d2008-02-07 00:15:36 -08002162 if (!S_ISREG(journal_inode->i_mode)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002163 ext3_msg(sb, KERN_ERR, "error: invalid journal inode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 iput(journal_inode);
2165 return NULL;
2166 }
2167
2168 journal = journal_init_inode(journal_inode);
2169 if (!journal) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002170 ext3_msg(sb, KERN_ERR, "error: could not load journal inode");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 iput(journal_inode);
2172 return NULL;
2173 }
2174 journal->j_private = sb;
2175 ext3_init_journal_params(sb, journal);
2176 return journal;
2177}
2178
2179static journal_t *ext3_get_dev_journal(struct super_block *sb,
2180 dev_t j_dev)
2181{
2182 struct buffer_head * bh;
2183 journal_t *journal;
Mingming Cao43d23f92006-06-25 05:48:07 -07002184 ext3_fsblk_t start;
Mingming Cao1c2bf372006-06-25 05:48:06 -07002185 ext3_fsblk_t len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 int hblock, blocksize;
Mingming Cao43d23f92006-06-25 05:48:07 -07002187 ext3_fsblk_t sb_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 unsigned long offset;
2189 struct ext3_super_block * es;
2190 struct block_device *bdev;
2191
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002192 bdev = ext3_blkdev_get(j_dev, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 if (bdev == NULL)
2194 return NULL;
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04002197 hblock = bdev_logical_block_size(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 if (blocksize < hblock) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002199 ext3_msg(sb, KERN_ERR,
2200 "error: blocksize too small for journal device");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 goto out_bdev;
2202 }
2203
2204 sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
2205 offset = EXT3_MIN_BLOCK_SIZE % blocksize;
2206 set_blocksize(bdev, blocksize);
2207 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002208 ext3_msg(sb, KERN_ERR, "error: couldn't read superblock of "
2209 "external journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto out_bdev;
2211 }
2212
Namhyung Kim57e94d82010-10-11 02:10:45 +09002213 es = (struct ext3_super_block *) (bh->b_data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
2215 !(le32_to_cpu(es->s_feature_incompat) &
2216 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002217 ext3_msg(sb, KERN_ERR, "error: external journal has "
2218 "bad superblock");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 brelse(bh);
2220 goto out_bdev;
2221 }
2222
2223 if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002224 ext3_msg(sb, KERN_ERR, "error: journal UUID does not match");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 brelse(bh);
2226 goto out_bdev;
2227 }
2228
2229 len = le32_to_cpu(es->s_blocks_count);
2230 start = sb_block + 1;
2231 brelse(bh); /* we're done with the superblock */
2232
2233 journal = journal_init_dev(bdev, sb->s_bdev,
2234 start, len, blocksize);
2235 if (!journal) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002236 ext3_msg(sb, KERN_ERR,
2237 "error: failed to create device journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 goto out_bdev;
2239 }
2240 journal->j_private = sb;
Zheng Liud03e1292011-12-05 15:55:11 +08002241 if (!bh_uptodate_or_lock(journal->j_sb_buffer)) {
2242 if (bh_submit_read(journal->j_sb_buffer)) {
2243 ext3_msg(sb, KERN_ERR, "I/O error on journal device");
2244 goto out_journal;
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002248 ext3_msg(sb, KERN_ERR,
2249 "error: external journal has more than one "
2250 "user (unsupported) - %d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 be32_to_cpu(journal->j_superblock->s_nr_users));
2252 goto out_journal;
2253 }
2254 EXT3_SB(sb)->journal_bdev = bdev;
2255 ext3_init_journal_params(sb, journal);
2256 return journal;
2257out_journal:
2258 journal_destroy(journal);
2259out_bdev:
2260 ext3_blkdev_put(bdev);
2261 return NULL;
2262}
2263
Johann Lombardi71b96252006-01-08 01:03:20 -08002264static int ext3_load_journal(struct super_block *sb,
2265 struct ext3_super_block *es,
2266 unsigned long journal_devnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267{
2268 journal_t *journal;
Eric Sandeeneee194e2006-09-27 01:49:30 -07002269 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
Johann Lombardi71b96252006-01-08 01:03:20 -08002270 dev_t journal_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 int err = 0;
2272 int really_read_only;
2273
Johann Lombardi71b96252006-01-08 01:03:20 -08002274 if (journal_devnum &&
2275 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002276 ext3_msg(sb, KERN_INFO, "external journal device major/minor "
2277 "numbers have changed");
Johann Lombardi71b96252006-01-08 01:03:20 -08002278 journal_dev = new_decode_dev(journal_devnum);
2279 } else
2280 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
2281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 really_read_only = bdev_read_only(sb->s_bdev);
2283
2284 /*
2285 * Are we loading a blank journal or performing recovery after a
2286 * crash? For recovery, we need to check in advance whether we
2287 * can get read-write access to the device.
2288 */
2289
2290 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
2291 if (sb->s_flags & MS_RDONLY) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002292 ext3_msg(sb, KERN_INFO,
2293 "recovery required on readonly filesystem");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 if (really_read_only) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002295 ext3_msg(sb, KERN_ERR, "error: write access "
2296 "unavailable, cannot proceed");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return -EROFS;
2298 }
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002299 ext3_msg(sb, KERN_INFO,
2300 "write access will be enabled during recovery");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
2302 }
2303
2304 if (journal_inum && journal_dev) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002305 ext3_msg(sb, KERN_ERR, "error: filesystem has both journal "
2306 "and inode journals");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 return -EINVAL;
2308 }
2309
2310 if (journal_inum) {
2311 if (!(journal = ext3_get_journal(sb, journal_inum)))
2312 return -EINVAL;
2313 } else {
2314 if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
2315 return -EINVAL;
2316 }
2317
Eric Sandeen0636c732010-04-30 11:09:34 -05002318 if (!(journal->j_flags & JFS_BARRIER))
2319 printk(KERN_INFO "EXT3-fs: barriers not enabled\n");
2320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
2322 err = journal_update_format(journal);
2323 if (err) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002324 ext3_msg(sb, KERN_ERR, "error updating journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 journal_destroy(journal);
2326 return err;
2327 }
2328 }
2329
2330 if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
2331 err = journal_wipe(journal, !really_read_only);
2332 if (!err)
2333 err = journal_load(journal);
2334
2335 if (err) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002336 ext3_msg(sb, KERN_ERR, "error loading journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 journal_destroy(journal);
2338 return err;
2339 }
2340
2341 EXT3_SB(sb)->s_journal = journal;
2342 ext3_clear_journal_err(sb, es);
Johann Lombardi71b96252006-01-08 01:03:20 -08002343
Maciej Żenczykowski31d710a2010-09-26 12:38:28 +00002344 if (!really_read_only && journal_devnum &&
Johann Lombardi71b96252006-01-08 01:03:20 -08002345 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
2346 es->s_journal_dev = cpu_to_le32(journal_devnum);
Johann Lombardi71b96252006-01-08 01:03:20 -08002347
2348 /* Make sure we flush the recovery flag to disk. */
2349 ext3_commit_super(sb, es, 1);
2350 }
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return 0;
2353}
2354
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002355static int ext3_create_journal(struct super_block *sb,
2356 struct ext3_super_block *es,
Eric Sandeeneee194e2006-09-27 01:49:30 -07002357 unsigned int journal_inum)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 journal_t *journal;
Borislav Petkov952d9de2007-07-15 23:41:45 -07002360 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 if (sb->s_flags & MS_RDONLY) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002363 ext3_msg(sb, KERN_ERR,
2364 "error: readonly filesystem when trying to "
2365 "create journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return -EROFS;
2367 }
2368
Borislav Petkov952d9de2007-07-15 23:41:45 -07002369 journal = ext3_get_journal(sb, journal_inum);
2370 if (!journal)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return -EINVAL;
2372
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002373 ext3_msg(sb, KERN_INFO, "creating new journal on inode %u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 journal_inum);
2375
Borislav Petkov952d9de2007-07-15 23:41:45 -07002376 err = journal_create(journal);
2377 if (err) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002378 ext3_msg(sb, KERN_ERR, "error creating journal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 journal_destroy(journal);
2380 return -EIO;
2381 }
2382
2383 EXT3_SB(sb)->s_journal = journal;
2384
2385 ext3_update_dynamic_rev(sb);
2386 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2387 EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
2388
2389 es->s_journal_inum = cpu_to_le32(journal_inum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 /* Make sure we flush the recovery flag to disk. */
2392 ext3_commit_super(sb, es, 1);
2393
2394 return 0;
2395}
2396
Takashi Satoc4be0c12009-01-09 16:40:58 -08002397static int ext3_commit_super(struct super_block *sb,
2398 struct ext3_super_block *es,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399 int sync)
2400{
2401 struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08002402 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 if (!sbh)
Takashi Satoc4be0c12009-01-09 16:40:58 -08002405 return error;
Darrick J. Wongdff68252010-10-04 12:35:05 -07002406
2407 if (buffer_write_io_error(sbh)) {
2408 /*
2409 * Oh, dear. A previous attempt to write the
2410 * superblock failed. This could happen because the
2411 * USB device was yanked out. Or it could happen to
2412 * be a transient write error and maybe the block will
2413 * be remapped. Nothing we can do but to retry the
2414 * write and hope for the best.
2415 */
2416 ext3_msg(sb, KERN_ERR, "previous I/O error to "
2417 "superblock detected");
2418 clear_buffer_write_io_error(sbh);
2419 set_buffer_uptodate(sbh);
2420 }
Theodore Ts'o96ec2e02009-09-16 11:21:13 -04002421 /*
2422 * If the file system is mounted read-only, don't update the
2423 * superblock write time. This avoids updating the superblock
2424 * write time when we are mounting the root file system
2425 * read/only but we need to replay the journal; at that point,
2426 * for people who are east of GMT and who make their clock
2427 * tick in localtime for Windows bug-for-bug compatibility,
2428 * the clock is set in the future, and this will cause e2fsck
2429 * to complain and force a full file system check.
2430 */
2431 if (!(sb->s_flags & MS_RDONLY))
2432 es->s_wtime = cpu_to_le32(get_seconds());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
2434 es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
2435 BUFFER_TRACE(sbh, "marking dirty");
2436 mark_buffer_dirty(sbh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07002437 if (sync) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08002438 error = sync_dirty_buffer(sbh);
Darrick J. Wongdff68252010-10-04 12:35:05 -07002439 if (buffer_write_io_error(sbh)) {
2440 ext3_msg(sb, KERN_ERR, "I/O error while writing "
2441 "superblock");
2442 clear_buffer_write_io_error(sbh);
2443 set_buffer_uptodate(sbh);
2444 }
2445 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08002446 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
2448
2449
2450/*
2451 * Have we just finished recovery? If so, and if we are mounting (or
2452 * remounting) the filesystem readonly, then we will end up with a
2453 * consistent fs on disk. Record that fact.
2454 */
2455static void ext3_mark_recovery_complete(struct super_block * sb,
2456 struct ext3_super_block * es)
2457{
2458 journal_t *journal = EXT3_SB(sb)->s_journal;
2459
2460 journal_lock_updates(journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -07002461 if (journal_flush(journal) < 0)
2462 goto out;
2463
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
2465 sb->s_flags & MS_RDONLY) {
2466 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 ext3_commit_super(sb, es, 1);
2468 }
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -07002469
2470out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 journal_unlock_updates(journal);
2472}
2473
2474/*
2475 * If we are mounting (or read-write remounting) a filesystem whose journal
2476 * has recorded an error from a previous lifetime, move that error to the
2477 * main filesystem now.
2478 */
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002479static void ext3_clear_journal_err(struct super_block *sb,
2480 struct ext3_super_block *es)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 journal_t *journal;
2483 int j_errno;
2484 const char *errstr;
2485
2486 journal = EXT3_SB(sb)->s_journal;
2487
2488 /*
2489 * Now check for any error status which may have been recorded in the
2490 * journal by a prior ext3_error() or ext3_abort()
2491 */
2492
2493 j_errno = journal_errno(journal);
2494 if (j_errno) {
2495 char nbuf[16];
2496
2497 errstr = ext3_decode_error(sb, j_errno, nbuf);
Harvey Harrisone05b6b52008-04-28 02:16:15 -07002498 ext3_warning(sb, __func__, "Filesystem error recorded "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 "from previous mount: %s", errstr);
Harvey Harrisone05b6b52008-04-28 02:16:15 -07002500 ext3_warning(sb, __func__, "Marking fs in need of "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 "filesystem check.");
2502
2503 EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
2504 es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
2505 ext3_commit_super (sb, es, 1);
2506
2507 journal_clear_err(journal);
2508 }
2509}
2510
2511/*
2512 * Force the running and committing transactions to commit,
2513 * and wait on the commit.
2514 */
2515int ext3_force_commit(struct super_block *sb)
2516{
2517 journal_t *journal;
2518 int ret;
2519
2520 if (sb->s_flags & MS_RDONLY)
2521 return 0;
2522
2523 journal = EXT3_SB(sb)->s_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 ret = ext3_journal_force_commit(journal);
2525 return ret;
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528static int ext3_sync_fs(struct super_block *sb, int wait)
2529{
Jan Kara02ac5972009-02-11 13:04:26 -08002530 tid_t target;
Arthur Jonesc87591b2008-11-06 12:53:35 -08002531
Lukas Czerner785c4bc2011-05-23 18:33:01 +02002532 trace_ext3_sync_fs(sb, wait);
Jan Karaa1177822012-07-03 16:45:29 +02002533 /*
2534 * Writeback quota in non-journalled quota case - journalled quota has
2535 * no dirty dquots
2536 */
2537 dquot_writeback_dquots(sb, -1);
Jan Kara02ac5972009-02-11 13:04:26 -08002538 if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
2539 if (wait)
2540 log_wait_commit(EXT3_SB(sb)->s_journal, target);
2541 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return 0;
2543}
2544
2545/*
2546 * LVM calls this function before a (read-only) snapshot is created. This
2547 * gives us a chance to flush the journal completely and mark the fs clean.
2548 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08002549static int ext3_freeze(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
Takashi Satoc4be0c12009-01-09 16:40:58 -08002551 int error = 0;
2552 journal_t *journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 if (!(sb->s_flags & MS_RDONLY)) {
Takashi Satoc4be0c12009-01-09 16:40:58 -08002555 journal = EXT3_SB(sb)->s_journal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556
2557 /* Now we set up the journal barrier. */
2558 journal_lock_updates(journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -07002559
2560 /*
2561 * We don't want to clear needs_recovery flag when we failed
2562 * to flush the journal.
2563 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08002564 error = journal_flush(journal);
2565 if (error < 0)
2566 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
2568 /* Journal blocked and flushed, clear needs_recovery flag. */
2569 EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
Takashi Satoc4be0c12009-01-09 16:40:58 -08002570 error = ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
2571 if (error)
2572 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08002574 return 0;
2575
2576out:
2577 journal_unlock_updates(journal);
2578 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579}
2580
2581/*
2582 * Called by LVM after the snapshot is done. We need to reset the RECOVER
2583 * flag here, even though the filesystem is not technically dirty yet.
2584 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08002585static int ext3_unfreeze(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586{
2587 if (!(sb->s_flags & MS_RDONLY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 /* Reser the needs_recovery flag before the fs is unlocked. */
2589 EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
2590 ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 journal_unlock_updates(EXT3_SB(sb)->s_journal);
2592 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08002593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
2596static int ext3_remount (struct super_block * sb, int * flags, char * data)
2597{
2598 struct ext3_super_block * es;
2599 struct ext3_sb_info *sbi = EXT3_SB(sb);
Mingming Cao43d23f92006-06-25 05:48:07 -07002600 ext3_fsblk_t n_blocks_count = 0;
Jan Kara08c6a962005-07-12 13:58:28 -07002601 unsigned long old_sb_flags;
2602 struct ext3_mount_options old_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04002603 int enable_quota = 0;
Jan Kara08c6a962005-07-12 13:58:28 -07002604 int err;
2605#ifdef CONFIG_QUOTA
2606 int i;
2607#endif
2608
2609 /* Store the original options */
2610 old_sb_flags = sb->s_flags;
2611 old_opts.s_mount_opt = sbi->s_mount_opt;
2612 old_opts.s_resuid = sbi->s_resuid;
2613 old_opts.s_resgid = sbi->s_resgid;
2614 old_opts.s_commit_interval = sbi->s_commit_interval;
2615#ifdef CONFIG_QUOTA
2616 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
2617 for (i = 0; i < MAXQUOTAS; i++)
Jan Karaf56426a2012-12-31 12:38:36 +01002618 if (sbi->s_qf_names[i]) {
2619 old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
2620 GFP_KERNEL);
2621 if (!old_opts.s_qf_names[i]) {
2622 int j;
2623
2624 for (j = 0; j < i; j++)
2625 kfree(old_opts.s_qf_names[j]);
2626 return -ENOMEM;
2627 }
2628 } else
2629 old_opts.s_qf_names[i] = NULL;
Jan Kara08c6a962005-07-12 13:58:28 -07002630#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
2632 /*
2633 * Allow the "check" option to be passed as a remount option.
2634 */
Johann Lombardi71b96252006-01-08 01:03:20 -08002635 if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
Jan Kara08c6a962005-07-12 13:58:28 -07002636 err = -EINVAL;
2637 goto restore_opts;
2638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Dmitry Monakhove3c96432010-02-02 16:05:51 +03002640 if (test_opt(sb, ABORT))
Harvey Harrisone05b6b52008-04-28 02:16:15 -07002641 ext3_abort(sb, __func__, "Abort forced by user");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
Dmitry Monakhove3c96432010-02-02 16:05:51 +03002644 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645
2646 es = sbi->s_es;
2647
2648 ext3_init_journal_params(sb, sbi->s_journal);
2649
2650 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
2651 n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
Dmitry Monakhove3c96432010-02-02 16:05:51 +03002652 if (test_opt(sb, ABORT)) {
Jan Kara08c6a962005-07-12 13:58:28 -07002653 err = -EROFS;
2654 goto restore_opts;
2655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 if (*flags & MS_RDONLY) {
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002658 err = dquot_suspend(sb, -1);
2659 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04002660 goto restore_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04002661
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 /*
2663 * First of all, the unconditional stuff we have to do
2664 * to disable replay of the journal when we next remount
2665 */
2666 sb->s_flags |= MS_RDONLY;
2667
2668 /*
2669 * OK, test if we are remounting a valid rw partition
2670 * readonly, and if so set the rdonly flag and then
2671 * mark the partition as valid again.
2672 */
2673 if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
2674 (sbi->s_mount_state & EXT3_VALID_FS))
2675 es->s_state = cpu_to_le16(sbi->s_mount_state);
2676
2677 ext3_mark_recovery_complete(sb, es);
2678 } else {
2679 __le32 ret;
2680 if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
2681 ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002682 ext3_msg(sb, KERN_WARNING,
2683 "warning: couldn't remount RDWR "
2684 "because of unsupported optional "
2685 "features (%x)", le32_to_cpu(ret));
Jan Kara08c6a962005-07-12 13:58:28 -07002686 err = -EROFS;
2687 goto restore_opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 }
Eric Sandeenea9a05a2007-02-10 01:46:07 -08002689
2690 /*
2691 * If we have an unprocessed orphan list hanging
2692 * around from a previously readonly bdev mount,
Toshiyuki Okajima1cde2012011-08-02 18:16:57 +09002693 * require a full umount & mount for now.
Eric Sandeenea9a05a2007-02-10 01:46:07 -08002694 */
2695 if (es->s_last_orphan) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002696 ext3_msg(sb, KERN_WARNING, "warning: couldn't "
Eric Sandeenea9a05a2007-02-10 01:46:07 -08002697 "remount RDWR because of unprocessed "
2698 "orphan inode list. Please "
Toshiyuki Okajima1cde2012011-08-02 18:16:57 +09002699 "umount & mount instead.");
Eric Sandeenea9a05a2007-02-10 01:46:07 -08002700 err = -EINVAL;
2701 goto restore_opts;
2702 }
2703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 /*
2705 * Mounting a RDONLY partition read-write, so reread
2706 * and store the current valid flag. (It may have
2707 * been changed by e2fsck since we originally mounted
2708 * the partition.)
2709 */
2710 ext3_clear_journal_err(sb, es);
2711 sbi->s_mount_state = le16_to_cpu(es->s_state);
Dave Kleikampa4e4de32006-09-27 01:49:36 -07002712 if ((err = ext3_group_extend(sb, es, n_blocks_count)))
Jan Kara08c6a962005-07-12 13:58:28 -07002713 goto restore_opts;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (!ext3_setup_super (sb, es, 0))
2715 sb->s_flags &= ~MS_RDONLY;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04002716 enable_quota = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718 }
Jan Kara08c6a962005-07-12 13:58:28 -07002719#ifdef CONFIG_QUOTA
2720 /* Release old quota file names */
2721 for (i = 0; i < MAXQUOTAS; i++)
Jan Karaf56426a2012-12-31 12:38:36 +01002722 kfree(old_opts.s_qf_names[i]);
Jan Kara08c6a962005-07-12 13:58:28 -07002723#endif
Christoph Hellwigc79d9672010-05-19 07:16:40 -04002724 if (enable_quota)
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04002725 dquot_resume(sb, -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 return 0;
Jan Kara08c6a962005-07-12 13:58:28 -07002727restore_opts:
2728 sb->s_flags = old_sb_flags;
2729 sbi->s_mount_opt = old_opts.s_mount_opt;
2730 sbi->s_resuid = old_opts.s_resuid;
2731 sbi->s_resgid = old_opts.s_resgid;
2732 sbi->s_commit_interval = old_opts.s_commit_interval;
2733#ifdef CONFIG_QUOTA
2734 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
2735 for (i = 0; i < MAXQUOTAS; i++) {
Jan Karaf56426a2012-12-31 12:38:36 +01002736 kfree(sbi->s_qf_names[i]);
Jan Kara08c6a962005-07-12 13:58:28 -07002737 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
2738 }
2739#endif
2740 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
David Howells726c3342006-06-23 02:02:58 -07002743static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
David Howells726c3342006-06-23 02:02:58 -07002745 struct super_block *sb = dentry->d_sb;
Alex Tomas09fe3162006-03-24 03:16:16 -08002746 struct ext3_sb_info *sbi = EXT3_SB(sb);
2747 struct ext3_super_block *es = sbi->s_es;
Pekka Enberg50ee0a32006-12-06 20:35:28 -08002748 u64 fsid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
Badari Pulavartya71ce8c2007-07-15 23:41:59 -07002750 if (test_opt(sb, MINIX_DF)) {
2751 sbi->s_overhead_last = 0;
2752 } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
2753 unsigned long ngroups = sbi->s_groups_count, i;
2754 ext3_fsblk_t overhead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 smp_rmb();
2756
2757 /*
Badari Pulavartya71ce8c2007-07-15 23:41:59 -07002758 * Compute the overhead (FS structures). This is constant
2759 * for a given filesystem unless the number of block groups
2760 * changes so we cache the previous value until it does.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 */
2762
2763 /*
2764 * All of the blocks before first_data_block are
2765 * overhead
2766 */
2767 overhead = le32_to_cpu(es->s_first_data_block);
2768
2769 /*
2770 * Add the overhead attributed to the superblock and
2771 * block group descriptors. If the sparse superblocks
2772 * feature is turned on, then not all groups have this.
2773 */
2774 for (i = 0; i < ngroups; i++) {
2775 overhead += ext3_bg_has_super(sb, i) +
2776 ext3_bg_num_gdb(sb, i);
2777 cond_resched();
2778 }
2779
2780 /*
2781 * Every block group has an inode bitmap, a block
2782 * bitmap, and an inode table.
2783 */
Badari Pulavartya71ce8c2007-07-15 23:41:59 -07002784 overhead += ngroups * (2 + sbi->s_itb_per_group);
2785 sbi->s_overhead_last = overhead;
2786 smp_wmb();
2787 sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
2789
2790 buf->f_type = EXT3_SUPER_MAGIC;
2791 buf->f_bsize = sb->s_blocksize;
Badari Pulavartya71ce8c2007-07-15 23:41:59 -07002792 buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07002793 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
2795 if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
2796 buf->f_bavail = 0;
2797 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07002798 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 buf->f_namelen = EXT3_NAME_LEN;
Pekka Enberg50ee0a32006-12-06 20:35:28 -08002800 fsid = le64_to_cpup((void *)es->s_uuid) ^
2801 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
2802 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
2803 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 return 0;
2805}
2806
2807/* Helper function for writing quotas on sync - we need to start transaction before quota file
2808 * is locked for write. Otherwise the are possible deadlocks:
2809 * Process 1 Process 2
2810 * ext3_create() quota_sync()
2811 * journal_start() write_dquot()
Christoph Hellwig871a2932010-03-03 09:05:07 -05002812 * dquot_initialize() down(dqio_mutex)
Ingo Molnard3be9152006-03-23 03:00:29 -08002813 * down(dqio_mutex) journal_start()
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 *
2815 */
2816
2817#ifdef CONFIG_QUOTA
2818
2819static inline struct inode *dquot_to_inode(struct dquot *dquot)
2820{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07002821 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822}
2823
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824static int ext3_write_dquot(struct dquot *dquot)
2825{
2826 int ret, err;
2827 handle_t *handle;
2828 struct inode *inode;
2829
2830 inode = dquot_to_inode(dquot);
2831 handle = ext3_journal_start(inode,
Jan Kara1f545872005-06-23 22:01:04 -07002832 EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 if (IS_ERR(handle))
2834 return PTR_ERR(handle);
2835 ret = dquot_commit(dquot);
2836 err = ext3_journal_stop(handle);
2837 if (!ret)
2838 ret = err;
2839 return ret;
2840}
2841
2842static int ext3_acquire_dquot(struct dquot *dquot)
2843{
2844 int ret, err;
2845 handle_t *handle;
2846
2847 handle = ext3_journal_start(dquot_to_inode(dquot),
Jan Kara1f545872005-06-23 22:01:04 -07002848 EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 if (IS_ERR(handle))
2850 return PTR_ERR(handle);
2851 ret = dquot_acquire(dquot);
2852 err = ext3_journal_stop(handle);
2853 if (!ret)
2854 ret = err;
2855 return ret;
2856}
2857
2858static int ext3_release_dquot(struct dquot *dquot)
2859{
2860 int ret, err;
2861 handle_t *handle;
2862
2863 handle = ext3_journal_start(dquot_to_inode(dquot),
Jan Kara1f545872005-06-23 22:01:04 -07002864 EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07002865 if (IS_ERR(handle)) {
2866 /* Release dquot anyway to avoid endless cycle in dqput() */
2867 dquot_release(dquot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07002869 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 ret = dquot_release(dquot);
2871 err = ext3_journal_stop(handle);
2872 if (!ret)
2873 ret = err;
2874 return ret;
2875}
2876
2877static int ext3_mark_dquot_dirty(struct dquot *dquot)
2878{
Jan Kara99aeaf62008-07-25 01:46:17 -07002879 /* Are we journaling quotas? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
2881 EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
2882 dquot_mark_dquot_dirty(dquot);
2883 return ext3_write_dquot(dquot);
2884 } else {
2885 return dquot_mark_dquot_dirty(dquot);
2886 }
2887}
2888
2889static int ext3_write_info(struct super_block *sb, int type)
2890{
2891 int ret, err;
2892 handle_t *handle;
2893
2894 /* Data block + inode block */
2895 handle = ext3_journal_start(sb->s_root->d_inode, 2);
2896 if (IS_ERR(handle))
2897 return PTR_ERR(handle);
2898 ret = dquot_commit_info(sb, type);
2899 err = ext3_journal_stop(handle);
2900 if (!ret)
2901 ret = err;
2902 return ret;
2903}
2904
2905/*
2906 * Turn on quotas during mount time - we need to find
2907 * the quota file and such...
2908 */
2909static int ext3_quota_on_mount(struct super_block *sb, int type)
2910{
Christoph Hellwig287a8092010-05-19 07:16:45 -04002911 return dquot_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
2912 EXT3_SB(sb)->s_jquota_fmt, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915/*
2916 * Standard function to be called on quota_on
2917 */
2918static int ext3_quota_on(struct super_block *sb, int type, int format_id,
Jan Karaf00c9e42010-09-15 17:38:58 +02002919 struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
2921 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Jan Kara1f545872005-06-23 22:01:04 -07002923 if (!test_opt(sb, QUOTA))
2924 return -EINVAL;
Jan Kara9cfe7b92008-07-25 01:46:16 -07002925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 /* Quotafile not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05002927 if (path->dentry->d_sb != sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 return -EXDEV;
Jan Kara9cfe7b92008-07-25 01:46:16 -07002929 /* Journaling quota? */
2930 if (EXT3_SB(sb)->s_qf_names[type]) {
2931 /* Quotafile not of fs root? */
Jan Karaf00c9e42010-09-15 17:38:58 +02002932 if (path->dentry->d_parent != sb->s_root)
Alexey Fisher4cf46b62009-11-22 20:38:55 +01002933 ext3_msg(sb, KERN_WARNING,
2934 "warning: Quota file not on filesystem root. "
2935 "Journaled quota will not work.");
Jan Kara9cfe7b92008-07-25 01:46:16 -07002936 }
2937
2938 /*
2939 * When we journal data on quota file, we have to flush journal to see
2940 * all updates to the file when we bypass pagecache...
2941 */
Jan Karaf00c9e42010-09-15 17:38:58 +02002942 if (ext3_should_journal_data(path->dentry->d_inode)) {
Jan Kara9cfe7b92008-07-25 01:46:16 -07002943 /*
2944 * We don't need to lock updates but journal_flush() could
2945 * otherwise be livelocked...
2946 */
2947 journal_lock_updates(EXT3_SB(sb)->s_journal);
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -07002948 err = journal_flush(EXT3_SB(sb)->s_journal);
Jan Kara9cfe7b92008-07-25 01:46:16 -07002949 journal_unlock_updates(EXT3_SB(sb)->s_journal);
Jan Karaf00c9e42010-09-15 17:38:58 +02002950 if (err)
Hidehiro Kawai2d7c8202008-10-22 14:15:01 -07002951 return err;
Jan Kara9cfe7b92008-07-25 01:46:16 -07002952 }
2953
Jan Karaf00c9e42010-09-15 17:38:58 +02002954 return dquot_quota_on(sb, type, format_id, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
2957/* Read data from quotafile - avoid pagecache and such because we cannot afford
2958 * acquiring the locks... As quota files are never truncated and quota code
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002959 * itself serializes the operations (and no one else should touch the files)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 * we don't have to be afraid of races */
2961static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
2962 size_t len, loff_t off)
2963{
2964 struct inode *inode = sb_dqopt(sb)->files[type];
2965 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
2966 int err = 0;
2967 int offset = off & (sb->s_blocksize - 1);
2968 int tocopy;
2969 size_t toread;
2970 struct buffer_head *bh;
2971 loff_t i_size = i_size_read(inode);
2972
2973 if (off > i_size)
2974 return 0;
2975 if (off+len > i_size)
2976 len = i_size-off;
2977 toread = len;
2978 while (toread > 0) {
2979 tocopy = sb->s_blocksize - offset < toread ?
2980 sb->s_blocksize - offset : toread;
2981 bh = ext3_bread(NULL, inode, blk, 0, &err);
2982 if (err)
2983 return err;
2984 if (!bh) /* A hole? */
2985 memset(data, 0, tocopy);
2986 else
2987 memcpy(data, bh->b_data+offset, tocopy);
2988 brelse(bh);
2989 offset = 0;
2990 toread -= tocopy;
2991 data += tocopy;
2992 blk++;
2993 }
2994 return len;
2995}
2996
2997/* Write to quotafile (we know the transaction is already started and has
2998 * enough credits) */
2999static ssize_t ext3_quota_write(struct super_block *sb, int type,
3000 const char *data, size_t len, loff_t off)
3001{
3002 struct inode *inode = sb_dqopt(sb)->files[type];
3003 sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
3004 int err = 0;
3005 int offset = off & (sb->s_blocksize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct buffer_head *bh;
3008 handle_t *handle = journal_current_handle();
3009
Jan Kara9c3013e2007-09-11 15:23:29 -07003010 if (!handle) {
Alexey Fisher4cf46b62009-11-22 20:38:55 +01003011 ext3_msg(sb, KERN_WARNING,
3012 "warning: quota write (off=%llu, len=%llu)"
3013 " cancelled because transaction is not started.",
Jan Kara9c3013e2007-09-11 15:23:29 -07003014 (unsigned long long)off, (unsigned long long)len);
3015 return -EIO;
3016 }
Dmitry Monakhove5472142010-02-16 19:33:42 +03003017
3018 /*
3019 * Since we account only one data block in transaction credits,
3020 * then it is impossible to cross a block boundary.
3021 */
3022 if (sb->s_blocksize - offset < len) {
3023 ext3_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
3024 " cancelled because not block aligned",
3025 (unsigned long long)off, (unsigned long long)len);
3026 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 }
Dmitry Monakhove5472142010-02-16 19:33:42 +03003028 bh = ext3_bread(handle, inode, blk, 1, &err);
3029 if (!bh)
3030 goto out;
3031 if (journal_quota) {
3032 err = ext3_journal_get_write_access(handle, bh);
3033 if (err) {
3034 brelse(bh);
3035 goto out;
3036 }
3037 }
3038 lock_buffer(bh);
3039 memcpy(bh->b_data+offset, data, len);
3040 flush_dcache_page(bh->b_page);
3041 unlock_buffer(bh);
3042 if (journal_quota)
3043 err = ext3_journal_dirty_metadata(handle, bh);
3044 else {
3045 /* Always do at least ordered writes for quotas */
3046 err = ext3_journal_dirty_data(handle, bh);
3047 mark_buffer_dirty(bh);
3048 }
3049 brelse(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050out:
Jan Kara905c3932012-04-25 21:24:30 +02003051 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 return err;
Dmitry Monakhove5472142010-02-16 19:33:42 +03003053 if (inode->i_size < off + len) {
3054 i_size_write(inode, off + len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 EXT3_I(inode)->i_disksize = inode->i_size;
3056 }
3057 inode->i_version++;
3058 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
3059 ext3_mark_inode_dirty(handle, inode);
Dmitry Monakhove5472142010-02-16 19:33:42 +03003060 return len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063#endif
3064
Al Viro152a0832010-07-25 00:46:55 +04003065static struct dentry *ext3_mount(struct file_system_type *fs_type,
3066 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067{
Al Viro152a0832010-07-25 00:46:55 +04003068 return mount_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069}
3070
3071static struct file_system_type ext3_fs_type = {
3072 .owner = THIS_MODULE,
3073 .name = "ext3",
Al Viro152a0832010-07-25 00:46:55 +04003074 .mount = ext3_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 .kill_sb = kill_block_super,
3076 .fs_flags = FS_REQUIRES_DEV,
3077};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08003078MODULE_ALIAS_FS("ext3");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079
3080static int __init init_ext3_fs(void)
3081{
3082 int err = init_ext3_xattr();
3083 if (err)
3084 return err;
3085 err = init_inodecache();
3086 if (err)
3087 goto out1;
3088 err = register_filesystem(&ext3_fs_type);
3089 if (err)
3090 goto out;
3091 return 0;
3092out:
3093 destroy_inodecache();
3094out1:
Dave Kleikampe9ad5622006-09-27 01:49:35 -07003095 exit_ext3_xattr();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return err;
3097}
3098
3099static void __exit exit_ext3_fs(void)
3100{
3101 unregister_filesystem(&ext3_fs_type);
3102 destroy_inodecache();
3103 exit_ext3_xattr();
3104}
3105
3106MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
3107MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
3108MODULE_LICENSE("GPL");
3109module_init(init_ext3_fs)
3110module_exit(exit_ext3_fs)