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