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