]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/ext4/super.c
ext4: Fix spelling of CONTIG_FS_EXT3 to CONFIG_FS_EXT3
[thirdparty/kernel/linux.git] / fs / ext4 / super.c
CommitLineData
ac27a0ec 1/*
617ba13b 2 * linux/fs/ext4/super.c
ac27a0ec
DK
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
19#include <linux/module.h>
20#include <linux/string.h>
21#include <linux/fs.h>
22#include <linux/time.h>
c5ca7c76 23#include <linux/vmalloc.h>
dab291af 24#include <linux/jbd2.h>
ac27a0ec
DK
25#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/blkdev.h>
28#include <linux/parser.h>
29#include <linux/smp_lock.h>
30#include <linux/buffer_head.h>
a5694255 31#include <linux/exportfs.h>
ac27a0ec
DK
32#include <linux/vfs.h>
33#include <linux/random.h>
34#include <linux/mount.h>
35#include <linux/namei.h>
36#include <linux/quotaops.h>
37#include <linux/seq_file.h>
9f6200bb 38#include <linux/proc_fs.h>
3197ebdb 39#include <linux/ctype.h>
1330593e 40#include <linux/log2.h>
717d50e4 41#include <linux/crc16.h>
ac27a0ec
DK
42#include <asm/uaccess.h>
43
3dcf5451
CH
44#include "ext4.h"
45#include "ext4_jbd2.h"
ac27a0ec
DK
46#include "xattr.h"
47#include "acl.h"
3661d286 48#include "mballoc.h"
ac27a0ec 49
9bffad1e
TT
50#define CREATE_TRACE_POINTS
51#include <trace/events/ext4.h>
52
9f6200bb 53struct proc_dir_entry *ext4_proc_root;
3197ebdb 54static struct kset *ext4_kset;
9f6200bb 55
617ba13b 56static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 57 unsigned long journal_devnum);
e2d67052 58static int ext4_commit_super(struct super_block *sb, int sync);
2b2d6d01
TT
59static void ext4_mark_recovery_complete(struct super_block *sb,
60 struct ext4_super_block *es);
61static void ext4_clear_journal_err(struct super_block *sb,
62 struct ext4_super_block *es);
617ba13b 63static int ext4_sync_fs(struct super_block *sb, int wait);
2b2d6d01 64static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec 65 char nbuf[16]);
2b2d6d01
TT
66static int ext4_remount(struct super_block *sb, int *flags, char *data);
67static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
c4be0c1d 68static int ext4_unfreeze(struct super_block *sb);
2b2d6d01 69static void ext4_write_super(struct super_block *sb);
c4be0c1d 70static int ext4_freeze(struct super_block *sb);
ac27a0ec 71
bd81d8ee 72
8fadc143
AR
73ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
74 struct ext4_group_desc *bg)
bd81d8ee 75{
3a14589c 76 return le32_to_cpu(bg->bg_block_bitmap_lo) |
8fadc143 77 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 78 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
bd81d8ee
LV
79}
80
8fadc143
AR
81ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
82 struct ext4_group_desc *bg)
bd81d8ee 83{
5272f837 84 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
8fadc143 85 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 86 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
bd81d8ee
LV
87}
88
8fadc143
AR
89ext4_fsblk_t ext4_inode_table(struct super_block *sb,
90 struct ext4_group_desc *bg)
bd81d8ee 91{
5272f837 92 return le32_to_cpu(bg->bg_inode_table_lo) |
8fadc143 93 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 94 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
bd81d8ee
LV
95}
96
560671a0
AK
97__u32 ext4_free_blks_count(struct super_block *sb,
98 struct ext4_group_desc *bg)
99{
100 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
101 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 102 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
560671a0
AK
103}
104
105__u32 ext4_free_inodes_count(struct super_block *sb,
106 struct ext4_group_desc *bg)
107{
108 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
109 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 110 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
560671a0
AK
111}
112
113__u32 ext4_used_dirs_count(struct super_block *sb,
114 struct ext4_group_desc *bg)
115{
116 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
117 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 118 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
560671a0
AK
119}
120
121__u32 ext4_itable_unused_count(struct super_block *sb,
122 struct ext4_group_desc *bg)
123{
124 return le16_to_cpu(bg->bg_itable_unused_lo) |
125 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 126 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
560671a0
AK
127}
128
8fadc143
AR
129void ext4_block_bitmap_set(struct super_block *sb,
130 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 131{
3a14589c 132 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
133 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
134 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
135}
136
8fadc143
AR
137void ext4_inode_bitmap_set(struct super_block *sb,
138 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 139{
5272f837 140 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
141 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
142 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
143}
144
8fadc143
AR
145void ext4_inode_table_set(struct super_block *sb,
146 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 147{
5272f837 148 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
8fadc143
AR
149 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
150 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
151}
152
560671a0
AK
153void ext4_free_blks_set(struct super_block *sb,
154 struct ext4_group_desc *bg, __u32 count)
155{
156 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
157 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
158 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
159}
160
161void ext4_free_inodes_set(struct super_block *sb,
162 struct ext4_group_desc *bg, __u32 count)
163{
164 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
165 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
166 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
167}
168
169void ext4_used_dirs_set(struct super_block *sb,
170 struct ext4_group_desc *bg, __u32 count)
171{
172 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
173 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
174 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
175}
176
177void ext4_itable_unused_set(struct super_block *sb,
178 struct ext4_group_desc *bg, __u32 count)
179{
180 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
181 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
182 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
183}
184
d3d1faf6
CW
185
186/* Just increment the non-pointer handle value */
187static handle_t *ext4_get_nojournal(void)
188{
189 handle_t *handle = current->journal_info;
190 unsigned long ref_cnt = (unsigned long)handle;
191
192 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
193
194 ref_cnt++;
195 handle = (handle_t *)ref_cnt;
196
197 current->journal_info = handle;
198 return handle;
199}
200
201
202/* Decrement the non-pointer handle value */
203static void ext4_put_nojournal(handle_t *handle)
204{
205 unsigned long ref_cnt = (unsigned long)handle;
206
207 BUG_ON(ref_cnt == 0);
208
209 ref_cnt--;
210 handle = (handle_t *)ref_cnt;
211
212 current->journal_info = handle;
213}
214
ac27a0ec 215/*
dab291af 216 * Wrappers for jbd2_journal_start/end.
ac27a0ec
DK
217 *
218 * The only special thing we need to do here is to make sure that all
219 * journal_end calls result in the superblock being marked dirty, so
220 * that sync() will call the filesystem's write_super callback if
221 * appropriate.
222 */
617ba13b 223handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
ac27a0ec
DK
224{
225 journal_t *journal;
226
227 if (sb->s_flags & MS_RDONLY)
228 return ERR_PTR(-EROFS);
229
230 /* Special case here: if the journal has aborted behind our
231 * backs (eg. EIO in the commit thread), then we still need to
232 * take the FS itself readonly cleanly. */
617ba13b 233 journal = EXT4_SB(sb)->s_journal;
0390131b
FM
234 if (journal) {
235 if (is_journal_aborted(journal)) {
0b8e58a1 236 ext4_abort(sb, __func__, "Detected aborted journal");
0390131b
FM
237 return ERR_PTR(-EROFS);
238 }
239 return jbd2_journal_start(journal, nblocks);
ac27a0ec 240 }
d3d1faf6 241 return ext4_get_nojournal();
ac27a0ec
DK
242}
243
244/*
245 * The only special thing we need to do here is to make sure that all
dab291af 246 * jbd2_journal_stop calls result in the superblock being marked dirty, so
ac27a0ec
DK
247 * that sync() will call the filesystem's write_super callback if
248 * appropriate.
249 */
617ba13b 250int __ext4_journal_stop(const char *where, handle_t *handle)
ac27a0ec
DK
251{
252 struct super_block *sb;
253 int err;
254 int rc;
255
0390131b 256 if (!ext4_handle_valid(handle)) {
d3d1faf6 257 ext4_put_nojournal(handle);
0390131b
FM
258 return 0;
259 }
ac27a0ec
DK
260 sb = handle->h_transaction->t_journal->j_private;
261 err = handle->h_err;
dab291af 262 rc = jbd2_journal_stop(handle);
ac27a0ec
DK
263
264 if (!err)
265 err = rc;
266 if (err)
617ba13b 267 __ext4_std_error(sb, where, err);
ac27a0ec
DK
268 return err;
269}
270
617ba13b 271void ext4_journal_abort_handle(const char *caller, const char *err_fn,
ac27a0ec
DK
272 struct buffer_head *bh, handle_t *handle, int err)
273{
274 char nbuf[16];
617ba13b 275 const char *errstr = ext4_decode_error(NULL, err, nbuf);
ac27a0ec 276
0390131b
FM
277 BUG_ON(!ext4_handle_valid(handle));
278
ac27a0ec
DK
279 if (bh)
280 BUFFER_TRACE(bh, "abort");
281
282 if (!handle->h_err)
283 handle->h_err = err;
284
285 if (is_handle_aborted(handle))
286 return;
287
288 printk(KERN_ERR "%s: aborting transaction: %s in %s\n",
289 caller, errstr, err_fn);
290
dab291af 291 jbd2_journal_abort_handle(handle);
ac27a0ec
DK
292}
293
294/* Deal with the reporting of failure conditions on a filesystem such as
295 * inconsistencies detected or read IO failures.
296 *
297 * On ext2, we can store the error state of the filesystem in the
617ba13b 298 * superblock. That is not possible on ext4, because we may have other
ac27a0ec
DK
299 * write ordering constraints on the superblock which prevent us from
300 * writing it out straight away; and given that the journal is about to
301 * be aborted, we can't rely on the current, or future, transactions to
302 * write out the superblock safely.
303 *
dab291af 304 * We'll just use the jbd2_journal_abort() error code to record an error in
ac27a0ec
DK
305 * the journal instead. On recovery, the journal will compain about
306 * that error until we've noted it down and cleared it.
307 */
308
617ba13b 309static void ext4_handle_error(struct super_block *sb)
ac27a0ec 310{
617ba13b 311 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 312
617ba13b
MC
313 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
314 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
ac27a0ec
DK
315
316 if (sb->s_flags & MS_RDONLY)
317 return;
318
2b2d6d01 319 if (!test_opt(sb, ERRORS_CONT)) {
617ba13b 320 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 321
4ab2f15b 322 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
ac27a0ec 323 if (journal)
dab291af 324 jbd2_journal_abort(journal, -EIO);
ac27a0ec 325 }
2b2d6d01 326 if (test_opt(sb, ERRORS_RO)) {
b31e1552 327 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
ac27a0ec
DK
328 sb->s_flags |= MS_RDONLY;
329 }
e2d67052 330 ext4_commit_super(sb, 1);
ac27a0ec 331 if (test_opt(sb, ERRORS_PANIC))
617ba13b 332 panic("EXT4-fs (device %s): panic forced after error\n",
ac27a0ec
DK
333 sb->s_id);
334}
335
12062ddd 336void __ext4_error(struct super_block *sb, const char *function,
2b2d6d01 337 const char *fmt, ...)
ac27a0ec
DK
338{
339 va_list args;
340
341 va_start(args, fmt);
2b2d6d01 342 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
ac27a0ec
DK
343 vprintk(fmt, args);
344 printk("\n");
345 va_end(args);
346
617ba13b 347 ext4_handle_error(sb);
ac27a0ec
DK
348}
349
273df556
FM
350void ext4_error_inode(const char *function, struct inode *inode,
351 const char *fmt, ...)
352{
353 va_list args;
354
355 va_start(args, fmt);
356 printk(KERN_CRIT "EXT4-fs error (device %s): %s: inode #%lu: (comm %s) ",
357 inode->i_sb->s_id, function, inode->i_ino, current->comm);
358 vprintk(fmt, args);
359 printk("\n");
360 va_end(args);
361
362 ext4_handle_error(inode->i_sb);
363}
364
365void ext4_error_file(const char *function, struct file *file,
366 const char *fmt, ...)
367{
368 va_list args;
369 struct inode *inode = file->f_dentry->d_inode;
370 char pathname[80], *path;
371
372 va_start(args, fmt);
373 path = d_path(&(file->f_path), pathname, sizeof(pathname));
374 if (!path)
375 path = "(unknown)";
376 printk(KERN_CRIT
377 "EXT4-fs error (device %s): %s: inode #%lu (comm %s path %s): ",
378 inode->i_sb->s_id, function, inode->i_ino, current->comm, path);
379 vprintk(fmt, args);
380 printk("\n");
381 va_end(args);
382
383 ext4_handle_error(inode->i_sb);
384}
385
2b2d6d01 386static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec
DK
387 char nbuf[16])
388{
389 char *errstr = NULL;
390
391 switch (errno) {
392 case -EIO:
393 errstr = "IO failure";
394 break;
395 case -ENOMEM:
396 errstr = "Out of memory";
397 break;
398 case -EROFS:
78f1ddbb
TT
399 if (!sb || (EXT4_SB(sb)->s_journal &&
400 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
ac27a0ec
DK
401 errstr = "Journal has aborted";
402 else
403 errstr = "Readonly filesystem";
404 break;
405 default:
406 /* If the caller passed in an extra buffer for unknown
407 * errors, textualise them now. Else we just return
408 * NULL. */
409 if (nbuf) {
410 /* Check for truncated error codes... */
411 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
412 errstr = nbuf;
413 }
414 break;
415 }
416
417 return errstr;
418}
419
617ba13b 420/* __ext4_std_error decodes expected errors from journaling functions
ac27a0ec
DK
421 * automatically and invokes the appropriate error response. */
422
2b2d6d01 423void __ext4_std_error(struct super_block *sb, const char *function, int errno)
ac27a0ec
DK
424{
425 char nbuf[16];
426 const char *errstr;
427
428 /* Special case: if the error is EROFS, and we're not already
429 * inside a transaction, then there's really no point in logging
430 * an error. */
431 if (errno == -EROFS && journal_current_handle() == NULL &&
432 (sb->s_flags & MS_RDONLY))
433 return;
434
617ba13b 435 errstr = ext4_decode_error(sb, errno, nbuf);
2b2d6d01
TT
436 printk(KERN_CRIT "EXT4-fs error (device %s) in %s: %s\n",
437 sb->s_id, function, errstr);
ac27a0ec 438
617ba13b 439 ext4_handle_error(sb);
ac27a0ec
DK
440}
441
442/*
617ba13b 443 * ext4_abort is a much stronger failure handler than ext4_error. The
ac27a0ec
DK
444 * abort function may be used to deal with unrecoverable failures such
445 * as journal IO errors or ENOMEM at a critical moment in log management.
446 *
447 * We unconditionally force the filesystem into an ABORT|READONLY state,
448 * unless the error response on the fs has been set to panic in which
449 * case we take the easy way out and panic immediately.
450 */
451
2b2d6d01
TT
452void ext4_abort(struct super_block *sb, const char *function,
453 const char *fmt, ...)
ac27a0ec
DK
454{
455 va_list args;
456
ac27a0ec 457 va_start(args, fmt);
2b2d6d01 458 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
ac27a0ec
DK
459 vprintk(fmt, args);
460 printk("\n");
461 va_end(args);
462
463 if (test_opt(sb, ERRORS_PANIC))
617ba13b 464 panic("EXT4-fs panic from previous error\n");
ac27a0ec
DK
465
466 if (sb->s_flags & MS_RDONLY)
467 return;
468
b31e1552 469 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
617ba13b 470 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
ac27a0ec 471 sb->s_flags |= MS_RDONLY;
4ab2f15b 472 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
ef2cabf7
HK
473 if (EXT4_SB(sb)->s_journal)
474 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
ac27a0ec
DK
475}
476
b31e1552
ES
477void ext4_msg (struct super_block * sb, const char *prefix,
478 const char *fmt, ...)
479{
480 va_list args;
481
482 va_start(args, fmt);
483 printk("%sEXT4-fs (%s): ", prefix, sb->s_id);
484 vprintk(fmt, args);
485 printk("\n");
486 va_end(args);
487}
488
12062ddd 489void __ext4_warning(struct super_block *sb, const char *function,
2b2d6d01 490 const char *fmt, ...)
ac27a0ec
DK
491{
492 va_list args;
493
494 va_start(args, fmt);
617ba13b 495 printk(KERN_WARNING "EXT4-fs warning (device %s): %s: ",
ac27a0ec
DK
496 sb->s_id, function);
497 vprintk(fmt, args);
498 printk("\n");
499 va_end(args);
500}
501
5d1b1b3f 502void ext4_grp_locked_error(struct super_block *sb, ext4_group_t grp,
0b8e58a1 503 const char *function, const char *fmt, ...)
5d1b1b3f
AK
504__releases(bitlock)
505__acquires(bitlock)
506{
507 va_list args;
508 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
509
510 va_start(args, fmt);
511 printk(KERN_CRIT "EXT4-fs error (device %s): %s: ", sb->s_id, function);
512 vprintk(fmt, args);
513 printk("\n");
514 va_end(args);
515
516 if (test_opt(sb, ERRORS_CONT)) {
517 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
518 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
e2d67052 519 ext4_commit_super(sb, 0);
5d1b1b3f
AK
520 return;
521 }
522 ext4_unlock_group(sb, grp);
523 ext4_handle_error(sb);
524 /*
525 * We only get here in the ERRORS_RO case; relocking the group
526 * may be dangerous, but nothing bad will happen since the
527 * filesystem will have already been marked read/only and the
528 * journal has been aborted. We return 1 as a hint to callers
529 * who might what to use the return value from
530 * ext4_grp_locked_error() to distinguish beween the
531 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
532 * aggressively from the ext4 function in question, with a
533 * more appropriate error code.
534 */
535 ext4_lock_group(sb, grp);
536 return;
537}
538
617ba13b 539void ext4_update_dynamic_rev(struct super_block *sb)
ac27a0ec 540{
617ba13b 541 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 542
617ba13b 543 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
ac27a0ec
DK
544 return;
545
12062ddd 546 ext4_warning(sb,
ac27a0ec
DK
547 "updating to rev %d because of new feature flag, "
548 "running e2fsck is recommended",
617ba13b 549 EXT4_DYNAMIC_REV);
ac27a0ec 550
617ba13b
MC
551 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
552 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
553 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
ac27a0ec
DK
554 /* leave es->s_feature_*compat flags alone */
555 /* es->s_uuid will be set by e2fsck if empty */
556
557 /*
558 * The rest of the superblock fields should be zero, and if not it
559 * means they are likely already in use, so leave them alone. We
560 * can leave it up to e2fsck to clean up any inconsistencies there.
561 */
562}
563
564/*
565 * Open the external journal device
566 */
b31e1552 567static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
ac27a0ec
DK
568{
569 struct block_device *bdev;
570 char b[BDEVNAME_SIZE];
571
572 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
573 if (IS_ERR(bdev))
574 goto fail;
575 return bdev;
576
577fail:
b31e1552 578 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
ac27a0ec
DK
579 __bdevname(dev, b), PTR_ERR(bdev));
580 return NULL;
581}
582
583/*
584 * Release the journal device
585 */
617ba13b 586static int ext4_blkdev_put(struct block_device *bdev)
ac27a0ec
DK
587{
588 bd_release(bdev);
9a1c3542 589 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
ac27a0ec
DK
590}
591
617ba13b 592static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
ac27a0ec
DK
593{
594 struct block_device *bdev;
595 int ret = -ENODEV;
596
597 bdev = sbi->journal_bdev;
598 if (bdev) {
617ba13b 599 ret = ext4_blkdev_put(bdev);
ac27a0ec
DK
600 sbi->journal_bdev = NULL;
601 }
602 return ret;
603}
604
605static inline struct inode *orphan_list_entry(struct list_head *l)
606{
617ba13b 607 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec
DK
608}
609
617ba13b 610static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
ac27a0ec
DK
611{
612 struct list_head *l;
613
b31e1552
ES
614 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
615 le32_to_cpu(sbi->s_es->s_last_orphan));
ac27a0ec
DK
616
617 printk(KERN_ERR "sb_info orphan list:\n");
618 list_for_each(l, &sbi->s_orphan) {
619 struct inode *inode = orphan_list_entry(l);
620 printk(KERN_ERR " "
621 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
622 inode->i_sb->s_id, inode->i_ino, inode,
623 inode->i_mode, inode->i_nlink,
624 NEXT_ORPHAN(inode));
625 }
626}
627
2b2d6d01 628static void ext4_put_super(struct super_block *sb)
ac27a0ec 629{
617ba13b
MC
630 struct ext4_sb_info *sbi = EXT4_SB(sb);
631 struct ext4_super_block *es = sbi->s_es;
ef2cabf7 632 int i, err;
ac27a0ec 633
4c0425ff
MC
634 flush_workqueue(sbi->dio_unwritten_wq);
635 destroy_workqueue(sbi->dio_unwritten_wq);
636
a9e220f8 637 lock_super(sb);
6cfd0148 638 lock_kernel();
8c85e125 639 if (sb->s_dirt)
ebc1ac16 640 ext4_commit_super(sb, 1);
8c85e125 641
0390131b
FM
642 if (sbi->s_journal) {
643 err = jbd2_journal_destroy(sbi->s_journal);
644 sbi->s_journal = NULL;
645 if (err < 0)
646 ext4_abort(sb, __func__,
647 "Couldn't clean up the journal");
648 }
d4edac31
JB
649
650 ext4_release_system_zone(sb);
651 ext4_mb_release(sb);
652 ext4_ext_release(sb);
653 ext4_xattr_put_super(sb);
654
ac27a0ec 655 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 656 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 657 es->s_state = cpu_to_le16(sbi->s_mount_state);
e2d67052 658 ext4_commit_super(sb, 1);
ac27a0ec 659 }
240799cd 660 if (sbi->s_proc) {
9f6200bb 661 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 662 }
3197ebdb 663 kobject_del(&sbi->s_kobj);
ac27a0ec
DK
664
665 for (i = 0; i < sbi->s_gdb_count; i++)
666 brelse(sbi->s_group_desc[i]);
667 kfree(sbi->s_group_desc);
c5ca7c76
TT
668 if (is_vmalloc_addr(sbi->s_flex_groups))
669 vfree(sbi->s_flex_groups);
670 else
671 kfree(sbi->s_flex_groups);
ac27a0ec
DK
672 percpu_counter_destroy(&sbi->s_freeblocks_counter);
673 percpu_counter_destroy(&sbi->s_freeinodes_counter);
674 percpu_counter_destroy(&sbi->s_dirs_counter);
6bc6e63f 675 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
ac27a0ec
DK
676 brelse(sbi->s_sbh);
677#ifdef CONFIG_QUOTA
678 for (i = 0; i < MAXQUOTAS; i++)
679 kfree(sbi->s_qf_names[i]);
680#endif
681
682 /* Debugging code just in case the in-memory inode orphan list
683 * isn't empty. The on-disk one can be non-empty if we've
684 * detected an error and taken the fs readonly, but the
685 * in-memory list had better be clean by this point. */
686 if (!list_empty(&sbi->s_orphan))
687 dump_orphan_list(sb, sbi);
688 J_ASSERT(list_empty(&sbi->s_orphan));
689
f98393a6 690 invalidate_bdev(sb->s_bdev);
ac27a0ec
DK
691 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
692 /*
693 * Invalidate the journal device's buffers. We don't want them
694 * floating about in memory - the physical journal device may
695 * hotswapped, and it breaks the `ro-after' testing code.
696 */
697 sync_blockdev(sbi->journal_bdev);
f98393a6 698 invalidate_bdev(sbi->journal_bdev);
617ba13b 699 ext4_blkdev_remove(sbi);
ac27a0ec
DK
700 }
701 sb->s_fs_info = NULL;
3197ebdb
TT
702 /*
703 * Now that we are completely done shutting down the
704 * superblock, we need to actually destroy the kobject.
705 */
706 unlock_kernel();
707 unlock_super(sb);
708 kobject_put(&sbi->s_kobj);
709 wait_for_completion(&sbi->s_kobj_unregister);
705895b6 710 kfree(sbi->s_blockgroup_lock);
ac27a0ec 711 kfree(sbi);
ac27a0ec
DK
712}
713
e18b890b 714static struct kmem_cache *ext4_inode_cachep;
ac27a0ec
DK
715
716/*
717 * Called inside transaction, so use GFP_NOFS
718 */
617ba13b 719static struct inode *ext4_alloc_inode(struct super_block *sb)
ac27a0ec 720{
617ba13b 721 struct ext4_inode_info *ei;
ac27a0ec 722
e6b4f8da 723 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
ac27a0ec
DK
724 if (!ei)
725 return NULL;
0b8e58a1 726
ac27a0ec 727 ei->vfs_inode.i_version = 1;
91246c00 728 ei->vfs_inode.i_data.writeback_index = 0;
a86c6181 729 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
c9de560d
AT
730 INIT_LIST_HEAD(&ei->i_prealloc_list);
731 spin_lock_init(&ei->i_prealloc_lock);
0390131b
FM
732 /*
733 * Note: We can be called before EXT4_SB(sb)->s_journal is set,
734 * therefore it can be null here. Don't check it, just initialize
735 * jinode.
736 */
678aaf48 737 jbd2_journal_init_jbd_inode(&ei->jinode, &ei->vfs_inode);
d2a17637
MC
738 ei->i_reserved_data_blocks = 0;
739 ei->i_reserved_meta_blocks = 0;
740 ei->i_allocated_meta_blocks = 0;
9d0be502 741 ei->i_da_metadata_calc_len = 0;
d2a17637
MC
742 ei->i_delalloc_reserved_flag = 0;
743 spin_lock_init(&(ei->i_block_reservation_lock));
a9e7f447
DM
744#ifdef CONFIG_QUOTA
745 ei->i_reserved_quota = 0;
746#endif
c7064ef1 747 INIT_LIST_HEAD(&ei->i_completed_io_list);
744692dc 748 spin_lock_init(&ei->i_completed_io_lock);
8d5d02e6 749 ei->cur_aio_dio = NULL;
b436b9be
JK
750 ei->i_sync_tid = 0;
751 ei->i_datasync_tid = 0;
0b8e58a1 752
ac27a0ec
DK
753 return &ei->vfs_inode;
754}
755
617ba13b 756static void ext4_destroy_inode(struct inode *inode)
ac27a0ec 757{
9f7dd93d 758 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
b31e1552
ES
759 ext4_msg(inode->i_sb, KERN_ERR,
760 "Inode %lu (%p): orphan list check failed!",
761 inode->i_ino, EXT4_I(inode));
9f7dd93d
VA
762 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
763 EXT4_I(inode), sizeof(struct ext4_inode_info),
764 true);
765 dump_stack();
766 }
617ba13b 767 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
ac27a0ec
DK
768}
769
51cc5068 770static void init_once(void *foo)
ac27a0ec 771{
617ba13b 772 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
ac27a0ec 773
a35afb83 774 INIT_LIST_HEAD(&ei->i_orphan);
03010a33 775#ifdef CONFIG_EXT4_FS_XATTR
a35afb83 776 init_rwsem(&ei->xattr_sem);
ac27a0ec 777#endif
0e855ac8 778 init_rwsem(&ei->i_data_sem);
a35afb83 779 inode_init_once(&ei->vfs_inode);
ac27a0ec
DK
780}
781
782static int init_inodecache(void)
783{
617ba13b
MC
784 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
785 sizeof(struct ext4_inode_info),
ac27a0ec
DK
786 0, (SLAB_RECLAIM_ACCOUNT|
787 SLAB_MEM_SPREAD),
20c2df83 788 init_once);
617ba13b 789 if (ext4_inode_cachep == NULL)
ac27a0ec
DK
790 return -ENOMEM;
791 return 0;
792}
793
794static void destroy_inodecache(void)
795{
617ba13b 796 kmem_cache_destroy(ext4_inode_cachep);
ac27a0ec
DK
797}
798
617ba13b 799static void ext4_clear_inode(struct inode *inode)
ac27a0ec 800{
9f754758 801 dquot_drop(inode);
c2ea3fde 802 ext4_discard_preallocations(inode);
0390131b
FM
803 if (EXT4_JOURNAL(inode))
804 jbd2_journal_release_jbd_inode(EXT4_SB(inode->i_sb)->s_journal,
678aaf48 805 &EXT4_I(inode)->jinode);
ac27a0ec
DK
806}
807
2b2d6d01
TT
808static inline void ext4_show_quota_options(struct seq_file *seq,
809 struct super_block *sb)
ac27a0ec
DK
810{
811#if defined(CONFIG_QUOTA)
617ba13b 812 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 813
5a20bdfc
JK
814 if (sbi->s_jquota_fmt) {
815 char *fmtname = "";
816
817 switch (sbi->s_jquota_fmt) {
818 case QFMT_VFS_OLD:
819 fmtname = "vfsold";
820 break;
821 case QFMT_VFS_V0:
822 fmtname = "vfsv0";
823 break;
824 case QFMT_VFS_V1:
825 fmtname = "vfsv1";
826 break;
827 }
828 seq_printf(seq, ",jqfmt=%s", fmtname);
829 }
ac27a0ec
DK
830
831 if (sbi->s_qf_names[USRQUOTA])
832 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
833
834 if (sbi->s_qf_names[GRPQUOTA])
835 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
836
482a7425 837 if (test_opt(sb, USRQUOTA))
ac27a0ec
DK
838 seq_puts(seq, ",usrquota");
839
482a7425 840 if (test_opt(sb, GRPQUOTA))
ac27a0ec
DK
841 seq_puts(seq, ",grpquota");
842#endif
843}
844
d9c9bef1
MS
845/*
846 * Show an option if
847 * - it's set to a non-default value OR
848 * - if the per-sb default is different from the global default
849 */
617ba13b 850static int ext4_show_options(struct seq_file *seq, struct vfsmount *vfs)
ac27a0ec 851{
aa22df2c
AK
852 int def_errors;
853 unsigned long def_mount_opts;
ac27a0ec 854 struct super_block *sb = vfs->mnt_sb;
d9c9bef1
MS
855 struct ext4_sb_info *sbi = EXT4_SB(sb);
856 struct ext4_super_block *es = sbi->s_es;
d9c9bef1
MS
857
858 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
aa22df2c 859 def_errors = le16_to_cpu(es->s_errors);
d9c9bef1
MS
860
861 if (sbi->s_sb_block != 1)
862 seq_printf(seq, ",sb=%llu", sbi->s_sb_block);
863 if (test_opt(sb, MINIX_DF))
864 seq_puts(seq, ",minixdf");
aa22df2c 865 if (test_opt(sb, GRPID) && !(def_mount_opts & EXT4_DEFM_BSDGROUPS))
d9c9bef1
MS
866 seq_puts(seq, ",grpid");
867 if (!test_opt(sb, GRPID) && (def_mount_opts & EXT4_DEFM_BSDGROUPS))
868 seq_puts(seq, ",nogrpid");
869 if (sbi->s_resuid != EXT4_DEF_RESUID ||
870 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID) {
871 seq_printf(seq, ",resuid=%u", sbi->s_resuid);
872 }
873 if (sbi->s_resgid != EXT4_DEF_RESGID ||
874 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID) {
875 seq_printf(seq, ",resgid=%u", sbi->s_resgid);
876 }
bb4f397a 877 if (test_opt(sb, ERRORS_RO)) {
d9c9bef1 878 if (def_errors == EXT4_ERRORS_PANIC ||
bb4f397a
AK
879 def_errors == EXT4_ERRORS_CONTINUE) {
880 seq_puts(seq, ",errors=remount-ro");
d9c9bef1
MS
881 }
882 }
aa22df2c 883 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
bb4f397a 884 seq_puts(seq, ",errors=continue");
aa22df2c 885 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
d9c9bef1 886 seq_puts(seq, ",errors=panic");
aa22df2c 887 if (test_opt(sb, NO_UID32) && !(def_mount_opts & EXT4_DEFM_UID16))
d9c9bef1 888 seq_puts(seq, ",nouid32");
aa22df2c 889 if (test_opt(sb, DEBUG) && !(def_mount_opts & EXT4_DEFM_DEBUG))
d9c9bef1
MS
890 seq_puts(seq, ",debug");
891 if (test_opt(sb, OLDALLOC))
892 seq_puts(seq, ",oldalloc");
03010a33 893#ifdef CONFIG_EXT4_FS_XATTR
aa22df2c
AK
894 if (test_opt(sb, XATTR_USER) &&
895 !(def_mount_opts & EXT4_DEFM_XATTR_USER))
d9c9bef1
MS
896 seq_puts(seq, ",user_xattr");
897 if (!test_opt(sb, XATTR_USER) &&
898 (def_mount_opts & EXT4_DEFM_XATTR_USER)) {
899 seq_puts(seq, ",nouser_xattr");
900 }
901#endif
03010a33 902#ifdef CONFIG_EXT4_FS_POSIX_ACL
aa22df2c 903 if (test_opt(sb, POSIX_ACL) && !(def_mount_opts & EXT4_DEFM_ACL))
d9c9bef1
MS
904 seq_puts(seq, ",acl");
905 if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT4_DEFM_ACL))
906 seq_puts(seq, ",noacl");
907#endif
30773840 908 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
d9c9bef1
MS
909 seq_printf(seq, ",commit=%u",
910 (unsigned) (sbi->s_commit_interval / HZ));
911 }
30773840
TT
912 if (sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME) {
913 seq_printf(seq, ",min_batch_time=%u",
914 (unsigned) sbi->s_min_batch_time);
915 }
916 if (sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME) {
917 seq_printf(seq, ",max_batch_time=%u",
918 (unsigned) sbi->s_min_batch_time);
919 }
920
571640ca
ES
921 /*
922 * We're changing the default of barrier mount option, so
923 * let's always display its mount state so it's clear what its
924 * status is.
925 */
926 seq_puts(seq, ",barrier=");
927 seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
cd0b6a39
TT
928 if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
929 seq_puts(seq, ",journal_async_commit");
d9c9bef1
MS
930 if (test_opt(sb, NOBH))
931 seq_puts(seq, ",nobh");
25ec56b5
JNC
932 if (test_opt(sb, I_VERSION))
933 seq_puts(seq, ",i_version");
dd919b98
AK
934 if (!test_opt(sb, DELALLOC))
935 seq_puts(seq, ",nodelalloc");
936
ac27a0ec 937
cb45bbe4
MS
938 if (sbi->s_stripe)
939 seq_printf(seq, ",stripe=%lu", sbi->s_stripe);
aa22df2c
AK
940 /*
941 * journal mode get enabled in different ways
942 * So just print the value even if we didn't specify it
943 */
617ba13b 944 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
ac27a0ec 945 seq_puts(seq, ",data=journal");
617ba13b 946 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
ac27a0ec 947 seq_puts(seq, ",data=ordered");
617ba13b 948 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
ac27a0ec
DK
949 seq_puts(seq, ",data=writeback");
950
240799cd
TT
951 if (sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
952 seq_printf(seq, ",inode_readahead_blks=%u",
953 sbi->s_inode_readahead_blks);
954
5bf5683a
HK
955 if (test_opt(sb, DATA_ERR_ABORT))
956 seq_puts(seq, ",data_err=abort");
957
afd4672d 958 if (test_opt(sb, NO_AUTO_DA_ALLOC))
06705bff 959 seq_puts(seq, ",noauto_da_alloc");
afd4672d 960
5328e635
ES
961 if (test_opt(sb, DISCARD))
962 seq_puts(seq, ",discard");
963
e3bb52ae
ES
964 if (test_opt(sb, NOLOAD))
965 seq_puts(seq, ",norecovery");
966
744692dc
JZ
967 if (test_opt(sb, DIOREAD_NOLOCK))
968 seq_puts(seq, ",dioread_nolock");
969
617ba13b 970 ext4_show_quota_options(seq, sb);
0b8e58a1 971
ac27a0ec
DK
972 return 0;
973}
974
1b961ac0 975static struct inode *ext4_nfs_get_inode(struct super_block *sb,
0b8e58a1 976 u64 ino, u32 generation)
ac27a0ec 977{
ac27a0ec 978 struct inode *inode;
ac27a0ec 979
617ba13b 980 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
ac27a0ec 981 return ERR_PTR(-ESTALE);
617ba13b 982 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
ac27a0ec
DK
983 return ERR_PTR(-ESTALE);
984
985 /* iget isn't really right if the inode is currently unallocated!!
986 *
617ba13b 987 * ext4_read_inode will return a bad_inode if the inode had been
ac27a0ec
DK
988 * deleted, so we should be safe.
989 *
990 * Currently we don't know the generation for parent directory, so
991 * a generation of 0 means "accept any"
992 */
1d1fe1ee
DH
993 inode = ext4_iget(sb, ino);
994 if (IS_ERR(inode))
995 return ERR_CAST(inode);
996 if (generation && inode->i_generation != generation) {
ac27a0ec
DK
997 iput(inode);
998 return ERR_PTR(-ESTALE);
999 }
1b961ac0
CH
1000
1001 return inode;
1002}
1003
1004static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
0b8e58a1 1005 int fh_len, int fh_type)
1b961ac0
CH
1006{
1007 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1008 ext4_nfs_get_inode);
1009}
1010
1011static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
0b8e58a1 1012 int fh_len, int fh_type)
1b961ac0
CH
1013{
1014 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1015 ext4_nfs_get_inode);
ac27a0ec
DK
1016}
1017
c39a7f84
TO
1018/*
1019 * Try to release metadata pages (indirect blocks, directories) which are
1020 * mapped via the block device. Since these pages could have journal heads
1021 * which would prevent try_to_free_buffers() from freeing them, we must use
1022 * jbd2 layer's try_to_free_buffers() function to release them.
1023 */
0b8e58a1
AD
1024static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1025 gfp_t wait)
c39a7f84
TO
1026{
1027 journal_t *journal = EXT4_SB(sb)->s_journal;
1028
1029 WARN_ON(PageChecked(page));
1030 if (!page_has_buffers(page))
1031 return 0;
1032 if (journal)
1033 return jbd2_journal_try_to_free_buffers(journal, page,
1034 wait & ~__GFP_WAIT);
1035 return try_to_free_buffers(page);
1036}
1037
ac27a0ec 1038#ifdef CONFIG_QUOTA
af5bc92d 1039#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
2b2d6d01 1040#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
ac27a0ec 1041
617ba13b
MC
1042static int ext4_write_dquot(struct dquot *dquot);
1043static int ext4_acquire_dquot(struct dquot *dquot);
1044static int ext4_release_dquot(struct dquot *dquot);
1045static int ext4_mark_dquot_dirty(struct dquot *dquot);
1046static int ext4_write_info(struct super_block *sb, int type);
6f28e087
JK
1047static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1048 char *path, int remount);
617ba13b
MC
1049static int ext4_quota_on_mount(struct super_block *sb, int type);
1050static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec 1051 size_t len, loff_t off);
617ba13b 1052static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
1053 const char *data, size_t len, loff_t off);
1054
61e225dc 1055static const struct dquot_operations ext4_quota_operations = {
a9e7f447 1056#ifdef CONFIG_QUOTA
60e58e0f 1057 .get_reserved_space = ext4_get_reserved_space,
a9e7f447 1058#endif
617ba13b
MC
1059 .write_dquot = ext4_write_dquot,
1060 .acquire_dquot = ext4_acquire_dquot,
1061 .release_dquot = ext4_release_dquot,
1062 .mark_dirty = ext4_mark_dquot_dirty,
a5b5ee32
JK
1063 .write_info = ext4_write_info,
1064 .alloc_dquot = dquot_alloc,
1065 .destroy_dquot = dquot_destroy,
ac27a0ec
DK
1066};
1067
0d54b217 1068static const struct quotactl_ops ext4_qctl_operations = {
617ba13b 1069 .quota_on = ext4_quota_on,
ac27a0ec
DK
1070 .quota_off = vfs_quota_off,
1071 .quota_sync = vfs_quota_sync,
1072 .get_info = vfs_get_dqinfo,
1073 .set_info = vfs_set_dqinfo,
1074 .get_dqblk = vfs_get_dqblk,
1075 .set_dqblk = vfs_set_dqblk
1076};
1077#endif
1078
ee9b6d61 1079static const struct super_operations ext4_sops = {
617ba13b
MC
1080 .alloc_inode = ext4_alloc_inode,
1081 .destroy_inode = ext4_destroy_inode,
617ba13b
MC
1082 .write_inode = ext4_write_inode,
1083 .dirty_inode = ext4_dirty_inode,
1084 .delete_inode = ext4_delete_inode,
1085 .put_super = ext4_put_super,
617ba13b 1086 .sync_fs = ext4_sync_fs,
c4be0c1d
TS
1087 .freeze_fs = ext4_freeze,
1088 .unfreeze_fs = ext4_unfreeze,
617ba13b
MC
1089 .statfs = ext4_statfs,
1090 .remount_fs = ext4_remount,
1091 .clear_inode = ext4_clear_inode,
1092 .show_options = ext4_show_options,
ac27a0ec 1093#ifdef CONFIG_QUOTA
617ba13b
MC
1094 .quota_read = ext4_quota_read,
1095 .quota_write = ext4_quota_write,
ac27a0ec 1096#endif
c39a7f84 1097 .bdev_try_to_free_page = bdev_try_to_free_page,
ac27a0ec
DK
1098};
1099
9ca92389
TT
1100static const struct super_operations ext4_nojournal_sops = {
1101 .alloc_inode = ext4_alloc_inode,
1102 .destroy_inode = ext4_destroy_inode,
1103 .write_inode = ext4_write_inode,
1104 .dirty_inode = ext4_dirty_inode,
1105 .delete_inode = ext4_delete_inode,
1106 .write_super = ext4_write_super,
1107 .put_super = ext4_put_super,
1108 .statfs = ext4_statfs,
1109 .remount_fs = ext4_remount,
1110 .clear_inode = ext4_clear_inode,
1111 .show_options = ext4_show_options,
1112#ifdef CONFIG_QUOTA
1113 .quota_read = ext4_quota_read,
1114 .quota_write = ext4_quota_write,
1115#endif
1116 .bdev_try_to_free_page = bdev_try_to_free_page,
1117};
1118
39655164 1119static const struct export_operations ext4_export_ops = {
1b961ac0
CH
1120 .fh_to_dentry = ext4_fh_to_dentry,
1121 .fh_to_parent = ext4_fh_to_parent,
617ba13b 1122 .get_parent = ext4_get_parent,
ac27a0ec
DK
1123};
1124
1125enum {
1126 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1127 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
01436ef2 1128 Opt_nouid32, Opt_debug, Opt_oldalloc, Opt_orlov,
ac27a0ec 1129 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
06705bff 1130 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload, Opt_nobh, Opt_bh,
30773840 1131 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
c3191067 1132 Opt_journal_update, Opt_journal_dev,
818d276c 1133 Opt_journal_checksum, Opt_journal_async_commit,
ac27a0ec 1134 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
296c355c 1135 Opt_data_err_abort, Opt_data_err_ignore,
ac27a0ec 1136 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
5a20bdfc
JK
1137 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1138 Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
1139 Opt_resize, Opt_usrquota, Opt_grpquota, Opt_i_version,
01436ef2 1140 Opt_stripe, Opt_delalloc, Opt_nodelalloc,
6fd058f7 1141 Opt_block_validity, Opt_noblock_validity,
5328e635 1142 Opt_inode_readahead_blks, Opt_journal_ioprio,
744692dc 1143 Opt_dioread_nolock, Opt_dioread_lock,
5328e635 1144 Opt_discard, Opt_nodiscard,
ac27a0ec
DK
1145};
1146
a447c093 1147static const match_table_t tokens = {
ac27a0ec
DK
1148 {Opt_bsd_df, "bsddf"},
1149 {Opt_minix_df, "minixdf"},
1150 {Opt_grpid, "grpid"},
1151 {Opt_grpid, "bsdgroups"},
1152 {Opt_nogrpid, "nogrpid"},
1153 {Opt_nogrpid, "sysvgroups"},
1154 {Opt_resgid, "resgid=%u"},
1155 {Opt_resuid, "resuid=%u"},
1156 {Opt_sb, "sb=%u"},
1157 {Opt_err_cont, "errors=continue"},
1158 {Opt_err_panic, "errors=panic"},
1159 {Opt_err_ro, "errors=remount-ro"},
1160 {Opt_nouid32, "nouid32"},
ac27a0ec
DK
1161 {Opt_debug, "debug"},
1162 {Opt_oldalloc, "oldalloc"},
1163 {Opt_orlov, "orlov"},
1164 {Opt_user_xattr, "user_xattr"},
1165 {Opt_nouser_xattr, "nouser_xattr"},
1166 {Opt_acl, "acl"},
1167 {Opt_noacl, "noacl"},
ac27a0ec 1168 {Opt_noload, "noload"},
e3bb52ae 1169 {Opt_noload, "norecovery"},
ac27a0ec
DK
1170 {Opt_nobh, "nobh"},
1171 {Opt_bh, "bh"},
1172 {Opt_commit, "commit=%u"},
30773840
TT
1173 {Opt_min_batch_time, "min_batch_time=%u"},
1174 {Opt_max_batch_time, "max_batch_time=%u"},
ac27a0ec 1175 {Opt_journal_update, "journal=update"},
ac27a0ec 1176 {Opt_journal_dev, "journal_dev=%u"},
818d276c
GS
1177 {Opt_journal_checksum, "journal_checksum"},
1178 {Opt_journal_async_commit, "journal_async_commit"},
ac27a0ec
DK
1179 {Opt_abort, "abort"},
1180 {Opt_data_journal, "data=journal"},
1181 {Opt_data_ordered, "data=ordered"},
1182 {Opt_data_writeback, "data=writeback"},
5bf5683a
HK
1183 {Opt_data_err_abort, "data_err=abort"},
1184 {Opt_data_err_ignore, "data_err=ignore"},
ac27a0ec
DK
1185 {Opt_offusrjquota, "usrjquota="},
1186 {Opt_usrjquota, "usrjquota=%s"},
1187 {Opt_offgrpjquota, "grpjquota="},
1188 {Opt_grpjquota, "grpjquota=%s"},
1189 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1190 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
5a20bdfc 1191 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
ac27a0ec
DK
1192 {Opt_grpquota, "grpquota"},
1193 {Opt_noquota, "noquota"},
1194 {Opt_quota, "quota"},
1195 {Opt_usrquota, "usrquota"},
1196 {Opt_barrier, "barrier=%u"},
06705bff
TT
1197 {Opt_barrier, "barrier"},
1198 {Opt_nobarrier, "nobarrier"},
25ec56b5 1199 {Opt_i_version, "i_version"},
c9de560d 1200 {Opt_stripe, "stripe=%u"},
ac27a0ec 1201 {Opt_resize, "resize"},
64769240 1202 {Opt_delalloc, "delalloc"},
dd919b98 1203 {Opt_nodelalloc, "nodelalloc"},
6fd058f7
TT
1204 {Opt_block_validity, "block_validity"},
1205 {Opt_noblock_validity, "noblock_validity"},
240799cd 1206 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
b3881f74 1207 {Opt_journal_ioprio, "journal_ioprio=%u"},
afd4672d 1208 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
06705bff
TT
1209 {Opt_auto_da_alloc, "auto_da_alloc"},
1210 {Opt_noauto_da_alloc, "noauto_da_alloc"},
744692dc
JZ
1211 {Opt_dioread_nolock, "dioread_nolock"},
1212 {Opt_dioread_lock, "dioread_lock"},
5328e635
ES
1213 {Opt_discard, "discard"},
1214 {Opt_nodiscard, "nodiscard"},
f3f12faa 1215 {Opt_err, NULL},
ac27a0ec
DK
1216};
1217
617ba13b 1218static ext4_fsblk_t get_sb_block(void **data)
ac27a0ec 1219{
617ba13b 1220 ext4_fsblk_t sb_block;
ac27a0ec
DK
1221 char *options = (char *) *data;
1222
1223 if (!options || strncmp(options, "sb=", 3) != 0)
1224 return 1; /* Default location */
0b8e58a1 1225
ac27a0ec 1226 options += 3;
0b8e58a1 1227 /* TODO: use simple_strtoll with >32bit ext4 */
ac27a0ec
DK
1228 sb_block = simple_strtoul(options, &options, 0);
1229 if (*options && *options != ',') {
4776004f 1230 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
ac27a0ec
DK
1231 (char *) *data);
1232 return 1;
1233 }
1234 if (*options == ',')
1235 options++;
1236 *data = (void *) options;
0b8e58a1 1237
ac27a0ec
DK
1238 return sb_block;
1239}
1240
b3881f74 1241#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
437ca0fd
DM
1242static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1243 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
b3881f74 1244
56c50f11
DM
1245#ifdef CONFIG_QUOTA
1246static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1247{
1248 struct ext4_sb_info *sbi = EXT4_SB(sb);
1249 char *qname;
1250
1251 if (sb_any_quota_loaded(sb) &&
1252 !sbi->s_qf_names[qtype]) {
1253 ext4_msg(sb, KERN_ERR,
1254 "Cannot change journaled "
1255 "quota options when quota turned on");
1256 return 0;
1257 }
1258 qname = match_strdup(args);
1259 if (!qname) {
1260 ext4_msg(sb, KERN_ERR,
1261 "Not enough memory for storing quotafile name");
1262 return 0;
1263 }
1264 if (sbi->s_qf_names[qtype] &&
1265 strcmp(sbi->s_qf_names[qtype], qname)) {
1266 ext4_msg(sb, KERN_ERR,
1267 "%s quota file already specified", QTYPE2NAME(qtype));
1268 kfree(qname);
1269 return 0;
1270 }
1271 sbi->s_qf_names[qtype] = qname;
1272 if (strchr(sbi->s_qf_names[qtype], '/')) {
1273 ext4_msg(sb, KERN_ERR,
1274 "quotafile must be on filesystem root");
1275 kfree(sbi->s_qf_names[qtype]);
1276 sbi->s_qf_names[qtype] = NULL;
1277 return 0;
1278 }
1279 set_opt(sbi->s_mount_opt, QUOTA);
1280 return 1;
1281}
1282
1283static int clear_qf_name(struct super_block *sb, int qtype)
1284{
1285
1286 struct ext4_sb_info *sbi = EXT4_SB(sb);
1287
1288 if (sb_any_quota_loaded(sb) &&
1289 sbi->s_qf_names[qtype]) {
1290 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1291 " when quota turned on");
1292 return 0;
1293 }
1294 /*
1295 * The space will be released later when all options are confirmed
1296 * to be correct
1297 */
1298 sbi->s_qf_names[qtype] = NULL;
1299 return 1;
1300}
1301#endif
1302
2b2d6d01 1303static int parse_options(char *options, struct super_block *sb,
c3191067 1304 unsigned long *journal_devnum,
b3881f74 1305 unsigned int *journal_ioprio,
2b2d6d01 1306 ext4_fsblk_t *n_blocks_count, int is_remount)
ac27a0ec 1307{
617ba13b 1308 struct ext4_sb_info *sbi = EXT4_SB(sb);
2b2d6d01 1309 char *p;
ac27a0ec
DK
1310 substring_t args[MAX_OPT_ARGS];
1311 int data_opt = 0;
1312 int option;
1313#ifdef CONFIG_QUOTA
56c50f11 1314 int qfmt;
ac27a0ec
DK
1315#endif
1316
1317 if (!options)
1318 return 1;
1319
2b2d6d01 1320 while ((p = strsep(&options, ",")) != NULL) {
ac27a0ec
DK
1321 int token;
1322 if (!*p)
1323 continue;
1324
15121c18
ES
1325 /*
1326 * Initialize args struct so we know whether arg was
1327 * found; some options take optional arguments.
1328 */
1329 args[0].to = args[0].from = 0;
ac27a0ec
DK
1330 token = match_token(p, tokens, args);
1331 switch (token) {
1332 case Opt_bsd_df:
437ca0fd 1333 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
2b2d6d01 1334 clear_opt(sbi->s_mount_opt, MINIX_DF);
ac27a0ec
DK
1335 break;
1336 case Opt_minix_df:
437ca0fd 1337 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
2b2d6d01 1338 set_opt(sbi->s_mount_opt, MINIX_DF);
437ca0fd 1339
ac27a0ec
DK
1340 break;
1341 case Opt_grpid:
437ca0fd 1342 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
2b2d6d01 1343 set_opt(sbi->s_mount_opt, GRPID);
437ca0fd 1344
ac27a0ec
DK
1345 break;
1346 case Opt_nogrpid:
437ca0fd 1347 ext4_msg(sb, KERN_WARNING, deprecated_msg, p, "2.6.38");
2b2d6d01 1348 clear_opt(sbi->s_mount_opt, GRPID);
437ca0fd 1349
ac27a0ec
DK
1350 break;
1351 case Opt_resuid:
1352 if (match_int(&args[0], &option))
1353 return 0;
1354 sbi->s_resuid = option;
1355 break;
1356 case Opt_resgid:
1357 if (match_int(&args[0], &option))
1358 return 0;
1359 sbi->s_resgid = option;
1360 break;
1361 case Opt_sb:
1362 /* handled by get_sb_block() instead of here */
1363 /* *sb_block = match_int(&args[0]); */
1364 break;
1365 case Opt_err_panic:
2b2d6d01
TT
1366 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1367 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1368 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
ac27a0ec
DK
1369 break;
1370 case Opt_err_ro:
2b2d6d01
TT
1371 clear_opt(sbi->s_mount_opt, ERRORS_CONT);
1372 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1373 set_opt(sbi->s_mount_opt, ERRORS_RO);
ac27a0ec
DK
1374 break;
1375 case Opt_err_cont:
2b2d6d01
TT
1376 clear_opt(sbi->s_mount_opt, ERRORS_RO);
1377 clear_opt(sbi->s_mount_opt, ERRORS_PANIC);
1378 set_opt(sbi->s_mount_opt, ERRORS_CONT);
ac27a0ec
DK
1379 break;
1380 case Opt_nouid32:
2b2d6d01 1381 set_opt(sbi->s_mount_opt, NO_UID32);
ac27a0ec 1382 break;
ac27a0ec 1383 case Opt_debug:
2b2d6d01 1384 set_opt(sbi->s_mount_opt, DEBUG);
ac27a0ec
DK
1385 break;
1386 case Opt_oldalloc:
2b2d6d01 1387 set_opt(sbi->s_mount_opt, OLDALLOC);
ac27a0ec
DK
1388 break;
1389 case Opt_orlov:
2b2d6d01 1390 clear_opt(sbi->s_mount_opt, OLDALLOC);
ac27a0ec 1391 break;
03010a33 1392#ifdef CONFIG_EXT4_FS_XATTR
ac27a0ec 1393 case Opt_user_xattr:
2b2d6d01 1394 set_opt(sbi->s_mount_opt, XATTR_USER);
ac27a0ec
DK
1395 break;
1396 case Opt_nouser_xattr:
2b2d6d01 1397 clear_opt(sbi->s_mount_opt, XATTR_USER);
ac27a0ec
DK
1398 break;
1399#else
1400 case Opt_user_xattr:
1401 case Opt_nouser_xattr:
b31e1552 1402 ext4_msg(sb, KERN_ERR, "(no)user_xattr options not supported");
ac27a0ec
DK
1403 break;
1404#endif
03010a33 1405#ifdef CONFIG_EXT4_FS_POSIX_ACL
ac27a0ec
DK
1406 case Opt_acl:
1407 set_opt(sbi->s_mount_opt, POSIX_ACL);
1408 break;
1409 case Opt_noacl:
1410 clear_opt(sbi->s_mount_opt, POSIX_ACL);
1411 break;
1412#else
1413 case Opt_acl:
1414 case Opt_noacl:
b31e1552 1415 ext4_msg(sb, KERN_ERR, "(no)acl options not supported");
ac27a0ec
DK
1416 break;
1417#endif
ac27a0ec
DK
1418 case Opt_journal_update:
1419 /* @@@ FIXME */
1420 /* Eventually we will want to be able to create
1421 a journal file here. For now, only allow the
1422 user to specify an existing inode to be the
1423 journal file. */
1424 if (is_remount) {
b31e1552
ES
1425 ext4_msg(sb, KERN_ERR,
1426 "Cannot specify journal on remount");
ac27a0ec
DK
1427 return 0;
1428 }
2b2d6d01 1429 set_opt(sbi->s_mount_opt, UPDATE_JOURNAL);
ac27a0ec 1430 break;
ac27a0ec
DK
1431 case Opt_journal_dev:
1432 if (is_remount) {
b31e1552
ES
1433 ext4_msg(sb, KERN_ERR,
1434 "Cannot specify journal on remount");
ac27a0ec
DK
1435 return 0;
1436 }
1437 if (match_int(&args[0], &option))
1438 return 0;
1439 *journal_devnum = option;
1440 break;
818d276c 1441 case Opt_journal_checksum:
d4da6c9c
LT
1442 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
1443 break;
818d276c
GS
1444 case Opt_journal_async_commit:
1445 set_opt(sbi->s_mount_opt, JOURNAL_ASYNC_COMMIT);
d4da6c9c 1446 set_opt(sbi->s_mount_opt, JOURNAL_CHECKSUM);
818d276c 1447 break;
ac27a0ec 1448 case Opt_noload:
2b2d6d01 1449 set_opt(sbi->s_mount_opt, NOLOAD);
ac27a0ec
DK
1450 break;
1451 case Opt_commit:
1452 if (match_int(&args[0], &option))
1453 return 0;
1454 if (option < 0)
1455 return 0;
1456 if (option == 0)
cd02ff0b 1457 option = JBD2_DEFAULT_MAX_COMMIT_AGE;
ac27a0ec
DK
1458 sbi->s_commit_interval = HZ * option;
1459 break;
30773840
TT
1460 case Opt_max_batch_time:
1461 if (match_int(&args[0], &option))
1462 return 0;
1463 if (option < 0)
1464 return 0;
1465 if (option == 0)
1466 option = EXT4_DEF_MAX_BATCH_TIME;
1467 sbi->s_max_batch_time = option;
1468 break;
1469 case Opt_min_batch_time:
1470 if (match_int(&args[0], &option))
1471 return 0;
1472 if (option < 0)
1473 return 0;
1474 sbi->s_min_batch_time = option;
1475 break;
ac27a0ec 1476 case Opt_data_journal:
617ba13b 1477 data_opt = EXT4_MOUNT_JOURNAL_DATA;
ac27a0ec
DK
1478 goto datacheck;
1479 case Opt_data_ordered:
617ba13b 1480 data_opt = EXT4_MOUNT_ORDERED_DATA;
ac27a0ec
DK
1481 goto datacheck;
1482 case Opt_data_writeback:
617ba13b 1483 data_opt = EXT4_MOUNT_WRITEBACK_DATA;
ac27a0ec
DK
1484 datacheck:
1485 if (is_remount) {
482a7425 1486 if (test_opt(sb, DATA_FLAGS) != data_opt) {
b31e1552
ES
1487 ext4_msg(sb, KERN_ERR,
1488 "Cannot change data mode on remount");
ac27a0ec
DK
1489 return 0;
1490 }
1491 } else {
482a7425 1492 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
ac27a0ec
DK
1493 sbi->s_mount_opt |= data_opt;
1494 }
1495 break;
5bf5683a
HK
1496 case Opt_data_err_abort:
1497 set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1498 break;
1499 case Opt_data_err_ignore:
1500 clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
1501 break;
ac27a0ec
DK
1502#ifdef CONFIG_QUOTA
1503 case Opt_usrjquota:
56c50f11 1504 if (!set_qf_name(sb, USRQUOTA, &args[0]))
ac27a0ec 1505 return 0;
56c50f11
DM
1506 break;
1507 case Opt_grpjquota:
1508 if (!set_qf_name(sb, GRPQUOTA, &args[0]))
ac27a0ec 1509 return 0;
ac27a0ec
DK
1510 break;
1511 case Opt_offusrjquota:
56c50f11
DM
1512 if (!clear_qf_name(sb, USRQUOTA))
1513 return 0;
1514 break;
ac27a0ec 1515 case Opt_offgrpjquota:
56c50f11 1516 if (!clear_qf_name(sb, GRPQUOTA))
ac27a0ec 1517 return 0;
ac27a0ec 1518 break;
56c50f11 1519
ac27a0ec 1520 case Opt_jqfmt_vfsold:
dfc5d03f
JK
1521 qfmt = QFMT_VFS_OLD;
1522 goto set_qf_format;
ac27a0ec 1523 case Opt_jqfmt_vfsv0:
dfc5d03f 1524 qfmt = QFMT_VFS_V0;
5a20bdfc
JK
1525 goto set_qf_format;
1526 case Opt_jqfmt_vfsv1:
1527 qfmt = QFMT_VFS_V1;
dfc5d03f 1528set_qf_format:
17bd13b3 1529 if (sb_any_quota_loaded(sb) &&
dfc5d03f 1530 sbi->s_jquota_fmt != qfmt) {
b31e1552 1531 ext4_msg(sb, KERN_ERR, "Cannot change "
dfc5d03f 1532 "journaled quota options when "
b31e1552 1533 "quota turned on");
dfc5d03f
JK
1534 return 0;
1535 }
1536 sbi->s_jquota_fmt = qfmt;
ac27a0ec
DK
1537 break;
1538 case Opt_quota:
1539 case Opt_usrquota:
1540 set_opt(sbi->s_mount_opt, QUOTA);
1541 set_opt(sbi->s_mount_opt, USRQUOTA);
1542 break;
1543 case Opt_grpquota:
1544 set_opt(sbi->s_mount_opt, QUOTA);
1545 set_opt(sbi->s_mount_opt, GRPQUOTA);
1546 break;
1547 case Opt_noquota:
17bd13b3 1548 if (sb_any_quota_loaded(sb)) {
b31e1552
ES
1549 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1550 "options when quota turned on");
ac27a0ec
DK
1551 return 0;
1552 }
1553 clear_opt(sbi->s_mount_opt, QUOTA);
1554 clear_opt(sbi->s_mount_opt, USRQUOTA);
1555 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1556 break;
1557#else
1558 case Opt_quota:
1559 case Opt_usrquota:
1560 case Opt_grpquota:
b31e1552
ES
1561 ext4_msg(sb, KERN_ERR,
1562 "quota options not supported");
cd59e7b9 1563 break;
ac27a0ec
DK
1564 case Opt_usrjquota:
1565 case Opt_grpjquota:
1566 case Opt_offusrjquota:
1567 case Opt_offgrpjquota:
1568 case Opt_jqfmt_vfsold:
1569 case Opt_jqfmt_vfsv0:
5a20bdfc 1570 case Opt_jqfmt_vfsv1:
b31e1552
ES
1571 ext4_msg(sb, KERN_ERR,
1572 "journaled quota options not supported");
ac27a0ec
DK
1573 break;
1574 case Opt_noquota:
1575 break;
1576#endif
1577 case Opt_abort:
4ab2f15b 1578 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
ac27a0ec 1579 break;
06705bff
TT
1580 case Opt_nobarrier:
1581 clear_opt(sbi->s_mount_opt, BARRIER);
1582 break;
ac27a0ec 1583 case Opt_barrier:
15121c18
ES
1584 if (args[0].from) {
1585 if (match_int(&args[0], &option))
1586 return 0;
1587 } else
1588 option = 1; /* No argument, default to 1 */
ac27a0ec
DK
1589 if (option)
1590 set_opt(sbi->s_mount_opt, BARRIER);
1591 else
1592 clear_opt(sbi->s_mount_opt, BARRIER);
1593 break;
1594 case Opt_ignore:
1595 break;
1596 case Opt_resize:
1597 if (!is_remount) {
b31e1552
ES
1598 ext4_msg(sb, KERN_ERR,
1599 "resize option only available "
1600 "for remount");
ac27a0ec
DK
1601 return 0;
1602 }
1603 if (match_int(&args[0], &option) != 0)
1604 return 0;
1605 *n_blocks_count = option;
1606 break;
1607 case Opt_nobh:
1608 set_opt(sbi->s_mount_opt, NOBH);
1609 break;
1610 case Opt_bh:
1611 clear_opt(sbi->s_mount_opt, NOBH);
1612 break;
25ec56b5
JNC
1613 case Opt_i_version:
1614 set_opt(sbi->s_mount_opt, I_VERSION);
1615 sb->s_flags |= MS_I_VERSION;
1616 break;
dd919b98
AK
1617 case Opt_nodelalloc:
1618 clear_opt(sbi->s_mount_opt, DELALLOC);
1619 break;
c9de560d
AT
1620 case Opt_stripe:
1621 if (match_int(&args[0], &option))
1622 return 0;
1623 if (option < 0)
1624 return 0;
1625 sbi->s_stripe = option;
1626 break;
64769240
AT
1627 case Opt_delalloc:
1628 set_opt(sbi->s_mount_opt, DELALLOC);
1629 break;
6fd058f7
TT
1630 case Opt_block_validity:
1631 set_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1632 break;
1633 case Opt_noblock_validity:
1634 clear_opt(sbi->s_mount_opt, BLOCK_VALIDITY);
1635 break;
240799cd
TT
1636 case Opt_inode_readahead_blks:
1637 if (match_int(&args[0], &option))
1638 return 0;
1639 if (option < 0 || option > (1 << 30))
1640 return 0;
f7c43950 1641 if (!is_power_of_2(option)) {
b31e1552
ES
1642 ext4_msg(sb, KERN_ERR,
1643 "EXT4-fs: inode_readahead_blks"
1644 " must be a power of 2");
3197ebdb
TT
1645 return 0;
1646 }
240799cd
TT
1647 sbi->s_inode_readahead_blks = option;
1648 break;
b3881f74
TT
1649 case Opt_journal_ioprio:
1650 if (match_int(&args[0], &option))
1651 return 0;
1652 if (option < 0 || option > 7)
1653 break;
1654 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE,
1655 option);
1656 break;
06705bff
TT
1657 case Opt_noauto_da_alloc:
1658 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1659 break;
afd4672d 1660 case Opt_auto_da_alloc:
15121c18
ES
1661 if (args[0].from) {
1662 if (match_int(&args[0], &option))
1663 return 0;
1664 } else
1665 option = 1; /* No argument, default to 1 */
afd4672d
TT
1666 if (option)
1667 clear_opt(sbi->s_mount_opt, NO_AUTO_DA_ALLOC);
1668 else
1669 set_opt(sbi->s_mount_opt,NO_AUTO_DA_ALLOC);
1670 break;
5328e635
ES
1671 case Opt_discard:
1672 set_opt(sbi->s_mount_opt, DISCARD);
1673 break;
1674 case Opt_nodiscard:
1675 clear_opt(sbi->s_mount_opt, DISCARD);
1676 break;
744692dc
JZ
1677 case Opt_dioread_nolock:
1678 set_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1679 break;
1680 case Opt_dioread_lock:
1681 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
1682 break;
ac27a0ec 1683 default:
b31e1552
ES
1684 ext4_msg(sb, KERN_ERR,
1685 "Unrecognized mount option \"%s\" "
1686 "or missing value", p);
ac27a0ec
DK
1687 return 0;
1688 }
1689 }
1690#ifdef CONFIG_QUOTA
1691 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
482a7425 1692 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
ac27a0ec
DK
1693 clear_opt(sbi->s_mount_opt, USRQUOTA);
1694
482a7425 1695 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
ac27a0ec
DK
1696 clear_opt(sbi->s_mount_opt, GRPQUOTA);
1697
56c50f11 1698 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
b31e1552
ES
1699 ext4_msg(sb, KERN_ERR, "old and new quota "
1700 "format mixing");
ac27a0ec
DK
1701 return 0;
1702 }
1703
1704 if (!sbi->s_jquota_fmt) {
b31e1552
ES
1705 ext4_msg(sb, KERN_ERR, "journaled quota format "
1706 "not specified");
ac27a0ec
DK
1707 return 0;
1708 }
1709 } else {
1710 if (sbi->s_jquota_fmt) {
b31e1552 1711 ext4_msg(sb, KERN_ERR, "journaled quota format "
2c8be6b2 1712 "specified with no journaling "
b31e1552 1713 "enabled");
ac27a0ec
DK
1714 return 0;
1715 }
1716 }
1717#endif
1718 return 1;
1719}
1720
617ba13b 1721static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
1722 int read_only)
1723{
617ba13b 1724 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1725 int res = 0;
1726
617ba13b 1727 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
b31e1552
ES
1728 ext4_msg(sb, KERN_ERR, "revision level too high, "
1729 "forcing read-only mode");
ac27a0ec
DK
1730 res = MS_RDONLY;
1731 }
1732 if (read_only)
1733 return res;
617ba13b 1734 if (!(sbi->s_mount_state & EXT4_VALID_FS))
b31e1552
ES
1735 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1736 "running e2fsck is recommended");
617ba13b 1737 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
b31e1552
ES
1738 ext4_msg(sb, KERN_WARNING,
1739 "warning: mounting fs with errors, "
1740 "running e2fsck is recommended");
ac27a0ec
DK
1741 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
1742 le16_to_cpu(es->s_mnt_count) >=
1743 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
b31e1552
ES
1744 ext4_msg(sb, KERN_WARNING,
1745 "warning: maximal mount count reached, "
1746 "running e2fsck is recommended");
ac27a0ec
DK
1747 else if (le32_to_cpu(es->s_checkinterval) &&
1748 (le32_to_cpu(es->s_lastcheck) +
1749 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
b31e1552
ES
1750 ext4_msg(sb, KERN_WARNING,
1751 "warning: checktime reached, "
1752 "running e2fsck is recommended");
0b8e58a1 1753 if (!sbi->s_journal)
0390131b 1754 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec 1755 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 1756 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
e8546d06 1757 le16_add_cpu(&es->s_mnt_count, 1);
ac27a0ec 1758 es->s_mtime = cpu_to_le32(get_seconds());
617ba13b 1759 ext4_update_dynamic_rev(sb);
0390131b
FM
1760 if (sbi->s_journal)
1761 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 1762
e2d67052 1763 ext4_commit_super(sb, 1);
ac27a0ec 1764 if (test_opt(sb, DEBUG))
a9df9a49 1765 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
7f4520cc 1766 "bpg=%lu, ipg=%lu, mo=%04x]\n",
ac27a0ec
DK
1767 sb->s_blocksize,
1768 sbi->s_groups_count,
617ba13b
MC
1769 EXT4_BLOCKS_PER_GROUP(sb),
1770 EXT4_INODES_PER_GROUP(sb),
ac27a0ec
DK
1771 sbi->s_mount_opt);
1772
ac27a0ec
DK
1773 return res;
1774}
1775
772cb7c8
JS
1776static int ext4_fill_flex_info(struct super_block *sb)
1777{
1778 struct ext4_sb_info *sbi = EXT4_SB(sb);
1779 struct ext4_group_desc *gdp = NULL;
772cb7c8
JS
1780 ext4_group_t flex_group_count;
1781 ext4_group_t flex_group;
1782 int groups_per_flex = 0;
c5ca7c76 1783 size_t size;
772cb7c8
JS
1784 int i;
1785
503358ae
TT
1786 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1787 groups_per_flex = 1 << sbi->s_log_groups_per_flex;
1788
1789 if (groups_per_flex < 2) {
772cb7c8
JS
1790 sbi->s_log_groups_per_flex = 0;
1791 return 1;
1792 }
1793
c62a11fd
FB
1794 /* We allocate both existing and potentially added groups */
1795 flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
d94e99a6
AK
1796 ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
1797 EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
c5ca7c76
TT
1798 size = flex_group_count * sizeof(struct flex_groups);
1799 sbi->s_flex_groups = kzalloc(size, GFP_KERNEL);
1800 if (sbi->s_flex_groups == NULL) {
1801 sbi->s_flex_groups = vmalloc(size);
1802 if (sbi->s_flex_groups)
1803 memset(sbi->s_flex_groups, 0, size);
1804 }
772cb7c8 1805 if (sbi->s_flex_groups == NULL) {
b31e1552
ES
1806 ext4_msg(sb, KERN_ERR, "not enough memory for "
1807 "%u flex groups", flex_group_count);
772cb7c8
JS
1808 goto failed;
1809 }
772cb7c8 1810
772cb7c8 1811 for (i = 0; i < sbi->s_groups_count; i++) {
88b6edd1 1812 gdp = ext4_get_group_desc(sb, i, NULL);
772cb7c8
JS
1813
1814 flex_group = ext4_flex_group(sbi, i);
7ad9bb65
TT
1815 atomic_add(ext4_free_inodes_count(sb, gdp),
1816 &sbi->s_flex_groups[flex_group].free_inodes);
1817 atomic_add(ext4_free_blks_count(sb, gdp),
1818 &sbi->s_flex_groups[flex_group].free_blocks);
1819 atomic_add(ext4_used_dirs_count(sb, gdp),
1820 &sbi->s_flex_groups[flex_group].used_dirs);
772cb7c8
JS
1821 }
1822
1823 return 1;
1824failed:
1825 return 0;
1826}
1827
717d50e4
AD
1828__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1829 struct ext4_group_desc *gdp)
1830{
1831 __u16 crc = 0;
1832
1833 if (sbi->s_es->s_feature_ro_compat &
1834 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
1835 int offset = offsetof(struct ext4_group_desc, bg_checksum);
1836 __le32 le_group = cpu_to_le32(block_group);
1837
1838 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
1839 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
1840 crc = crc16(crc, (__u8 *)gdp, offset);
1841 offset += sizeof(gdp->bg_checksum); /* skip checksum */
1842 /* for checksum of struct ext4_group_desc do the rest...*/
1843 if ((sbi->s_es->s_feature_incompat &
1844 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
1845 offset < le16_to_cpu(sbi->s_es->s_desc_size))
1846 crc = crc16(crc, (__u8 *)gdp + offset,
1847 le16_to_cpu(sbi->s_es->s_desc_size) -
1848 offset);
1849 }
1850
1851 return cpu_to_le16(crc);
1852}
1853
1854int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
1855 struct ext4_group_desc *gdp)
1856{
1857 if ((sbi->s_es->s_feature_ro_compat &
1858 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
1859 (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
1860 return 0;
1861
1862 return 1;
1863}
1864
ac27a0ec 1865/* Called at mount-time, super-block is locked */
197cd65a 1866static int ext4_check_descriptors(struct super_block *sb)
ac27a0ec 1867{
617ba13b
MC
1868 struct ext4_sb_info *sbi = EXT4_SB(sb);
1869 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
1870 ext4_fsblk_t last_block;
bd81d8ee
LV
1871 ext4_fsblk_t block_bitmap;
1872 ext4_fsblk_t inode_bitmap;
1873 ext4_fsblk_t inode_table;
ce421581 1874 int flexbg_flag = 0;
fd2d4291 1875 ext4_group_t i;
ac27a0ec 1876
ce421581
JS
1877 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
1878 flexbg_flag = 1;
1879
af5bc92d 1880 ext4_debug("Checking group descriptors");
ac27a0ec 1881
197cd65a
AM
1882 for (i = 0; i < sbi->s_groups_count; i++) {
1883 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
1884
ce421581 1885 if (i == sbi->s_groups_count - 1 || flexbg_flag)
bd81d8ee 1886 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
1887 else
1888 last_block = first_block +
617ba13b 1889 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 1890
8fadc143 1891 block_bitmap = ext4_block_bitmap(sb, gdp);
2b2d6d01 1892 if (block_bitmap < first_block || block_bitmap > last_block) {
b31e1552 1893 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1894 "Block bitmap for group %u not in group "
b31e1552 1895 "(block %llu)!", i, block_bitmap);
ac27a0ec
DK
1896 return 0;
1897 }
8fadc143 1898 inode_bitmap = ext4_inode_bitmap(sb, gdp);
2b2d6d01 1899 if (inode_bitmap < first_block || inode_bitmap > last_block) {
b31e1552 1900 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1901 "Inode bitmap for group %u not in group "
b31e1552 1902 "(block %llu)!", i, inode_bitmap);
ac27a0ec
DK
1903 return 0;
1904 }
8fadc143 1905 inode_table = ext4_inode_table(sb, gdp);
bd81d8ee 1906 if (inode_table < first_block ||
2b2d6d01 1907 inode_table + sbi->s_itb_per_group - 1 > last_block) {
b31e1552 1908 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 1909 "Inode table for group %u not in group "
b31e1552 1910 "(block %llu)!", i, inode_table);
ac27a0ec
DK
1911 return 0;
1912 }
955ce5f5 1913 ext4_lock_group(sb, i);
717d50e4 1914 if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
b31e1552
ES
1915 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
1916 "Checksum for group %u failed (%u!=%u)",
1917 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
1918 gdp)), le16_to_cpu(gdp->bg_checksum));
7ee1ec4c 1919 if (!(sb->s_flags & MS_RDONLY)) {
955ce5f5 1920 ext4_unlock_group(sb, i);
8a266467 1921 return 0;
7ee1ec4c 1922 }
717d50e4 1923 }
955ce5f5 1924 ext4_unlock_group(sb, i);
ce421581
JS
1925 if (!flexbg_flag)
1926 first_block += EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec
DK
1927 }
1928
bd81d8ee 1929 ext4_free_blocks_count_set(sbi->s_es, ext4_count_free_blocks(sb));
0b8e58a1 1930 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
1931 return 1;
1932}
1933
617ba13b 1934/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
ac27a0ec
DK
1935 * the superblock) which were deleted from all directories, but held open by
1936 * a process at the time of a crash. We walk the list and try to delete these
1937 * inodes at recovery time (only with a read-write filesystem).
1938 *
1939 * In order to keep the orphan inode chain consistent during traversal (in
1940 * case of crash during recovery), we link each inode into the superblock
1941 * orphan list_head and handle it the same way as an inode deletion during
1942 * normal operation (which journals the operations for us).
1943 *
1944 * We only do an iget() and an iput() on each inode, which is very safe if we
1945 * accidentally point at an in-use or already deleted inode. The worst that
1946 * can happen in this case is that we get a "bit already cleared" message from
617ba13b 1947 * ext4_free_inode(). The only reason we would point at a wrong inode is if
ac27a0ec
DK
1948 * e2fsck was run on this filesystem, and it must have already done the orphan
1949 * inode cleanup for us, so we can safely abort without any further action.
1950 */
2b2d6d01
TT
1951static void ext4_orphan_cleanup(struct super_block *sb,
1952 struct ext4_super_block *es)
ac27a0ec
DK
1953{
1954 unsigned int s_flags = sb->s_flags;
1955 int nr_orphans = 0, nr_truncates = 0;
1956#ifdef CONFIG_QUOTA
1957 int i;
1958#endif
1959 if (!es->s_last_orphan) {
1960 jbd_debug(4, "no orphan inodes to clean up\n");
1961 return;
1962 }
1963
a8f48a95 1964 if (bdev_read_only(sb->s_bdev)) {
b31e1552
ES
1965 ext4_msg(sb, KERN_ERR, "write access "
1966 "unavailable, skipping orphan cleanup");
a8f48a95
ES
1967 return;
1968 }
1969
617ba13b 1970 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
ac27a0ec
DK
1971 if (es->s_last_orphan)
1972 jbd_debug(1, "Errors on filesystem, "
1973 "clearing orphan list.\n");
1974 es->s_last_orphan = 0;
1975 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
1976 return;
1977 }
1978
1979 if (s_flags & MS_RDONLY) {
b31e1552 1980 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
ac27a0ec
DK
1981 sb->s_flags &= ~MS_RDONLY;
1982 }
1983#ifdef CONFIG_QUOTA
1984 /* Needed for iput() to work correctly and not trash data */
1985 sb->s_flags |= MS_ACTIVE;
1986 /* Turn on quotas so that they are updated correctly */
1987 for (i = 0; i < MAXQUOTAS; i++) {
617ba13b
MC
1988 if (EXT4_SB(sb)->s_qf_names[i]) {
1989 int ret = ext4_quota_on_mount(sb, i);
ac27a0ec 1990 if (ret < 0)
b31e1552
ES
1991 ext4_msg(sb, KERN_ERR,
1992 "Cannot turn on journaled "
1993 "quota: error %d", ret);
ac27a0ec
DK
1994 }
1995 }
1996#endif
1997
1998 while (es->s_last_orphan) {
1999 struct inode *inode;
2000
97bd42b9
JB
2001 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2002 if (IS_ERR(inode)) {
ac27a0ec
DK
2003 es->s_last_orphan = 0;
2004 break;
2005 }
2006
617ba13b 2007 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
871a2931 2008 dquot_initialize(inode);
ac27a0ec 2009 if (inode->i_nlink) {
b31e1552
ES
2010 ext4_msg(sb, KERN_DEBUG,
2011 "%s: truncating inode %lu to %lld bytes",
46e665e9 2012 __func__, inode->i_ino, inode->i_size);
e5f8eab8 2013 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
ac27a0ec 2014 inode->i_ino, inode->i_size);
617ba13b 2015 ext4_truncate(inode);
ac27a0ec
DK
2016 nr_truncates++;
2017 } else {
b31e1552
ES
2018 ext4_msg(sb, KERN_DEBUG,
2019 "%s: deleting unreferenced inode %lu",
46e665e9 2020 __func__, inode->i_ino);
ac27a0ec
DK
2021 jbd_debug(2, "deleting unreferenced inode %lu\n",
2022 inode->i_ino);
2023 nr_orphans++;
2024 }
2025 iput(inode); /* The delete magic happens here! */
2026 }
2027
2b2d6d01 2028#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
ac27a0ec
DK
2029
2030 if (nr_orphans)
b31e1552
ES
2031 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2032 PLURAL(nr_orphans));
ac27a0ec 2033 if (nr_truncates)
b31e1552
ES
2034 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2035 PLURAL(nr_truncates));
ac27a0ec
DK
2036#ifdef CONFIG_QUOTA
2037 /* Turn quotas off */
2038 for (i = 0; i < MAXQUOTAS; i++) {
2039 if (sb_dqopt(sb)->files[i])
6f28e087 2040 vfs_quota_off(sb, i, 0);
ac27a0ec
DK
2041 }
2042#endif
2043 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2044}
0b8e58a1 2045
cd2291a4
ES
2046/*
2047 * Maximal extent format file size.
2048 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2049 * extent format containers, within a sector_t, and within i_blocks
2050 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2051 * so that won't be a limiting factor.
2052 *
2053 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2054 */
f287a1a5 2055static loff_t ext4_max_size(int blkbits, int has_huge_files)
cd2291a4
ES
2056{
2057 loff_t res;
2058 loff_t upper_limit = MAX_LFS_FILESIZE;
2059
2060 /* small i_blocks in vfs inode? */
f287a1a5 2061 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
cd2291a4 2062 /*
90c699a9 2063 * CONFIG_LBDAF is not enabled implies the inode
cd2291a4
ES
2064 * i_block represent total blocks in 512 bytes
2065 * 32 == size of vfs inode i_blocks * 8
2066 */
2067 upper_limit = (1LL << 32) - 1;
2068
2069 /* total blocks in file system block size */
2070 upper_limit >>= (blkbits - 9);
2071 upper_limit <<= blkbits;
2072 }
2073
2074 /* 32-bit extent-start container, ee_block */
2075 res = 1LL << 32;
2076 res <<= blkbits;
2077 res -= 1;
2078
2079 /* Sanity check against vm- & vfs- imposed limits */
2080 if (res > upper_limit)
2081 res = upper_limit;
2082
2083 return res;
2084}
ac27a0ec 2085
ac27a0ec 2086/*
cd2291a4 2087 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
0fc1b451
AK
2088 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2089 * We need to be 1 filesystem block less than the 2^48 sector limit.
ac27a0ec 2090 */
f287a1a5 2091static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
ac27a0ec 2092{
617ba13b 2093 loff_t res = EXT4_NDIR_BLOCKS;
0fc1b451
AK
2094 int meta_blocks;
2095 loff_t upper_limit;
0b8e58a1
AD
2096 /* This is calculated to be the largest file size for a dense, block
2097 * mapped file such that the file's total number of 512-byte sectors,
2098 * including data and all indirect blocks, does not exceed (2^48 - 1).
2099 *
2100 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2101 * number of 512-byte sectors of the file.
0fc1b451
AK
2102 */
2103
f287a1a5 2104 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
0fc1b451 2105 /*
90c699a9 2106 * !has_huge_files or CONFIG_LBDAF not enabled implies that
0b8e58a1
AD
2107 * the inode i_block field represents total file blocks in
2108 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
0fc1b451
AK
2109 */
2110 upper_limit = (1LL << 32) - 1;
2111
2112 /* total blocks in file system block size */
2113 upper_limit >>= (bits - 9);
2114
2115 } else {
8180a562
AK
2116 /*
2117 * We use 48 bit ext4_inode i_blocks
2118 * With EXT4_HUGE_FILE_FL set the i_blocks
2119 * represent total number of blocks in
2120 * file system block size
2121 */
0fc1b451
AK
2122 upper_limit = (1LL << 48) - 1;
2123
0fc1b451
AK
2124 }
2125
2126 /* indirect blocks */
2127 meta_blocks = 1;
2128 /* double indirect blocks */
2129 meta_blocks += 1 + (1LL << (bits-2));
2130 /* tripple indirect blocks */
2131 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2132
2133 upper_limit -= meta_blocks;
2134 upper_limit <<= bits;
ac27a0ec
DK
2135
2136 res += 1LL << (bits-2);
2137 res += 1LL << (2*(bits-2));
2138 res += 1LL << (3*(bits-2));
2139 res <<= bits;
2140 if (res > upper_limit)
2141 res = upper_limit;
0fc1b451
AK
2142
2143 if (res > MAX_LFS_FILESIZE)
2144 res = MAX_LFS_FILESIZE;
2145
ac27a0ec
DK
2146 return res;
2147}
2148
617ba13b 2149static ext4_fsblk_t descriptor_loc(struct super_block *sb,
0b8e58a1 2150 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 2151{
617ba13b 2152 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 2153 ext4_group_t bg, first_meta_bg;
ac27a0ec
DK
2154 int has_super = 0;
2155
2156 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2157
617ba13b 2158 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 2159 nr < first_meta_bg)
70bbb3e0 2160 return logical_sb_block + nr + 1;
ac27a0ec 2161 bg = sbi->s_desc_per_block * nr;
617ba13b 2162 if (ext4_bg_has_super(sb, bg))
ac27a0ec 2163 has_super = 1;
0b8e58a1 2164
617ba13b 2165 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
2166}
2167
c9de560d
AT
2168/**
2169 * ext4_get_stripe_size: Get the stripe size.
2170 * @sbi: In memory super block info
2171 *
2172 * If we have specified it via mount option, then
2173 * use the mount option value. If the value specified at mount time is
2174 * greater than the blocks per group use the super block value.
2175 * If the super block value is greater than blocks per group return 0.
2176 * Allocator needs it be less than blocks per group.
2177 *
2178 */
2179static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2180{
2181 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2182 unsigned long stripe_width =
2183 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2184
2185 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2186 return sbi->s_stripe;
2187
2188 if (stripe_width <= sbi->s_blocks_per_group)
2189 return stripe_width;
2190
2191 if (stride <= sbi->s_blocks_per_group)
2192 return stride;
2193
2194 return 0;
2195}
ac27a0ec 2196
3197ebdb
TT
2197/* sysfs supprt */
2198
2199struct ext4_attr {
2200 struct attribute attr;
2201 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2202 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2203 const char *, size_t);
2204 int offset;
2205};
2206
2207static int parse_strtoul(const char *buf,
2208 unsigned long max, unsigned long *value)
2209{
2210 char *endp;
2211
e7d2860b
AGR
2212 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
2213 endp = skip_spaces(endp);
3197ebdb
TT
2214 if (*endp || *value > max)
2215 return -EINVAL;
2216
2217 return 0;
2218}
2219
2220static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2221 struct ext4_sb_info *sbi,
2222 char *buf)
2223{
2224 return snprintf(buf, PAGE_SIZE, "%llu\n",
2225 (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
2226}
2227
2228static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2229 struct ext4_sb_info *sbi, char *buf)
2230{
2231 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2232
2233 return snprintf(buf, PAGE_SIZE, "%lu\n",
2234 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2235 sbi->s_sectors_written_start) >> 1);
2236}
2237
2238static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2239 struct ext4_sb_info *sbi, char *buf)
2240{
2241 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2242
2243 return snprintf(buf, PAGE_SIZE, "%llu\n",
a6b43e38 2244 (unsigned long long)(sbi->s_kbytes_written +
3197ebdb 2245 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
a6b43e38 2246 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
3197ebdb
TT
2247}
2248
2249static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2250 struct ext4_sb_info *sbi,
2251 const char *buf, size_t count)
2252{
2253 unsigned long t;
2254
2255 if (parse_strtoul(buf, 0x40000000, &t))
2256 return -EINVAL;
2257
f7c43950 2258 if (!is_power_of_2(t))
3197ebdb
TT
2259 return -EINVAL;
2260
2261 sbi->s_inode_readahead_blks = t;
2262 return count;
2263}
2264
2265static ssize_t sbi_ui_show(struct ext4_attr *a,
0b8e58a1 2266 struct ext4_sb_info *sbi, char *buf)
3197ebdb
TT
2267{
2268 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2269
2270 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2271}
2272
2273static ssize_t sbi_ui_store(struct ext4_attr *a,
2274 struct ext4_sb_info *sbi,
2275 const char *buf, size_t count)
2276{
2277 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2278 unsigned long t;
2279
2280 if (parse_strtoul(buf, 0xffffffff, &t))
2281 return -EINVAL;
2282 *ui = t;
2283 return count;
2284}
2285
2286#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2287static struct ext4_attr ext4_attr_##_name = { \
2288 .attr = {.name = __stringify(_name), .mode = _mode }, \
2289 .show = _show, \
2290 .store = _store, \
2291 .offset = offsetof(struct ext4_sb_info, _elname), \
2292}
2293#define EXT4_ATTR(name, mode, show, store) \
2294static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2295
2296#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2297#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2298#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2299 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2300#define ATTR_LIST(name) &ext4_attr_##name.attr
2301
2302EXT4_RO_ATTR(delayed_allocation_blocks);
2303EXT4_RO_ATTR(session_write_kbytes);
2304EXT4_RO_ATTR(lifetime_write_kbytes);
2305EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2306 inode_readahead_blks_store, s_inode_readahead_blks);
11013911 2307EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
3197ebdb
TT
2308EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2309EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2310EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2311EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2312EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2313EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
55138e0b 2314EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
3197ebdb
TT
2315
2316static struct attribute *ext4_attrs[] = {
2317 ATTR_LIST(delayed_allocation_blocks),
2318 ATTR_LIST(session_write_kbytes),
2319 ATTR_LIST(lifetime_write_kbytes),
2320 ATTR_LIST(inode_readahead_blks),
11013911 2321 ATTR_LIST(inode_goal),
3197ebdb
TT
2322 ATTR_LIST(mb_stats),
2323 ATTR_LIST(mb_max_to_scan),
2324 ATTR_LIST(mb_min_to_scan),
2325 ATTR_LIST(mb_order2_req),
2326 ATTR_LIST(mb_stream_req),
2327 ATTR_LIST(mb_group_prealloc),
55138e0b 2328 ATTR_LIST(max_writeback_mb_bump),
3197ebdb
TT
2329 NULL,
2330};
2331
2332static ssize_t ext4_attr_show(struct kobject *kobj,
2333 struct attribute *attr, char *buf)
2334{
2335 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2336 s_kobj);
2337 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2338
2339 return a->show ? a->show(a, sbi, buf) : 0;
2340}
2341
2342static ssize_t ext4_attr_store(struct kobject *kobj,
2343 struct attribute *attr,
2344 const char *buf, size_t len)
2345{
2346 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2347 s_kobj);
2348 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2349
2350 return a->store ? a->store(a, sbi, buf, len) : 0;
2351}
2352
2353static void ext4_sb_release(struct kobject *kobj)
2354{
2355 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2356 s_kobj);
2357 complete(&sbi->s_kobj_unregister);
2358}
2359
2360
52cf25d0 2361static const struct sysfs_ops ext4_attr_ops = {
3197ebdb
TT
2362 .show = ext4_attr_show,
2363 .store = ext4_attr_store,
2364};
2365
2366static struct kobj_type ext4_ktype = {
2367 .default_attrs = ext4_attrs,
2368 .sysfs_ops = &ext4_attr_ops,
2369 .release = ext4_sb_release,
2370};
2371
a13fb1a4
ES
2372/*
2373 * Check whether this filesystem can be mounted based on
2374 * the features present and the RDONLY/RDWR mount requested.
2375 * Returns 1 if this filesystem can be mounted as requested,
2376 * 0 if it cannot be.
2377 */
2378static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2379{
2380 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2381 ext4_msg(sb, KERN_ERR,
2382 "Couldn't mount because of "
2383 "unsupported optional features (%x)",
2384 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2385 ~EXT4_FEATURE_INCOMPAT_SUPP));
2386 return 0;
2387 }
2388
2389 if (readonly)
2390 return 1;
2391
2392 /* Check that feature set is OK for a read-write mount */
2393 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2394 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2395 "unsupported optional features (%x)",
2396 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2397 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2398 return 0;
2399 }
2400 /*
2401 * Large file size enabled file system can only be mounted
2402 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2403 */
2404 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2405 if (sizeof(blkcnt_t) < sizeof(u64)) {
2406 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2407 "cannot be mounted RDWR without "
2408 "CONFIG_LBDAF");
2409 return 0;
2410 }
2411 }
2412 return 1;
2413}
2414
2b2d6d01 2415static int ext4_fill_super(struct super_block *sb, void *data, int silent)
7477827f
AK
2416 __releases(kernel_lock)
2417 __acquires(kernel_lock)
ac27a0ec 2418{
2b2d6d01 2419 struct buffer_head *bh;
617ba13b
MC
2420 struct ext4_super_block *es = NULL;
2421 struct ext4_sb_info *sbi;
2422 ext4_fsblk_t block;
2423 ext4_fsblk_t sb_block = get_sb_block(&data);
70bbb3e0 2424 ext4_fsblk_t logical_sb_block;
ac27a0ec 2425 unsigned long offset = 0;
ac27a0ec
DK
2426 unsigned long journal_devnum = 0;
2427 unsigned long def_mount_opts;
2428 struct inode *root;
9f6200bb 2429 char *cp;
0390131b 2430 const char *descr;
1d1fe1ee 2431 int ret = -EINVAL;
ac27a0ec 2432 int blocksize;
4ec11028
TT
2433 unsigned int db_count;
2434 unsigned int i;
f287a1a5 2435 int needs_recovery, has_huge_files;
bd81d8ee 2436 __u64 blocks_count;
833f4077 2437 int err;
b3881f74 2438 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
ac27a0ec
DK
2439
2440 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
2441 if (!sbi)
2442 return -ENOMEM;
705895b6
PE
2443
2444 sbi->s_blockgroup_lock =
2445 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
2446 if (!sbi->s_blockgroup_lock) {
2447 kfree(sbi);
2448 return -ENOMEM;
2449 }
ac27a0ec
DK
2450 sb->s_fs_info = sbi;
2451 sbi->s_mount_opt = 0;
617ba13b
MC
2452 sbi->s_resuid = EXT4_DEF_RESUID;
2453 sbi->s_resgid = EXT4_DEF_RESGID;
240799cd 2454 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
d9c9bef1 2455 sbi->s_sb_block = sb_block;
afc32f7e
TT
2456 sbi->s_sectors_written_start = part_stat_read(sb->s_bdev->bd_part,
2457 sectors[1]);
ac27a0ec
DK
2458
2459 unlock_kernel();
2460
9f6200bb
TT
2461 /* Cleanup superblock name */
2462 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
2463 *cp = '!';
2464
617ba13b 2465 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 2466 if (!blocksize) {
b31e1552 2467 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
ac27a0ec
DK
2468 goto out_fail;
2469 }
2470
2471 /*
617ba13b 2472 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
2473 * block sizes. We need to calculate the offset from buffer start.
2474 */
617ba13b 2475 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
70bbb3e0
AM
2476 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2477 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 2478 } else {
70bbb3e0 2479 logical_sb_block = sb_block;
ac27a0ec
DK
2480 }
2481
70bbb3e0 2482 if (!(bh = sb_bread(sb, logical_sb_block))) {
b31e1552 2483 ext4_msg(sb, KERN_ERR, "unable to read superblock");
ac27a0ec
DK
2484 goto out_fail;
2485 }
2486 /*
2487 * Note: s_es must be initialized as soon as possible because
617ba13b 2488 * some ext4 macro-instructions depend on its value
ac27a0ec 2489 */
617ba13b 2490 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
ac27a0ec
DK
2491 sbi->s_es = es;
2492 sb->s_magic = le16_to_cpu(es->s_magic);
617ba13b
MC
2493 if (sb->s_magic != EXT4_SUPER_MAGIC)
2494 goto cantfind_ext4;
afc32f7e 2495 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
ac27a0ec
DK
2496
2497 /* Set defaults before we parse the mount options */
2498 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
617ba13b 2499 if (def_mount_opts & EXT4_DEFM_DEBUG)
ac27a0ec 2500 set_opt(sbi->s_mount_opt, DEBUG);
437ca0fd
DM
2501 if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
2502 ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
2503 "2.6.38");
ac27a0ec 2504 set_opt(sbi->s_mount_opt, GRPID);
437ca0fd 2505 }
617ba13b 2506 if (def_mount_opts & EXT4_DEFM_UID16)
ac27a0ec 2507 set_opt(sbi->s_mount_opt, NO_UID32);
03010a33 2508#ifdef CONFIG_EXT4_FS_XATTR
617ba13b 2509 if (def_mount_opts & EXT4_DEFM_XATTR_USER)
ac27a0ec 2510 set_opt(sbi->s_mount_opt, XATTR_USER);
2e7842b8 2511#endif
03010a33 2512#ifdef CONFIG_EXT4_FS_POSIX_ACL
617ba13b 2513 if (def_mount_opts & EXT4_DEFM_ACL)
ac27a0ec 2514 set_opt(sbi->s_mount_opt, POSIX_ACL);
2e7842b8 2515#endif
617ba13b 2516 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
482a7425 2517 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
617ba13b 2518 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
482a7425 2519 set_opt(sbi->s_mount_opt, ORDERED_DATA);
617ba13b 2520 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
482a7425 2521 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
617ba13b
MC
2522
2523 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
ac27a0ec 2524 set_opt(sbi->s_mount_opt, ERRORS_PANIC);
bb4f397a 2525 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
ceea16bf 2526 set_opt(sbi->s_mount_opt, ERRORS_CONT);
bb4f397a
AK
2527 else
2528 set_opt(sbi->s_mount_opt, ERRORS_RO);
ac27a0ec
DK
2529
2530 sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
2531 sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
30773840
TT
2532 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
2533 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
2534 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
ac27a0ec 2535
571640ca 2536 set_opt(sbi->s_mount_opt, BARRIER);
ac27a0ec 2537
dd919b98
AK
2538 /*
2539 * enable delayed allocation by default
2540 * Use -o nodelalloc to turn it off
2541 */
2542 set_opt(sbi->s_mount_opt, DELALLOC);
2543
b3881f74
TT
2544 if (!parse_options((char *) data, sb, &journal_devnum,
2545 &journal_ioprio, NULL, 0))
ac27a0ec
DK
2546 goto failed_mount;
2547
2548 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
482a7425 2549 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec 2550
617ba13b
MC
2551 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
2552 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
2553 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
2554 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
b31e1552
ES
2555 ext4_msg(sb, KERN_WARNING,
2556 "feature flags set on rev 0 fs, "
2557 "running e2fsck is recommended");
469108ff 2558
ac27a0ec
DK
2559 /*
2560 * Check feature flags regardless of the revision level, since we
2561 * previously didn't change the revision level when setting the flags,
2562 * so there is a chance incompat flags are set on a rev 0 filesystem.
2563 */
a13fb1a4 2564 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
ac27a0ec 2565 goto failed_mount;
a13fb1a4 2566
ac27a0ec
DK
2567 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
2568
617ba13b
MC
2569 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
2570 blocksize > EXT4_MAX_BLOCK_SIZE) {
b31e1552
ES
2571 ext4_msg(sb, KERN_ERR,
2572 "Unsupported filesystem blocksize %d", blocksize);
ac27a0ec
DK
2573 goto failed_mount;
2574 }
2575
ac27a0ec 2576 if (sb->s_blocksize != blocksize) {
ce40733c
AK
2577 /* Validate the filesystem blocksize */
2578 if (!sb_set_blocksize(sb, blocksize)) {
b31e1552 2579 ext4_msg(sb, KERN_ERR, "bad block size %d",
ce40733c 2580 blocksize);
ac27a0ec
DK
2581 goto failed_mount;
2582 }
2583
2b2d6d01 2584 brelse(bh);
70bbb3e0
AM
2585 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
2586 offset = do_div(logical_sb_block, blocksize);
2587 bh = sb_bread(sb, logical_sb_block);
ac27a0ec 2588 if (!bh) {
b31e1552
ES
2589 ext4_msg(sb, KERN_ERR,
2590 "Can't read superblock on 2nd try");
ac27a0ec
DK
2591 goto failed_mount;
2592 }
617ba13b 2593 es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
ac27a0ec 2594 sbi->s_es = es;
617ba13b 2595 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
b31e1552
ES
2596 ext4_msg(sb, KERN_ERR,
2597 "Magic mismatch, very weird!");
ac27a0ec
DK
2598 goto failed_mount;
2599 }
2600 }
2601
a13fb1a4
ES
2602 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
2603 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
f287a1a5
TT
2604 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
2605 has_huge_files);
2606 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
ac27a0ec 2607
617ba13b
MC
2608 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
2609 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
2610 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
ac27a0ec
DK
2611 } else {
2612 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
2613 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
617ba13b 2614 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1330593e 2615 (!is_power_of_2(sbi->s_inode_size)) ||
ac27a0ec 2616 (sbi->s_inode_size > blocksize)) {
b31e1552
ES
2617 ext4_msg(sb, KERN_ERR,
2618 "unsupported inode size: %d",
2b2d6d01 2619 sbi->s_inode_size);
ac27a0ec
DK
2620 goto failed_mount;
2621 }
ef7f3835
KS
2622 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
2623 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
ac27a0ec 2624 }
0b8e58a1 2625
0d1ee42f
AR
2626 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
2627 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
8fadc143 2628 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
0d1ee42f 2629 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
d8ea6cf8 2630 !is_power_of_2(sbi->s_desc_size)) {
b31e1552
ES
2631 ext4_msg(sb, KERN_ERR,
2632 "unsupported descriptor size %lu",
0d1ee42f
AR
2633 sbi->s_desc_size);
2634 goto failed_mount;
2635 }
2636 } else
2637 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
0b8e58a1 2638
ac27a0ec 2639 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
ac27a0ec 2640 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
b47b6f38 2641 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
617ba13b 2642 goto cantfind_ext4;
0b8e58a1 2643
617ba13b 2644 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
ac27a0ec 2645 if (sbi->s_inodes_per_block == 0)
617ba13b 2646 goto cantfind_ext4;
ac27a0ec
DK
2647 sbi->s_itb_per_group = sbi->s_inodes_per_group /
2648 sbi->s_inodes_per_block;
0d1ee42f 2649 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
ac27a0ec
DK
2650 sbi->s_sbh = bh;
2651 sbi->s_mount_state = le16_to_cpu(es->s_state);
e57aa839
FW
2652 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
2653 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
0b8e58a1 2654
2b2d6d01 2655 for (i = 0; i < 4; i++)
ac27a0ec
DK
2656 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
2657 sbi->s_def_hash_version = es->s_def_hash_version;
f99b2589
TT
2658 i = le32_to_cpu(es->s_flags);
2659 if (i & EXT2_FLAGS_UNSIGNED_HASH)
2660 sbi->s_hash_unsigned = 3;
2661 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
2662#ifdef __CHAR_UNSIGNED__
2663 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
2664 sbi->s_hash_unsigned = 3;
2665#else
2666 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
2667#endif
2668 sb->s_dirt = 1;
2669 }
ac27a0ec
DK
2670
2671 if (sbi->s_blocks_per_group > blocksize * 8) {
b31e1552
ES
2672 ext4_msg(sb, KERN_ERR,
2673 "#blocks per group too big: %lu",
2b2d6d01 2674 sbi->s_blocks_per_group);
ac27a0ec
DK
2675 goto failed_mount;
2676 }
ac27a0ec 2677 if (sbi->s_inodes_per_group > blocksize * 8) {
b31e1552
ES
2678 ext4_msg(sb, KERN_ERR,
2679 "#inodes per group too big: %lu",
2b2d6d01 2680 sbi->s_inodes_per_group);
ac27a0ec
DK
2681 goto failed_mount;
2682 }
2683
bf43d84b
ES
2684 /*
2685 * Test whether we have more sectors than will fit in sector_t,
2686 * and whether the max offset is addressable by the page cache.
2687 */
2688 if ((ext4_blocks_count(es) >
2689 (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
2690 (ext4_blocks_count(es) >
2691 (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
b31e1552 2692 ext4_msg(sb, KERN_ERR, "filesystem"
bf43d84b 2693 " too large to mount safely on this system");
ac27a0ec 2694 if (sizeof(sector_t) < 8)
90c699a9 2695 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
bf43d84b 2696 ret = -EFBIG;
ac27a0ec
DK
2697 goto failed_mount;
2698 }
2699
617ba13b
MC
2700 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
2701 goto cantfind_ext4;
e7c95593 2702
0f2ddca6
FTN
2703 /* check blocks count against device size */
2704 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
2705 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
b31e1552
ES
2706 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
2707 "exceeds size of device (%llu blocks)",
0f2ddca6
FTN
2708 ext4_blocks_count(es), blocks_count);
2709 goto failed_mount;
2710 }
2711
0b8e58a1
AD
2712 /*
2713 * It makes no sense for the first data block to be beyond the end
2714 * of the filesystem.
2715 */
2716 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
b31e1552
ES
2717 ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
2718 "block %u is beyond end of filesystem (%llu)",
2719 le32_to_cpu(es->s_first_data_block),
2720 ext4_blocks_count(es));
e7c95593
ES
2721 goto failed_mount;
2722 }
bd81d8ee
LV
2723 blocks_count = (ext4_blocks_count(es) -
2724 le32_to_cpu(es->s_first_data_block) +
2725 EXT4_BLOCKS_PER_GROUP(sb) - 1);
2726 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4ec11028 2727 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
b31e1552 2728 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
4ec11028 2729 "(block count %llu, first data block %u, "
b31e1552 2730 "blocks per group %lu)", sbi->s_groups_count,
4ec11028
TT
2731 ext4_blocks_count(es),
2732 le32_to_cpu(es->s_first_data_block),
2733 EXT4_BLOCKS_PER_GROUP(sb));
2734 goto failed_mount;
2735 }
bd81d8ee 2736 sbi->s_groups_count = blocks_count;
fb0a387d
ES
2737 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
2738 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
617ba13b
MC
2739 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
2740 EXT4_DESC_PER_BLOCK(sb);
2b2d6d01 2741 sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
ac27a0ec
DK
2742 GFP_KERNEL);
2743 if (sbi->s_group_desc == NULL) {
b31e1552 2744 ext4_msg(sb, KERN_ERR, "not enough memory");
ac27a0ec
DK
2745 goto failed_mount;
2746 }
2747
3244fcb1 2748#ifdef CONFIG_PROC_FS
9f6200bb
TT
2749 if (ext4_proc_root)
2750 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3244fcb1 2751#endif
240799cd 2752
705895b6 2753 bgl_lock_init(sbi->s_blockgroup_lock);
ac27a0ec
DK
2754
2755 for (i = 0; i < db_count; i++) {
70bbb3e0 2756 block = descriptor_loc(sb, logical_sb_block, i);
ac27a0ec
DK
2757 sbi->s_group_desc[i] = sb_bread(sb, block);
2758 if (!sbi->s_group_desc[i]) {
b31e1552
ES
2759 ext4_msg(sb, KERN_ERR,
2760 "can't read group descriptor %d", i);
ac27a0ec
DK
2761 db_count = i;
2762 goto failed_mount2;
2763 }
2764 }
2b2d6d01 2765 if (!ext4_check_descriptors(sb)) {
b31e1552 2766 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
ac27a0ec
DK
2767 goto failed_mount2;
2768 }
772cb7c8
JS
2769 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2770 if (!ext4_fill_flex_info(sb)) {
b31e1552
ES
2771 ext4_msg(sb, KERN_ERR,
2772 "unable to initialize "
2773 "flex_bg meta info!");
772cb7c8
JS
2774 goto failed_mount2;
2775 }
2776
ac27a0ec
DK
2777 sbi->s_gdb_count = db_count;
2778 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
2779 spin_lock_init(&sbi->s_next_gen_lock);
2780
833f4077
PZ
2781 err = percpu_counter_init(&sbi->s_freeblocks_counter,
2782 ext4_count_free_blocks(sb));
2783 if (!err) {
2784 err = percpu_counter_init(&sbi->s_freeinodes_counter,
2785 ext4_count_free_inodes(sb));
2786 }
2787 if (!err) {
2788 err = percpu_counter_init(&sbi->s_dirs_counter,
2789 ext4_count_dirs(sb));
2790 }
6bc6e63f
AK
2791 if (!err) {
2792 err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
2793 }
833f4077 2794 if (err) {
b31e1552 2795 ext4_msg(sb, KERN_ERR, "insufficient memory");
833f4077
PZ
2796 goto failed_mount3;
2797 }
ac27a0ec 2798
c9de560d 2799 sbi->s_stripe = ext4_get_stripe_size(sbi);
55138e0b 2800 sbi->s_max_writeback_mb_bump = 128;
c9de560d 2801
ac27a0ec
DK
2802 /*
2803 * set up enough so that it can read an inode
2804 */
9ca92389
TT
2805 if (!test_opt(sb, NOLOAD) &&
2806 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
2807 sb->s_op = &ext4_sops;
2808 else
2809 sb->s_op = &ext4_nojournal_sops;
617ba13b
MC
2810 sb->s_export_op = &ext4_export_ops;
2811 sb->s_xattr = ext4_xattr_handlers;
ac27a0ec 2812#ifdef CONFIG_QUOTA
617ba13b
MC
2813 sb->s_qcop = &ext4_qctl_operations;
2814 sb->dq_op = &ext4_quota_operations;
ac27a0ec
DK
2815#endif
2816 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
3b9d4ed2 2817 mutex_init(&sbi->s_orphan_lock);
32ed5058 2818 mutex_init(&sbi->s_resize_lock);
ac27a0ec
DK
2819
2820 sb->s_root = NULL;
2821
2822 needs_recovery = (es->s_last_orphan != 0 ||
617ba13b
MC
2823 EXT4_HAS_INCOMPAT_FEATURE(sb,
2824 EXT4_FEATURE_INCOMPAT_RECOVER));
ac27a0ec
DK
2825
2826 /*
2827 * The first inode we look at is the journal inode. Don't try
2828 * root first: it may be modified in the journal!
2829 */
2830 if (!test_opt(sb, NOLOAD) &&
617ba13b
MC
2831 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
2832 if (ext4_load_journal(sb, es, journal_devnum))
ac27a0ec 2833 goto failed_mount3;
0390131b
FM
2834 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
2835 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
b31e1552
ES
2836 ext4_msg(sb, KERN_ERR, "required journal recovery "
2837 "suppressed and not mounted read-only");
744692dc 2838 goto failed_mount_wq;
ac27a0ec 2839 } else {
0390131b
FM
2840 clear_opt(sbi->s_mount_opt, DATA_FLAGS);
2841 set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
2842 sbi->s_journal = NULL;
2843 needs_recovery = 0;
2844 goto no_journal;
ac27a0ec
DK
2845 }
2846
eb40a09c
JS
2847 if (ext4_blocks_count(es) > 0xffffffffULL &&
2848 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
2849 JBD2_FEATURE_INCOMPAT_64BIT)) {
b31e1552 2850 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
744692dc 2851 goto failed_mount_wq;
eb40a09c
JS
2852 }
2853
d4da6c9c
LT
2854 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
2855 jbd2_journal_set_features(sbi->s_journal,
2856 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
818d276c 2857 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
d4da6c9c
LT
2858 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
2859 jbd2_journal_set_features(sbi->s_journal,
2860 JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
818d276c
GS
2861 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
2862 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
d4da6c9c
LT
2863 } else {
2864 jbd2_journal_clear_features(sbi->s_journal,
2865 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
2866 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
2867 }
818d276c 2868
ac27a0ec
DK
2869 /* We have now updated the journal if required, so we can
2870 * validate the data journaling mode. */
2871 switch (test_opt(sb, DATA_FLAGS)) {
2872 case 0:
2873 /* No mode set, assume a default based on the journal
63f57933
AM
2874 * capabilities: ORDERED_DATA if the journal can
2875 * cope, else JOURNAL_DATA
2876 */
dab291af
MC
2877 if (jbd2_journal_check_available_features
2878 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
ac27a0ec
DK
2879 set_opt(sbi->s_mount_opt, ORDERED_DATA);
2880 else
2881 set_opt(sbi->s_mount_opt, JOURNAL_DATA);
2882 break;
2883
617ba13b
MC
2884 case EXT4_MOUNT_ORDERED_DATA:
2885 case EXT4_MOUNT_WRITEBACK_DATA:
dab291af
MC
2886 if (!jbd2_journal_check_available_features
2887 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
b31e1552
ES
2888 ext4_msg(sb, KERN_ERR, "Journal does not support "
2889 "requested data journaling mode");
744692dc 2890 goto failed_mount_wq;
ac27a0ec
DK
2891 }
2892 default:
2893 break;
2894 }
b3881f74 2895 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
ac27a0ec 2896
0390131b 2897no_journal:
ac27a0ec 2898 if (test_opt(sb, NOBH)) {
617ba13b 2899 if (!(test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)) {
b31e1552
ES
2900 ext4_msg(sb, KERN_WARNING, "Ignoring nobh option - "
2901 "its supported only with writeback mode");
ac27a0ec
DK
2902 clear_opt(sbi->s_mount_opt, NOBH);
2903 }
744692dc
JZ
2904 if (test_opt(sb, DIOREAD_NOLOCK)) {
2905 ext4_msg(sb, KERN_WARNING, "dioread_nolock option is "
2906 "not supported with nobh mode");
2907 goto failed_mount_wq;
2908 }
ac27a0ec 2909 }
4c0425ff
MC
2910 EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
2911 if (!EXT4_SB(sb)->dio_unwritten_wq) {
2912 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
2913 goto failed_mount_wq;
2914 }
2915
ac27a0ec 2916 /*
dab291af 2917 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
2918 * so we can safely mount the rest of the filesystem now.
2919 */
2920
1d1fe1ee
DH
2921 root = ext4_iget(sb, EXT4_ROOT_INO);
2922 if (IS_ERR(root)) {
b31e1552 2923 ext4_msg(sb, KERN_ERR, "get root inode failed");
1d1fe1ee 2924 ret = PTR_ERR(root);
ac27a0ec
DK
2925 goto failed_mount4;
2926 }
2927 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
1d1fe1ee 2928 iput(root);
b31e1552 2929 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
ac27a0ec
DK
2930 goto failed_mount4;
2931 }
1d1fe1ee
DH
2932 sb->s_root = d_alloc_root(root);
2933 if (!sb->s_root) {
b31e1552 2934 ext4_msg(sb, KERN_ERR, "get root dentry failed");
1d1fe1ee
DH
2935 iput(root);
2936 ret = -ENOMEM;
2937 goto failed_mount4;
2938 }
ac27a0ec 2939
2b2d6d01 2940 ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
ef7f3835
KS
2941
2942 /* determine the minimum size of new large inodes, if present */
2943 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
2944 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2945 EXT4_GOOD_OLD_INODE_SIZE;
2946 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
2947 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
2948 if (sbi->s_want_extra_isize <
2949 le16_to_cpu(es->s_want_extra_isize))
2950 sbi->s_want_extra_isize =
2951 le16_to_cpu(es->s_want_extra_isize);
2952 if (sbi->s_want_extra_isize <
2953 le16_to_cpu(es->s_min_extra_isize))
2954 sbi->s_want_extra_isize =
2955 le16_to_cpu(es->s_min_extra_isize);
2956 }
2957 }
2958 /* Check if enough inode space is available */
2959 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
2960 sbi->s_inode_size) {
2961 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
2962 EXT4_GOOD_OLD_INODE_SIZE;
b31e1552
ES
2963 ext4_msg(sb, KERN_INFO, "required extra inode space not"
2964 "available");
ef7f3835
KS
2965 }
2966
90576c0b
TT
2967 if (test_opt(sb, DELALLOC) &&
2968 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
b31e1552
ES
2969 ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
2970 "requested data journaling mode");
c2774d84 2971 clear_opt(sbi->s_mount_opt, DELALLOC);
90576c0b 2972 }
744692dc
JZ
2973 if (test_opt(sb, DIOREAD_NOLOCK)) {
2974 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
2975 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2976 "option - requested data journaling mode");
2977 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2978 }
2979 if (sb->s_blocksize < PAGE_SIZE) {
2980 ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
2981 "option - block size is too small");
2982 clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
2983 }
2984 }
c2774d84 2985
6fd058f7
TT
2986 err = ext4_setup_system_zone(sb);
2987 if (err) {
b31e1552
ES
2988 ext4_msg(sb, KERN_ERR, "failed to initialize system "
2989 "zone (%d)\n", err);
6fd058f7
TT
2990 goto failed_mount4;
2991 }
2992
c2774d84
AK
2993 ext4_ext_init(sb);
2994 err = ext4_mb_init(sb, needs_recovery);
2995 if (err) {
b31e1552
ES
2996 ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
2997 err);
c2774d84
AK
2998 goto failed_mount4;
2999 }
3000
3197ebdb
TT
3001 sbi->s_kobj.kset = ext4_kset;
3002 init_completion(&sbi->s_kobj_unregister);
3003 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
3004 "%s", sb->s_id);
3005 if (err) {
3006 ext4_mb_release(sb);
3007 ext4_ext_release(sb);
3008 goto failed_mount4;
3009 };
3010
617ba13b
MC
3011 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3012 ext4_orphan_cleanup(sb, es);
3013 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
0390131b 3014 if (needs_recovery) {
b31e1552 3015 ext4_msg(sb, KERN_INFO, "recovery complete");
0390131b
FM
3016 ext4_mark_recovery_complete(sb, es);
3017 }
3018 if (EXT4_SB(sb)->s_journal) {
3019 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
3020 descr = " journalled data mode";
3021 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
3022 descr = " ordered data mode";
3023 else
3024 descr = " writeback data mode";
3025 } else
3026 descr = "out journal";
3027
b31e1552 3028 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s", descr);
ac27a0ec
DK
3029
3030 lock_kernel();
3031 return 0;
3032
617ba13b 3033cantfind_ext4:
ac27a0ec 3034 if (!silent)
b31e1552 3035 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
ac27a0ec
DK
3036 goto failed_mount;
3037
3038failed_mount4:
b31e1552 3039 ext4_msg(sb, KERN_ERR, "mount failed");
4c0425ff
MC
3040 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
3041failed_mount_wq:
6fd058f7 3042 ext4_release_system_zone(sb);
0390131b
FM
3043 if (sbi->s_journal) {
3044 jbd2_journal_destroy(sbi->s_journal);
3045 sbi->s_journal = NULL;
3046 }
ac27a0ec 3047failed_mount3:
c5ca7c76
TT
3048 if (sbi->s_flex_groups) {
3049 if (is_vmalloc_addr(sbi->s_flex_groups))
3050 vfree(sbi->s_flex_groups);
3051 else
3052 kfree(sbi->s_flex_groups);
3053 }
ac27a0ec
DK
3054 percpu_counter_destroy(&sbi->s_freeblocks_counter);
3055 percpu_counter_destroy(&sbi->s_freeinodes_counter);
3056 percpu_counter_destroy(&sbi->s_dirs_counter);
6bc6e63f 3057 percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
ac27a0ec
DK
3058failed_mount2:
3059 for (i = 0; i < db_count; i++)
3060 brelse(sbi->s_group_desc[i]);
3061 kfree(sbi->s_group_desc);
3062failed_mount:
240799cd 3063 if (sbi->s_proc) {
9f6200bb 3064 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 3065 }
ac27a0ec
DK
3066#ifdef CONFIG_QUOTA
3067 for (i = 0; i < MAXQUOTAS; i++)
3068 kfree(sbi->s_qf_names[i]);
3069#endif
617ba13b 3070 ext4_blkdev_remove(sbi);
ac27a0ec
DK
3071 brelse(bh);
3072out_fail:
3073 sb->s_fs_info = NULL;
f6830165 3074 kfree(sbi->s_blockgroup_lock);
ac27a0ec
DK
3075 kfree(sbi);
3076 lock_kernel();
1d1fe1ee 3077 return ret;
ac27a0ec
DK
3078}
3079
3080/*
3081 * Setup any per-fs journal parameters now. We'll do this both on
3082 * initial mount, once the journal has been initialised but before we've
3083 * done any recovery; and again on any subsequent remount.
3084 */
617ba13b 3085static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 3086{
617ba13b 3087 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 3088
30773840
TT
3089 journal->j_commit_interval = sbi->s_commit_interval;
3090 journal->j_min_batch_time = sbi->s_min_batch_time;
3091 journal->j_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
3092
3093 spin_lock(&journal->j_state_lock);
3094 if (test_opt(sb, BARRIER))
dab291af 3095 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 3096 else
dab291af 3097 journal->j_flags &= ~JBD2_BARRIER;
5bf5683a
HK
3098 if (test_opt(sb, DATA_ERR_ABORT))
3099 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
3100 else
3101 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
ac27a0ec
DK
3102 spin_unlock(&journal->j_state_lock);
3103}
3104
617ba13b 3105static journal_t *ext4_get_journal(struct super_block *sb,
ac27a0ec
DK
3106 unsigned int journal_inum)
3107{
3108 struct inode *journal_inode;
3109 journal_t *journal;
3110
0390131b
FM
3111 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3112
ac27a0ec
DK
3113 /* First, test for the existence of a valid inode on disk. Bad
3114 * things happen if we iget() an unused inode, as the subsequent
3115 * iput() will try to delete it. */
3116
1d1fe1ee
DH
3117 journal_inode = ext4_iget(sb, journal_inum);
3118 if (IS_ERR(journal_inode)) {
b31e1552 3119 ext4_msg(sb, KERN_ERR, "no journal found");
ac27a0ec
DK
3120 return NULL;
3121 }
3122 if (!journal_inode->i_nlink) {
3123 make_bad_inode(journal_inode);
3124 iput(journal_inode);
b31e1552 3125 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
ac27a0ec
DK
3126 return NULL;
3127 }
3128
e5f8eab8 3129 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
ac27a0ec 3130 journal_inode, journal_inode->i_size);
1d1fe1ee 3131 if (!S_ISREG(journal_inode->i_mode)) {
b31e1552 3132 ext4_msg(sb, KERN_ERR, "invalid journal inode");
ac27a0ec
DK
3133 iput(journal_inode);
3134 return NULL;
3135 }
3136
dab291af 3137 journal = jbd2_journal_init_inode(journal_inode);
ac27a0ec 3138 if (!journal) {
b31e1552 3139 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
ac27a0ec
DK
3140 iput(journal_inode);
3141 return NULL;
3142 }
3143 journal->j_private = sb;
617ba13b 3144 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
3145 return journal;
3146}
3147
617ba13b 3148static journal_t *ext4_get_dev_journal(struct super_block *sb,
ac27a0ec
DK
3149 dev_t j_dev)
3150{
2b2d6d01 3151 struct buffer_head *bh;
ac27a0ec 3152 journal_t *journal;
617ba13b
MC
3153 ext4_fsblk_t start;
3154 ext4_fsblk_t len;
ac27a0ec 3155 int hblock, blocksize;
617ba13b 3156 ext4_fsblk_t sb_block;
ac27a0ec 3157 unsigned long offset;
2b2d6d01 3158 struct ext4_super_block *es;
ac27a0ec
DK
3159 struct block_device *bdev;
3160
0390131b
FM
3161 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3162
b31e1552 3163 bdev = ext4_blkdev_get(j_dev, sb);
ac27a0ec
DK
3164 if (bdev == NULL)
3165 return NULL;
3166
3167 if (bd_claim(bdev, sb)) {
b31e1552
ES
3168 ext4_msg(sb, KERN_ERR,
3169 "failed to claim external journal device");
9a1c3542 3170 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
ac27a0ec
DK
3171 return NULL;
3172 }
3173
3174 blocksize = sb->s_blocksize;
e1defc4f 3175 hblock = bdev_logical_block_size(bdev);
ac27a0ec 3176 if (blocksize < hblock) {
b31e1552
ES
3177 ext4_msg(sb, KERN_ERR,
3178 "blocksize too small for journal device");
ac27a0ec
DK
3179 goto out_bdev;
3180 }
3181
617ba13b
MC
3182 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
3183 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ac27a0ec
DK
3184 set_blocksize(bdev, blocksize);
3185 if (!(bh = __bread(bdev, sb_block, blocksize))) {
b31e1552
ES
3186 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
3187 "external journal");
ac27a0ec
DK
3188 goto out_bdev;
3189 }
3190
617ba13b
MC
3191 es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
3192 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 3193 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b 3194 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
b31e1552
ES
3195 ext4_msg(sb, KERN_ERR, "external journal has "
3196 "bad superblock");
ac27a0ec
DK
3197 brelse(bh);
3198 goto out_bdev;
3199 }
3200
617ba13b 3201 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
b31e1552 3202 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
ac27a0ec
DK
3203 brelse(bh);
3204 goto out_bdev;
3205 }
3206
bd81d8ee 3207 len = ext4_blocks_count(es);
ac27a0ec
DK
3208 start = sb_block + 1;
3209 brelse(bh); /* we're done with the superblock */
3210
dab291af 3211 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
ac27a0ec
DK
3212 start, len, blocksize);
3213 if (!journal) {
b31e1552 3214 ext4_msg(sb, KERN_ERR, "failed to create device journal");
ac27a0ec
DK
3215 goto out_bdev;
3216 }
3217 journal->j_private = sb;
3218 ll_rw_block(READ, 1, &journal->j_sb_buffer);
3219 wait_on_buffer(journal->j_sb_buffer);
3220 if (!buffer_uptodate(journal->j_sb_buffer)) {
b31e1552 3221 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
ac27a0ec
DK
3222 goto out_journal;
3223 }
3224 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
b31e1552
ES
3225 ext4_msg(sb, KERN_ERR, "External journal has more than one "
3226 "user (unsupported) - %d",
ac27a0ec
DK
3227 be32_to_cpu(journal->j_superblock->s_nr_users));
3228 goto out_journal;
3229 }
617ba13b
MC
3230 EXT4_SB(sb)->journal_bdev = bdev;
3231 ext4_init_journal_params(sb, journal);
ac27a0ec 3232 return journal;
0b8e58a1 3233
ac27a0ec 3234out_journal:
dab291af 3235 jbd2_journal_destroy(journal);
ac27a0ec 3236out_bdev:
617ba13b 3237 ext4_blkdev_put(bdev);
ac27a0ec
DK
3238 return NULL;
3239}
3240
617ba13b
MC
3241static int ext4_load_journal(struct super_block *sb,
3242 struct ext4_super_block *es,
ac27a0ec
DK
3243 unsigned long journal_devnum)
3244{
3245 journal_t *journal;
3246 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
3247 dev_t journal_dev;
3248 int err = 0;
3249 int really_read_only;
3250
0390131b
FM
3251 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3252
ac27a0ec
DK
3253 if (journal_devnum &&
3254 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
b31e1552
ES
3255 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
3256 "numbers have changed");
ac27a0ec
DK
3257 journal_dev = new_decode_dev(journal_devnum);
3258 } else
3259 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
3260
3261 really_read_only = bdev_read_only(sb->s_bdev);
3262
3263 /*
3264 * Are we loading a blank journal or performing recovery after a
3265 * crash? For recovery, we need to check in advance whether we
3266 * can get read-write access to the device.
3267 */
617ba13b 3268 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
ac27a0ec 3269 if (sb->s_flags & MS_RDONLY) {
b31e1552
ES
3270 ext4_msg(sb, KERN_INFO, "INFO: recovery "
3271 "required on readonly filesystem");
ac27a0ec 3272 if (really_read_only) {
b31e1552
ES
3273 ext4_msg(sb, KERN_ERR, "write access "
3274 "unavailable, cannot proceed");
ac27a0ec
DK
3275 return -EROFS;
3276 }
b31e1552
ES
3277 ext4_msg(sb, KERN_INFO, "write access will "
3278 "be enabled during recovery");
ac27a0ec
DK
3279 }
3280 }
3281
3282 if (journal_inum && journal_dev) {
b31e1552
ES
3283 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
3284 "and inode journals!");
ac27a0ec
DK
3285 return -EINVAL;
3286 }
3287
3288 if (journal_inum) {
617ba13b 3289 if (!(journal = ext4_get_journal(sb, journal_inum)))
ac27a0ec
DK
3290 return -EINVAL;
3291 } else {
617ba13b 3292 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
ac27a0ec
DK
3293 return -EINVAL;
3294 }
3295
90576c0b 3296 if (!(journal->j_flags & JBD2_BARRIER))
b31e1552 3297 ext4_msg(sb, KERN_INFO, "barriers disabled");
4776004f 3298
ac27a0ec 3299 if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
dab291af 3300 err = jbd2_journal_update_format(journal);
ac27a0ec 3301 if (err) {
b31e1552 3302 ext4_msg(sb, KERN_ERR, "error updating journal");
dab291af 3303 jbd2_journal_destroy(journal);
ac27a0ec
DK
3304 return err;
3305 }
3306 }
3307
617ba13b 3308 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
dab291af 3309 err = jbd2_journal_wipe(journal, !really_read_only);
ac27a0ec 3310 if (!err)
dab291af 3311 err = jbd2_journal_load(journal);
ac27a0ec
DK
3312
3313 if (err) {
b31e1552 3314 ext4_msg(sb, KERN_ERR, "error loading journal");
dab291af 3315 jbd2_journal_destroy(journal);
ac27a0ec
DK
3316 return err;
3317 }
3318
617ba13b
MC
3319 EXT4_SB(sb)->s_journal = journal;
3320 ext4_clear_journal_err(sb, es);
ac27a0ec
DK
3321
3322 if (journal_devnum &&
3323 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
3324 es->s_journal_dev = cpu_to_le32(journal_devnum);
ac27a0ec
DK
3325
3326 /* Make sure we flush the recovery flag to disk. */
e2d67052 3327 ext4_commit_super(sb, 1);
ac27a0ec
DK
3328 }
3329
3330 return 0;
3331}
3332
e2d67052 3333static int ext4_commit_super(struct super_block *sb, int sync)
ac27a0ec 3334{
e2d67052 3335 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
617ba13b 3336 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
c4be0c1d 3337 int error = 0;
ac27a0ec
DK
3338
3339 if (!sbh)
c4be0c1d 3340 return error;
914258bf
TT
3341 if (buffer_write_io_error(sbh)) {
3342 /*
3343 * Oh, dear. A previous attempt to write the
3344 * superblock failed. This could happen because the
3345 * USB device was yanked out. Or it could happen to
3346 * be a transient write error and maybe the block will
3347 * be remapped. Nothing we can do but to retry the
3348 * write and hope for the best.
3349 */
b31e1552
ES
3350 ext4_msg(sb, KERN_ERR, "previous I/O error to "
3351 "superblock detected");
914258bf
TT
3352 clear_buffer_write_io_error(sbh);
3353 set_buffer_uptodate(sbh);
3354 }
71290b36
TT
3355 /*
3356 * If the file system is mounted read-only, don't update the
3357 * superblock write time. This avoids updating the superblock
3358 * write time when we are mounting the root file system
3359 * read/only but we need to replay the journal; at that point,
3360 * for people who are east of GMT and who make their clock
3361 * tick in localtime for Windows bug-for-bug compatibility,
3362 * the clock is set in the future, and this will cause e2fsck
3363 * to complain and force a full file system check.
3364 */
3365 if (!(sb->s_flags & MS_RDONLY))
3366 es->s_wtime = cpu_to_le32(get_seconds());
afc32f7e
TT
3367 es->s_kbytes_written =
3368 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
3369 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
3370 EXT4_SB(sb)->s_sectors_written_start) >> 1));
5d1b1b3f
AK
3371 ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
3372 &EXT4_SB(sb)->s_freeblocks_counter));
3373 es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
3374 &EXT4_SB(sb)->s_freeinodes_counter));
7234ab2a 3375 sb->s_dirt = 0;
ac27a0ec
DK
3376 BUFFER_TRACE(sbh, "marking dirty");
3377 mark_buffer_dirty(sbh);
914258bf 3378 if (sync) {
c4be0c1d
TS
3379 error = sync_dirty_buffer(sbh);
3380 if (error)
3381 return error;
3382
3383 error = buffer_write_io_error(sbh);
3384 if (error) {
b31e1552
ES
3385 ext4_msg(sb, KERN_ERR, "I/O error while writing "
3386 "superblock");
914258bf
TT
3387 clear_buffer_write_io_error(sbh);
3388 set_buffer_uptodate(sbh);
3389 }
3390 }
c4be0c1d 3391 return error;
ac27a0ec
DK
3392}
3393
ac27a0ec
DK
3394/*
3395 * Have we just finished recovery? If so, and if we are mounting (or
3396 * remounting) the filesystem readonly, then we will end up with a
3397 * consistent fs on disk. Record that fact.
3398 */
2b2d6d01
TT
3399static void ext4_mark_recovery_complete(struct super_block *sb,
3400 struct ext4_super_block *es)
ac27a0ec 3401{
617ba13b 3402 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 3403
0390131b
FM
3404 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3405 BUG_ON(journal != NULL);
3406 return;
3407 }
dab291af 3408 jbd2_journal_lock_updates(journal);
7ffe1ea8
HK
3409 if (jbd2_journal_flush(journal) < 0)
3410 goto out;
3411
617ba13b 3412 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
ac27a0ec 3413 sb->s_flags & MS_RDONLY) {
617ba13b 3414 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
e2d67052 3415 ext4_commit_super(sb, 1);
ac27a0ec 3416 }
7ffe1ea8
HK
3417
3418out:
dab291af 3419 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
3420}
3421
3422/*
3423 * If we are mounting (or read-write remounting) a filesystem whose journal
3424 * has recorded an error from a previous lifetime, move that error to the
3425 * main filesystem now.
3426 */
2b2d6d01
TT
3427static void ext4_clear_journal_err(struct super_block *sb,
3428 struct ext4_super_block *es)
ac27a0ec
DK
3429{
3430 journal_t *journal;
3431 int j_errno;
3432 const char *errstr;
3433
0390131b
FM
3434 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
3435
617ba13b 3436 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
3437
3438 /*
3439 * Now check for any error status which may have been recorded in the
617ba13b 3440 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
3441 */
3442
dab291af 3443 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
3444 if (j_errno) {
3445 char nbuf[16];
3446
617ba13b 3447 errstr = ext4_decode_error(sb, j_errno, nbuf);
12062ddd 3448 ext4_warning(sb, "Filesystem error recorded "
ac27a0ec 3449 "from previous mount: %s", errstr);
12062ddd 3450 ext4_warning(sb, "Marking fs in need of filesystem check.");
ac27a0ec 3451
617ba13b
MC
3452 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
3453 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
e2d67052 3454 ext4_commit_super(sb, 1);
ac27a0ec 3455
dab291af 3456 jbd2_journal_clear_err(journal);
ac27a0ec
DK
3457 }
3458}
3459
3460/*
3461 * Force the running and committing transactions to commit,
3462 * and wait on the commit.
3463 */
617ba13b 3464int ext4_force_commit(struct super_block *sb)
ac27a0ec
DK
3465{
3466 journal_t *journal;
0390131b 3467 int ret = 0;
ac27a0ec
DK
3468
3469 if (sb->s_flags & MS_RDONLY)
3470 return 0;
3471
617ba13b 3472 journal = EXT4_SB(sb)->s_journal;
7234ab2a 3473 if (journal)
0390131b 3474 ret = ext4_journal_force_commit(journal);
0390131b 3475
ac27a0ec
DK
3476 return ret;
3477}
3478
2b2d6d01 3479static void ext4_write_super(struct super_block *sb)
ac27a0ec 3480{
ebc1ac16 3481 lock_super(sb);
9ca92389 3482 ext4_commit_super(sb, 1);
ebc1ac16 3483 unlock_super(sb);
ac27a0ec
DK
3484}
3485
617ba13b 3486static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec 3487{
14ce0cb4 3488 int ret = 0;
9eddacf9 3489 tid_t target;
8d5d02e6 3490 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 3491
9bffad1e 3492 trace_ext4_sync_fs(sb, wait);
8d5d02e6
MC
3493 flush_workqueue(sbi->dio_unwritten_wq);
3494 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
9ca92389 3495 if (wait)
8d5d02e6 3496 jbd2_log_wait_commit(sbi->s_journal, target);
0390131b 3497 }
14ce0cb4 3498 return ret;
ac27a0ec
DK
3499}
3500
3501/*
3502 * LVM calls this function before a (read-only) snapshot is created. This
3503 * gives us a chance to flush the journal completely and mark the fs clean.
3504 */
c4be0c1d 3505static int ext4_freeze(struct super_block *sb)
ac27a0ec 3506{
c4be0c1d
TS
3507 int error = 0;
3508 journal_t *journal;
ac27a0ec 3509
9ca92389
TT
3510 if (sb->s_flags & MS_RDONLY)
3511 return 0;
ac27a0ec 3512
9ca92389 3513 journal = EXT4_SB(sb)->s_journal;
7ffe1ea8 3514
9ca92389
TT
3515 /* Now we set up the journal barrier. */
3516 jbd2_journal_lock_updates(journal);
ac27a0ec 3517
9ca92389
TT
3518 /*
3519 * Don't clear the needs_recovery flag if we failed to flush
3520 * the journal.
3521 */
3522 error = jbd2_journal_flush(journal);
3523 if (error < 0) {
3524 out:
3525 jbd2_journal_unlock_updates(journal);
3526 return error;
ac27a0ec 3527 }
9ca92389
TT
3528
3529 /* Journal blocked and flushed, clear needs_recovery flag. */
3530 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3531 error = ext4_commit_super(sb, 1);
3532 if (error)
3533 goto out;
c4be0c1d 3534 return 0;
ac27a0ec
DK
3535}
3536
3537/*
3538 * Called by LVM after the snapshot is done. We need to reset the RECOVER
3539 * flag here, even though the filesystem is not technically dirty yet.
3540 */
c4be0c1d 3541static int ext4_unfreeze(struct super_block *sb)
ac27a0ec 3542{
9ca92389
TT
3543 if (sb->s_flags & MS_RDONLY)
3544 return 0;
3545
3546 lock_super(sb);
3547 /* Reset the needs_recovery flag before the fs is unlocked. */
3548 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
3549 ext4_commit_super(sb, 1);
3550 unlock_super(sb);
3551 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
c4be0c1d 3552 return 0;
ac27a0ec
DK
3553}
3554
2b2d6d01 3555static int ext4_remount(struct super_block *sb, int *flags, char *data)
ac27a0ec 3556{
2b2d6d01 3557 struct ext4_super_block *es;
617ba13b
MC
3558 struct ext4_sb_info *sbi = EXT4_SB(sb);
3559 ext4_fsblk_t n_blocks_count = 0;
ac27a0ec 3560 unsigned long old_sb_flags;
617ba13b 3561 struct ext4_mount_options old_opts;
8a266467 3562 ext4_group_t g;
b3881f74 3563 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
ac27a0ec
DK
3564 int err;
3565#ifdef CONFIG_QUOTA
3566 int i;
3567#endif
3568
337eb00a
AIB
3569 lock_kernel();
3570
ac27a0ec 3571 /* Store the original options */
bbd6851a 3572 lock_super(sb);
ac27a0ec
DK
3573 old_sb_flags = sb->s_flags;
3574 old_opts.s_mount_opt = sbi->s_mount_opt;
3575 old_opts.s_resuid = sbi->s_resuid;
3576 old_opts.s_resgid = sbi->s_resgid;
3577 old_opts.s_commit_interval = sbi->s_commit_interval;
30773840
TT
3578 old_opts.s_min_batch_time = sbi->s_min_batch_time;
3579 old_opts.s_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
3580#ifdef CONFIG_QUOTA
3581 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
3582 for (i = 0; i < MAXQUOTAS; i++)
3583 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
3584#endif
b3881f74
TT
3585 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
3586 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
ac27a0ec
DK
3587
3588 /*
3589 * Allow the "check" option to be passed as a remount option.
3590 */
b3881f74
TT
3591 if (!parse_options(data, sb, NULL, &journal_ioprio,
3592 &n_blocks_count, 1)) {
ac27a0ec
DK
3593 err = -EINVAL;
3594 goto restore_opts;
3595 }
3596
4ab2f15b 3597 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
46e665e9 3598 ext4_abort(sb, __func__, "Abort forced by user");
ac27a0ec
DK
3599
3600 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
482a7425 3601 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec
DK
3602
3603 es = sbi->s_es;
3604
b3881f74 3605 if (sbi->s_journal) {
0390131b 3606 ext4_init_journal_params(sb, sbi->s_journal);
b3881f74
TT
3607 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
3608 }
ac27a0ec
DK
3609
3610 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
bd81d8ee 3611 n_blocks_count > ext4_blocks_count(es)) {
4ab2f15b 3612 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
ac27a0ec
DK
3613 err = -EROFS;
3614 goto restore_opts;
3615 }
3616
3617 if (*flags & MS_RDONLY) {
3618 /*
3619 * First of all, the unconditional stuff we have to do
3620 * to disable replay of the journal when we next remount
3621 */
3622 sb->s_flags |= MS_RDONLY;
3623
3624 /*
3625 * OK, test if we are remounting a valid rw partition
3626 * readonly, and if so set the rdonly flag and then
3627 * mark the partition as valid again.
3628 */
617ba13b
MC
3629 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
3630 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
3631 es->s_state = cpu_to_le16(sbi->s_mount_state);
3632
a63c9eb2 3633 if (sbi->s_journal)
0390131b 3634 ext4_mark_recovery_complete(sb, es);
ac27a0ec 3635 } else {
a13fb1a4
ES
3636 /* Make sure we can mount this feature set readwrite */
3637 if (!ext4_feature_set_ok(sb, 0)) {
ac27a0ec
DK
3638 err = -EROFS;
3639 goto restore_opts;
3640 }
8a266467
TT
3641 /*
3642 * Make sure the group descriptor checksums
0b8e58a1 3643 * are sane. If they aren't, refuse to remount r/w.
8a266467
TT
3644 */
3645 for (g = 0; g < sbi->s_groups_count; g++) {
3646 struct ext4_group_desc *gdp =
3647 ext4_get_group_desc(sb, g, NULL);
3648
3649 if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
b31e1552
ES
3650 ext4_msg(sb, KERN_ERR,
3651 "ext4_remount: Checksum for group %u failed (%u!=%u)",
8a266467
TT
3652 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
3653 le16_to_cpu(gdp->bg_checksum));
3654 err = -EINVAL;
3655 goto restore_opts;
3656 }
3657 }
3658
ead6596b
ES
3659 /*
3660 * If we have an unprocessed orphan list hanging
3661 * around from a previously readonly bdev mount,
3662 * require a full umount/remount for now.
3663 */
3664 if (es->s_last_orphan) {
b31e1552 3665 ext4_msg(sb, KERN_WARNING, "Couldn't "
ead6596b
ES
3666 "remount RDWR because of unprocessed "
3667 "orphan inode list. Please "
b31e1552 3668 "umount/remount instead");
ead6596b
ES
3669 err = -EINVAL;
3670 goto restore_opts;
3671 }
3672
ac27a0ec
DK
3673 /*
3674 * Mounting a RDONLY partition read-write, so reread
3675 * and store the current valid flag. (It may have
3676 * been changed by e2fsck since we originally mounted
3677 * the partition.)
3678 */
0390131b
FM
3679 if (sbi->s_journal)
3680 ext4_clear_journal_err(sb, es);
ac27a0ec 3681 sbi->s_mount_state = le16_to_cpu(es->s_state);
617ba13b 3682 if ((err = ext4_group_extend(sb, es, n_blocks_count)))
ac27a0ec 3683 goto restore_opts;
2b2d6d01 3684 if (!ext4_setup_super(sb, es, 0))
ac27a0ec
DK
3685 sb->s_flags &= ~MS_RDONLY;
3686 }
3687 }
6fd058f7 3688 ext4_setup_system_zone(sb);
0390131b 3689 if (sbi->s_journal == NULL)
e2d67052 3690 ext4_commit_super(sb, 1);
0390131b 3691
ac27a0ec
DK
3692#ifdef CONFIG_QUOTA
3693 /* Release old quota file names */
3694 for (i = 0; i < MAXQUOTAS; i++)
3695 if (old_opts.s_qf_names[i] &&
3696 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3697 kfree(old_opts.s_qf_names[i]);
3698#endif
bbd6851a 3699 unlock_super(sb);
337eb00a 3700 unlock_kernel();
ac27a0ec 3701 return 0;
0b8e58a1 3702
ac27a0ec
DK
3703restore_opts:
3704 sb->s_flags = old_sb_flags;
3705 sbi->s_mount_opt = old_opts.s_mount_opt;
3706 sbi->s_resuid = old_opts.s_resuid;
3707 sbi->s_resgid = old_opts.s_resgid;
3708 sbi->s_commit_interval = old_opts.s_commit_interval;
30773840
TT
3709 sbi->s_min_batch_time = old_opts.s_min_batch_time;
3710 sbi->s_max_batch_time = old_opts.s_max_batch_time;
ac27a0ec
DK
3711#ifdef CONFIG_QUOTA
3712 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
3713 for (i = 0; i < MAXQUOTAS; i++) {
3714 if (sbi->s_qf_names[i] &&
3715 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
3716 kfree(sbi->s_qf_names[i]);
3717 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
3718 }
3719#endif
bbd6851a 3720 unlock_super(sb);
337eb00a 3721 unlock_kernel();
ac27a0ec
DK
3722 return err;
3723}
3724
2b2d6d01 3725static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
ac27a0ec
DK
3726{
3727 struct super_block *sb = dentry->d_sb;
617ba13b
MC
3728 struct ext4_sb_info *sbi = EXT4_SB(sb);
3729 struct ext4_super_block *es = sbi->s_es;
960cc398 3730 u64 fsid;
ac27a0ec 3731
5e70030d
BP
3732 if (test_opt(sb, MINIX_DF)) {
3733 sbi->s_overhead_last = 0;
6bc9feff 3734 } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
8df9675f 3735 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
5e70030d 3736 ext4_fsblk_t overhead = 0;
ac27a0ec
DK
3737
3738 /*
5e70030d
BP
3739 * Compute the overhead (FS structures). This is constant
3740 * for a given filesystem unless the number of block groups
3741 * changes so we cache the previous value until it does.
ac27a0ec
DK
3742 */
3743
3744 /*
3745 * All of the blocks before first_data_block are
3746 * overhead
3747 */
3748 overhead = le32_to_cpu(es->s_first_data_block);
3749
3750 /*
3751 * Add the overhead attributed to the superblock and
3752 * block group descriptors. If the sparse superblocks
3753 * feature is turned on, then not all groups have this.
3754 */
3755 for (i = 0; i < ngroups; i++) {
617ba13b
MC
3756 overhead += ext4_bg_has_super(sb, i) +
3757 ext4_bg_num_gdb(sb, i);
ac27a0ec
DK
3758 cond_resched();
3759 }
3760
3761 /*
3762 * Every block group has an inode bitmap, a block
3763 * bitmap, and an inode table.
3764 */
5e70030d
BP
3765 overhead += ngroups * (2 + sbi->s_itb_per_group);
3766 sbi->s_overhead_last = overhead;
3767 smp_wmb();
6bc9feff 3768 sbi->s_blocks_last = ext4_blocks_count(es);
ac27a0ec
DK
3769 }
3770
617ba13b 3771 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 3772 buf->f_bsize = sb->s_blocksize;
5e70030d 3773 buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
6bc6e63f
AK
3774 buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
3775 percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
bd81d8ee
LV
3776 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
3777 if (buf->f_bfree < ext4_r_blocks_count(es))
ac27a0ec
DK
3778 buf->f_bavail = 0;
3779 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 3780 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
617ba13b 3781 buf->f_namelen = EXT4_NAME_LEN;
960cc398
PE
3782 fsid = le64_to_cpup((void *)es->s_uuid) ^
3783 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
3784 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
3785 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
0b8e58a1 3786
ac27a0ec
DK
3787 return 0;
3788}
3789
0b8e58a1
AD
3790/* Helper function for writing quotas on sync - we need to start transaction
3791 * before quota file is locked for write. Otherwise the are possible deadlocks:
ac27a0ec 3792 * Process 1 Process 2
617ba13b 3793 * ext4_create() quota_sync()
a269eb18 3794 * jbd2_journal_start() write_dquot()
871a2931 3795 * dquot_initialize() down(dqio_mutex)
dab291af 3796 * down(dqio_mutex) jbd2_journal_start()
ac27a0ec
DK
3797 *
3798 */
3799
3800#ifdef CONFIG_QUOTA
3801
3802static inline struct inode *dquot_to_inode(struct dquot *dquot)
3803{
3804 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
3805}
3806
617ba13b 3807static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
3808{
3809 int ret, err;
3810 handle_t *handle;
3811 struct inode *inode;
3812
3813 inode = dquot_to_inode(dquot);
617ba13b 3814 handle = ext4_journal_start(inode,
0b8e58a1 3815 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3816 if (IS_ERR(handle))
3817 return PTR_ERR(handle);
3818 ret = dquot_commit(dquot);
617ba13b 3819 err = ext4_journal_stop(handle);
ac27a0ec
DK
3820 if (!ret)
3821 ret = err;
3822 return ret;
3823}
3824
617ba13b 3825static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
3826{
3827 int ret, err;
3828 handle_t *handle;
3829
617ba13b 3830 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 3831 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
3832 if (IS_ERR(handle))
3833 return PTR_ERR(handle);
3834 ret = dquot_acquire(dquot);
617ba13b 3835 err = ext4_journal_stop(handle);
ac27a0ec
DK
3836 if (!ret)
3837 ret = err;
3838 return ret;
3839}
3840
617ba13b 3841static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
3842{
3843 int ret, err;
3844 handle_t *handle;
3845
617ba13b 3846 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 3847 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
3848 if (IS_ERR(handle)) {
3849 /* Release dquot anyway to avoid endless cycle in dqput() */
3850 dquot_release(dquot);
ac27a0ec 3851 return PTR_ERR(handle);
9c3013e9 3852 }
ac27a0ec 3853 ret = dquot_release(dquot);
617ba13b 3854 err = ext4_journal_stop(handle);
ac27a0ec
DK
3855 if (!ret)
3856 ret = err;
3857 return ret;
3858}
3859
617ba13b 3860static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec 3861{
2c8be6b2 3862 /* Are we journaling quotas? */
617ba13b
MC
3863 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
3864 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
ac27a0ec 3865 dquot_mark_dquot_dirty(dquot);
617ba13b 3866 return ext4_write_dquot(dquot);
ac27a0ec
DK
3867 } else {
3868 return dquot_mark_dquot_dirty(dquot);
3869 }
3870}
3871
617ba13b 3872static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
3873{
3874 int ret, err;
3875 handle_t *handle;
3876
3877 /* Data block + inode block */
617ba13b 3878 handle = ext4_journal_start(sb->s_root->d_inode, 2);
ac27a0ec
DK
3879 if (IS_ERR(handle))
3880 return PTR_ERR(handle);
3881 ret = dquot_commit_info(sb, type);
617ba13b 3882 err = ext4_journal_stop(handle);
ac27a0ec
DK
3883 if (!ret)
3884 ret = err;
3885 return ret;
3886}
3887
3888/*
3889 * Turn on quotas during mount time - we need to find
3890 * the quota file and such...
3891 */
617ba13b 3892static int ext4_quota_on_mount(struct super_block *sb, int type)
ac27a0ec 3893{
617ba13b 3894 return vfs_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
0b8e58a1 3895 EXT4_SB(sb)->s_jquota_fmt, type);
ac27a0ec
DK
3896}
3897
3898/*
3899 * Standard function to be called on quota_on
3900 */
617ba13b 3901static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8264613d 3902 char *name, int remount)
ac27a0ec
DK
3903{
3904 int err;
8264613d 3905 struct path path;
ac27a0ec
DK
3906
3907 if (!test_opt(sb, QUOTA))
3908 return -EINVAL;
8264613d 3909 /* When remounting, no checks are needed and in fact, name is NULL */
0623543b 3910 if (remount)
8264613d 3911 return vfs_quota_on(sb, type, format_id, name, remount);
0623543b 3912
8264613d 3913 err = kern_path(name, LOOKUP_FOLLOW, &path);
ac27a0ec
DK
3914 if (err)
3915 return err;
0623543b 3916
ac27a0ec 3917 /* Quotafile not on the same filesystem? */
8264613d
AV
3918 if (path.mnt->mnt_sb != sb) {
3919 path_put(&path);
ac27a0ec
DK
3920 return -EXDEV;
3921 }
0623543b
JK
3922 /* Journaling quota? */
3923 if (EXT4_SB(sb)->s_qf_names[type]) {
2b2d6d01 3924 /* Quotafile not in fs root? */
8264613d 3925 if (path.dentry->d_parent != sb->s_root)
b31e1552
ES
3926 ext4_msg(sb, KERN_WARNING,
3927 "Quota file not on filesystem root. "
3928 "Journaled quota will not work");
2b2d6d01 3929 }
0623543b
JK
3930
3931 /*
3932 * When we journal data on quota file, we have to flush journal to see
3933 * all updates to the file when we bypass pagecache...
3934 */
0390131b
FM
3935 if (EXT4_SB(sb)->s_journal &&
3936 ext4_should_journal_data(path.dentry->d_inode)) {
0623543b
JK
3937 /*
3938 * We don't need to lock updates but journal_flush() could
3939 * otherwise be livelocked...
3940 */
3941 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3942 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
0623543b 3943 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 3944 if (err) {
8264613d 3945 path_put(&path);
7ffe1ea8
HK
3946 return err;
3947 }
0623543b
JK
3948 }
3949
8264613d
AV
3950 err = vfs_quota_on_path(sb, type, format_id, &path);
3951 path_put(&path);
77e69dac 3952 return err;
ac27a0ec
DK
3953}
3954
3955/* Read data from quotafile - avoid pagecache and such because we cannot afford
3956 * acquiring the locks... As quota files are never truncated and quota code
3957 * itself serializes the operations (and noone else should touch the files)
3958 * we don't have to be afraid of races */
617ba13b 3959static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
3960 size_t len, loff_t off)
3961{
3962 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 3963 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
3964 int err = 0;
3965 int offset = off & (sb->s_blocksize - 1);
3966 int tocopy;
3967 size_t toread;
3968 struct buffer_head *bh;
3969 loff_t i_size = i_size_read(inode);
3970
3971 if (off > i_size)
3972 return 0;
3973 if (off+len > i_size)
3974 len = i_size-off;
3975 toread = len;
3976 while (toread > 0) {
3977 tocopy = sb->s_blocksize - offset < toread ?
3978 sb->s_blocksize - offset : toread;
617ba13b 3979 bh = ext4_bread(NULL, inode, blk, 0, &err);
ac27a0ec
DK
3980 if (err)
3981 return err;
3982 if (!bh) /* A hole? */
3983 memset(data, 0, tocopy);
3984 else
3985 memcpy(data, bh->b_data+offset, tocopy);
3986 brelse(bh);
3987 offset = 0;
3988 toread -= tocopy;
3989 data += tocopy;
3990 blk++;
3991 }
3992 return len;
3993}
3994
3995/* Write to quotafile (we know the transaction is already started and has
3996 * enough credits) */
617ba13b 3997static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
3998 const char *data, size_t len, loff_t off)
3999{
4000 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 4001 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
4002 int err = 0;
4003 int offset = off & (sb->s_blocksize - 1);
617ba13b 4004 int journal_quota = EXT4_SB(sb)->s_qf_names[type] != NULL;
ac27a0ec
DK
4005 struct buffer_head *bh;
4006 handle_t *handle = journal_current_handle();
4007
0390131b 4008 if (EXT4_SB(sb)->s_journal && !handle) {
b31e1552
ES
4009 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4010 " cancelled because transaction is not started",
9c3013e9
JK
4011 (unsigned long long)off, (unsigned long long)len);
4012 return -EIO;
4013 }
67eeb568
DM
4014 /*
4015 * Since we account only one data block in transaction credits,
4016 * then it is impossible to cross a block boundary.
4017 */
4018 if (sb->s_blocksize - offset < len) {
4019 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
4020 " cancelled because not block aligned",
4021 (unsigned long long)off, (unsigned long long)len);
4022 return -EIO;
4023 }
4024
ac27a0ec 4025 mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
67eeb568
DM
4026 bh = ext4_bread(handle, inode, blk, 1, &err);
4027 if (!bh)
4028 goto out;
4029 if (journal_quota) {
4030 err = ext4_journal_get_write_access(handle, bh);
4031 if (err) {
4032 brelse(bh);
ac27a0ec 4033 goto out;
ac27a0ec 4034 }
ac27a0ec 4035 }
67eeb568
DM
4036 lock_buffer(bh);
4037 memcpy(bh->b_data+offset, data, len);
4038 flush_dcache_page(bh->b_page);
4039 unlock_buffer(bh);
4040 if (journal_quota)
4041 err = ext4_handle_dirty_metadata(handle, NULL, bh);
4042 else {
4043 /* Always do at least ordered writes for quotas */
4044 err = ext4_jbd2_file_inode(handle, inode);
4045 mark_buffer_dirty(bh);
4046 }
4047 brelse(bh);
ac27a0ec 4048out:
67eeb568 4049 if (err) {
4d04e4fb 4050 mutex_unlock(&inode->i_mutex);
ac27a0ec 4051 return err;
4d04e4fb 4052 }
67eeb568
DM
4053 if (inode->i_size < off + len) {
4054 i_size_write(inode, off + len);
617ba13b 4055 EXT4_I(inode)->i_disksize = inode->i_size;
ac27a0ec 4056 }
ac27a0ec 4057 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
617ba13b 4058 ext4_mark_inode_dirty(handle, inode);
ac27a0ec 4059 mutex_unlock(&inode->i_mutex);
67eeb568 4060 return len;
ac27a0ec
DK
4061}
4062
4063#endif
4064
0b8e58a1
AD
4065static int ext4_get_sb(struct file_system_type *fs_type, int flags,
4066 const char *dev_name, void *data, struct vfsmount *mnt)
ac27a0ec 4067{
0b8e58a1 4068 return get_sb_bdev(fs_type, flags, dev_name, data, ext4_fill_super,mnt);
ac27a0ec
DK
4069}
4070
37f328eb 4071#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
24b58424
TT
4072static struct file_system_type ext2_fs_type = {
4073 .owner = THIS_MODULE,
4074 .name = "ext2",
4075 .get_sb = ext4_get_sb,
4076 .kill_sb = kill_block_super,
4077 .fs_flags = FS_REQUIRES_DEV,
4078};
4079
4080static inline void register_as_ext2(void)
4081{
4082 int err = register_filesystem(&ext2_fs_type);
4083 if (err)
4084 printk(KERN_WARNING
4085 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
4086}
4087
4088static inline void unregister_as_ext2(void)
4089{
4090 unregister_filesystem(&ext2_fs_type);
4091}
51b7e3c9 4092MODULE_ALIAS("ext2");
24b58424
TT
4093#else
4094static inline void register_as_ext2(void) { }
4095static inline void unregister_as_ext2(void) { }
4096#endif
4097
37f328eb 4098#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
24b58424
TT
4099static struct file_system_type ext3_fs_type = {
4100 .owner = THIS_MODULE,
4101 .name = "ext3",
4102 .get_sb = ext4_get_sb,
4103 .kill_sb = kill_block_super,
4104 .fs_flags = FS_REQUIRES_DEV,
4105};
4106
4107static inline void register_as_ext3(void)
4108{
4109 int err = register_filesystem(&ext3_fs_type);
4110 if (err)
4111 printk(KERN_WARNING
4112 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
4113}
4114
4115static inline void unregister_as_ext3(void)
4116{
4117 unregister_filesystem(&ext3_fs_type);
4118}
51b7e3c9 4119MODULE_ALIAS("ext3");
24b58424
TT
4120#else
4121static inline void register_as_ext3(void) { }
4122static inline void unregister_as_ext3(void) { }
4123#endif
4124
03010a33
TT
4125static struct file_system_type ext4_fs_type = {
4126 .owner = THIS_MODULE,
4127 .name = "ext4",
4128 .get_sb = ext4_get_sb,
4129 .kill_sb = kill_block_super,
4130 .fs_flags = FS_REQUIRES_DEV,
4131};
4132
617ba13b 4133static int __init init_ext4_fs(void)
ac27a0ec 4134{
c9de560d
AT
4135 int err;
4136
6fd058f7
TT
4137 err = init_ext4_system_zone();
4138 if (err)
4139 return err;
3197ebdb
TT
4140 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4141 if (!ext4_kset)
6fd058f7 4142 goto out4;
9f6200bb 4143 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
c9de560d 4144 err = init_ext4_mballoc();
ac27a0ec 4145 if (err)
6fd058f7 4146 goto out3;
c9de560d
AT
4147
4148 err = init_ext4_xattr();
4149 if (err)
4150 goto out2;
ac27a0ec
DK
4151 err = init_inodecache();
4152 if (err)
4153 goto out1;
24b58424
TT
4154 register_as_ext2();
4155 register_as_ext3();
03010a33 4156 err = register_filesystem(&ext4_fs_type);
ac27a0ec
DK
4157 if (err)
4158 goto out;
4159 return 0;
4160out:
24b58424
TT
4161 unregister_as_ext2();
4162 unregister_as_ext3();
ac27a0ec
DK
4163 destroy_inodecache();
4164out1:
617ba13b 4165 exit_ext4_xattr();
c9de560d
AT
4166out2:
4167 exit_ext4_mballoc();
6fd058f7
TT
4168out3:
4169 remove_proc_entry("fs/ext4", NULL);
4170 kset_unregister(ext4_kset);
4171out4:
4172 exit_ext4_system_zone();
ac27a0ec
DK
4173 return err;
4174}
4175
617ba13b 4176static void __exit exit_ext4_fs(void)
ac27a0ec 4177{
24b58424
TT
4178 unregister_as_ext2();
4179 unregister_as_ext3();
03010a33 4180 unregister_filesystem(&ext4_fs_type);
ac27a0ec 4181 destroy_inodecache();
617ba13b 4182 exit_ext4_xattr();
c9de560d 4183 exit_ext4_mballoc();
9f6200bb 4184 remove_proc_entry("fs/ext4", NULL);
3197ebdb 4185 kset_unregister(ext4_kset);
6fd058f7 4186 exit_ext4_system_zone();
ac27a0ec
DK
4187}
4188
4189MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
83982b6f 4190MODULE_DESCRIPTION("Fourth Extended Filesystem");
ac27a0ec 4191MODULE_LICENSE("GPL");
617ba13b
MC
4192module_init(init_ext4_fs)
4193module_exit(exit_ext4_fs)