ext4: fixed potential NULL dereference in ext4_calculate_overhead()
[GitHub/LineageOS/android_kernel_motorola_exynos9610.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>
ac27a0ec 29#include <linux/buffer_head.h>
a5694255 30#include <linux/exportfs.h>
ac27a0ec
DK
31#include <linux/vfs.h>
32#include <linux/random.h>
33#include <linux/mount.h>
34#include <linux/namei.h>
35#include <linux/quotaops.h>
36#include <linux/seq_file.h>
9f6200bb 37#include <linux/proc_fs.h>
3197ebdb 38#include <linux/ctype.h>
1330593e 39#include <linux/log2.h>
717d50e4 40#include <linux/crc16.h>
7abc52c2 41#include <linux/cleancache.h>
ac27a0ec
DK
42#include <asm/uaccess.h>
43
bfff6873
LC
44#include <linux/kthread.h>
45#include <linux/freezer.h>
46
3dcf5451 47#include "ext4.h"
6f91bc5f 48#include "ext4_extents.h"
3dcf5451 49#include "ext4_jbd2.h"
ac27a0ec
DK
50#include "xattr.h"
51#include "acl.h"
3661d286 52#include "mballoc.h"
51865fda 53#include "ext4_extents.h"
ac27a0ec 54
9bffad1e
TT
55#define CREATE_TRACE_POINTS
56#include <trace/events/ext4.h>
57
1f109d5a 58static struct proc_dir_entry *ext4_proc_root;
3197ebdb 59static struct kset *ext4_kset;
0b75a840
LC
60static struct ext4_lazy_init *ext4_li_info;
61static struct mutex ext4_li_mtx;
62static struct ext4_features *ext4_feat;
9f6200bb 63
617ba13b 64static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 65 unsigned long journal_devnum);
2adf6da8 66static int ext4_show_options(struct seq_file *seq, struct dentry *root);
e2d67052 67static int ext4_commit_super(struct super_block *sb, int sync);
2b2d6d01
TT
68static void ext4_mark_recovery_complete(struct super_block *sb,
69 struct ext4_super_block *es);
70static void ext4_clear_journal_err(struct super_block *sb,
71 struct ext4_super_block *es);
617ba13b 72static int ext4_sync_fs(struct super_block *sb, int wait);
2b2d6d01 73static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec 74 char nbuf[16]);
2b2d6d01
TT
75static int ext4_remount(struct super_block *sb, int *flags, char *data);
76static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
c4be0c1d 77static int ext4_unfreeze(struct super_block *sb);
c4be0c1d 78static int ext4_freeze(struct super_block *sb);
152a0836
AV
79static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
80 const char *dev_name, void *data);
2035e776
TT
81static inline int ext2_feature_set_ok(struct super_block *sb);
82static inline int ext3_feature_set_ok(struct super_block *sb);
d39195c3 83static int ext4_feature_set_ok(struct super_block *sb, int readonly);
bfff6873
LC
84static void ext4_destroy_lazyinit_thread(void);
85static void ext4_unregister_li_request(struct super_block *sb);
8f1f7453 86static void ext4_clear_request_list(void);
ac27a0ec 87
2035e776
TT
88#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
89static struct file_system_type ext2_fs_type = {
90 .owner = THIS_MODULE,
91 .name = "ext2",
92 .mount = ext4_mount,
93 .kill_sb = kill_block_super,
94 .fs_flags = FS_REQUIRES_DEV,
95};
96#define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
97#else
98#define IS_EXT2_SB(sb) (0)
99#endif
100
101
ba69f9ab
JK
102#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
103static struct file_system_type ext3_fs_type = {
104 .owner = THIS_MODULE,
105 .name = "ext3",
152a0836 106 .mount = ext4_mount,
ba69f9ab
JK
107 .kill_sb = kill_block_super,
108 .fs_flags = FS_REQUIRES_DEV,
109};
110#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
111#else
112#define IS_EXT3_SB(sb) (0)
113#endif
bd81d8ee 114
d25425f8
DW
115static int ext4_verify_csum_type(struct super_block *sb,
116 struct ext4_super_block *es)
117{
118 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
119 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
120 return 1;
121
122 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
123}
124
a9c47317
DW
125static __le32 ext4_superblock_csum(struct super_block *sb,
126 struct ext4_super_block *es)
127{
128 struct ext4_sb_info *sbi = EXT4_SB(sb);
129 int offset = offsetof(struct ext4_super_block, s_checksum);
130 __u32 csum;
131
132 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
133
134 return cpu_to_le32(csum);
135}
136
137int ext4_superblock_csum_verify(struct super_block *sb,
138 struct ext4_super_block *es)
139{
140 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
141 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
142 return 1;
143
144 return es->s_checksum == ext4_superblock_csum(sb, es);
145}
146
06db49e6 147void ext4_superblock_csum_set(struct super_block *sb)
a9c47317 148{
06db49e6
TT
149 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
150
a9c47317
DW
151 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
152 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
153 return;
154
155 es->s_checksum = ext4_superblock_csum(sb, es);
156}
157
9933fc0a
TT
158void *ext4_kvmalloc(size_t size, gfp_t flags)
159{
160 void *ret;
161
162 ret = kmalloc(size, flags);
163 if (!ret)
164 ret = __vmalloc(size, flags, PAGE_KERNEL);
165 return ret;
166}
167
168void *ext4_kvzalloc(size_t size, gfp_t flags)
169{
170 void *ret;
171
db9481c0 172 ret = kzalloc(size, flags);
9933fc0a
TT
173 if (!ret)
174 ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
175 return ret;
176}
177
178void ext4_kvfree(void *ptr)
179{
180 if (is_vmalloc_addr(ptr))
181 vfree(ptr);
182 else
183 kfree(ptr);
184
185}
186
8fadc143
AR
187ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
188 struct ext4_group_desc *bg)
bd81d8ee 189{
3a14589c 190 return le32_to_cpu(bg->bg_block_bitmap_lo) |
8fadc143 191 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 192 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
bd81d8ee
LV
193}
194
8fadc143
AR
195ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
196 struct ext4_group_desc *bg)
bd81d8ee 197{
5272f837 198 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
8fadc143 199 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 200 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
bd81d8ee
LV
201}
202
8fadc143
AR
203ext4_fsblk_t ext4_inode_table(struct super_block *sb,
204 struct ext4_group_desc *bg)
bd81d8ee 205{
5272f837 206 return le32_to_cpu(bg->bg_inode_table_lo) |
8fadc143 207 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 208 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
bd81d8ee
LV
209}
210
021b65bb
TT
211__u32 ext4_free_group_clusters(struct super_block *sb,
212 struct ext4_group_desc *bg)
560671a0
AK
213{
214 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
215 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 216 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
560671a0
AK
217}
218
219__u32 ext4_free_inodes_count(struct super_block *sb,
220 struct ext4_group_desc *bg)
221{
222 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
223 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 224 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
560671a0
AK
225}
226
227__u32 ext4_used_dirs_count(struct super_block *sb,
228 struct ext4_group_desc *bg)
229{
230 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
231 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 232 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
560671a0
AK
233}
234
235__u32 ext4_itable_unused_count(struct super_block *sb,
236 struct ext4_group_desc *bg)
237{
238 return le16_to_cpu(bg->bg_itable_unused_lo) |
239 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 240 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
560671a0
AK
241}
242
8fadc143
AR
243void ext4_block_bitmap_set(struct super_block *sb,
244 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 245{
3a14589c 246 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
247 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
248 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
249}
250
8fadc143
AR
251void ext4_inode_bitmap_set(struct super_block *sb,
252 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 253{
5272f837 254 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
255 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
256 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
257}
258
8fadc143
AR
259void ext4_inode_table_set(struct super_block *sb,
260 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 261{
5272f837 262 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
8fadc143
AR
263 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
264 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
265}
266
021b65bb
TT
267void ext4_free_group_clusters_set(struct super_block *sb,
268 struct ext4_group_desc *bg, __u32 count)
560671a0
AK
269{
270 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
271 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
272 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
273}
274
275void ext4_free_inodes_set(struct super_block *sb,
276 struct ext4_group_desc *bg, __u32 count)
277{
278 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
279 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
280 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
281}
282
283void ext4_used_dirs_set(struct super_block *sb,
284 struct ext4_group_desc *bg, __u32 count)
285{
286 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
287 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
288 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
289}
290
291void ext4_itable_unused_set(struct super_block *sb,
292 struct ext4_group_desc *bg, __u32 count)
293{
294 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
295 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
296 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
297}
298
d3d1faf6
CW
299
300/* Just increment the non-pointer handle value */
301static handle_t *ext4_get_nojournal(void)
302{
303 handle_t *handle = current->journal_info;
304 unsigned long ref_cnt = (unsigned long)handle;
305
306 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
307
308 ref_cnt++;
309 handle = (handle_t *)ref_cnt;
310
311 current->journal_info = handle;
312 return handle;
313}
314
315
316/* Decrement the non-pointer handle value */
317static void ext4_put_nojournal(handle_t *handle)
318{
319 unsigned long ref_cnt = (unsigned long)handle;
320
321 BUG_ON(ref_cnt == 0);
322
323 ref_cnt--;
324 handle = (handle_t *)ref_cnt;
325
326 current->journal_info = handle;
327}
328
ac27a0ec 329/*
dab291af 330 * Wrappers for jbd2_journal_start/end.
ac27a0ec 331 */
617ba13b 332handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
ac27a0ec
DK
333{
334 journal_t *journal;
335
12706394 336 trace_ext4_journal_start(sb, nblocks, _RET_IP_);
ac27a0ec
DK
337 if (sb->s_flags & MS_RDONLY)
338 return ERR_PTR(-EROFS);
339
8e8ad8a5 340 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
617ba13b 341 journal = EXT4_SB(sb)->s_journal;
be4f27d3
YY
342 if (!journal)
343 return ext4_get_nojournal();
344 /*
345 * Special case here: if the journal has aborted behind our
346 * backs (eg. EIO in the commit thread), then we still need to
347 * take the FS itself readonly cleanly.
348 */
349 if (is_journal_aborted(journal)) {
350 ext4_abort(sb, "Detected aborted journal");
351 return ERR_PTR(-EROFS);
ac27a0ec 352 }
be4f27d3 353 return jbd2_journal_start(journal, nblocks);
ac27a0ec
DK
354}
355
c398eda0 356int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
ac27a0ec
DK
357{
358 struct super_block *sb;
359 int err;
360 int rc;
361
0390131b 362 if (!ext4_handle_valid(handle)) {
d3d1faf6 363 ext4_put_nojournal(handle);
0390131b
FM
364 return 0;
365 }
ac27a0ec
DK
366 sb = handle->h_transaction->t_journal->j_private;
367 err = handle->h_err;
dab291af 368 rc = jbd2_journal_stop(handle);
ac27a0ec
DK
369
370 if (!err)
371 err = rc;
372 if (err)
c398eda0 373 __ext4_std_error(sb, where, line, err);
ac27a0ec
DK
374 return err;
375}
376
90c7201b
TT
377void ext4_journal_abort_handle(const char *caller, unsigned int line,
378 const char *err_fn, struct buffer_head *bh,
379 handle_t *handle, int err)
ac27a0ec
DK
380{
381 char nbuf[16];
617ba13b 382 const char *errstr = ext4_decode_error(NULL, err, nbuf);
ac27a0ec 383
0390131b
FM
384 BUG_ON(!ext4_handle_valid(handle));
385
ac27a0ec
DK
386 if (bh)
387 BUFFER_TRACE(bh, "abort");
388
389 if (!handle->h_err)
390 handle->h_err = err;
391
392 if (is_handle_aborted(handle))
393 return;
394
92b97816 395 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
90c7201b 396 caller, line, errstr, err_fn);
ac27a0ec 397
dab291af 398 jbd2_journal_abort_handle(handle);
ac27a0ec
DK
399}
400
1c13d5c0
TT
401static void __save_error_info(struct super_block *sb, const char *func,
402 unsigned int line)
403{
404 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
405
406 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
407 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
408 es->s_last_error_time = cpu_to_le32(get_seconds());
409 strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
410 es->s_last_error_line = cpu_to_le32(line);
411 if (!es->s_first_error_time) {
412 es->s_first_error_time = es->s_last_error_time;
413 strncpy(es->s_first_error_func, func,
414 sizeof(es->s_first_error_func));
415 es->s_first_error_line = cpu_to_le32(line);
416 es->s_first_error_ino = es->s_last_error_ino;
417 es->s_first_error_block = es->s_last_error_block;
418 }
66e61a9e
TT
419 /*
420 * Start the daily error reporting function if it hasn't been
421 * started already
422 */
423 if (!es->s_error_count)
424 mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
ba39ebb6 425 le32_add_cpu(&es->s_error_count, 1);
1c13d5c0
TT
426}
427
428static void save_error_info(struct super_block *sb, const char *func,
429 unsigned int line)
430{
431 __save_error_info(sb, func, line);
432 ext4_commit_super(sb, 1);
433}
434
7c2e7087
TT
435/*
436 * The del_gendisk() function uninitializes the disk-specific data
437 * structures, including the bdi structure, without telling anyone
438 * else. Once this happens, any attempt to call mark_buffer_dirty()
439 * (for example, by ext4_commit_super), will cause a kernel OOPS.
440 * This is a kludge to prevent these oops until we can put in a proper
441 * hook in del_gendisk() to inform the VFS and file system layers.
442 */
443static int block_device_ejected(struct super_block *sb)
444{
445 struct inode *bd_inode = sb->s_bdev->bd_inode;
446 struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
447
448 return bdi->dev == NULL;
449}
450
18aadd47
BJ
451static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
452{
453 struct super_block *sb = journal->j_private;
454 struct ext4_sb_info *sbi = EXT4_SB(sb);
455 int error = is_journal_aborted(journal);
456 struct ext4_journal_cb_entry *jce, *tmp;
457
458 spin_lock(&sbi->s_md_lock);
459 list_for_each_entry_safe(jce, tmp, &txn->t_private_list, jce_list) {
460 list_del_init(&jce->jce_list);
461 spin_unlock(&sbi->s_md_lock);
462 jce->jce_func(sb, jce, error);
463 spin_lock(&sbi->s_md_lock);
464 }
465 spin_unlock(&sbi->s_md_lock);
466}
1c13d5c0 467
ac27a0ec
DK
468/* Deal with the reporting of failure conditions on a filesystem such as
469 * inconsistencies detected or read IO failures.
470 *
471 * On ext2, we can store the error state of the filesystem in the
617ba13b 472 * superblock. That is not possible on ext4, because we may have other
ac27a0ec
DK
473 * write ordering constraints on the superblock which prevent us from
474 * writing it out straight away; and given that the journal is about to
475 * be aborted, we can't rely on the current, or future, transactions to
476 * write out the superblock safely.
477 *
dab291af 478 * We'll just use the jbd2_journal_abort() error code to record an error in
d6b198bc 479 * the journal instead. On recovery, the journal will complain about
ac27a0ec
DK
480 * that error until we've noted it down and cleared it.
481 */
482
617ba13b 483static void ext4_handle_error(struct super_block *sb)
ac27a0ec 484{
ac27a0ec
DK
485 if (sb->s_flags & MS_RDONLY)
486 return;
487
2b2d6d01 488 if (!test_opt(sb, ERRORS_CONT)) {
617ba13b 489 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 490
4ab2f15b 491 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
ac27a0ec 492 if (journal)
dab291af 493 jbd2_journal_abort(journal, -EIO);
ac27a0ec 494 }
2b2d6d01 495 if (test_opt(sb, ERRORS_RO)) {
b31e1552 496 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
ac27a0ec
DK
497 sb->s_flags |= MS_RDONLY;
498 }
ac27a0ec 499 if (test_opt(sb, ERRORS_PANIC))
617ba13b 500 panic("EXT4-fs (device %s): panic forced after error\n",
ac27a0ec
DK
501 sb->s_id);
502}
503
12062ddd 504void __ext4_error(struct super_block *sb, const char *function,
c398eda0 505 unsigned int line, const char *fmt, ...)
ac27a0ec 506{
0ff2ea7d 507 struct va_format vaf;
ac27a0ec
DK
508 va_list args;
509
510 va_start(args, fmt);
0ff2ea7d
JP
511 vaf.fmt = fmt;
512 vaf.va = &args;
513 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
514 sb->s_id, function, line, current->comm, &vaf);
ac27a0ec 515 va_end(args);
f3fc0210 516 save_error_info(sb, function, line);
ac27a0ec 517
617ba13b 518 ext4_handle_error(sb);
ac27a0ec
DK
519}
520
c398eda0
TT
521void ext4_error_inode(struct inode *inode, const char *function,
522 unsigned int line, ext4_fsblk_t block,
273df556
FM
523 const char *fmt, ...)
524{
525 va_list args;
f7c21177 526 struct va_format vaf;
1c13d5c0 527 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
273df556 528
1c13d5c0
TT
529 es->s_last_error_ino = cpu_to_le32(inode->i_ino);
530 es->s_last_error_block = cpu_to_le64(block);
531 save_error_info(inode->i_sb, function, line);
273df556 532 va_start(args, fmt);
f7c21177
TT
533 vaf.fmt = fmt;
534 vaf.va = &args;
c398eda0 535 if (block)
d9ee81da
JP
536 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
537 "inode #%lu: block %llu: comm %s: %pV\n",
538 inode->i_sb->s_id, function, line, inode->i_ino,
539 block, current->comm, &vaf);
540 else
541 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
542 "inode #%lu: comm %s: %pV\n",
543 inode->i_sb->s_id, function, line, inode->i_ino,
544 current->comm, &vaf);
273df556
FM
545 va_end(args);
546
547 ext4_handle_error(inode->i_sb);
548}
549
c398eda0 550void ext4_error_file(struct file *file, const char *function,
f7c21177
TT
551 unsigned int line, ext4_fsblk_t block,
552 const char *fmt, ...)
273df556
FM
553{
554 va_list args;
f7c21177 555 struct va_format vaf;
1c13d5c0 556 struct ext4_super_block *es;
273df556
FM
557 struct inode *inode = file->f_dentry->d_inode;
558 char pathname[80], *path;
559
1c13d5c0
TT
560 es = EXT4_SB(inode->i_sb)->s_es;
561 es->s_last_error_ino = cpu_to_le32(inode->i_ino);
562 save_error_info(inode->i_sb, function, line);
273df556 563 path = d_path(&(file->f_path), pathname, sizeof(pathname));
f9a62d09 564 if (IS_ERR(path))
273df556 565 path = "(unknown)";
f7c21177
TT
566 va_start(args, fmt);
567 vaf.fmt = fmt;
568 vaf.va = &args;
d9ee81da
JP
569 if (block)
570 printk(KERN_CRIT
571 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
572 "block %llu: comm %s: path %s: %pV\n",
573 inode->i_sb->s_id, function, line, inode->i_ino,
574 block, current->comm, path, &vaf);
575 else
576 printk(KERN_CRIT
577 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
578 "comm %s: path %s: %pV\n",
579 inode->i_sb->s_id, function, line, inode->i_ino,
580 current->comm, path, &vaf);
273df556
FM
581 va_end(args);
582
583 ext4_handle_error(inode->i_sb);
584}
585
2b2d6d01 586static const char *ext4_decode_error(struct super_block *sb, int errno,
ac27a0ec
DK
587 char nbuf[16])
588{
589 char *errstr = NULL;
590
591 switch (errno) {
592 case -EIO:
593 errstr = "IO failure";
594 break;
595 case -ENOMEM:
596 errstr = "Out of memory";
597 break;
598 case -EROFS:
78f1ddbb
TT
599 if (!sb || (EXT4_SB(sb)->s_journal &&
600 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
ac27a0ec
DK
601 errstr = "Journal has aborted";
602 else
603 errstr = "Readonly filesystem";
604 break;
605 default:
606 /* If the caller passed in an extra buffer for unknown
607 * errors, textualise them now. Else we just return
608 * NULL. */
609 if (nbuf) {
610 /* Check for truncated error codes... */
611 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
612 errstr = nbuf;
613 }
614 break;
615 }
616
617 return errstr;
618}
619
617ba13b 620/* __ext4_std_error decodes expected errors from journaling functions
ac27a0ec
DK
621 * automatically and invokes the appropriate error response. */
622
c398eda0
TT
623void __ext4_std_error(struct super_block *sb, const char *function,
624 unsigned int line, int errno)
ac27a0ec
DK
625{
626 char nbuf[16];
627 const char *errstr;
628
629 /* Special case: if the error is EROFS, and we're not already
630 * inside a transaction, then there's really no point in logging
631 * an error. */
632 if (errno == -EROFS && journal_current_handle() == NULL &&
633 (sb->s_flags & MS_RDONLY))
634 return;
635
617ba13b 636 errstr = ext4_decode_error(sb, errno, nbuf);
c398eda0
TT
637 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
638 sb->s_id, function, line, errstr);
1c13d5c0 639 save_error_info(sb, function, line);
ac27a0ec 640
617ba13b 641 ext4_handle_error(sb);
ac27a0ec
DK
642}
643
644/*
617ba13b 645 * ext4_abort is a much stronger failure handler than ext4_error. The
ac27a0ec
DK
646 * abort function may be used to deal with unrecoverable failures such
647 * as journal IO errors or ENOMEM at a critical moment in log management.
648 *
649 * We unconditionally force the filesystem into an ABORT|READONLY state,
650 * unless the error response on the fs has been set to panic in which
651 * case we take the easy way out and panic immediately.
652 */
653
c67d859e 654void __ext4_abort(struct super_block *sb, const char *function,
c398eda0 655 unsigned int line, const char *fmt, ...)
ac27a0ec
DK
656{
657 va_list args;
658
1c13d5c0 659 save_error_info(sb, function, line);
ac27a0ec 660 va_start(args, fmt);
c398eda0
TT
661 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
662 function, line);
ac27a0ec
DK
663 vprintk(fmt, args);
664 printk("\n");
665 va_end(args);
666
1c13d5c0
TT
667 if ((sb->s_flags & MS_RDONLY) == 0) {
668 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
669 sb->s_flags |= MS_RDONLY;
670 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
671 if (EXT4_SB(sb)->s_journal)
672 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
673 save_error_info(sb, function, line);
674 }
ac27a0ec 675 if (test_opt(sb, ERRORS_PANIC))
617ba13b 676 panic("EXT4-fs panic from previous error\n");
ac27a0ec
DK
677}
678
0ff2ea7d 679void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
b31e1552 680{
0ff2ea7d 681 struct va_format vaf;
b31e1552
ES
682 va_list args;
683
684 va_start(args, fmt);
0ff2ea7d
JP
685 vaf.fmt = fmt;
686 vaf.va = &args;
687 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
b31e1552
ES
688 va_end(args);
689}
690
12062ddd 691void __ext4_warning(struct super_block *sb, const char *function,
c398eda0 692 unsigned int line, const char *fmt, ...)
ac27a0ec 693{
0ff2ea7d 694 struct va_format vaf;
ac27a0ec
DK
695 va_list args;
696
697 va_start(args, fmt);
0ff2ea7d
JP
698 vaf.fmt = fmt;
699 vaf.va = &args;
700 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
701 sb->s_id, function, line, &vaf);
ac27a0ec
DK
702 va_end(args);
703}
704
e29136f8
TT
705void __ext4_grp_locked_error(const char *function, unsigned int line,
706 struct super_block *sb, ext4_group_t grp,
707 unsigned long ino, ext4_fsblk_t block,
708 const char *fmt, ...)
5d1b1b3f
AK
709__releases(bitlock)
710__acquires(bitlock)
711{
0ff2ea7d 712 struct va_format vaf;
5d1b1b3f
AK
713 va_list args;
714 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
715
1c13d5c0
TT
716 es->s_last_error_ino = cpu_to_le32(ino);
717 es->s_last_error_block = cpu_to_le64(block);
718 __save_error_info(sb, function, line);
0ff2ea7d 719
5d1b1b3f 720 va_start(args, fmt);
0ff2ea7d
JP
721
722 vaf.fmt = fmt;
723 vaf.va = &args;
21149d61 724 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
e29136f8
TT
725 sb->s_id, function, line, grp);
726 if (ino)
0ff2ea7d 727 printk(KERN_CONT "inode %lu: ", ino);
e29136f8 728 if (block)
0ff2ea7d
JP
729 printk(KERN_CONT "block %llu:", (unsigned long long) block);
730 printk(KERN_CONT "%pV\n", &vaf);
5d1b1b3f
AK
731 va_end(args);
732
733 if (test_opt(sb, ERRORS_CONT)) {
e2d67052 734 ext4_commit_super(sb, 0);
5d1b1b3f
AK
735 return;
736 }
1c13d5c0 737
5d1b1b3f
AK
738 ext4_unlock_group(sb, grp);
739 ext4_handle_error(sb);
740 /*
741 * We only get here in the ERRORS_RO case; relocking the group
742 * may be dangerous, but nothing bad will happen since the
743 * filesystem will have already been marked read/only and the
744 * journal has been aborted. We return 1 as a hint to callers
745 * who might what to use the return value from
25985edc 746 * ext4_grp_locked_error() to distinguish between the
5d1b1b3f
AK
747 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
748 * aggressively from the ext4 function in question, with a
749 * more appropriate error code.
750 */
751 ext4_lock_group(sb, grp);
752 return;
753}
754
617ba13b 755void ext4_update_dynamic_rev(struct super_block *sb)
ac27a0ec 756{
617ba13b 757 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 758
617ba13b 759 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
ac27a0ec
DK
760 return;
761
12062ddd 762 ext4_warning(sb,
ac27a0ec
DK
763 "updating to rev %d because of new feature flag, "
764 "running e2fsck is recommended",
617ba13b 765 EXT4_DYNAMIC_REV);
ac27a0ec 766
617ba13b
MC
767 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
768 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
769 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
ac27a0ec
DK
770 /* leave es->s_feature_*compat flags alone */
771 /* es->s_uuid will be set by e2fsck if empty */
772
773 /*
774 * The rest of the superblock fields should be zero, and if not it
775 * means they are likely already in use, so leave them alone. We
776 * can leave it up to e2fsck to clean up any inconsistencies there.
777 */
778}
779
780/*
781 * Open the external journal device
782 */
b31e1552 783static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
ac27a0ec
DK
784{
785 struct block_device *bdev;
786 char b[BDEVNAME_SIZE];
787
d4d77629 788 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
ac27a0ec
DK
789 if (IS_ERR(bdev))
790 goto fail;
791 return bdev;
792
793fail:
b31e1552 794 ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
ac27a0ec
DK
795 __bdevname(dev, b), PTR_ERR(bdev));
796 return NULL;
797}
798
799/*
800 * Release the journal device
801 */
617ba13b 802static int ext4_blkdev_put(struct block_device *bdev)
ac27a0ec 803{
e525fd89 804 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
ac27a0ec
DK
805}
806
617ba13b 807static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
ac27a0ec
DK
808{
809 struct block_device *bdev;
810 int ret = -ENODEV;
811
812 bdev = sbi->journal_bdev;
813 if (bdev) {
617ba13b 814 ret = ext4_blkdev_put(bdev);
ac27a0ec
DK
815 sbi->journal_bdev = NULL;
816 }
817 return ret;
818}
819
820static inline struct inode *orphan_list_entry(struct list_head *l)
821{
617ba13b 822 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec
DK
823}
824
617ba13b 825static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
ac27a0ec
DK
826{
827 struct list_head *l;
828
b31e1552
ES
829 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
830 le32_to_cpu(sbi->s_es->s_last_orphan));
ac27a0ec
DK
831
832 printk(KERN_ERR "sb_info orphan list:\n");
833 list_for_each(l, &sbi->s_orphan) {
834 struct inode *inode = orphan_list_entry(l);
835 printk(KERN_ERR " "
836 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
837 inode->i_sb->s_id, inode->i_ino, inode,
838 inode->i_mode, inode->i_nlink,
839 NEXT_ORPHAN(inode));
840 }
841}
842
2b2d6d01 843static void ext4_put_super(struct super_block *sb)
ac27a0ec 844{
617ba13b
MC
845 struct ext4_sb_info *sbi = EXT4_SB(sb);
846 struct ext4_super_block *es = sbi->s_es;
ef2cabf7 847 int i, err;
ac27a0ec 848
857ac889 849 ext4_unregister_li_request(sb);
e0ccfd95
CH
850 dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
851
4c0425ff
MC
852 flush_workqueue(sbi->dio_unwritten_wq);
853 destroy_workqueue(sbi->dio_unwritten_wq);
854
0390131b
FM
855 if (sbi->s_journal) {
856 err = jbd2_journal_destroy(sbi->s_journal);
857 sbi->s_journal = NULL;
858 if (err < 0)
c67d859e 859 ext4_abort(sb, "Couldn't clean up the journal");
0390131b 860 }
d4edac31 861
a1c6c569 862 del_timer(&sbi->s_err_report);
d4edac31
JB
863 ext4_release_system_zone(sb);
864 ext4_mb_release(sb);
865 ext4_ext_release(sb);
866 ext4_xattr_put_super(sb);
867
ac27a0ec 868 if (!(sb->s_flags & MS_RDONLY)) {
617ba13b 869 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 870 es->s_state = cpu_to_le16(sbi->s_mount_state);
ac27a0ec 871 }
58c5873a 872 if (!(sb->s_flags & MS_RDONLY))
a8e25a83
AB
873 ext4_commit_super(sb, 1);
874
240799cd 875 if (sbi->s_proc) {
66acdcf4 876 remove_proc_entry("options", sbi->s_proc);
9f6200bb 877 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 878 }
3197ebdb 879 kobject_del(&sbi->s_kobj);
ac27a0ec
DK
880
881 for (i = 0; i < sbi->s_gdb_count; i++)
882 brelse(sbi->s_group_desc[i]);
f18a5f21 883 ext4_kvfree(sbi->s_group_desc);
9933fc0a 884 ext4_kvfree(sbi->s_flex_groups);
57042651 885 percpu_counter_destroy(&sbi->s_freeclusters_counter);
ac27a0ec
DK
886 percpu_counter_destroy(&sbi->s_freeinodes_counter);
887 percpu_counter_destroy(&sbi->s_dirs_counter);
57042651 888 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
ac27a0ec
DK
889 brelse(sbi->s_sbh);
890#ifdef CONFIG_QUOTA
891 for (i = 0; i < MAXQUOTAS; i++)
892 kfree(sbi->s_qf_names[i]);
893#endif
894
895 /* Debugging code just in case the in-memory inode orphan list
896 * isn't empty. The on-disk one can be non-empty if we've
897 * detected an error and taken the fs readonly, but the
898 * in-memory list had better be clean by this point. */
899 if (!list_empty(&sbi->s_orphan))
900 dump_orphan_list(sb, sbi);
901 J_ASSERT(list_empty(&sbi->s_orphan));
902
f98393a6 903 invalidate_bdev(sb->s_bdev);
ac27a0ec
DK
904 if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
905 /*
906 * Invalidate the journal device's buffers. We don't want them
907 * floating about in memory - the physical journal device may
908 * hotswapped, and it breaks the `ro-after' testing code.
909 */
910 sync_blockdev(sbi->journal_bdev);
f98393a6 911 invalidate_bdev(sbi->journal_bdev);
617ba13b 912 ext4_blkdev_remove(sbi);
ac27a0ec 913 }
c5e06d10
JL
914 if (sbi->s_mmp_tsk)
915 kthread_stop(sbi->s_mmp_tsk);
ac27a0ec 916 sb->s_fs_info = NULL;
3197ebdb
TT
917 /*
918 * Now that we are completely done shutting down the
919 * superblock, we need to actually destroy the kobject.
920 */
3197ebdb
TT
921 kobject_put(&sbi->s_kobj);
922 wait_for_completion(&sbi->s_kobj_unregister);
0441984a
DW
923 if (sbi->s_chksum_driver)
924 crypto_free_shash(sbi->s_chksum_driver);
705895b6 925 kfree(sbi->s_blockgroup_lock);
ac27a0ec 926 kfree(sbi);
ac27a0ec
DK
927}
928
e18b890b 929static struct kmem_cache *ext4_inode_cachep;
ac27a0ec
DK
930
931/*
932 * Called inside transaction, so use GFP_NOFS
933 */
617ba13b 934static struct inode *ext4_alloc_inode(struct super_block *sb)
ac27a0ec 935{
617ba13b 936 struct ext4_inode_info *ei;
ac27a0ec 937
e6b4f8da 938 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
ac27a0ec
DK
939 if (!ei)
940 return NULL;
0b8e58a1 941
ac27a0ec 942 ei->vfs_inode.i_version = 1;
91246c00 943 ei->vfs_inode.i_data.writeback_index = 0;
a86c6181 944 memset(&ei->i_cached_extent, 0, sizeof(struct ext4_ext_cache));
c9de560d
AT
945 INIT_LIST_HEAD(&ei->i_prealloc_list);
946 spin_lock_init(&ei->i_prealloc_lock);
9a26b661
ZL
947 ext4_es_init_tree(&ei->i_es_tree);
948 rwlock_init(&ei->i_es_lock);
d2a17637
MC
949 ei->i_reserved_data_blocks = 0;
950 ei->i_reserved_meta_blocks = 0;
951 ei->i_allocated_meta_blocks = 0;
9d0be502 952 ei->i_da_metadata_calc_len = 0;
7e731bc9 953 ei->i_da_metadata_calc_last_lblock = 0;
d2a17637 954 spin_lock_init(&(ei->i_block_reservation_lock));
a9e7f447
DM
955#ifdef CONFIG_QUOTA
956 ei->i_reserved_quota = 0;
957#endif
8aefcd55 958 ei->jinode = NULL;
c7064ef1 959 INIT_LIST_HEAD(&ei->i_completed_io_list);
744692dc 960 spin_lock_init(&ei->i_completed_io_lock);
b436b9be
JK
961 ei->i_sync_tid = 0;
962 ei->i_datasync_tid = 0;
f7ad6d2e 963 atomic_set(&ei->i_ioend_count, 0);
e27f41e1 964 atomic_set(&ei->i_unwritten, 0);
0b8e58a1 965
ac27a0ec
DK
966 return &ei->vfs_inode;
967}
968
7ff9c073
TT
969static int ext4_drop_inode(struct inode *inode)
970{
971 int drop = generic_drop_inode(inode);
972
973 trace_ext4_drop_inode(inode, drop);
974 return drop;
975}
976
fa0d7e3d
NP
977static void ext4_i_callback(struct rcu_head *head)
978{
979 struct inode *inode = container_of(head, struct inode, i_rcu);
fa0d7e3d
NP
980 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
981}
982
617ba13b 983static void ext4_destroy_inode(struct inode *inode)
ac27a0ec 984{
9f7dd93d 985 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
b31e1552
ES
986 ext4_msg(inode->i_sb, KERN_ERR,
987 "Inode %lu (%p): orphan list check failed!",
988 inode->i_ino, EXT4_I(inode));
9f7dd93d
VA
989 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
990 EXT4_I(inode), sizeof(struct ext4_inode_info),
991 true);
992 dump_stack();
993 }
fa0d7e3d 994 call_rcu(&inode->i_rcu, ext4_i_callback);
ac27a0ec
DK
995}
996
51cc5068 997static void init_once(void *foo)
ac27a0ec 998{
617ba13b 999 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
ac27a0ec 1000
a35afb83 1001 INIT_LIST_HEAD(&ei->i_orphan);
03010a33 1002#ifdef CONFIG_EXT4_FS_XATTR
a35afb83 1003 init_rwsem(&ei->xattr_sem);
ac27a0ec 1004#endif
0e855ac8 1005 init_rwsem(&ei->i_data_sem);
a35afb83 1006 inode_init_once(&ei->vfs_inode);
ac27a0ec
DK
1007}
1008
1009static int init_inodecache(void)
1010{
617ba13b
MC
1011 ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
1012 sizeof(struct ext4_inode_info),
ac27a0ec
DK
1013 0, (SLAB_RECLAIM_ACCOUNT|
1014 SLAB_MEM_SPREAD),
20c2df83 1015 init_once);
617ba13b 1016 if (ext4_inode_cachep == NULL)
ac27a0ec
DK
1017 return -ENOMEM;
1018 return 0;
1019}
1020
1021static void destroy_inodecache(void)
1022{
8c0a8537
KS
1023 /*
1024 * Make sure all delayed rcu free inodes are flushed before we
1025 * destroy cache.
1026 */
1027 rcu_barrier();
617ba13b 1028 kmem_cache_destroy(ext4_inode_cachep);
ac27a0ec
DK
1029}
1030
0930fcc1 1031void ext4_clear_inode(struct inode *inode)
ac27a0ec 1032{
0930fcc1 1033 invalidate_inode_buffers(inode);
dbd5768f 1034 clear_inode(inode);
9f754758 1035 dquot_drop(inode);
c2ea3fde 1036 ext4_discard_preallocations(inode);
51865fda 1037 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
8aefcd55
TT
1038 if (EXT4_I(inode)->jinode) {
1039 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1040 EXT4_I(inode)->jinode);
1041 jbd2_free_inode(EXT4_I(inode)->jinode);
1042 EXT4_I(inode)->jinode = NULL;
1043 }
ac27a0ec
DK
1044}
1045
1b961ac0 1046static struct inode *ext4_nfs_get_inode(struct super_block *sb,
0b8e58a1 1047 u64 ino, u32 generation)
ac27a0ec 1048{
ac27a0ec 1049 struct inode *inode;
ac27a0ec 1050
617ba13b 1051 if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
ac27a0ec 1052 return ERR_PTR(-ESTALE);
617ba13b 1053 if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
ac27a0ec
DK
1054 return ERR_PTR(-ESTALE);
1055
1056 /* iget isn't really right if the inode is currently unallocated!!
1057 *
617ba13b 1058 * ext4_read_inode will return a bad_inode if the inode had been
ac27a0ec
DK
1059 * deleted, so we should be safe.
1060 *
1061 * Currently we don't know the generation for parent directory, so
1062 * a generation of 0 means "accept any"
1063 */
1d1fe1ee
DH
1064 inode = ext4_iget(sb, ino);
1065 if (IS_ERR(inode))
1066 return ERR_CAST(inode);
1067 if (generation && inode->i_generation != generation) {
ac27a0ec
DK
1068 iput(inode);
1069 return ERR_PTR(-ESTALE);
1070 }
1b961ac0
CH
1071
1072 return inode;
1073}
1074
1075static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
0b8e58a1 1076 int fh_len, int fh_type)
1b961ac0
CH
1077{
1078 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1079 ext4_nfs_get_inode);
1080}
1081
1082static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
0b8e58a1 1083 int fh_len, int fh_type)
1b961ac0
CH
1084{
1085 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1086 ext4_nfs_get_inode);
ac27a0ec
DK
1087}
1088
c39a7f84
TO
1089/*
1090 * Try to release metadata pages (indirect blocks, directories) which are
1091 * mapped via the block device. Since these pages could have journal heads
1092 * which would prevent try_to_free_buffers() from freeing them, we must use
1093 * jbd2 layer's try_to_free_buffers() function to release them.
1094 */
0b8e58a1
AD
1095static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1096 gfp_t wait)
c39a7f84
TO
1097{
1098 journal_t *journal = EXT4_SB(sb)->s_journal;
1099
1100 WARN_ON(PageChecked(page));
1101 if (!page_has_buffers(page))
1102 return 0;
1103 if (journal)
1104 return jbd2_journal_try_to_free_buffers(journal, page,
1105 wait & ~__GFP_WAIT);
1106 return try_to_free_buffers(page);
1107}
1108
ac27a0ec 1109#ifdef CONFIG_QUOTA
af5bc92d 1110#define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
2b2d6d01 1111#define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
ac27a0ec 1112
617ba13b
MC
1113static int ext4_write_dquot(struct dquot *dquot);
1114static int ext4_acquire_dquot(struct dquot *dquot);
1115static int ext4_release_dquot(struct dquot *dquot);
1116static int ext4_mark_dquot_dirty(struct dquot *dquot);
1117static int ext4_write_info(struct super_block *sb, int type);
6f28e087 1118static int ext4_quota_on(struct super_block *sb, int type, int format_id,
f00c9e44 1119 struct path *path);
7c319d32
AK
1120static int ext4_quota_on_sysfile(struct super_block *sb, int type,
1121 int format_id);
ca0e05e4 1122static int ext4_quota_off(struct super_block *sb, int type);
7c319d32 1123static int ext4_quota_off_sysfile(struct super_block *sb, int type);
617ba13b
MC
1124static int ext4_quota_on_mount(struct super_block *sb, int type);
1125static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec 1126 size_t len, loff_t off);
617ba13b 1127static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec 1128 const char *data, size_t len, loff_t off);
7c319d32
AK
1129static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1130 unsigned int flags);
1131static int ext4_enable_quotas(struct super_block *sb);
ac27a0ec 1132
61e225dc 1133static const struct dquot_operations ext4_quota_operations = {
60e58e0f 1134 .get_reserved_space = ext4_get_reserved_space,
617ba13b
MC
1135 .write_dquot = ext4_write_dquot,
1136 .acquire_dquot = ext4_acquire_dquot,
1137 .release_dquot = ext4_release_dquot,
1138 .mark_dirty = ext4_mark_dquot_dirty,
a5b5ee32
JK
1139 .write_info = ext4_write_info,
1140 .alloc_dquot = dquot_alloc,
1141 .destroy_dquot = dquot_destroy,
ac27a0ec
DK
1142};
1143
0d54b217 1144static const struct quotactl_ops ext4_qctl_operations = {
617ba13b 1145 .quota_on = ext4_quota_on,
ca0e05e4 1146 .quota_off = ext4_quota_off,
287a8095
CH
1147 .quota_sync = dquot_quota_sync,
1148 .get_info = dquot_get_dqinfo,
1149 .set_info = dquot_set_dqinfo,
1150 .get_dqblk = dquot_get_dqblk,
1151 .set_dqblk = dquot_set_dqblk
ac27a0ec 1152};
7c319d32
AK
1153
1154static const struct quotactl_ops ext4_qctl_sysfile_operations = {
1155 .quota_on_meta = ext4_quota_on_sysfile,
1156 .quota_off = ext4_quota_off_sysfile,
1157 .quota_sync = dquot_quota_sync,
1158 .get_info = dquot_get_dqinfo,
1159 .set_info = dquot_set_dqinfo,
1160 .get_dqblk = dquot_get_dqblk,
1161 .set_dqblk = dquot_set_dqblk
1162};
ac27a0ec
DK
1163#endif
1164
ee9b6d61 1165static const struct super_operations ext4_sops = {
617ba13b
MC
1166 .alloc_inode = ext4_alloc_inode,
1167 .destroy_inode = ext4_destroy_inode,
617ba13b
MC
1168 .write_inode = ext4_write_inode,
1169 .dirty_inode = ext4_dirty_inode,
7ff9c073 1170 .drop_inode = ext4_drop_inode,
0930fcc1 1171 .evict_inode = ext4_evict_inode,
617ba13b 1172 .put_super = ext4_put_super,
617ba13b 1173 .sync_fs = ext4_sync_fs,
c4be0c1d
TS
1174 .freeze_fs = ext4_freeze,
1175 .unfreeze_fs = ext4_unfreeze,
617ba13b
MC
1176 .statfs = ext4_statfs,
1177 .remount_fs = ext4_remount,
617ba13b 1178 .show_options = ext4_show_options,
ac27a0ec 1179#ifdef CONFIG_QUOTA
617ba13b
MC
1180 .quota_read = ext4_quota_read,
1181 .quota_write = ext4_quota_write,
ac27a0ec 1182#endif
c39a7f84 1183 .bdev_try_to_free_page = bdev_try_to_free_page,
ac27a0ec
DK
1184};
1185
9ca92389
TT
1186static const struct super_operations ext4_nojournal_sops = {
1187 .alloc_inode = ext4_alloc_inode,
1188 .destroy_inode = ext4_destroy_inode,
1189 .write_inode = ext4_write_inode,
1190 .dirty_inode = ext4_dirty_inode,
7ff9c073 1191 .drop_inode = ext4_drop_inode,
0930fcc1 1192 .evict_inode = ext4_evict_inode,
9ca92389
TT
1193 .put_super = ext4_put_super,
1194 .statfs = ext4_statfs,
1195 .remount_fs = ext4_remount,
9ca92389
TT
1196 .show_options = ext4_show_options,
1197#ifdef CONFIG_QUOTA
1198 .quota_read = ext4_quota_read,
1199 .quota_write = ext4_quota_write,
1200#endif
1201 .bdev_try_to_free_page = bdev_try_to_free_page,
1202};
1203
39655164 1204static const struct export_operations ext4_export_ops = {
1b961ac0
CH
1205 .fh_to_dentry = ext4_fh_to_dentry,
1206 .fh_to_parent = ext4_fh_to_parent,
617ba13b 1207 .get_parent = ext4_get_parent,
ac27a0ec
DK
1208};
1209
1210enum {
1211 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1212 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
72578c33 1213 Opt_nouid32, Opt_debug, Opt_removed,
ac27a0ec 1214 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
72578c33 1215 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
30773840 1216 Opt_commit, Opt_min_batch_time, Opt_max_batch_time,
43e625d8 1217 Opt_journal_dev, Opt_journal_checksum, Opt_journal_async_commit,
ac27a0ec 1218 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
296c355c 1219 Opt_data_err_abort, Opt_data_err_ignore,
ac27a0ec 1220 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
5a20bdfc 1221 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
ee4a3fcd 1222 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
661aa520 1223 Opt_usrquota, Opt_grpquota, Opt_i_version,
1449032b
TT
1224 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
1225 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
5328e635 1226 Opt_inode_readahead_blks, Opt_journal_ioprio,
744692dc 1227 Opt_dioread_nolock, Opt_dioread_lock,
fc6cb1cd 1228 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
df981d03 1229 Opt_max_dir_size_kb,
ac27a0ec
DK
1230};
1231
a447c093 1232static const match_table_t tokens = {
ac27a0ec
DK
1233 {Opt_bsd_df, "bsddf"},
1234 {Opt_minix_df, "minixdf"},
1235 {Opt_grpid, "grpid"},
1236 {Opt_grpid, "bsdgroups"},
1237 {Opt_nogrpid, "nogrpid"},
1238 {Opt_nogrpid, "sysvgroups"},
1239 {Opt_resgid, "resgid=%u"},
1240 {Opt_resuid, "resuid=%u"},
1241 {Opt_sb, "sb=%u"},
1242 {Opt_err_cont, "errors=continue"},
1243 {Opt_err_panic, "errors=panic"},
1244 {Opt_err_ro, "errors=remount-ro"},
1245 {Opt_nouid32, "nouid32"},
ac27a0ec 1246 {Opt_debug, "debug"},
72578c33
TT
1247 {Opt_removed, "oldalloc"},
1248 {Opt_removed, "orlov"},
ac27a0ec
DK
1249 {Opt_user_xattr, "user_xattr"},
1250 {Opt_nouser_xattr, "nouser_xattr"},
1251 {Opt_acl, "acl"},
1252 {Opt_noacl, "noacl"},
e3bb52ae 1253 {Opt_noload, "norecovery"},
5a916be1 1254 {Opt_noload, "noload"},
72578c33
TT
1255 {Opt_removed, "nobh"},
1256 {Opt_removed, "bh"},
ac27a0ec 1257 {Opt_commit, "commit=%u"},
30773840
TT
1258 {Opt_min_batch_time, "min_batch_time=%u"},
1259 {Opt_max_batch_time, "max_batch_time=%u"},
ac27a0ec 1260 {Opt_journal_dev, "journal_dev=%u"},
818d276c
GS
1261 {Opt_journal_checksum, "journal_checksum"},
1262 {Opt_journal_async_commit, "journal_async_commit"},
ac27a0ec
DK
1263 {Opt_abort, "abort"},
1264 {Opt_data_journal, "data=journal"},
1265 {Opt_data_ordered, "data=ordered"},
1266 {Opt_data_writeback, "data=writeback"},
5bf5683a
HK
1267 {Opt_data_err_abort, "data_err=abort"},
1268 {Opt_data_err_ignore, "data_err=ignore"},
ac27a0ec
DK
1269 {Opt_offusrjquota, "usrjquota="},
1270 {Opt_usrjquota, "usrjquota=%s"},
1271 {Opt_offgrpjquota, "grpjquota="},
1272 {Opt_grpjquota, "grpjquota=%s"},
1273 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1274 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
5a20bdfc 1275 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
ac27a0ec
DK
1276 {Opt_grpquota, "grpquota"},
1277 {Opt_noquota, "noquota"},
1278 {Opt_quota, "quota"},
1279 {Opt_usrquota, "usrquota"},
1280 {Opt_barrier, "barrier=%u"},
06705bff
TT
1281 {Opt_barrier, "barrier"},
1282 {Opt_nobarrier, "nobarrier"},
25ec56b5 1283 {Opt_i_version, "i_version"},
c9de560d 1284 {Opt_stripe, "stripe=%u"},
64769240 1285 {Opt_delalloc, "delalloc"},
dd919b98 1286 {Opt_nodelalloc, "nodelalloc"},
1449032b
TT
1287 {Opt_mblk_io_submit, "mblk_io_submit"},
1288 {Opt_nomblk_io_submit, "nomblk_io_submit"},
6fd058f7
TT
1289 {Opt_block_validity, "block_validity"},
1290 {Opt_noblock_validity, "noblock_validity"},
240799cd 1291 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
b3881f74 1292 {Opt_journal_ioprio, "journal_ioprio=%u"},
afd4672d 1293 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
06705bff
TT
1294 {Opt_auto_da_alloc, "auto_da_alloc"},
1295 {Opt_noauto_da_alloc, "noauto_da_alloc"},
744692dc
JZ
1296 {Opt_dioread_nolock, "dioread_nolock"},
1297 {Opt_dioread_lock, "dioread_lock"},
5328e635
ES
1298 {Opt_discard, "discard"},
1299 {Opt_nodiscard, "nodiscard"},
fc6cb1cd
TT
1300 {Opt_init_itable, "init_itable=%u"},
1301 {Opt_init_itable, "init_itable"},
1302 {Opt_noinit_itable, "noinit_itable"},
df981d03 1303 {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
c7198b9c
TT
1304 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
1305 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
1306 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
1307 {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1308 {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */
f3f12faa 1309 {Opt_err, NULL},
ac27a0ec
DK
1310};
1311
617ba13b 1312static ext4_fsblk_t get_sb_block(void **data)
ac27a0ec 1313{
617ba13b 1314 ext4_fsblk_t sb_block;
ac27a0ec
DK
1315 char *options = (char *) *data;
1316
1317 if (!options || strncmp(options, "sb=", 3) != 0)
1318 return 1; /* Default location */
0b8e58a1 1319
ac27a0ec 1320 options += 3;
0b8e58a1 1321 /* TODO: use simple_strtoll with >32bit ext4 */
ac27a0ec
DK
1322 sb_block = simple_strtoul(options, &options, 0);
1323 if (*options && *options != ',') {
4776004f 1324 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
ac27a0ec
DK
1325 (char *) *data);
1326 return 1;
1327 }
1328 if (*options == ',')
1329 options++;
1330 *data = (void *) options;
0b8e58a1 1331
ac27a0ec
DK
1332 return sb_block;
1333}
1334
b3881f74 1335#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
437ca0fd
DM
1336static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1337 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
b3881f74 1338
56c50f11
DM
1339#ifdef CONFIG_QUOTA
1340static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1341{
1342 struct ext4_sb_info *sbi = EXT4_SB(sb);
1343 char *qname;
1344
1345 if (sb_any_quota_loaded(sb) &&
1346 !sbi->s_qf_names[qtype]) {
1347 ext4_msg(sb, KERN_ERR,
1348 "Cannot change journaled "
1349 "quota options when quota turned on");
57f73c2c 1350 return -1;
56c50f11
DM
1351 }
1352 qname = match_strdup(args);
1353 if (!qname) {
1354 ext4_msg(sb, KERN_ERR,
1355 "Not enough memory for storing quotafile name");
57f73c2c 1356 return -1;
56c50f11
DM
1357 }
1358 if (sbi->s_qf_names[qtype] &&
1359 strcmp(sbi->s_qf_names[qtype], qname)) {
1360 ext4_msg(sb, KERN_ERR,
1361 "%s quota file already specified", QTYPE2NAME(qtype));
1362 kfree(qname);
57f73c2c 1363 return -1;
56c50f11
DM
1364 }
1365 sbi->s_qf_names[qtype] = qname;
1366 if (strchr(sbi->s_qf_names[qtype], '/')) {
1367 ext4_msg(sb, KERN_ERR,
1368 "quotafile must be on filesystem root");
1369 kfree(sbi->s_qf_names[qtype]);
1370 sbi->s_qf_names[qtype] = NULL;
57f73c2c 1371 return -1;
56c50f11 1372 }
fd8c37ec 1373 set_opt(sb, QUOTA);
56c50f11
DM
1374 return 1;
1375}
1376
1377static int clear_qf_name(struct super_block *sb, int qtype)
1378{
1379
1380 struct ext4_sb_info *sbi = EXT4_SB(sb);
1381
1382 if (sb_any_quota_loaded(sb) &&
1383 sbi->s_qf_names[qtype]) {
1384 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1385 " when quota turned on");
57f73c2c 1386 return -1;
56c50f11
DM
1387 }
1388 /*
1389 * The space will be released later when all options are confirmed
1390 * to be correct
1391 */
1392 sbi->s_qf_names[qtype] = NULL;
1393 return 1;
1394}
1395#endif
1396
26092bf5
TT
1397#define MOPT_SET 0x0001
1398#define MOPT_CLEAR 0x0002
1399#define MOPT_NOSUPPORT 0x0004
1400#define MOPT_EXPLICIT 0x0008
1401#define MOPT_CLEAR_ERR 0x0010
1402#define MOPT_GTE0 0x0020
ac27a0ec 1403#ifdef CONFIG_QUOTA
26092bf5
TT
1404#define MOPT_Q 0
1405#define MOPT_QFMT 0x0040
1406#else
1407#define MOPT_Q MOPT_NOSUPPORT
1408#define MOPT_QFMT MOPT_NOSUPPORT
ac27a0ec 1409#endif
26092bf5
TT
1410#define MOPT_DATAJ 0x0080
1411
1412static const struct mount_opts {
1413 int token;
1414 int mount_opt;
1415 int flags;
1416} ext4_mount_opts[] = {
1417 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1418 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1419 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1420 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
1421 {Opt_mblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_SET},
1422 {Opt_nomblk_io_submit, EXT4_MOUNT_MBLK_IO_SUBMIT, MOPT_CLEAR},
1423 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1424 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
1425 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_SET},
1426 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK, MOPT_CLEAR},
1427 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1428 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
1429 {Opt_delalloc, EXT4_MOUNT_DELALLOC, MOPT_SET | MOPT_EXPLICIT},
1430 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC, MOPT_CLEAR | MOPT_EXPLICIT},
1431 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM, MOPT_SET},
1432 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
1433 EXT4_MOUNT_JOURNAL_CHECKSUM), MOPT_SET},
1434 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_SET},
1435 {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1436 {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1437 {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
1438 {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_SET},
1439 {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_CLEAR},
1440 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1441 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1442 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1443 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1444 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1445 {Opt_commit, 0, MOPT_GTE0},
1446 {Opt_max_batch_time, 0, MOPT_GTE0},
1447 {Opt_min_batch_time, 0, MOPT_GTE0},
1448 {Opt_inode_readahead_blks, 0, MOPT_GTE0},
1449 {Opt_init_itable, 0, MOPT_GTE0},
1450 {Opt_stripe, 0, MOPT_GTE0},
1451 {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_DATAJ},
1452 {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_DATAJ},
1453 {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA, MOPT_DATAJ},
03010a33 1454#ifdef CONFIG_EXT4_FS_XATTR
26092bf5
TT
1455 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1456 {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
ac27a0ec 1457#else
26092bf5
TT
1458 {Opt_user_xattr, 0, MOPT_NOSUPPORT},
1459 {Opt_nouser_xattr, 0, MOPT_NOSUPPORT},
ac27a0ec 1460#endif
03010a33 1461#ifdef CONFIG_EXT4_FS_POSIX_ACL
26092bf5
TT
1462 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1463 {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
ac27a0ec 1464#else
26092bf5
TT
1465 {Opt_acl, 0, MOPT_NOSUPPORT},
1466 {Opt_noacl, 0, MOPT_NOSUPPORT},
ac27a0ec 1467#endif
26092bf5
TT
1468 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1469 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1470 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1471 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1472 MOPT_SET | MOPT_Q},
1473 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1474 MOPT_SET | MOPT_Q},
1475 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
1476 EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
1477 {Opt_usrjquota, 0, MOPT_Q},
1478 {Opt_grpjquota, 0, MOPT_Q},
1479 {Opt_offusrjquota, 0, MOPT_Q},
1480 {Opt_offgrpjquota, 0, MOPT_Q},
1481 {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
1482 {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
1483 {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
df981d03 1484 {Opt_max_dir_size_kb, 0, MOPT_GTE0},
26092bf5
TT
1485 {Opt_err, 0, 0}
1486};
1487
1488static int handle_mount_opt(struct super_block *sb, char *opt, int token,
1489 substring_t *args, unsigned long *journal_devnum,
1490 unsigned int *journal_ioprio, int is_remount)
1491{
1492 struct ext4_sb_info *sbi = EXT4_SB(sb);
1493 const struct mount_opts *m;
08cefc7a
EB
1494 kuid_t uid;
1495 kgid_t gid;
26092bf5
TT
1496 int arg = 0;
1497
57f73c2c
TT
1498#ifdef CONFIG_QUOTA
1499 if (token == Opt_usrjquota)
1500 return set_qf_name(sb, USRQUOTA, &args[0]);
1501 else if (token == Opt_grpjquota)
1502 return set_qf_name(sb, GRPQUOTA, &args[0]);
1503 else if (token == Opt_offusrjquota)
1504 return clear_qf_name(sb, USRQUOTA);
1505 else if (token == Opt_offgrpjquota)
1506 return clear_qf_name(sb, GRPQUOTA);
1507#endif
26092bf5
TT
1508 if (args->from && match_int(args, &arg))
1509 return -1;
1510 switch (token) {
f7048605
TT
1511 case Opt_noacl:
1512 case Opt_nouser_xattr:
1513 ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1514 break;
26092bf5
TT
1515 case Opt_sb:
1516 return 1; /* handled by get_sb_block() */
1517 case Opt_removed:
1518 ext4_msg(sb, KERN_WARNING,
1519 "Ignoring removed %s option", opt);
1520 return 1;
1521 case Opt_resuid:
08cefc7a
EB
1522 uid = make_kuid(current_user_ns(), arg);
1523 if (!uid_valid(uid)) {
1524 ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
1525 return -1;
1526 }
1527 sbi->s_resuid = uid;
26092bf5
TT
1528 return 1;
1529 case Opt_resgid:
08cefc7a
EB
1530 gid = make_kgid(current_user_ns(), arg);
1531 if (!gid_valid(gid)) {
1532 ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
1533 return -1;
1534 }
1535 sbi->s_resgid = gid;
26092bf5
TT
1536 return 1;
1537 case Opt_abort:
1538 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1539 return 1;
1540 case Opt_i_version:
1541 sb->s_flags |= MS_I_VERSION;
1542 return 1;
1543 case Opt_journal_dev:
1544 if (is_remount) {
1545 ext4_msg(sb, KERN_ERR,
1546 "Cannot specify journal on remount");
1547 return -1;
1548 }
1549 *journal_devnum = arg;
1550 return 1;
1551 case Opt_journal_ioprio:
1552 if (arg < 0 || arg > 7)
1553 return -1;
1554 *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
1555 return 1;
1556 }
1557
1558 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
1559 if (token != m->token)
1560 continue;
1561 if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
1562 return -1;
1563 if (m->flags & MOPT_EXPLICIT)
1564 set_opt2(sb, EXPLICIT_DELALLOC);
1565 if (m->flags & MOPT_CLEAR_ERR)
1566 clear_opt(sb, ERRORS_MASK);
1567 if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
1568 ext4_msg(sb, KERN_ERR, "Cannot change quota "
1569 "options when quota turned on");
1570 return -1;
1571 }
1572
1573 if (m->flags & MOPT_NOSUPPORT) {
1574 ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
1575 } else if (token == Opt_commit) {
1576 if (arg == 0)
1577 arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
1578 sbi->s_commit_interval = HZ * arg;
1579 } else if (token == Opt_max_batch_time) {
1580 if (arg == 0)
1581 arg = EXT4_DEF_MAX_BATCH_TIME;
1582 sbi->s_max_batch_time = arg;
1583 } else if (token == Opt_min_batch_time) {
1584 sbi->s_min_batch_time = arg;
1585 } else if (token == Opt_inode_readahead_blks) {
1586 if (arg > (1 << 30))
1587 return -1;
1588 if (arg && !is_power_of_2(arg)) {
b31e1552 1589 ext4_msg(sb, KERN_ERR,
26092bf5
TT
1590 "EXT4-fs: inode_readahead_blks"
1591 " must be a power of 2");
1592 return -1;
ac27a0ec 1593 }
26092bf5
TT
1594 sbi->s_inode_readahead_blks = arg;
1595 } else if (token == Opt_init_itable) {
1596 set_opt(sb, INIT_INODE_TABLE);
1597 if (!args->from)
1598 arg = EXT4_DEF_LI_WAIT_MULT;
1599 sbi->s_li_wait_mult = arg;
df981d03
TT
1600 } else if (token == Opt_max_dir_size_kb) {
1601 sbi->s_max_dir_size_kb = arg;
26092bf5
TT
1602 } else if (token == Opt_stripe) {
1603 sbi->s_stripe = arg;
1604 } else if (m->flags & MOPT_DATAJ) {
ac27a0ec 1605 if (is_remount) {
eb513689
TT
1606 if (!sbi->s_journal)
1607 ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
26092bf5
TT
1608 else if (test_opt(sb, DATA_FLAGS) !=
1609 m->mount_opt) {
b31e1552 1610 ext4_msg(sb, KERN_ERR,
26092bf5
TT
1611 "Cannot change data mode on remount");
1612 return -1;
ac27a0ec
DK
1613 }
1614 } else {
fd8c37ec 1615 clear_opt(sb, DATA_FLAGS);
26092bf5 1616 sbi->s_mount_opt |= m->mount_opt;
ac27a0ec 1617 }
ac27a0ec 1618#ifdef CONFIG_QUOTA
26092bf5 1619 } else if (m->flags & MOPT_QFMT) {
17bd13b3 1620 if (sb_any_quota_loaded(sb) &&
26092bf5
TT
1621 sbi->s_jquota_fmt != m->mount_opt) {
1622 ext4_msg(sb, KERN_ERR, "Cannot "
1623 "change journaled quota options "
1624 "when quota turned on");
1625 return -1;
ac27a0ec 1626 }
26092bf5 1627 sbi->s_jquota_fmt = m->mount_opt;
ac27a0ec 1628#endif
26092bf5
TT
1629 } else {
1630 if (!args->from)
1631 arg = 1;
1632 if (m->flags & MOPT_CLEAR)
1633 arg = !arg;
1634 else if (unlikely(!(m->flags & MOPT_SET))) {
1635 ext4_msg(sb, KERN_WARNING,
1636 "buggy handling of option %s", opt);
1637 WARN_ON(1);
1638 return -1;
3197ebdb 1639 }
26092bf5
TT
1640 if (arg != 0)
1641 sbi->s_mount_opt |= m->mount_opt;
afd4672d 1642 else
26092bf5 1643 sbi->s_mount_opt &= ~m->mount_opt;
ac27a0ec 1644 }
26092bf5
TT
1645 return 1;
1646 }
1647 ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
1648 "or missing value", opt);
1649 return -1;
1650}
1651
1652static int parse_options(char *options, struct super_block *sb,
1653 unsigned long *journal_devnum,
1654 unsigned int *journal_ioprio,
1655 int is_remount)
1656{
db7e5c66 1657#ifdef CONFIG_QUOTA
26092bf5 1658 struct ext4_sb_info *sbi = EXT4_SB(sb);
db7e5c66 1659#endif
26092bf5
TT
1660 char *p;
1661 substring_t args[MAX_OPT_ARGS];
1662 int token;
1663
1664 if (!options)
1665 return 1;
1666
1667 while ((p = strsep(&options, ",")) != NULL) {
1668 if (!*p)
1669 continue;
1670 /*
1671 * Initialize args struct so we know whether arg was
1672 * found; some options take optional arguments.
1673 */
caecd0af 1674 args[0].to = args[0].from = NULL;
26092bf5
TT
1675 token = match_token(p, tokens, args);
1676 if (handle_mount_opt(sb, p, token, args, journal_devnum,
1677 journal_ioprio, is_remount) < 0)
1678 return 0;
ac27a0ec
DK
1679 }
1680#ifdef CONFIG_QUOTA
1681 if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
482a7425 1682 if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
fd8c37ec 1683 clear_opt(sb, USRQUOTA);
ac27a0ec 1684
482a7425 1685 if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
fd8c37ec 1686 clear_opt(sb, GRPQUOTA);
ac27a0ec 1687
56c50f11 1688 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
b31e1552
ES
1689 ext4_msg(sb, KERN_ERR, "old and new quota "
1690 "format mixing");
ac27a0ec
DK
1691 return 0;
1692 }
1693
1694 if (!sbi->s_jquota_fmt) {
b31e1552
ES
1695 ext4_msg(sb, KERN_ERR, "journaled quota format "
1696 "not specified");
ac27a0ec
DK
1697 return 0;
1698 }
1699 } else {
1700 if (sbi->s_jquota_fmt) {
b31e1552 1701 ext4_msg(sb, KERN_ERR, "journaled quota format "
2c8be6b2 1702 "specified with no journaling "
b31e1552 1703 "enabled");
ac27a0ec
DK
1704 return 0;
1705 }
1706 }
1707#endif
1708 return 1;
1709}
1710
2adf6da8
TT
1711static inline void ext4_show_quota_options(struct seq_file *seq,
1712 struct super_block *sb)
1713{
1714#if defined(CONFIG_QUOTA)
1715 struct ext4_sb_info *sbi = EXT4_SB(sb);
1716
1717 if (sbi->s_jquota_fmt) {
1718 char *fmtname = "";
1719
1720 switch (sbi->s_jquota_fmt) {
1721 case QFMT_VFS_OLD:
1722 fmtname = "vfsold";
1723 break;
1724 case QFMT_VFS_V0:
1725 fmtname = "vfsv0";
1726 break;
1727 case QFMT_VFS_V1:
1728 fmtname = "vfsv1";
1729 break;
1730 }
1731 seq_printf(seq, ",jqfmt=%s", fmtname);
1732 }
1733
1734 if (sbi->s_qf_names[USRQUOTA])
1735 seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
1736
1737 if (sbi->s_qf_names[GRPQUOTA])
1738 seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
1739
1740 if (test_opt(sb, USRQUOTA))
1741 seq_puts(seq, ",usrquota");
1742
1743 if (test_opt(sb, GRPQUOTA))
1744 seq_puts(seq, ",grpquota");
1745#endif
1746}
1747
5a916be1
TT
1748static const char *token2str(int token)
1749{
50df9fd5 1750 const struct match_token *t;
5a916be1
TT
1751
1752 for (t = tokens; t->token != Opt_err; t++)
1753 if (t->token == token && !strchr(t->pattern, '='))
1754 break;
1755 return t->pattern;
1756}
1757
2adf6da8
TT
1758/*
1759 * Show an option if
1760 * - it's set to a non-default value OR
1761 * - if the per-sb default is different from the global default
1762 */
66acdcf4
TT
1763static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
1764 int nodefs)
2adf6da8 1765{
2adf6da8
TT
1766 struct ext4_sb_info *sbi = EXT4_SB(sb);
1767 struct ext4_super_block *es = sbi->s_es;
66acdcf4 1768 int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
5a916be1 1769 const struct mount_opts *m;
66acdcf4 1770 char sep = nodefs ? '\n' : ',';
2adf6da8 1771
66acdcf4
TT
1772#define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
1773#define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
2adf6da8
TT
1774
1775 if (sbi->s_sb_block != 1)
5a916be1
TT
1776 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
1777
1778 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
1779 int want_set = m->flags & MOPT_SET;
1780 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
1781 (m->flags & MOPT_CLEAR_ERR))
1782 continue;
66acdcf4 1783 if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
5a916be1
TT
1784 continue; /* skip if same as the default */
1785 if ((want_set &&
1786 (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
1787 (!want_set && (sbi->s_mount_opt & m->mount_opt)))
1788 continue; /* select Opt_noFoo vs Opt_Foo */
1789 SEQ_OPTS_PRINT("%s", token2str(m->token));
2adf6da8 1790 }
5a916be1 1791
08cefc7a 1792 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
5a916be1 1793 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
08cefc7a
EB
1794 SEQ_OPTS_PRINT("resuid=%u",
1795 from_kuid_munged(&init_user_ns, sbi->s_resuid));
1796 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
5a916be1 1797 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
08cefc7a
EB
1798 SEQ_OPTS_PRINT("resgid=%u",
1799 from_kgid_munged(&init_user_ns, sbi->s_resgid));
66acdcf4 1800 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
5a916be1
TT
1801 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
1802 SEQ_OPTS_PUTS("errors=remount-ro");
2adf6da8 1803 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
5a916be1 1804 SEQ_OPTS_PUTS("errors=continue");
2adf6da8 1805 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
5a916be1 1806 SEQ_OPTS_PUTS("errors=panic");
66acdcf4 1807 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
5a916be1 1808 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
66acdcf4 1809 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
5a916be1 1810 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
66acdcf4 1811 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
5a916be1 1812 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
2adf6da8 1813 if (sb->s_flags & MS_I_VERSION)
5a916be1 1814 SEQ_OPTS_PUTS("i_version");
66acdcf4 1815 if (nodefs || sbi->s_stripe)
5a916be1 1816 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
66acdcf4 1817 if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
5a916be1
TT
1818 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
1819 SEQ_OPTS_PUTS("data=journal");
1820 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
1821 SEQ_OPTS_PUTS("data=ordered");
1822 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
1823 SEQ_OPTS_PUTS("data=writeback");
1824 }
66acdcf4
TT
1825 if (nodefs ||
1826 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
5a916be1
TT
1827 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
1828 sbi->s_inode_readahead_blks);
2adf6da8 1829
66acdcf4
TT
1830 if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
1831 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
5a916be1 1832 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
df981d03
TT
1833 if (nodefs || sbi->s_max_dir_size_kb)
1834 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
2adf6da8
TT
1835
1836 ext4_show_quota_options(seq, sb);
2adf6da8
TT
1837 return 0;
1838}
1839
66acdcf4
TT
1840static int ext4_show_options(struct seq_file *seq, struct dentry *root)
1841{
1842 return _ext4_show_options(seq, root->d_sb, 0);
1843}
1844
1845static int options_seq_show(struct seq_file *seq, void *offset)
1846{
1847 struct super_block *sb = seq->private;
1848 int rc;
1849
1850 seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
1851 rc = _ext4_show_options(seq, sb, 1);
1852 seq_puts(seq, "\n");
1853 return rc;
1854}
1855
1856static int options_open_fs(struct inode *inode, struct file *file)
1857{
1858 return single_open(file, options_seq_show, PDE(inode)->data);
1859}
1860
1861static const struct file_operations ext4_seq_options_fops = {
1862 .owner = THIS_MODULE,
1863 .open = options_open_fs,
1864 .read = seq_read,
1865 .llseek = seq_lseek,
1866 .release = single_release,
1867};
1868
617ba13b 1869static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
1870 int read_only)
1871{
617ba13b 1872 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec
DK
1873 int res = 0;
1874
617ba13b 1875 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
b31e1552
ES
1876 ext4_msg(sb, KERN_ERR, "revision level too high, "
1877 "forcing read-only mode");
ac27a0ec
DK
1878 res = MS_RDONLY;
1879 }
1880 if (read_only)
281b5995 1881 goto done;
617ba13b 1882 if (!(sbi->s_mount_state & EXT4_VALID_FS))
b31e1552
ES
1883 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1884 "running e2fsck is recommended");
617ba13b 1885 else if ((sbi->s_mount_state & EXT4_ERROR_FS))
b31e1552
ES
1886 ext4_msg(sb, KERN_WARNING,
1887 "warning: mounting fs with errors, "
1888 "running e2fsck is recommended");
ed3ce80a 1889 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
ac27a0ec
DK
1890 le16_to_cpu(es->s_mnt_count) >=
1891 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
b31e1552
ES
1892 ext4_msg(sb, KERN_WARNING,
1893 "warning: maximal mount count reached, "
1894 "running e2fsck is recommended");
ac27a0ec
DK
1895 else if (le32_to_cpu(es->s_checkinterval) &&
1896 (le32_to_cpu(es->s_lastcheck) +
1897 le32_to_cpu(es->s_checkinterval) <= get_seconds()))
b31e1552
ES
1898 ext4_msg(sb, KERN_WARNING,
1899 "warning: checktime reached, "
1900 "running e2fsck is recommended");
0b8e58a1 1901 if (!sbi->s_journal)
0390131b 1902 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec 1903 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 1904 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
e8546d06 1905 le16_add_cpu(&es->s_mnt_count, 1);
ac27a0ec 1906 es->s_mtime = cpu_to_le32(get_seconds());
617ba13b 1907 ext4_update_dynamic_rev(sb);
0390131b
FM
1908 if (sbi->s_journal)
1909 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
ac27a0ec 1910
e2d67052 1911 ext4_commit_super(sb, 1);
281b5995 1912done:
ac27a0ec 1913 if (test_opt(sb, DEBUG))
a9df9a49 1914 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
a2595b8a 1915 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
ac27a0ec
DK
1916 sb->s_blocksize,
1917 sbi->s_groups_count,
617ba13b
MC
1918 EXT4_BLOCKS_PER_GROUP(sb),
1919 EXT4_INODES_PER_GROUP(sb),
a2595b8a 1920 sbi->s_mount_opt, sbi->s_mount_opt2);
ac27a0ec 1921
7abc52c2 1922 cleancache_init_fs(sb);
ac27a0ec
DK
1923 return res;
1924}
1925
117fff10
TT
1926int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1927{
1928 struct ext4_sb_info *sbi = EXT4_SB(sb);
1929 struct flex_groups *new_groups;
1930 int size;
1931
1932 if (!sbi->s_log_groups_per_flex)
1933 return 0;
1934
1935 size = ext4_flex_group(sbi, ngroup - 1) + 1;
1936 if (size <= sbi->s_flex_groups_allocated)
1937 return 0;
1938
1939 size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1940 new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1941 if (!new_groups) {
1942 ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1943 size / (int) sizeof(struct flex_groups));
1944 return -ENOMEM;
1945 }
1946
1947 if (sbi->s_flex_groups) {
1948 memcpy(new_groups, sbi->s_flex_groups,
1949 (sbi->s_flex_groups_allocated *
1950 sizeof(struct flex_groups)));
1951 ext4_kvfree(sbi->s_flex_groups);
1952 }
1953 sbi->s_flex_groups = new_groups;
1954 sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1955 return 0;
1956}
1957
772cb7c8
JS
1958static int ext4_fill_flex_info(struct super_block *sb)
1959{
1960 struct ext4_sb_info *sbi = EXT4_SB(sb);
1961 struct ext4_group_desc *gdp = NULL;
772cb7c8 1962 ext4_group_t flex_group;
d50f2ab6 1963 unsigned int groups_per_flex = 0;
117fff10 1964 int i, err;
772cb7c8 1965
503358ae 1966 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
d50f2ab6 1967 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
772cb7c8
JS
1968 sbi->s_log_groups_per_flex = 0;
1969 return 1;
1970 }
76495ec1 1971 groups_per_flex = 1U << sbi->s_log_groups_per_flex;
772cb7c8 1972
117fff10
TT
1973 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1974 if (err)
9933fc0a 1975 goto failed;
772cb7c8 1976
772cb7c8 1977 for (i = 0; i < sbi->s_groups_count; i++) {
88b6edd1 1978 gdp = ext4_get_group_desc(sb, i, NULL);
772cb7c8
JS
1979
1980 flex_group = ext4_flex_group(sbi, i);
7ad9bb65
TT
1981 atomic_add(ext4_free_inodes_count(sb, gdp),
1982 &sbi->s_flex_groups[flex_group].free_inodes);
021b65bb 1983 atomic_add(ext4_free_group_clusters(sb, gdp),
24aaa8ef 1984 &sbi->s_flex_groups[flex_group].free_clusters);
7ad9bb65
TT
1985 atomic_add(ext4_used_dirs_count(sb, gdp),
1986 &sbi->s_flex_groups[flex_group].used_dirs);
772cb7c8
JS
1987 }
1988
1989 return 1;
1990failed:
1991 return 0;
1992}
1993
feb0ab32
DW
1994static __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
1995 struct ext4_group_desc *gdp)
717d50e4 1996{
feb0ab32 1997 int offset;
717d50e4 1998 __u16 crc = 0;
feb0ab32 1999 __le32 le_group = cpu_to_le32(block_group);
717d50e4 2000
feb0ab32
DW
2001 if ((sbi->s_es->s_feature_ro_compat &
2002 cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))) {
2003 /* Use new metadata_csum algorithm */
2004 __u16 old_csum;
2005 __u32 csum32;
2006
2007 old_csum = gdp->bg_checksum;
2008 gdp->bg_checksum = 0;
2009 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2010 sizeof(le_group));
2011 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
2012 sbi->s_desc_size);
2013 gdp->bg_checksum = old_csum;
2014
2015 crc = csum32 & 0xFFFF;
2016 goto out;
717d50e4
AD
2017 }
2018
feb0ab32
DW
2019 /* old crc16 code */
2020 offset = offsetof(struct ext4_group_desc, bg_checksum);
2021
2022 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2023 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2024 crc = crc16(crc, (__u8 *)gdp, offset);
2025 offset += sizeof(gdp->bg_checksum); /* skip checksum */
2026 /* for checksum of struct ext4_group_desc do the rest...*/
2027 if ((sbi->s_es->s_feature_incompat &
2028 cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2029 offset < le16_to_cpu(sbi->s_es->s_desc_size))
2030 crc = crc16(crc, (__u8 *)gdp + offset,
2031 le16_to_cpu(sbi->s_es->s_desc_size) -
2032 offset);
2033
2034out:
717d50e4
AD
2035 return cpu_to_le16(crc);
2036}
2037
feb0ab32 2038int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
717d50e4
AD
2039 struct ext4_group_desc *gdp)
2040{
feb0ab32
DW
2041 if (ext4_has_group_desc_csum(sb) &&
2042 (gdp->bg_checksum != ext4_group_desc_csum(EXT4_SB(sb),
2043 block_group, gdp)))
717d50e4
AD
2044 return 0;
2045
2046 return 1;
2047}
2048
feb0ab32
DW
2049void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2050 struct ext4_group_desc *gdp)
2051{
2052 if (!ext4_has_group_desc_csum(sb))
2053 return;
2054 gdp->bg_checksum = ext4_group_desc_csum(EXT4_SB(sb), block_group, gdp);
2055}
2056
ac27a0ec 2057/* Called at mount-time, super-block is locked */
bfff6873
LC
2058static int ext4_check_descriptors(struct super_block *sb,
2059 ext4_group_t *first_not_zeroed)
ac27a0ec 2060{
617ba13b
MC
2061 struct ext4_sb_info *sbi = EXT4_SB(sb);
2062 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2063 ext4_fsblk_t last_block;
bd81d8ee
LV
2064 ext4_fsblk_t block_bitmap;
2065 ext4_fsblk_t inode_bitmap;
2066 ext4_fsblk_t inode_table;
ce421581 2067 int flexbg_flag = 0;
bfff6873 2068 ext4_group_t i, grp = sbi->s_groups_count;
ac27a0ec 2069
ce421581
JS
2070 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2071 flexbg_flag = 1;
2072
af5bc92d 2073 ext4_debug("Checking group descriptors");
ac27a0ec 2074
197cd65a
AM
2075 for (i = 0; i < sbi->s_groups_count; i++) {
2076 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2077
ce421581 2078 if (i == sbi->s_groups_count - 1 || flexbg_flag)
bd81d8ee 2079 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
2080 else
2081 last_block = first_block +
617ba13b 2082 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 2083
bfff6873
LC
2084 if ((grp == sbi->s_groups_count) &&
2085 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2086 grp = i;
2087
8fadc143 2088 block_bitmap = ext4_block_bitmap(sb, gdp);
2b2d6d01 2089 if (block_bitmap < first_block || block_bitmap > last_block) {
b31e1552 2090 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 2091 "Block bitmap for group %u not in group "
b31e1552 2092 "(block %llu)!", i, block_bitmap);
ac27a0ec
DK
2093 return 0;
2094 }
8fadc143 2095 inode_bitmap = ext4_inode_bitmap(sb, gdp);
2b2d6d01 2096 if (inode_bitmap < first_block || inode_bitmap > last_block) {
b31e1552 2097 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 2098 "Inode bitmap for group %u not in group "
b31e1552 2099 "(block %llu)!", i, inode_bitmap);
ac27a0ec
DK
2100 return 0;
2101 }
8fadc143 2102 inode_table = ext4_inode_table(sb, gdp);
bd81d8ee 2103 if (inode_table < first_block ||
2b2d6d01 2104 inode_table + sbi->s_itb_per_group - 1 > last_block) {
b31e1552 2105 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 2106 "Inode table for group %u not in group "
b31e1552 2107 "(block %llu)!", i, inode_table);
ac27a0ec
DK
2108 return 0;
2109 }
955ce5f5 2110 ext4_lock_group(sb, i);
feb0ab32 2111 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
b31e1552
ES
2112 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2113 "Checksum for group %u failed (%u!=%u)",
2114 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2115 gdp)), le16_to_cpu(gdp->bg_checksum));
7ee1ec4c 2116 if (!(sb->s_flags & MS_RDONLY)) {
955ce5f5 2117 ext4_unlock_group(sb, i);
8a266467 2118 return 0;
7ee1ec4c 2119 }
717d50e4 2120 }
955ce5f5 2121 ext4_unlock_group(sb, i);
ce421581
JS
2122 if (!flexbg_flag)
2123 first_block += EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec 2124 }
bfff6873
LC
2125 if (NULL != first_not_zeroed)
2126 *first_not_zeroed = grp;
ac27a0ec 2127
5dee5437
TT
2128 ext4_free_blocks_count_set(sbi->s_es,
2129 EXT4_C2B(sbi, ext4_count_free_clusters(sb)));
0b8e58a1 2130 sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
ac27a0ec
DK
2131 return 1;
2132}
2133
617ba13b 2134/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
ac27a0ec
DK
2135 * the superblock) which were deleted from all directories, but held open by
2136 * a process at the time of a crash. We walk the list and try to delete these
2137 * inodes at recovery time (only with a read-write filesystem).
2138 *
2139 * In order to keep the orphan inode chain consistent during traversal (in
2140 * case of crash during recovery), we link each inode into the superblock
2141 * orphan list_head and handle it the same way as an inode deletion during
2142 * normal operation (which journals the operations for us).
2143 *
2144 * We only do an iget() and an iput() on each inode, which is very safe if we
2145 * accidentally point at an in-use or already deleted inode. The worst that
2146 * can happen in this case is that we get a "bit already cleared" message from
617ba13b 2147 * ext4_free_inode(). The only reason we would point at a wrong inode is if
ac27a0ec
DK
2148 * e2fsck was run on this filesystem, and it must have already done the orphan
2149 * inode cleanup for us, so we can safely abort without any further action.
2150 */
2b2d6d01
TT
2151static void ext4_orphan_cleanup(struct super_block *sb,
2152 struct ext4_super_block *es)
ac27a0ec
DK
2153{
2154 unsigned int s_flags = sb->s_flags;
2155 int nr_orphans = 0, nr_truncates = 0;
2156#ifdef CONFIG_QUOTA
2157 int i;
2158#endif
2159 if (!es->s_last_orphan) {
2160 jbd_debug(4, "no orphan inodes to clean up\n");
2161 return;
2162 }
2163
a8f48a95 2164 if (bdev_read_only(sb->s_bdev)) {
b31e1552
ES
2165 ext4_msg(sb, KERN_ERR, "write access "
2166 "unavailable, skipping orphan cleanup");
a8f48a95
ES
2167 return;
2168 }
2169
d39195c3
AG
2170 /* Check if feature set would not allow a r/w mount */
2171 if (!ext4_feature_set_ok(sb, 0)) {
2172 ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2173 "unknown ROCOMPAT features");
2174 return;
2175 }
2176
617ba13b 2177 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
c25f9bc6
ES
2178 /* don't clear list on RO mount w/ errors */
2179 if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
ac27a0ec
DK
2180 jbd_debug(1, "Errors on filesystem, "
2181 "clearing orphan list.\n");
c25f9bc6
ES
2182 es->s_last_orphan = 0;
2183 }
ac27a0ec
DK
2184 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2185 return;
2186 }
2187
2188 if (s_flags & MS_RDONLY) {
b31e1552 2189 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
ac27a0ec
DK
2190 sb->s_flags &= ~MS_RDONLY;
2191 }
2192#ifdef CONFIG_QUOTA
2193 /* Needed for iput() to work correctly and not trash data */
2194 sb->s_flags |= MS_ACTIVE;
2195 /* Turn on quotas so that they are updated correctly */
2196 for (i = 0; i < MAXQUOTAS; i++) {
617ba13b
MC
2197 if (EXT4_SB(sb)->s_qf_names[i]) {
2198 int ret = ext4_quota_on_mount(sb, i);
ac27a0ec 2199 if (ret < 0)
b31e1552
ES
2200 ext4_msg(sb, KERN_ERR,
2201 "Cannot turn on journaled "
2202 "quota: error %d", ret);
ac27a0ec
DK
2203 }
2204 }
2205#endif
2206
2207 while (es->s_last_orphan) {
2208 struct inode *inode;
2209
97bd42b9
JB
2210 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2211 if (IS_ERR(inode)) {
ac27a0ec
DK
2212 es->s_last_orphan = 0;
2213 break;
2214 }
2215
617ba13b 2216 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
871a2931 2217 dquot_initialize(inode);
ac27a0ec 2218 if (inode->i_nlink) {
b31e1552
ES
2219 ext4_msg(sb, KERN_DEBUG,
2220 "%s: truncating inode %lu to %lld bytes",
46e665e9 2221 __func__, inode->i_ino, inode->i_size);
e5f8eab8 2222 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
ac27a0ec 2223 inode->i_ino, inode->i_size);
617ba13b 2224 ext4_truncate(inode);
ac27a0ec
DK
2225 nr_truncates++;
2226 } else {
b31e1552
ES
2227 ext4_msg(sb, KERN_DEBUG,
2228 "%s: deleting unreferenced inode %lu",
46e665e9 2229 __func__, inode->i_ino);
ac27a0ec
DK
2230 jbd_debug(2, "deleting unreferenced inode %lu\n",
2231 inode->i_ino);
2232 nr_orphans++;
2233 }
2234 iput(inode); /* The delete magic happens here! */
2235 }
2236
2b2d6d01 2237#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
ac27a0ec
DK
2238
2239 if (nr_orphans)
b31e1552
ES
2240 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2241 PLURAL(nr_orphans));
ac27a0ec 2242 if (nr_truncates)
b31e1552
ES
2243 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2244 PLURAL(nr_truncates));
ac27a0ec
DK
2245#ifdef CONFIG_QUOTA
2246 /* Turn quotas off */
2247 for (i = 0; i < MAXQUOTAS; i++) {
2248 if (sb_dqopt(sb)->files[i])
287a8095 2249 dquot_quota_off(sb, i);
ac27a0ec
DK
2250 }
2251#endif
2252 sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2253}
0b8e58a1 2254
cd2291a4
ES
2255/*
2256 * Maximal extent format file size.
2257 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2258 * extent format containers, within a sector_t, and within i_blocks
2259 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2260 * so that won't be a limiting factor.
2261 *
f17722f9
LC
2262 * However there is other limiting factor. We do store extents in the form
2263 * of starting block and length, hence the resulting length of the extent
2264 * covering maximum file size must fit into on-disk format containers as
2265 * well. Given that length is always by 1 unit bigger than max unit (because
2266 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2267 *
cd2291a4
ES
2268 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2269 */
f287a1a5 2270static loff_t ext4_max_size(int blkbits, int has_huge_files)
cd2291a4
ES
2271{
2272 loff_t res;
2273 loff_t upper_limit = MAX_LFS_FILESIZE;
2274
2275 /* small i_blocks in vfs inode? */
f287a1a5 2276 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
cd2291a4 2277 /*
90c699a9 2278 * CONFIG_LBDAF is not enabled implies the inode
cd2291a4
ES
2279 * i_block represent total blocks in 512 bytes
2280 * 32 == size of vfs inode i_blocks * 8
2281 */
2282 upper_limit = (1LL << 32) - 1;
2283
2284 /* total blocks in file system block size */
2285 upper_limit >>= (blkbits - 9);
2286 upper_limit <<= blkbits;
2287 }
2288
f17722f9
LC
2289 /*
2290 * 32-bit extent-start container, ee_block. We lower the maxbytes
2291 * by one fs block, so ee_len can cover the extent of maximum file
2292 * size
2293 */
2294 res = (1LL << 32) - 1;
cd2291a4 2295 res <<= blkbits;
cd2291a4
ES
2296
2297 /* Sanity check against vm- & vfs- imposed limits */
2298 if (res > upper_limit)
2299 res = upper_limit;
2300
2301 return res;
2302}
ac27a0ec 2303
ac27a0ec 2304/*
cd2291a4 2305 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
0fc1b451
AK
2306 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2307 * We need to be 1 filesystem block less than the 2^48 sector limit.
ac27a0ec 2308 */
f287a1a5 2309static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
ac27a0ec 2310{
617ba13b 2311 loff_t res = EXT4_NDIR_BLOCKS;
0fc1b451
AK
2312 int meta_blocks;
2313 loff_t upper_limit;
0b8e58a1
AD
2314 /* This is calculated to be the largest file size for a dense, block
2315 * mapped file such that the file's total number of 512-byte sectors,
2316 * including data and all indirect blocks, does not exceed (2^48 - 1).
2317 *
2318 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2319 * number of 512-byte sectors of the file.
0fc1b451
AK
2320 */
2321
f287a1a5 2322 if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
0fc1b451 2323 /*
90c699a9 2324 * !has_huge_files or CONFIG_LBDAF not enabled implies that
0b8e58a1
AD
2325 * the inode i_block field represents total file blocks in
2326 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
0fc1b451
AK
2327 */
2328 upper_limit = (1LL << 32) - 1;
2329
2330 /* total blocks in file system block size */
2331 upper_limit >>= (bits - 9);
2332
2333 } else {
8180a562
AK
2334 /*
2335 * We use 48 bit ext4_inode i_blocks
2336 * With EXT4_HUGE_FILE_FL set the i_blocks
2337 * represent total number of blocks in
2338 * file system block size
2339 */
0fc1b451
AK
2340 upper_limit = (1LL << 48) - 1;
2341
0fc1b451
AK
2342 }
2343
2344 /* indirect blocks */
2345 meta_blocks = 1;
2346 /* double indirect blocks */
2347 meta_blocks += 1 + (1LL << (bits-2));
2348 /* tripple indirect blocks */
2349 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2350
2351 upper_limit -= meta_blocks;
2352 upper_limit <<= bits;
ac27a0ec
DK
2353
2354 res += 1LL << (bits-2);
2355 res += 1LL << (2*(bits-2));
2356 res += 1LL << (3*(bits-2));
2357 res <<= bits;
2358 if (res > upper_limit)
2359 res = upper_limit;
0fc1b451
AK
2360
2361 if (res > MAX_LFS_FILESIZE)
2362 res = MAX_LFS_FILESIZE;
2363
ac27a0ec
DK
2364 return res;
2365}
2366
617ba13b 2367static ext4_fsblk_t descriptor_loc(struct super_block *sb,
0b8e58a1 2368 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 2369{
617ba13b 2370 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 2371 ext4_group_t bg, first_meta_bg;
ac27a0ec
DK
2372 int has_super = 0;
2373
2374 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2375
617ba13b 2376 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
ac27a0ec 2377 nr < first_meta_bg)
70bbb3e0 2378 return logical_sb_block + nr + 1;
ac27a0ec 2379 bg = sbi->s_desc_per_block * nr;
617ba13b 2380 if (ext4_bg_has_super(sb, bg))
ac27a0ec 2381 has_super = 1;
0b8e58a1 2382
617ba13b 2383 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
2384}
2385
c9de560d
AT
2386/**
2387 * ext4_get_stripe_size: Get the stripe size.
2388 * @sbi: In memory super block info
2389 *
2390 * If we have specified it via mount option, then
2391 * use the mount option value. If the value specified at mount time is
2392 * greater than the blocks per group use the super block value.
2393 * If the super block value is greater than blocks per group return 0.
2394 * Allocator needs it be less than blocks per group.
2395 *
2396 */
2397static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2398{
2399 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2400 unsigned long stripe_width =
2401 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
3eb08658 2402 int ret;
c9de560d
AT
2403
2404 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
3eb08658
DE
2405 ret = sbi->s_stripe;
2406 else if (stripe_width <= sbi->s_blocks_per_group)
2407 ret = stripe_width;
2408 else if (stride <= sbi->s_blocks_per_group)
2409 ret = stride;
2410 else
2411 ret = 0;
c9de560d 2412
3eb08658
DE
2413 /*
2414 * If the stripe width is 1, this makes no sense and
2415 * we set it to 0 to turn off stripe handling code.
2416 */
2417 if (ret <= 1)
2418 ret = 0;
c9de560d 2419
3eb08658 2420 return ret;
c9de560d 2421}
ac27a0ec 2422
3197ebdb
TT
2423/* sysfs supprt */
2424
2425struct ext4_attr {
2426 struct attribute attr;
2427 ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
60e6679e 2428 ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
3197ebdb
TT
2429 const char *, size_t);
2430 int offset;
2431};
2432
2433static int parse_strtoul(const char *buf,
2434 unsigned long max, unsigned long *value)
2435{
2436 char *endp;
2437
e7d2860b
AGR
2438 *value = simple_strtoul(skip_spaces(buf), &endp, 0);
2439 endp = skip_spaces(endp);
3197ebdb
TT
2440 if (*endp || *value > max)
2441 return -EINVAL;
2442
2443 return 0;
2444}
2445
2446static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2447 struct ext4_sb_info *sbi,
2448 char *buf)
2449{
2450 return snprintf(buf, PAGE_SIZE, "%llu\n",
7b415bf6
AK
2451 (s64) EXT4_C2B(sbi,
2452 percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
3197ebdb
TT
2453}
2454
2455static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2456 struct ext4_sb_info *sbi, char *buf)
2457{
2458 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2459
f613dfcb
TT
2460 if (!sb->s_bdev->bd_part)
2461 return snprintf(buf, PAGE_SIZE, "0\n");
3197ebdb
TT
2462 return snprintf(buf, PAGE_SIZE, "%lu\n",
2463 (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2464 sbi->s_sectors_written_start) >> 1);
2465}
2466
2467static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2468 struct ext4_sb_info *sbi, char *buf)
2469{
2470 struct super_block *sb = sbi->s_buddy_cache->i_sb;
2471
f613dfcb
TT
2472 if (!sb->s_bdev->bd_part)
2473 return snprintf(buf, PAGE_SIZE, "0\n");
3197ebdb 2474 return snprintf(buf, PAGE_SIZE, "%llu\n",
a6b43e38 2475 (unsigned long long)(sbi->s_kbytes_written +
3197ebdb 2476 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
a6b43e38 2477 EXT4_SB(sb)->s_sectors_written_start) >> 1)));
3197ebdb
TT
2478}
2479
2480static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2481 struct ext4_sb_info *sbi,
2482 const char *buf, size_t count)
2483{
2484 unsigned long t;
2485
2486 if (parse_strtoul(buf, 0x40000000, &t))
2487 return -EINVAL;
2488
5dbd571d 2489 if (t && !is_power_of_2(t))
3197ebdb
TT
2490 return -EINVAL;
2491
2492 sbi->s_inode_readahead_blks = t;
2493 return count;
2494}
2495
2496static ssize_t sbi_ui_show(struct ext4_attr *a,
0b8e58a1 2497 struct ext4_sb_info *sbi, char *buf)
3197ebdb
TT
2498{
2499 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2500
2501 return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2502}
2503
2504static ssize_t sbi_ui_store(struct ext4_attr *a,
2505 struct ext4_sb_info *sbi,
2506 const char *buf, size_t count)
2507{
2508 unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
2509 unsigned long t;
2510
2511 if (parse_strtoul(buf, 0xffffffff, &t))
2512 return -EINVAL;
2513 *ui = t;
2514 return count;
2515}
2516
2c0544b2
TT
2517static ssize_t trigger_test_error(struct ext4_attr *a,
2518 struct ext4_sb_info *sbi,
2519 const char *buf, size_t count)
2520{
2521 int len = count;
2522
2523 if (!capable(CAP_SYS_ADMIN))
2524 return -EPERM;
2525
2526 if (len && buf[len-1] == '\n')
2527 len--;
2528
2529 if (len)
2530 ext4_error(sbi->s_sb, "%.*s", len, buf);
2531 return count;
2532}
2533
3197ebdb
TT
2534#define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2535static struct ext4_attr ext4_attr_##_name = { \
2536 .attr = {.name = __stringify(_name), .mode = _mode }, \
2537 .show = _show, \
2538 .store = _store, \
2539 .offset = offsetof(struct ext4_sb_info, _elname), \
2540}
2541#define EXT4_ATTR(name, mode, show, store) \
2542static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2543
857ac889 2544#define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
3197ebdb
TT
2545#define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2546#define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2547#define EXT4_RW_ATTR_SBI_UI(name, elname) \
2548 EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2549#define ATTR_LIST(name) &ext4_attr_##name.attr
2550
2551EXT4_RO_ATTR(delayed_allocation_blocks);
2552EXT4_RO_ATTR(session_write_kbytes);
2553EXT4_RO_ATTR(lifetime_write_kbytes);
2554EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2555 inode_readahead_blks_store, s_inode_readahead_blks);
11013911 2556EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
3197ebdb
TT
2557EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2558EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2559EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2560EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2561EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2562EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
55138e0b 2563EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
67a5da56 2564EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
2c0544b2 2565EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
3197ebdb
TT
2566
2567static struct attribute *ext4_attrs[] = {
2568 ATTR_LIST(delayed_allocation_blocks),
2569 ATTR_LIST(session_write_kbytes),
2570 ATTR_LIST(lifetime_write_kbytes),
2571 ATTR_LIST(inode_readahead_blks),
11013911 2572 ATTR_LIST(inode_goal),
3197ebdb
TT
2573 ATTR_LIST(mb_stats),
2574 ATTR_LIST(mb_max_to_scan),
2575 ATTR_LIST(mb_min_to_scan),
2576 ATTR_LIST(mb_order2_req),
2577 ATTR_LIST(mb_stream_req),
2578 ATTR_LIST(mb_group_prealloc),
55138e0b 2579 ATTR_LIST(max_writeback_mb_bump),
67a5da56 2580 ATTR_LIST(extent_max_zeroout_kb),
2c0544b2 2581 ATTR_LIST(trigger_fs_error),
3197ebdb
TT
2582 NULL,
2583};
2584
857ac889
LC
2585/* Features this copy of ext4 supports */
2586EXT4_INFO_ATTR(lazy_itable_init);
27ee40df 2587EXT4_INFO_ATTR(batched_discard);
5e7bbef1 2588EXT4_INFO_ATTR(meta_bg_resize);
857ac889
LC
2589
2590static struct attribute *ext4_feat_attrs[] = {
2591 ATTR_LIST(lazy_itable_init),
27ee40df 2592 ATTR_LIST(batched_discard),
5e7bbef1 2593 ATTR_LIST(meta_bg_resize),
857ac889
LC
2594 NULL,
2595};
2596
3197ebdb
TT
2597static ssize_t ext4_attr_show(struct kobject *kobj,
2598 struct attribute *attr, char *buf)
2599{
2600 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2601 s_kobj);
2602 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2603
2604 return a->show ? a->show(a, sbi, buf) : 0;
2605}
2606
2607static ssize_t ext4_attr_store(struct kobject *kobj,
2608 struct attribute *attr,
2609 const char *buf, size_t len)
2610{
2611 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2612 s_kobj);
2613 struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2614
2615 return a->store ? a->store(a, sbi, buf, len) : 0;
2616}
2617
2618static void ext4_sb_release(struct kobject *kobj)
2619{
2620 struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2621 s_kobj);
2622 complete(&sbi->s_kobj_unregister);
2623}
2624
52cf25d0 2625static const struct sysfs_ops ext4_attr_ops = {
3197ebdb
TT
2626 .show = ext4_attr_show,
2627 .store = ext4_attr_store,
2628};
2629
2630static struct kobj_type ext4_ktype = {
2631 .default_attrs = ext4_attrs,
2632 .sysfs_ops = &ext4_attr_ops,
2633 .release = ext4_sb_release,
2634};
2635
857ac889
LC
2636static void ext4_feat_release(struct kobject *kobj)
2637{
2638 complete(&ext4_feat->f_kobj_unregister);
2639}
2640
2641static struct kobj_type ext4_feat_ktype = {
2642 .default_attrs = ext4_feat_attrs,
2643 .sysfs_ops = &ext4_attr_ops,
2644 .release = ext4_feat_release,
2645};
2646
a13fb1a4
ES
2647/*
2648 * Check whether this filesystem can be mounted based on
2649 * the features present and the RDONLY/RDWR mount requested.
2650 * Returns 1 if this filesystem can be mounted as requested,
2651 * 0 if it cannot be.
2652 */
2653static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2654{
2655 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2656 ext4_msg(sb, KERN_ERR,
2657 "Couldn't mount because of "
2658 "unsupported optional features (%x)",
2659 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2660 ~EXT4_FEATURE_INCOMPAT_SUPP));
2661 return 0;
2662 }
2663
2664 if (readonly)
2665 return 1;
2666
2667 /* Check that feature set is OK for a read-write mount */
2668 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2669 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2670 "unsupported optional features (%x)",
2671 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2672 ~EXT4_FEATURE_RO_COMPAT_SUPP));
2673 return 0;
2674 }
2675 /*
2676 * Large file size enabled file system can only be mounted
2677 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2678 */
2679 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2680 if (sizeof(blkcnt_t) < sizeof(u64)) {
2681 ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2682 "cannot be mounted RDWR without "
2683 "CONFIG_LBDAF");
2684 return 0;
2685 }
2686 }
bab08ab9
TT
2687 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2688 !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2689 ext4_msg(sb, KERN_ERR,
2690 "Can't support bigalloc feature without "
2691 "extents feature\n");
2692 return 0;
2693 }
7c319d32
AK
2694
2695#ifndef CONFIG_QUOTA
2696 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
2697 !readonly) {
2698 ext4_msg(sb, KERN_ERR,
2699 "Filesystem with quota feature cannot be mounted RDWR "
2700 "without CONFIG_QUOTA");
2701 return 0;
2702 }
2703#endif /* CONFIG_QUOTA */
a13fb1a4
ES
2704 return 1;
2705}
2706
66e61a9e
TT
2707/*
2708 * This function is called once a day if we have errors logged
2709 * on the file system
2710 */
2711static void print_daily_error_info(unsigned long arg)
2712{
2713 struct super_block *sb = (struct super_block *) arg;
2714 struct ext4_sb_info *sbi;
2715 struct ext4_super_block *es;
2716
2717 sbi = EXT4_SB(sb);
2718 es = sbi->s_es;
2719
2720 if (es->s_error_count)
2721 ext4_msg(sb, KERN_NOTICE, "error count: %u",
2722 le32_to_cpu(es->s_error_count));
2723 if (es->s_first_error_time) {
2724 printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
2725 sb->s_id, le32_to_cpu(es->s_first_error_time),
2726 (int) sizeof(es->s_first_error_func),
2727 es->s_first_error_func,
2728 le32_to_cpu(es->s_first_error_line));
2729 if (es->s_first_error_ino)
2730 printk(": inode %u",
2731 le32_to_cpu(es->s_first_error_ino));
2732 if (es->s_first_error_block)
2733 printk(": block %llu", (unsigned long long)
2734 le64_to_cpu(es->s_first_error_block));
2735 printk("\n");
2736 }
2737 if (es->s_last_error_time) {
2738 printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
2739 sb->s_id, le32_to_cpu(es->s_last_error_time),
2740 (int) sizeof(es->s_last_error_func),
2741 es->s_last_error_func,
2742 le32_to_cpu(es->s_last_error_line));
2743 if (es->s_last_error_ino)
2744 printk(": inode %u",
2745 le32_to_cpu(es->s_last_error_ino));
2746 if (es->s_last_error_block)
2747 printk(": block %llu", (unsigned long long)
2748 le64_to_cpu(es->s_last_error_block));
2749 printk("\n");
2750 }
2751 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
2752}
2753
bfff6873
LC
2754/* Find next suitable group and run ext4_init_inode_table */
2755static int ext4_run_li_request(struct ext4_li_request *elr)
2756{
2757 struct ext4_group_desc *gdp = NULL;
2758 ext4_group_t group, ngroups;
2759 struct super_block *sb;
2760 unsigned long timeout = 0;
2761 int ret = 0;
2762
2763 sb = elr->lr_super;
2764 ngroups = EXT4_SB(sb)->s_groups_count;
2765
8e8ad8a5 2766 sb_start_write(sb);
bfff6873
LC
2767 for (group = elr->lr_next_group; group < ngroups; group++) {
2768 gdp = ext4_get_group_desc(sb, group, NULL);
2769 if (!gdp) {
2770 ret = 1;
2771 break;
2772 }
2773
2774 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2775 break;
2776 }
2777
2778 if (group == ngroups)
2779 ret = 1;
2780
2781 if (!ret) {
2782 timeout = jiffies;
2783 ret = ext4_init_inode_table(sb, group,
2784 elr->lr_timeout ? 0 : 1);
2785 if (elr->lr_timeout == 0) {
51ce6511
LC
2786 timeout = (jiffies - timeout) *
2787 elr->lr_sbi->s_li_wait_mult;
bfff6873
LC
2788 elr->lr_timeout = timeout;
2789 }
2790 elr->lr_next_sched = jiffies + elr->lr_timeout;
2791 elr->lr_next_group = group + 1;
2792 }
8e8ad8a5 2793 sb_end_write(sb);
bfff6873
LC
2794
2795 return ret;
2796}
2797
2798/*
2799 * Remove lr_request from the list_request and free the
4ed5c033 2800 * request structure. Should be called with li_list_mtx held
bfff6873
LC
2801 */
2802static void ext4_remove_li_request(struct ext4_li_request *elr)
2803{
2804 struct ext4_sb_info *sbi;
2805
2806 if (!elr)
2807 return;
2808
2809 sbi = elr->lr_sbi;
2810
2811 list_del(&elr->lr_request);
2812 sbi->s_li_request = NULL;
2813 kfree(elr);
2814}
2815
2816static void ext4_unregister_li_request(struct super_block *sb)
2817{
1bb933fb
LC
2818 mutex_lock(&ext4_li_mtx);
2819 if (!ext4_li_info) {
2820 mutex_unlock(&ext4_li_mtx);
bfff6873 2821 return;
1bb933fb 2822 }
bfff6873
LC
2823
2824 mutex_lock(&ext4_li_info->li_list_mtx);
1bb933fb 2825 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
bfff6873 2826 mutex_unlock(&ext4_li_info->li_list_mtx);
1bb933fb 2827 mutex_unlock(&ext4_li_mtx);
bfff6873
LC
2828}
2829
8f1f7453
ES
2830static struct task_struct *ext4_lazyinit_task;
2831
bfff6873
LC
2832/*
2833 * This is the function where ext4lazyinit thread lives. It walks
2834 * through the request list searching for next scheduled filesystem.
2835 * When such a fs is found, run the lazy initialization request
2836 * (ext4_rn_li_request) and keep track of the time spend in this
2837 * function. Based on that time we compute next schedule time of
2838 * the request. When walking through the list is complete, compute
2839 * next waking time and put itself into sleep.
2840 */
2841static int ext4_lazyinit_thread(void *arg)
2842{
2843 struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
2844 struct list_head *pos, *n;
2845 struct ext4_li_request *elr;
4ed5c033 2846 unsigned long next_wakeup, cur;
bfff6873
LC
2847
2848 BUG_ON(NULL == eli);
2849
bfff6873
LC
2850cont_thread:
2851 while (true) {
2852 next_wakeup = MAX_JIFFY_OFFSET;
2853
2854 mutex_lock(&eli->li_list_mtx);
2855 if (list_empty(&eli->li_request_list)) {
2856 mutex_unlock(&eli->li_list_mtx);
2857 goto exit_thread;
2858 }
2859
2860 list_for_each_safe(pos, n, &eli->li_request_list) {
2861 elr = list_entry(pos, struct ext4_li_request,
2862 lr_request);
2863
b2c78cd0
TT
2864 if (time_after_eq(jiffies, elr->lr_next_sched)) {
2865 if (ext4_run_li_request(elr) != 0) {
2866 /* error, remove the lazy_init job */
2867 ext4_remove_li_request(elr);
2868 continue;
2869 }
bfff6873
LC
2870 }
2871
2872 if (time_before(elr->lr_next_sched, next_wakeup))
2873 next_wakeup = elr->lr_next_sched;
2874 }
2875 mutex_unlock(&eli->li_list_mtx);
2876
a0acae0e 2877 try_to_freeze();
bfff6873 2878
4ed5c033
LC
2879 cur = jiffies;
2880 if ((time_after_eq(cur, next_wakeup)) ||
f4245bd4 2881 (MAX_JIFFY_OFFSET == next_wakeup)) {
bfff6873
LC
2882 cond_resched();
2883 continue;
2884 }
2885
4ed5c033
LC
2886 schedule_timeout_interruptible(next_wakeup - cur);
2887
8f1f7453
ES
2888 if (kthread_should_stop()) {
2889 ext4_clear_request_list();
2890 goto exit_thread;
2891 }
bfff6873
LC
2892 }
2893
2894exit_thread:
2895 /*
2896 * It looks like the request list is empty, but we need
2897 * to check it under the li_list_mtx lock, to prevent any
2898 * additions into it, and of course we should lock ext4_li_mtx
2899 * to atomically free the list and ext4_li_info, because at
2900 * this point another ext4 filesystem could be registering
2901 * new one.
2902 */
2903 mutex_lock(&ext4_li_mtx);
2904 mutex_lock(&eli->li_list_mtx);
2905 if (!list_empty(&eli->li_request_list)) {
2906 mutex_unlock(&eli->li_list_mtx);
2907 mutex_unlock(&ext4_li_mtx);
2908 goto cont_thread;
2909 }
2910 mutex_unlock(&eli->li_list_mtx);
bfff6873
LC
2911 kfree(ext4_li_info);
2912 ext4_li_info = NULL;
2913 mutex_unlock(&ext4_li_mtx);
2914
2915 return 0;
2916}
2917
2918static void ext4_clear_request_list(void)
2919{
2920 struct list_head *pos, *n;
2921 struct ext4_li_request *elr;
2922
2923 mutex_lock(&ext4_li_info->li_list_mtx);
bfff6873
LC
2924 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
2925 elr = list_entry(pos, struct ext4_li_request,
2926 lr_request);
2927 ext4_remove_li_request(elr);
2928 }
2929 mutex_unlock(&ext4_li_info->li_list_mtx);
2930}
2931
2932static int ext4_run_lazyinit_thread(void)
2933{
8f1f7453
ES
2934 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
2935 ext4_li_info, "ext4lazyinit");
2936 if (IS_ERR(ext4_lazyinit_task)) {
2937 int err = PTR_ERR(ext4_lazyinit_task);
bfff6873 2938 ext4_clear_request_list();
bfff6873
LC
2939 kfree(ext4_li_info);
2940 ext4_li_info = NULL;
92b97816 2941 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
bfff6873
LC
2942 "initialization thread\n",
2943 err);
2944 return err;
2945 }
2946 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
bfff6873
LC
2947 return 0;
2948}
2949
2950/*
2951 * Check whether it make sense to run itable init. thread or not.
2952 * If there is at least one uninitialized inode table, return
2953 * corresponding group number, else the loop goes through all
2954 * groups and return total number of groups.
2955 */
2956static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
2957{
2958 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
2959 struct ext4_group_desc *gdp = NULL;
2960
2961 for (group = 0; group < ngroups; group++) {
2962 gdp = ext4_get_group_desc(sb, group, NULL);
2963 if (!gdp)
2964 continue;
2965
2966 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2967 break;
2968 }
2969
2970 return group;
2971}
2972
2973static int ext4_li_info_new(void)
2974{
2975 struct ext4_lazy_init *eli = NULL;
2976
2977 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
2978 if (!eli)
2979 return -ENOMEM;
2980
bfff6873
LC
2981 INIT_LIST_HEAD(&eli->li_request_list);
2982 mutex_init(&eli->li_list_mtx);
2983
bfff6873
LC
2984 eli->li_state |= EXT4_LAZYINIT_QUIT;
2985
2986 ext4_li_info = eli;
2987
2988 return 0;
2989}
2990
2991static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
2992 ext4_group_t start)
2993{
2994 struct ext4_sb_info *sbi = EXT4_SB(sb);
2995 struct ext4_li_request *elr;
2996 unsigned long rnd;
2997
2998 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
2999 if (!elr)
3000 return NULL;
3001
3002 elr->lr_super = sb;
3003 elr->lr_sbi = sbi;
3004 elr->lr_next_group = start;
3005
3006 /*
3007 * Randomize first schedule time of the request to
3008 * spread the inode table initialization requests
3009 * better.
3010 */
3011 get_random_bytes(&rnd, sizeof(rnd));
3012 elr->lr_next_sched = jiffies + (unsigned long)rnd %
3013 (EXT4_DEF_LI_MAX_START_DELAY * HZ);
3014
3015 return elr;
3016}
3017
3018static int ext4_register_li_request(struct super_block *sb,
3019 ext4_group_t first_not_zeroed)
3020{
3021 struct ext4_sb_info *sbi = EXT4_SB(sb);
3022 struct ext4_li_request *elr;
3023 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
6c5a6cb9 3024 int ret = 0;
bfff6873 3025
51ce6511
LC
3026 if (sbi->s_li_request != NULL) {
3027 /*
3028 * Reset timeout so it can be computed again, because
3029 * s_li_wait_mult might have changed.
3030 */
3031 sbi->s_li_request->lr_timeout = 0;
beed5ecb 3032 return 0;
51ce6511 3033 }
bfff6873
LC
3034
3035 if (first_not_zeroed == ngroups ||
3036 (sb->s_flags & MS_RDONLY) ||
55ff3840 3037 !test_opt(sb, INIT_INODE_TABLE))
beed5ecb 3038 return 0;
bfff6873
LC
3039
3040 elr = ext4_li_request_new(sb, first_not_zeroed);
beed5ecb
NK
3041 if (!elr)
3042 return -ENOMEM;
bfff6873
LC
3043
3044 mutex_lock(&ext4_li_mtx);
3045
3046 if (NULL == ext4_li_info) {
3047 ret = ext4_li_info_new();
3048 if (ret)
3049 goto out;
3050 }
3051
3052 mutex_lock(&ext4_li_info->li_list_mtx);
3053 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3054 mutex_unlock(&ext4_li_info->li_list_mtx);
3055
3056 sbi->s_li_request = elr;
46e4690b
TM
3057 /*
3058 * set elr to NULL here since it has been inserted to
3059 * the request_list and the removal and free of it is
3060 * handled by ext4_clear_request_list from now on.
3061 */
3062 elr = NULL;
bfff6873
LC
3063
3064 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3065 ret = ext4_run_lazyinit_thread();
3066 if (ret)
3067 goto out;
3068 }
bfff6873 3069out:
beed5ecb
NK
3070 mutex_unlock(&ext4_li_mtx);
3071 if (ret)
bfff6873 3072 kfree(elr);
bfff6873
LC
3073 return ret;
3074}
3075
3076/*
3077 * We do not need to lock anything since this is called on
3078 * module unload.
3079 */
3080static void ext4_destroy_lazyinit_thread(void)
3081{
3082 /*
3083 * If thread exited earlier
3084 * there's nothing to be done.
3085 */
8f1f7453 3086 if (!ext4_li_info || !ext4_lazyinit_task)
bfff6873
LC
3087 return;
3088
8f1f7453 3089 kthread_stop(ext4_lazyinit_task);
bfff6873
LC
3090}
3091
25ed6e8a
DW
3092static int set_journal_csum_feature_set(struct super_block *sb)
3093{
3094 int ret = 1;
3095 int compat, incompat;
3096 struct ext4_sb_info *sbi = EXT4_SB(sb);
3097
3098 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3099 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3100 /* journal checksum v2 */
3101 compat = 0;
3102 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V2;
3103 } else {
3104 /* journal checksum v1 */
3105 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3106 incompat = 0;
3107 }
3108
3109 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3110 ret = jbd2_journal_set_features(sbi->s_journal,
3111 compat, 0,
3112 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3113 incompat);
3114 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3115 ret = jbd2_journal_set_features(sbi->s_journal,
3116 compat, 0,
3117 incompat);
3118 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3119 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3120 } else {
3121 jbd2_journal_clear_features(sbi->s_journal,
3122 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3123 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3124 JBD2_FEATURE_INCOMPAT_CSUM_V2);
3125 }
3126
3127 return ret;
3128}
3129
952fc18e
TT
3130/*
3131 * Note: calculating the overhead so we can be compatible with
3132 * historical BSD practice is quite difficult in the face of
3133 * clusters/bigalloc. This is because multiple metadata blocks from
3134 * different block group can end up in the same allocation cluster.
3135 * Calculating the exact overhead in the face of clustered allocation
3136 * requires either O(all block bitmaps) in memory or O(number of block
3137 * groups**2) in time. We will still calculate the superblock for
3138 * older file systems --- and if we come across with a bigalloc file
3139 * system with zero in s_overhead_clusters the estimate will be close to
3140 * correct especially for very large cluster sizes --- but for newer
3141 * file systems, it's better to calculate this figure once at mkfs
3142 * time, and store it in the superblock. If the superblock value is
3143 * present (even for non-bigalloc file systems), we will use it.
3144 */
3145static int count_overhead(struct super_block *sb, ext4_group_t grp,
3146 char *buf)
3147{
3148 struct ext4_sb_info *sbi = EXT4_SB(sb);
3149 struct ext4_group_desc *gdp;
3150 ext4_fsblk_t first_block, last_block, b;
3151 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3152 int s, j, count = 0;
3153
0548bbb8
TT
3154 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC))
3155 return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3156 sbi->s_itb_per_group + 2);
3157
952fc18e
TT
3158 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3159 (grp * EXT4_BLOCKS_PER_GROUP(sb));
3160 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3161 for (i = 0; i < ngroups; i++) {
3162 gdp = ext4_get_group_desc(sb, i, NULL);
3163 b = ext4_block_bitmap(sb, gdp);
3164 if (b >= first_block && b <= last_block) {
3165 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3166 count++;
3167 }
3168 b = ext4_inode_bitmap(sb, gdp);
3169 if (b >= first_block && b <= last_block) {
3170 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3171 count++;
3172 }
3173 b = ext4_inode_table(sb, gdp);
3174 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3175 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3176 int c = EXT4_B2C(sbi, b - first_block);
3177 ext4_set_bit(c, buf);
3178 count++;
3179 }
3180 if (i != grp)
3181 continue;
3182 s = 0;
3183 if (ext4_bg_has_super(sb, grp)) {
3184 ext4_set_bit(s++, buf);
3185 count++;
3186 }
3187 for (j = ext4_bg_num_gdb(sb, grp); j > 0; j--) {
3188 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3189 count++;
3190 }
3191 }
3192 if (!count)
3193 return 0;
3194 return EXT4_CLUSTERS_PER_GROUP(sb) -
3195 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3196}
3197
3198/*
3199 * Compute the overhead and stash it in sbi->s_overhead
3200 */
3201int ext4_calculate_overhead(struct super_block *sb)
3202{
3203 struct ext4_sb_info *sbi = EXT4_SB(sb);
3204 struct ext4_super_block *es = sbi->s_es;
3205 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3206 ext4_fsblk_t overhead = 0;
3207 char *buf = (char *) get_zeroed_page(GFP_KERNEL);
3208
952fc18e
TT
3209 if (!buf)
3210 return -ENOMEM;
3211
3212 /*
3213 * Compute the overhead (FS structures). This is constant
3214 * for a given filesystem unless the number of block groups
3215 * changes so we cache the previous value until it does.
3216 */
3217
3218 /*
3219 * All of the blocks before first_data_block are overhead
3220 */
3221 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3222
3223 /*
3224 * Add the overhead found in each block group
3225 */
3226 for (i = 0; i < ngroups; i++) {
3227 int blks;
3228
3229 blks = count_overhead(sb, i, buf);
3230 overhead += blks;
3231 if (blks)
3232 memset(buf, 0, PAGE_SIZE);
3233 cond_resched();
3234 }
3235 sbi->s_overhead = overhead;
3236 smp_wmb();
3237 free_page((unsigned long) buf);
3238 return 0;
3239}
3240
2b2d6d01 3241static int ext4_fill_super(struct super_block *sb, void *data, int silent)
ac27a0ec 3242{
d4c402d9 3243 char *orig_data = kstrdup(data, GFP_KERNEL);
2b2d6d01 3244 struct buffer_head *bh;
617ba13b
MC
3245 struct ext4_super_block *es = NULL;
3246 struct ext4_sb_info *sbi;
3247 ext4_fsblk_t block;
3248 ext4_fsblk_t sb_block = get_sb_block(&data);
70bbb3e0 3249 ext4_fsblk_t logical_sb_block;
ac27a0ec 3250 unsigned long offset = 0;
ac27a0ec
DK
3251 unsigned long journal_devnum = 0;
3252 unsigned long def_mount_opts;
3253 struct inode *root;
9f6200bb 3254 char *cp;
0390131b 3255 const char *descr;
dcc7dae3 3256 int ret = -ENOMEM;
281b5995 3257 int blocksize, clustersize;
4ec11028
TT
3258 unsigned int db_count;
3259 unsigned int i;
281b5995 3260 int needs_recovery, has_huge_files, has_bigalloc;
bd81d8ee 3261 __u64 blocks_count;
07aa2ea1 3262 int err = 0;
b3881f74 3263 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
bfff6873 3264 ext4_group_t first_not_zeroed;
ac27a0ec
DK
3265
3266 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3267 if (!sbi)
dcc7dae3 3268 goto out_free_orig;
705895b6
PE
3269
3270 sbi->s_blockgroup_lock =
3271 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3272 if (!sbi->s_blockgroup_lock) {
3273 kfree(sbi);
dcc7dae3 3274 goto out_free_orig;
705895b6 3275 }
ac27a0ec 3276 sb->s_fs_info = sbi;
2c0544b2 3277 sbi->s_sb = sb;
240799cd 3278 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
d9c9bef1 3279 sbi->s_sb_block = sb_block;
f613dfcb
TT
3280 if (sb->s_bdev->bd_part)
3281 sbi->s_sectors_written_start =
3282 part_stat_read(sb->s_bdev->bd_part, sectors[1]);
ac27a0ec 3283
9f6200bb
TT
3284 /* Cleanup superblock name */
3285 for (cp = sb->s_id; (cp = strchr(cp, '/'));)
3286 *cp = '!';
3287
07aa2ea1 3288 /* -EINVAL is default */
dcc7dae3 3289 ret = -EINVAL;
617ba13b 3290 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 3291 if (!blocksize) {
b31e1552 3292 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
ac27a0ec
DK
3293 goto out_fail;
3294 }
3295
3296 /*
617ba13b 3297 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
3298 * block sizes. We need to calculate the offset from buffer start.
3299 */
617ba13b 3300 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
70bbb3e0
AM
3301 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3302 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 3303 } else {
70bbb3e0 3304 logical_sb_block = sb_block;
ac27a0ec
DK
3305 }
3306
70bbb3e0 3307 if (!(bh = sb_bread(sb, logical_sb_block))) {
b31e1552 3308 ext4_msg(sb, KERN_ERR, "unable to read superblock");
ac27a0ec
DK
3309 goto out_fail;
3310 }
3311 /*
3312 * Note: s_es must be initialized as soon as possible because
617ba13b 3313 * some ext4 macro-instructions depend on its value
ac27a0ec 3314 */
2716b802 3315 es = (struct ext4_super_block *) (bh->b_data + offset);
ac27a0ec
DK
3316 sbi->s_es = es;
3317 sb->s_magic = le16_to_cpu(es->s_magic);
617ba13b
MC
3318 if (sb->s_magic != EXT4_SUPER_MAGIC)
3319 goto cantfind_ext4;
afc32f7e 3320 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
ac27a0ec 3321
feb0ab32
DW
3322 /* Warn if metadata_csum and gdt_csum are both set. */
3323 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3324 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
3325 EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3326 ext4_warning(sb, KERN_INFO "metadata_csum and uninit_bg are "
3327 "redundant flags; please run fsck.");
3328
d25425f8
DW
3329 /* Check for a known checksum algorithm */
3330 if (!ext4_verify_csum_type(sb, es)) {
3331 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3332 "unknown checksum algorithm.");
3333 silent = 1;
3334 goto cantfind_ext4;
3335 }
3336
0441984a
DW
3337 /* Load the checksum driver */
3338 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3339 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3340 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3341 if (IS_ERR(sbi->s_chksum_driver)) {
3342 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3343 ret = PTR_ERR(sbi->s_chksum_driver);
3344 sbi->s_chksum_driver = NULL;
3345 goto failed_mount;
3346 }
3347 }
3348
a9c47317
DW
3349 /* Check superblock checksum */
3350 if (!ext4_superblock_csum_verify(sb, es)) {
3351 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3352 "invalid superblock checksum. Run e2fsck?");
3353 silent = 1;
3354 goto cantfind_ext4;
3355 }
3356
3357 /* Precompute checksum seed for all metadata */
3358 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3359 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
3360 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3361 sizeof(es->s_uuid));
3362
ac27a0ec
DK
3363 /* Set defaults before we parse the mount options */
3364 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
fd8c37ec 3365 set_opt(sb, INIT_INODE_TABLE);
617ba13b 3366 if (def_mount_opts & EXT4_DEFM_DEBUG)
fd8c37ec 3367 set_opt(sb, DEBUG);
87f26807 3368 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
fd8c37ec 3369 set_opt(sb, GRPID);
617ba13b 3370 if (def_mount_opts & EXT4_DEFM_UID16)
fd8c37ec 3371 set_opt(sb, NO_UID32);
ea663336 3372 /* xattr user namespace & acls are now defaulted on */
03010a33 3373#ifdef CONFIG_EXT4_FS_XATTR
ea663336 3374 set_opt(sb, XATTR_USER);
2e7842b8 3375#endif
03010a33 3376#ifdef CONFIG_EXT4_FS_POSIX_ACL
ea663336 3377 set_opt(sb, POSIX_ACL);
2e7842b8 3378#endif
6fd7a467 3379 set_opt(sb, MBLK_IO_SUBMIT);
617ba13b 3380 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
fd8c37ec 3381 set_opt(sb, JOURNAL_DATA);
617ba13b 3382 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
fd8c37ec 3383 set_opt(sb, ORDERED_DATA);
617ba13b 3384 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
fd8c37ec 3385 set_opt(sb, WRITEBACK_DATA);
617ba13b
MC
3386
3387 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
fd8c37ec 3388 set_opt(sb, ERRORS_PANIC);
bb4f397a 3389 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
fd8c37ec 3390 set_opt(sb, ERRORS_CONT);
bb4f397a 3391 else
fd8c37ec 3392 set_opt(sb, ERRORS_RO);
8b67f04a 3393 if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
fd8c37ec 3394 set_opt(sb, BLOCK_VALIDITY);
8b67f04a 3395 if (def_mount_opts & EXT4_DEFM_DISCARD)
fd8c37ec 3396 set_opt(sb, DISCARD);
ac27a0ec 3397
08cefc7a
EB
3398 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
3399 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
30773840
TT
3400 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
3401 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
3402 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
ac27a0ec 3403
8b67f04a 3404 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
fd8c37ec 3405 set_opt(sb, BARRIER);
ac27a0ec 3406
dd919b98
AK
3407 /*
3408 * enable delayed allocation by default
3409 * Use -o nodelalloc to turn it off
3410 */
bc0b75f7 3411 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
8b67f04a 3412 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
fd8c37ec 3413 set_opt(sb, DELALLOC);
dd919b98 3414
51ce6511
LC
3415 /*
3416 * set default s_li_wait_mult for lazyinit, for the case there is
3417 * no mount option specified.
3418 */
3419 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
3420
8b67f04a 3421 if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
661aa520 3422 &journal_devnum, &journal_ioprio, 0)) {
8b67f04a
TT
3423 ext4_msg(sb, KERN_WARNING,
3424 "failed to parse options in superblock: %s",
3425 sbi->s_es->s_mount_opts);
3426 }
5a916be1 3427 sbi->s_def_mount_opt = sbi->s_mount_opt;
b3881f74 3428 if (!parse_options((char *) data, sb, &journal_devnum,
661aa520 3429 &journal_ioprio, 0))
ac27a0ec
DK
3430 goto failed_mount;
3431
56889787
TT
3432 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3433 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
3434 "with data=journal disables delayed "
3435 "allocation and O_DIRECT support!\n");
3436 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
3437 ext4_msg(sb, KERN_ERR, "can't mount with "
3438 "both data=journal and delalloc");
3439 goto failed_mount;
3440 }
3441 if (test_opt(sb, DIOREAD_NOLOCK)) {
3442 ext4_msg(sb, KERN_ERR, "can't mount with "
3443 "both data=journal and delalloc");
3444 goto failed_mount;
3445 }
3446 if (test_opt(sb, DELALLOC))
3447 clear_opt(sb, DELALLOC);
3448 }
3449
3450 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3451 if (test_opt(sb, DIOREAD_NOLOCK)) {
3452 if (blocksize < PAGE_SIZE) {
3453 ext4_msg(sb, KERN_ERR, "can't mount with "
3454 "dioread_nolock if block size != PAGE_SIZE");
3455 goto failed_mount;
3456 }
3457 }
3458
ac27a0ec 3459 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
482a7425 3460 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec 3461
617ba13b
MC
3462 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3463 (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3464 EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3465 EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
b31e1552
ES
3466 ext4_msg(sb, KERN_WARNING,
3467 "feature flags set on rev 0 fs, "
3468 "running e2fsck is recommended");
469108ff 3469
2035e776
TT
3470 if (IS_EXT2_SB(sb)) {
3471 if (ext2_feature_set_ok(sb))
3472 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
3473 "using the ext4 subsystem");
3474 else {
3475 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
3476 "to feature incompatibilities");
3477 goto failed_mount;
3478 }
3479 }
3480
3481 if (IS_EXT3_SB(sb)) {
3482 if (ext3_feature_set_ok(sb))
3483 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
3484 "using the ext4 subsystem");
3485 else {
3486 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
3487 "to feature incompatibilities");
3488 goto failed_mount;
3489 }
3490 }
3491
ac27a0ec
DK
3492 /*
3493 * Check feature flags regardless of the revision level, since we
3494 * previously didn't change the revision level when setting the flags,
3495 * so there is a chance incompat flags are set on a rev 0 filesystem.
3496 */
a13fb1a4 3497 if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
ac27a0ec 3498 goto failed_mount;
a13fb1a4 3499
617ba13b
MC
3500 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3501 blocksize > EXT4_MAX_BLOCK_SIZE) {
b31e1552
ES
3502 ext4_msg(sb, KERN_ERR,
3503 "Unsupported filesystem blocksize %d", blocksize);
ac27a0ec
DK
3504 goto failed_mount;
3505 }
3506
ac27a0ec 3507 if (sb->s_blocksize != blocksize) {
ce40733c
AK
3508 /* Validate the filesystem blocksize */
3509 if (!sb_set_blocksize(sb, blocksize)) {
b31e1552 3510 ext4_msg(sb, KERN_ERR, "bad block size %d",
ce40733c 3511 blocksize);
ac27a0ec
DK
3512 goto failed_mount;
3513 }
3514
2b2d6d01 3515 brelse(bh);
70bbb3e0
AM
3516 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3517 offset = do_div(logical_sb_block, blocksize);
3518 bh = sb_bread(sb, logical_sb_block);
ac27a0ec 3519 if (!bh) {
b31e1552
ES
3520 ext4_msg(sb, KERN_ERR,
3521 "Can't read superblock on 2nd try");
ac27a0ec
DK
3522 goto failed_mount;
3523 }
2716b802 3524 es = (struct ext4_super_block *)(bh->b_data + offset);
ac27a0ec 3525 sbi->s_es = es;
617ba13b 3526 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
b31e1552
ES
3527 ext4_msg(sb, KERN_ERR,
3528 "Magic mismatch, very weird!");
ac27a0ec
DK
3529 goto failed_mount;
3530 }
3531 }
3532
a13fb1a4
ES
3533 has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3534 EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
f287a1a5
TT
3535 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3536 has_huge_files);
3537 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
ac27a0ec 3538
617ba13b
MC
3539 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3540 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3541 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
ac27a0ec
DK
3542 } else {
3543 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3544 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
617ba13b 3545 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
1330593e 3546 (!is_power_of_2(sbi->s_inode_size)) ||
ac27a0ec 3547 (sbi->s_inode_size > blocksize)) {
b31e1552
ES
3548 ext4_msg(sb, KERN_ERR,
3549 "unsupported inode size: %d",
2b2d6d01 3550 sbi->s_inode_size);
ac27a0ec
DK
3551 goto failed_mount;
3552 }
ef7f3835
KS
3553 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3554 sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
ac27a0ec 3555 }
0b8e58a1 3556
0d1ee42f
AR
3557 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
3558 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
8fadc143 3559 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
0d1ee42f 3560 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
d8ea6cf8 3561 !is_power_of_2(sbi->s_desc_size)) {
b31e1552
ES
3562 ext4_msg(sb, KERN_ERR,
3563 "unsupported descriptor size %lu",
0d1ee42f
AR
3564 sbi->s_desc_size);
3565 goto failed_mount;
3566 }
3567 } else
3568 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
0b8e58a1 3569
ac27a0ec 3570 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
ac27a0ec 3571 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
b47b6f38 3572 if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
617ba13b 3573 goto cantfind_ext4;
0b8e58a1 3574
617ba13b 3575 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
ac27a0ec 3576 if (sbi->s_inodes_per_block == 0)
617ba13b 3577 goto cantfind_ext4;
ac27a0ec
DK
3578 sbi->s_itb_per_group = sbi->s_inodes_per_group /
3579 sbi->s_inodes_per_block;
0d1ee42f 3580 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
ac27a0ec
DK
3581 sbi->s_sbh = bh;
3582 sbi->s_mount_state = le16_to_cpu(es->s_state);
e57aa839
FW
3583 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3584 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
0b8e58a1 3585
2b2d6d01 3586 for (i = 0; i < 4; i++)
ac27a0ec
DK
3587 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3588 sbi->s_def_hash_version = es->s_def_hash_version;
f99b2589
TT
3589 i = le32_to_cpu(es->s_flags);
3590 if (i & EXT2_FLAGS_UNSIGNED_HASH)
3591 sbi->s_hash_unsigned = 3;
3592 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3593#ifdef __CHAR_UNSIGNED__
3594 es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3595 sbi->s_hash_unsigned = 3;
3596#else
3597 es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3598#endif
f99b2589 3599 }
ac27a0ec 3600
281b5995
TT
3601 /* Handle clustersize */
3602 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3603 has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3604 EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3605 if (has_bigalloc) {
3606 if (clustersize < blocksize) {
3607 ext4_msg(sb, KERN_ERR,
3608 "cluster size (%d) smaller than "
3609 "block size (%d)", clustersize, blocksize);
3610 goto failed_mount;
3611 }
3612 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3613 le32_to_cpu(es->s_log_block_size);
3614 sbi->s_clusters_per_group =
3615 le32_to_cpu(es->s_clusters_per_group);
3616 if (sbi->s_clusters_per_group > blocksize * 8) {
3617 ext4_msg(sb, KERN_ERR,
3618 "#clusters per group too big: %lu",
3619 sbi->s_clusters_per_group);
3620 goto failed_mount;
3621 }
3622 if (sbi->s_blocks_per_group !=
3623 (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3624 ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3625 "clusters per group (%lu) inconsistent",
3626 sbi->s_blocks_per_group,
3627 sbi->s_clusters_per_group);
3628 goto failed_mount;
3629 }
3630 } else {
3631 if (clustersize != blocksize) {
3632 ext4_warning(sb, "fragment/cluster size (%d) != "
3633 "block size (%d)", clustersize,
3634 blocksize);
3635 clustersize = blocksize;
3636 }
3637 if (sbi->s_blocks_per_group > blocksize * 8) {
3638 ext4_msg(sb, KERN_ERR,
3639 "#blocks per group too big: %lu",
3640 sbi->s_blocks_per_group);
3641 goto failed_mount;
3642 }
3643 sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3644 sbi->s_cluster_bits = 0;
ac27a0ec 3645 }
281b5995
TT
3646 sbi->s_cluster_ratio = clustersize / blocksize;
3647
ac27a0ec 3648 if (sbi->s_inodes_per_group > blocksize * 8) {
b31e1552
ES
3649 ext4_msg(sb, KERN_ERR,
3650 "#inodes per group too big: %lu",
2b2d6d01 3651 sbi->s_inodes_per_group);
ac27a0ec
DK
3652 goto failed_mount;
3653 }
3654
bf43d84b
ES
3655 /*
3656 * Test whether we have more sectors than will fit in sector_t,
3657 * and whether the max offset is addressable by the page cache.
3658 */
5a9ae68a 3659 err = generic_check_addressable(sb->s_blocksize_bits,
30ca22c7 3660 ext4_blocks_count(es));
5a9ae68a 3661 if (err) {
b31e1552 3662 ext4_msg(sb, KERN_ERR, "filesystem"
bf43d84b 3663 " too large to mount safely on this system");
ac27a0ec 3664 if (sizeof(sector_t) < 8)
90c699a9 3665 ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
ac27a0ec
DK
3666 goto failed_mount;
3667 }
3668
617ba13b
MC
3669 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3670 goto cantfind_ext4;
e7c95593 3671
0f2ddca6
FTN
3672 /* check blocks count against device size */
3673 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
3674 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
b31e1552
ES
3675 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3676 "exceeds size of device (%llu blocks)",
0f2ddca6
FTN
3677 ext4_blocks_count(es), blocks_count);
3678 goto failed_mount;
3679 }
3680
0b8e58a1
AD
3681 /*
3682 * It makes no sense for the first data block to be beyond the end
3683 * of the filesystem.
3684 */
3685 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
5635a62b 3686 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
b31e1552
ES
3687 "block %u is beyond end of filesystem (%llu)",
3688 le32_to_cpu(es->s_first_data_block),
3689 ext4_blocks_count(es));
e7c95593
ES
3690 goto failed_mount;
3691 }
bd81d8ee
LV
3692 blocks_count = (ext4_blocks_count(es) -
3693 le32_to_cpu(es->s_first_data_block) +
3694 EXT4_BLOCKS_PER_GROUP(sb) - 1);
3695 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4ec11028 3696 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
b31e1552 3697 ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
4ec11028 3698 "(block count %llu, first data block %u, "
b31e1552 3699 "blocks per group %lu)", sbi->s_groups_count,
4ec11028
TT
3700 ext4_blocks_count(es),
3701 le32_to_cpu(es->s_first_data_block),
3702 EXT4_BLOCKS_PER_GROUP(sb));
3703 goto failed_mount;
3704 }
bd81d8ee 3705 sbi->s_groups_count = blocks_count;
fb0a387d
ES
3706 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3707 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
617ba13b
MC
3708 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3709 EXT4_DESC_PER_BLOCK(sb);
f18a5f21
TT
3710 sbi->s_group_desc = ext4_kvmalloc(db_count *
3711 sizeof(struct buffer_head *),
3712 GFP_KERNEL);
ac27a0ec 3713 if (sbi->s_group_desc == NULL) {
b31e1552 3714 ext4_msg(sb, KERN_ERR, "not enough memory");
2cde417d 3715 ret = -ENOMEM;
ac27a0ec
DK
3716 goto failed_mount;
3717 }
3718
9f6200bb
TT
3719 if (ext4_proc_root)
3720 sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
240799cd 3721
66acdcf4
TT
3722 if (sbi->s_proc)
3723 proc_create_data("options", S_IRUGO, sbi->s_proc,
3724 &ext4_seq_options_fops, sb);
3725
705895b6 3726 bgl_lock_init(sbi->s_blockgroup_lock);
ac27a0ec
DK
3727
3728 for (i = 0; i < db_count; i++) {
70bbb3e0 3729 block = descriptor_loc(sb, logical_sb_block, i);
ac27a0ec
DK
3730 sbi->s_group_desc[i] = sb_bread(sb, block);
3731 if (!sbi->s_group_desc[i]) {
b31e1552
ES
3732 ext4_msg(sb, KERN_ERR,
3733 "can't read group descriptor %d", i);
ac27a0ec
DK
3734 db_count = i;
3735 goto failed_mount2;
3736 }
3737 }
bfff6873 3738 if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
b31e1552 3739 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
ac27a0ec
DK
3740 goto failed_mount2;
3741 }
772cb7c8
JS
3742 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
3743 if (!ext4_fill_flex_info(sb)) {
b31e1552
ES
3744 ext4_msg(sb, KERN_ERR,
3745 "unable to initialize "
3746 "flex_bg meta info!");
772cb7c8
JS
3747 goto failed_mount2;
3748 }
3749
ac27a0ec
DK
3750 sbi->s_gdb_count = db_count;
3751 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
3752 spin_lock_init(&sbi->s_next_gen_lock);
3753
04496411
TM
3754 init_timer(&sbi->s_err_report);
3755 sbi->s_err_report.function = print_daily_error_info;
3756 sbi->s_err_report.data = (unsigned long) sb;
3757
57042651 3758 err = percpu_counter_init(&sbi->s_freeclusters_counter,
5dee5437 3759 ext4_count_free_clusters(sb));
ce7e010a
TT
3760 if (!err) {
3761 err = percpu_counter_init(&sbi->s_freeinodes_counter,
3762 ext4_count_free_inodes(sb));
3763 }
3764 if (!err) {
3765 err = percpu_counter_init(&sbi->s_dirs_counter,
3766 ext4_count_dirs(sb));
3767 }
3768 if (!err) {
57042651 3769 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
ce7e010a
TT
3770 }
3771 if (err) {
3772 ext4_msg(sb, KERN_ERR, "insufficient memory");
3773 goto failed_mount3;
3774 }
3775
c9de560d 3776 sbi->s_stripe = ext4_get_stripe_size(sbi);
55138e0b 3777 sbi->s_max_writeback_mb_bump = 128;
67a5da56 3778 sbi->s_extent_max_zeroout_kb = 32;
c9de560d 3779
ac27a0ec
DK
3780 /*
3781 * set up enough so that it can read an inode
3782 */
9ca92389
TT
3783 if (!test_opt(sb, NOLOAD) &&
3784 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
3785 sb->s_op = &ext4_sops;
3786 else
3787 sb->s_op = &ext4_nojournal_sops;
617ba13b
MC
3788 sb->s_export_op = &ext4_export_ops;
3789 sb->s_xattr = ext4_xattr_handlers;
ac27a0ec 3790#ifdef CONFIG_QUOTA
617ba13b
MC
3791 sb->s_qcop = &ext4_qctl_operations;
3792 sb->dq_op = &ext4_quota_operations;
7c319d32
AK
3793
3794 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
3795 /* Use qctl operations for hidden quota files. */
3796 sb->s_qcop = &ext4_qctl_sysfile_operations;
3797 }
ac27a0ec 3798#endif
f2fa2ffc
AK
3799 memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
3800
ac27a0ec 3801 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
3b9d4ed2 3802 mutex_init(&sbi->s_orphan_lock);
8f82f840 3803 sbi->s_resize_flags = 0;
ac27a0ec
DK
3804
3805 sb->s_root = NULL;
3806
3807 needs_recovery = (es->s_last_orphan != 0 ||
617ba13b
MC
3808 EXT4_HAS_INCOMPAT_FEATURE(sb,
3809 EXT4_FEATURE_INCOMPAT_RECOVER));
ac27a0ec 3810
c5e06d10
JL
3811 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
3812 !(sb->s_flags & MS_RDONLY))
3813 if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
3814 goto failed_mount3;
3815
ac27a0ec
DK
3816 /*
3817 * The first inode we look at is the journal inode. Don't try
3818 * root first: it may be modified in the journal!
3819 */
3820 if (!test_opt(sb, NOLOAD) &&
617ba13b
MC
3821 EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
3822 if (ext4_load_journal(sb, es, journal_devnum))
ac27a0ec 3823 goto failed_mount3;
0390131b
FM
3824 } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
3825 EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
b31e1552
ES
3826 ext4_msg(sb, KERN_ERR, "required journal recovery "
3827 "suppressed and not mounted read-only");
744692dc 3828 goto failed_mount_wq;
ac27a0ec 3829 } else {
fd8c37ec 3830 clear_opt(sb, DATA_FLAGS);
0390131b
FM
3831 sbi->s_journal = NULL;
3832 needs_recovery = 0;
3833 goto no_journal;
ac27a0ec
DK
3834 }
3835
f32aaf2d 3836 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT) &&
eb40a09c
JS
3837 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
3838 JBD2_FEATURE_INCOMPAT_64BIT)) {
b31e1552 3839 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
744692dc 3840 goto failed_mount_wq;
eb40a09c
JS
3841 }
3842
25ed6e8a
DW
3843 if (!set_journal_csum_feature_set(sb)) {
3844 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
3845 "feature set");
3846 goto failed_mount_wq;
d4da6c9c 3847 }
818d276c 3848
ac27a0ec
DK
3849 /* We have now updated the journal if required, so we can
3850 * validate the data journaling mode. */
3851 switch (test_opt(sb, DATA_FLAGS)) {
3852 case 0:
3853 /* No mode set, assume a default based on the journal
63f57933
AM
3854 * capabilities: ORDERED_DATA if the journal can
3855 * cope, else JOURNAL_DATA
3856 */
dab291af
MC
3857 if (jbd2_journal_check_available_features
3858 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
fd8c37ec 3859 set_opt(sb, ORDERED_DATA);
ac27a0ec 3860 else
fd8c37ec 3861 set_opt(sb, JOURNAL_DATA);
ac27a0ec
DK
3862 break;
3863
617ba13b
MC
3864 case EXT4_MOUNT_ORDERED_DATA:
3865 case EXT4_MOUNT_WRITEBACK_DATA:
dab291af
MC
3866 if (!jbd2_journal_check_available_features
3867 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
b31e1552
ES
3868 ext4_msg(sb, KERN_ERR, "Journal does not support "
3869 "requested data journaling mode");
744692dc 3870 goto failed_mount_wq;
ac27a0ec
DK
3871 }
3872 default:
3873 break;
3874 }
b3881f74 3875 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
ac27a0ec 3876
18aadd47
BJ
3877 sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
3878
ce7e010a
TT
3879 /*
3880 * The journal may have updated the bg summary counts, so we
3881 * need to update the global counters.
3882 */
57042651 3883 percpu_counter_set(&sbi->s_freeclusters_counter,
5dee5437 3884 ext4_count_free_clusters(sb));
ce7e010a
TT
3885 percpu_counter_set(&sbi->s_freeinodes_counter,
3886 ext4_count_free_inodes(sb));
3887 percpu_counter_set(&sbi->s_dirs_counter,
3888 ext4_count_dirs(sb));
57042651 3889 percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
206f7ab4 3890
ce7e010a 3891no_journal:
952fc18e
TT
3892 /*
3893 * Get the # of file system overhead blocks from the
3894 * superblock if present.
3895 */
3896 if (es->s_overhead_clusters)
3897 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
3898 else {
07aa2ea1
LC
3899 err = ext4_calculate_overhead(sb);
3900 if (err)
952fc18e
TT
3901 goto failed_mount_wq;
3902 }
3903
fd89d5f2
TH
3904 /*
3905 * The maximum number of concurrent works can be high and
3906 * concurrency isn't really necessary. Limit it to 1.
3907 */
3908 EXT4_SB(sb)->dio_unwritten_wq =
ae005cbe 3909 alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4c0425ff
MC
3910 if (!EXT4_SB(sb)->dio_unwritten_wq) {
3911 printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
07aa2ea1 3912 ret = -ENOMEM;
4c0425ff
MC
3913 goto failed_mount_wq;
3914 }
3915
ac27a0ec 3916 /*
dab291af 3917 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
3918 * so we can safely mount the rest of the filesystem now.
3919 */
3920
1d1fe1ee
DH
3921 root = ext4_iget(sb, EXT4_ROOT_INO);
3922 if (IS_ERR(root)) {
b31e1552 3923 ext4_msg(sb, KERN_ERR, "get root inode failed");
1d1fe1ee 3924 ret = PTR_ERR(root);
32a9bb57 3925 root = NULL;
ac27a0ec
DK
3926 goto failed_mount4;
3927 }
3928 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
b31e1552 3929 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
94bf608a 3930 iput(root);
ac27a0ec
DK
3931 goto failed_mount4;
3932 }
48fde701 3933 sb->s_root = d_make_root(root);
1d1fe1ee 3934 if (!sb->s_root) {
b31e1552 3935 ext4_msg(sb, KERN_ERR, "get root dentry failed");
1d1fe1ee
DH
3936 ret = -ENOMEM;
3937 goto failed_mount4;
3938 }
ac27a0ec 3939
7e84b621
ES
3940 if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
3941 sb->s_flags |= MS_RDONLY;
ef7f3835
KS
3942
3943 /* determine the minimum size of new large inodes, if present */
3944 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
3945 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3946 EXT4_GOOD_OLD_INODE_SIZE;
3947 if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3948 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
3949 if (sbi->s_want_extra_isize <
3950 le16_to_cpu(es->s_want_extra_isize))
3951 sbi->s_want_extra_isize =
3952 le16_to_cpu(es->s_want_extra_isize);
3953 if (sbi->s_want_extra_isize <
3954 le16_to_cpu(es->s_min_extra_isize))
3955 sbi->s_want_extra_isize =
3956 le16_to_cpu(es->s_min_extra_isize);
3957 }
3958 }
3959 /* Check if enough inode space is available */
3960 if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
3961 sbi->s_inode_size) {
3962 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
3963 EXT4_GOOD_OLD_INODE_SIZE;
b31e1552
ES
3964 ext4_msg(sb, KERN_INFO, "required extra inode space not"
3965 "available");
ef7f3835
KS
3966 }
3967
6fd058f7
TT
3968 err = ext4_setup_system_zone(sb);
3969 if (err) {
b31e1552 3970 ext4_msg(sb, KERN_ERR, "failed to initialize system "
fbe845dd 3971 "zone (%d)", err);
94bf608a 3972 goto failed_mount4a;
6fd058f7
TT
3973 }
3974
c2774d84 3975 ext4_ext_init(sb);
9d99012f 3976 err = ext4_mb_init(sb);
c2774d84 3977 if (err) {
421f91d2 3978 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
b31e1552 3979 err);
dcf2d804 3980 goto failed_mount5;
c2774d84
AK
3981 }
3982
bfff6873
LC
3983 err = ext4_register_li_request(sb, first_not_zeroed);
3984 if (err)
dcf2d804 3985 goto failed_mount6;
bfff6873 3986
3197ebdb
TT
3987 sbi->s_kobj.kset = ext4_kset;
3988 init_completion(&sbi->s_kobj_unregister);
3989 err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
3990 "%s", sb->s_id);
dcf2d804
TM
3991 if (err)
3992 goto failed_mount7;
3197ebdb 3993
617ba13b
MC
3994 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
3995 ext4_orphan_cleanup(sb, es);
3996 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
0390131b 3997 if (needs_recovery) {
b31e1552 3998 ext4_msg(sb, KERN_INFO, "recovery complete");
0390131b
FM
3999 ext4_mark_recovery_complete(sb, es);
4000 }
4001 if (EXT4_SB(sb)->s_journal) {
4002 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
4003 descr = " journalled data mode";
4004 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4005 descr = " ordered data mode";
4006 else
4007 descr = " writeback data mode";
4008 } else
4009 descr = "out journal";
4010
7c319d32
AK
4011#ifdef CONFIG_QUOTA
4012 /* Enable quota usage during mount. */
4013 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
4014 !(sb->s_flags & MS_RDONLY)) {
07aa2ea1
LC
4015 err = ext4_enable_quotas(sb);
4016 if (err)
7c319d32
AK
4017 goto failed_mount7;
4018 }
4019#endif /* CONFIG_QUOTA */
4020
79add3a3
LC
4021 if (test_opt(sb, DISCARD)) {
4022 struct request_queue *q = bdev_get_queue(sb->s_bdev);
4023 if (!blk_queue_discard(q))
4024 ext4_msg(sb, KERN_WARNING,
4025 "mounting with \"discard\" option, but "
4026 "the device does not support discard");
4027 }
4028
d4c402d9 4029 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
8b67f04a
TT
4030 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
4031 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
ac27a0ec 4032
66e61a9e
TT
4033 if (es->s_error_count)
4034 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
ac27a0ec 4035
d4c402d9 4036 kfree(orig_data);
ac27a0ec
DK
4037 return 0;
4038
617ba13b 4039cantfind_ext4:
ac27a0ec 4040 if (!silent)
b31e1552 4041 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
ac27a0ec
DK
4042 goto failed_mount;
4043
dcf2d804
TM
4044failed_mount7:
4045 ext4_unregister_li_request(sb);
4046failed_mount6:
dcf2d804 4047 ext4_mb_release(sb);
94bf608a
AV
4048failed_mount5:
4049 ext4_ext_release(sb);
dcf2d804 4050 ext4_release_system_zone(sb);
94bf608a
AV
4051failed_mount4a:
4052 dput(sb->s_root);
32a9bb57 4053 sb->s_root = NULL;
94bf608a 4054failed_mount4:
b31e1552 4055 ext4_msg(sb, KERN_ERR, "mount failed");
4c0425ff
MC
4056 destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
4057failed_mount_wq:
0390131b
FM
4058 if (sbi->s_journal) {
4059 jbd2_journal_destroy(sbi->s_journal);
4060 sbi->s_journal = NULL;
4061 }
ac27a0ec 4062failed_mount3:
04496411 4063 del_timer(&sbi->s_err_report);
9933fc0a
TT
4064 if (sbi->s_flex_groups)
4065 ext4_kvfree(sbi->s_flex_groups);
57042651 4066 percpu_counter_destroy(&sbi->s_freeclusters_counter);
ce7e010a
TT
4067 percpu_counter_destroy(&sbi->s_freeinodes_counter);
4068 percpu_counter_destroy(&sbi->s_dirs_counter);
57042651 4069 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
c5e06d10
JL
4070 if (sbi->s_mmp_tsk)
4071 kthread_stop(sbi->s_mmp_tsk);
ac27a0ec
DK
4072failed_mount2:
4073 for (i = 0; i < db_count; i++)
4074 brelse(sbi->s_group_desc[i]);
f18a5f21 4075 ext4_kvfree(sbi->s_group_desc);
ac27a0ec 4076failed_mount:
0441984a
DW
4077 if (sbi->s_chksum_driver)
4078 crypto_free_shash(sbi->s_chksum_driver);
240799cd 4079 if (sbi->s_proc) {
66acdcf4 4080 remove_proc_entry("options", sbi->s_proc);
9f6200bb 4081 remove_proc_entry(sb->s_id, ext4_proc_root);
240799cd 4082 }
ac27a0ec
DK
4083#ifdef CONFIG_QUOTA
4084 for (i = 0; i < MAXQUOTAS; i++)
4085 kfree(sbi->s_qf_names[i]);
4086#endif
617ba13b 4087 ext4_blkdev_remove(sbi);
ac27a0ec
DK
4088 brelse(bh);
4089out_fail:
4090 sb->s_fs_info = NULL;
f6830165 4091 kfree(sbi->s_blockgroup_lock);
ac27a0ec 4092 kfree(sbi);
dcc7dae3 4093out_free_orig:
d4c402d9 4094 kfree(orig_data);
07aa2ea1 4095 return err ? err : ret;
ac27a0ec
DK
4096}
4097
4098/*
4099 * Setup any per-fs journal parameters now. We'll do this both on
4100 * initial mount, once the journal has been initialised but before we've
4101 * done any recovery; and again on any subsequent remount.
4102 */
617ba13b 4103static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 4104{
617ba13b 4105 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 4106
30773840
TT
4107 journal->j_commit_interval = sbi->s_commit_interval;
4108 journal->j_min_batch_time = sbi->s_min_batch_time;
4109 journal->j_max_batch_time = sbi->s_max_batch_time;
ac27a0ec 4110
a931da6a 4111 write_lock(&journal->j_state_lock);
ac27a0ec 4112 if (test_opt(sb, BARRIER))
dab291af 4113 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 4114 else
dab291af 4115 journal->j_flags &= ~JBD2_BARRIER;
5bf5683a
HK
4116 if (test_opt(sb, DATA_ERR_ABORT))
4117 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
4118 else
4119 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
a931da6a 4120 write_unlock(&journal->j_state_lock);
ac27a0ec
DK
4121}
4122
617ba13b 4123static journal_t *ext4_get_journal(struct super_block *sb,
ac27a0ec
DK
4124 unsigned int journal_inum)
4125{
4126 struct inode *journal_inode;
4127 journal_t *journal;
4128
0390131b
FM
4129 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4130
ac27a0ec
DK
4131 /* First, test for the existence of a valid inode on disk. Bad
4132 * things happen if we iget() an unused inode, as the subsequent
4133 * iput() will try to delete it. */
4134
1d1fe1ee
DH
4135 journal_inode = ext4_iget(sb, journal_inum);
4136 if (IS_ERR(journal_inode)) {
b31e1552 4137 ext4_msg(sb, KERN_ERR, "no journal found");
ac27a0ec
DK
4138 return NULL;
4139 }
4140 if (!journal_inode->i_nlink) {
4141 make_bad_inode(journal_inode);
4142 iput(journal_inode);
b31e1552 4143 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
ac27a0ec
DK
4144 return NULL;
4145 }
4146
e5f8eab8 4147 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
ac27a0ec 4148 journal_inode, journal_inode->i_size);
1d1fe1ee 4149 if (!S_ISREG(journal_inode->i_mode)) {
b31e1552 4150 ext4_msg(sb, KERN_ERR, "invalid journal inode");
ac27a0ec
DK
4151 iput(journal_inode);
4152 return NULL;
4153 }
4154
dab291af 4155 journal = jbd2_journal_init_inode(journal_inode);
ac27a0ec 4156 if (!journal) {
b31e1552 4157 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
ac27a0ec
DK
4158 iput(journal_inode);
4159 return NULL;
4160 }
4161 journal->j_private = sb;
617ba13b 4162 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
4163 return journal;
4164}
4165
617ba13b 4166static journal_t *ext4_get_dev_journal(struct super_block *sb,
ac27a0ec
DK
4167 dev_t j_dev)
4168{
2b2d6d01 4169 struct buffer_head *bh;
ac27a0ec 4170 journal_t *journal;
617ba13b
MC
4171 ext4_fsblk_t start;
4172 ext4_fsblk_t len;
ac27a0ec 4173 int hblock, blocksize;
617ba13b 4174 ext4_fsblk_t sb_block;
ac27a0ec 4175 unsigned long offset;
2b2d6d01 4176 struct ext4_super_block *es;
ac27a0ec
DK
4177 struct block_device *bdev;
4178
0390131b
FM
4179 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4180
b31e1552 4181 bdev = ext4_blkdev_get(j_dev, sb);
ac27a0ec
DK
4182 if (bdev == NULL)
4183 return NULL;
4184
ac27a0ec 4185 blocksize = sb->s_blocksize;
e1defc4f 4186 hblock = bdev_logical_block_size(bdev);
ac27a0ec 4187 if (blocksize < hblock) {
b31e1552
ES
4188 ext4_msg(sb, KERN_ERR,
4189 "blocksize too small for journal device");
ac27a0ec
DK
4190 goto out_bdev;
4191 }
4192
617ba13b
MC
4193 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4194 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ac27a0ec
DK
4195 set_blocksize(bdev, blocksize);
4196 if (!(bh = __bread(bdev, sb_block, blocksize))) {
b31e1552
ES
4197 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4198 "external journal");
ac27a0ec
DK
4199 goto out_bdev;
4200 }
4201
2716b802 4202 es = (struct ext4_super_block *) (bh->b_data + offset);
617ba13b 4203 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 4204 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b 4205 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
b31e1552
ES
4206 ext4_msg(sb, KERN_ERR, "external journal has "
4207 "bad superblock");
ac27a0ec
DK
4208 brelse(bh);
4209 goto out_bdev;
4210 }
4211
617ba13b 4212 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
b31e1552 4213 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
ac27a0ec
DK
4214 brelse(bh);
4215 goto out_bdev;
4216 }
4217
bd81d8ee 4218 len = ext4_blocks_count(es);
ac27a0ec
DK
4219 start = sb_block + 1;
4220 brelse(bh); /* we're done with the superblock */
4221
dab291af 4222 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
ac27a0ec
DK
4223 start, len, blocksize);
4224 if (!journal) {
b31e1552 4225 ext4_msg(sb, KERN_ERR, "failed to create device journal");
ac27a0ec
DK
4226 goto out_bdev;
4227 }
4228 journal->j_private = sb;
4229 ll_rw_block(READ, 1, &journal->j_sb_buffer);
4230 wait_on_buffer(journal->j_sb_buffer);
4231 if (!buffer_uptodate(journal->j_sb_buffer)) {
b31e1552 4232 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
ac27a0ec
DK
4233 goto out_journal;
4234 }
4235 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
b31e1552
ES
4236 ext4_msg(sb, KERN_ERR, "External journal has more than one "
4237 "user (unsupported) - %d",
ac27a0ec
DK
4238 be32_to_cpu(journal->j_superblock->s_nr_users));
4239 goto out_journal;
4240 }
617ba13b
MC
4241 EXT4_SB(sb)->journal_bdev = bdev;
4242 ext4_init_journal_params(sb, journal);
ac27a0ec 4243 return journal;
0b8e58a1 4244
ac27a0ec 4245out_journal:
dab291af 4246 jbd2_journal_destroy(journal);
ac27a0ec 4247out_bdev:
617ba13b 4248 ext4_blkdev_put(bdev);
ac27a0ec
DK
4249 return NULL;
4250}
4251
617ba13b
MC
4252static int ext4_load_journal(struct super_block *sb,
4253 struct ext4_super_block *es,
ac27a0ec
DK
4254 unsigned long journal_devnum)
4255{
4256 journal_t *journal;
4257 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4258 dev_t journal_dev;
4259 int err = 0;
4260 int really_read_only;
4261
0390131b
FM
4262 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4263
ac27a0ec
DK
4264 if (journal_devnum &&
4265 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
b31e1552
ES
4266 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4267 "numbers have changed");
ac27a0ec
DK
4268 journal_dev = new_decode_dev(journal_devnum);
4269 } else
4270 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4271
4272 really_read_only = bdev_read_only(sb->s_bdev);
4273
4274 /*
4275 * Are we loading a blank journal or performing recovery after a
4276 * crash? For recovery, we need to check in advance whether we
4277 * can get read-write access to the device.
4278 */
617ba13b 4279 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
ac27a0ec 4280 if (sb->s_flags & MS_RDONLY) {
b31e1552
ES
4281 ext4_msg(sb, KERN_INFO, "INFO: recovery "
4282 "required on readonly filesystem");
ac27a0ec 4283 if (really_read_only) {
b31e1552
ES
4284 ext4_msg(sb, KERN_ERR, "write access "
4285 "unavailable, cannot proceed");
ac27a0ec
DK
4286 return -EROFS;
4287 }
b31e1552
ES
4288 ext4_msg(sb, KERN_INFO, "write access will "
4289 "be enabled during recovery");
ac27a0ec
DK
4290 }
4291 }
4292
4293 if (journal_inum && journal_dev) {
b31e1552
ES
4294 ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4295 "and inode journals!");
ac27a0ec
DK
4296 return -EINVAL;
4297 }
4298
4299 if (journal_inum) {
617ba13b 4300 if (!(journal = ext4_get_journal(sb, journal_inum)))
ac27a0ec
DK
4301 return -EINVAL;
4302 } else {
617ba13b 4303 if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
ac27a0ec
DK
4304 return -EINVAL;
4305 }
4306
90576c0b 4307 if (!(journal->j_flags & JBD2_BARRIER))
b31e1552 4308 ext4_msg(sb, KERN_INFO, "barriers disabled");
4776004f 4309
617ba13b 4310 if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
dab291af 4311 err = jbd2_journal_wipe(journal, !really_read_only);
1c13d5c0
TT
4312 if (!err) {
4313 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
4314 if (save)
4315 memcpy(save, ((char *) es) +
4316 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
dab291af 4317 err = jbd2_journal_load(journal);
1c13d5c0
TT
4318 if (save)
4319 memcpy(((char *) es) + EXT4_S_ERR_START,
4320 save, EXT4_S_ERR_LEN);
4321 kfree(save);
4322 }
ac27a0ec
DK
4323
4324 if (err) {
b31e1552 4325 ext4_msg(sb, KERN_ERR, "error loading journal");
dab291af 4326 jbd2_journal_destroy(journal);
ac27a0ec
DK
4327 return err;
4328 }
4329
617ba13b
MC
4330 EXT4_SB(sb)->s_journal = journal;
4331 ext4_clear_journal_err(sb, es);
ac27a0ec 4332
c41303ce 4333 if (!really_read_only && journal_devnum &&
ac27a0ec
DK
4334 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4335 es->s_journal_dev = cpu_to_le32(journal_devnum);
ac27a0ec
DK
4336
4337 /* Make sure we flush the recovery flag to disk. */
e2d67052 4338 ext4_commit_super(sb, 1);
ac27a0ec
DK
4339 }
4340
4341 return 0;
4342}
4343
e2d67052 4344static int ext4_commit_super(struct super_block *sb, int sync)
ac27a0ec 4345{
e2d67052 4346 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
617ba13b 4347 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
c4be0c1d 4348 int error = 0;
ac27a0ec 4349
7c2e7087 4350 if (!sbh || block_device_ejected(sb))
c4be0c1d 4351 return error;
914258bf
TT
4352 if (buffer_write_io_error(sbh)) {
4353 /*
4354 * Oh, dear. A previous attempt to write the
4355 * superblock failed. This could happen because the
4356 * USB device was yanked out. Or it could happen to
4357 * be a transient write error and maybe the block will
4358 * be remapped. Nothing we can do but to retry the
4359 * write and hope for the best.
4360 */
b31e1552
ES
4361 ext4_msg(sb, KERN_ERR, "previous I/O error to "
4362 "superblock detected");
914258bf
TT
4363 clear_buffer_write_io_error(sbh);
4364 set_buffer_uptodate(sbh);
4365 }
71290b36
TT
4366 /*
4367 * If the file system is mounted read-only, don't update the
4368 * superblock write time. This avoids updating the superblock
4369 * write time when we are mounting the root file system
4370 * read/only but we need to replay the journal; at that point,
4371 * for people who are east of GMT and who make their clock
4372 * tick in localtime for Windows bug-for-bug compatibility,
4373 * the clock is set in the future, and this will cause e2fsck
4374 * to complain and force a full file system check.
4375 */
4376 if (!(sb->s_flags & MS_RDONLY))
4377 es->s_wtime = cpu_to_le32(get_seconds());
f613dfcb
TT
4378 if (sb->s_bdev->bd_part)
4379 es->s_kbytes_written =
4380 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
afc32f7e
TT
4381 ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4382 EXT4_SB(sb)->s_sectors_written_start) >> 1));
f613dfcb
TT
4383 else
4384 es->s_kbytes_written =
4385 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
57042651
TT
4386 ext4_free_blocks_count_set(es,
4387 EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
4388 &EXT4_SB(sb)->s_freeclusters_counter)));
ce7e010a
TT
4389 es->s_free_inodes_count =
4390 cpu_to_le32(percpu_counter_sum_positive(
4391 &EXT4_SB(sb)->s_freeinodes_counter));
ac27a0ec 4392 BUFFER_TRACE(sbh, "marking dirty");
06db49e6 4393 ext4_superblock_csum_set(sb);
ac27a0ec 4394 mark_buffer_dirty(sbh);
914258bf 4395 if (sync) {
c4be0c1d
TS
4396 error = sync_dirty_buffer(sbh);
4397 if (error)
4398 return error;
4399
4400 error = buffer_write_io_error(sbh);
4401 if (error) {
b31e1552
ES
4402 ext4_msg(sb, KERN_ERR, "I/O error while writing "
4403 "superblock");
914258bf
TT
4404 clear_buffer_write_io_error(sbh);
4405 set_buffer_uptodate(sbh);
4406 }
4407 }
c4be0c1d 4408 return error;
ac27a0ec
DK
4409}
4410
ac27a0ec
DK
4411/*
4412 * Have we just finished recovery? If so, and if we are mounting (or
4413 * remounting) the filesystem readonly, then we will end up with a
4414 * consistent fs on disk. Record that fact.
4415 */
2b2d6d01
TT
4416static void ext4_mark_recovery_complete(struct super_block *sb,
4417 struct ext4_super_block *es)
ac27a0ec 4418{
617ba13b 4419 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 4420
0390131b
FM
4421 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
4422 BUG_ON(journal != NULL);
4423 return;
4424 }
dab291af 4425 jbd2_journal_lock_updates(journal);
7ffe1ea8
HK
4426 if (jbd2_journal_flush(journal) < 0)
4427 goto out;
4428
617ba13b 4429 if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
ac27a0ec 4430 sb->s_flags & MS_RDONLY) {
617ba13b 4431 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
e2d67052 4432 ext4_commit_super(sb, 1);
ac27a0ec 4433 }
7ffe1ea8
HK
4434
4435out:
dab291af 4436 jbd2_journal_unlock_updates(journal);
ac27a0ec
DK
4437}
4438
4439/*
4440 * If we are mounting (or read-write remounting) a filesystem whose journal
4441 * has recorded an error from a previous lifetime, move that error to the
4442 * main filesystem now.
4443 */
2b2d6d01
TT
4444static void ext4_clear_journal_err(struct super_block *sb,
4445 struct ext4_super_block *es)
ac27a0ec
DK
4446{
4447 journal_t *journal;
4448 int j_errno;
4449 const char *errstr;
4450
0390131b
FM
4451 BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4452
617ba13b 4453 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
4454
4455 /*
4456 * Now check for any error status which may have been recorded in the
617ba13b 4457 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
4458 */
4459
dab291af 4460 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
4461 if (j_errno) {
4462 char nbuf[16];
4463
617ba13b 4464 errstr = ext4_decode_error(sb, j_errno, nbuf);
12062ddd 4465 ext4_warning(sb, "Filesystem error recorded "
ac27a0ec 4466 "from previous mount: %s", errstr);
12062ddd 4467 ext4_warning(sb, "Marking fs in need of filesystem check.");
ac27a0ec 4468
617ba13b
MC
4469 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4470 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
e2d67052 4471 ext4_commit_super(sb, 1);
ac27a0ec 4472
dab291af 4473 jbd2_journal_clear_err(journal);
d796c52e 4474 jbd2_journal_update_sb_errno(journal);
ac27a0ec
DK
4475 }
4476}
4477
4478/*
4479 * Force the running and committing transactions to commit,
4480 * and wait on the commit.
4481 */
617ba13b 4482int ext4_force_commit(struct super_block *sb)
ac27a0ec
DK
4483{
4484 journal_t *journal;
0390131b 4485 int ret = 0;
ac27a0ec
DK
4486
4487 if (sb->s_flags & MS_RDONLY)
4488 return 0;
4489
617ba13b 4490 journal = EXT4_SB(sb)->s_journal;
8e8ad8a5 4491 if (journal)
0390131b 4492 ret = ext4_journal_force_commit(journal);
0390131b 4493
ac27a0ec
DK
4494 return ret;
4495}
4496
617ba13b 4497static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec 4498{
14ce0cb4 4499 int ret = 0;
9eddacf9 4500 tid_t target;
8d5d02e6 4501 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 4502
9bffad1e 4503 trace_ext4_sync_fs(sb, wait);
8d5d02e6 4504 flush_workqueue(sbi->dio_unwritten_wq);
a1177825
JK
4505 /*
4506 * Writeback quota in non-journalled quota case - journalled quota has
4507 * no dirty dquots
4508 */
4509 dquot_writeback_dquots(sb, -1);
8d5d02e6 4510 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
9ca92389 4511 if (wait)
8d5d02e6 4512 jbd2_log_wait_commit(sbi->s_journal, target);
0390131b 4513 }
14ce0cb4 4514 return ret;
ac27a0ec
DK
4515}
4516
4517/*
4518 * LVM calls this function before a (read-only) snapshot is created. This
4519 * gives us a chance to flush the journal completely and mark the fs clean.
be4f27d3
YY
4520 *
4521 * Note that only this function cannot bring a filesystem to be in a clean
8e8ad8a5
JK
4522 * state independently. It relies on upper layer to stop all data & metadata
4523 * modifications.
ac27a0ec 4524 */
c4be0c1d 4525static int ext4_freeze(struct super_block *sb)
ac27a0ec 4526{
c4be0c1d
TS
4527 int error = 0;
4528 journal_t *journal;
ac27a0ec 4529
9ca92389
TT
4530 if (sb->s_flags & MS_RDONLY)
4531 return 0;
ac27a0ec 4532
9ca92389 4533 journal = EXT4_SB(sb)->s_journal;
7ffe1ea8 4534
9ca92389
TT
4535 /* Now we set up the journal barrier. */
4536 jbd2_journal_lock_updates(journal);
ac27a0ec 4537
9ca92389
TT
4538 /*
4539 * Don't clear the needs_recovery flag if we failed to flush
4540 * the journal.
4541 */
4542 error = jbd2_journal_flush(journal);
6b0310fb
ES
4543 if (error < 0)
4544 goto out;
9ca92389
TT
4545
4546 /* Journal blocked and flushed, clear needs_recovery flag. */
4547 EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4548 error = ext4_commit_super(sb, 1);
6b0310fb 4549out:
8e8ad8a5 4550 /* we rely on upper layer to stop further updates */
6b0310fb
ES
4551 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
4552 return error;
ac27a0ec
DK
4553}
4554
4555/*
4556 * Called by LVM after the snapshot is done. We need to reset the RECOVER
4557 * flag here, even though the filesystem is not technically dirty yet.
4558 */
c4be0c1d 4559static int ext4_unfreeze(struct super_block *sb)
ac27a0ec 4560{
9ca92389
TT
4561 if (sb->s_flags & MS_RDONLY)
4562 return 0;
4563
9ca92389
TT
4564 /* Reset the needs_recovery flag before the fs is unlocked. */
4565 EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4566 ext4_commit_super(sb, 1);
c4be0c1d 4567 return 0;
ac27a0ec
DK
4568}
4569
673c6100
TT
4570/*
4571 * Structure to save mount options for ext4_remount's benefit
4572 */
4573struct ext4_mount_options {
4574 unsigned long s_mount_opt;
a2595b8a 4575 unsigned long s_mount_opt2;
08cefc7a
EB
4576 kuid_t s_resuid;
4577 kgid_t s_resgid;
673c6100
TT
4578 unsigned long s_commit_interval;
4579 u32 s_min_batch_time, s_max_batch_time;
4580#ifdef CONFIG_QUOTA
4581 int s_jquota_fmt;
4582 char *s_qf_names[MAXQUOTAS];
4583#endif
4584};
4585
2b2d6d01 4586static int ext4_remount(struct super_block *sb, int *flags, char *data)
ac27a0ec 4587{
2b2d6d01 4588 struct ext4_super_block *es;
617ba13b 4589 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 4590 unsigned long old_sb_flags;
617ba13b 4591 struct ext4_mount_options old_opts;
c79d967d 4592 int enable_quota = 0;
8a266467 4593 ext4_group_t g;
b3881f74 4594 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
c5e06d10 4595 int err = 0;
ac27a0ec
DK
4596#ifdef CONFIG_QUOTA
4597 int i;
4598#endif
d4c402d9 4599 char *orig_data = kstrdup(data, GFP_KERNEL);
ac27a0ec
DK
4600
4601 /* Store the original options */
4602 old_sb_flags = sb->s_flags;
4603 old_opts.s_mount_opt = sbi->s_mount_opt;
a2595b8a 4604 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
ac27a0ec
DK
4605 old_opts.s_resuid = sbi->s_resuid;
4606 old_opts.s_resgid = sbi->s_resgid;
4607 old_opts.s_commit_interval = sbi->s_commit_interval;
30773840
TT
4608 old_opts.s_min_batch_time = sbi->s_min_batch_time;
4609 old_opts.s_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
4610#ifdef CONFIG_QUOTA
4611 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4612 for (i = 0; i < MAXQUOTAS; i++)
4613 old_opts.s_qf_names[i] = sbi->s_qf_names[i];
4614#endif
b3881f74
TT
4615 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4616 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
ac27a0ec
DK
4617
4618 /*
4619 * Allow the "check" option to be passed as a remount option.
4620 */
661aa520 4621 if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
ac27a0ec
DK
4622 err = -EINVAL;
4623 goto restore_opts;
4624 }
4625
4ab2f15b 4626 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
c67d859e 4627 ext4_abort(sb, "Abort forced by user");
ac27a0ec
DK
4628
4629 sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
482a7425 4630 (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
ac27a0ec
DK
4631
4632 es = sbi->s_es;
4633
b3881f74 4634 if (sbi->s_journal) {
0390131b 4635 ext4_init_journal_params(sb, sbi->s_journal);
b3881f74
TT
4636 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4637 }
ac27a0ec 4638
661aa520 4639 if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
4ab2f15b 4640 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
ac27a0ec
DK
4641 err = -EROFS;
4642 goto restore_opts;
4643 }
4644
4645 if (*flags & MS_RDONLY) {
0f0dd62f
CH
4646 err = dquot_suspend(sb, -1);
4647 if (err < 0)
c79d967d 4648 goto restore_opts;
c79d967d 4649
ac27a0ec
DK
4650 /*
4651 * First of all, the unconditional stuff we have to do
4652 * to disable replay of the journal when we next remount
4653 */
4654 sb->s_flags |= MS_RDONLY;
4655
4656 /*
4657 * OK, test if we are remounting a valid rw partition
4658 * readonly, and if so set the rdonly flag and then
4659 * mark the partition as valid again.
4660 */
617ba13b
MC
4661 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
4662 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
4663 es->s_state = cpu_to_le16(sbi->s_mount_state);
4664
a63c9eb2 4665 if (sbi->s_journal)
0390131b 4666 ext4_mark_recovery_complete(sb, es);
ac27a0ec 4667 } else {
a13fb1a4
ES
4668 /* Make sure we can mount this feature set readwrite */
4669 if (!ext4_feature_set_ok(sb, 0)) {
ac27a0ec
DK
4670 err = -EROFS;
4671 goto restore_opts;
4672 }
8a266467
TT
4673 /*
4674 * Make sure the group descriptor checksums
0b8e58a1 4675 * are sane. If they aren't, refuse to remount r/w.
8a266467
TT
4676 */
4677 for (g = 0; g < sbi->s_groups_count; g++) {
4678 struct ext4_group_desc *gdp =
4679 ext4_get_group_desc(sb, g, NULL);
4680
feb0ab32 4681 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
b31e1552
ES
4682 ext4_msg(sb, KERN_ERR,
4683 "ext4_remount: Checksum for group %u failed (%u!=%u)",
8a266467
TT
4684 g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
4685 le16_to_cpu(gdp->bg_checksum));
4686 err = -EINVAL;
4687 goto restore_opts;
4688 }
4689 }
4690
ead6596b
ES
4691 /*
4692 * If we have an unprocessed orphan list hanging
4693 * around from a previously readonly bdev mount,
4694 * require a full umount/remount for now.
4695 */
4696 if (es->s_last_orphan) {
b31e1552 4697 ext4_msg(sb, KERN_WARNING, "Couldn't "
ead6596b
ES
4698 "remount RDWR because of unprocessed "
4699 "orphan inode list. Please "
b31e1552 4700 "umount/remount instead");
ead6596b
ES
4701 err = -EINVAL;
4702 goto restore_opts;
4703 }
4704
ac27a0ec
DK
4705 /*
4706 * Mounting a RDONLY partition read-write, so reread
4707 * and store the current valid flag. (It may have
4708 * been changed by e2fsck since we originally mounted
4709 * the partition.)
4710 */
0390131b
FM
4711 if (sbi->s_journal)
4712 ext4_clear_journal_err(sb, es);
ac27a0ec 4713 sbi->s_mount_state = le16_to_cpu(es->s_state);
2b2d6d01 4714 if (!ext4_setup_super(sb, es, 0))
ac27a0ec 4715 sb->s_flags &= ~MS_RDONLY;
c5e06d10
JL
4716 if (EXT4_HAS_INCOMPAT_FEATURE(sb,
4717 EXT4_FEATURE_INCOMPAT_MMP))
4718 if (ext4_multi_mount_protect(sb,
4719 le64_to_cpu(es->s_mmp_block))) {
4720 err = -EROFS;
4721 goto restore_opts;
4722 }
c79d967d 4723 enable_quota = 1;
ac27a0ec
DK
4724 }
4725 }
bfff6873
LC
4726
4727 /*
4728 * Reinitialize lazy itable initialization thread based on
4729 * current settings
4730 */
4731 if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
4732 ext4_unregister_li_request(sb);
4733 else {
4734 ext4_group_t first_not_zeroed;
4735 first_not_zeroed = ext4_has_uninit_itable(sb);
4736 ext4_register_li_request(sb, first_not_zeroed);
4737 }
4738
6fd058f7 4739 ext4_setup_system_zone(sb);
0390131b 4740 if (sbi->s_journal == NULL)
e2d67052 4741 ext4_commit_super(sb, 1);
0390131b 4742
ac27a0ec
DK
4743#ifdef CONFIG_QUOTA
4744 /* Release old quota file names */
4745 for (i = 0; i < MAXQUOTAS; i++)
4746 if (old_opts.s_qf_names[i] &&
4747 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4748 kfree(old_opts.s_qf_names[i]);
7c319d32
AK
4749 if (enable_quota) {
4750 if (sb_any_quota_suspended(sb))
4751 dquot_resume(sb, -1);
4752 else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4753 EXT4_FEATURE_RO_COMPAT_QUOTA)) {
4754 err = ext4_enable_quotas(sb);
07724f98 4755 if (err)
7c319d32 4756 goto restore_opts;
7c319d32
AK
4757 }
4758 }
ac27a0ec 4759#endif
d4c402d9
CW
4760
4761 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
4762 kfree(orig_data);
ac27a0ec 4763 return 0;
0b8e58a1 4764
ac27a0ec
DK
4765restore_opts:
4766 sb->s_flags = old_sb_flags;
4767 sbi->s_mount_opt = old_opts.s_mount_opt;
a2595b8a 4768 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
ac27a0ec
DK
4769 sbi->s_resuid = old_opts.s_resuid;
4770 sbi->s_resgid = old_opts.s_resgid;
4771 sbi->s_commit_interval = old_opts.s_commit_interval;
30773840
TT
4772 sbi->s_min_batch_time = old_opts.s_min_batch_time;
4773 sbi->s_max_batch_time = old_opts.s_max_batch_time;
ac27a0ec
DK
4774#ifdef CONFIG_QUOTA
4775 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
4776 for (i = 0; i < MAXQUOTAS; i++) {
4777 if (sbi->s_qf_names[i] &&
4778 old_opts.s_qf_names[i] != sbi->s_qf_names[i])
4779 kfree(sbi->s_qf_names[i]);
4780 sbi->s_qf_names[i] = old_opts.s_qf_names[i];
4781 }
4782#endif
d4c402d9 4783 kfree(orig_data);
ac27a0ec
DK
4784 return err;
4785}
4786
2b2d6d01 4787static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
ac27a0ec
DK
4788{
4789 struct super_block *sb = dentry->d_sb;
617ba13b
MC
4790 struct ext4_sb_info *sbi = EXT4_SB(sb);
4791 struct ext4_super_block *es = sbi->s_es;
952fc18e 4792 ext4_fsblk_t overhead = 0;
960cc398 4793 u64 fsid;
d02a9391 4794 s64 bfree;
ac27a0ec 4795
952fc18e
TT
4796 if (!test_opt(sb, MINIX_DF))
4797 overhead = sbi->s_overhead;
ac27a0ec 4798
617ba13b 4799 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 4800 buf->f_bsize = sb->s_blocksize;
b72f78cb 4801 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
57042651
TT
4802 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
4803 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
d02a9391 4804 /* prevent underflow in case that few free space is available */
57042651 4805 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
bd81d8ee
LV
4806 buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
4807 if (buf->f_bfree < ext4_r_blocks_count(es))
ac27a0ec
DK
4808 buf->f_bavail = 0;
4809 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 4810 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
617ba13b 4811 buf->f_namelen = EXT4_NAME_LEN;
960cc398
PE
4812 fsid = le64_to_cpup((void *)es->s_uuid) ^
4813 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
4814 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
4815 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
0b8e58a1 4816
ac27a0ec
DK
4817 return 0;
4818}
4819
0b8e58a1
AD
4820/* Helper function for writing quotas on sync - we need to start transaction
4821 * before quota file is locked for write. Otherwise the are possible deadlocks:
ac27a0ec 4822 * Process 1 Process 2
617ba13b 4823 * ext4_create() quota_sync()
a269eb18 4824 * jbd2_journal_start() write_dquot()
871a2931 4825 * dquot_initialize() down(dqio_mutex)
dab291af 4826 * down(dqio_mutex) jbd2_journal_start()
ac27a0ec
DK
4827 *
4828 */
4829
4830#ifdef CONFIG_QUOTA
4831
4832static inline struct inode *dquot_to_inode(struct dquot *dquot)
4833{
4c376dca 4834 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
ac27a0ec
DK
4835}
4836
617ba13b 4837static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
4838{
4839 int ret, err;
4840 handle_t *handle;
4841 struct inode *inode;
4842
4843 inode = dquot_to_inode(dquot);
617ba13b 4844 handle = ext4_journal_start(inode,
0b8e58a1 4845 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
4846 if (IS_ERR(handle))
4847 return PTR_ERR(handle);
4848 ret = dquot_commit(dquot);
617ba13b 4849 err = ext4_journal_stop(handle);
ac27a0ec
DK
4850 if (!ret)
4851 ret = err;
4852 return ret;
4853}
4854
617ba13b 4855static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
4856{
4857 int ret, err;
4858 handle_t *handle;
4859
617ba13b 4860 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 4861 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
4862 if (IS_ERR(handle))
4863 return PTR_ERR(handle);
4864 ret = dquot_acquire(dquot);
617ba13b 4865 err = ext4_journal_stop(handle);
ac27a0ec
DK
4866 if (!ret)
4867 ret = err;
4868 return ret;
4869}
4870
617ba13b 4871static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
4872{
4873 int ret, err;
4874 handle_t *handle;
4875
617ba13b 4876 handle = ext4_journal_start(dquot_to_inode(dquot),
0b8e58a1 4877 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
4878 if (IS_ERR(handle)) {
4879 /* Release dquot anyway to avoid endless cycle in dqput() */
4880 dquot_release(dquot);
ac27a0ec 4881 return PTR_ERR(handle);
9c3013e9 4882 }
ac27a0ec 4883 ret = dquot_release(dquot);
617ba13b 4884 err = ext4_journal_stop(handle);
ac27a0ec
DK
4885 if (!ret)
4886 ret = err;
4887 return ret;
4888}
4889
617ba13b 4890static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec 4891{
2c8be6b2 4892 /* Are we journaling quotas? */
617ba13b
MC
4893 if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
4894 EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
ac27a0ec 4895 dquot_mark_dquot_dirty(dquot);
617ba13b 4896 return ext4_write_dquot(dquot);
ac27a0ec
DK
4897 } else {
4898 return dquot_mark_dquot_dirty(dquot);
4899 }
4900}
4901
617ba13b 4902static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
4903{
4904 int ret, err;
4905 handle_t *handle;
4906
4907 /* Data block + inode block */
617ba13b 4908 handle = ext4_journal_start(sb->s_root->d_inode, 2);
ac27a0ec
DK
4909 if (IS_ERR(handle))
4910 return PTR_ERR(handle);
4911 ret = dquot_commit_info(sb, type);
617ba13b 4912 err = ext4_journal_stop(handle);
ac27a0ec
DK
4913 if (!ret)
4914 ret = err;
4915 return ret;
4916}
4917
4918/*
4919 * Turn on quotas during mount time - we need to find
4920 * the quota file and such...
4921 */
617ba13b 4922static int ext4_quota_on_mount(struct super_block *sb, int type)
ac27a0ec 4923{
287a8095
CH
4924 return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
4925 EXT4_SB(sb)->s_jquota_fmt, type);
ac27a0ec
DK
4926}
4927
4928/*
4929 * Standard function to be called on quota_on
4930 */
617ba13b 4931static int ext4_quota_on(struct super_block *sb, int type, int format_id,
f00c9e44 4932 struct path *path)
ac27a0ec
DK
4933{
4934 int err;
ac27a0ec
DK
4935
4936 if (!test_opt(sb, QUOTA))
4937 return -EINVAL;
0623543b 4938
ac27a0ec 4939 /* Quotafile not on the same filesystem? */
d8c9584e 4940 if (path->dentry->d_sb != sb)
ac27a0ec 4941 return -EXDEV;
0623543b
JK
4942 /* Journaling quota? */
4943 if (EXT4_SB(sb)->s_qf_names[type]) {
2b2d6d01 4944 /* Quotafile not in fs root? */
f00c9e44 4945 if (path->dentry->d_parent != sb->s_root)
b31e1552
ES
4946 ext4_msg(sb, KERN_WARNING,
4947 "Quota file not on filesystem root. "
4948 "Journaled quota will not work");
2b2d6d01 4949 }
0623543b
JK
4950
4951 /*
4952 * When we journal data on quota file, we have to flush journal to see
4953 * all updates to the file when we bypass pagecache...
4954 */
0390131b 4955 if (EXT4_SB(sb)->s_journal &&
f00c9e44 4956 ext4_should_journal_data(path->dentry->d_inode)) {
0623543b
JK
4957 /*
4958 * We don't need to lock updates but journal_flush() could
4959 * otherwise be livelocked...
4960 */
4961 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
7ffe1ea8 4962 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
0623543b 4963 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
f00c9e44 4964 if (err)
7ffe1ea8 4965 return err;
0623543b
JK
4966 }
4967
f00c9e44 4968 return dquot_quota_on(sb, type, format_id, path);
ac27a0ec
DK
4969}
4970
7c319d32
AK
4971static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
4972 unsigned int flags)
4973{
4974 int err;
4975 struct inode *qf_inode;
4976 unsigned long qf_inums[MAXQUOTAS] = {
4977 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
4978 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
4979 };
4980
4981 BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
4982
4983 if (!qf_inums[type])
4984 return -EPERM;
4985
4986 qf_inode = ext4_iget(sb, qf_inums[type]);
4987 if (IS_ERR(qf_inode)) {
4988 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
4989 return PTR_ERR(qf_inode);
4990 }
4991
4992 err = dquot_enable(qf_inode, type, format_id, flags);
4993 iput(qf_inode);
4994
4995 return err;
4996}
4997
4998/* Enable usage tracking for all quota types. */
4999static int ext4_enable_quotas(struct super_block *sb)
5000{
5001 int type, err = 0;
5002 unsigned long qf_inums[MAXQUOTAS] = {
5003 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5004 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
5005 };
5006
5007 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
5008 for (type = 0; type < MAXQUOTAS; type++) {
5009 if (qf_inums[type]) {
5010 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
5011 DQUOT_USAGE_ENABLED);
5012 if (err) {
5013 ext4_warning(sb,
5014 "Failed to enable quota (type=%d) "
5015 "tracking. Please run e2fsck to fix.",
5016 type);
5017 return err;
5018 }
5019 }
5020 }
5021 return 0;
5022}
5023
5024/*
5025 * quota_on function that is used when QUOTA feature is set.
5026 */
5027static int ext4_quota_on_sysfile(struct super_block *sb, int type,
5028 int format_id)
5029{
5030 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5031 return -EINVAL;
5032
5033 /*
5034 * USAGE was enabled at mount time. Only need to enable LIMITS now.
5035 */
5036 return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
5037}
5038
ca0e05e4
DM
5039static int ext4_quota_off(struct super_block *sb, int type)
5040{
21f97697
JK
5041 struct inode *inode = sb_dqopt(sb)->files[type];
5042 handle_t *handle;
5043
87009d86
DM
5044 /* Force all delayed allocation blocks to be allocated.
5045 * Caller already holds s_umount sem */
5046 if (test_opt(sb, DELALLOC))
ca0e05e4 5047 sync_filesystem(sb);
ca0e05e4 5048
0b268590
AG
5049 if (!inode)
5050 goto out;
5051
21f97697
JK
5052 /* Update modification times of quota files when userspace can
5053 * start looking at them */
5054 handle = ext4_journal_start(inode, 1);
5055 if (IS_ERR(handle))
5056 goto out;
5057 inode->i_mtime = inode->i_ctime = CURRENT_TIME;
5058 ext4_mark_inode_dirty(handle, inode);
5059 ext4_journal_stop(handle);
5060
5061out:
ca0e05e4
DM
5062 return dquot_quota_off(sb, type);
5063}
5064
7c319d32
AK
5065/*
5066 * quota_off function that is used when QUOTA feature is set.
5067 */
5068static int ext4_quota_off_sysfile(struct super_block *sb, int type)
5069{
5070 if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5071 return -EINVAL;
5072
5073 /* Disable only the limits. */
5074 return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
5075}
5076
ac27a0ec
DK
5077/* Read data from quotafile - avoid pagecache and such because we cannot afford
5078 * acquiring the locks... As quota files are never truncated and quota code
25985edc 5079 * itself serializes the operations (and no one else should touch the files)
ac27a0ec 5080 * we don't have to be afraid of races */
617ba13b 5081static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
5082 size_t len, loff_t off)
5083{
5084 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 5085 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
5086 int err = 0;
5087 int offset = off & (sb->s_blocksize - 1);
5088 int tocopy;
5089 size_t toread;
5090 struct buffer_head *bh;
5091 loff_t i_size = i_size_read(inode);
5092
5093 if (off > i_size)
5094 return 0;
5095 if (off+len > i_size)
5096 len = i_size-off;
5097 toread = len;
5098 while (toread > 0) {
5099 tocopy = sb->s_blocksize - offset < toread ?
5100 sb->s_blocksize - offset : toread;
617ba13b 5101 bh = ext4_bread(NULL, inode, blk, 0, &err);
ac27a0ec
DK
5102 if (err)
5103 return err;
5104 if (!bh) /* A hole? */
5105 memset(data, 0, tocopy);
5106 else
5107 memcpy(data, bh->b_data+offset, tocopy);
5108 brelse(bh);
5109 offset = 0;
5110 toread -= tocopy;
5111 data += tocopy;
5112 blk++;
5113 }
5114 return len;
5115}
5116
5117/* Write to quotafile (we know the transaction is already started and has
5118 * enough credits) */
617ba13b 5119static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
5120 const char *data, size_t len, loff_t off)
5121{
5122 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 5123 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
5124 int err = 0;
5125 int offset = off & (sb->s_blocksize - 1);
ac27a0ec
DK
5126 struct buffer_head *bh;
5127 handle_t *handle = journal_current_handle();
5128
0390131b 5129 if (EXT4_SB(sb)->s_journal && !handle) {
b31e1552
ES
5130 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5131 " cancelled because transaction is not started",
9c3013e9
JK
5132 (unsigned long long)off, (unsigned long long)len);
5133 return -EIO;
5134 }
67eeb568
DM
5135 /*
5136 * Since we account only one data block in transaction credits,
5137 * then it is impossible to cross a block boundary.
5138 */
5139 if (sb->s_blocksize - offset < len) {
5140 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5141 " cancelled because not block aligned",
5142 (unsigned long long)off, (unsigned long long)len);
5143 return -EIO;
5144 }
5145
67eeb568
DM
5146 bh = ext4_bread(handle, inode, blk, 1, &err);
5147 if (!bh)
5148 goto out;
62d2b5f2
JK
5149 err = ext4_journal_get_write_access(handle, bh);
5150 if (err) {
5151 brelse(bh);
5152 goto out;
ac27a0ec 5153 }
67eeb568
DM
5154 lock_buffer(bh);
5155 memcpy(bh->b_data+offset, data, len);
5156 flush_dcache_page(bh->b_page);
5157 unlock_buffer(bh);
62d2b5f2 5158 err = ext4_handle_dirty_metadata(handle, NULL, bh);
67eeb568 5159 brelse(bh);
ac27a0ec 5160out:
0b7f7cef 5161 if (err)
ac27a0ec 5162 return err;
67eeb568
DM
5163 if (inode->i_size < off + len) {
5164 i_size_write(inode, off + len);
617ba13b 5165 EXT4_I(inode)->i_disksize = inode->i_size;
21f97697 5166 ext4_mark_inode_dirty(handle, inode);
ac27a0ec 5167 }
67eeb568 5168 return len;
ac27a0ec
DK
5169}
5170
5171#endif
5172
152a0836
AV
5173static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5174 const char *dev_name, void *data)
ac27a0ec 5175{
152a0836 5176 return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
ac27a0ec
DK
5177}
5178
37f328eb 5179#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
24b58424
TT
5180static inline void register_as_ext2(void)
5181{
5182 int err = register_filesystem(&ext2_fs_type);
5183 if (err)
5184 printk(KERN_WARNING
5185 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
5186}
5187
5188static inline void unregister_as_ext2(void)
5189{
5190 unregister_filesystem(&ext2_fs_type);
5191}
2035e776
TT
5192
5193static inline int ext2_feature_set_ok(struct super_block *sb)
5194{
5195 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
5196 return 0;
5197 if (sb->s_flags & MS_RDONLY)
5198 return 1;
5199 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
5200 return 0;
5201 return 1;
5202}
51b7e3c9 5203MODULE_ALIAS("ext2");
24b58424
TT
5204#else
5205static inline void register_as_ext2(void) { }
5206static inline void unregister_as_ext2(void) { }
2035e776 5207static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
24b58424
TT
5208#endif
5209
37f328eb 5210#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
24b58424
TT
5211static inline void register_as_ext3(void)
5212{
5213 int err = register_filesystem(&ext3_fs_type);
5214 if (err)
5215 printk(KERN_WARNING
5216 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
5217}
5218
5219static inline void unregister_as_ext3(void)
5220{
5221 unregister_filesystem(&ext3_fs_type);
5222}
2035e776
TT
5223
5224static inline int ext3_feature_set_ok(struct super_block *sb)
5225{
5226 if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
5227 return 0;
5228 if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
5229 return 0;
5230 if (sb->s_flags & MS_RDONLY)
5231 return 1;
5232 if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
5233 return 0;
5234 return 1;
5235}
51b7e3c9 5236MODULE_ALIAS("ext3");
24b58424
TT
5237#else
5238static inline void register_as_ext3(void) { }
5239static inline void unregister_as_ext3(void) { }
2035e776 5240static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
24b58424
TT
5241#endif
5242
03010a33
TT
5243static struct file_system_type ext4_fs_type = {
5244 .owner = THIS_MODULE,
5245 .name = "ext4",
152a0836 5246 .mount = ext4_mount,
03010a33
TT
5247 .kill_sb = kill_block_super,
5248 .fs_flags = FS_REQUIRES_DEV,
5249};
5250
8f021222 5251static int __init ext4_init_feat_adverts(void)
857ac889
LC
5252{
5253 struct ext4_features *ef;
5254 int ret = -ENOMEM;
5255
5256 ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5257 if (!ef)
5258 goto out;
5259
5260 ef->f_kobj.kset = ext4_kset;
5261 init_completion(&ef->f_kobj_unregister);
5262 ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5263 "features");
5264 if (ret) {
5265 kfree(ef);
5266 goto out;
5267 }
5268
5269 ext4_feat = ef;
5270 ret = 0;
5271out:
5272 return ret;
5273}
5274
8f021222
LC
5275static void ext4_exit_feat_adverts(void)
5276{
5277 kobject_put(&ext4_feat->f_kobj);
5278 wait_for_completion(&ext4_feat->f_kobj_unregister);
5279 kfree(ext4_feat);
5280}
5281
e9e3bcec
ES
5282/* Shared across all ext4 file systems */
5283wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5284struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5285
5dabfc78 5286static int __init ext4_init_fs(void)
ac27a0ec 5287{
e9e3bcec 5288 int i, err;
c9de560d 5289
07c0c5d8
AV
5290 ext4_li_info = NULL;
5291 mutex_init(&ext4_li_mtx);
5292
12e9b892 5293 ext4_check_flag_values();
e9e3bcec
ES
5294
5295 for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5296 mutex_init(&ext4__aio_mutex[i]);
5297 init_waitqueue_head(&ext4__ioend_wq[i]);
5298 }
5299
51865fda 5300 err = ext4_init_es();
6fd058f7
TT
5301 if (err)
5302 return err;
51865fda
ZL
5303
5304 err = ext4_init_pageio();
5305 if (err)
5306 goto out7;
5307
5dabfc78 5308 err = ext4_init_system_zone();
bd2d0210 5309 if (err)
d44651d0 5310 goto out6;
3197ebdb 5311 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
0e376b1e
TT
5312 if (!ext4_kset) {
5313 err = -ENOMEM;
dd68314c 5314 goto out5;
0e376b1e 5315 }
d44651d0 5316 ext4_proc_root = proc_mkdir("fs/ext4", NULL);
857ac889
LC
5317
5318 err = ext4_init_feat_adverts();
dd68314c
TT
5319 if (err)
5320 goto out4;
857ac889 5321
5dabfc78 5322 err = ext4_init_mballoc();
ac27a0ec 5323 if (err)
6fd058f7 5324 goto out3;
c9de560d 5325
5dabfc78 5326 err = ext4_init_xattr();
c9de560d
AT
5327 if (err)
5328 goto out2;
ac27a0ec
DK
5329 err = init_inodecache();
5330 if (err)
5331 goto out1;
24b58424 5332 register_as_ext3();
2035e776 5333 register_as_ext2();
03010a33 5334 err = register_filesystem(&ext4_fs_type);
ac27a0ec
DK
5335 if (err)
5336 goto out;
bfff6873 5337
ac27a0ec
DK
5338 return 0;
5339out:
24b58424
TT
5340 unregister_as_ext2();
5341 unregister_as_ext3();
ac27a0ec
DK
5342 destroy_inodecache();
5343out1:
5dabfc78 5344 ext4_exit_xattr();
c9de560d 5345out2:
5dabfc78 5346 ext4_exit_mballoc();
6fd058f7 5347out3:
8f021222 5348 ext4_exit_feat_adverts();
dd68314c 5349out4:
d44651d0
FJ
5350 if (ext4_proc_root)
5351 remove_proc_entry("fs/ext4", NULL);
6fd058f7 5352 kset_unregister(ext4_kset);
d44651d0 5353out5:
5dabfc78 5354 ext4_exit_system_zone();
d44651d0 5355out6:
5dabfc78 5356 ext4_exit_pageio();
51865fda
ZL
5357out7:
5358 ext4_exit_es();
5359
ac27a0ec
DK
5360 return err;
5361}
5362
5dabfc78 5363static void __exit ext4_exit_fs(void)
ac27a0ec 5364{
bfff6873 5365 ext4_destroy_lazyinit_thread();
24b58424
TT
5366 unregister_as_ext2();
5367 unregister_as_ext3();
03010a33 5368 unregister_filesystem(&ext4_fs_type);
ac27a0ec 5369 destroy_inodecache();
5dabfc78
TT
5370 ext4_exit_xattr();
5371 ext4_exit_mballoc();
8f021222 5372 ext4_exit_feat_adverts();
9f6200bb 5373 remove_proc_entry("fs/ext4", NULL);
3197ebdb 5374 kset_unregister(ext4_kset);
5dabfc78
TT
5375 ext4_exit_system_zone();
5376 ext4_exit_pageio();
ac27a0ec
DK
5377}
5378
5379MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
83982b6f 5380MODULE_DESCRIPTION("Fourth Extended Filesystem");
ac27a0ec 5381MODULE_LICENSE("GPL");
5dabfc78
TT
5382module_init(ext4_init_fs)
5383module_exit(ext4_exit_fs)