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