Merge tag 'bcachefs-2024-05-24' of https://evilpiepirate.org/git/bcachefs
[linux-2.6-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>
7c0f6ba6 42#include <linux/uaccess.h>
ee73f9a5 43#include <linux/iversion.h>
c83ad55e 44#include <linux/unicode.h>
c6a564ff 45#include <linux/part_stat.h>
bfff6873
LC
46#include <linux/kthread.h>
47#include <linux/freezer.h>
9a089b21 48#include <linux/fsnotify.h>
e5a185c2
LC
49#include <linux/fs_context.h>
50#include <linux/fs_parser.h>
bfff6873 51
3dcf5451 52#include "ext4.h"
4a092d73 53#include "ext4_extents.h" /* Needed for trace points definition */
3dcf5451 54#include "ext4_jbd2.h"
ac27a0ec
DK
55#include "xattr.h"
56#include "acl.h"
3661d286 57#include "mballoc.h"
0c9ec4be 58#include "fsmap.h"
ac27a0ec 59
9bffad1e
TT
60#define CREATE_TRACE_POINTS
61#include <trace/events/ext4.h>
62
0b75a840 63static struct ext4_lazy_init *ext4_li_info;
59ebc7fd 64static DEFINE_MUTEX(ext4_li_mtx);
e294a537 65static struct ratelimit_state ext4_mount_msg_ratelimit;
9f6200bb 66
617ba13b 67static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
ac27a0ec 68 unsigned long journal_devnum);
2adf6da8 69static int ext4_show_options(struct seq_file *seq, struct dentry *root);
2d01ddc8 70static void ext4_update_super(struct super_block *sb);
4392fbc4 71static int ext4_commit_super(struct super_block *sb);
11215630 72static int ext4_mark_recovery_complete(struct super_block *sb,
2b2d6d01 73 struct ext4_super_block *es);
11215630
JK
74static int ext4_clear_journal_err(struct super_block *sb,
75 struct ext4_super_block *es);
617ba13b 76static int ext4_sync_fs(struct super_block *sb, int wait);
2b2d6d01 77static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
c4be0c1d 78static int ext4_unfreeze(struct super_block *sb);
c4be0c1d 79static int ext4_freeze(struct super_block *sb);
2035e776
TT
80static inline int ext2_feature_set_ok(struct super_block *sb);
81static inline int ext3_feature_set_ok(struct super_block *sb);
bfff6873
LC
82static void ext4_destroy_lazyinit_thread(void);
83static void ext4_unregister_li_request(struct super_block *sb);
8f1f7453 84static void ext4_clear_request_list(void);
c6cb7e77
EW
85static struct inode *ext4_get_journal_inode(struct super_block *sb,
86 unsigned int journal_inum);
da812f61 87static int ext4_validate_options(struct fs_context *fc);
b6bd2435
LC
88static int ext4_check_opt_consistency(struct fs_context *fc,
89 struct super_block *sb);
85456054 90static void ext4_apply_options(struct fs_context *fc, struct super_block *sb);
02f960f8 91static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param);
cebe85d5
LC
92static int ext4_get_tree(struct fs_context *fc);
93static int ext4_reconfigure(struct fs_context *fc);
94static void ext4_fc_free(struct fs_context *fc);
95static int ext4_init_fs_context(struct fs_context *fc);
1489dffd 96static void ext4_kill_sb(struct super_block *sb);
cebe85d5 97static const struct fs_parameter_spec ext4_param_specs[];
ac27a0ec 98
e74031fd
JK
99/*
100 * Lock ordering
101 *
e74031fd 102 * page fault path:
d4f5258e
JK
103 * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
104 * -> page lock -> i_data_sem (rw)
e74031fd
JK
105 *
106 * buffered write path:
c1e8d7c6 107 * sb_start_write -> i_mutex -> mmap_lock
e74031fd
JK
108 * sb_start_write -> i_mutex -> transaction start -> page lock ->
109 * i_data_sem (rw)
110 *
111 * truncate:
d4f5258e
JK
112 * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
113 * page lock
114 * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
1d39834f 115 * i_data_sem (rw)
e74031fd
JK
116 *
117 * direct IO:
c1e8d7c6 118 * sb_start_write -> i_mutex -> mmap_lock
1d39834f 119 * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
e74031fd
JK
120 *
121 * writepages:
122 * transaction start -> page lock(s) -> i_data_sem (rw)
123 */
124
02f960f8
LC
125static const struct fs_context_operations ext4_context_ops = {
126 .parse_param = ext4_parse_param,
cebe85d5
LC
127 .get_tree = ext4_get_tree,
128 .reconfigure = ext4_reconfigure,
129 .free = ext4_fc_free,
02f960f8
LC
130};
131
132
c290ea01 133#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
2035e776 134static struct file_system_type ext2_fs_type = {
cebe85d5
LC
135 .owner = THIS_MODULE,
136 .name = "ext2",
137 .init_fs_context = ext4_init_fs_context,
138 .parameters = ext4_param_specs,
1489dffd 139 .kill_sb = ext4_kill_sb,
cebe85d5 140 .fs_flags = FS_REQUIRES_DEV,
2035e776 141};
7f78e035 142MODULE_ALIAS_FS("ext2");
fa7614dd 143MODULE_ALIAS("ext2");
4b41828b 144#define IS_EXT2_SB(sb) ((sb)->s_type == &ext2_fs_type)
2035e776
TT
145#else
146#define IS_EXT2_SB(sb) (0)
147#endif
148
149
ba69f9ab 150static struct file_system_type ext3_fs_type = {
cebe85d5
LC
151 .owner = THIS_MODULE,
152 .name = "ext3",
153 .init_fs_context = ext4_init_fs_context,
154 .parameters = ext4_param_specs,
1489dffd 155 .kill_sb = ext4_kill_sb,
cebe85d5 156 .fs_flags = FS_REQUIRES_DEV,
ba69f9ab 157};
7f78e035 158MODULE_ALIAS_FS("ext3");
fa7614dd 159MODULE_ALIAS("ext3");
4b41828b 160#define IS_EXT3_SB(sb) ((sb)->s_type == &ext3_fs_type)
bd81d8ee 161
fa491b14 162
67c0f556 163static inline void __ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
fa491b14 164 bh_end_io_t *end_io)
165{
166 /*
167 * buffer's verified bit is no longer valid after reading from
168 * disk again due to write out error, clear it to make sure we
169 * recheck the buffer contents.
170 */
171 clear_buffer_verified(bh);
172
173 bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
174 get_bh(bh);
1420c4a5 175 submit_bh(REQ_OP_READ | op_flags, bh);
fa491b14 176}
177
67c0f556 178void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
fa491b14 179 bh_end_io_t *end_io)
180{
181 BUG_ON(!buffer_locked(bh));
182
183 if (ext4_buffer_uptodate(bh)) {
184 unlock_buffer(bh);
185 return;
186 }
187 __ext4_read_bh(bh, op_flags, end_io);
188}
189
67c0f556 190int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags, bh_end_io_t *end_io)
fa491b14 191{
192 BUG_ON(!buffer_locked(bh));
193
194 if (ext4_buffer_uptodate(bh)) {
195 unlock_buffer(bh);
196 return 0;
197 }
198
199 __ext4_read_bh(bh, op_flags, end_io);
200
201 wait_on_buffer(bh);
202 if (buffer_uptodate(bh))
203 return 0;
204 return -EIO;
205}
206
67c0f556 207int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
fa491b14 208{
0b73284c
ZY
209 lock_buffer(bh);
210 if (!wait) {
fa491b14 211 ext4_read_bh_nowait(bh, op_flags, NULL);
212 return 0;
213 }
0b73284c 214 return ext4_read_bh(bh, op_flags, NULL);
fa491b14 215}
216
fb265c9c 217/*
8394a6ab 218 * This works like __bread_gfp() except it uses ERR_PTR for error
fb265c9c
TT
219 * returns. Currently with sb_bread it's impossible to distinguish
220 * between ENOMEM and EIO situations (since both result in a NULL
221 * return.
222 */
8394a6ab 223static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
67c0f556
BVA
224 sector_t block,
225 blk_opf_t op_flags, gfp_t gfp)
fb265c9c 226{
2d069c08 227 struct buffer_head *bh;
228 int ret;
fb265c9c 229
8394a6ab 230 bh = sb_getblk_gfp(sb, block, gfp);
fb265c9c
TT
231 if (bh == NULL)
232 return ERR_PTR(-ENOMEM);
cf2834a5 233 if (ext4_buffer_uptodate(bh))
fb265c9c 234 return bh;
2d069c08 235
236 ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
237 if (ret) {
238 put_bh(bh);
239 return ERR_PTR(ret);
240 }
241 return bh;
fb265c9c
TT
242}
243
8394a6ab 244struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
67c0f556 245 blk_opf_t op_flags)
8394a6ab 246{
224941e8 247 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
8a83ac54
MWO
248 ~__GFP_FS) | __GFP_MOVABLE;
249
250 return __ext4_sb_bread_gfp(sb, block, op_flags, gfp);
8394a6ab 251}
252
253struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
254 sector_t block)
255{
224941e8 256 gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
8a83ac54
MWO
257 ~__GFP_FS);
258
259 return __ext4_sb_bread_gfp(sb, block, 0, gfp);
8394a6ab 260}
261
5df1d412 262void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
263{
e509ad4d 264 struct buffer_head *bh = bdev_getblk(sb->s_bdev, block,
1cbf0a58 265 sb->s_blocksize, GFP_NOWAIT | __GFP_NOWARN);
5df1d412 266
267 if (likely(bh)) {
0b73284c
ZY
268 if (trylock_buffer(bh))
269 ext4_read_bh_nowait(bh, REQ_RAHEAD, NULL);
5df1d412 270 brelse(bh);
271 }
fb265c9c
TT
272}
273
d25425f8
DW
274static int ext4_verify_csum_type(struct super_block *sb,
275 struct ext4_super_block *es)
276{
e2b911c5 277 if (!ext4_has_feature_metadata_csum(sb))
d25425f8
DW
278 return 1;
279
280 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
281}
282
bbc605cd
LC
283__le32 ext4_superblock_csum(struct super_block *sb,
284 struct ext4_super_block *es)
a9c47317
DW
285{
286 struct ext4_sb_info *sbi = EXT4_SB(sb);
287 int offset = offsetof(struct ext4_super_block, s_checksum);
288 __u32 csum;
289
290 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
291
292 return cpu_to_le32(csum);
293}
294
c197855e
SH
295static int ext4_superblock_csum_verify(struct super_block *sb,
296 struct ext4_super_block *es)
a9c47317 297{
9aa5d32b 298 if (!ext4_has_metadata_csum(sb))
a9c47317
DW
299 return 1;
300
301 return es->s_checksum == ext4_superblock_csum(sb, es);
302}
303
06db49e6 304void ext4_superblock_csum_set(struct super_block *sb)
a9c47317 305{
06db49e6
TT
306 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
307
9aa5d32b 308 if (!ext4_has_metadata_csum(sb))
a9c47317
DW
309 return;
310
311 es->s_checksum = ext4_superblock_csum(sb, es);
312}
313
8fadc143
AR
314ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
315 struct ext4_group_desc *bg)
bd81d8ee 316{
3a14589c 317 return le32_to_cpu(bg->bg_block_bitmap_lo) |
8fadc143 318 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 319 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
bd81d8ee
LV
320}
321
8fadc143
AR
322ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
323 struct ext4_group_desc *bg)
bd81d8ee 324{
5272f837 325 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
8fadc143 326 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 327 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
bd81d8ee
LV
328}
329
8fadc143
AR
330ext4_fsblk_t ext4_inode_table(struct super_block *sb,
331 struct ext4_group_desc *bg)
bd81d8ee 332{
5272f837 333 return le32_to_cpu(bg->bg_inode_table_lo) |
8fadc143 334 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 335 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
bd81d8ee
LV
336}
337
021b65bb
TT
338__u32 ext4_free_group_clusters(struct super_block *sb,
339 struct ext4_group_desc *bg)
560671a0
AK
340{
341 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
342 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 343 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
560671a0
AK
344}
345
346__u32 ext4_free_inodes_count(struct super_block *sb,
347 struct ext4_group_desc *bg)
348{
349 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
350 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 351 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
560671a0
AK
352}
353
354__u32 ext4_used_dirs_count(struct super_block *sb,
355 struct ext4_group_desc *bg)
356{
357 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
358 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 359 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
560671a0
AK
360}
361
362__u32 ext4_itable_unused_count(struct super_block *sb,
363 struct ext4_group_desc *bg)
364{
365 return le16_to_cpu(bg->bg_itable_unused_lo) |
366 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
0b8e58a1 367 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
560671a0
AK
368}
369
8fadc143
AR
370void ext4_block_bitmap_set(struct super_block *sb,
371 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 372{
3a14589c 373 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
374 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
375 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
376}
377
8fadc143
AR
378void ext4_inode_bitmap_set(struct super_block *sb,
379 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 380{
5272f837 381 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
8fadc143
AR
382 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
383 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
384}
385
8fadc143
AR
386void ext4_inode_table_set(struct super_block *sb,
387 struct ext4_group_desc *bg, ext4_fsblk_t blk)
bd81d8ee 388{
5272f837 389 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
8fadc143
AR
390 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
391 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
bd81d8ee
LV
392}
393
021b65bb
TT
394void ext4_free_group_clusters_set(struct super_block *sb,
395 struct ext4_group_desc *bg, __u32 count)
560671a0
AK
396{
397 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
398 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
399 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
400}
401
402void ext4_free_inodes_set(struct super_block *sb,
403 struct ext4_group_desc *bg, __u32 count)
404{
405 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
406 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
407 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
408}
409
410void ext4_used_dirs_set(struct super_block *sb,
411 struct ext4_group_desc *bg, __u32 count)
412{
413 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
414 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
415 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
416}
417
418void ext4_itable_unused_set(struct super_block *sb,
419 struct ext4_group_desc *bg, __u32 count)
420{
421 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
422 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
423 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
424}
425
c92dc856 426static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
6a0678a7 427{
6a0678a7
AB
428 now = clamp_val(now, 0, (1ull << 40) - 1);
429
430 *lo = cpu_to_le32(lower_32_bits(now));
431 *hi = upper_32_bits(now);
432}
433
434static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
435{
436 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
437}
438#define ext4_update_tstamp(es, tstamp) \
c92dc856
JK
439 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
440 ktime_get_real_seconds())
6a0678a7
AB
441#define ext4_get_tstamp(es, tstamp) \
442 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
d3d1faf6 443
ff0722de
VK
444#define EXT4_SB_REFRESH_INTERVAL_SEC (3600) /* seconds (1 hour) */
445#define EXT4_SB_REFRESH_INTERVAL_KB (16384) /* kilobytes (16MB) */
446
447/*
448 * The ext4_maybe_update_superblock() function checks and updates the
449 * superblock if needed.
450 *
451 * This function is designed to update the on-disk superblock only under
452 * certain conditions to prevent excessive disk writes and unnecessary
453 * waking of the disk from sleep. The superblock will be updated if:
454 * 1. More than an hour has passed since the last superblock update, and
455 * 2. More than 16MB have been written since the last superblock update.
456 *
457 * @sb: The superblock
458 */
459static void ext4_maybe_update_superblock(struct super_block *sb)
460{
461 struct ext4_sb_info *sbi = EXT4_SB(sb);
462 struct ext4_super_block *es = sbi->s_es;
463 journal_t *journal = sbi->s_journal;
464 time64_t now;
465 __u64 last_update;
466 __u64 lifetime_write_kbytes;
467 __u64 diff_size;
468
469 if (sb_rdonly(sb) || !(sb->s_flags & SB_ACTIVE) ||
470 !journal || (journal->j_flags & JBD2_UNMOUNT))
471 return;
472
473 now = ktime_get_real_seconds();
474 last_update = ext4_get_tstamp(es, s_wtime);
475
476 if (likely(now - last_update < EXT4_SB_REFRESH_INTERVAL_SEC))
477 return;
478
479 lifetime_write_kbytes = sbi->s_kbytes_written +
480 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
481 sbi->s_sectors_written_start) >> 1);
482
483 /* Get the number of kilobytes not written to disk to account
484 * for statistics and compare with a multiple of 16 MB. This
485 * is used to determine when the next superblock commit should
486 * occur (i.e. not more often than once per 16MB if there was
487 * less written in an hour).
488 */
489 diff_size = lifetime_write_kbytes - le64_to_cpu(es->s_kbytes_written);
490
491 if (diff_size > EXT4_SB_REFRESH_INTERVAL_KB)
bb15cea2 492 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
ff0722de
VK
493}
494
18aadd47
BJ
495static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
496{
497 struct super_block *sb = journal->j_private;
498 struct ext4_sb_info *sbi = EXT4_SB(sb);
499 int error = is_journal_aborted(journal);
5d3ee208 500 struct ext4_journal_cb_entry *jce;
18aadd47 501
5d3ee208 502 BUG_ON(txn->t_state == T_FINISHED);
a0154344
DJ
503
504 ext4_process_freed_data(sb, txn->t_tid);
ff0722de 505 ext4_maybe_update_superblock(sb);
a0154344 506
18aadd47 507 spin_lock(&sbi->s_md_lock);
5d3ee208
DM
508 while (!list_empty(&txn->t_private_list)) {
509 jce = list_entry(txn->t_private_list.next,
510 struct ext4_journal_cb_entry, jce_list);
18aadd47
BJ
511 list_del_init(&jce->jce_list);
512 spin_unlock(&sbi->s_md_lock);
513 jce->jce_func(sb, jce, error);
514 spin_lock(&sbi->s_md_lock);
515 }
516 spin_unlock(&sbi->s_md_lock);
517}
1c13d5c0 518
afb585a9
MFO
519/*
520 * This writepage callback for write_cache_pages()
521 * takes care of a few cases after page cleaning.
522 *
523 * write_cache_pages() already checks for dirty pages
524 * and calls clear_page_dirty_for_io(), which we want,
525 * to write protect the pages.
526 *
527 * However, we may have to redirty a page (see below.)
528 */
d585bdbe 529static int ext4_journalled_writepage_callback(struct folio *folio,
afb585a9
MFO
530 struct writeback_control *wbc,
531 void *data)
532{
533 transaction_t *transaction = (transaction_t *) data;
534 struct buffer_head *bh, *head;
535 struct journal_head *jh;
536
d585bdbe 537 bh = head = folio_buffers(folio);
afb585a9
MFO
538 do {
539 /*
540 * We have to redirty a page in these cases:
541 * 1) If buffer is dirty, it means the page was dirty because it
542 * contains a buffer that needs checkpointing. So the dirty bit
543 * needs to be preserved so that checkpointing writes the buffer
544 * properly.
545 * 2) If buffer is not part of the committing transaction
546 * (we may have just accidentally come across this buffer because
547 * inode range tracking is not exact) or if the currently running
548 * transaction already contains this buffer as well, dirty bit
549 * needs to be preserved so that the buffer gets writeprotected
550 * properly on running transaction's commit.
551 */
552 jh = bh2jh(bh);
553 if (buffer_dirty(bh) ||
554 (jh && (jh->b_transaction != transaction ||
555 jh->b_next_transaction))) {
d585bdbe 556 folio_redirty_for_writepage(wbc, folio);
afb585a9
MFO
557 goto out;
558 }
559 } while ((bh = bh->b_this_page) != head);
560
561out:
562 return AOP_WRITEPAGE_ACTIVATE;
563}
564
565static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
566{
567 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
568 struct writeback_control wbc = {
569 .sync_mode = WB_SYNC_ALL,
570 .nr_to_write = LONG_MAX,
571 .range_start = jinode->i_dirty_start,
572 .range_end = jinode->i_dirty_end,
573 };
574
575 return write_cache_pages(mapping, &wbc,
576 ext4_journalled_writepage_callback,
577 jinode->i_transaction);
578}
579
580static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
581{
582 int ret;
583
584 if (ext4_should_journal_data(jinode->i_vfs_inode))
585 ret = ext4_journalled_submit_inode_data_buffers(jinode);
586 else
59205c8d 587 ret = ext4_normal_submit_inode_data_buffers(jinode);
afb585a9
MFO
588 return ret;
589}
590
591static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
592{
593 int ret = 0;
594
595 if (!ext4_should_journal_data(jinode->i_vfs_inode))
596 ret = jbd2_journal_finish_inode_data_buffers(jinode);
597
598 return ret;
599}
600
1dc1097f
JK
601static bool system_going_down(void)
602{
603 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
604 || system_state == SYSTEM_RESTART;
605}
606
02a7780e
JK
607struct ext4_err_translation {
608 int code;
609 int errno;
610};
611
612#define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
613
614static struct ext4_err_translation err_translation[] = {
615 EXT4_ERR_TRANSLATE(EIO),
616 EXT4_ERR_TRANSLATE(ENOMEM),
617 EXT4_ERR_TRANSLATE(EFSBADCRC),
618 EXT4_ERR_TRANSLATE(EFSCORRUPTED),
619 EXT4_ERR_TRANSLATE(ENOSPC),
620 EXT4_ERR_TRANSLATE(ENOKEY),
621 EXT4_ERR_TRANSLATE(EROFS),
622 EXT4_ERR_TRANSLATE(EFBIG),
623 EXT4_ERR_TRANSLATE(EEXIST),
624 EXT4_ERR_TRANSLATE(ERANGE),
625 EXT4_ERR_TRANSLATE(EOVERFLOW),
626 EXT4_ERR_TRANSLATE(EBUSY),
627 EXT4_ERR_TRANSLATE(ENOTDIR),
628 EXT4_ERR_TRANSLATE(ENOTEMPTY),
629 EXT4_ERR_TRANSLATE(ESHUTDOWN),
630 EXT4_ERR_TRANSLATE(EFAULT),
631};
632
633static int ext4_errno_to_code(int errno)
634{
635 int i;
636
637 for (i = 0; i < ARRAY_SIZE(err_translation); i++)
638 if (err_translation[i].errno == errno)
639 return err_translation[i].code;
640 return EXT4_ERR_UNKNOWN;
641}
642
2d01ddc8
JK
643static void save_error_info(struct super_block *sb, int error,
644 __u32 ino, __u64 block,
645 const char *func, unsigned int line)
40676623 646{
c92dc856 647 struct ext4_sb_info *sbi = EXT4_SB(sb);
40676623 648
02a7780e
JK
649 /* We default to EFSCORRUPTED error... */
650 if (error == 0)
651 error = EFSCORRUPTED;
c92dc856
JK
652
653 spin_lock(&sbi->s_error_lock);
654 sbi->s_add_error_count++;
655 sbi->s_last_error_code = error;
656 sbi->s_last_error_line = line;
657 sbi->s_last_error_ino = ino;
658 sbi->s_last_error_block = block;
659 sbi->s_last_error_func = func;
660 sbi->s_last_error_time = ktime_get_real_seconds();
661 if (!sbi->s_first_error_time) {
662 sbi->s_first_error_code = error;
663 sbi->s_first_error_line = line;
664 sbi->s_first_error_ino = ino;
665 sbi->s_first_error_block = block;
666 sbi->s_first_error_func = func;
667 sbi->s_first_error_time = sbi->s_last_error_time;
668 }
669 spin_unlock(&sbi->s_error_lock);
40676623
JK
670}
671
ac27a0ec
DK
672/* Deal with the reporting of failure conditions on a filesystem such as
673 * inconsistencies detected or read IO failures.
674 *
675 * On ext2, we can store the error state of the filesystem in the
617ba13b 676 * superblock. That is not possible on ext4, because we may have other
ac27a0ec
DK
677 * write ordering constraints on the superblock which prevent us from
678 * writing it out straight away; and given that the journal is about to
679 * be aborted, we can't rely on the current, or future, transactions to
680 * write out the superblock safely.
681 *
dab291af 682 * We'll just use the jbd2_journal_abort() error code to record an error in
d6b198bc 683 * the journal instead. On recovery, the journal will complain about
ac27a0ec 684 * that error until we've noted it down and cleared it.
014c9caa
JK
685 *
686 * If force_ro is set, we unconditionally force the filesystem into an
687 * ABORT|READONLY state, unless the error response on the fs has been set to
688 * panic in which case we take the easy way out and panic immediately. This is
689 * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
690 * at a critical moment in log management.
ac27a0ec 691 */
e789ca0c
JK
692static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
693 __u32 ino, __u64 block,
694 const char *func, unsigned int line)
ac27a0ec 695{
b08070ec 696 journal_t *journal = EXT4_SB(sb)->s_journal;
2d01ddc8 697 bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
b08070ec 698
e789ca0c 699 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
327eaf73
TT
700 if (test_opt(sb, WARN_ON_ERROR))
701 WARN_ON_ONCE(1);
702
2d01ddc8 703 if (!continue_fs && !sb_rdonly(sb)) {
95257987 704 set_bit(EXT4_FLAGS_SHUTDOWN, &EXT4_SB(sb)->s_ext4_flags);
2d01ddc8
JK
705 if (journal)
706 jbd2_journal_abort(journal, -EIO);
707 }
708
709 if (!bdev_read_only(sb->s_bdev)) {
e789ca0c 710 save_error_info(sb, error, ino, block, func, line);
2d01ddc8
JK
711 /*
712 * In case the fs should keep running, we need to writeout
713 * superblock through the journal. Due to lock ordering
714 * constraints, it may not be safe to do it right here so we
715 * defer superblock flushing to a workqueue.
716 */
bb9464e0 717 if (continue_fs && journal)
bb15cea2 718 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
2d01ddc8
JK
719 else
720 ext4_commit_super(sb);
721 }
e789ca0c 722
1dc1097f
JK
723 /*
724 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
725 * could panic during 'reboot -f' as the underlying device got already
726 * disabled.
727 */
014c9caa 728 if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
617ba13b 729 panic("EXT4-fs (device %s): panic forced after error\n",
ac27a0ec 730 sb->s_id);
4327ba52 731 }
ac2f7ca5
YB
732
733 if (sb_rdonly(sb) || continue_fs)
734 return;
735
014c9caa
JK
736 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
737 /*
738 * Make sure updated value of ->s_mount_flags will be visible before
739 * ->s_flags update
740 */
741 smp_wmb();
742 sb->s_flags |= SB_RDONLY;
ac27a0ec
DK
743}
744
bb15cea2 745static void update_super_work(struct work_struct *work)
c92dc856
JK
746{
747 struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
bb15cea2 748 s_sb_upd_work);
2d01ddc8
JK
749 journal_t *journal = sbi->s_journal;
750 handle_t *handle;
c92dc856 751
2d01ddc8
JK
752 /*
753 * If the journal is still running, we have to write out superblock
754 * through the journal to avoid collisions of other journalled sb
755 * updates.
756 *
757 * We use directly jbd2 functions here to avoid recursing back into
758 * ext4 error handling code during handling of previous errors.
759 */
760 if (!sb_rdonly(sbi->s_sb) && journal) {
558d6450 761 struct buffer_head *sbh = sbi->s_sbh;
ee6a12d0
TT
762 bool call_notify_err = false;
763
2d01ddc8
JK
764 handle = jbd2_journal_start(journal, 1);
765 if (IS_ERR(handle))
766 goto write_directly;
558d6450 767 if (jbd2_journal_get_write_access(handle, sbh)) {
2d01ddc8
JK
768 jbd2_journal_stop(handle);
769 goto write_directly;
770 }
ff0722de
VK
771
772 if (sbi->s_add_error_count > 0)
773 call_notify_err = true;
774
2d01ddc8 775 ext4_update_super(sbi->s_sb);
558d6450
YB
776 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
777 ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
778 "superblock detected");
779 clear_buffer_write_io_error(sbh);
780 set_buffer_uptodate(sbh);
781 }
782
783 if (jbd2_journal_dirty_metadata(handle, sbh)) {
2d01ddc8
JK
784 jbd2_journal_stop(handle);
785 goto write_directly;
786 }
787 jbd2_journal_stop(handle);
ff0722de
VK
788
789 if (call_notify_err)
790 ext4_notify_error_sysfs(sbi);
791
2d01ddc8
JK
792 return;
793 }
794write_directly:
795 /*
796 * Write through journal failed. Write sb directly to get error info
797 * out and hope for the best.
798 */
4392fbc4 799 ext4_commit_super(sbi->s_sb);
d578b994 800 ext4_notify_error_sysfs(sbi);
ac27a0ec
DK
801}
802
efbed4dc
TT
803#define ext4_error_ratelimit(sb) \
804 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
805 "EXT4-fs error")
806
12062ddd 807void __ext4_error(struct super_block *sb, const char *function,
014c9caa 808 unsigned int line, bool force_ro, int error, __u64 block,
54d3adbc 809 const char *fmt, ...)
ac27a0ec 810{
0ff2ea7d 811 struct va_format vaf;
ac27a0ec
DK
812 va_list args;
813
eb8ab444 814 if (unlikely(ext4_forced_shutdown(sb)))
0db1ff22
TT
815 return;
816
ccf0f32a 817 trace_ext4_error(sb, function, line);
efbed4dc
TT
818 if (ext4_error_ratelimit(sb)) {
819 va_start(args, fmt);
820 vaf.fmt = fmt;
821 vaf.va = &args;
822 printk(KERN_CRIT
823 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
824 sb->s_id, function, line, current->comm, &vaf);
825 va_end(args);
826 }
9a089b21
GKB
827 fsnotify_sb_error(sb, NULL, error ? error : EFSCORRUPTED);
828
e789ca0c 829 ext4_handle_error(sb, force_ro, error, 0, block, function, line);
ac27a0ec
DK
830}
831
e7c96e8e 832void __ext4_error_inode(struct inode *inode, const char *function,
54d3adbc 833 unsigned int line, ext4_fsblk_t block, int error,
e7c96e8e 834 const char *fmt, ...)
273df556
FM
835{
836 va_list args;
f7c21177 837 struct va_format vaf;
273df556 838
eb8ab444 839 if (unlikely(ext4_forced_shutdown(inode->i_sb)))
0db1ff22
TT
840 return;
841
ccf0f32a 842 trace_ext4_error(inode->i_sb, function, line);
efbed4dc
TT
843 if (ext4_error_ratelimit(inode->i_sb)) {
844 va_start(args, fmt);
845 vaf.fmt = fmt;
846 vaf.va = &args;
847 if (block)
848 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
849 "inode #%lu: block %llu: comm %s: %pV\n",
850 inode->i_sb->s_id, function, line, inode->i_ino,
851 block, current->comm, &vaf);
852 else
853 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
854 "inode #%lu: comm %s: %pV\n",
855 inode->i_sb->s_id, function, line, inode->i_ino,
856 current->comm, &vaf);
857 va_end(args);
858 }
9a089b21
GKB
859 fsnotify_sb_error(inode->i_sb, inode, error ? error : EFSCORRUPTED);
860
e789ca0c
JK
861 ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
862 function, line);
273df556
FM
863}
864
e7c96e8e
JP
865void __ext4_error_file(struct file *file, const char *function,
866 unsigned int line, ext4_fsblk_t block,
867 const char *fmt, ...)
273df556
FM
868{
869 va_list args;
f7c21177 870 struct va_format vaf;
496ad9aa 871 struct inode *inode = file_inode(file);
273df556
FM
872 char pathname[80], *path;
873
eb8ab444 874 if (unlikely(ext4_forced_shutdown(inode->i_sb)))
0db1ff22
TT
875 return;
876
ccf0f32a 877 trace_ext4_error(inode->i_sb, function, line);
efbed4dc 878 if (ext4_error_ratelimit(inode->i_sb)) {
9bf39ab2 879 path = file_path(file, pathname, sizeof(pathname));
efbed4dc
TT
880 if (IS_ERR(path))
881 path = "(unknown)";
882 va_start(args, fmt);
883 vaf.fmt = fmt;
884 vaf.va = &args;
885 if (block)
886 printk(KERN_CRIT
887 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
888 "block %llu: comm %s: path %s: %pV\n",
889 inode->i_sb->s_id, function, line, inode->i_ino,
890 block, current->comm, path, &vaf);
891 else
892 printk(KERN_CRIT
893 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
894 "comm %s: path %s: %pV\n",
895 inode->i_sb->s_id, function, line, inode->i_ino,
896 current->comm, path, &vaf);
897 va_end(args);
898 }
9a089b21
GKB
899 fsnotify_sb_error(inode->i_sb, inode, EFSCORRUPTED);
900
e789ca0c
JK
901 ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
902 function, line);
273df556
FM
903}
904
722887dd
TT
905const char *ext4_decode_error(struct super_block *sb, int errno,
906 char nbuf[16])
ac27a0ec
DK
907{
908 char *errstr = NULL;
909
910 switch (errno) {
6a797d27
DW
911 case -EFSCORRUPTED:
912 errstr = "Corrupt filesystem";
913 break;
914 case -EFSBADCRC:
915 errstr = "Filesystem failed CRC";
916 break;
ac27a0ec
DK
917 case -EIO:
918 errstr = "IO failure";
919 break;
920 case -ENOMEM:
921 errstr = "Out of memory";
922 break;
923 case -EROFS:
78f1ddbb
TT
924 if (!sb || (EXT4_SB(sb)->s_journal &&
925 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
ac27a0ec
DK
926 errstr = "Journal has aborted";
927 else
928 errstr = "Readonly filesystem";
929 break;
930 default:
931 /* If the caller passed in an extra buffer for unknown
932 * errors, textualise them now. Else we just return
933 * NULL. */
934 if (nbuf) {
935 /* Check for truncated error codes... */
936 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
937 errstr = nbuf;
938 }
939 break;
940 }
941
942 return errstr;
943}
944
617ba13b 945/* __ext4_std_error decodes expected errors from journaling functions
ac27a0ec
DK
946 * automatically and invokes the appropriate error response. */
947
c398eda0
TT
948void __ext4_std_error(struct super_block *sb, const char *function,
949 unsigned int line, int errno)
ac27a0ec
DK
950{
951 char nbuf[16];
952 const char *errstr;
953
eb8ab444 954 if (unlikely(ext4_forced_shutdown(sb)))
0db1ff22
TT
955 return;
956
ac27a0ec
DK
957 /* Special case: if the error is EROFS, and we're not already
958 * inside a transaction, then there's really no point in logging
959 * an error. */
bc98a42c 960 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
ac27a0ec
DK
961 return;
962
efbed4dc
TT
963 if (ext4_error_ratelimit(sb)) {
964 errstr = ext4_decode_error(sb, errno, nbuf);
965 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
966 sb->s_id, function, line, errstr);
967 }
9a089b21 968 fsnotify_sb_error(sb, NULL, errno ? errno : EFSCORRUPTED);
ac27a0ec 969
e789ca0c 970 ext4_handle_error(sb, false, -errno, 0, 0, function, line);
ac27a0ec
DK
971}
972
e7c96e8e
JP
973void __ext4_msg(struct super_block *sb,
974 const char *prefix, const char *fmt, ...)
b31e1552 975{
0ff2ea7d 976 struct va_format vaf;
b31e1552
ES
977 va_list args;
978
da812f61
LC
979 if (sb) {
980 atomic_inc(&EXT4_SB(sb)->s_msg_count);
981 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state),
982 "EXT4-fs"))
983 return;
984 }
efbed4dc 985
b31e1552 986 va_start(args, fmt);
0ff2ea7d
JP
987 vaf.fmt = fmt;
988 vaf.va = &args;
da812f61
LC
989 if (sb)
990 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
991 else
992 printk("%sEXT4-fs: %pV\n", prefix, &vaf);
b31e1552
ES
993 va_end(args);
994}
995
1cf006ed
DM
996static int ext4_warning_ratelimit(struct super_block *sb)
997{
998 atomic_inc(&EXT4_SB(sb)->s_warning_count);
999 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
1000 "EXT4-fs warning");
1001}
b03a2f7e 1002
12062ddd 1003void __ext4_warning(struct super_block *sb, const char *function,
c398eda0 1004 unsigned int line, const char *fmt, ...)
ac27a0ec 1005{
0ff2ea7d 1006 struct va_format vaf;
ac27a0ec
DK
1007 va_list args;
1008
b03a2f7e 1009 if (!ext4_warning_ratelimit(sb))
efbed4dc
TT
1010 return;
1011
ac27a0ec 1012 va_start(args, fmt);
0ff2ea7d
JP
1013 vaf.fmt = fmt;
1014 vaf.va = &args;
1015 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
1016 sb->s_id, function, line, &vaf);
ac27a0ec
DK
1017 va_end(args);
1018}
1019
b03a2f7e
AD
1020void __ext4_warning_inode(const struct inode *inode, const char *function,
1021 unsigned int line, const char *fmt, ...)
1022{
1023 struct va_format vaf;
1024 va_list args;
1025
1026 if (!ext4_warning_ratelimit(inode->i_sb))
1027 return;
1028
1029 va_start(args, fmt);
1030 vaf.fmt = fmt;
1031 vaf.va = &args;
1032 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
1033 "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
1034 function, line, inode->i_ino, current->comm, &vaf);
1035 va_end(args);
1036}
1037
e29136f8
TT
1038void __ext4_grp_locked_error(const char *function, unsigned int line,
1039 struct super_block *sb, ext4_group_t grp,
1040 unsigned long ino, ext4_fsblk_t block,
1041 const char *fmt, ...)
5d1b1b3f
AK
1042__releases(bitlock)
1043__acquires(bitlock)
1044{
0ff2ea7d 1045 struct va_format vaf;
5d1b1b3f 1046 va_list args;
5d1b1b3f 1047
eb8ab444 1048 if (unlikely(ext4_forced_shutdown(sb)))
0db1ff22
TT
1049 return;
1050
ccf0f32a 1051 trace_ext4_error(sb, function, line);
efbed4dc
TT
1052 if (ext4_error_ratelimit(sb)) {
1053 va_start(args, fmt);
1054 vaf.fmt = fmt;
1055 vaf.va = &args;
1056 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
1057 sb->s_id, function, line, grp);
1058 if (ino)
1059 printk(KERN_CONT "inode %lu: ", ino);
1060 if (block)
1061 printk(KERN_CONT "block %llu:",
1062 (unsigned long long) block);
1063 printk(KERN_CONT "%pV\n", &vaf);
1064 va_end(args);
1065 }
5d1b1b3f
AK
1066
1067 if (test_opt(sb, ERRORS_CONT)) {
c92dc856
JK
1068 if (test_opt(sb, WARN_ON_ERROR))
1069 WARN_ON_ONCE(1);
e789ca0c 1070 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
2d01ddc8
JK
1071 if (!bdev_read_only(sb->s_bdev)) {
1072 save_error_info(sb, EFSCORRUPTED, ino, block, function,
1073 line);
bb15cea2 1074 schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
2d01ddc8 1075 }
5d1b1b3f
AK
1076 return;
1077 }
1078 ext4_unlock_group(sb, grp);
e789ca0c 1079 ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
5d1b1b3f
AK
1080 /*
1081 * We only get here in the ERRORS_RO case; relocking the group
1082 * may be dangerous, but nothing bad will happen since the
1083 * filesystem will have already been marked read/only and the
1084 * journal has been aborted. We return 1 as a hint to callers
1085 * who might what to use the return value from
25985edc 1086 * ext4_grp_locked_error() to distinguish between the
5d1b1b3f
AK
1087 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
1088 * aggressively from the ext4 function in question, with a
1089 * more appropriate error code.
1090 */
1091 ext4_lock_group(sb, grp);
1092 return;
1093}
1094
db79e6d1
WS
1095void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1096 ext4_group_t group,
1097 unsigned int flags)
1098{
1099 struct ext4_sb_info *sbi = EXT4_SB(sb);
1100 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1101 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
9af0b3d1
WS
1102 int ret;
1103
5354b2af
TT
1104 if (!grp || !gdp)
1105 return;
9af0b3d1
WS
1106 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
1107 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1108 &grp->bb_state);
1109 if (!ret)
1110 percpu_counter_sub(&sbi->s_freeclusters_counter,
1111 grp->bb_free);
db79e6d1
WS
1112 }
1113
9af0b3d1
WS
1114 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
1115 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
1116 &grp->bb_state);
1117 if (!ret && gdp) {
db79e6d1
WS
1118 int count;
1119
1120 count = ext4_free_inodes_count(sb, gdp);
1121 percpu_counter_sub(&sbi->s_freeinodes_counter,
1122 count);
1123 }
db79e6d1
WS
1124 }
1125}
1126
617ba13b 1127void ext4_update_dynamic_rev(struct super_block *sb)
ac27a0ec 1128{
617ba13b 1129 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
ac27a0ec 1130
617ba13b 1131 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
ac27a0ec
DK
1132 return;
1133
12062ddd 1134 ext4_warning(sb,
ac27a0ec
DK
1135 "updating to rev %d because of new feature flag, "
1136 "running e2fsck is recommended",
617ba13b 1137 EXT4_DYNAMIC_REV);
ac27a0ec 1138
617ba13b
MC
1139 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1140 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1141 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
ac27a0ec
DK
1142 /* leave es->s_feature_*compat flags alone */
1143 /* es->s_uuid will be set by e2fsck if empty */
1144
1145 /*
1146 * The rest of the superblock fields should be zero, and if not it
1147 * means they are likely already in use, so leave them alone. We
1148 * can leave it up to e2fsck to clean up any inconsistencies there.
1149 */
1150}
1151
ac27a0ec
DK
1152static inline struct inode *orphan_list_entry(struct list_head *l)
1153{
617ba13b 1154 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
ac27a0ec
DK
1155}
1156
617ba13b 1157static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
ac27a0ec
DK
1158{
1159 struct list_head *l;
1160
b31e1552
ES
1161 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1162 le32_to_cpu(sbi->s_es->s_last_orphan));
ac27a0ec
DK
1163
1164 printk(KERN_ERR "sb_info orphan list:\n");
1165 list_for_each(l, &sbi->s_orphan) {
1166 struct inode *inode = orphan_list_entry(l);
1167 printk(KERN_ERR " "
1168 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1169 inode->i_sb->s_id, inode->i_ino, inode,
1170 inode->i_mode, inode->i_nlink,
1171 NEXT_ORPHAN(inode));
1172 }
1173}
1174
957153fc
JK
1175#ifdef CONFIG_QUOTA
1176static int ext4_quota_off(struct super_block *sb, int type);
1177
f3c1c42e 1178static inline void ext4_quotas_off(struct super_block *sb, int type)
957153fc 1179{
f3c1c42e 1180 BUG_ON(type > EXT4_MAXQUOTAS);
957153fc 1181
964edf66 1182 /* Use our quota_off function to clear inode flags etc. */
f3c1c42e 1183 for (type--; type >= 0; type--)
964edf66 1184 ext4_quota_off(sb, type);
957153fc 1185}
33458eab
TT
1186
1187/*
1188 * This is a helper function which is used in the mount/remount
1189 * codepaths (which holds s_umount) to fetch the quota file name.
1190 */
1191static inline char *get_qf_name(struct super_block *sb,
1192 struct ext4_sb_info *sbi,
1193 int type)
1194{
1195 return rcu_dereference_protected(sbi->s_qf_names[type],
1196 lockdep_is_held(&sb->s_umount));
1197}
957153fc 1198#else
f3c1c42e 1199static inline void ext4_quotas_off(struct super_block *sb, int type)
957153fc
JK
1200{
1201}
1202#endif
1203
1f79467c
JY
1204static int ext4_percpu_param_init(struct ext4_sb_info *sbi)
1205{
1206 ext4_fsblk_t block;
1207 int err;
1208
1209 block = ext4_count_free_clusters(sbi->s_sb);
1210 ext4_free_blocks_count_set(sbi->s_es, EXT4_C2B(sbi, block));
1211 err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
1212 GFP_KERNEL);
1213 if (!err) {
1214 unsigned long freei = ext4_count_free_inodes(sbi->s_sb);
1215 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
1216 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
1217 GFP_KERNEL);
1218 }
1219 if (!err)
1220 err = percpu_counter_init(&sbi->s_dirs_counter,
1221 ext4_count_dirs(sbi->s_sb), GFP_KERNEL);
1222 if (!err)
1223 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
1224 GFP_KERNEL);
1225 if (!err)
1226 err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
1227 GFP_KERNEL);
1228 if (!err)
1229 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
1230
1231 if (err)
1232 ext4_msg(sbi->s_sb, KERN_ERR, "insufficient memory");
1233
1234 return err;
1235}
1236
1237static void ext4_percpu_param_destroy(struct ext4_sb_info *sbi)
1238{
1239 percpu_counter_destroy(&sbi->s_freeclusters_counter);
1240 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1241 percpu_counter_destroy(&sbi->s_dirs_counter);
1242 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
1243 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
1244 percpu_free_rwsem(&sbi->s_writepages_rwsem);
1245}
1246
6ef68498
JY
1247static void ext4_group_desc_free(struct ext4_sb_info *sbi)
1248{
1249 struct buffer_head **group_desc;
1250 int i;
1251
1252 rcu_read_lock();
1253 group_desc = rcu_dereference(sbi->s_group_desc);
1254 for (i = 0; i < sbi->s_gdb_count; i++)
1255 brelse(group_desc[i]);
1256 kvfree(group_desc);
1257 rcu_read_unlock();
1258}
1259
dcbf8758
JY
1260static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
1261{
1262 struct flex_groups **flex_groups;
1263 int i;
1264
1265 rcu_read_lock();
1266 flex_groups = rcu_dereference(sbi->s_flex_groups);
1267 if (flex_groups) {
1268 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1269 kvfree(flex_groups[i]);
1270 kvfree(flex_groups);
1271 }
1272 rcu_read_unlock();
1273}
1274
2b2d6d01 1275static void ext4_put_super(struct super_block *sb)
ac27a0ec 1276{
617ba13b
MC
1277 struct ext4_sb_info *sbi = EXT4_SB(sb);
1278 struct ext4_super_block *es = sbi->s_es;
97abd7d4 1279 int aborted = 0;
856dd6c5 1280 int err;
ac27a0ec 1281
5e47868f
RH
1282 /*
1283 * Unregister sysfs before destroying jbd2 journal.
1284 * Since we could still access attr_journal_task attribute via sysfs
1285 * path which could have sbi->s_journal->j_task as NULL
bb15cea2 1286 * Unregister sysfs before flush sbi->s_sb_upd_work.
b98535d0
YB
1287 * Since user may read /proc/fs/ext4/xx/mb_groups during umount, If
1288 * read metadata verify failed then will queue error work.
bb15cea2 1289 * update_super_work will call start_this_handle may trigger
b98535d0 1290 * BUG_ON.
5e47868f
RH
1291 */
1292 ext4_unregister_sysfs(sb);
1293
4808cb5b 1294 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs unmount"))
bb0fbc78
LC
1295 ext4_msg(sb, KERN_INFO, "unmounting filesystem %pU.",
1296 &sb->s_uuid);
4808cb5b 1297
b98535d0 1298 ext4_unregister_li_request(sb);
f3c1c42e 1299 ext4_quotas_off(sb, EXT4_MAXQUOTAS);
b98535d0 1300
bb15cea2 1301 flush_work(&sbi->s_sb_upd_work);
b98535d0
YB
1302 destroy_workqueue(sbi->rsv_conversion_wq);
1303 ext4_release_orphan_info(sb);
1304
0390131b 1305 if (sbi->s_journal) {
97abd7d4 1306 aborted = is_journal_aborted(sbi->s_journal);
0390131b
FM
1307 err = jbd2_journal_destroy(sbi->s_journal);
1308 sbi->s_journal = NULL;
878520ac 1309 if ((err < 0) && !aborted) {
54d3adbc 1310 ext4_abort(sb, -err, "Couldn't clean up the journal");
878520ac 1311 }
0390131b 1312 }
d4edac31 1313
d3922a77 1314 ext4_es_unregister_shrinker(sbi);
292a089d 1315 timer_shutdown_sync(&sbi->s_err_report);
d4edac31
JB
1316 ext4_release_system_zone(sb);
1317 ext4_mb_release(sb);
1318 ext4_ext_release(sb);
d4edac31 1319
bc98a42c 1320 if (!sb_rdonly(sb) && !aborted) {
e2b911c5 1321 ext4_clear_feature_journal_needs_recovery(sb);
02f310fc 1322 ext4_clear_feature_orphan_present(sb);
ac27a0ec 1323 es->s_state = cpu_to_le16(sbi->s_mount_state);
ac27a0ec 1324 }
bc98a42c 1325 if (!sb_rdonly(sb))
4392fbc4 1326 ext4_commit_super(sb);
a8e25a83 1327
6ef68498 1328 ext4_group_desc_free(sbi);
dcbf8758 1329 ext4_flex_groups_free(sbi);
1f79467c 1330 ext4_percpu_param_destroy(sbi);
ac27a0ec 1331#ifdef CONFIG_QUOTA
856dd6c5 1332 for (int i = 0; i < EXT4_MAXQUOTAS; i++)
33458eab 1333 kfree(get_qf_name(sb, sbi, i));
ac27a0ec
DK
1334#endif
1335
1336 /* Debugging code just in case the in-memory inode orphan list
1337 * isn't empty. The on-disk one can be non-empty if we've
1338 * detected an error and taken the fs readonly, but the
1339 * in-memory list had better be clean by this point. */
1340 if (!list_empty(&sbi->s_orphan))
1341 dump_orphan_list(sb, sbi);
837c23fb 1342 ASSERT(list_empty(&sbi->s_orphan));
ac27a0ec 1343
89d96a6f 1344 sync_blockdev(sb->s_bdev);
f98393a6 1345 invalidate_bdev(sb->s_bdev);
61ead714 1346 if (sbi->s_journal_bdev_file) {
1489dffd
CH
1347 /*
1348 * Invalidate the journal device's buffers. We don't want them
1349 * floating about in memory - the physical journal device may
1350 * hotswapped, and it breaks the `ro-after' testing code.
1351 */
61ead714
CB
1352 sync_blockdev(file_bdev(sbi->s_journal_bdev_file));
1353 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
ac27a0ec 1354 }
50c15df6
CX
1355
1356 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1357 sbi->s_ea_inode_cache = NULL;
1358
1359 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1360 sbi->s_ea_block_cache = NULL;
1361
618f0031
PS
1362 ext4_stop_mmpd(sbi);
1363
9060dd2c 1364 brelse(sbi->s_sbh);
ac27a0ec 1365 sb->s_fs_info = NULL;
3197ebdb
TT
1366 /*
1367 * Now that we are completely done shutting down the
1368 * superblock, we need to actually destroy the kobject.
1369 */
3197ebdb
TT
1370 kobject_put(&sbi->s_kobj);
1371 wait_for_completion(&sbi->s_kobj_unregister);
0441984a
DW
1372 if (sbi->s_chksum_driver)
1373 crypto_free_shash(sbi->s_chksum_driver);
705895b6 1374 kfree(sbi->s_blockgroup_lock);
8012b866 1375 fs_put_dax(sbi->s_daxdev, NULL);
ac4acb1f 1376 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
5298d4bf 1377#if IS_ENABLED(CONFIG_UNICODE)
f8f4acb6 1378 utf8_unload(sb->s_encoding);
c83ad55e 1379#endif
ac27a0ec 1380 kfree(sbi);
ac27a0ec
DK
1381}
1382
e18b890b 1383static struct kmem_cache *ext4_inode_cachep;
ac27a0ec
DK
1384
1385/*
1386 * Called inside transaction, so use GFP_NOFS
1387 */
617ba13b 1388static struct inode *ext4_alloc_inode(struct super_block *sb)
ac27a0ec 1389{
617ba13b 1390 struct ext4_inode_info *ei;
ac27a0ec 1391
fd60b288 1392 ei = alloc_inode_sb(sb, ext4_inode_cachep, GFP_NOFS);
ac27a0ec
DK
1393 if (!ei)
1394 return NULL;
0b8e58a1 1395
ee73f9a5 1396 inode_set_iversion(&ei->vfs_inode, 1);
7ea71af9 1397 ei->i_flags = 0;
202ee5df 1398 spin_lock_init(&ei->i_raw_lock);
38727786 1399 ei->i_prealloc_node = RB_ROOT;
27bc446e 1400 atomic_set(&ei->i_prealloc_active, 0);
38727786 1401 rwlock_init(&ei->i_prealloc_lock);
9a26b661
ZL
1402 ext4_es_init_tree(&ei->i_es_tree);
1403 rwlock_init(&ei->i_es_lock);
edaa53ca 1404 INIT_LIST_HEAD(&ei->i_es_list);
eb68d0e2 1405 ei->i_es_all_nr = 0;
edaa53ca 1406 ei->i_es_shk_nr = 0;
dd475925 1407 ei->i_es_shrink_lblk = 0;
d2a17637 1408 ei->i_reserved_data_blocks = 0;
d2a17637 1409 spin_lock_init(&(ei->i_block_reservation_lock));
1dc0aa46 1410 ext4_init_pending_tree(&ei->i_pending_tree);
a9e7f447
DM
1411#ifdef CONFIG_QUOTA
1412 ei->i_reserved_quota = 0;
96c7e0d9 1413 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
a9e7f447 1414#endif
8aefcd55 1415 ei->jinode = NULL;
2e8fa54e 1416 INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
744692dc 1417 spin_lock_init(&ei->i_completed_io_lock);
b436b9be
JK
1418 ei->i_sync_tid = 0;
1419 ei->i_datasync_tid = 0;
e27f41e1 1420 atomic_set(&ei->i_unwritten, 0);
2e8fa54e 1421 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
aa75f4d3
HS
1422 ext4_fc_init_inode(&ei->vfs_inode);
1423 mutex_init(&ei->i_fc_lock);
ac27a0ec
DK
1424 return &ei->vfs_inode;
1425}
1426
7ff9c073
TT
1427static int ext4_drop_inode(struct inode *inode)
1428{
1429 int drop = generic_drop_inode(inode);
1430
29b3692e
EB
1431 if (!drop)
1432 drop = fscrypt_drop_inode(inode);
1433
7ff9c073
TT
1434 trace_ext4_drop_inode(inode, drop);
1435 return drop;
1436}
1437
94053139 1438static void ext4_free_in_core_inode(struct inode *inode)
fa0d7e3d 1439{
2c58d548 1440 fscrypt_free_inode(inode);
aa75f4d3
HS
1441 if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1442 pr_warn("%s: inode %ld still in fc list",
1443 __func__, inode->i_ino);
1444 }
fa0d7e3d
NP
1445 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1446}
1447
617ba13b 1448static void ext4_destroy_inode(struct inode *inode)
ac27a0ec 1449{
9f7dd93d 1450 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
b31e1552
ES
1451 ext4_msg(inode->i_sb, KERN_ERR,
1452 "Inode %lu (%p): orphan list check failed!",
1453 inode->i_ino, EXT4_I(inode));
9f7dd93d
VA
1454 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1455 EXT4_I(inode), sizeof(struct ext4_inode_info),
1456 true);
1457 dump_stack();
1458 }
6fed8395
JX
1459
1460 if (EXT4_I(inode)->i_reserved_data_blocks)
1461 ext4_msg(inode->i_sb, KERN_ERR,
1462 "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
1463 inode->i_ino, EXT4_I(inode),
1464 EXT4_I(inode)->i_reserved_data_blocks);
ac27a0ec
DK
1465}
1466
f5db130d
CH
1467static void ext4_shutdown(struct super_block *sb)
1468{
1469 ext4_force_shutdown(sb, EXT4_GOING_FLAGS_NOLOGFLUSH);
1470}
1471
51cc5068 1472static void init_once(void *foo)
ac27a0ec 1473{
c30365b9 1474 struct ext4_inode_info *ei = foo;
ac27a0ec 1475
a35afb83 1476 INIT_LIST_HEAD(&ei->i_orphan);
a35afb83 1477 init_rwsem(&ei->xattr_sem);
0e855ac8 1478 init_rwsem(&ei->i_data_sem);
a35afb83 1479 inode_init_once(&ei->vfs_inode);
aa75f4d3 1480 ext4_fc_init_inode(&ei->vfs_inode);
ac27a0ec
DK
1481}
1482
e67bc2b3 1483static int __init init_inodecache(void)
ac27a0ec 1484{
f8dd7c70
DW
1485 ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1486 sizeof(struct ext4_inode_info), 0,
f88c3fb8 1487 SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT,
f8dd7c70
DW
1488 offsetof(struct ext4_inode_info, i_data),
1489 sizeof_field(struct ext4_inode_info, i_data),
1490 init_once);
617ba13b 1491 if (ext4_inode_cachep == NULL)
ac27a0ec
DK
1492 return -ENOMEM;
1493 return 0;
1494}
1495
1496static void destroy_inodecache(void)
1497{
8c0a8537
KS
1498 /*
1499 * Make sure all delayed rcu free inodes are flushed before we
1500 * destroy cache.
1501 */
1502 rcu_barrier();
617ba13b 1503 kmem_cache_destroy(ext4_inode_cachep);
ac27a0ec
DK
1504}
1505
0930fcc1 1506void ext4_clear_inode(struct inode *inode)
ac27a0ec 1507{
aa75f4d3 1508 ext4_fc_del(inode);
0930fcc1 1509 invalidate_inode_buffers(inode);
dbd5768f 1510 clear_inode(inode);
2ffd2a6a 1511 ext4_discard_preallocations(inode);
51865fda 1512 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
f4c2d372 1513 dquot_drop(inode);
8aefcd55
TT
1514 if (EXT4_I(inode)->jinode) {
1515 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1516 EXT4_I(inode)->jinode);
1517 jbd2_free_inode(EXT4_I(inode)->jinode);
1518 EXT4_I(inode)->jinode = NULL;
1519 }
3d204e24 1520 fscrypt_put_encryption_info(inode);
c93d8f88 1521 fsverity_cleanup_inode(inode);
ac27a0ec
DK
1522}
1523
1b961ac0 1524static struct inode *ext4_nfs_get_inode(struct super_block *sb,
0b8e58a1 1525 u64 ino, u32 generation)
ac27a0ec 1526{
ac27a0ec 1527 struct inode *inode;
ac27a0ec 1528
8a363970 1529 /*
ac27a0ec
DK
1530 * Currently we don't know the generation for parent directory, so
1531 * a generation of 0 means "accept any"
1532 */
8a363970 1533 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
1d1fe1ee
DH
1534 if (IS_ERR(inode))
1535 return ERR_CAST(inode);
1536 if (generation && inode->i_generation != generation) {
ac27a0ec
DK
1537 iput(inode);
1538 return ERR_PTR(-ESTALE);
1539 }
1b961ac0
CH
1540
1541 return inode;
1542}
1543
1544static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
0b8e58a1 1545 int fh_len, int fh_type)
1b961ac0
CH
1546{
1547 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1548 ext4_nfs_get_inode);
1549}
1550
1551static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
0b8e58a1 1552 int fh_len, int fh_type)
1b961ac0
CH
1553{
1554 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1555 ext4_nfs_get_inode);
ac27a0ec
DK
1556}
1557
fde87268
TT
1558static int ext4_nfs_commit_metadata(struct inode *inode)
1559{
1560 struct writeback_control wbc = {
1561 .sync_mode = WB_SYNC_ALL
1562 };
1563
1564 trace_ext4_nfs_commit_metadata(inode);
1565 return ext4_write_inode(inode, &wbc);
1566}
1567
ac27a0ec 1568#ifdef CONFIG_QUOTA
d6006186 1569static const char * const quotatypes[] = INITQFNAMES;
689c958c 1570#define QTYPE2NAME(t) (quotatypes[t])
ac27a0ec 1571
617ba13b
MC
1572static int ext4_write_dquot(struct dquot *dquot);
1573static int ext4_acquire_dquot(struct dquot *dquot);
1574static int ext4_release_dquot(struct dquot *dquot);
1575static int ext4_mark_dquot_dirty(struct dquot *dquot);
1576static int ext4_write_info(struct super_block *sb, int type);
6f28e087 1577static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8c54ca9c 1578 const struct path *path);
617ba13b 1579static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec 1580 size_t len, loff_t off);
617ba13b 1581static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec 1582 const char *data, size_t len, loff_t off);
7c319d32
AK
1583static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1584 unsigned int flags);
ac27a0ec 1585
ccb49011 1586static struct dquot __rcu **ext4_get_dquots(struct inode *inode)
96c7e0d9
JK
1587{
1588 return EXT4_I(inode)->i_dquot;
1589}
1590
61e225dc 1591static const struct dquot_operations ext4_quota_operations = {
7a9ca53a
TE
1592 .get_reserved_space = ext4_get_reserved_space,
1593 .write_dquot = ext4_write_dquot,
1594 .acquire_dquot = ext4_acquire_dquot,
1595 .release_dquot = ext4_release_dquot,
1596 .mark_dirty = ext4_mark_dquot_dirty,
1597 .write_info = ext4_write_info,
1598 .alloc_dquot = dquot_alloc,
1599 .destroy_dquot = dquot_destroy,
1600 .get_projid = ext4_get_projid,
1601 .get_inode_usage = ext4_get_inode_usage,
ebc11f7b 1602 .get_next_id = dquot_get_next_id,
ac27a0ec
DK
1603};
1604
0d54b217 1605static const struct quotactl_ops ext4_qctl_operations = {
617ba13b 1606 .quota_on = ext4_quota_on,
ca0e05e4 1607 .quota_off = ext4_quota_off,
287a8095 1608 .quota_sync = dquot_quota_sync,
0a240339 1609 .get_state = dquot_get_state,
287a8095
CH
1610 .set_info = dquot_set_dqinfo,
1611 .get_dqblk = dquot_get_dqblk,
6332b9b5
ES
1612 .set_dqblk = dquot_set_dqblk,
1613 .get_nextdqblk = dquot_get_next_dqblk,
ac27a0ec
DK
1614};
1615#endif
1616
ee9b6d61 1617static const struct super_operations ext4_sops = {
617ba13b 1618 .alloc_inode = ext4_alloc_inode,
94053139 1619 .free_inode = ext4_free_in_core_inode,
617ba13b 1620 .destroy_inode = ext4_destroy_inode,
617ba13b
MC
1621 .write_inode = ext4_write_inode,
1622 .dirty_inode = ext4_dirty_inode,
7ff9c073 1623 .drop_inode = ext4_drop_inode,
0930fcc1 1624 .evict_inode = ext4_evict_inode,
617ba13b 1625 .put_super = ext4_put_super,
617ba13b 1626 .sync_fs = ext4_sync_fs,
c4be0c1d
TS
1627 .freeze_fs = ext4_freeze,
1628 .unfreeze_fs = ext4_unfreeze,
617ba13b 1629 .statfs = ext4_statfs,
617ba13b 1630 .show_options = ext4_show_options,
f5db130d 1631 .shutdown = ext4_shutdown,
ac27a0ec 1632#ifdef CONFIG_QUOTA
617ba13b
MC
1633 .quota_read = ext4_quota_read,
1634 .quota_write = ext4_quota_write,
96c7e0d9 1635 .get_dquots = ext4_get_dquots,
ac27a0ec
DK
1636#endif
1637};
1638
39655164 1639static const struct export_operations ext4_export_ops = {
e21fc203 1640 .encode_fh = generic_encode_ino32_fh,
1b961ac0
CH
1641 .fh_to_dentry = ext4_fh_to_dentry,
1642 .fh_to_parent = ext4_fh_to_parent,
617ba13b 1643 .get_parent = ext4_get_parent,
fde87268 1644 .commit_metadata = ext4_nfs_commit_metadata,
ac27a0ec
DK
1645};
1646
1647enum {
1648 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
ba2e524d 1649 Opt_resgid, Opt_resuid, Opt_sb,
72578c33 1650 Opt_nouid32, Opt_debug, Opt_removed,
2d544ec9 1651 Opt_user_xattr, Opt_acl,
72578c33 1652 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
ad4eec61
ES
1653 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1654 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
ac27a0ec 1655 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
6ddb2447 1656 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
4f74d15f 1657 Opt_inlinecrypt,
ba2e524d 1658 Opt_usrjquota, Opt_grpjquota, Opt_quota,
ee4a3fcd 1659 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1ff20307 1660 Opt_usrquota, Opt_grpquota, Opt_prjquota,
9cb20f94 1661 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
327eaf73 1662 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
4437992b 1663 Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize,
1449032b 1664 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
5328e635 1665 Opt_inode_readahead_blks, Opt_journal_ioprio,
744692dc 1666 Opt_dioread_nolock, Opt_dioread_lock,
fc6cb1cd 1667 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
cdb7ee4c 1668 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
21175ca4 1669 Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
e5a185c2 1670 Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
8016e29f 1671#ifdef CONFIG_EXT4_DEBUG
99c880de 1672 Opt_fc_debug_max_replay, Opt_fc_debug_force
8016e29f 1673#endif
ac27a0ec
DK
1674};
1675
e5a185c2 1676static const struct constant_table ext4_param_errors[] = {
ba2e524d
LC
1677 {"continue", EXT4_MOUNT_ERRORS_CONT},
1678 {"panic", EXT4_MOUNT_ERRORS_PANIC},
1679 {"remount-ro", EXT4_MOUNT_ERRORS_RO},
e5a185c2
LC
1680 {}
1681};
1682
1683static const struct constant_table ext4_param_data[] = {
ba2e524d
LC
1684 {"journal", EXT4_MOUNT_JOURNAL_DATA},
1685 {"ordered", EXT4_MOUNT_ORDERED_DATA},
1686 {"writeback", EXT4_MOUNT_WRITEBACK_DATA},
e5a185c2
LC
1687 {}
1688};
1689
1690static const struct constant_table ext4_param_data_err[] = {
1691 {"abort", Opt_data_err_abort},
1692 {"ignore", Opt_data_err_ignore},
1693 {}
1694};
1695
1696static const struct constant_table ext4_param_jqfmt[] = {
ba2e524d
LC
1697 {"vfsold", QFMT_VFS_OLD},
1698 {"vfsv0", QFMT_VFS_V0},
1699 {"vfsv1", QFMT_VFS_V1},
e5a185c2
LC
1700 {}
1701};
1702
1703static const struct constant_table ext4_param_dax[] = {
1704 {"always", Opt_dax_always},
1705 {"inode", Opt_dax_inode},
1706 {"never", Opt_dax_never},
1707 {}
1708};
1709
e5a185c2
LC
1710/*
1711 * Mount option specification
1712 * We don't use fsparam_flag_no because of the way we set the
1713 * options and the way we show them in _ext4_show_options(). To
1714 * keep the changes to a minimum, let's keep the negative options
1715 * separate for now.
1716 */
1717static const struct fs_parameter_spec ext4_param_specs[] = {
1718 fsparam_flag ("bsddf", Opt_bsd_df),
1719 fsparam_flag ("minixdf", Opt_minix_df),
1720 fsparam_flag ("grpid", Opt_grpid),
1721 fsparam_flag ("bsdgroups", Opt_grpid),
1722 fsparam_flag ("nogrpid", Opt_nogrpid),
1723 fsparam_flag ("sysvgroups", Opt_nogrpid),
1724 fsparam_u32 ("resgid", Opt_resgid),
1725 fsparam_u32 ("resuid", Opt_resuid),
1726 fsparam_u32 ("sb", Opt_sb),
1727 fsparam_enum ("errors", Opt_errors, ext4_param_errors),
1728 fsparam_flag ("nouid32", Opt_nouid32),
1729 fsparam_flag ("debug", Opt_debug),
1730 fsparam_flag ("oldalloc", Opt_removed),
1731 fsparam_flag ("orlov", Opt_removed),
1732 fsparam_flag ("user_xattr", Opt_user_xattr),
e5a185c2 1733 fsparam_flag ("acl", Opt_acl),
e5a185c2
LC
1734 fsparam_flag ("norecovery", Opt_noload),
1735 fsparam_flag ("noload", Opt_noload),
1736 fsparam_flag ("bh", Opt_removed),
1737 fsparam_flag ("nobh", Opt_removed),
1738 fsparam_u32 ("commit", Opt_commit),
1739 fsparam_u32 ("min_batch_time", Opt_min_batch_time),
1740 fsparam_u32 ("max_batch_time", Opt_max_batch_time),
1741 fsparam_u32 ("journal_dev", Opt_journal_dev),
1742 fsparam_bdev ("journal_path", Opt_journal_path),
1743 fsparam_flag ("journal_checksum", Opt_journal_checksum),
1744 fsparam_flag ("nojournal_checksum", Opt_nojournal_checksum),
1745 fsparam_flag ("journal_async_commit",Opt_journal_async_commit),
1746 fsparam_flag ("abort", Opt_abort),
1747 fsparam_enum ("data", Opt_data, ext4_param_data),
1748 fsparam_enum ("data_err", Opt_data_err,
1749 ext4_param_data_err),
1750 fsparam_string_empty
1751 ("usrjquota", Opt_usrjquota),
1752 fsparam_string_empty
1753 ("grpjquota", Opt_grpjquota),
1754 fsparam_enum ("jqfmt", Opt_jqfmt, ext4_param_jqfmt),
1755 fsparam_flag ("grpquota", Opt_grpquota),
1756 fsparam_flag ("quota", Opt_quota),
1757 fsparam_flag ("noquota", Opt_noquota),
1758 fsparam_flag ("usrquota", Opt_usrquota),
1759 fsparam_flag ("prjquota", Opt_prjquota),
1760 fsparam_flag ("barrier", Opt_barrier),
1761 fsparam_u32 ("barrier", Opt_barrier),
1762 fsparam_flag ("nobarrier", Opt_nobarrier),
1ff20307 1763 fsparam_flag ("i_version", Opt_removed),
e5a185c2
LC
1764 fsparam_flag ("dax", Opt_dax),
1765 fsparam_enum ("dax", Opt_dax_type, ext4_param_dax),
1766 fsparam_u32 ("stripe", Opt_stripe),
1767 fsparam_flag ("delalloc", Opt_delalloc),
1768 fsparam_flag ("nodelalloc", Opt_nodelalloc),
1769 fsparam_flag ("warn_on_error", Opt_warn_on_error),
1770 fsparam_flag ("nowarn_on_error", Opt_nowarn_on_error),
e5a185c2
LC
1771 fsparam_u32 ("debug_want_extra_isize",
1772 Opt_debug_want_extra_isize),
1773 fsparam_flag ("mblk_io_submit", Opt_removed),
1774 fsparam_flag ("nomblk_io_submit", Opt_removed),
1775 fsparam_flag ("block_validity", Opt_block_validity),
1776 fsparam_flag ("noblock_validity", Opt_noblock_validity),
1777 fsparam_u32 ("inode_readahead_blks",
1778 Opt_inode_readahead_blks),
1779 fsparam_u32 ("journal_ioprio", Opt_journal_ioprio),
1780 fsparam_u32 ("auto_da_alloc", Opt_auto_da_alloc),
1781 fsparam_flag ("auto_da_alloc", Opt_auto_da_alloc),
1782 fsparam_flag ("noauto_da_alloc", Opt_noauto_da_alloc),
1783 fsparam_flag ("dioread_nolock", Opt_dioread_nolock),
1784 fsparam_flag ("nodioread_nolock", Opt_dioread_lock),
1785 fsparam_flag ("dioread_lock", Opt_dioread_lock),
1786 fsparam_flag ("discard", Opt_discard),
1787 fsparam_flag ("nodiscard", Opt_nodiscard),
1788 fsparam_u32 ("init_itable", Opt_init_itable),
1789 fsparam_flag ("init_itable", Opt_init_itable),
1790 fsparam_flag ("noinit_itable", Opt_noinit_itable),
1791#ifdef CONFIG_EXT4_DEBUG
1792 fsparam_flag ("fc_debug_force", Opt_fc_debug_force),
1793 fsparam_u32 ("fc_debug_max_replay", Opt_fc_debug_max_replay),
1794#endif
1795 fsparam_u32 ("max_dir_size_kb", Opt_max_dir_size_kb),
1796 fsparam_flag ("test_dummy_encryption",
1797 Opt_test_dummy_encryption),
1798 fsparam_string ("test_dummy_encryption",
1799 Opt_test_dummy_encryption),
1800 fsparam_flag ("inlinecrypt", Opt_inlinecrypt),
1801 fsparam_flag ("nombcache", Opt_nombcache),
1802 fsparam_flag ("no_mbcache", Opt_nombcache), /* for backward compatibility */
1803 fsparam_flag ("prefetch_block_bitmaps",
1804 Opt_removed),
1805 fsparam_flag ("no_prefetch_block_bitmaps",
1806 Opt_no_prefetch_block_bitmaps),
1807 fsparam_s32 ("mb_optimize_scan", Opt_mb_optimize_scan),
1808 fsparam_string ("check", Opt_removed), /* mount option from ext2/3 */
1809 fsparam_flag ("nocheck", Opt_removed), /* mount option from ext2/3 */
1810 fsparam_flag ("reservation", Opt_removed), /* mount option from ext2/3 */
1811 fsparam_flag ("noreservation", Opt_removed), /* mount option from ext2/3 */
1812 fsparam_u32 ("journal", Opt_removed), /* mount option from ext2/3 */
1813 {}
1814};
1815
b3881f74 1816#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
196e402a 1817
26092bf5
TT
1818#define MOPT_SET 0x0001
1819#define MOPT_CLEAR 0x0002
1820#define MOPT_NOSUPPORT 0x0004
1821#define MOPT_EXPLICIT 0x0008
ac27a0ec 1822#ifdef CONFIG_QUOTA
26092bf5 1823#define MOPT_Q 0
ba2e524d 1824#define MOPT_QFMT 0x0010
26092bf5
TT
1825#else
1826#define MOPT_Q MOPT_NOSUPPORT
1827#define MOPT_QFMT MOPT_NOSUPPORT
ac27a0ec 1828#endif
ba2e524d
LC
1829#define MOPT_NO_EXT2 0x0020
1830#define MOPT_NO_EXT3 0x0040
8dc0aa8c 1831#define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
ba2e524d
LC
1832#define MOPT_SKIP 0x0080
1833#define MOPT_2 0x0100
26092bf5
TT
1834
1835static const struct mount_opts {
1836 int token;
1837 int mount_opt;
1838 int flags;
1839} ext4_mount_opts[] = {
1840 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1841 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1842 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1843 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
26092bf5
TT
1844 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1845 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
8dc0aa8c
TT
1846 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1847 MOPT_EXT4_ONLY | MOPT_SET},
1848 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1849 MOPT_EXT4_ONLY | MOPT_CLEAR},
26092bf5
TT
1850 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1851 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
8dc0aa8c
TT
1852 {Opt_delalloc, EXT4_MOUNT_DELALLOC,
1853 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1854 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
59d9fa5c 1855 MOPT_EXT4_ONLY | MOPT_CLEAR},
327eaf73
TT
1856 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1857 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
cb8435dc 1858 {Opt_commit, 0, MOPT_NO_EXT2},
c6d3d56d
DW
1859 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1860 MOPT_EXT4_ONLY | MOPT_CLEAR},
8dc0aa8c 1861 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1e381f60 1862 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
26092bf5 1863 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
8dc0aa8c 1864 EXT4_MOUNT_JOURNAL_CHECKSUM),
1e381f60 1865 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
8dc0aa8c 1866 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
ba2e524d 1867 {Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2},
26092bf5
TT
1868 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1869 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1870 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1871 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1872 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
ba2e524d
LC
1873 {Opt_dax_type, 0, MOPT_EXT4_ONLY},
1874 {Opt_journal_dev, 0, MOPT_NO_EXT2},
1875 {Opt_journal_path, 0, MOPT_NO_EXT2},
1876 {Opt_journal_ioprio, 0, MOPT_NO_EXT2},
1877 {Opt_data, 0, MOPT_NO_EXT2},
26092bf5 1878 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
03010a33 1879#ifdef CONFIG_EXT4_FS_POSIX_ACL
26092bf5 1880 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
ac27a0ec 1881#else
26092bf5 1882 {Opt_acl, 0, MOPT_NOSUPPORT},
ac27a0ec 1883#endif
26092bf5
TT
1884 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1885 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1886 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1887 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1888 MOPT_SET | MOPT_Q},
1889 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1890 MOPT_SET | MOPT_Q},
49da9392
JK
1891 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
1892 MOPT_SET | MOPT_Q},
26092bf5 1893 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
49da9392
JK
1894 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
1895 MOPT_CLEAR | MOPT_Q},
ba2e524d
LC
1896 {Opt_usrjquota, 0, MOPT_Q},
1897 {Opt_grpjquota, 0, MOPT_Q},
1898 {Opt_jqfmt, 0, MOPT_QFMT},
cdb7ee4c 1899 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
21175ca4 1900 {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
3d392b26 1901 MOPT_SET},
99c880de 1902#ifdef CONFIG_EXT4_DEBUG
0f0672ff
HS
1903 {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
1904 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
8016e29f 1905#endif
22b8d707 1906 {Opt_abort, EXT4_MOUNT2_ABORT, MOPT_SET | MOPT_2},
26092bf5
TT
1907 {Opt_err, 0, 0}
1908};
1909
5298d4bf 1910#if IS_ENABLED(CONFIG_UNICODE)
c83ad55e
GKB
1911static const struct ext4_sb_encodings {
1912 __u16 magic;
1913 char *name;
49bd03cc 1914 unsigned int version;
c83ad55e 1915} ext4_sb_encoding_map[] = {
49bd03cc 1916 {EXT4_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
c83ad55e
GKB
1917};
1918
aa8bf298
CH
1919static const struct ext4_sb_encodings *
1920ext4_sb_read_encoding(const struct ext4_super_block *es)
c83ad55e
GKB
1921{
1922 __u16 magic = le16_to_cpu(es->s_encoding);
1923 int i;
1924
1925 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1926 if (magic == ext4_sb_encoding_map[i].magic)
aa8bf298 1927 return &ext4_sb_encoding_map[i];
c83ad55e 1928
aa8bf298 1929 return NULL;
c83ad55e
GKB
1930}
1931#endif
1932
6e47a3cc
LC
1933#define EXT4_SPEC_JQUOTA (1 << 0)
1934#define EXT4_SPEC_JQFMT (1 << 1)
1935#define EXT4_SPEC_DATAJ (1 << 2)
1936#define EXT4_SPEC_SB_BLOCK (1 << 3)
1937#define EXT4_SPEC_JOURNAL_DEV (1 << 4)
1938#define EXT4_SPEC_JOURNAL_IOPRIO (1 << 5)
6e47a3cc
LC
1939#define EXT4_SPEC_s_want_extra_isize (1 << 7)
1940#define EXT4_SPEC_s_max_batch_time (1 << 8)
1941#define EXT4_SPEC_s_min_batch_time (1 << 9)
1942#define EXT4_SPEC_s_inode_readahead_blks (1 << 10)
1943#define EXT4_SPEC_s_li_wait_mult (1 << 11)
1944#define EXT4_SPEC_s_max_dir_size_kb (1 << 12)
1945#define EXT4_SPEC_s_stripe (1 << 13)
1946#define EXT4_SPEC_s_resuid (1 << 14)
1947#define EXT4_SPEC_s_resgid (1 << 15)
1948#define EXT4_SPEC_s_commit_interval (1 << 16)
1949#define EXT4_SPEC_s_fc_debug_max_replay (1 << 17)
7edfd85b 1950#define EXT4_SPEC_s_sb_block (1 << 18)
27b38686 1951#define EXT4_SPEC_mb_optimize_scan (1 << 19)
6e47a3cc 1952
461c3af0 1953struct ext4_fs_context {
e6e268cb 1954 char *s_qf_names[EXT4_MAXQUOTAS];
85456054 1955 struct fscrypt_dummy_policy dummy_enc_policy;
e6e268cb 1956 int s_jquota_fmt; /* Format of quota to use */
6e47a3cc
LC
1957#ifdef CONFIG_EXT4_DEBUG
1958 int s_fc_debug_max_replay;
1959#endif
e6e268cb 1960 unsigned short qname_spec;
6e47a3cc
LC
1961 unsigned long vals_s_flags; /* Bits to set in s_flags */
1962 unsigned long mask_s_flags; /* Bits changed in s_flags */
e6e268cb 1963 unsigned long journal_devnum;
6e47a3cc
LC
1964 unsigned long s_commit_interval;
1965 unsigned long s_stripe;
1966 unsigned int s_inode_readahead_blks;
1967 unsigned int s_want_extra_isize;
1968 unsigned int s_li_wait_mult;
1969 unsigned int s_max_dir_size_kb;
e6e268cb 1970 unsigned int journal_ioprio;
6e47a3cc
LC
1971 unsigned int vals_s_mount_opt;
1972 unsigned int mask_s_mount_opt;
1973 unsigned int vals_s_mount_opt2;
1974 unsigned int mask_s_mount_opt2;
b6bd2435 1975 unsigned int opt_flags; /* MOPT flags */
6e47a3cc
LC
1976 unsigned int spec;
1977 u32 s_max_batch_time;
1978 u32 s_min_batch_time;
1979 kuid_t s_resuid;
1980 kgid_t s_resgid;
7edfd85b 1981 ext4_fsblk_t s_sb_block;
b237e304
HS
1982};
1983
cebe85d5
LC
1984static void ext4_fc_free(struct fs_context *fc)
1985{
1986 struct ext4_fs_context *ctx = fc->fs_private;
1987 int i;
1988
1989 if (!ctx)
1990 return;
1991
1992 for (i = 0; i < EXT4_MAXQUOTAS; i++)
1993 kfree(ctx->s_qf_names[i]);
1994
85456054 1995 fscrypt_free_dummy_policy(&ctx->dummy_enc_policy);
cebe85d5
LC
1996 kfree(ctx);
1997}
1998
1999int ext4_init_fs_context(struct fs_context *fc)
2000{
da9e4802 2001 struct ext4_fs_context *ctx;
cebe85d5
LC
2002
2003 ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
2004 if (!ctx)
2005 return -ENOMEM;
2006
2007 fc->fs_private = ctx;
2008 fc->ops = &ext4_context_ops;
2009
2010 return 0;
2011}
2012
e6e268cb
LC
2013#ifdef CONFIG_QUOTA
2014/*
2015 * Note the name of the specified quota file.
2016 */
2017static int note_qf_name(struct fs_context *fc, int qtype,
2018 struct fs_parameter *param)
2019{
2020 struct ext4_fs_context *ctx = fc->fs_private;
2021 char *qname;
2022
2023 if (param->size < 1) {
2024 ext4_msg(NULL, KERN_ERR, "Missing quota name");
2025 return -EINVAL;
2026 }
2027 if (strchr(param->string, '/')) {
2028 ext4_msg(NULL, KERN_ERR,
2029 "quotafile must be on filesystem root");
2030 return -EINVAL;
2031 }
2032 if (ctx->s_qf_names[qtype]) {
2033 if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) {
2034 ext4_msg(NULL, KERN_ERR,
2035 "%s quota file already specified",
2036 QTYPE2NAME(qtype));
2037 return -EINVAL;
2038 }
2039 return 0;
2040 }
2041
2042 qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
2043 if (!qname) {
2044 ext4_msg(NULL, KERN_ERR,
2045 "Not enough memory for storing quotafile name");
2046 return -ENOMEM;
2047 }
2048 ctx->s_qf_names[qtype] = qname;
2049 ctx->qname_spec |= 1 << qtype;
6e47a3cc 2050 ctx->spec |= EXT4_SPEC_JQUOTA;
e6e268cb
LC
2051 return 0;
2052}
2053
2054/*
2055 * Clear the name of the specified quota file.
2056 */
2057static int unnote_qf_name(struct fs_context *fc, int qtype)
2058{
2059 struct ext4_fs_context *ctx = fc->fs_private;
2060
ea7d09ad 2061 kfree(ctx->s_qf_names[qtype]);
e6e268cb
LC
2062
2063 ctx->s_qf_names[qtype] = NULL;
2064 ctx->qname_spec |= 1 << qtype;
6e47a3cc 2065 ctx->spec |= EXT4_SPEC_JQUOTA;
e6e268cb
LC
2066 return 0;
2067}
2068#endif
2069
85456054
EB
2070static int ext4_parse_test_dummy_encryption(const struct fs_parameter *param,
2071 struct ext4_fs_context *ctx)
2072{
2073 int err;
2074
2075 if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
2076 ext4_msg(NULL, KERN_WARNING,
2077 "test_dummy_encryption option not supported");
2078 return -EINVAL;
2079 }
2080 err = fscrypt_parse_test_dummy_encryption(param,
2081 &ctx->dummy_enc_policy);
2082 if (err == -EINVAL) {
2083 ext4_msg(NULL, KERN_WARNING,
2084 "Value of option \"%s\" is unrecognized", param->key);
2085 } else if (err == -EEXIST) {
2086 ext4_msg(NULL, KERN_WARNING,
2087 "Conflicting test_dummy_encryption options");
2088 return -EINVAL;
2089 }
2090 return err;
2091}
2092
6e47a3cc 2093#define EXT4_SET_CTX(name) \
4c246728
LC
2094static inline void ctx_set_##name(struct ext4_fs_context *ctx, \
2095 unsigned long flag) \
6e47a3cc
LC
2096{ \
2097 ctx->mask_s_##name |= flag; \
2098 ctx->vals_s_##name |= flag; \
e3952fcc
LC
2099}
2100
2101#define EXT4_CLEAR_CTX(name) \
4c246728
LC
2102static inline void ctx_clear_##name(struct ext4_fs_context *ctx, \
2103 unsigned long flag) \
6e47a3cc
LC
2104{ \
2105 ctx->mask_s_##name |= flag; \
2106 ctx->vals_s_##name &= ~flag; \
e3952fcc
LC
2107}
2108
2109#define EXT4_TEST_CTX(name) \
4c246728
LC
2110static inline unsigned long \
2111ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag) \
6e47a3cc 2112{ \
4c246728 2113 return (ctx->vals_s_##name & flag); \
e3952fcc 2114}
6e47a3cc 2115
e3952fcc 2116EXT4_SET_CTX(flags); /* set only */
6e47a3cc 2117EXT4_SET_CTX(mount_opt);
e3952fcc
LC
2118EXT4_CLEAR_CTX(mount_opt);
2119EXT4_TEST_CTX(mount_opt);
6e47a3cc 2120EXT4_SET_CTX(mount_opt2);
e3952fcc
LC
2121EXT4_CLEAR_CTX(mount_opt2);
2122EXT4_TEST_CTX(mount_opt2);
2123
02f960f8 2124static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param)
26092bf5 2125{
461c3af0 2126 struct ext4_fs_context *ctx = fc->fs_private;
461c3af0 2127 struct fs_parse_result result;
26092bf5 2128 const struct mount_opts *m;
461c3af0 2129 int is_remount;
08cefc7a
EB
2130 kuid_t uid;
2131 kgid_t gid;
461c3af0
LC
2132 int token;
2133
2134 token = fs_parse(fc, ext4_param_specs, param, &result);
2135 if (token < 0)
2136 return token;
2137 is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
26092bf5 2138
ba2e524d
LC
2139 for (m = ext4_mount_opts; m->token != Opt_err; m++)
2140 if (token == m->token)
2141 break;
2142
2143 ctx->opt_flags |= m->flags;
2144
2145 if (m->flags & MOPT_EXPLICIT) {
2146 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2147 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC);
2148 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2149 ctx_set_mount_opt2(ctx,
2150 EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM);
2151 } else
2152 return -EINVAL;
2153 }
2154
2155 if (m->flags & MOPT_NOSUPPORT) {
2156 ext4_msg(NULL, KERN_ERR, "%s option not supported",
2157 param->key);
2158 return 0;
2159 }
2160
2161 switch (token) {
57f73c2c 2162#ifdef CONFIG_QUOTA
ba2e524d 2163 case Opt_usrjquota:
461c3af0 2164 if (!*param->string)
e6e268cb 2165 return unnote_qf_name(fc, USRQUOTA);
461c3af0 2166 else
e6e268cb 2167 return note_qf_name(fc, USRQUOTA, param);
ba2e524d 2168 case Opt_grpjquota:
461c3af0 2169 if (!*param->string)
e6e268cb 2170 return unnote_qf_name(fc, GRPQUOTA);
461c3af0 2171 else
e6e268cb 2172 return note_qf_name(fc, GRPQUOTA, param);
57f73c2c 2173#endif
26092bf5 2174 case Opt_sb:
7edfd85b
LC
2175 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2176 ext4_msg(NULL, KERN_WARNING,
2177 "Ignoring %s option on remount", param->key);
2178 } else {
2179 ctx->s_sb_block = result.uint_32;
2180 ctx->spec |= EXT4_SPEC_s_sb_block;
2181 }
02f960f8 2182 return 0;
26092bf5 2183 case Opt_removed:
da812f61 2184 ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option",
461c3af0 2185 param->key);
02f960f8 2186 return 0;
4f74d15f
EB
2187 case Opt_inlinecrypt:
2188#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
6e47a3cc 2189 ctx_set_flags(ctx, SB_INLINECRYPT);
4f74d15f 2190#else
da812f61 2191 ext4_msg(NULL, KERN_ERR, "inline encryption not supported");
4f74d15f 2192#endif
02f960f8 2193 return 0;
461c3af0 2194 case Opt_errors:
6e47a3cc 2195 ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK);
ba2e524d
LC
2196 ctx_set_mount_opt(ctx, result.uint_32);
2197 return 0;
2198#ifdef CONFIG_QUOTA
2199 case Opt_jqfmt:
2200 ctx->s_jquota_fmt = result.uint_32;
2201 ctx->spec |= EXT4_SPEC_JQFMT;
2202 return 0;
2203#endif
2204 case Opt_data:
2205 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
2206 ctx_set_mount_opt(ctx, result.uint_32);
2207 ctx->spec |= EXT4_SPEC_DATAJ;
2208 return 0;
2209 case Opt_commit:
461c3af0 2210 if (result.uint_32 == 0)
934b0de1 2211 result.uint_32 = JBD2_DEFAULT_MAX_COMMIT_AGE;
461c3af0 2212 else if (result.uint_32 > INT_MAX / HZ) {
da812f61 2213 ext4_msg(NULL, KERN_ERR,
9ba55543 2214 "Invalid commit interval %d, "
2215 "must be smaller than %d",
461c3af0 2216 result.uint_32, INT_MAX / HZ);
da812f61 2217 return -EINVAL;
9ba55543 2218 }
6e47a3cc
LC
2219 ctx->s_commit_interval = HZ * result.uint_32;
2220 ctx->spec |= EXT4_SPEC_s_commit_interval;
ba2e524d
LC
2221 return 0;
2222 case Opt_debug_want_extra_isize:
6e47a3cc 2223 if ((result.uint_32 & 1) || (result.uint_32 < 4)) {
da812f61 2224 ext4_msg(NULL, KERN_ERR,
461c3af0 2225 "Invalid want_extra_isize %d", result.uint_32);
da812f61 2226 return -EINVAL;
9803387c 2227 }
6e47a3cc
LC
2228 ctx->s_want_extra_isize = result.uint_32;
2229 ctx->spec |= EXT4_SPEC_s_want_extra_isize;
ba2e524d
LC
2230 return 0;
2231 case Opt_max_batch_time:
6e47a3cc
LC
2232 ctx->s_max_batch_time = result.uint_32;
2233 ctx->spec |= EXT4_SPEC_s_max_batch_time;
ba2e524d
LC
2234 return 0;
2235 case Opt_min_batch_time:
6e47a3cc
LC
2236 ctx->s_min_batch_time = result.uint_32;
2237 ctx->spec |= EXT4_SPEC_s_min_batch_time;
ba2e524d
LC
2238 return 0;
2239 case Opt_inode_readahead_blks:
461c3af0
LC
2240 if (result.uint_32 &&
2241 (result.uint_32 > (1 << 30) ||
2242 !is_power_of_2(result.uint_32))) {
da812f61 2243 ext4_msg(NULL, KERN_ERR,
e33e60ea
JK
2244 "EXT4-fs: inode_readahead_blks must be "
2245 "0 or a power of 2 smaller than 2^31");
da812f61 2246 return -EINVAL;
5f3633e3 2247 }
6e47a3cc
LC
2248 ctx->s_inode_readahead_blks = result.uint_32;
2249 ctx->spec |= EXT4_SPEC_s_inode_readahead_blks;
ba2e524d
LC
2250 return 0;
2251 case Opt_init_itable:
6e47a3cc
LC
2252 ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE);
2253 ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
461c3af0 2254 if (param->type == fs_value_is_string)
6e47a3cc
LC
2255 ctx->s_li_wait_mult = result.uint_32;
2256 ctx->spec |= EXT4_SPEC_s_li_wait_mult;
ba2e524d
LC
2257 return 0;
2258 case Opt_max_dir_size_kb:
6e47a3cc
LC
2259 ctx->s_max_dir_size_kb = result.uint_32;
2260 ctx->spec |= EXT4_SPEC_s_max_dir_size_kb;
ba2e524d 2261 return 0;
8016e29f 2262#ifdef CONFIG_EXT4_DEBUG
ba2e524d 2263 case Opt_fc_debug_max_replay:
6e47a3cc
LC
2264 ctx->s_fc_debug_max_replay = result.uint_32;
2265 ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay;
ba2e524d 2266 return 0;
8016e29f 2267#endif
ba2e524d 2268 case Opt_stripe:
6e47a3cc
LC
2269 ctx->s_stripe = result.uint_32;
2270 ctx->spec |= EXT4_SPEC_s_stripe;
ba2e524d
LC
2271 return 0;
2272 case Opt_resuid:
461c3af0 2273 uid = make_kuid(current_user_ns(), result.uint_32);
5f3633e3 2274 if (!uid_valid(uid)) {
da812f61 2275 ext4_msg(NULL, KERN_ERR, "Invalid uid value %d",
461c3af0 2276 result.uint_32);
da812f61 2277 return -EINVAL;
26092bf5 2278 }
6e47a3cc
LC
2279 ctx->s_resuid = uid;
2280 ctx->spec |= EXT4_SPEC_s_resuid;
ba2e524d
LC
2281 return 0;
2282 case Opt_resgid:
461c3af0 2283 gid = make_kgid(current_user_ns(), result.uint_32);
5f3633e3 2284 if (!gid_valid(gid)) {
da812f61 2285 ext4_msg(NULL, KERN_ERR, "Invalid gid value %d",
461c3af0 2286 result.uint_32);
da812f61 2287 return -EINVAL;
5f3633e3 2288 }
6e47a3cc
LC
2289 ctx->s_resgid = gid;
2290 ctx->spec |= EXT4_SPEC_s_resgid;
ba2e524d
LC
2291 return 0;
2292 case Opt_journal_dev:
5f3633e3 2293 if (is_remount) {
da812f61 2294 ext4_msg(NULL, KERN_ERR,
5f3633e3 2295 "Cannot specify journal on remount");
da812f61 2296 return -EINVAL;
5f3633e3 2297 }
461c3af0 2298 ctx->journal_devnum = result.uint_32;
6e47a3cc 2299 ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
ba2e524d
LC
2300 return 0;
2301 case Opt_journal_path:
2302 {
ad4eec61
ES
2303 struct inode *journal_inode;
2304 struct path path;
2305 int error;
2306
2307 if (is_remount) {
da812f61 2308 ext4_msg(NULL, KERN_ERR,
ad4eec61 2309 "Cannot specify journal on remount");
da812f61 2310 return -EINVAL;
ad4eec61 2311 }
ad4eec61 2312
e3ea75ee 2313 error = fs_lookup_param(fc, param, 1, LOOKUP_FOLLOW, &path);
ad4eec61 2314 if (error) {
da812f61 2315 ext4_msg(NULL, KERN_ERR, "error: could not find "
461c3af0 2316 "journal device path");
da812f61 2317 return -EINVAL;
ad4eec61
ES
2318 }
2319
2b0143b5 2320 journal_inode = d_inode(path.dentry);
461c3af0 2321 ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev);
6e47a3cc 2322 ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
ad4eec61 2323 path_put(&path);
ba2e524d
LC
2324 return 0;
2325 }
2326 case Opt_journal_ioprio:
461c3af0 2327 if (result.uint_32 > 7) {
da812f61 2328 ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority"
5f3633e3 2329 " (must be 0-7)");
da812f61 2330 return -EINVAL;
5f3633e3 2331 }
461c3af0
LC
2332 ctx->journal_ioprio =
2333 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32);
6e47a3cc 2334 ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO;
ba2e524d
LC
2335 return 0;
2336 case Opt_test_dummy_encryption:
85456054 2337 return ext4_parse_test_dummy_encryption(param, ctx);
ba2e524d
LC
2338 case Opt_dax:
2339 case Opt_dax_type:
ef83b6e8 2340#ifdef CONFIG_FS_DAX
ba2e524d
LC
2341 {
2342 int type = (token == Opt_dax) ?
2343 Opt_dax : result.uint_32;
2344
2345 switch (type) {
9cb20f94
IW
2346 case Opt_dax:
2347 case Opt_dax_always:
ba2e524d 2348 ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
6e47a3cc 2349 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
9cb20f94
IW
2350 break;
2351 case Opt_dax_never:
ba2e524d 2352 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
6e47a3cc 2353 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
9cb20f94
IW
2354 break;
2355 case Opt_dax_inode:
6e47a3cc
LC
2356 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
2357 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
9cb20f94 2358 /* Strictly for printing options */
ba2e524d 2359 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE);
9cb20f94
IW
2360 break;
2361 }
ba2e524d
LC
2362 return 0;
2363 }
ef83b6e8 2364#else
da812f61 2365 ext4_msg(NULL, KERN_INFO, "dax option not supported");
da812f61 2366 return -EINVAL;
5f3633e3 2367#endif
ba2e524d
LC
2368 case Opt_data_err:
2369 if (result.uint_32 == Opt_data_err_abort)
2370 ctx_set_mount_opt(ctx, m->mount_opt);
2371 else if (result.uint_32 == Opt_data_err_ignore)
2372 ctx_clear_mount_opt(ctx, m->mount_opt);
2373 return 0;
2374 case Opt_mb_optimize_scan:
27b38686
OM
2375 if (result.int_32 == 1) {
2376 ctx_set_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
2377 ctx->spec |= EXT4_SPEC_mb_optimize_scan;
2378 } else if (result.int_32 == 0) {
2379 ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
2380 ctx->spec |= EXT4_SPEC_mb_optimize_scan;
2381 } else {
da812f61 2382 ext4_msg(NULL, KERN_WARNING,
196e402a 2383 "mb_optimize_scan should be set to 0 or 1.");
da812f61 2384 return -EINVAL;
196e402a 2385 }
ba2e524d
LC
2386 return 0;
2387 }
2388
2389 /*
2390 * At this point we should only be getting options requiring MOPT_SET,
2391 * or MOPT_CLEAR. Anything else is a bug
2392 */
2393 if (m->token == Opt_err) {
2394 ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s",
2395 param->key);
2396 WARN_ON(1);
2397 return -EINVAL;
2398 }
2399
2400 else {
461c3af0
LC
2401 unsigned int set = 0;
2402
2403 if ((param->type == fs_value_is_flag) ||
2404 result.uint_32 > 0)
2405 set = 1;
2406
5f3633e3 2407 if (m->flags & MOPT_CLEAR)
461c3af0 2408 set = !set;
5f3633e3 2409 else if (unlikely(!(m->flags & MOPT_SET))) {
da812f61 2410 ext4_msg(NULL, KERN_WARNING,
461c3af0
LC
2411 "buggy handling of option %s",
2412 param->key);
5f3633e3 2413 WARN_ON(1);
da812f61 2414 return -EINVAL;
5f3633e3 2415 }
995a3ed6 2416 if (m->flags & MOPT_2) {
461c3af0 2417 if (set != 0)
6e47a3cc 2418 ctx_set_mount_opt2(ctx, m->mount_opt);
995a3ed6 2419 else
6e47a3cc 2420 ctx_clear_mount_opt2(ctx, m->mount_opt);
995a3ed6 2421 } else {
461c3af0 2422 if (set != 0)
6e47a3cc 2423 ctx_set_mount_opt(ctx, m->mount_opt);
995a3ed6 2424 else
6e47a3cc 2425 ctx_clear_mount_opt(ctx, m->mount_opt);
995a3ed6 2426 }
26092bf5 2427 }
ba2e524d 2428
02f960f8 2429 return 0;
26092bf5
TT
2430}
2431
7edfd85b 2432static int parse_options(struct fs_context *fc, char *options)
26092bf5 2433{
461c3af0 2434 struct fs_parameter param;
461c3af0
LC
2435 int ret;
2436 char *key;
26092bf5
TT
2437
2438 if (!options)
7edfd85b 2439 return 0;
461c3af0
LC
2440
2441 while ((key = strsep(&options, ",")) != NULL) {
2442 if (*key) {
2443 size_t v_len = 0;
2444 char *value = strchr(key, '=');
2445
2446 param.type = fs_value_is_flag;
2447 param.string = NULL;
2448
2449 if (value) {
2450 if (value == key)
2451 continue;
2452
2453 *value++ = 0;
2454 v_len = strlen(value);
2455 param.string = kmemdup_nul(value, v_len,
2456 GFP_KERNEL);
2457 if (!param.string)
7edfd85b 2458 return -ENOMEM;
461c3af0
LC
2459 param.type = fs_value_is_string;
2460 }
2461
2462 param.key = key;
2463 param.size = v_len;
2464
02f960f8 2465 ret = ext4_parse_param(fc, &param);
ea7d09ad 2466 kfree(param.string);
461c3af0 2467 if (ret < 0)
7edfd85b 2468 return ret;
461c3af0 2469 }
ac27a0ec 2470 }
461c3af0 2471
7edfd85b 2472 ret = ext4_validate_options(fc);
da812f61 2473 if (ret < 0)
7edfd85b 2474 return ret;
da812f61 2475
7edfd85b
LC
2476 return 0;
2477}
2478
2479static int parse_apply_sb_mount_options(struct super_block *sb,
2480 struct ext4_fs_context *m_ctx)
2481{
2482 struct ext4_sb_info *sbi = EXT4_SB(sb);
2483 char *s_mount_opts = NULL;
2484 struct ext4_fs_context *s_ctx = NULL;
2485 struct fs_context *fc = NULL;
2486 int ret = -ENOMEM;
2487
2488 if (!sbi->s_es->s_mount_opts[0])
e6e268cb
LC
2489 return 0;
2490
7edfd85b
LC
2491 s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
2492 sizeof(sbi->s_es->s_mount_opts),
2493 GFP_KERNEL);
2494 if (!s_mount_opts)
2495 return ret;
2496
2497 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
2498 if (!fc)
2499 goto out_free;
2500
2501 s_ctx = kzalloc(sizeof(struct ext4_fs_context), GFP_KERNEL);
2502 if (!s_ctx)
2503 goto out_free;
2504
2505 fc->fs_private = s_ctx;
2506 fc->s_fs_info = sbi;
2507
2508 ret = parse_options(fc, s_mount_opts);
6e47a3cc 2509 if (ret < 0)
7edfd85b 2510 goto parse_failed;
e6e268cb 2511
7edfd85b
LC
2512 ret = ext4_check_opt_consistency(fc, sb);
2513 if (ret < 0) {
2514parse_failed:
2515 ext4_msg(sb, KERN_WARNING,
2516 "failed to parse options in superblock: %s",
2517 s_mount_opts);
2518 ret = 0;
2519 goto out_free;
2520 }
2521
2522 if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV)
2523 m_ctx->journal_devnum = s_ctx->journal_devnum;
2524 if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)
2525 m_ctx->journal_ioprio = s_ctx->journal_ioprio;
2526
85456054
EB
2527 ext4_apply_options(fc, sb);
2528 ret = 0;
7edfd85b
LC
2529
2530out_free:
c069db76
EB
2531 if (fc) {
2532 ext4_fc_free(fc);
2533 kfree(fc);
2534 }
7edfd85b
LC
2535 kfree(s_mount_opts);
2536 return ret;
4c94bff9
LC
2537}
2538
e6e268cb
LC
2539static void ext4_apply_quota_options(struct fs_context *fc,
2540 struct super_block *sb)
2541{
2542#ifdef CONFIG_QUOTA
6e47a3cc 2543 bool quota_feature = ext4_has_feature_quota(sb);
e6e268cb
LC
2544 struct ext4_fs_context *ctx = fc->fs_private;
2545 struct ext4_sb_info *sbi = EXT4_SB(sb);
2546 char *qname;
2547 int i;
2548
6e47a3cc
LC
2549 if (quota_feature)
2550 return;
2551
2552 if (ctx->spec & EXT4_SPEC_JQUOTA) {
2553 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2554 if (!(ctx->qname_spec & (1 << i)))
2555 continue;
2556
2557 qname = ctx->s_qf_names[i]; /* May be NULL */
4c1bd5a9
LC
2558 if (qname)
2559 set_opt(sb, QUOTA);
6e47a3cc 2560 ctx->s_qf_names[i] = NULL;
13b215a9
LC
2561 qname = rcu_replace_pointer(sbi->s_qf_names[i], qname,
2562 lockdep_is_held(&sb->s_umount));
2563 if (qname)
10e4f310 2564 kfree_rcu_mightsleep(qname);
6e47a3cc 2565 }
e6e268cb 2566 }
6e47a3cc
LC
2567
2568 if (ctx->spec & EXT4_SPEC_JQFMT)
2569 sbi->s_jquota_fmt = ctx->s_jquota_fmt;
e6e268cb
LC
2570#endif
2571}
2572
2573/*
2574 * Check quota settings consistency.
2575 */
2576static int ext4_check_quota_consistency(struct fs_context *fc,
2577 struct super_block *sb)
2578{
2579#ifdef CONFIG_QUOTA
2580 struct ext4_fs_context *ctx = fc->fs_private;
2581 struct ext4_sb_info *sbi = EXT4_SB(sb);
2582 bool quota_feature = ext4_has_feature_quota(sb);
2583 bool quota_loaded = sb_any_quota_loaded(sb);
6e47a3cc
LC
2584 bool usr_qf_name, grp_qf_name, usrquota, grpquota;
2585 int quota_flags, i;
2586
2587 /*
2588 * We do the test below only for project quotas. 'usrquota' and
2589 * 'grpquota' mount options are allowed even without quota feature
2590 * to support legacy quotas in quota files.
2591 */
2592 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) &&
2593 !ext4_has_feature_project(sb)) {
2594 ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. "
2595 "Cannot enable project quota enforcement.");
2596 return -EINVAL;
2597 }
e6e268cb 2598
6e47a3cc
LC
2599 quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
2600 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA;
2601 if (quota_loaded &&
2602 ctx->mask_s_mount_opt & quota_flags &&
2603 !ctx_test_mount_opt(ctx, quota_flags))
2604 goto err_quota_change;
2605
2606 if (ctx->spec & EXT4_SPEC_JQUOTA) {
e6e268cb
LC
2607
2608 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2609 if (!(ctx->qname_spec & (1 << i)))
2610 continue;
2611
6e47a3cc
LC
2612 if (quota_loaded &&
2613 !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i])
e6e268cb
LC
2614 goto err_jquota_change;
2615
2616 if (sbi->s_qf_names[i] && ctx->s_qf_names[i] &&
13b215a9 2617 strcmp(get_qf_name(sb, sbi, i),
e6e268cb
LC
2618 ctx->s_qf_names[i]) != 0)
2619 goto err_jquota_specified;
2620 }
6e47a3cc
LC
2621
2622 if (quota_feature) {
2623 ext4_msg(NULL, KERN_INFO,
2624 "Journaled quota options ignored when "
2625 "QUOTA feature is enabled");
2626 return 0;
2627 }
e6e268cb
LC
2628 }
2629
6e47a3cc 2630 if (ctx->spec & EXT4_SPEC_JQFMT) {
e6e268cb 2631 if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded)
6e47a3cc 2632 goto err_jquota_change;
e6e268cb
LC
2633 if (quota_feature) {
2634 ext4_msg(NULL, KERN_INFO, "Quota format mount options "
2635 "ignored when QUOTA feature is enabled");
2636 return 0;
2637 }
2638 }
6e47a3cc
LC
2639
2640 /* Make sure we don't mix old and new quota format */
2641 usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) ||
2642 ctx->s_qf_names[USRQUOTA]);
2643 grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) ||
2644 ctx->s_qf_names[GRPQUOTA]);
2645
2646 usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
2647 test_opt(sb, USRQUOTA));
2648
2649 grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) ||
2650 test_opt(sb, GRPQUOTA));
2651
2652 if (usr_qf_name) {
2653 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
2654 usrquota = false;
2655 }
2656 if (grp_qf_name) {
2657 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
2658 grpquota = false;
2659 }
2660
2661 if (usr_qf_name || grp_qf_name) {
2662 if (usrquota || grpquota) {
2663 ext4_msg(NULL, KERN_ERR, "old and new quota "
2664 "format mixing");
2665 return -EINVAL;
2666 }
2667
2668 if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) {
2669 ext4_msg(NULL, KERN_ERR, "journaled quota format "
2670 "not specified");
2671 return -EINVAL;
2672 }
2673 }
2674
e6e268cb
LC
2675 return 0;
2676
2677err_quota_change:
2678 ext4_msg(NULL, KERN_ERR,
2679 "Cannot change quota options when quota turned on");
2680 return -EINVAL;
2681err_jquota_change:
2682 ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota "
2683 "options when quota turned on");
2684 return -EINVAL;
2685err_jquota_specified:
2686 ext4_msg(NULL, KERN_ERR, "%s quota file already specified",
2687 QTYPE2NAME(i));
2688 return -EINVAL;
e6e268cb
LC
2689#else
2690 return 0;
2691#endif
2692}
2693
5f41fdae
EB
2694static int ext4_check_test_dummy_encryption(const struct fs_context *fc,
2695 struct super_block *sb)
2696{
5f41fdae
EB
2697 const struct ext4_fs_context *ctx = fc->fs_private;
2698 const struct ext4_sb_info *sbi = EXT4_SB(sb);
2699
85456054 2700 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy))
5f41fdae
EB
2701 return 0;
2702
2703 if (!ext4_has_feature_encrypt(sb)) {
2704 ext4_msg(NULL, KERN_WARNING,
2705 "test_dummy_encryption requires encrypt feature");
2706 return -EINVAL;
2707 }
2708 /*
2709 * This mount option is just for testing, and it's not worthwhile to
2710 * implement the extra complexity (e.g. RCU protection) that would be
2711 * needed to allow it to be set or changed during remount. We do allow
2712 * it to be specified during remount, but only if there is no change.
2713 */
85456054
EB
2714 if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
2715 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
2716 &ctx->dummy_enc_policy))
2717 return 0;
5f41fdae 2718 ext4_msg(NULL, KERN_WARNING,
85456054 2719 "Can't set or change test_dummy_encryption on remount");
5f41fdae
EB
2720 return -EINVAL;
2721 }
85456054
EB
2722 /* Also make sure s_mount_opts didn't contain a conflicting value. */
2723 if (fscrypt_is_dummy_policy_set(&sbi->s_dummy_enc_policy)) {
2724 if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
2725 &ctx->dummy_enc_policy))
2726 return 0;
2727 ext4_msg(NULL, KERN_WARNING,
2728 "Conflicting test_dummy_encryption options");
2729 return -EINVAL;
2730 }
7959eb19 2731 return 0;
85456054
EB
2732}
2733
2734static void ext4_apply_test_dummy_encryption(struct ext4_fs_context *ctx,
2735 struct super_block *sb)
2736{
2737 if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) ||
2738 /* if already set, it was already verified to be the same */
2739 fscrypt_is_dummy_policy_set(&EXT4_SB(sb)->s_dummy_enc_policy))
2740 return;
2741 EXT4_SB(sb)->s_dummy_enc_policy = ctx->dummy_enc_policy;
2742 memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy));
2743 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
5f41fdae
EB
2744}
2745
b6bd2435
LC
2746static int ext4_check_opt_consistency(struct fs_context *fc,
2747 struct super_block *sb)
2748{
2749 struct ext4_fs_context *ctx = fc->fs_private;
6e47a3cc
LC
2750 struct ext4_sb_info *sbi = fc->s_fs_info;
2751 int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
5f41fdae 2752 int err;
b6bd2435
LC
2753
2754 if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2755 ext4_msg(NULL, KERN_ERR,
2756 "Mount option(s) incompatible with ext2");
2757 return -EINVAL;
2758 }
2759 if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2760 ext4_msg(NULL, KERN_ERR,
2761 "Mount option(s) incompatible with ext3");
2762 return -EINVAL;
2763 }
2764
6e47a3cc
LC
2765 if (ctx->s_want_extra_isize >
2766 (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) {
2767 ext4_msg(NULL, KERN_ERR,
2768 "Invalid want_extra_isize %d",
2769 ctx->s_want_extra_isize);
2770 return -EINVAL;
2771 }
2772
5f41fdae
EB
2773 err = ext4_check_test_dummy_encryption(fc, sb);
2774 if (err)
2775 return err;
6e47a3cc
LC
2776
2777 if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) {
2778 if (!sbi->s_journal) {
2779 ext4_msg(NULL, KERN_WARNING,
2780 "Remounting file system with no journal "
2781 "so ignoring journalled data option");
2782 ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
4c246728
LC
2783 } else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) !=
2784 test_opt(sb, DATA_FLAGS)) {
6e47a3cc
LC
2785 ext4_msg(NULL, KERN_ERR, "Cannot change data mode "
2786 "on remount");
2787 return -EINVAL;
2788 }
2789 }
2790
2791 if (is_remount) {
2792 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
2793 (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
2794 ext4_msg(NULL, KERN_ERR, "can't mount with "
2795 "both data=journal and dax");
2796 return -EINVAL;
2797 }
2798
2799 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
2800 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2801 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2802fail_dax_change_remount:
2803 ext4_msg(NULL, KERN_ERR, "can't change "
2804 "dax mount option while remounting");
2805 return -EINVAL;
2806 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) &&
2807 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2808 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) {
2809 goto fail_dax_change_remount;
2810 } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) &&
2811 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2812 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2813 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) {
2814 goto fail_dax_change_remount;
2815 }
2816 }
2817
b6bd2435
LC
2818 return ext4_check_quota_consistency(fc, sb);
2819}
2820
85456054 2821static void ext4_apply_options(struct fs_context *fc, struct super_block *sb)
4c94bff9 2822{
6e47a3cc 2823 struct ext4_fs_context *ctx = fc->fs_private;
da812f61 2824 struct ext4_sb_info *sbi = fc->s_fs_info;
6e47a3cc
LC
2825
2826 sbi->s_mount_opt &= ~ctx->mask_s_mount_opt;
2827 sbi->s_mount_opt |= ctx->vals_s_mount_opt;
2828 sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2;
2829 sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2;
6e47a3cc
LC
2830 sb->s_flags &= ~ctx->mask_s_flags;
2831 sb->s_flags |= ctx->vals_s_flags;
2832
2833#define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; })
2834 APPLY(s_commit_interval);
2835 APPLY(s_stripe);
2836 APPLY(s_max_batch_time);
2837 APPLY(s_min_batch_time);
2838 APPLY(s_want_extra_isize);
2839 APPLY(s_inode_readahead_blks);
2840 APPLY(s_max_dir_size_kb);
2841 APPLY(s_li_wait_mult);
2842 APPLY(s_resgid);
2843 APPLY(s_resuid);
2844
2845#ifdef CONFIG_EXT4_DEBUG
2846 APPLY(s_fc_debug_max_replay);
2847#endif
2848
2849 ext4_apply_quota_options(fc, sb);
85456054 2850 ext4_apply_test_dummy_encryption(ctx, sb);
6e47a3cc
LC
2851}
2852
2853
2854static int ext4_validate_options(struct fs_context *fc)
2855{
ac27a0ec 2856#ifdef CONFIG_QUOTA
6e47a3cc 2857 struct ext4_fs_context *ctx = fc->fs_private;
4c94bff9 2858 char *usr_qf_name, *grp_qf_name;
6e47a3cc
LC
2859
2860 usr_qf_name = ctx->s_qf_names[USRQUOTA];
2861 grp_qf_name = ctx->s_qf_names[GRPQUOTA];
2862
33458eab 2863 if (usr_qf_name || grp_qf_name) {
6e47a3cc
LC
2864 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name)
2865 ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
ac27a0ec 2866
6e47a3cc
LC
2867 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name)
2868 ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
ac27a0ec 2869
6e47a3cc
LC
2870 if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
2871 ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) {
da812f61 2872 ext4_msg(NULL, KERN_ERR, "old and new quota "
6e47a3cc 2873 "format mixing");
da812f61 2874 return -EINVAL;
ac27a0ec 2875 }
ac27a0ec
DK
2876 }
2877#endif
6e47a3cc 2878 return 1;
ac27a0ec
DK
2879}
2880
2adf6da8
TT
2881static inline void ext4_show_quota_options(struct seq_file *seq,
2882 struct super_block *sb)
2883{
2884#if defined(CONFIG_QUOTA)
2885 struct ext4_sb_info *sbi = EXT4_SB(sb);
33458eab 2886 char *usr_qf_name, *grp_qf_name;
2adf6da8
TT
2887
2888 if (sbi->s_jquota_fmt) {
2889 char *fmtname = "";
2890
2891 switch (sbi->s_jquota_fmt) {
2892 case QFMT_VFS_OLD:
2893 fmtname = "vfsold";
2894 break;
2895 case QFMT_VFS_V0:
2896 fmtname = "vfsv0";
2897 break;
2898 case QFMT_VFS_V1:
2899 fmtname = "vfsv1";
2900 break;
2901 }
2902 seq_printf(seq, ",jqfmt=%s", fmtname);
2903 }
2904
33458eab
TT
2905 rcu_read_lock();
2906 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2907 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2908 if (usr_qf_name)
2909 seq_show_option(seq, "usrjquota", usr_qf_name);
2910 if (grp_qf_name)
2911 seq_show_option(seq, "grpjquota", grp_qf_name);
2912 rcu_read_unlock();
2adf6da8
TT
2913#endif
2914}
2915
5a916be1
TT
2916static const char *token2str(int token)
2917{
97d8a670 2918 const struct fs_parameter_spec *spec;
5a916be1 2919
97d8a670
LC
2920 for (spec = ext4_param_specs; spec->name != NULL; spec++)
2921 if (spec->opt == token && !spec->type)
5a916be1 2922 break;
97d8a670 2923 return spec->name;
5a916be1
TT
2924}
2925
2adf6da8
TT
2926/*
2927 * Show an option if
2928 * - it's set to a non-default value OR
2929 * - if the per-sb default is different from the global default
2930 */
66acdcf4
TT
2931static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2932 int nodefs)
2adf6da8 2933{
2adf6da8
TT
2934 struct ext4_sb_info *sbi = EXT4_SB(sb);
2935 struct ext4_super_block *es = sbi->s_es;
e3645d72 2936 int def_errors;
5a916be1 2937 const struct mount_opts *m;
66acdcf4 2938 char sep = nodefs ? '\n' : ',';
2adf6da8 2939
66acdcf4
TT
2940#define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2941#define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
2adf6da8
TT
2942
2943 if (sbi->s_sb_block != 1)
5a916be1
TT
2944 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2945
2946 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2947 int want_set = m->flags & MOPT_SET;
e3645d72
ZY
2948 int opt_2 = m->flags & MOPT_2;
2949 unsigned int mount_opt, def_mount_opt;
2950
5a916be1 2951 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
ba2e524d 2952 m->flags & MOPT_SKIP)
5a916be1 2953 continue;
e3645d72
ZY
2954
2955 if (opt_2) {
2956 mount_opt = sbi->s_mount_opt2;
2957 def_mount_opt = sbi->s_def_mount_opt2;
2958 } else {
2959 mount_opt = sbi->s_mount_opt;
2960 def_mount_opt = sbi->s_def_mount_opt;
2961 }
2962 /* skip if same as the default */
2963 if (!nodefs && !(m->mount_opt & (mount_opt ^ def_mount_opt)))
2964 continue;
2965 /* select Opt_noFoo vs Opt_Foo */
5a916be1 2966 if ((want_set &&
e3645d72
ZY
2967 (mount_opt & m->mount_opt) != m->mount_opt) ||
2968 (!want_set && (mount_opt & m->mount_opt)))
2969 continue;
5a916be1 2970 SEQ_OPTS_PRINT("%s", token2str(m->token));
2adf6da8 2971 }
5a916be1 2972
08cefc7a 2973 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
5a916be1 2974 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
08cefc7a
EB
2975 SEQ_OPTS_PRINT("resuid=%u",
2976 from_kuid_munged(&init_user_ns, sbi->s_resuid));
2977 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
5a916be1 2978 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
08cefc7a
EB
2979 SEQ_OPTS_PRINT("resgid=%u",
2980 from_kgid_munged(&init_user_ns, sbi->s_resgid));
66acdcf4 2981 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
5a916be1
TT
2982 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2983 SEQ_OPTS_PUTS("errors=remount-ro");
2adf6da8 2984 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
5a916be1 2985 SEQ_OPTS_PUTS("errors=continue");
2adf6da8 2986 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
5a916be1 2987 SEQ_OPTS_PUTS("errors=panic");
66acdcf4 2988 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
5a916be1 2989 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
66acdcf4 2990 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
5a916be1 2991 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
66acdcf4 2992 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
5a916be1 2993 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
66acdcf4 2994 if (nodefs || sbi->s_stripe)
5a916be1 2995 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
68afa7e0 2996 if (nodefs || EXT4_MOUNT_DATA_FLAGS &
e3645d72 2997 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
5a916be1
TT
2998 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2999 SEQ_OPTS_PUTS("data=journal");
3000 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
3001 SEQ_OPTS_PUTS("data=ordered");
3002 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
3003 SEQ_OPTS_PUTS("data=writeback");
3004 }
66acdcf4
TT
3005 if (nodefs ||
3006 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
5a916be1
TT
3007 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
3008 sbi->s_inode_readahead_blks);
2adf6da8 3009
ceec0376 3010 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
66acdcf4 3011 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
5a916be1 3012 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
df981d03
TT
3013 if (nodefs || sbi->s_max_dir_size_kb)
3014 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
7915a861
AN
3015 if (test_opt(sb, DATA_ERR_ABORT))
3016 SEQ_OPTS_PUTS("data_err=abort");
ed318a6c
EB
3017
3018 fscrypt_show_test_dummy_encryption(seq, sep, sb);
2adf6da8 3019
4f74d15f
EB
3020 if (sb->s_flags & SB_INLINECRYPT)
3021 SEQ_OPTS_PUTS("inlinecrypt");
3022
9cb20f94
IW
3023 if (test_opt(sb, DAX_ALWAYS)) {
3024 if (IS_EXT2_SB(sb))
3025 SEQ_OPTS_PUTS("dax");
3026 else
3027 SEQ_OPTS_PUTS("dax=always");
3028 } else if (test_opt2(sb, DAX_NEVER)) {
3029 SEQ_OPTS_PUTS("dax=never");
3030 } else if (test_opt2(sb, DAX_INODE)) {
3031 SEQ_OPTS_PUTS("dax=inode");
3032 }
3fa5d23e
OM
3033
3034 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
3035 !test_opt2(sb, MB_OPTIMIZE_SCAN)) {
3036 SEQ_OPTS_PUTS("mb_optimize_scan=0");
3037 } else if (sbi->s_groups_count < MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
3038 test_opt2(sb, MB_OPTIMIZE_SCAN)) {
3039 SEQ_OPTS_PUTS("mb_optimize_scan=1");
3040 }
3041
2adf6da8 3042 ext4_show_quota_options(seq, sb);
2adf6da8
TT
3043 return 0;
3044}
3045
66acdcf4
TT
3046static int ext4_show_options(struct seq_file *seq, struct dentry *root)
3047{
3048 return _ext4_show_options(seq, root->d_sb, 0);
3049}
3050
ebd173be 3051int ext4_seq_options_show(struct seq_file *seq, void *offset)
66acdcf4
TT
3052{
3053 struct super_block *sb = seq->private;
3054 int rc;
3055
bc98a42c 3056 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
66acdcf4
TT
3057 rc = _ext4_show_options(seq, sb, 1);
3058 seq_puts(seq, "\n");
3059 return rc;
3060}
3061
617ba13b 3062static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
ac27a0ec
DK
3063 int read_only)
3064{
617ba13b 3065 struct ext4_sb_info *sbi = EXT4_SB(sb);
c89128a0 3066 int err = 0;
ac27a0ec 3067
617ba13b 3068 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
b31e1552
ES
3069 ext4_msg(sb, KERN_ERR, "revision level too high, "
3070 "forcing read-only mode");
c89128a0 3071 err = -EROFS;
5adaccac 3072 goto done;
ac27a0ec
DK
3073 }
3074 if (read_only)
281b5995 3075 goto done;
617ba13b 3076 if (!(sbi->s_mount_state & EXT4_VALID_FS))
b31e1552
ES
3077 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
3078 "running e2fsck is recommended");
c8b459f4 3079 else if (sbi->s_mount_state & EXT4_ERROR_FS)
b31e1552
ES
3080 ext4_msg(sb, KERN_WARNING,
3081 "warning: mounting fs with errors, "
3082 "running e2fsck is recommended");
ed3ce80a 3083 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
ac27a0ec
DK
3084 le16_to_cpu(es->s_mnt_count) >=
3085 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
b31e1552
ES
3086 ext4_msg(sb, KERN_WARNING,
3087 "warning: maximal mount count reached, "
3088 "running e2fsck is recommended");
ac27a0ec 3089 else if (le32_to_cpu(es->s_checkinterval) &&
6a0678a7
AB
3090 (ext4_get_tstamp(es, s_lastcheck) +
3091 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
b31e1552
ES
3092 ext4_msg(sb, KERN_WARNING,
3093 "warning: checktime reached, "
3094 "running e2fsck is recommended");
0b8e58a1 3095 if (!sbi->s_journal)
0390131b 3096 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
ac27a0ec 3097 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
617ba13b 3098 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
e8546d06 3099 le16_add_cpu(&es->s_mnt_count, 1);
6a0678a7 3100 ext4_update_tstamp(es, s_mtime);
02f310fc 3101 if (sbi->s_journal) {
e2b911c5 3102 ext4_set_feature_journal_needs_recovery(sb);
02f310fc
JK
3103 if (ext4_has_feature_orphan_file(sb))
3104 ext4_set_feature_orphan_present(sb);
3105 }
ac27a0ec 3106
4392fbc4 3107 err = ext4_commit_super(sb);
281b5995 3108done:
ac27a0ec 3109 if (test_opt(sb, DEBUG))
a9df9a49 3110 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
a2595b8a 3111 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
ac27a0ec
DK
3112 sb->s_blocksize,
3113 sbi->s_groups_count,
617ba13b
MC
3114 EXT4_BLOCKS_PER_GROUP(sb),
3115 EXT4_INODES_PER_GROUP(sb),
a2595b8a 3116 sbi->s_mount_opt, sbi->s_mount_opt2);
c89128a0 3117 return err;
ac27a0ec
DK
3118}
3119
117fff10
TT
3120int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
3121{
3122 struct ext4_sb_info *sbi = EXT4_SB(sb);
7c990728 3123 struct flex_groups **old_groups, **new_groups;
37b0b6b8 3124 int size, i, j;
117fff10
TT
3125
3126 if (!sbi->s_log_groups_per_flex)
3127 return 0;
3128
3129 size = ext4_flex_group(sbi, ngroup - 1) + 1;
3130 if (size <= sbi->s_flex_groups_allocated)
3131 return 0;
3132
7c990728
SJS
3133 new_groups = kvzalloc(roundup_pow_of_two(size *
3134 sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
117fff10 3135 if (!new_groups) {
7c990728
SJS
3136 ext4_msg(sb, KERN_ERR,
3137 "not enough memory for %d flex group pointers", size);
117fff10
TT
3138 return -ENOMEM;
3139 }
7c990728
SJS
3140 for (i = sbi->s_flex_groups_allocated; i < size; i++) {
3141 new_groups[i] = kvzalloc(roundup_pow_of_two(
3142 sizeof(struct flex_groups)),
3143 GFP_KERNEL);
3144 if (!new_groups[i]) {
37b0b6b8
DC
3145 for (j = sbi->s_flex_groups_allocated; j < i; j++)
3146 kvfree(new_groups[j]);
7c990728
SJS
3147 kvfree(new_groups);
3148 ext4_msg(sb, KERN_ERR,
3149 "not enough memory for %d flex groups", size);
3150 return -ENOMEM;
3151 }
117fff10 3152 }
7c990728
SJS
3153 rcu_read_lock();
3154 old_groups = rcu_dereference(sbi->s_flex_groups);
3155 if (old_groups)
3156 memcpy(new_groups, old_groups,
3157 (sbi->s_flex_groups_allocated *
3158 sizeof(struct flex_groups *)));
3159 rcu_read_unlock();
3160 rcu_assign_pointer(sbi->s_flex_groups, new_groups);
3161 sbi->s_flex_groups_allocated = size;
3162 if (old_groups)
3163 ext4_kvfree_array_rcu(old_groups);
117fff10
TT
3164 return 0;
3165}
3166
772cb7c8
JS
3167static int ext4_fill_flex_info(struct super_block *sb)
3168{
3169 struct ext4_sb_info *sbi = EXT4_SB(sb);
3170 struct ext4_group_desc *gdp = NULL;
7c990728 3171 struct flex_groups *fg;
772cb7c8 3172 ext4_group_t flex_group;
117fff10 3173 int i, err;
772cb7c8 3174
503358ae 3175 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
d50f2ab6 3176 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
772cb7c8
JS
3177 sbi->s_log_groups_per_flex = 0;
3178 return 1;
3179 }
3180
117fff10
TT
3181 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
3182 if (err)
9933fc0a 3183 goto failed;
772cb7c8 3184
772cb7c8 3185 for (i = 0; i < sbi->s_groups_count; i++) {
88b6edd1 3186 gdp = ext4_get_group_desc(sb, i, NULL);
772cb7c8
JS
3187
3188 flex_group = ext4_flex_group(sbi, i);
7c990728
SJS
3189 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
3190 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
90ba983f 3191 atomic64_add(ext4_free_group_clusters(sb, gdp),
7c990728
SJS
3192 &fg->free_clusters);
3193 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
772cb7c8
JS
3194 }
3195
3196 return 1;
3197failed:
3198 return 0;
3199}
3200
e2b911c5 3201static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
feb0ab32 3202 struct ext4_group_desc *gdp)
717d50e4 3203{
b47820ed 3204 int offset = offsetof(struct ext4_group_desc, bg_checksum);
717d50e4 3205 __u16 crc = 0;
feb0ab32 3206 __le32 le_group = cpu_to_le32(block_group);
e2b911c5 3207 struct ext4_sb_info *sbi = EXT4_SB(sb);
717d50e4 3208
9aa5d32b 3209 if (ext4_has_metadata_csum(sbi->s_sb)) {
feb0ab32 3210 /* Use new metadata_csum algorithm */
feb0ab32 3211 __u32 csum32;
b47820ed 3212 __u16 dummy_csum = 0;
feb0ab32 3213
feb0ab32
DW
3214 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
3215 sizeof(le_group));
b47820ed
DJ
3216 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
3217 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
3218 sizeof(dummy_csum));
3219 offset += sizeof(dummy_csum);
3220 if (offset < sbi->s_desc_size)
3221 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
3222 sbi->s_desc_size - offset);
feb0ab32
DW
3223
3224 crc = csum32 & 0xFFFF;
3225 goto out;
717d50e4
AD
3226 }
3227
feb0ab32 3228 /* old crc16 code */
e2b911c5 3229 if (!ext4_has_feature_gdt_csum(sb))
813d32f9
DW
3230 return 0;
3231
feb0ab32
DW
3232 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
3233 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
3234 crc = crc16(crc, (__u8 *)gdp, offset);
3235 offset += sizeof(gdp->bg_checksum); /* skip checksum */
3236 /* for checksum of struct ext4_group_desc do the rest...*/
4f043518 3237 if (ext4_has_feature_64bit(sb) && offset < sbi->s_desc_size)
feb0ab32 3238 crc = crc16(crc, (__u8 *)gdp + offset,
4f043518 3239 sbi->s_desc_size - offset);
feb0ab32
DW
3240
3241out:
717d50e4
AD
3242 return cpu_to_le16(crc);
3243}
3244
feb0ab32 3245int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
717d50e4
AD
3246 struct ext4_group_desc *gdp)
3247{
feb0ab32 3248 if (ext4_has_group_desc_csum(sb) &&
e2b911c5 3249 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
717d50e4
AD
3250 return 0;
3251
3252 return 1;
3253}
3254
feb0ab32
DW
3255void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
3256 struct ext4_group_desc *gdp)
3257{
3258 if (!ext4_has_group_desc_csum(sb))
3259 return;
e2b911c5 3260 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
feb0ab32
DW
3261}
3262
ac27a0ec 3263/* Called at mount-time, super-block is locked */
bfff6873 3264static int ext4_check_descriptors(struct super_block *sb,
829fa70d 3265 ext4_fsblk_t sb_block,
bfff6873 3266 ext4_group_t *first_not_zeroed)
ac27a0ec 3267{
617ba13b
MC
3268 struct ext4_sb_info *sbi = EXT4_SB(sb);
3269 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
3270 ext4_fsblk_t last_block;
44de022c 3271 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
bd81d8ee
LV
3272 ext4_fsblk_t block_bitmap;
3273 ext4_fsblk_t inode_bitmap;
3274 ext4_fsblk_t inode_table;
ce421581 3275 int flexbg_flag = 0;
bfff6873 3276 ext4_group_t i, grp = sbi->s_groups_count;
ac27a0ec 3277
e2b911c5 3278 if (ext4_has_feature_flex_bg(sb))
ce421581
JS
3279 flexbg_flag = 1;
3280
af5bc92d 3281 ext4_debug("Checking group descriptors");
ac27a0ec 3282
197cd65a
AM
3283 for (i = 0; i < sbi->s_groups_count; i++) {
3284 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
3285
ce421581 3286 if (i == sbi->s_groups_count - 1 || flexbg_flag)
bd81d8ee 3287 last_block = ext4_blocks_count(sbi->s_es) - 1;
ac27a0ec
DK
3288 else
3289 last_block = first_block +
617ba13b 3290 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
ac27a0ec 3291
bfff6873
LC
3292 if ((grp == sbi->s_groups_count) &&
3293 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3294 grp = i;
3295
8fadc143 3296 block_bitmap = ext4_block_bitmap(sb, gdp);
829fa70d
TT
3297 if (block_bitmap == sb_block) {
3298 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3299 "Block bitmap for group %u overlaps "
3300 "superblock", i);
18db4b4e
TT
3301 if (!sb_rdonly(sb))
3302 return 0;
829fa70d 3303 }
77260807
TT
3304 if (block_bitmap >= sb_block + 1 &&
3305 block_bitmap <= last_bg_block) {
3306 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3307 "Block bitmap for group %u overlaps "
3308 "block group descriptors", i);
3309 if (!sb_rdonly(sb))
3310 return 0;
3311 }
2b2d6d01 3312 if (block_bitmap < first_block || block_bitmap > last_block) {
b31e1552 3313 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 3314 "Block bitmap for group %u not in group "
b31e1552 3315 "(block %llu)!", i, block_bitmap);
ac27a0ec
DK
3316 return 0;
3317 }
8fadc143 3318 inode_bitmap = ext4_inode_bitmap(sb, gdp);
829fa70d
TT
3319 if (inode_bitmap == sb_block) {
3320 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3321 "Inode bitmap for group %u overlaps "
3322 "superblock", i);
18db4b4e
TT
3323 if (!sb_rdonly(sb))
3324 return 0;
829fa70d 3325 }
77260807
TT
3326 if (inode_bitmap >= sb_block + 1 &&
3327 inode_bitmap <= last_bg_block) {
3328 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3329 "Inode bitmap for group %u overlaps "
3330 "block group descriptors", i);
3331 if (!sb_rdonly(sb))
3332 return 0;
3333 }
2b2d6d01 3334 if (inode_bitmap < first_block || inode_bitmap > last_block) {
b31e1552 3335 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 3336 "Inode bitmap for group %u not in group "
b31e1552 3337 "(block %llu)!", i, inode_bitmap);
ac27a0ec
DK
3338 return 0;
3339 }
8fadc143 3340 inode_table = ext4_inode_table(sb, gdp);
829fa70d
TT
3341 if (inode_table == sb_block) {
3342 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3343 "Inode table for group %u overlaps "
3344 "superblock", i);
18db4b4e
TT
3345 if (!sb_rdonly(sb))
3346 return 0;
829fa70d 3347 }
77260807
TT
3348 if (inode_table >= sb_block + 1 &&
3349 inode_table <= last_bg_block) {
3350 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3351 "Inode table for group %u overlaps "
3352 "block group descriptors", i);
3353 if (!sb_rdonly(sb))
3354 return 0;
3355 }
bd81d8ee 3356 if (inode_table < first_block ||
2b2d6d01 3357 inode_table + sbi->s_itb_per_group - 1 > last_block) {
b31e1552 3358 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
a9df9a49 3359 "Inode table for group %u not in group "
b31e1552 3360 "(block %llu)!", i, inode_table);
ac27a0ec
DK
3361 return 0;
3362 }
955ce5f5 3363 ext4_lock_group(sb, i);
feb0ab32 3364 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
b31e1552
ES
3365 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
3366 "Checksum for group %u failed (%u!=%u)",
e2b911c5 3367 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
b31e1552 3368 gdp)), le16_to_cpu(gdp->bg_checksum));
bc98a42c 3369 if (!sb_rdonly(sb)) {
955ce5f5 3370 ext4_unlock_group(sb, i);
8a266467 3371 return 0;
7ee1ec4c 3372 }
717d50e4 3373 }
955ce5f5 3374 ext4_unlock_group(sb, i);
ce421581
JS
3375 if (!flexbg_flag)
3376 first_block += EXT4_BLOCKS_PER_GROUP(sb);
ac27a0ec 3377 }
bfff6873
LC
3378 if (NULL != first_not_zeroed)
3379 *first_not_zeroed = grp;
ac27a0ec
DK
3380 return 1;
3381}
3382
cd2291a4
ES
3383/*
3384 * Maximal extent format file size.
3385 * Resulting logical blkno at s_maxbytes must fit in our on-disk
3386 * extent format containers, within a sector_t, and within i_blocks
3387 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
3388 * so that won't be a limiting factor.
3389 *
f17722f9
LC
3390 * However there is other limiting factor. We do store extents in the form
3391 * of starting block and length, hence the resulting length of the extent
3392 * covering maximum file size must fit into on-disk format containers as
3393 * well. Given that length is always by 1 unit bigger than max unit (because
3394 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3395 *
cd2291a4
ES
3396 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3397 */
f287a1a5 3398static loff_t ext4_max_size(int blkbits, int has_huge_files)
cd2291a4
ES
3399{
3400 loff_t res;
3401 loff_t upper_limit = MAX_LFS_FILESIZE;
3402
72deb455
CH
3403 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
3404
3405 if (!has_huge_files) {
cd2291a4
ES
3406 upper_limit = (1LL << 32) - 1;
3407
3408 /* total blocks in file system block size */
3409 upper_limit >>= (blkbits - 9);
3410 upper_limit <<= blkbits;
3411 }
3412
f17722f9
LC
3413 /*
3414 * 32-bit extent-start container, ee_block. We lower the maxbytes
3415 * by one fs block, so ee_len can cover the extent of maximum file
3416 * size
3417 */
3418 res = (1LL << 32) - 1;
cd2291a4 3419 res <<= blkbits;
cd2291a4
ES
3420
3421 /* Sanity check against vm- & vfs- imposed limits */
3422 if (res > upper_limit)
3423 res = upper_limit;
3424
3425 return res;
3426}
ac27a0ec 3427
ac27a0ec 3428/*
cd2291a4 3429 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
0fc1b451
AK
3430 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3431 * We need to be 1 filesystem block less than the 2^48 sector limit.
ac27a0ec 3432 */
f287a1a5 3433static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
ac27a0ec 3434{
5c93e8ec 3435 loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
0fc1b451 3436 int meta_blocks;
5c93e8ec 3437 unsigned int ppb = 1 << (bits - 2);
75ca6ad4
RH
3438
3439 /*
3440 * This is calculated to be the largest file size for a dense, block
0b8e58a1
AD
3441 * mapped file such that the file's total number of 512-byte sectors,
3442 * including data and all indirect blocks, does not exceed (2^48 - 1).
3443 *
3444 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3445 * number of 512-byte sectors of the file.
0fc1b451 3446 */
72deb455 3447 if (!has_huge_files) {
0fc1b451 3448 /*
72deb455
CH
3449 * !has_huge_files or implies that the inode i_block field
3450 * represents total file blocks in 2^32 512-byte sectors ==
3451 * size of vfs inode i_blocks * 8
0fc1b451
AK
3452 */
3453 upper_limit = (1LL << 32) - 1;
3454
3455 /* total blocks in file system block size */
3456 upper_limit >>= (bits - 9);
3457
3458 } else {
8180a562
AK
3459 /*
3460 * We use 48 bit ext4_inode i_blocks
3461 * With EXT4_HUGE_FILE_FL set the i_blocks
3462 * represent total number of blocks in
3463 * file system block size
3464 */
0fc1b451
AK
3465 upper_limit = (1LL << 48) - 1;
3466
0fc1b451
AK
3467 }
3468
5c93e8ec
ZY
3469 /* Compute how many blocks we can address by block tree */
3470 res += ppb;
3471 res += ppb * ppb;
3472 res += ((loff_t)ppb) * ppb * ppb;
3473 /* Compute how many metadata blocks are needed */
3474 meta_blocks = 1;
3475 meta_blocks += 1 + ppb;
3476 meta_blocks += 1 + ppb + ppb * ppb;
3477 /* Does block tree limit file size? */
3478 if (res + meta_blocks <= upper_limit)
3479 goto check_lfs;
3480
3481 res = upper_limit;
3482 /* How many metadata blocks are needed for addressing upper_limit? */
3483 upper_limit -= EXT4_NDIR_BLOCKS;
0fc1b451
AK
3484 /* indirect blocks */
3485 meta_blocks = 1;
5c93e8ec 3486 upper_limit -= ppb;
0fc1b451 3487 /* double indirect blocks */
5c93e8ec
ZY
3488 if (upper_limit < ppb * ppb) {
3489 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
3490 res -= meta_blocks;
3491 goto check_lfs;
3492 }
3493 meta_blocks += 1 + ppb;
3494 upper_limit -= ppb * ppb;
3495 /* tripple indirect blocks for the rest */
3496 meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
3497 DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
3498 res -= meta_blocks;
3499check_lfs:
ac27a0ec 3500 res <<= bits;
0fc1b451
AK
3501 if (res > MAX_LFS_FILESIZE)
3502 res = MAX_LFS_FILESIZE;
3503
5c93e8ec 3504 return res;
ac27a0ec
DK
3505}
3506
617ba13b 3507static ext4_fsblk_t descriptor_loc(struct super_block *sb,
0b8e58a1 3508 ext4_fsblk_t logical_sb_block, int nr)
ac27a0ec 3509{
617ba13b 3510 struct ext4_sb_info *sbi = EXT4_SB(sb);
fd2d4291 3511 ext4_group_t bg, first_meta_bg;
ac27a0ec
DK
3512 int has_super = 0;
3513
3514 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3515
e2b911c5 3516 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
70bbb3e0 3517 return logical_sb_block + nr + 1;
ac27a0ec 3518 bg = sbi->s_desc_per_block * nr;
617ba13b 3519 if (ext4_bg_has_super(sb, bg))
ac27a0ec 3520 has_super = 1;
0b8e58a1 3521
bd63f6b0
DW
3522 /*
3523 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3524 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
3525 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3526 * compensate.
3527 */
3528 if (sb->s_blocksize == 1024 && nr == 0 &&
49598e04 3529 le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
bd63f6b0
DW
3530 has_super++;
3531
617ba13b 3532 return (has_super + ext4_group_first_block_no(sb, bg));
ac27a0ec
DK
3533}
3534
c9de560d
AT
3535/**
3536 * ext4_get_stripe_size: Get the stripe size.
3537 * @sbi: In memory super block info
3538 *
3539 * If we have specified it via mount option, then
3540 * use the mount option value. If the value specified at mount time is
3541 * greater than the blocks per group use the super block value.
3542 * If the super block value is greater than blocks per group return 0.
3543 * Allocator needs it be less than blocks per group.
3544 *
3545 */
3546static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3547{
3548 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3549 unsigned long stripe_width =
3550 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
3eb08658 3551 int ret;
c9de560d
AT
3552
3553 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
3eb08658 3554 ret = sbi->s_stripe;
5469d7c3 3555 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
3eb08658 3556 ret = stripe_width;
5469d7c3 3557 else if (stride && stride <= sbi->s_blocks_per_group)
3eb08658
DE
3558 ret = stride;
3559 else
3560 ret = 0;
c9de560d 3561
3eb08658
DE
3562 /*
3563 * If the stripe width is 1, this makes no sense and
3564 * we set it to 0 to turn off stripe handling code.
3565 */
3566 if (ret <= 1)
3567 ret = 0;
c9de560d 3568
3eb08658 3569 return ret;
c9de560d 3570}
ac27a0ec 3571
a13fb1a4
ES
3572/*
3573 * Check whether this filesystem can be mounted based on
3574 * the features present and the RDONLY/RDWR mount requested.
3575 * Returns 1 if this filesystem can be mounted as requested,
3576 * 0 if it cannot be.
3577 */
25c6d98f 3578int ext4_feature_set_ok(struct super_block *sb, int readonly)
a13fb1a4 3579{
e2b911c5 3580 if (ext4_has_unknown_ext4_incompat_features(sb)) {
a13fb1a4
ES
3581 ext4_msg(sb, KERN_ERR,
3582 "Couldn't mount because of "
3583 "unsupported optional features (%x)",
3584 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3585 ~EXT4_FEATURE_INCOMPAT_SUPP));
3586 return 0;
3587 }
3588
5298d4bf 3589#if !IS_ENABLED(CONFIG_UNICODE)
c83ad55e
GKB
3590 if (ext4_has_feature_casefold(sb)) {
3591 ext4_msg(sb, KERN_ERR,
3592 "Filesystem with casefold feature cannot be "
3593 "mounted without CONFIG_UNICODE");
3594 return 0;
3595 }
3596#endif
3597
a13fb1a4
ES
3598 if (readonly)
3599 return 1;
3600
e2b911c5 3601 if (ext4_has_feature_readonly(sb)) {
2cb5cc8b 3602 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
1751e8a6 3603 sb->s_flags |= SB_RDONLY;
2cb5cc8b
DW
3604 return 1;
3605 }
3606
a13fb1a4 3607 /* Check that feature set is OK for a read-write mount */
e2b911c5 3608 if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
a13fb1a4
ES
3609 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3610 "unsupported optional features (%x)",
3611 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3612 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3613 return 0;
3614 }
e2b911c5 3615 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
bab08ab9
TT
3616 ext4_msg(sb, KERN_ERR,
3617 "Can't support bigalloc feature without "
3618 "extents feature\n");
3619 return 0;
3620 }
7c319d32 3621
9db176bc 3622#if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
d65d87a0
TT
3623 if (!readonly && (ext4_has_feature_quota(sb) ||
3624 ext4_has_feature_project(sb))) {
7c319d32 3625 ext4_msg(sb, KERN_ERR,
d65d87a0 3626 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
689c958c
LX
3627 return 0;
3628 }
7c319d32 3629#endif /* CONFIG_QUOTA */
a13fb1a4
ES
3630 return 1;
3631}
3632
66e61a9e
TT
3633/*
3634 * This function is called once a day if we have errors logged
3635 * on the file system
3636 */
235699a8 3637static void print_daily_error_info(struct timer_list *t)
66e61a9e 3638{
235699a8
KC
3639 struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3640 struct super_block *sb = sbi->s_sb;
3641 struct ext4_super_block *es = sbi->s_es;
66e61a9e
TT
3642
3643 if (es->s_error_count)
ae0f78de
TT
3644 /* fsck newer than v1.41.13 is needed to clean this condition. */
3645 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
66e61a9e
TT
3646 le32_to_cpu(es->s_error_count));
3647 if (es->s_first_error_time) {
6a0678a7
AB
3648 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3649 sb->s_id,
3650 ext4_get_tstamp(es, s_first_error_time),
66e61a9e
TT
3651 (int) sizeof(es->s_first_error_func),
3652 es->s_first_error_func,
3653 le32_to_cpu(es->s_first_error_line));
3654 if (es->s_first_error_ino)
651e1c3b 3655 printk(KERN_CONT ": inode %u",
66e61a9e
TT
3656 le32_to_cpu(es->s_first_error_ino));
3657 if (es->s_first_error_block)
651e1c3b 3658 printk(KERN_CONT ": block %llu", (unsigned long long)
66e61a9e 3659 le64_to_cpu(es->s_first_error_block));
651e1c3b 3660 printk(KERN_CONT "\n");
66e61a9e
TT
3661 }
3662 if (es->s_last_error_time) {
6a0678a7
AB
3663 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3664 sb->s_id,
3665 ext4_get_tstamp(es, s_last_error_time),
66e61a9e
TT
3666 (int) sizeof(es->s_last_error_func),
3667 es->s_last_error_func,
3668 le32_to_cpu(es->s_last_error_line));
3669 if (es->s_last_error_ino)
651e1c3b 3670 printk(KERN_CONT ": inode %u",
66e61a9e
TT
3671 le32_to_cpu(es->s_last_error_ino));
3672 if (es->s_last_error_block)
651e1c3b 3673 printk(KERN_CONT ": block %llu", (unsigned long long)
66e61a9e 3674 le64_to_cpu(es->s_last_error_block));
651e1c3b 3675 printk(KERN_CONT "\n");
66e61a9e
TT
3676 }
3677 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
3678}
3679
bfff6873
LC
3680/* Find next suitable group and run ext4_init_inode_table */
3681static int ext4_run_li_request(struct ext4_li_request *elr)
3682{
3683 struct ext4_group_desc *gdp = NULL;
3d392b26
TT
3684 struct super_block *sb = elr->lr_super;
3685 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3686 ext4_group_t group = elr->lr_next_group;
3d392b26 3687 unsigned int prefetch_ios = 0;
bfff6873 3688 int ret = 0;
4f3d1e45 3689 int nr = EXT4_SB(sb)->s_mb_prefetch;
39fec688 3690 u64 start_time;
bfff6873 3691
3d392b26 3692 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
4f3d1e45
OM
3693 elr->lr_next_group = ext4_mb_prefetch(sb, group, nr, &prefetch_ios);
3694 ext4_mb_prefetch_fini(sb, elr->lr_next_group, nr);
3695 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, nr);
3d392b26
TT
3696 if (group >= elr->lr_next_group) {
3697 ret = 1;
3698 if (elr->lr_first_not_zeroed != ngroups &&
3699 !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3700 elr->lr_next_group = elr->lr_first_not_zeroed;
3701 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3702 ret = 0;
3703 }
3704 }
3705 return ret;
3706 }
bfff6873 3707
3d392b26 3708 for (; group < ngroups; group++) {
bfff6873
LC
3709 gdp = ext4_get_group_desc(sb, group, NULL);
3710 if (!gdp) {
3711 ret = 1;
3712 break;
3713 }
3714
3715 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3716 break;
3717 }
3718
7f511862 3719 if (group >= ngroups)
bfff6873
LC
3720 ret = 1;
3721
3722 if (!ret) {
39fec688 3723 start_time = ktime_get_real_ns();
bfff6873
LC
3724 ret = ext4_init_inode_table(sb, group,
3725 elr->lr_timeout ? 0 : 1);
3d392b26 3726 trace_ext4_lazy_itable_init(sb, group);
bfff6873 3727 if (elr->lr_timeout == 0) {
39fec688
SX
3728 elr->lr_timeout = nsecs_to_jiffies((ktime_get_real_ns() - start_time) *
3729 EXT4_SB(elr->lr_super)->s_li_wait_mult);
bfff6873
LC
3730 }
3731 elr->lr_next_sched = jiffies + elr->lr_timeout;
3732 elr->lr_next_group = group + 1;
3733 }
bfff6873
LC
3734 return ret;
3735}
3736
3737/*
3738 * Remove lr_request from the list_request and free the
4ed5c033 3739 * request structure. Should be called with li_list_mtx held
bfff6873
LC
3740 */
3741static void ext4_remove_li_request(struct ext4_li_request *elr)
3742{
bfff6873
LC
3743 if (!elr)
3744 return;
3745
bfff6873 3746 list_del(&elr->lr_request);
3d392b26 3747 EXT4_SB(elr->lr_super)->s_li_request = NULL;
bfff6873
LC
3748 kfree(elr);
3749}
3750
3751static void ext4_unregister_li_request(struct super_block *sb)
3752{
1bb933fb
LC
3753 mutex_lock(&ext4_li_mtx);
3754 if (!ext4_li_info) {
3755 mutex_unlock(&ext4_li_mtx);
bfff6873 3756 return;
1bb933fb 3757 }
bfff6873
LC
3758
3759 mutex_lock(&ext4_li_info->li_list_mtx);
1bb933fb 3760 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
bfff6873 3761 mutex_unlock(&ext4_li_info->li_list_mtx);
1bb933fb 3762 mutex_unlock(&ext4_li_mtx);
bfff6873
LC
3763}
3764
8f1f7453
ES
3765static struct task_struct *ext4_lazyinit_task;
3766
bfff6873
LC
3767/*
3768 * This is the function where ext4lazyinit thread lives. It walks
3769 * through the request list searching for next scheduled filesystem.
3770 * When such a fs is found, run the lazy initialization request
3771 * (ext4_rn_li_request) and keep track of the time spend in this
3772 * function. Based on that time we compute next schedule time of
3773 * the request. When walking through the list is complete, compute
3774 * next waking time and put itself into sleep.
3775 */
3776static int ext4_lazyinit_thread(void *arg)
3777{
c30365b9 3778 struct ext4_lazy_init *eli = arg;
bfff6873
LC
3779 struct list_head *pos, *n;
3780 struct ext4_li_request *elr;
4ed5c033 3781 unsigned long next_wakeup, cur;
bfff6873
LC
3782
3783 BUG_ON(NULL == eli);
3b575495 3784 set_freezable();
bfff6873 3785
bfff6873
LC
3786cont_thread:
3787 while (true) {
3788 next_wakeup = MAX_JIFFY_OFFSET;
3789
3790 mutex_lock(&eli->li_list_mtx);
3791 if (list_empty(&eli->li_request_list)) {
3792 mutex_unlock(&eli->li_list_mtx);
3793 goto exit_thread;
3794 }
bfff6873 3795 list_for_each_safe(pos, n, &eli->li_request_list) {
e22834f0
DM
3796 int err = 0;
3797 int progress = 0;
bfff6873
LC
3798 elr = list_entry(pos, struct ext4_li_request,
3799 lr_request);
3800
e22834f0
DM
3801 if (time_before(jiffies, elr->lr_next_sched)) {
3802 if (time_before(elr->lr_next_sched, next_wakeup))
3803 next_wakeup = elr->lr_next_sched;
3804 continue;
3805 }
3806 if (down_read_trylock(&elr->lr_super->s_umount)) {
3807 if (sb_start_write_trylock(elr->lr_super)) {
3808 progress = 1;
3809 /*
3810 * We hold sb->s_umount, sb can not
3811 * be removed from the list, it is
3812 * now safe to drop li_list_mtx
3813 */
3814 mutex_unlock(&eli->li_list_mtx);
3815 err = ext4_run_li_request(elr);
3816 sb_end_write(elr->lr_super);
3817 mutex_lock(&eli->li_list_mtx);
3818 n = pos->next;
b2c78cd0 3819 }
e22834f0
DM
3820 up_read((&elr->lr_super->s_umount));
3821 }
3822 /* error, remove the lazy_init job */
3823 if (err) {
3824 ext4_remove_li_request(elr);
3825 continue;
3826 }
3827 if (!progress) {
3828 elr->lr_next_sched = jiffies +
8032bf12 3829 get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
bfff6873 3830 }
bfff6873
LC
3831 if (time_before(elr->lr_next_sched, next_wakeup))
3832 next_wakeup = elr->lr_next_sched;
3833 }
3834 mutex_unlock(&eli->li_list_mtx);
3835
a0acae0e 3836 try_to_freeze();
bfff6873 3837
4ed5c033
LC
3838 cur = jiffies;
3839 if ((time_after_eq(cur, next_wakeup)) ||
f4245bd4 3840 (MAX_JIFFY_OFFSET == next_wakeup)) {
bfff6873
LC
3841 cond_resched();
3842 continue;
3843 }
3844
4ed5c033
LC
3845 schedule_timeout_interruptible(next_wakeup - cur);
3846
8f1f7453
ES
3847 if (kthread_should_stop()) {
3848 ext4_clear_request_list();
3849 goto exit_thread;
3850 }
bfff6873
LC
3851 }
3852
3853exit_thread:
3854 /*
3855 * It looks like the request list is empty, but we need
3856 * to check it under the li_list_mtx lock, to prevent any
3857 * additions into it, and of course we should lock ext4_li_mtx
3858 * to atomically free the list and ext4_li_info, because at
3859 * this point another ext4 filesystem could be registering
3860 * new one.
3861 */
3862 mutex_lock(&ext4_li_mtx);
3863 mutex_lock(&eli->li_list_mtx);
3864 if (!list_empty(&eli->li_request_list)) {
3865 mutex_unlock(&eli->li_list_mtx);
3866 mutex_unlock(&ext4_li_mtx);
3867 goto cont_thread;
3868 }
3869 mutex_unlock(&eli->li_list_mtx);
bfff6873
LC
3870 kfree(ext4_li_info);
3871 ext4_li_info = NULL;
3872 mutex_unlock(&ext4_li_mtx);
3873
3874 return 0;
3875}
3876
3877static void ext4_clear_request_list(void)
3878{
3879 struct list_head *pos, *n;
3880 struct ext4_li_request *elr;
3881
3882 mutex_lock(&ext4_li_info->li_list_mtx);
bfff6873
LC
3883 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3884 elr = list_entry(pos, struct ext4_li_request,
3885 lr_request);
3886 ext4_remove_li_request(elr);
3887 }
3888 mutex_unlock(&ext4_li_info->li_list_mtx);
3889}
3890
3891static int ext4_run_lazyinit_thread(void)
3892{
8f1f7453
ES
3893 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3894 ext4_li_info, "ext4lazyinit");
3895 if (IS_ERR(ext4_lazyinit_task)) {
3896 int err = PTR_ERR(ext4_lazyinit_task);
bfff6873 3897 ext4_clear_request_list();
bfff6873
LC
3898 kfree(ext4_li_info);
3899 ext4_li_info = NULL;
92b97816 3900 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
bfff6873
LC
3901 "initialization thread\n",
3902 err);
3903 return err;
3904 }
3905 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
bfff6873
LC
3906 return 0;
3907}
3908
3909/*
3910 * Check whether it make sense to run itable init. thread or not.
3911 * If there is at least one uninitialized inode table, return
3912 * corresponding group number, else the loop goes through all
3913 * groups and return total number of groups.
3914 */
3915static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3916{
3917 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3918 struct ext4_group_desc *gdp = NULL;
3919
8844618d
TT
3920 if (!ext4_has_group_desc_csum(sb))
3921 return ngroups;
3922
bfff6873
LC
3923 for (group = 0; group < ngroups; group++) {
3924 gdp = ext4_get_group_desc(sb, group, NULL);
3925 if (!gdp)
3926 continue;
3927
50122847 3928 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
bfff6873
LC
3929 break;
3930 }
3931
3932 return group;
3933}
3934
3935static int ext4_li_info_new(void)
3936{
3937 struct ext4_lazy_init *eli = NULL;
3938
3939 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3940 if (!eli)
3941 return -ENOMEM;
3942
bfff6873
LC
3943 INIT_LIST_HEAD(&eli->li_request_list);
3944 mutex_init(&eli->li_list_mtx);
3945
bfff6873
LC
3946 eli->li_state |= EXT4_LAZYINIT_QUIT;
3947
3948 ext4_li_info = eli;
3949
3950 return 0;
3951}
3952
3953static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3954 ext4_group_t start)
3955{
bfff6873 3956 struct ext4_li_request *elr;
bfff6873
LC
3957
3958 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3959 if (!elr)
3960 return NULL;
3961
3962 elr->lr_super = sb;
3d392b26 3963 elr->lr_first_not_zeroed = start;
21175ca4 3964 if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
3d392b26
TT
3965 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3966 elr->lr_next_group = start;
21175ca4
HS
3967 } else {
3968 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3d392b26 3969 }
bfff6873
LC
3970
3971 /*
3972 * Randomize first schedule time of the request to
3973 * spread the inode table initialization requests
3974 * better.
3975 */
8032bf12 3976 elr->lr_next_sched = jiffies + get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
bfff6873
LC
3977 return elr;
3978}
3979
7f511862
TT
3980int ext4_register_li_request(struct super_block *sb,
3981 ext4_group_t first_not_zeroed)
bfff6873
LC
3982{
3983 struct ext4_sb_info *sbi = EXT4_SB(sb);
7f511862 3984 struct ext4_li_request *elr = NULL;
49598e04 3985 ext4_group_t ngroups = sbi->s_groups_count;
6c5a6cb9 3986 int ret = 0;
bfff6873 3987
7f511862 3988 mutex_lock(&ext4_li_mtx);
51ce6511
LC
3989 if (sbi->s_li_request != NULL) {
3990 /*
3991 * Reset timeout so it can be computed again, because
3992 * s_li_wait_mult might have changed.
3993 */
3994 sbi->s_li_request->lr_timeout = 0;
7f511862 3995 goto out;
51ce6511 3996 }
bfff6873 3997
426d15ad
JT
3998 if (sb_rdonly(sb) ||
3999 (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
4000 (first_not_zeroed == ngroups || !test_opt(sb, INIT_INODE_TABLE))))
7f511862 4001 goto out;
bfff6873
LC
4002
4003 elr = ext4_li_request_new(sb, first_not_zeroed);
7f511862
TT
4004 if (!elr) {
4005 ret = -ENOMEM;
4006 goto out;
4007 }
bfff6873
LC
4008
4009 if (NULL == ext4_li_info) {
4010 ret = ext4_li_info_new();
4011 if (ret)
4012 goto out;
4013 }
4014
4015 mutex_lock(&ext4_li_info->li_list_mtx);
4016 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
4017 mutex_unlock(&ext4_li_info->li_list_mtx);
4018
4019 sbi->s_li_request = elr;
46e4690b
TM
4020 /*
4021 * set elr to NULL here since it has been inserted to
4022 * the request_list and the removal and free of it is
4023 * handled by ext4_clear_request_list from now on.
4024 */
4025 elr = NULL;
bfff6873
LC
4026
4027 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
4028 ret = ext4_run_lazyinit_thread();
4029 if (ret)
4030 goto out;
4031 }
bfff6873 4032out:
beed5ecb
NK
4033 mutex_unlock(&ext4_li_mtx);
4034 if (ret)
bfff6873 4035 kfree(elr);
bfff6873
LC
4036 return ret;
4037}
4038
4039/*
4040 * We do not need to lock anything since this is called on
4041 * module unload.
4042 */
4043static void ext4_destroy_lazyinit_thread(void)
4044{
4045 /*
4046 * If thread exited earlier
4047 * there's nothing to be done.
4048 */
8f1f7453 4049 if (!ext4_li_info || !ext4_lazyinit_task)
bfff6873
LC
4050 return;
4051
8f1f7453 4052 kthread_stop(ext4_lazyinit_task);
bfff6873
LC
4053}
4054
25ed6e8a
DW
4055static int set_journal_csum_feature_set(struct super_block *sb)
4056{
4057 int ret = 1;
4058 int compat, incompat;
4059 struct ext4_sb_info *sbi = EXT4_SB(sb);
4060
9aa5d32b 4061 if (ext4_has_metadata_csum(sb)) {
db9ee220 4062 /* journal checksum v3 */
25ed6e8a 4063 compat = 0;
db9ee220 4064 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
25ed6e8a
DW
4065 } else {
4066 /* journal checksum v1 */
4067 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
4068 incompat = 0;
4069 }
4070
feb8c6d3
DW
4071 jbd2_journal_clear_features(sbi->s_journal,
4072 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
4073 JBD2_FEATURE_INCOMPAT_CSUM_V3 |
4074 JBD2_FEATURE_INCOMPAT_CSUM_V2);
25ed6e8a
DW
4075 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4076 ret = jbd2_journal_set_features(sbi->s_journal,
4077 compat, 0,
4078 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
4079 incompat);
4080 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
4081 ret = jbd2_journal_set_features(sbi->s_journal,
4082 compat, 0,
4083 incompat);
4084 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4085 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
4086 } else {
feb8c6d3
DW
4087 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
4088 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
25ed6e8a
DW
4089 }
4090
4091 return ret;
4092}
4093
952fc18e
TT
4094/*
4095 * Note: calculating the overhead so we can be compatible with
4096 * historical BSD practice is quite difficult in the face of
4097 * clusters/bigalloc. This is because multiple metadata blocks from
4098 * different block group can end up in the same allocation cluster.
4099 * Calculating the exact overhead in the face of clustered allocation
4100 * requires either O(all block bitmaps) in memory or O(number of block
4101 * groups**2) in time. We will still calculate the superblock for
4102 * older file systems --- and if we come across with a bigalloc file
4103 * system with zero in s_overhead_clusters the estimate will be close to
4104 * correct especially for very large cluster sizes --- but for newer
4105 * file systems, it's better to calculate this figure once at mkfs
4106 * time, and store it in the superblock. If the superblock value is
4107 * present (even for non-bigalloc file systems), we will use it.
4108 */
4109static int count_overhead(struct super_block *sb, ext4_group_t grp,
4110 char *buf)
4111{
4112 struct ext4_sb_info *sbi = EXT4_SB(sb);
4113 struct ext4_group_desc *gdp;
4114 ext4_fsblk_t first_block, last_block, b;
4115 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4116 int s, j, count = 0;
10b01ee9 4117 int has_super = ext4_bg_has_super(sb, grp);
952fc18e 4118
e2b911c5 4119 if (!ext4_has_feature_bigalloc(sb))
10b01ee9
TT
4120 return (has_super + ext4_bg_num_gdb(sb, grp) +
4121 (has_super ? le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0) +
0548bbb8
TT
4122 sbi->s_itb_per_group + 2);
4123
952fc18e
TT
4124 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
4125 (grp * EXT4_BLOCKS_PER_GROUP(sb));
4126 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
4127 for (i = 0; i < ngroups; i++) {
4128 gdp = ext4_get_group_desc(sb, i, NULL);
4129 b = ext4_block_bitmap(sb, gdp);
4130 if (b >= first_block && b <= last_block) {
4131 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4132 count++;
4133 }
4134 b = ext4_inode_bitmap(sb, gdp);
4135 if (b >= first_block && b <= last_block) {
4136 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
4137 count++;
4138 }
4139 b = ext4_inode_table(sb, gdp);
4140 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
4141 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
4142 int c = EXT4_B2C(sbi, b - first_block);
4143 ext4_set_bit(c, buf);
4144 count++;
4145 }
4146 if (i != grp)
4147 continue;
4148 s = 0;
4149 if (ext4_bg_has_super(sb, grp)) {
4150 ext4_set_bit(s++, buf);
4151 count++;
4152 }
c48ae41b
TT
4153 j = ext4_bg_num_gdb(sb, grp);
4154 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
4155 ext4_error(sb, "Invalid number of block group "
4156 "descriptor blocks: %d", j);
4157 j = EXT4_BLOCKS_PER_GROUP(sb) - s;
952fc18e 4158 }
c48ae41b
TT
4159 count += j;
4160 for (; j > 0; j--)
4161 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
952fc18e
TT
4162 }
4163 if (!count)
4164 return 0;
4165 return EXT4_CLUSTERS_PER_GROUP(sb) -
4166 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
4167}
4168
4169/*
4170 * Compute the overhead and stash it in sbi->s_overhead
4171 */
4172int ext4_calculate_overhead(struct super_block *sb)
4173{
4174 struct ext4_sb_info *sbi = EXT4_SB(sb);
4175 struct ext4_super_block *es = sbi->s_es;
3c816ded
EW
4176 struct inode *j_inode;
4177 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
952fc18e
TT
4178 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
4179 ext4_fsblk_t overhead = 0;
4fdb5543 4180 char *buf = (char *) get_zeroed_page(GFP_NOFS);
952fc18e 4181
952fc18e
TT
4182 if (!buf)
4183 return -ENOMEM;
4184
4185 /*
4186 * Compute the overhead (FS structures). This is constant
4187 * for a given filesystem unless the number of block groups
4188 * changes so we cache the previous value until it does.
4189 */
4190
4191 /*
4192 * All of the blocks before first_data_block are overhead
4193 */
4194 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
4195
4196 /*
4197 * Add the overhead found in each block group
4198 */
4199 for (i = 0; i < ngroups; i++) {
4200 int blks;
4201
4202 blks = count_overhead(sb, i, buf);
4203 overhead += blks;
4204 if (blks)
4205 memset(buf, 0, PAGE_SIZE);
4206 cond_resched();
4207 }
3c816ded
EW
4208
4209 /*
4210 * Add the internal journal blocks whether the journal has been
4211 * loaded or not
4212 */
61ead714 4213 if (sbi->s_journal && !sbi->s_journal_bdev_file)
ede7dc7f 4214 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
f1eec3b0
RH
4215 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
4216 /* j_inum for internal journal is non-zero */
3c816ded 4217 j_inode = ext4_get_journal_inode(sb, j_inum);
ee5c8071 4218 if (!IS_ERR(j_inode)) {
3c816ded
EW
4219 j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
4220 overhead += EXT4_NUM_B2C(sbi, j_blocks);
4221 iput(j_inode);
4222 } else {
4223 ext4_msg(sb, KERN_ERR, "can't get journal size");
4224 }
4225 }
952fc18e
TT
4226 sbi->s_overhead = overhead;
4227 smp_wmb();
4228 free_page((unsigned long) buf);
4229 return 0;
4230}
4231
b5799018 4232static void ext4_set_resv_clusters(struct super_block *sb)
27dd4385
LC
4233{
4234 ext4_fsblk_t resv_clusters;
b5799018 4235 struct ext4_sb_info *sbi = EXT4_SB(sb);
27dd4385 4236
30fac0f7
JK
4237 /*
4238 * There's no need to reserve anything when we aren't using extents.
4239 * The space estimates are exact, there are no unwritten extents,
4240 * hole punching doesn't need new metadata... This is needed especially
4241 * to keep ext2/3 backward compatibility.
4242 */
e2b911c5 4243 if (!ext4_has_feature_extents(sb))
b5799018 4244 return;
27dd4385
LC
4245 /*
4246 * By default we reserve 2% or 4096 clusters, whichever is smaller.
4247 * This should cover the situations where we can not afford to run
4248 * out of space like for example punch hole, or converting
556615dc 4249 * unwritten extents in delalloc path. In most cases such
27dd4385
LC
4250 * allocation would require 1, or 2 blocks, higher numbers are
4251 * very rare.
4252 */
b5799018
TT
4253 resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4254 sbi->s_cluster_bits);
27dd4385
LC
4255
4256 do_div(resv_clusters, 50);
4257 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
4258
b5799018 4259 atomic64_set(&sbi->s_resv_clusters, resv_clusters);
27dd4385
LC
4260}
4261
ca9b404f
RA
4262static const char *ext4_quota_mode(struct super_block *sb)
4263{
4264#ifdef CONFIG_QUOTA
4265 if (!ext4_quota_capable(sb))
4266 return "none";
4267
4268 if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
4269 return "journalled";
4270 else
4271 return "writeback";
4272#else
4273 return "disabled";
4274#endif
4275}
4276
188c299e
JK
4277static void ext4_setup_csum_trigger(struct super_block *sb,
4278 enum ext4_journal_trigger_type type,
4279 void (*trigger)(
4280 struct jbd2_buffer_trigger_type *type,
4281 struct buffer_head *bh,
4282 void *mapped_data,
4283 size_t size))
4284{
4285 struct ext4_sb_info *sbi = EXT4_SB(sb);
4286
4287 sbi->s_journal_triggers[type].sb = sb;
4288 sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
4289}
4290
7edfd85b
LC
4291static void ext4_free_sbi(struct ext4_sb_info *sbi)
4292{
4293 if (!sbi)
4294 return;
4295
4296 kfree(sbi->s_blockgroup_lock);
8012b866 4297 fs_put_dax(sbi->s_daxdev, NULL);
7edfd85b
LC
4298 kfree(sbi);
4299}
4300
4301static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb)
4302{
4303 struct ext4_sb_info *sbi;
4304
4305 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
4306 if (!sbi)
4307 return NULL;
4308
8012b866
SR
4309 sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off,
4310 NULL, NULL);
7edfd85b
LC
4311
4312 sbi->s_blockgroup_lock =
4313 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
4314
4315 if (!sbi->s_blockgroup_lock)
4316 goto err_out;
4317
4318 sb->s_fs_info = sbi;
4319 sbi->s_sb = sb;
4320 return sbi;
4321err_out:
8012b866 4322 fs_put_dax(sbi->s_daxdev, NULL);
7edfd85b
LC
4323 kfree(sbi);
4324 return NULL;
4325}
4326
5f6d662d
JY
4327static void ext4_set_def_opts(struct super_block *sb,
4328 struct ext4_super_block *es)
4329{
4330 unsigned long def_mount_opts;
4331
4332 /* Set defaults before we parse the mount options */
4333 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
4334 set_opt(sb, INIT_INODE_TABLE);
4335 if (def_mount_opts & EXT4_DEFM_DEBUG)
4336 set_opt(sb, DEBUG);
4337 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
4338 set_opt(sb, GRPID);
4339 if (def_mount_opts & EXT4_DEFM_UID16)
4340 set_opt(sb, NO_UID32);
4341 /* xattr user namespace & acls are now defaulted on */
4342 set_opt(sb, XATTR_USER);
4343#ifdef CONFIG_EXT4_FS_POSIX_ACL
4344 set_opt(sb, POSIX_ACL);
4345#endif
4346 if (ext4_has_feature_fast_commit(sb))
4347 set_opt2(sb, JOURNAL_FAST_COMMIT);
4348 /* don't forget to enable journal_csum when metadata_csum is enabled. */
4349 if (ext4_has_metadata_csum(sb))
4350 set_opt(sb, JOURNAL_CHECKSUM);
4351
4352 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
4353 set_opt(sb, JOURNAL_DATA);
4354 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
4355 set_opt(sb, ORDERED_DATA);
4356 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
4357 set_opt(sb, WRITEBACK_DATA);
4358
4359 if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_PANIC)
4360 set_opt(sb, ERRORS_PANIC);
4361 else if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_CONTINUE)
4362 set_opt(sb, ERRORS_CONT);
4363 else
4364 set_opt(sb, ERRORS_RO);
4365 /* block_validity enabled by default; disable with noblock_validity */
4366 set_opt(sb, BLOCK_VALIDITY);
4367 if (def_mount_opts & EXT4_DEFM_DISCARD)
4368 set_opt(sb, DISCARD);
4369
4370 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
4371 set_opt(sb, BARRIER);
4372
4373 /*
4374 * enable delayed allocation by default
4375 * Use -o nodelalloc to turn it off
4376 */
4377 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
4378 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
4379 set_opt(sb, DELALLOC);
3df11e27 4380
e89fdcc4 4381 if (sb->s_blocksize <= PAGE_SIZE)
3df11e27 4382 set_opt(sb, DIOREAD_NOLOCK);
5f6d662d
JY
4383}
4384
c8267c51 4385static int ext4_handle_clustersize(struct super_block *sb)
4a8557b0
JY
4386{
4387 struct ext4_sb_info *sbi = EXT4_SB(sb);
4388 struct ext4_super_block *es = sbi->s_es;
4389 int clustersize;
4390
4391 /* Handle clustersize */
4392 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
4393 if (ext4_has_feature_bigalloc(sb)) {
c8267c51 4394 if (clustersize < sb->s_blocksize) {
4a8557b0
JY
4395 ext4_msg(sb, KERN_ERR,
4396 "cluster size (%d) smaller than "
c8267c51 4397 "block size (%lu)", clustersize, sb->s_blocksize);
4a8557b0
JY
4398 return -EINVAL;
4399 }
4400 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4401 le32_to_cpu(es->s_log_block_size);
4a8557b0 4402 } else {
c8267c51 4403 if (clustersize != sb->s_blocksize) {
4a8557b0
JY
4404 ext4_msg(sb, KERN_ERR,
4405 "fragment/cluster size (%d) != "
c8267c51 4406 "block size (%lu)", clustersize, sb->s_blocksize);
4a8557b0
JY
4407 return -EINVAL;
4408 }
c8267c51 4409 if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
4a8557b0
JY
4410 ext4_msg(sb, KERN_ERR,
4411 "#blocks per group too big: %lu",
4412 sbi->s_blocks_per_group);
4413 return -EINVAL;
4414 }
4a8557b0
JY
4415 sbi->s_cluster_bits = 0;
4416 }
40da553f
JK
4417 sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
4418 if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
4419 ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
4420 sbi->s_clusters_per_group);
4421 return -EINVAL;
4422 }
4423 if (sbi->s_blocks_per_group !=
4424 (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) {
4425 ext4_msg(sb, KERN_ERR,
4426 "blocks per group (%lu) and clusters per group (%lu) inconsistent",
4427 sbi->s_blocks_per_group, sbi->s_clusters_per_group);
4428 return -EINVAL;
4429 }
c8267c51 4430 sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
4a8557b0
JY
4431
4432 /* Do we have standard group size of clustersize * 8 blocks ? */
4433 if (sbi->s_blocks_per_group == clustersize << 3)
4434 set_opt2(sb, STD_GROUP_SIZE);
4435
4436 return 0;
4437}
4438
f7314a67
JY
4439static void ext4_fast_commit_init(struct super_block *sb)
4440{
4441 struct ext4_sb_info *sbi = EXT4_SB(sb);
4442
4443 /* Initialize fast commit stuff */
4444 atomic_set(&sbi->s_fc_subtid, 0);
4445 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4446 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4447 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4448 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4449 sbi->s_fc_bytes = 0;
4450 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4451 sbi->s_fc_ineligible_tid = 0;
4452 spin_lock_init(&sbi->s_fc_lock);
4453 memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
4454 sbi->s_fc_replay_state.fc_regions = NULL;
4455 sbi->s_fc_replay_state.fc_regions_size = 0;
4456 sbi->s_fc_replay_state.fc_regions_used = 0;
4457 sbi->s_fc_replay_state.fc_regions_valid = 0;
4458 sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4459 sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4460 sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
4461}
4462
0e495f7c 4463static int ext4_inode_info_init(struct super_block *sb,
c8267c51 4464 struct ext4_super_block *es)
0e495f7c
JY
4465{
4466 struct ext4_sb_info *sbi = EXT4_SB(sb);
4467
4468 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4469 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4470 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4471 } else {
4472 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4473 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4474 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4475 ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4476 sbi->s_first_ino);
4477 return -EINVAL;
4478 }
4479 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4480 (!is_power_of_2(sbi->s_inode_size)) ||
c8267c51 4481 (sbi->s_inode_size > sb->s_blocksize)) {
0e495f7c
JY
4482 ext4_msg(sb, KERN_ERR,
4483 "unsupported inode size: %d",
4484 sbi->s_inode_size);
c8267c51 4485 ext4_msg(sb, KERN_ERR, "blocksize: %lu", sb->s_blocksize);
0e495f7c
JY
4486 return -EINVAL;
4487 }
4488 /*
4489 * i_atime_extra is the last extra field available for
4490 * [acm]times in struct ext4_inode. Checking for that
4491 * field should suffice to ensure we have extra space
4492 * for all three.
4493 */
4494 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4495 sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4496 sb->s_time_gran = 1;
4497 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4498 } else {
4499 sb->s_time_gran = NSEC_PER_SEC;
4500 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4501 }
4502 sb->s_time_min = EXT4_TIMESTAMP_MIN;
4503 }
4504
4505 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4506 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4507 EXT4_GOOD_OLD_INODE_SIZE;
4508 if (ext4_has_feature_extra_isize(sb)) {
4509 unsigned v, max = (sbi->s_inode_size -
4510 EXT4_GOOD_OLD_INODE_SIZE);
4511
4512 v = le16_to_cpu(es->s_want_extra_isize);
4513 if (v > max) {
4514 ext4_msg(sb, KERN_ERR,
4515 "bad s_want_extra_isize: %d", v);
4516 return -EINVAL;
4517 }
4518 if (sbi->s_want_extra_isize < v)
4519 sbi->s_want_extra_isize = v;
4520
4521 v = le16_to_cpu(es->s_min_extra_isize);
4522 if (v > max) {
4523 ext4_msg(sb, KERN_ERR,
4524 "bad s_min_extra_isize: %d", v);
4525 return -EINVAL;
4526 }
4527 if (sbi->s_want_extra_isize < v)
4528 sbi->s_want_extra_isize = v;
4529 }
4530 }
4531
4532 return 0;
4533}
4534
39c135b0
JY
4535#if IS_ENABLED(CONFIG_UNICODE)
4536static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
4537{
4538 const struct ext4_sb_encodings *encoding_info;
4539 struct unicode_map *encoding;
4540 __u16 encoding_flags = le16_to_cpu(es->s_encoding_flags);
4541
4542 if (!ext4_has_feature_casefold(sb) || sb->s_encoding)
4543 return 0;
4544
4545 encoding_info = ext4_sb_read_encoding(es);
4546 if (!encoding_info) {
4547 ext4_msg(sb, KERN_ERR,
4548 "Encoding requested by superblock is unknown");
4549 return -EINVAL;
4550 }
4551
4552 encoding = utf8_load(encoding_info->version);
4553 if (IS_ERR(encoding)) {
4554 ext4_msg(sb, KERN_ERR,
4555 "can't mount with superblock charset: %s-%u.%u.%u "
4556 "not supported by the kernel. flags: 0x%x.",
4557 encoding_info->name,
4558 unicode_major(encoding_info->version),
4559 unicode_minor(encoding_info->version),
4560 unicode_rev(encoding_info->version),
4561 encoding_flags);
4562 return -EINVAL;
4563 }
4564 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4565 "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
4566 unicode_major(encoding_info->version),
4567 unicode_minor(encoding_info->version),
4568 unicode_rev(encoding_info->version),
4569 encoding_flags);
4570
4571 sb->s_encoding = encoding;
4572 sb->s_encoding_flags = encoding_flags;
4573
4574 return 0;
4575}
4576#else
4577static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
4578{
4579 return 0;
4580}
4581#endif
4582
b26458d1
JY
4583static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es)
4584{
4585 struct ext4_sb_info *sbi = EXT4_SB(sb);
4586
4587 /* Warn if metadata_csum and gdt_csum are both set. */
4588 if (ext4_has_feature_metadata_csum(sb) &&
4589 ext4_has_feature_gdt_csum(sb))
4590 ext4_warning(sb, "metadata_csum and uninit_bg are "
4591 "redundant flags; please run fsck.");
4592
4593 /* Check for a known checksum algorithm */
4594 if (!ext4_verify_csum_type(sb, es)) {
4595 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4596 "unknown checksum algorithm.");
4597 return -EINVAL;
4598 }
4599 ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
4600 ext4_orphan_file_block_trigger);
4601
4602 /* Load the checksum driver */
4603 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
4604 if (IS_ERR(sbi->s_chksum_driver)) {
4605 int ret = PTR_ERR(sbi->s_chksum_driver);
4606 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
4607 sbi->s_chksum_driver = NULL;
4608 return ret;
4609 }
4610
4611 /* Check superblock checksum */
4612 if (!ext4_superblock_csum_verify(sb, es)) {
4613 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4614 "invalid superblock checksum. Run e2fsck?");
4615 return -EFSBADCRC;
4616 }
4617
4618 /* Precompute checksum seed for all metadata */
4619 if (ext4_has_feature_csum_seed(sb))
4620 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
4621 else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
4622 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4623 sizeof(es->s_uuid));
4624 return 0;
4625}
4626
d7f3542b
JY
4627static int ext4_check_feature_compatibility(struct super_block *sb,
4628 struct ext4_super_block *es,
4629 int silent)
4630{
54902099
JY
4631 struct ext4_sb_info *sbi = EXT4_SB(sb);
4632
d7f3542b
JY
4633 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
4634 (ext4_has_compat_features(sb) ||
4635 ext4_has_ro_compat_features(sb) ||
4636 ext4_has_incompat_features(sb)))
4637 ext4_msg(sb, KERN_WARNING,
4638 "feature flags set on rev 0 fs, "
4639 "running e2fsck is recommended");
4640
4641 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4642 set_opt2(sb, HURD_COMPAT);
4643 if (ext4_has_feature_64bit(sb)) {
4644 ext4_msg(sb, KERN_ERR,
4645 "The Hurd can't support 64-bit file systems");
4646 return -EINVAL;
4647 }
4648
4649 /*
4650 * ea_inode feature uses l_i_version field which is not
4651 * available in HURD_COMPAT mode.
4652 */
4653 if (ext4_has_feature_ea_inode(sb)) {
4654 ext4_msg(sb, KERN_ERR,
4655 "ea_inode feature is not supported for Hurd");
4656 return -EINVAL;
4657 }
4658 }
4659
4660 if (IS_EXT2_SB(sb)) {
4661 if (ext2_feature_set_ok(sb))
4662 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4663 "using the ext4 subsystem");
4664 else {
4665 /*
4666 * If we're probing be silent, if this looks like
4667 * it's actually an ext[34] filesystem.
4668 */
4669 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4670 return -EINVAL;
4671 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4672 "to feature incompatibilities");
4673 return -EINVAL;
4674 }
4675 }
4676
4677 if (IS_EXT3_SB(sb)) {
4678 if (ext3_feature_set_ok(sb))
4679 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4680 "using the ext4 subsystem");
4681 else {
4682 /*
4683 * If we're probing be silent, if this looks like
4684 * it's actually an ext4 filesystem.
4685 */
4686 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4687 return -EINVAL;
4688 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4689 "to feature incompatibilities");
4690 return -EINVAL;
4691 }
4692 }
4693
4694 /*
4695 * Check feature flags regardless of the revision level, since we
4696 * previously didn't change the revision level when setting the flags,
4697 * so there is a chance incompat flags are set on a rev 0 filesystem.
4698 */
4699 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
4700 return -EINVAL;
4701
54902099
JY
4702 if (sbi->s_daxdev) {
4703 if (sb->s_blocksize == PAGE_SIZE)
4704 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4705 else
4706 ext4_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n");
4707 }
4708
4709 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
4710 if (ext4_has_feature_inline_data(sb)) {
4711 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4712 " that may contain inline data");
4713 return -EINVAL;
4714 }
4715 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
4716 ext4_msg(sb, KERN_ERR,
4717 "DAX unsupported by block device.");
4718 return -EINVAL;
4719 }
4720 }
4721
4722 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
4723 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4724 es->s_encryption_level);
4725 return -EINVAL;
4726 }
4727
d7f3542b
JY
4728 return 0;
4729}
4730
68e62439 4731static int ext4_check_geometry(struct super_block *sb,
bc62dbf9
JY
4732 struct ext4_super_block *es)
4733{
4734 struct ext4_sb_info *sbi = EXT4_SB(sb);
4735 __u64 blocks_count;
269e9226
JY
4736 int err;
4737
4738 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (sb->s_blocksize / 4)) {
4739 ext4_msg(sb, KERN_ERR,
4740 "Number of reserved GDT blocks insanely large: %d",
4741 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4742 return -EINVAL;
4743 }
4744 /*
4745 * Test whether we have more sectors than will fit in sector_t,
4746 * and whether the max offset is addressable by the page cache.
4747 */
4748 err = generic_check_addressable(sb->s_blocksize_bits,
4749 ext4_blocks_count(es));
4750 if (err) {
4751 ext4_msg(sb, KERN_ERR, "filesystem"
4752 " too large to mount safely on this system");
4753 return err;
4754 }
bc62dbf9
JY
4755
4756 /* check blocks count against device size */
4757 blocks_count = sb_bdev_nr_blocks(sb);
4758 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
4759 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4760 "exceeds size of device (%llu blocks)",
4761 ext4_blocks_count(es), blocks_count);
4762 return -EINVAL;
4763 }
4764
4765 /*
4766 * It makes no sense for the first data block to be beyond the end
4767 * of the filesystem.
4768 */
4769 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
4770 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4771 "block %u is beyond end of filesystem (%llu)",
4772 le32_to_cpu(es->s_first_data_block),
4773 ext4_blocks_count(es));
4774 return -EINVAL;
4775 }
4776 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4777 (sbi->s_cluster_ratio == 1)) {
4778 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4779 "block is 0 with a 1k block and cluster size");
4780 return -EINVAL;
4781 }
4782
4783 blocks_count = (ext4_blocks_count(es) -
4784 le32_to_cpu(es->s_first_data_block) +
4785 EXT4_BLOCKS_PER_GROUP(sb) - 1);
4786 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
4787 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
4788 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
4789 "(block count %llu, first data block %u, "
4790 "blocks per group %lu)", blocks_count,
4791 ext4_blocks_count(es),
4792 le32_to_cpu(es->s_first_data_block),
4793 EXT4_BLOCKS_PER_GROUP(sb));
4794 return -EINVAL;
4795 }
4796 sbi->s_groups_count = blocks_count;
4797 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4798 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
4799 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4800 le32_to_cpu(es->s_inodes_count)) {
4801 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4802 le32_to_cpu(es->s_inodes_count),
4803 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4804 return -EINVAL;
4805 }
4806
4807 return 0;
4808}
4809
a4e6a511
JY
4810static int ext4_group_desc_init(struct super_block *sb,
4811 struct ext4_super_block *es,
4812 ext4_fsblk_t logical_sb_block,
4813 ext4_group_t *first_not_zeroed)
4814{
4815 struct ext4_sb_info *sbi = EXT4_SB(sb);
4816 unsigned int db_count;
4817 ext4_fsblk_t block;
a4e6a511
JY
4818 int i;
4819
4820 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4821 EXT4_DESC_PER_BLOCK(sb);
4822 if (ext4_has_feature_meta_bg(sb)) {
4823 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
4824 ext4_msg(sb, KERN_WARNING,
4825 "first meta block group too large: %u "
4826 "(group descriptor block count %u)",
4827 le32_to_cpu(es->s_first_meta_bg), db_count);
4828 return -EINVAL;
4829 }
4830 }
4831 rcu_assign_pointer(sbi->s_group_desc,
4832 kvmalloc_array(db_count,
4833 sizeof(struct buffer_head *),
4834 GFP_KERNEL));
4835 if (sbi->s_group_desc == NULL) {
4836 ext4_msg(sb, KERN_ERR, "not enough memory");
4837 return -ENOMEM;
4838 }
4839
4840 bgl_lock_init(sbi->s_blockgroup_lock);
4841
4842 /* Pre-read the descriptors into the buffer cache */
4843 for (i = 0; i < db_count; i++) {
4844 block = descriptor_loc(sb, logical_sb_block, i);
4845 ext4_sb_breadahead_unmovable(sb, block);
4846 }
4847
4848 for (i = 0; i < db_count; i++) {
4849 struct buffer_head *bh;
4850
4851 block = descriptor_loc(sb, logical_sb_block, i);
4852 bh = ext4_sb_bread_unmovable(sb, block);
4853 if (IS_ERR(bh)) {
4854 ext4_msg(sb, KERN_ERR,
4855 "can't read group descriptor %d", i);
4856 sbi->s_gdb_count = i;
172e344e 4857 return PTR_ERR(bh);
a4e6a511
JY
4858 }
4859 rcu_read_lock();
4860 rcu_dereference(sbi->s_group_desc)[i] = bh;
4861 rcu_read_unlock();
4862 }
4863 sbi->s_gdb_count = db_count;
4864 if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) {
4865 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
172e344e 4866 return -EFSCORRUPTED;
a4e6a511 4867 }
172e344e 4868
a4e6a511 4869 return 0;
a4e6a511
JY
4870}
4871
9c1dd22d
JY
4872static int ext4_load_and_init_journal(struct super_block *sb,
4873 struct ext4_super_block *es,
4874 struct ext4_fs_context *ctx)
4875{
4876 struct ext4_sb_info *sbi = EXT4_SB(sb);
4877 int err;
4878
4879 err = ext4_load_journal(sb, es, ctx->journal_devnum);
4880 if (err)
4881 return err;
4882
4883 if (ext4_has_feature_64bit(sb) &&
4884 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4885 JBD2_FEATURE_INCOMPAT_64BIT)) {
4886 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4887 goto out;
4888 }
4889
4890 if (!set_journal_csum_feature_set(sb)) {
4891 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4892 "feature set");
4893 goto out;
4894 }
4895
4896 if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4897 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4898 JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4899 ext4_msg(sb, KERN_ERR,
4900 "Failed to set fast commit journal feature");
4901 goto out;
4902 }
4903
4904 /* We have now updated the journal if required, so we can
4905 * validate the data journaling mode. */
4906 switch (test_opt(sb, DATA_FLAGS)) {
4907 case 0:
4908 /* No mode set, assume a default based on the journal
4909 * capabilities: ORDERED_DATA if the journal can
4910 * cope, else JOURNAL_DATA
4911 */
4912 if (jbd2_journal_check_available_features
4913 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4914 set_opt(sb, ORDERED_DATA);
4915 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4916 } else {
4917 set_opt(sb, JOURNAL_DATA);
4918 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4919 }
4920 break;
4921
4922 case EXT4_MOUNT_ORDERED_DATA:
4923 case EXT4_MOUNT_WRITEBACK_DATA:
4924 if (!jbd2_journal_check_available_features
4925 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4926 ext4_msg(sb, KERN_ERR, "Journal does not support "
4927 "requested data journaling mode");
4928 goto out;
4929 }
4930 break;
4931 default:
4932 break;
4933 }
4934
4935 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4936 test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4937 ext4_msg(sb, KERN_ERR, "can't mount with "
4938 "journal_async_commit in data=ordered mode");
4939 goto out;
4940 }
4941
4942 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
4943
4944 sbi->s_journal->j_submit_inode_data_buffers =
4945 ext4_journal_submit_inode_data_buffers;
4946 sbi->s_journal->j_finish_inode_data_buffers =
4947 ext4_journal_finish_inode_data_buffers;
4948
4949 return 0;
4950
4951out:
bb15cea2
TT
4952 /* flush s_sb_upd_work before destroying the journal. */
4953 flush_work(&sbi->s_sb_upd_work);
9c1dd22d
JY
4954 jbd2_journal_destroy(sbi->s_journal);
4955 sbi->s_journal = NULL;
9f2a1d9f 4956 return -EINVAL;
9c1dd22d
JY
4957}
4958
68e62439 4959static int ext4_check_journal_data_mode(struct super_block *sb)
a5991e53
JY
4960{
4961 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4962 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with "
4963 "data=journal disables delayed allocation, "
4964 "dioread_nolock, O_DIRECT and fast_commit support!\n");
4965 /* can't mount with both data=journal and dioread_nolock. */
4966 clear_opt(sb, DIOREAD_NOLOCK);
4967 clear_opt2(sb, JOURNAL_FAST_COMMIT);
4968 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4969 ext4_msg(sb, KERN_ERR, "can't mount with "
4970 "both data=journal and delalloc");
4971 return -EINVAL;
4972 }
4973 if (test_opt(sb, DAX_ALWAYS)) {
4974 ext4_msg(sb, KERN_ERR, "can't mount with "
4975 "both data=journal and dax");
4976 return -EINVAL;
4977 }
4978 if (ext4_has_feature_encrypt(sb)) {
4979 ext4_msg(sb, KERN_WARNING,
4980 "encrypted files will use data=ordered "
4981 "instead of data journaling mode");
4982 }
4983 if (test_opt(sb, DELALLOC))
4984 clear_opt(sb, DELALLOC);
4985 } else {
4986 sb->s_iflags |= SB_I_CGROUPWB;
4987 }
4988
4989 return 0;
4990}
4991
a7a79c29
JY
4992static int ext4_load_super(struct super_block *sb, ext4_fsblk_t *lsb,
4993 int silent)
ac27a0ec 4994{
7edfd85b 4995 struct ext4_sb_info *sbi = EXT4_SB(sb);
a7a79c29 4996 struct ext4_super_block *es;
70bbb3e0 4997 ext4_fsblk_t logical_sb_block;
ac27a0ec 4998 unsigned long offset = 0;
a7a79c29
JY
4999 struct buffer_head *bh;
5000 int ret = -EINVAL;
4a8557b0 5001 int blocksize;
705895b6 5002
617ba13b 5003 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
ac27a0ec 5004 if (!blocksize) {
b31e1552 5005 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
a7a79c29 5006 return -EINVAL;
ac27a0ec
DK
5007 }
5008
5009 /*
617ba13b 5010 * The ext4 superblock will not be buffer aligned for other than 1kB
ac27a0ec
DK
5011 * block sizes. We need to calculate the offset from buffer start.
5012 */
617ba13b 5013 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
7edfd85b 5014 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
70bbb3e0 5015 offset = do_div(logical_sb_block, blocksize);
ac27a0ec 5016 } else {
7edfd85b 5017 logical_sb_block = sbi->s_sb_block;
ac27a0ec
DK
5018 }
5019
8394a6ab 5020 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5021 if (IS_ERR(bh)) {
b31e1552 5022 ext4_msg(sb, KERN_ERR, "unable to read superblock");
a7a79c29 5023 return PTR_ERR(bh);
ac27a0ec
DK
5024 }
5025 /*
5026 * Note: s_es must be initialized as soon as possible because
617ba13b 5027 * some ext4 macro-instructions depend on its value
ac27a0ec 5028 */
2716b802 5029 es = (struct ext4_super_block *) (bh->b_data + offset);
ac27a0ec
DK
5030 sbi->s_es = es;
5031 sb->s_magic = le16_to_cpu(es->s_magic);
a5fc5119
JY
5032 if (sb->s_magic != EXT4_SUPER_MAGIC) {
5033 if (!silent)
5034 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
a7a79c29 5035 goto out;
a5fc5119
JY
5036 }
5037
a7a79c29
JY
5038 if (le32_to_cpu(es->s_log_block_size) >
5039 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5040 ext4_msg(sb, KERN_ERR,
5041 "Invalid log block size: %u",
5042 le32_to_cpu(es->s_log_block_size));
5043 goto out;
5044 }
5045 if (le32_to_cpu(es->s_log_cluster_size) >
5046 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
5047 ext4_msg(sb, KERN_ERR,
5048 "Invalid log cluster size: %u",
5049 le32_to_cpu(es->s_log_cluster_size));
5050 goto out;
5051 }
5052
5053 blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
5054
5055 /*
5056 * If the default block size is not the same as the real block size,
5057 * we need to reload it.
5058 */
5059 if (sb->s_blocksize == blocksize) {
5060 *lsb = logical_sb_block;
5061 sbi->s_sbh = bh;
5062 return 0;
5063 }
5064
5065 /*
5066 * bh must be released before kill_bdev(), otherwise
5067 * it won't be freed and its page also. kill_bdev()
5068 * is called by sb_set_blocksize().
5069 */
5070 brelse(bh);
5071 /* Validate the filesystem blocksize */
5072 if (!sb_set_blocksize(sb, blocksize)) {
5073 ext4_msg(sb, KERN_ERR, "bad block size %d",
5074 blocksize);
5075 bh = NULL;
5076 goto out;
5077 }
5078
5079 logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
5080 offset = do_div(logical_sb_block, blocksize);
5081 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
5082 if (IS_ERR(bh)) {
5083 ext4_msg(sb, KERN_ERR, "Can't read superblock on 2nd try");
5084 ret = PTR_ERR(bh);
5085 bh = NULL;
5086 goto out;
5087 }
5088 es = (struct ext4_super_block *)(bh->b_data + offset);
5089 sbi->s_es = es;
5090 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
5091 ext4_msg(sb, KERN_ERR, "Magic mismatch, very weird!");
5092 goto out;
5093 }
5094 *lsb = logical_sb_block;
5095 sbi->s_sbh = bh;
5096 return 0;
5097out:
5098 brelse(bh);
5099 return ret;
5100}
5101
db9345d9
JY
5102static void ext4_hash_info_init(struct super_block *sb)
5103{
5104 struct ext4_sb_info *sbi = EXT4_SB(sb);
5105 struct ext4_super_block *es = sbi->s_es;
5106 unsigned int i;
5107
5108 for (i = 0; i < 4; i++)
5109 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
5110
5111 sbi->s_def_hash_version = es->s_def_hash_version;
5112 if (ext4_has_feature_dir_index(sb)) {
5113 i = le32_to_cpu(es->s_flags);
5114 if (i & EXT2_FLAGS_UNSIGNED_HASH)
5115 sbi->s_hash_unsigned = 3;
5116 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
5117#ifdef __CHAR_UNSIGNED__
5118 if (!sb_rdonly(sb))
5119 es->s_flags |=
5120 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
5121 sbi->s_hash_unsigned = 3;
5122#else
5123 if (!sb_rdonly(sb))
5124 es->s_flags |=
5125 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
5126#endif
5127 }
5128 }
5129}
5130
107d2be9
JY
5131static int ext4_block_group_meta_init(struct super_block *sb, int silent)
5132{
5133 struct ext4_sb_info *sbi = EXT4_SB(sb);
5134 struct ext4_super_block *es = sbi->s_es;
5135 int has_huge_files;
5136
5137 has_huge_files = ext4_has_feature_huge_file(sb);
5138 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
5139 has_huge_files);
5140 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
5141
5142 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
5143 if (ext4_has_feature_64bit(sb)) {
5144 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
5145 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
5146 !is_power_of_2(sbi->s_desc_size)) {
5147 ext4_msg(sb, KERN_ERR,
5148 "unsupported descriptor size %lu",
5149 sbi->s_desc_size);
5150 return -EINVAL;
5151 }
5152 } else
5153 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
5154
5155 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
5156 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
5157
5158 sbi->s_inodes_per_block = sb->s_blocksize / EXT4_INODE_SIZE(sb);
5159 if (sbi->s_inodes_per_block == 0 || sbi->s_blocks_per_group == 0) {
5160 if (!silent)
5161 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
5162 return -EINVAL;
5163 }
5164 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
5165 sbi->s_inodes_per_group > sb->s_blocksize * 8) {
5166 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
5167 sbi->s_inodes_per_group);
5168 return -EINVAL;
5169 }
5170 sbi->s_itb_per_group = sbi->s_inodes_per_group /
5171 sbi->s_inodes_per_block;
5172 sbi->s_desc_per_block = sb->s_blocksize / EXT4_DESC_SIZE(sb);
5173 sbi->s_mount_state = le16_to_cpu(es->s_state) & ~EXT4_FC_REPLAY;
5174 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
5175 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
5176
5177 return 0;
5178}
5179
a7a79c29
JY
5180static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
5181{
5182 struct ext4_super_block *es = NULL;
5183 struct ext4_sb_info *sbi = EXT4_SB(sb);
a7a79c29
JY
5184 ext4_fsblk_t logical_sb_block;
5185 struct inode *root;
107d2be9 5186 int needs_recovery;
d4fab7b2 5187 int err;
a7a79c29
JY
5188 ext4_group_t first_not_zeroed;
5189 struct ext4_fs_context *ctx = fc->fs_private;
5190 int silent = fc->sb_flags & SB_SILENT;
5191
5192 /* Set defaults for the variables that will be set during parsing */
5193 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO))
5194 ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
5195
5196 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
5197 sbi->s_sectors_written_start =
5198 part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
5199
a7a79c29
JY
5200 err = ext4_load_super(sb, &logical_sb_block, silent);
5201 if (err)
5202 goto out_fail;
5203
5204 es = sbi->s_es;
afc32f7e 5205 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
ac27a0ec 5206
b26458d1
JY
5207 err = ext4_init_metadata_csum(sb, es);
5208 if (err)
a5fc5119 5209 goto failed_mount;
a9c47317 5210
5f6d662d 5211 ext4_set_def_opts(sb, es);
ac27a0ec 5212
08cefc7a
EB
5213 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
5214 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
30773840
TT
5215 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
5216 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
5217 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
ac27a0ec 5218
51ce6511
LC
5219 /*
5220 * set default s_li_wait_mult for lazyinit, for the case there is
5221 * no mount option specified.
5222 */
5223 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
5224
d4fab7b2
TT
5225 err = ext4_inode_info_init(sb, es);
5226 if (err)
0e495f7c 5227 goto failed_mount;
9803387c 5228
7edfd85b
LC
5229 err = parse_apply_sb_mount_options(sb, ctx);
5230 if (err < 0)
5231 goto failed_mount;
5232
5a916be1 5233 sbi->s_def_mount_opt = sbi->s_mount_opt;
e3645d72 5234 sbi->s_def_mount_opt2 = sbi->s_mount_opt2;
7edfd85b
LC
5235
5236 err = ext4_check_opt_consistency(fc, sb);
5237 if (err < 0)
5238 goto failed_mount;
5239
85456054 5240 ext4_apply_options(fc, sb);
ac27a0ec 5241
d4fab7b2
TT
5242 err = ext4_encoding_init(sb, es);
5243 if (err)
39c135b0 5244 goto failed_mount;
c83ad55e 5245
d4fab7b2
TT
5246 err = ext4_check_journal_data_mode(sb);
5247 if (err)
a5991e53 5248 goto failed_mount;
56889787 5249
1751e8a6
LT
5250 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5251 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
ac27a0ec 5252
1ff20307
JL
5253 /* i_version is always enabled now */
5254 sb->s_flags |= SB_I_VERSION;
5255
d4fab7b2
TT
5256 err = ext4_check_feature_compatibility(sb, es, silent);
5257 if (err)
ac27a0ec 5258 goto failed_mount;
a13fb1a4 5259
d4fab7b2
TT
5260 err = ext4_block_group_meta_init(sb, silent);
5261 if (err)
5b9554dc 5262 goto failed_mount;
5b9554dc 5263
db9345d9 5264 ext4_hash_info_init(sb);
ac27a0ec 5265
d4fab7b2
TT
5266 err = ext4_handle_clustersize(sb);
5267 if (err)
4a8557b0 5268 goto failed_mount;
960fd856 5269
d4fab7b2
TT
5270 err = ext4_check_geometry(sb, es);
5271 if (err)
0f2ddca6 5272 goto failed_mount;
0f2ddca6 5273
235699a8 5274 timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
c92dc856 5275 spin_lock_init(&sbi->s_error_lock);
bb15cea2 5276 INIT_WORK(&sbi->s_sb_upd_work, update_super_work);
04496411 5277
172e344e
YB
5278 err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
5279 if (err)
5280 goto failed_mount3;
5281
d4fab7b2
TT
5282 err = ext4_es_register_shrinker(sbi);
5283 if (err)
ce7e010a 5284 goto failed_mount3;
ce7e010a 5285
c9de560d 5286 sbi->s_stripe = ext4_get_stripe_size(sbi);
c3defd99
KS
5287 /*
5288 * It's hard to get stripe aligned blocks if stripe is not aligned with
5289 * cluster, just disable stripe and alert user to simpfy code and avoid
5290 * stripe aligned allocation which will rarely successes.
5291 */
5292 if (sbi->s_stripe > 0 && sbi->s_cluster_ratio > 1 &&
5293 sbi->s_stripe % sbi->s_cluster_ratio != 0) {
5294 ext4_msg(sb, KERN_WARNING,
5295 "stripe (%lu) is not aligned with cluster size (%u), "
5296 "stripe is disabled",
5297 sbi->s_stripe, sbi->s_cluster_ratio);
5298 sbi->s_stripe = 0;
5299 }
67a5da56 5300 sbi->s_extent_max_zeroout_kb = 32;
c9de560d 5301
f9ae9cf5
TT
5302 /*
5303 * set up enough so that it can read an inode
5304 */
f6e63f90 5305 sb->s_op = &ext4_sops;
617ba13b
MC
5306 sb->s_export_op = &ext4_export_ops;
5307 sb->s_xattr = ext4_xattr_handlers;
643fa961 5308#ifdef CONFIG_FS_ENCRYPTION
a7550b30 5309 sb->s_cop = &ext4_cryptops;
ffcc4182 5310#endif
c93d8f88
EB
5311#ifdef CONFIG_FS_VERITY
5312 sb->s_vop = &ext4_verityops;
5313#endif
ac27a0ec 5314#ifdef CONFIG_QUOTA
617ba13b 5315 sb->dq_op = &ext4_quota_operations;
e2b911c5 5316 if (ext4_has_feature_quota(sb))
1fa5efe3 5317 sb->s_qcop = &dquot_quotactl_sysfile_ops;
262b4662
JK
5318 else
5319 sb->s_qcop = &ext4_qctl_operations;
689c958c 5320 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
ac27a0ec 5321#endif
a4af51ce 5322 super_set_uuid(sb, es->s_uuid, sizeof(es->s_uuid));
fb092d40 5323 super_set_sysfs_name_bdev(sb);
f2fa2ffc 5324
ac27a0ec 5325 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
3b9d4ed2 5326 mutex_init(&sbi->s_orphan_lock);
ac27a0ec 5327
f7314a67 5328 ext4_fast_commit_init(sb);
aa75f4d3 5329
ac27a0ec
DK
5330 sb->s_root = NULL;
5331
5332 needs_recovery = (es->s_last_orphan != 0 ||
02f310fc 5333 ext4_has_feature_orphan_present(sb) ||
e2b911c5 5334 ext4_has_feature_journal_needs_recovery(sb));
ac27a0ec 5335
3b50d501
TT
5336 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) {
5337 err = ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block));
5338 if (err)
50460fe8 5339 goto failed_mount3a;
3b50d501 5340 }
c5e06d10 5341
d4fab7b2 5342 err = -EINVAL;
ac27a0ec
DK
5343 /*
5344 * The first inode we look at is the journal inode. Don't try
5345 * root first: it may be modified in the journal!
5346 */
e2b911c5 5347 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
9c1dd22d 5348 err = ext4_load_and_init_journal(sb, es, ctx);
4753d8a2 5349 if (err)
50460fe8 5350 goto failed_mount3a;
bc98a42c 5351 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
e2b911c5 5352 ext4_has_feature_journal_needs_recovery(sb)) {
b31e1552
ES
5353 ext4_msg(sb, KERN_ERR, "required journal recovery "
5354 "suppressed and not mounted read-only");
43bd6f1b 5355 goto failed_mount3a;
ac27a0ec 5356 } else {
1e381f60 5357 /* Nojournal mode, all journal mount options are illegal */
89481b5f 5358 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
1e381f60 5359 ext4_msg(sb, KERN_ERR, "can't mount with "
89481b5f 5360 "journal_async_commit, fs mounted w/o journal");
43bd6f1b 5361 goto failed_mount3a;
1e381f60 5362 }
89481b5f
BL
5363
5364 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
1e381f60 5365 ext4_msg(sb, KERN_ERR, "can't mount with "
89481b5f 5366 "journal_checksum, fs mounted w/o journal");
43bd6f1b 5367 goto failed_mount3a;
1e381f60
DM
5368 }
5369 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
5370 ext4_msg(sb, KERN_ERR, "can't mount with "
5371 "commit=%lu, fs mounted w/o journal",
5372 sbi->s_commit_interval / HZ);
43bd6f1b 5373 goto failed_mount3a;
1e381f60
DM
5374 }
5375 if (EXT4_MOUNT_DATA_FLAGS &
5376 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
5377 ext4_msg(sb, KERN_ERR, "can't mount with "
5378 "data=, fs mounted w/o journal");
43bd6f1b 5379 goto failed_mount3a;
1e381f60 5380 }
50b29d8f 5381 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
1e381f60 5382 clear_opt(sb, JOURNAL_CHECKSUM);
fd8c37ec 5383 clear_opt(sb, DATA_FLAGS);
995a3ed6 5384 clear_opt2(sb, JOURNAL_FAST_COMMIT);
0390131b
FM
5385 sbi->s_journal = NULL;
5386 needs_recovery = 0;
ac27a0ec
DK
5387 }
5388
cdb7ee4c
TE
5389 if (!test_opt(sb, NO_MBCACHE)) {
5390 sbi->s_ea_block_cache = ext4_xattr_create_cache();
5391 if (!sbi->s_ea_block_cache) {
dec214d0 5392 ext4_msg(sb, KERN_ERR,
cdb7ee4c 5393 "Failed to create ea_block_cache");
d4fab7b2 5394 err = -EINVAL;
dec214d0
TE
5395 goto failed_mount_wq;
5396 }
cdb7ee4c
TE
5397
5398 if (ext4_has_feature_ea_inode(sb)) {
5399 sbi->s_ea_inode_cache = ext4_xattr_create_cache();
5400 if (!sbi->s_ea_inode_cache) {
5401 ext4_msg(sb, KERN_ERR,
5402 "Failed to create ea_inode_cache");
d4fab7b2 5403 err = -EINVAL;
cdb7ee4c
TE
5404 goto failed_mount_wq;
5405 }
5406 }
9c191f70
M
5407 }
5408
952fc18e
TT
5409 /*
5410 * Get the # of file system overhead blocks from the
5411 * superblock if present.
5412 */
85d825db
TT
5413 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
5414 /* ignore the precalculated value if it is ridiculous */
5415 if (sbi->s_overhead > ext4_blocks_count(es))
5416 sbi->s_overhead = 0;
5417 /*
5418 * If the bigalloc feature is not enabled recalculating the
5419 * overhead doesn't take long, so we might as well just redo
5420 * it to make sure we are using the correct value.
5421 */
5422 if (!ext4_has_feature_bigalloc(sb))
5423 sbi->s_overhead = 0;
5424 if (sbi->s_overhead == 0) {
07aa2ea1
LC
5425 err = ext4_calculate_overhead(sb);
5426 if (err)
952fc18e
TT
5427 goto failed_mount_wq;
5428 }
5429
fd89d5f2
TH
5430 /*
5431 * The maximum number of concurrent works can be high and
5432 * concurrency isn't really necessary. Limit it to 1.
5433 */
2e8fa54e
JK
5434 EXT4_SB(sb)->rsv_conversion_wq =
5435 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
5436 if (!EXT4_SB(sb)->rsv_conversion_wq) {
5437 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
d4fab7b2 5438 err = -ENOMEM;
2e8fa54e
JK
5439 goto failed_mount4;
5440 }
5441
ac27a0ec 5442 /*
dab291af 5443 * The jbd2_journal_load will have done any necessary log recovery,
ac27a0ec
DK
5444 * so we can safely mount the rest of the filesystem now.
5445 */
5446
8a363970 5447 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
1d1fe1ee 5448 if (IS_ERR(root)) {
b31e1552 5449 ext4_msg(sb, KERN_ERR, "get root inode failed");
d4fab7b2 5450 err = PTR_ERR(root);
32a9bb57 5451 root = NULL;
ac27a0ec
DK
5452 goto failed_mount4;
5453 }
5454 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
b31e1552 5455 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
94bf608a 5456 iput(root);
d4fab7b2 5457 err = -EFSCORRUPTED;
ac27a0ec
DK
5458 goto failed_mount4;
5459 }
b886ee3e 5460
04aa5f4e 5461 generic_set_sb_d_ops(sb);
48fde701 5462 sb->s_root = d_make_root(root);
1d1fe1ee 5463 if (!sb->s_root) {
b31e1552 5464 ext4_msg(sb, KERN_ERR, "get root dentry failed");
d4fab7b2 5465 err = -ENOMEM;
1d1fe1ee
DH
5466 goto failed_mount4;
5467 }
ac27a0ec 5468
d4fab7b2
TT
5469 err = ext4_setup_super(sb, es, sb_rdonly(sb));
5470 if (err == -EROFS) {
1751e8a6 5471 sb->s_flags |= SB_RDONLY;
d4fab7b2 5472 } else if (err)
c89128a0 5473 goto failed_mount4a;
ef7f3835 5474
b5799018 5475 ext4_set_resv_clusters(sb);
27dd4385 5476
0f5bde1d
JK
5477 if (test_opt(sb, BLOCK_VALIDITY)) {
5478 err = ext4_setup_system_zone(sb);
5479 if (err) {
5480 ext4_msg(sb, KERN_ERR, "failed to initialize system "
5481 "zone (%d)", err);
5482 goto failed_mount4a;
5483 }
f9ae9cf5 5484 }
8016e29f 5485 ext4_fc_replay_cleanup(sb);
f9ae9cf5
TT
5486
5487 ext4_ext_init(sb);
196e402a
HS
5488
5489 /*
5490 * Enable optimize_scan if number of groups is > threshold. This can be
5491 * turned off by passing "mb_optimize_scan=0". This can also be
5492 * turned on forcefully by passing "mb_optimize_scan=1".
5493 */
27b38686
OM
5494 if (!(ctx->spec & EXT4_SPEC_mb_optimize_scan)) {
5495 if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
5496 set_opt2(sb, MB_OPTIMIZE_SCAN);
5497 else
5498 clear_opt2(sb, MB_OPTIMIZE_SCAN);
5499 }
196e402a 5500
f9ae9cf5
TT
5501 err = ext4_mb_init(sb);
5502 if (err) {
5503 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5504 err);
dcf2d804 5505 goto failed_mount5;
c2774d84
AK
5506 }
5507
027f14f5
TT
5508 /*
5509 * We can only set up the journal commit callback once
5510 * mballoc is initialized
5511 */
5512 if (sbi->s_journal)
5513 sbi->s_journal->j_commit_callback =
5514 ext4_journal_commit_callback;
5515
d5e72c4e
TT
5516 err = ext4_percpu_param_init(sbi);
5517 if (err)
d5e03cbb 5518 goto failed_mount6;
d5e03cbb 5519
e2b911c5 5520 if (ext4_has_feature_flex_bg(sb))
d5e03cbb
TT
5521 if (!ext4_fill_flex_info(sb)) {
5522 ext4_msg(sb, KERN_ERR,
5523 "unable to initialize "
5524 "flex_bg meta info!");
d4fab7b2 5525 err = -ENOMEM;
d5e03cbb
TT
5526 goto failed_mount6;
5527 }
5528
bfff6873
LC
5529 err = ext4_register_li_request(sb, first_not_zeroed);
5530 if (err)
dcf2d804 5531 goto failed_mount6;
bfff6873 5532
02f310fc
JK
5533 err = ext4_init_orphan_info(sb);
5534 if (err)
b4b4fda3 5535 goto failed_mount7;
9b2ff357
JK
5536#ifdef CONFIG_QUOTA
5537 /* Enable quota usage during mount. */
bc98a42c 5538 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
9b2ff357
JK
5539 err = ext4_enable_quotas(sb);
5540 if (err)
b4b4fda3 5541 goto failed_mount8;
9b2ff357
JK
5542 }
5543#endif /* CONFIG_QUOTA */
5544
bc71726c 5545 /*
5546 * Save the original bdev mapping's wb_err value which could be
5547 * used to detect the metadata async write error.
5548 */
5549 spin_lock_init(&sbi->s_bdev_wb_lock);
224941e8 5550 errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err,
9704a322 5551 &sbi->s_bdev_wb_err);
617ba13b
MC
5552 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5553 ext4_orphan_cleanup(sb, es);
5554 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
9b6641dd
YB
5555 /*
5556 * Update the checksum after updating free space/inode counters and
5557 * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
5558 * checksum in the buffer cache until it is written out and
5559 * e2fsprogs programs trying to open a file system immediately
5560 * after it is mounted can fail.
5561 */
5562 ext4_superblock_csum_set(sb);
0390131b 5563 if (needs_recovery) {
b31e1552 5564 ext4_msg(sb, KERN_INFO, "recovery complete");
11215630
JK
5565 err = ext4_mark_recovery_complete(sb, es);
5566 if (err)
b4b4fda3 5567 goto failed_mount9;
0390131b 5568 }
0390131b 5569
70200574
CH
5570 if (test_opt(sb, DISCARD) && !bdev_max_discard_sectors(sb->s_bdev))
5571 ext4_msg(sb, KERN_WARNING,
5572 "mounting with \"discard\" option, but the device does not support discard");
79add3a3 5573
66e61a9e
TT
5574 if (es->s_error_count)
5575 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
ac27a0ec 5576
efbed4dc
TT
5577 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5578 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5579 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5580 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
1cf006ed
DM
5581 atomic_set(&sbi->s_warning_count, 0);
5582 atomic_set(&sbi->s_msg_count, 0);
efbed4dc 5583
b4b4fda3
BL
5584 /* Register sysfs after all initializations are complete. */
5585 err = ext4_register_sysfs(sb);
5586 if (err)
5587 goto failed_mount9;
5588
ac27a0ec
DK
5589 return 0;
5590
b4b4fda3 5591failed_mount9:
f3c1c42e 5592 ext4_quotas_off(sb, EXT4_MAXQUOTAS);
b4b4fda3 5593failed_mount8: __maybe_unused
02f310fc 5594 ext4_release_orphan_info(sb);
dcf2d804
TM
5595failed_mount7:
5596 ext4_unregister_li_request(sb);
5597failed_mount6:
f9ae9cf5 5598 ext4_mb_release(sb);
dcbf8758 5599 ext4_flex_groups_free(sbi);
1f79467c 5600 ext4_percpu_param_destroy(sbi);
00764937 5601failed_mount5:
f9ae9cf5
TT
5602 ext4_ext_release(sb);
5603 ext4_release_system_zone(sb);
5604failed_mount4a:
94bf608a 5605 dput(sb->s_root);
32a9bb57 5606 sb->s_root = NULL;
94bf608a 5607failed_mount4:
b31e1552 5608 ext4_msg(sb, KERN_ERR, "mount failed");
2e8fa54e
JK
5609 if (EXT4_SB(sb)->rsv_conversion_wq)
5610 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
4c0425ff 5611failed_mount_wq:
50c15df6
CX
5612 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5613 sbi->s_ea_inode_cache = NULL;
5614
5615 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
5616 sbi->s_ea_block_cache = NULL;
5617
0390131b 5618 if (sbi->s_journal) {
bb15cea2
TT
5619 /* flush s_sb_upd_work before journal destroy. */
5620 flush_work(&sbi->s_sb_upd_work);
0390131b
FM
5621 jbd2_journal_destroy(sbi->s_journal);
5622 sbi->s_journal = NULL;
5623 }
50460fe8 5624failed_mount3a:
d3922a77 5625 ext4_es_unregister_shrinker(sbi);
eb68d0e2 5626failed_mount3:
bb15cea2
TT
5627 /* flush s_sb_upd_work before sbi destroy */
5628 flush_work(&sbi->s_sb_upd_work);
2a4ae3bc 5629 del_timer_sync(&sbi->s_err_report);
618f0031 5630 ext4_stop_mmpd(sbi);
a4e6a511 5631 ext4_group_desc_free(sbi);
ac27a0ec 5632failed_mount:
0441984a
DW
5633 if (sbi->s_chksum_driver)
5634 crypto_free_shash(sbi->s_chksum_driver);
c83ad55e 5635
5298d4bf 5636#if IS_ENABLED(CONFIG_UNICODE)
f8f4acb6 5637 utf8_unload(sb->s_encoding);
c83ad55e
GKB
5638#endif
5639
ac27a0ec 5640#ifdef CONFIG_QUOTA
856dd6c5 5641 for (unsigned int i = 0; i < EXT4_MAXQUOTAS; i++)
0ba33fac 5642 kfree(get_qf_name(sb, sbi, i));
ac27a0ec 5643#endif
ac4acb1f 5644 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
a7a79c29 5645 brelse(sbi->s_sbh);
61ead714
CB
5646 if (sbi->s_journal_bdev_file) {
5647 invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
22650a99 5648 bdev_fput(sbi->s_journal_bdev_file);
1489dffd 5649 }
ac27a0ec 5650out_fail:
26fb5290 5651 invalidate_bdev(sb->s_bdev);
ac27a0ec 5652 sb->s_fs_info = NULL;
d4fab7b2 5653 return err;
ac27a0ec
DK
5654}
5655
cebe85d5 5656static int ext4_fill_super(struct super_block *sb, struct fs_context *fc)
7edfd85b 5657{
cebe85d5 5658 struct ext4_fs_context *ctx = fc->fs_private;
7edfd85b 5659 struct ext4_sb_info *sbi;
7edfd85b 5660 const char *descr;
cebe85d5 5661 int ret;
7edfd85b
LC
5662
5663 sbi = ext4_alloc_sbi(sb);
cebe85d5 5664 if (!sbi)
7c268d4c 5665 return -ENOMEM;
7edfd85b 5666
cebe85d5
LC
5667 fc->s_fs_info = sbi;
5668
5669 /* Cleanup superblock name */
5670 strreplace(sb->s_id, '/', '!');
7edfd85b
LC
5671
5672 sbi->s_sb_block = 1; /* Default super block location */
cebe85d5
LC
5673 if (ctx->spec & EXT4_SPEC_s_sb_block)
5674 sbi->s_sb_block = ctx->s_sb_block;
7edfd85b 5675
960e0ab6 5676 ret = __ext4_fill_super(fc, sb);
7edfd85b
LC
5677 if (ret < 0)
5678 goto free_sbi;
5679
cebe85d5 5680 if (sbi->s_journal) {
7edfd85b
LC
5681 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
5682 descr = " journalled data mode";
5683 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
5684 descr = " ordered data mode";
5685 else
5686 descr = " writeback data mode";
5687 } else
5688 descr = "out journal";
5689
5690 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
6dcc98fb
TT
5691 ext4_msg(sb, KERN_INFO, "mounted filesystem %pU %s with%s. "
5692 "Quota mode: %s.", &sb->s_uuid,
5693 sb_rdonly(sb) ? "ro" : "r/w", descr,
bb0fbc78 5694 ext4_quota_mode(sb));
cebe85d5 5695
eb705421 5696 /* Update the s_overhead_clusters if necessary */
827891a3 5697 ext4_update_overhead(sb, false);
7edfd85b 5698 return 0;
cebe85d5 5699
7edfd85b
LC
5700free_sbi:
5701 ext4_free_sbi(sbi);
cebe85d5 5702 fc->s_fs_info = NULL;
7edfd85b
LC
5703 return ret;
5704}
5705
cebe85d5
LC
5706static int ext4_get_tree(struct fs_context *fc)
5707{
5708 return get_tree_bdev(fc, ext4_fill_super);
5709}
5710
ac27a0ec
DK
5711/*
5712 * Setup any per-fs journal parameters now. We'll do this both on
5713 * initial mount, once the journal has been initialised but before we've
5714 * done any recovery; and again on any subsequent remount.
5715 */
617ba13b 5716static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
ac27a0ec 5717{
617ba13b 5718 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 5719
30773840
TT
5720 journal->j_commit_interval = sbi->s_commit_interval;
5721 journal->j_min_batch_time = sbi->s_min_batch_time;
5722 journal->j_max_batch_time = sbi->s_max_batch_time;
6866d7b3 5723 ext4_fc_init(sb, journal);
ac27a0ec 5724
a931da6a 5725 write_lock(&journal->j_state_lock);
ac27a0ec 5726 if (test_opt(sb, BARRIER))
dab291af 5727 journal->j_flags |= JBD2_BARRIER;
ac27a0ec 5728 else
dab291af 5729 journal->j_flags &= ~JBD2_BARRIER;
5bf5683a
HK
5730 if (test_opt(sb, DATA_ERR_ABORT))
5731 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
5732 else
5733 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
72945058
ZY
5734 /*
5735 * Always enable journal cycle record option, letting the journal
5736 * records log transactions continuously between each mount.
5737 */
5738 journal->j_flags |= JBD2_CYCLE_RECORD;
a931da6a 5739 write_unlock(&journal->j_state_lock);
ac27a0ec
DK
5740}
5741
c6cb7e77
EW
5742static struct inode *ext4_get_journal_inode(struct super_block *sb,
5743 unsigned int journal_inum)
ac27a0ec
DK
5744{
5745 struct inode *journal_inode;
ac27a0ec 5746
c6cb7e77
EW
5747 /*
5748 * Test for the existence of a valid inode on disk. Bad things
5749 * happen if we iget() an unused inode, as the subsequent iput()
5750 * will try to delete it.
5751 */
8a363970 5752 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
1d1fe1ee 5753 if (IS_ERR(journal_inode)) {
b31e1552 5754 ext4_msg(sb, KERN_ERR, "no journal found");
ee5c8071 5755 return ERR_CAST(journal_inode);
ac27a0ec
DK
5756 }
5757 if (!journal_inode->i_nlink) {
5758 make_bad_inode(journal_inode);
5759 iput(journal_inode);
b31e1552 5760 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
ee5c8071 5761 return ERR_PTR(-EFSCORRUPTED);
ac27a0ec 5762 }
105c78e1 5763 if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
b31e1552 5764 ext4_msg(sb, KERN_ERR, "invalid journal inode");
ac27a0ec 5765 iput(journal_inode);
ee5c8071 5766 return ERR_PTR(-EFSCORRUPTED);
ac27a0ec 5767 }
bc74e6a3
ZY
5768
5769 ext4_debug("Journal inode found at %p: %lld bytes\n",
5770 journal_inode, journal_inode->i_size);
c6cb7e77
EW
5771 return journal_inode;
5772}
5773
62913ae9
TT
5774static int ext4_journal_bmap(journal_t *journal, sector_t *block)
5775{
5776 struct ext4_map_blocks map;
5777 int ret;
5778
5779 if (journal->j_inode == NULL)
5780 return 0;
5781
5782 map.m_lblk = *block;
5783 map.m_len = 1;
5784 ret = ext4_map_blocks(NULL, journal->j_inode, &map, 0);
5785 if (ret <= 0) {
5786 ext4_msg(journal->j_inode->i_sb, KERN_CRIT,
5787 "journal bmap failed: block %llu ret %d\n",
5788 *block, ret);
5789 jbd2_journal_abort(journal, ret ? ret : -EIO);
5790 return ret;
5791 }
5792 *block = map.m_pblk;
5793 return 0;
5794}
5795
ee5c8071
ZY
5796static journal_t *ext4_open_inode_journal(struct super_block *sb,
5797 unsigned int journal_inum)
c6cb7e77
EW
5798{
5799 struct inode *journal_inode;
5800 journal_t *journal;
5801
c6cb7e77 5802 journal_inode = ext4_get_journal_inode(sb, journal_inum);
ee5c8071
ZY
5803 if (IS_ERR(journal_inode))
5804 return ERR_CAST(journal_inode);
ac27a0ec 5805
dab291af 5806 journal = jbd2_journal_init_inode(journal_inode);
8e6cf5fb 5807 if (IS_ERR(journal)) {
b31e1552 5808 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
ac27a0ec 5809 iput(journal_inode);
ee5c8071 5810 return ERR_CAST(journal);
ac27a0ec
DK
5811 }
5812 journal->j_private = sb;
62913ae9 5813 journal->j_bmap = ext4_journal_bmap;
617ba13b 5814 ext4_init_journal_params(sb, journal);
ac27a0ec
DK
5815 return journal;
5816}
5817
61ead714 5818static struct file *ext4_get_journal_blkdev(struct super_block *sb,
bc74e6a3
ZY
5819 dev_t j_dev, ext4_fsblk_t *j_start,
5820 ext4_fsblk_t *j_len)
ac27a0ec 5821{
2b2d6d01 5822 struct buffer_head *bh;
bc74e6a3 5823 struct block_device *bdev;
61ead714 5824 struct file *bdev_file;
ac27a0ec 5825 int hblock, blocksize;
617ba13b 5826 ext4_fsblk_t sb_block;
ac27a0ec 5827 unsigned long offset;
2b2d6d01 5828 struct ext4_super_block *es;
ee5c8071 5829 int errno;
0390131b 5830
61ead714 5831 bdev_file = bdev_file_open_by_dev(j_dev,
afde134b
JK
5832 BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
5833 sb, &fs_holder_ops);
61ead714 5834 if (IS_ERR(bdev_file)) {
bc74e6a3
ZY
5835 ext4_msg(sb, KERN_ERR,
5836 "failed to open journal device unknown-block(%u,%u) %ld",
61ead714
CB
5837 MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev_file));
5838 return bdev_file;
bc74e6a3 5839 }
ac27a0ec 5840
61ead714 5841 bdev = file_bdev(bdev_file);
ac27a0ec 5842 blocksize = sb->s_blocksize;
e1defc4f 5843 hblock = bdev_logical_block_size(bdev);
ac27a0ec 5844 if (blocksize < hblock) {
b31e1552
ES
5845 ext4_msg(sb, KERN_ERR,
5846 "blocksize too small for journal device");
ee5c8071 5847 errno = -EINVAL;
ac27a0ec
DK
5848 goto out_bdev;
5849 }
5850
617ba13b
MC
5851 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5852 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
ead083ae 5853 set_blocksize(bdev_file, blocksize);
bc74e6a3
ZY
5854 bh = __bread(bdev, sb_block, blocksize);
5855 if (!bh) {
b31e1552
ES
5856 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5857 "external journal");
ee5c8071 5858 errno = -EINVAL;
ac27a0ec
DK
5859 goto out_bdev;
5860 }
5861
2716b802 5862 es = (struct ext4_super_block *) (bh->b_data + offset);
617ba13b 5863 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
ac27a0ec 5864 !(le32_to_cpu(es->s_feature_incompat) &
617ba13b 5865 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
bc74e6a3 5866 ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
ee5c8071 5867 errno = -EFSCORRUPTED;
bc74e6a3 5868 goto out_bh;
ac27a0ec
DK
5869 }
5870
df4763be
DW
5871 if ((le32_to_cpu(es->s_feature_ro_compat) &
5872 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5873 es->s_checksum != ext4_superblock_csum(sb, es)) {
bc74e6a3 5874 ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
ee5c8071 5875 errno = -EFSCORRUPTED;
bc74e6a3 5876 goto out_bh;
df4763be
DW
5877 }
5878
617ba13b 5879 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
b31e1552 5880 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
ee5c8071 5881 errno = -EFSCORRUPTED;
bc74e6a3 5882 goto out_bh;
ac27a0ec
DK
5883 }
5884
bc74e6a3
ZY
5885 *j_start = sb_block + 1;
5886 *j_len = ext4_blocks_count(es);
5887 brelse(bh);
61ead714 5888 return bdev_file;
ac27a0ec 5889
bc74e6a3
ZY
5890out_bh:
5891 brelse(bh);
5892out_bdev:
22650a99 5893 bdev_fput(bdev_file);
ee5c8071 5894 return ERR_PTR(errno);
bc74e6a3
ZY
5895}
5896
ee5c8071
ZY
5897static journal_t *ext4_open_dev_journal(struct super_block *sb,
5898 dev_t j_dev)
bc74e6a3
ZY
5899{
5900 journal_t *journal;
5901 ext4_fsblk_t j_start;
5902 ext4_fsblk_t j_len;
61ead714 5903 struct file *bdev_file;
ee5c8071 5904 int errno = 0;
bc74e6a3 5905
61ead714
CB
5906 bdev_file = ext4_get_journal_blkdev(sb, j_dev, &j_start, &j_len);
5907 if (IS_ERR(bdev_file))
5908 return ERR_CAST(bdev_file);
bc74e6a3 5909
61ead714 5910 journal = jbd2_journal_init_dev(file_bdev(bdev_file), sb->s_bdev, j_start,
bc74e6a3 5911 j_len, sb->s_blocksize);
8e6cf5fb 5912 if (IS_ERR(journal)) {
b31e1552 5913 ext4_msg(sb, KERN_ERR, "failed to create device journal");
ee5c8071 5914 errno = PTR_ERR(journal);
ac27a0ec
DK
5915 goto out_bdev;
5916 }
ac27a0ec 5917 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
b31e1552
ES
5918 ext4_msg(sb, KERN_ERR, "External journal has more than one "
5919 "user (unsupported) - %d",
ac27a0ec 5920 be32_to_cpu(journal->j_superblock->s_nr_users));
ee5c8071 5921 errno = -EINVAL;
ac27a0ec
DK
5922 goto out_journal;
5923 }
bc74e6a3 5924 journal->j_private = sb;
61ead714 5925 EXT4_SB(sb)->s_journal_bdev_file = bdev_file;
617ba13b 5926 ext4_init_journal_params(sb, journal);
ac27a0ec 5927 return journal;
0b8e58a1 5928
ac27a0ec 5929out_journal:
dab291af 5930 jbd2_journal_destroy(journal);
ac27a0ec 5931out_bdev:
22650a99 5932 bdev_fput(bdev_file);
ee5c8071 5933 return ERR_PTR(errno);
ac27a0ec
DK
5934}
5935
617ba13b
MC
5936static int ext4_load_journal(struct super_block *sb,
5937 struct ext4_super_block *es,
ac27a0ec
DK
5938 unsigned long journal_devnum)
5939{
5940 journal_t *journal;
5941 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5942 dev_t journal_dev;
5943 int err = 0;
5944 int really_read_only;
273108fa 5945 int journal_dev_ro;
ac27a0ec 5946
11215630
JK
5947 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5948 return -EFSCORRUPTED;
0390131b 5949
ac27a0ec
DK
5950 if (journal_devnum &&
5951 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
b31e1552
ES
5952 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5953 "numbers have changed");
ac27a0ec
DK
5954 journal_dev = new_decode_dev(journal_devnum);
5955 } else
5956 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5957
273108fa
LC
5958 if (journal_inum && journal_dev) {
5959 ext4_msg(sb, KERN_ERR,
5960 "filesystem has both journal inode and journal device!");
5961 return -EINVAL;
5962 }
5963
5964 if (journal_inum) {
ee5c8071
ZY
5965 journal = ext4_open_inode_journal(sb, journal_inum);
5966 if (IS_ERR(journal))
5967 return PTR_ERR(journal);
273108fa 5968 } else {
ee5c8071
ZY
5969 journal = ext4_open_dev_journal(sb, journal_dev);
5970 if (IS_ERR(journal))
5971 return PTR_ERR(journal);
273108fa
LC
5972 }
5973
5974 journal_dev_ro = bdev_read_only(journal->j_dev);
5975 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5976
5977 if (journal_dev_ro && !sb_rdonly(sb)) {
5978 ext4_msg(sb, KERN_ERR,
5979 "journal device read-only, try mounting with '-o ro'");
5980 err = -EROFS;
5981 goto err_out;
5982 }
ac27a0ec
DK
5983
5984 /*
5985 * Are we loading a blank journal or performing recovery after a
5986 * crash? For recovery, we need to check in advance whether we
5987 * can get read-write access to the device.
5988 */
e2b911c5 5989 if (ext4_has_feature_journal_needs_recovery(sb)) {
bc98a42c 5990 if (sb_rdonly(sb)) {
b31e1552
ES
5991 ext4_msg(sb, KERN_INFO, "INFO: recovery "
5992 "required on readonly filesystem");
ac27a0ec 5993 if (really_read_only) {
b31e1552 5994 ext4_msg(sb, KERN_ERR, "write access "
d98bf8cd
SR
5995 "unavailable, cannot proceed "
5996 "(try mounting with noload)");
273108fa
LC
5997 err = -EROFS;
5998 goto err_out;
ac27a0ec 5999 }
b31e1552
ES
6000 ext4_msg(sb, KERN_INFO, "write access will "
6001 "be enabled during recovery");
ac27a0ec
DK
6002 }
6003 }
6004
90576c0b 6005 if (!(journal->j_flags & JBD2_BARRIER))
b31e1552 6006 ext4_msg(sb, KERN_INFO, "barriers disabled");
4776004f 6007
e2b911c5 6008 if (!ext4_has_feature_journal_needs_recovery(sb))
dab291af 6009 err = jbd2_journal_wipe(journal, !really_read_only);
1c13d5c0
TT
6010 if (!err) {
6011 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
2ef6c32a
TT
6012 __le16 orig_state;
6013 bool changed = false;
eee00237 6014
1c13d5c0
TT
6015 if (save)
6016 memcpy(save, ((char *) es) +
6017 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
dab291af 6018 err = jbd2_journal_load(journal);
2ef6c32a
TT
6019 if (save && memcmp(((char *) es) + EXT4_S_ERR_START,
6020 save, EXT4_S_ERR_LEN)) {
1c13d5c0
TT
6021 memcpy(((char *) es) + EXT4_S_ERR_START,
6022 save, EXT4_S_ERR_LEN);
2ef6c32a
TT
6023 changed = true;
6024 }
1c13d5c0 6025 kfree(save);
2ef6c32a 6026 orig_state = es->s_state;
eee00237
YB
6027 es->s_state |= cpu_to_le16(EXT4_SB(sb)->s_mount_state &
6028 EXT4_ERROR_FS);
2ef6c32a
TT
6029 if (orig_state != es->s_state)
6030 changed = true;
eee00237 6031 /* Write out restored error information to the superblock */
2ef6c32a 6032 if (changed && !really_read_only) {
eee00237
YB
6033 int err2;
6034 err2 = ext4_commit_super(sb);
6035 err = err ? : err2;
6036 }
1c13d5c0 6037 }
ac27a0ec
DK
6038
6039 if (err) {
b31e1552 6040 ext4_msg(sb, KERN_ERR, "error loading journal");
273108fa 6041 goto err_out;
ac27a0ec
DK
6042 }
6043
617ba13b 6044 EXT4_SB(sb)->s_journal = journal;
11215630
JK
6045 err = ext4_clear_journal_err(sb, es);
6046 if (err) {
6047 EXT4_SB(sb)->s_journal = NULL;
6048 jbd2_journal_destroy(journal);
6049 return err;
6050 }
ac27a0ec 6051
c41303ce 6052 if (!really_read_only && journal_devnum &&
ac27a0ec
DK
6053 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
6054 es->s_journal_dev = cpu_to_le32(journal_devnum);
3039d8b8
BL
6055 ext4_commit_super(sb);
6056 }
6057 if (!really_read_only && journal_inum &&
6058 journal_inum != le32_to_cpu(es->s_journal_inum)) {
6059 es->s_journal_inum = cpu_to_le32(journal_inum);
4392fbc4 6060 ext4_commit_super(sb);
ac27a0ec
DK
6061 }
6062
6063 return 0;
273108fa
LC
6064
6065err_out:
6066 jbd2_journal_destroy(journal);
6067 return err;
ac27a0ec
DK
6068}
6069
2d01ddc8
JK
6070/* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
6071static void ext4_update_super(struct super_block *sb)
ac27a0ec 6072{
c92dc856 6073 struct ext4_sb_info *sbi = EXT4_SB(sb);
e92ad03f
JK
6074 struct ext4_super_block *es = sbi->s_es;
6075 struct buffer_head *sbh = sbi->s_sbh;
a17712c8 6076
05c2c00f 6077 lock_buffer(sbh);
71290b36
TT
6078 /*
6079 * If the file system is mounted read-only, don't update the
6080 * superblock write time. This avoids updating the superblock
6081 * write time when we are mounting the root file system
6082 * read/only but we need to replay the journal; at that point,
6083 * for people who are east of GMT and who make their clock
6084 * tick in localtime for Windows bug-for-bug compatibility,
6085 * the clock is set in the future, and this will cause e2fsck
6086 * to complain and force a full file system check.
6087 */
d5d020b3 6088 if (!sb_rdonly(sb))
6a0678a7 6089 ext4_update_tstamp(es, s_wtime);
8446fe92 6090 es->s_kbytes_written =
0bc9bc1d 6091 cpu_to_le64(sbi->s_kbytes_written +
8446fe92 6092 ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
0bc9bc1d 6093 sbi->s_sectors_written_start) >> 1));
e92ad03f 6094 if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
d5e03cbb 6095 ext4_free_blocks_count_set(es,
e92ad03f
JK
6096 EXT4_C2B(sbi, percpu_counter_sum_positive(
6097 &sbi->s_freeclusters_counter)));
6098 if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
d5e03cbb
TT
6099 es->s_free_inodes_count =
6100 cpu_to_le32(percpu_counter_sum_positive(
e92ad03f 6101 &sbi->s_freeinodes_counter));
c92dc856
JK
6102 /* Copy error information to the on-disk superblock */
6103 spin_lock(&sbi->s_error_lock);
6104 if (sbi->s_add_error_count > 0) {
6105 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
6106 if (!es->s_first_error_time && !es->s_first_error_time_hi) {
6107 __ext4_update_tstamp(&es->s_first_error_time,
6108 &es->s_first_error_time_hi,
6109 sbi->s_first_error_time);
744a5638
JS
6110 strtomem_pad(es->s_first_error_func,
6111 sbi->s_first_error_func, 0);
c92dc856
JK
6112 es->s_first_error_line =
6113 cpu_to_le32(sbi->s_first_error_line);
6114 es->s_first_error_ino =
6115 cpu_to_le32(sbi->s_first_error_ino);
6116 es->s_first_error_block =
6117 cpu_to_le64(sbi->s_first_error_block);
6118 es->s_first_error_errcode =
6119 ext4_errno_to_code(sbi->s_first_error_code);
6120 }
6121 __ext4_update_tstamp(&es->s_last_error_time,
6122 &es->s_last_error_time_hi,
6123 sbi->s_last_error_time);
744a5638 6124 strtomem_pad(es->s_last_error_func, sbi->s_last_error_func, 0);
c92dc856
JK
6125 es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
6126 es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
6127 es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
6128 es->s_last_error_errcode =
6129 ext4_errno_to_code(sbi->s_last_error_code);
6130 /*
6131 * Start the daily error reporting function if it hasn't been
6132 * started already
6133 */
6134 if (!es->s_error_count)
6135 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);
6136 le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
6137 sbi->s_add_error_count = 0;
6138 }
6139 spin_unlock(&sbi->s_error_lock);
6140
06db49e6 6141 ext4_superblock_csum_set(sb);
2d01ddc8
JK
6142 unlock_buffer(sbh);
6143}
6144
6145static int ext4_commit_super(struct super_block *sb)
6146{
6147 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
2d01ddc8 6148
f88f1466
FC
6149 if (!sbh)
6150 return -EINVAL;
2d01ddc8
JK
6151
6152 ext4_update_super(sb);
6153
15baa7dc
ZY
6154 lock_buffer(sbh);
6155 /* Buffer got discarded which means block device got invalidated */
6156 if (!buffer_mapped(sbh)) {
6157 unlock_buffer(sbh);
6158 return -EIO;
6159 }
6160
e8680786 6161 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
4743f839
PKS
6162 /*
6163 * Oh, dear. A previous attempt to write the
6164 * superblock failed. This could happen because the
6165 * USB device was yanked out. Or it could happen to
6166 * be a transient write error and maybe the block will
6167 * be remapped. Nothing we can do but to retry the
6168 * write and hope for the best.
6169 */
6170 ext4_msg(sb, KERN_ERR, "previous I/O error to "
6171 "superblock detected");
6172 clear_buffer_write_io_error(sbh);
6173 set_buffer_uptodate(sbh);
6174 }
15baa7dc
ZY
6175 get_bh(sbh);
6176 /* Clear potential dirty bit if it was journalled update */
6177 clear_buffer_dirty(sbh);
6178 sbh->b_end_io = end_buffer_write_sync;
1420c4a5
BVA
6179 submit_bh(REQ_OP_WRITE | REQ_SYNC |
6180 (test_opt(sb, BARRIER) ? REQ_FUA : 0), sbh);
15baa7dc 6181 wait_on_buffer(sbh);
4392fbc4
JK
6182 if (buffer_write_io_error(sbh)) {
6183 ext4_msg(sb, KERN_ERR, "I/O error while writing "
6184 "superblock");
6185 clear_buffer_write_io_error(sbh);
6186 set_buffer_uptodate(sbh);
15baa7dc 6187 return -EIO;
914258bf 6188 }
15baa7dc 6189 return 0;
ac27a0ec
DK
6190}
6191
ac27a0ec
DK
6192/*
6193 * Have we just finished recovery? If so, and if we are mounting (or
6194 * remounting) the filesystem readonly, then we will end up with a
6195 * consistent fs on disk. Record that fact.
6196 */
11215630
JK
6197static int ext4_mark_recovery_complete(struct super_block *sb,
6198 struct ext4_super_block *es)
ac27a0ec 6199{
11215630 6200 int err;
617ba13b 6201 journal_t *journal = EXT4_SB(sb)->s_journal;
ac27a0ec 6202
e2b911c5 6203 if (!ext4_has_feature_journal(sb)) {
11215630
JK
6204 if (journal != NULL) {
6205 ext4_error(sb, "Journal got removed while the fs was "
6206 "mounted!");
6207 return -EFSCORRUPTED;
6208 }
6209 return 0;
0390131b 6210 }
dab291af 6211 jbd2_journal_lock_updates(journal);
01d5d965 6212 err = jbd2_journal_flush(journal, 0);
11215630 6213 if (err < 0)
7ffe1ea8
HK
6214 goto out;
6215
02f310fc
JK
6216 if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
6217 ext4_has_feature_orphan_present(sb))) {
6218 if (!ext4_orphan_file_empty(sb)) {
6219 ext4_error(sb, "Orphan file not empty on read-only fs.");
6220 err = -EFSCORRUPTED;
6221 goto out;
6222 }
e2b911c5 6223 ext4_clear_feature_journal_needs_recovery(sb);
02f310fc 6224 ext4_clear_feature_orphan_present(sb);
4392fbc4 6225 ext4_commit_super(sb);
ac27a0ec 6226 }
7ffe1ea8 6227out:
dab291af 6228 jbd2_journal_unlock_updates(journal);
11215630 6229 return err;
ac27a0ec
DK
6230}
6231
6232/*
6233 * If we are mounting (or read-write remounting) a filesystem whose journal
6234 * has recorded an error from a previous lifetime, move that error to the
6235 * main filesystem now.
6236 */
11215630 6237static int ext4_clear_journal_err(struct super_block *sb,
2b2d6d01 6238 struct ext4_super_block *es)
ac27a0ec
DK
6239{
6240 journal_t *journal;
6241 int j_errno;
6242 const char *errstr;
6243
11215630
JK
6244 if (!ext4_has_feature_journal(sb)) {
6245 ext4_error(sb, "Journal got removed while the fs was mounted!");
6246 return -EFSCORRUPTED;
6247 }
0390131b 6248
617ba13b 6249 journal = EXT4_SB(sb)->s_journal;
ac27a0ec
DK
6250
6251 /*
6252 * Now check for any error status which may have been recorded in the
617ba13b 6253 * journal by a prior ext4_error() or ext4_abort()
ac27a0ec
DK
6254 */
6255
dab291af 6256 j_errno = jbd2_journal_errno(journal);
ac27a0ec
DK
6257 if (j_errno) {
6258 char nbuf[16];
6259
617ba13b 6260 errstr = ext4_decode_error(sb, j_errno, nbuf);
12062ddd 6261 ext4_warning(sb, "Filesystem error recorded "
ac27a0ec 6262 "from previous mount: %s", errstr);
ac27a0ec 6263
617ba13b
MC
6264 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
6265 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
f57886ca
YB
6266 j_errno = ext4_commit_super(sb);
6267 if (j_errno)
6268 return j_errno;
6269 ext4_warning(sb, "Marked fs in need of filesystem check.");
ac27a0ec 6270
dab291af 6271 jbd2_journal_clear_err(journal);
d796c52e 6272 jbd2_journal_update_sb_errno(journal);
ac27a0ec 6273 }
11215630 6274 return 0;
ac27a0ec
DK
6275}
6276
6277/*
6278 * Force the running and committing transactions to commit,
6279 * and wait on the commit.
6280 */
617ba13b 6281int ext4_force_commit(struct super_block *sb)
ac27a0ec 6282{
889860e4 6283 return ext4_journal_force_commit(EXT4_SB(sb)->s_journal);
ac27a0ec
DK
6284}
6285
617ba13b 6286static int ext4_sync_fs(struct super_block *sb, int wait)
ac27a0ec 6287{
14ce0cb4 6288 int ret = 0;
9eddacf9 6289 tid_t target;
06a407f1 6290 bool needs_barrier = false;
8d5d02e6 6291 struct ext4_sb_info *sbi = EXT4_SB(sb);
ac27a0ec 6292
eb8ab444 6293 if (unlikely(ext4_forced_shutdown(sb)))
0db1ff22
TT
6294 return 0;
6295
9bffad1e 6296 trace_ext4_sync_fs(sb, wait);
2e8fa54e 6297 flush_workqueue(sbi->rsv_conversion_wq);
a1177825
JK
6298 /*
6299 * Writeback quota in non-journalled quota case - journalled quota has
6300 * no dirty dquots
6301 */
6302 dquot_writeback_dquots(sb, -1);
06a407f1
DM
6303 /*
6304 * Data writeback is possible w/o journal transaction, so barrier must
6305 * being sent at the end of the function. But we can skip it if
6306 * transaction_commit will do it for us.
6307 */
bda32530
TT
6308 if (sbi->s_journal) {
6309 target = jbd2_get_latest_transaction(sbi->s_journal);
6310 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
6311 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
6312 needs_barrier = true;
6313
6314 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
6315 if (wait)
6316 ret = jbd2_log_wait_commit(sbi->s_journal,
6317 target);
6318 }
6319 } else if (wait && test_opt(sb, BARRIER))
06a407f1 6320 needs_barrier = true;
06a407f1
DM
6321 if (needs_barrier) {
6322 int err;
c6bf3f0e 6323 err = blkdev_issue_flush(sb->s_bdev);
06a407f1
DM
6324 if (!ret)
6325 ret = err;
0390131b 6326 }
06a407f1
DM
6327
6328 return ret;
6329}
6330
ac27a0ec
DK
6331/*
6332 * LVM calls this function before a (read-only) snapshot is created. This
6333 * gives us a chance to flush the journal completely and mark the fs clean.
be4f27d3
YY
6334 *
6335 * Note that only this function cannot bring a filesystem to be in a clean
8e8ad8a5
JK
6336 * state independently. It relies on upper layer to stop all data & metadata
6337 * modifications.
ac27a0ec 6338 */
c4be0c1d 6339static int ext4_freeze(struct super_block *sb)
ac27a0ec 6340{
c4be0c1d 6341 int error = 0;
98175720 6342 journal_t *journal = EXT4_SB(sb)->s_journal;
7ffe1ea8 6343
bb044576
TT
6344 if (journal) {
6345 /* Now we set up the journal barrier. */
6346 jbd2_journal_lock_updates(journal);
ac27a0ec 6347
bb044576
TT
6348 /*
6349 * Don't clear the needs_recovery flag if we failed to
6350 * flush the journal.
6351 */
01d5d965 6352 error = jbd2_journal_flush(journal, 0);
bb044576
TT
6353 if (error < 0)
6354 goto out;
c642dc9e
ES
6355
6356 /* Journal blocked and flushed, clear needs_recovery flag. */
e2b911c5 6357 ext4_clear_feature_journal_needs_recovery(sb);
02f310fc
JK
6358 if (ext4_orphan_file_empty(sb))
6359 ext4_clear_feature_orphan_present(sb);
bb044576 6360 }
9ca92389 6361
4392fbc4 6362 error = ext4_commit_super(sb);
6b0310fb 6363out:
bb044576
TT
6364 if (journal)
6365 /* we rely on upper layer to stop further updates */
6366 jbd2_journal_unlock_updates(journal);
6b0310fb 6367 return error;
ac27a0ec
DK
6368}
6369
6370/*
6371 * Called by LVM after the snapshot is done. We need to reset the RECOVER
6372 * flag here, even though the filesystem is not technically dirty yet.
6373 */
c4be0c1d 6374static int ext4_unfreeze(struct super_block *sb)
ac27a0ec 6375{
eb8ab444 6376 if (ext4_forced_shutdown(sb))
9ca92389
TT
6377 return 0;
6378
c642dc9e
ES
6379 if (EXT4_SB(sb)->s_journal) {
6380 /* Reset the needs_recovery flag before the fs is unlocked. */
e2b911c5 6381 ext4_set_feature_journal_needs_recovery(sb);
02f310fc
JK
6382 if (ext4_has_feature_orphan_file(sb))
6383 ext4_set_feature_orphan_present(sb);
c642dc9e
ES
6384 }
6385
4392fbc4 6386 ext4_commit_super(sb);
c4be0c1d 6387 return 0;
ac27a0ec
DK
6388}
6389
673c6100
TT
6390/*
6391 * Structure to save mount options for ext4_remount's benefit
6392 */
6393struct ext4_mount_options {
6394 unsigned long s_mount_opt;
a2595b8a 6395 unsigned long s_mount_opt2;
08cefc7a
EB
6396 kuid_t s_resuid;
6397 kgid_t s_resgid;
673c6100
TT
6398 unsigned long s_commit_interval;
6399 u32 s_min_batch_time, s_max_batch_time;
6400#ifdef CONFIG_QUOTA
6401 int s_jquota_fmt;
a2d4a646 6402 char *s_qf_names[EXT4_MAXQUOTAS];
673c6100
TT
6403#endif
6404};
6405
960e0ab6 6406static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
ac27a0ec 6407{
7edfd85b 6408 struct ext4_fs_context *ctx = fc->fs_private;
2b2d6d01 6409 struct ext4_super_block *es;
617ba13b 6410 struct ext4_sb_info *sbi = EXT4_SB(sb);
960e0ab6 6411 unsigned long old_sb_flags;
617ba13b 6412 struct ext4_mount_options old_opts;
8a266467 6413 ext4_group_t g;
c5e06d10 6414 int err = 0;
745f17a4 6415 int alloc_ctx;
ac27a0ec 6416#ifdef CONFIG_QUOTA
3bbef91b 6417 int enable_quota = 0;
03dafb5f 6418 int i, j;
33458eab 6419 char *to_free[EXT4_MAXQUOTAS];
ac27a0ec
DK
6420#endif
6421
21ac738e 6422
ac27a0ec
DK
6423 /* Store the original options */
6424 old_sb_flags = sb->s_flags;
6425 old_opts.s_mount_opt = sbi->s_mount_opt;
a2595b8a 6426 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
ac27a0ec
DK
6427 old_opts.s_resuid = sbi->s_resuid;
6428 old_opts.s_resgid = sbi->s_resgid;
6429 old_opts.s_commit_interval = sbi->s_commit_interval;
30773840
TT
6430 old_opts.s_min_batch_time = sbi->s_min_batch_time;
6431 old_opts.s_max_batch_time = sbi->s_max_batch_time;
ac27a0ec
DK
6432#ifdef CONFIG_QUOTA
6433 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
a2d4a646 6434 for (i = 0; i < EXT4_MAXQUOTAS; i++)
03dafb5f 6435 if (sbi->s_qf_names[i]) {
33458eab
TT
6436 char *qf_name = get_qf_name(sb, sbi, i);
6437
6438 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
03dafb5f
CG
6439 if (!old_opts.s_qf_names[i]) {
6440 for (j = 0; j < i; j++)
6441 kfree(old_opts.s_qf_names[j]);
6442 return -ENOMEM;
6443 }
6444 } else
6445 old_opts.s_qf_names[i] = NULL;
ac27a0ec 6446#endif
e4e58e5d
OM
6447 if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) {
6448 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
6449 ctx->journal_ioprio =
6450 sbi->s_journal->j_task->io_context->ioprio;
6451 else
6452 ctx->journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
6453
6454 }
ac27a0ec 6455
745f17a4
BL
6456 /*
6457 * Changing the DIOREAD_NOLOCK or DELALLOC mount options may cause
6458 * two calls to ext4_should_dioread_nolock() to return inconsistent
6459 * values, triggering WARN_ON in ext4_add_complete_io(). we grab
6460 * here s_writepages_rwsem to avoid race between writepages ops and
6461 * remount.
6462 */
6463 alloc_ctx = ext4_writepages_down_write(sb);
7edfd85b 6464 ext4_apply_options(fc, sb);
745f17a4 6465 ext4_writepages_up_write(sb, alloc_ctx);
ac27a0ec 6466
6b992ff2 6467 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
c6d3d56d
DW
6468 test_opt(sb, JOURNAL_CHECKSUM)) {
6469 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
2d5b86e0
ES
6470 "during remount not supported; ignoring");
6471 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
6b992ff2
DW
6472 }
6473
6ae6514b
PS
6474 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
6475 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
6476 ext4_msg(sb, KERN_ERR, "can't mount with "
6477 "both data=journal and delalloc");
6478 err = -EINVAL;
6479 goto restore_opts;
6480 }
6481 if (test_opt(sb, DIOREAD_NOLOCK)) {
6482 ext4_msg(sb, KERN_ERR, "can't mount with "
6483 "both data=journal and dioread_nolock");
6484 err = -EINVAL;
6485 goto restore_opts;
6486 }
ab04df78
JK
6487 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
6488 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
6489 ext4_msg(sb, KERN_ERR, "can't mount with "
6490 "journal_async_commit in data=ordered mode");
6491 err = -EINVAL;
6492 goto restore_opts;
6493 }
923ae0ff
RZ
6494 }
6495
cdb7ee4c
TE
6496 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
6497 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
6498 err = -EINVAL;
6499 goto restore_opts;
6500 }
6501
22b8d707 6502 if (test_opt2(sb, ABORT))
124e7c61 6503 ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
ac27a0ec 6504
1751e8a6
LT
6505 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
6506 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
ac27a0ec
DK
6507
6508 es = sbi->s_es;
6509
b3881f74 6510 if (sbi->s_journal) {
0390131b 6511 ext4_init_journal_params(sb, sbi->s_journal);
7edfd85b 6512 set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
b3881f74 6513 }
ac27a0ec 6514
c92dc856 6515 /* Flush outstanding errors before changing fs state */
bb15cea2 6516 flush_work(&sbi->s_sb_upd_work);
c92dc856 6517
960e0ab6 6518 if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) {
95257987 6519 if (ext4_forced_shutdown(sb)) {
ac27a0ec
DK
6520 err = -EROFS;
6521 goto restore_opts;
6522 }
6523
960e0ab6 6524 if (fc->sb_flags & SB_RDONLY) {
38c03b34
TT
6525 err = sync_filesystem(sb);
6526 if (err < 0)
6527 goto restore_opts;
0f0dd62f
CH
6528 err = dquot_suspend(sb, -1);
6529 if (err < 0)
c79d967d 6530 goto restore_opts;
c79d967d 6531
ac27a0ec
DK
6532 /*
6533 * First of all, the unconditional stuff we have to do
6534 * to disable replay of the journal when we next remount
6535 */
1751e8a6 6536 sb->s_flags |= SB_RDONLY;
ac27a0ec
DK
6537
6538 /*
6539 * OK, test if we are remounting a valid rw partition
6540 * readonly, and if so set the rdonly flag and then
6541 * mark the partition as valid again.
6542 */
617ba13b
MC
6543 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
6544 (sbi->s_mount_state & EXT4_VALID_FS))
ac27a0ec
DK
6545 es->s_state = cpu_to_le16(sbi->s_mount_state);
6546
11215630
JK
6547 if (sbi->s_journal) {
6548 /*
6549 * We let remount-ro finish even if marking fs
6550 * as clean failed...
6551 */
0390131b 6552 ext4_mark_recovery_complete(sb, es);
11215630 6553 }
ac27a0ec 6554 } else {
a13fb1a4 6555 /* Make sure we can mount this feature set readwrite */
e2b911c5 6556 if (ext4_has_feature_readonly(sb) ||
2cb5cc8b 6557 !ext4_feature_set_ok(sb, 0)) {
ac27a0ec
DK
6558 err = -EROFS;
6559 goto restore_opts;
6560 }
8a266467
TT
6561 /*
6562 * Make sure the group descriptor checksums
0b8e58a1 6563 * are sane. If they aren't, refuse to remount r/w.
8a266467
TT
6564 */
6565 for (g = 0; g < sbi->s_groups_count; g++) {
6566 struct ext4_group_desc *gdp =
6567 ext4_get_group_desc(sb, g, NULL);
6568
feb0ab32 6569 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
b31e1552
ES
6570 ext4_msg(sb, KERN_ERR,
6571 "ext4_remount: Checksum for group %u failed (%u!=%u)",
e2b911c5 6572 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
8a266467 6573 le16_to_cpu(gdp->bg_checksum));
6a797d27 6574 err = -EFSBADCRC;
8a266467
TT
6575 goto restore_opts;
6576 }
6577 }
6578
ead6596b
ES
6579 /*
6580 * If we have an unprocessed orphan list hanging
6581 * around from a previously readonly bdev mount,
6582 * require a full umount/remount for now.
6583 */
02f310fc 6584 if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
b31e1552 6585 ext4_msg(sb, KERN_WARNING, "Couldn't "
ead6596b
ES
6586 "remount RDWR because of unprocessed "
6587 "orphan inode list. Please "
b31e1552 6588 "umount/remount instead");
ead6596b
ES
6589 err = -EINVAL;
6590 goto restore_opts;
6591 }
6592
ac27a0ec
DK
6593 /*
6594 * Mounting a RDONLY partition read-write, so reread
6595 * and store the current valid flag. (It may have
6596 * been changed by e2fsck since we originally mounted
6597 * the partition.)
6598 */
11215630
JK
6599 if (sbi->s_journal) {
6600 err = ext4_clear_journal_err(sb, es);
6601 if (err)
6602 goto restore_opts;
6603 }
c878bea3
TT
6604 sbi->s_mount_state = (le16_to_cpu(es->s_state) &
6605 ~EXT4_FC_REPLAY);
c89128a0
JK
6606
6607 err = ext4_setup_super(sb, es, 0);
6608 if (err)
6609 goto restore_opts;
6610
1b292439 6611 sb->s_flags &= ~SB_RDONLY;
3b50d501
TT
6612 if (ext4_has_feature_mmp(sb)) {
6613 err = ext4_multi_mount_protect(sb,
6614 le64_to_cpu(es->s_mmp_block));
6615 if (err)
c5e06d10 6616 goto restore_opts;
3b50d501 6617 }
3bbef91b 6618#ifdef CONFIG_QUOTA
c79d967d 6619 enable_quota = 1;
3bbef91b 6620#endif
ac27a0ec
DK
6621 }
6622 }
bfff6873 6623
0f5bde1d
JK
6624 /*
6625 * Handle creation of system zone data early because it can fail.
6626 * Releasing of existing data is done when we are sure remount will
6627 * succeed.
6628 */
dd0db94f 6629 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
0f5bde1d
JK
6630 err = ext4_setup_system_zone(sb);
6631 if (err)
6632 goto restore_opts;
6633 }
d176b1f6 6634
c89128a0 6635 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
4392fbc4 6636 err = ext4_commit_super(sb);
c89128a0
JK
6637 if (err)
6638 goto restore_opts;
6639 }
0390131b 6640
ac27a0ec 6641#ifdef CONFIG_QUOTA
7c319d32
AK
6642 if (enable_quota) {
6643 if (sb_any_quota_suspended(sb))
6644 dquot_resume(sb, -1);
e2b911c5 6645 else if (ext4_has_feature_quota(sb)) {
7c319d32 6646 err = ext4_enable_quotas(sb);
07724f98 6647 if (err)
7c319d32 6648 goto restore_opts;
7c319d32
AK
6649 }
6650 }
4c0b4818
TT
6651 /* Release old quota file names */
6652 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6653 kfree(old_opts.s_qf_names[i]);
ac27a0ec 6654#endif
dd0db94f 6655 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
0f5bde1d 6656 ext4_release_system_zone(sb);
d4c402d9 6657
eb1f822c
TT
6658 /*
6659 * Reinitialize lazy itable initialization thread based on
6660 * current settings
6661 */
6662 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
6663 ext4_unregister_li_request(sb);
6664 else {
6665 ext4_group_t first_not_zeroed;
6666 first_not_zeroed = ext4_has_uninit_itable(sb);
6667 ext4_register_li_request(sb, first_not_zeroed);
6668 }
6669
61bb4a1c
TT
6670 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6671 ext4_stop_mmpd(sbi);
6672
ac27a0ec 6673 return 0;
0b8e58a1 6674
ac27a0ec 6675restore_opts:
4c0b4818
TT
6676 /*
6677 * If there was a failing r/w to ro transition, we may need to
6678 * re-enable quota
6679 */
d5d020b3 6680 if (sb_rdonly(sb) && !(old_sb_flags & SB_RDONLY) &&
4c0b4818
TT
6681 sb_any_quota_suspended(sb))
6682 dquot_resume(sb, -1);
745f17a4
BL
6683
6684 alloc_ctx = ext4_writepages_down_write(sb);
ac27a0ec
DK
6685 sb->s_flags = old_sb_flags;
6686 sbi->s_mount_opt = old_opts.s_mount_opt;
a2595b8a 6687 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
ac27a0ec
DK
6688 sbi->s_resuid = old_opts.s_resuid;
6689 sbi->s_resgid = old_opts.s_resgid;
6690 sbi->s_commit_interval = old_opts.s_commit_interval;
30773840
TT
6691 sbi->s_min_batch_time = old_opts.s_min_batch_time;
6692 sbi->s_max_batch_time = old_opts.s_max_batch_time;
745f17a4
BL
6693 ext4_writepages_up_write(sb, alloc_ctx);
6694
dd0db94f 6695 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
0f5bde1d 6696 ext4_release_system_zone(sb);
ac27a0ec
DK
6697#ifdef CONFIG_QUOTA
6698 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
a2d4a646 6699 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
33458eab
TT
6700 to_free[i] = get_qf_name(sb, sbi, i);
6701 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
ac27a0ec 6702 }
33458eab
TT
6703 synchronize_rcu();
6704 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6705 kfree(to_free[i]);
ac27a0ec 6706#endif
61bb4a1c
TT
6707 if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
6708 ext4_stop_mmpd(sbi);
ac27a0ec
DK
6709 return err;
6710}
6711
cebe85d5 6712static int ext4_reconfigure(struct fs_context *fc)
7edfd85b 6713{
cebe85d5 6714 struct super_block *sb = fc->root->d_sb;
7edfd85b
LC
6715 int ret;
6716
cebe85d5 6717 fc->s_fs_info = EXT4_SB(sb);
7edfd85b 6718
cebe85d5 6719 ret = ext4_check_opt_consistency(fc, sb);
7edfd85b 6720 if (ret < 0)
cebe85d5 6721 return ret;
7edfd85b 6722
960e0ab6 6723 ret = __ext4_remount(fc, sb);
7edfd85b 6724 if (ret < 0)
cebe85d5 6725 return ret;
7edfd85b 6726
6dcc98fb
TT
6727 ext4_msg(sb, KERN_INFO, "re-mounted %pU %s. Quota mode: %s.",
6728 &sb->s_uuid, sb_rdonly(sb) ? "ro" : "r/w",
6729 ext4_quota_mode(sb));
7edfd85b 6730
7edfd85b 6731 return 0;
7edfd85b
LC
6732}
6733
689c958c
LX
6734#ifdef CONFIG_QUOTA
6735static int ext4_statfs_project(struct super_block *sb,
6736 kprojid_t projid, struct kstatfs *buf)
6737{
6738 struct kqid qid;
6739 struct dquot *dquot;
6740 u64 limit;
6741 u64 curblock;
6742
6743 qid = make_kqid_projid(projid);
6744 dquot = dqget(sb, qid);
6745 if (IS_ERR(dquot))
6746 return PTR_ERR(dquot);
7b9ca4c6 6747 spin_lock(&dquot->dq_dqb_lock);
689c958c 6748
a08fe66e
CX
6749 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6750 dquot->dq_dqb.dqb_bhardlimit);
57c32ea4
CX
6751 limit >>= sb->s_blocksize_bits;
6752
689c958c 6753 if (limit && buf->f_blocks > limit) {
f06925c7
KK
6754 curblock = (dquot->dq_dqb.dqb_curspace +
6755 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
689c958c
LX
6756 buf->f_blocks = limit;
6757 buf->f_bfree = buf->f_bavail =
6758 (buf->f_blocks > curblock) ?
6759 (buf->f_blocks - curblock) : 0;
6760 }
6761
a08fe66e
CX
6762 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6763 dquot->dq_dqb.dqb_ihardlimit);
689c958c
LX
6764 if (limit && buf->f_files > limit) {
6765 buf->f_files = limit;
6766 buf->f_ffree =
6767 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6768 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6769 }
6770
7b9ca4c6 6771 spin_unlock(&dquot->dq_dqb_lock);
689c958c
LX
6772 dqput(dquot);
6773 return 0;
6774}
6775#endif
6776
2b2d6d01 6777static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
ac27a0ec
DK
6778{
6779 struct super_block *sb = dentry->d_sb;
617ba13b
MC
6780 struct ext4_sb_info *sbi = EXT4_SB(sb);
6781 struct ext4_super_block *es = sbi->s_es;
27dd4385 6782 ext4_fsblk_t overhead = 0, resv_blocks;
d02a9391 6783 s64 bfree;
27dd4385 6784 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
ac27a0ec 6785
952fc18e
TT
6786 if (!test_opt(sb, MINIX_DF))
6787 overhead = sbi->s_overhead;
ac27a0ec 6788
617ba13b 6789 buf->f_type = EXT4_SUPER_MAGIC;
ac27a0ec 6790 buf->f_bsize = sb->s_blocksize;
b72f78cb 6791 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
57042651
TT
6792 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
6793 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
d02a9391 6794 /* prevent underflow in case that few free space is available */
57042651 6795 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
27dd4385
LC
6796 buf->f_bavail = buf->f_bfree -
6797 (ext4_r_blocks_count(es) + resv_blocks);
6798 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
ac27a0ec
DK
6799 buf->f_bavail = 0;
6800 buf->f_files = le32_to_cpu(es->s_inodes_count);
52d9f3b4 6801 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
617ba13b 6802 buf->f_namelen = EXT4_NAME_LEN;
9591c3a3 6803 buf->f_fsid = uuid_to_fsid(es->s_uuid);
0b8e58a1 6804
689c958c
LX
6805#ifdef CONFIG_QUOTA
6806 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6807 sb_has_quota_limits_enabled(sb, PRJQUOTA))
6808 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6809#endif
ac27a0ec
DK
6810 return 0;
6811}
6812
ac27a0ec
DK
6813
6814#ifdef CONFIG_QUOTA
6815
bc8230ee
JK
6816/*
6817 * Helper functions so that transaction is started before we acquire dqio_sem
6818 * to keep correct lock ordering of transaction > dqio_sem
6819 */
ac27a0ec
DK
6820static inline struct inode *dquot_to_inode(struct dquot *dquot)
6821{
4c376dca 6822 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
ac27a0ec
DK
6823}
6824
617ba13b 6825static int ext4_write_dquot(struct dquot *dquot)
ac27a0ec
DK
6826{
6827 int ret, err;
6828 handle_t *handle;
6829 struct inode *inode;
6830
6831 inode = dquot_to_inode(dquot);
9924a92a 6832 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
0b8e58a1 6833 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
6834 if (IS_ERR(handle))
6835 return PTR_ERR(handle);
6836 ret = dquot_commit(dquot);
d8b945fa
YB
6837 if (ret < 0)
6838 ext4_error_err(dquot->dq_sb, -ret,
6839 "Failed to commit dquot type %d",
6840 dquot->dq_id.type);
617ba13b 6841 err = ext4_journal_stop(handle);
ac27a0ec
DK
6842 if (!ret)
6843 ret = err;
6844 return ret;
6845}
6846
617ba13b 6847static int ext4_acquire_dquot(struct dquot *dquot)
ac27a0ec
DK
6848{
6849 int ret, err;
6850 handle_t *handle;
6851
9924a92a 6852 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
0b8e58a1 6853 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
ac27a0ec
DK
6854 if (IS_ERR(handle))
6855 return PTR_ERR(handle);
6856 ret = dquot_acquire(dquot);
d8b945fa
YB
6857 if (ret < 0)
6858 ext4_error_err(dquot->dq_sb, -ret,
6859 "Failed to acquire dquot type %d",
6860 dquot->dq_id.type);
617ba13b 6861 err = ext4_journal_stop(handle);
ac27a0ec
DK
6862 if (!ret)
6863 ret = err;
6864 return ret;
6865}
6866
617ba13b 6867static int ext4_release_dquot(struct dquot *dquot)
ac27a0ec
DK
6868{
6869 int ret, err;
6870 handle_t *handle;
6871
9924a92a 6872 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
0b8e58a1 6873 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
9c3013e9
JK
6874 if (IS_ERR(handle)) {
6875 /* Release dquot anyway to avoid endless cycle in dqput() */
6876 dquot_release(dquot);
ac27a0ec 6877 return PTR_ERR(handle);
9c3013e9 6878 }
ac27a0ec 6879 ret = dquot_release(dquot);
d8b945fa
YB
6880 if (ret < 0)
6881 ext4_error_err(dquot->dq_sb, -ret,
6882 "Failed to release dquot type %d",
6883 dquot->dq_id.type);
617ba13b 6884 err = ext4_journal_stop(handle);
ac27a0ec
DK
6885 if (!ret)
6886 ret = err;
6887 return ret;
6888}
6889
617ba13b 6890static int ext4_mark_dquot_dirty(struct dquot *dquot)
ac27a0ec 6891{
262b4662 6892 struct super_block *sb = dquot->dq_sb;
262b4662 6893
f177ee08 6894 if (ext4_is_quota_journalled(sb)) {
ac27a0ec 6895 dquot_mark_dquot_dirty(dquot);
617ba13b 6896 return ext4_write_dquot(dquot);
ac27a0ec
DK
6897 } else {
6898 return dquot_mark_dquot_dirty(dquot);
6899 }
6900}
6901
617ba13b 6902static int ext4_write_info(struct super_block *sb, int type)
ac27a0ec
DK
6903{
6904 int ret, err;
6905 handle_t *handle;
6906
6907 /* Data block + inode block */
f9c1f248 6908 handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
ac27a0ec
DK
6909 if (IS_ERR(handle))
6910 return PTR_ERR(handle);
6911 ret = dquot_commit_info(sb, type);
617ba13b 6912 err = ext4_journal_stop(handle);
ac27a0ec
DK
6913 if (!ret)
6914 ret = err;
6915 return ret;
6916}
6917
daf647d2
TT
6918static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6919{
6920 struct ext4_inode_info *ei = EXT4_I(inode);
6921
6922 /* The first argument of lockdep_set_subclass has to be
6923 * *exactly* the same as the argument to init_rwsem() --- in
6924 * this case, in init_once() --- or lockdep gets unhappy
6925 * because the name of the lock is set using the
6926 * stringification of the argument to init_rwsem().
6927 */
6928 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
6929 lockdep_set_subclass(&ei->i_data_sem, subclass);
6930}
6931
ac27a0ec
DK
6932/*
6933 * Standard function to be called on quota_on
6934 */
617ba13b 6935static int ext4_quota_on(struct super_block *sb, int type, int format_id,
8c54ca9c 6936 const struct path *path)
ac27a0ec
DK
6937{
6938 int err;
ac27a0ec
DK
6939
6940 if (!test_opt(sb, QUOTA))
6941 return -EINVAL;
0623543b 6942
ac27a0ec 6943 /* Quotafile not on the same filesystem? */
d8c9584e 6944 if (path->dentry->d_sb != sb)
ac27a0ec 6945 return -EXDEV;
e0770e91
JK
6946
6947 /* Quota already enabled for this file? */
6948 if (IS_NOQUOTA(d_inode(path->dentry)))
6949 return -EBUSY;
6950
0623543b
JK
6951 /* Journaling quota? */
6952 if (EXT4_SB(sb)->s_qf_names[type]) {
2b2d6d01 6953 /* Quotafile not in fs root? */
f00c9e44 6954 if (path->dentry->d_parent != sb->s_root)
b31e1552
ES
6955 ext4_msg(sb, KERN_WARNING,
6956 "Quota file not on filesystem root. "
6957 "Journaled quota will not work");
91389240
JK
6958 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6959 } else {
6960 /*
6961 * Clear the flag just in case mount options changed since
6962 * last time.
6963 */
6964 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
2b2d6d01 6965 }
0623543b 6966
daf647d2
TT
6967 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6968 err = dquot_quota_on(sb, type, format_id, path);
15fc69bb 6969 if (!err) {
957153fc
JK
6970 struct inode *inode = d_inode(path->dentry);
6971 handle_t *handle;
6972
61a92987
JK
6973 /*
6974 * Set inode flags to prevent userspace from messing with quota
6975 * files. If this fails, we return success anyway since quotas
6976 * are already enabled and this is not a hard failure.
6977 */
957153fc
JK
6978 inode_lock(inode);
6979 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6980 if (IS_ERR(handle))
6981 goto unlock_inode;
6982 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6983 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6984 S_NOATIME | S_IMMUTABLE);
4209ae12 6985 err = ext4_mark_inode_dirty(handle, inode);
957153fc
JK
6986 ext4_journal_stop(handle);
6987 unlock_inode:
6988 inode_unlock(inode);
15fc69bb
JK
6989 if (err)
6990 dquot_quota_off(sb, type);
957153fc 6991 }
15fc69bb
JK
6992 if (err)
6993 lockdep_set_quota_inode(path->dentry->d_inode,
6994 I_DATA_SEM_NORMAL);
daf647d2 6995 return err;
ac27a0ec
DK
6996}
6997
07342ec2
BL
6998static inline bool ext4_check_quota_inum(int type, unsigned long qf_inum)
6999{
7000 switch (type) {
7001 case USRQUOTA:
7002 return qf_inum == EXT4_USR_QUOTA_INO;
7003 case GRPQUOTA:
7004 return qf_inum == EXT4_GRP_QUOTA_INO;
7005 case PRJQUOTA:
7006 return qf_inum >= EXT4_GOOD_OLD_FIRST_INO;
7007 default:
7008 BUG();
7009 }
7010}
7011
7c319d32
AK
7012static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
7013 unsigned int flags)
7014{
7015 int err;
7016 struct inode *qf_inode;
a2d4a646 7017 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
7c319d32 7018 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
689c958c
LX
7019 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
7020 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
7c319d32
AK
7021 };
7022
e2b911c5 7023 BUG_ON(!ext4_has_feature_quota(sb));
7c319d32
AK
7024
7025 if (!qf_inums[type])
7026 return -EPERM;
7027
07342ec2
BL
7028 if (!ext4_check_quota_inum(type, qf_inums[type])) {
7029 ext4_error(sb, "Bad quota inum: %lu, type: %d",
7030 qf_inums[type], type);
7031 return -EUCLEAN;
7032 }
7033
8a363970 7034 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
7c319d32 7035 if (IS_ERR(qf_inode)) {
07342ec2
BL
7036 ext4_error(sb, "Bad quota inode: %lu, type: %d",
7037 qf_inums[type], type);
7c319d32
AK
7038 return PTR_ERR(qf_inode);
7039 }
7040
bcb13850
JK
7041 /* Don't account quota for quota files to avoid recursion */
7042 qf_inode->i_flags |= S_NOQUOTA;
daf647d2 7043 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
7212b95e 7044 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
daf647d2
TT
7045 if (err)
7046 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
61157b24 7047 iput(qf_inode);
7c319d32
AK
7048
7049 return err;
7050}
7051
7052/* Enable usage tracking for all quota types. */
25c6d98f 7053int ext4_enable_quotas(struct super_block *sb)
7c319d32
AK
7054{
7055 int type, err = 0;
a2d4a646 7056 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
7c319d32 7057 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
689c958c
LX
7058 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
7059 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
7c319d32 7060 };
49da9392
JK
7061 bool quota_mopt[EXT4_MAXQUOTAS] = {
7062 test_opt(sb, USRQUOTA),
7063 test_opt(sb, GRPQUOTA),
7064 test_opt(sb, PRJQUOTA),
7065 };
7c319d32 7066
91389240 7067 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
a2d4a646 7068 for (type = 0; type < EXT4_MAXQUOTAS; type++) {
7c319d32
AK
7069 if (qf_inums[type]) {
7070 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
49da9392
JK
7071 DQUOT_USAGE_ENABLED |
7072 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
7c319d32
AK
7073 if (err) {
7074 ext4_warning(sb,
72ba7450 7075 "Failed to enable quota tracking "
07342ec2
BL
7076 "(type=%d, err=%d, ino=%lu). "
7077 "Please run e2fsck to fix.", type,
7078 err, qf_inums[type]);
7f144fd0 7079
f3c1c42e 7080 ext4_quotas_off(sb, type);
7c319d32
AK
7081 return err;
7082 }
7083 }
7084 }
7085 return 0;
7086}
7087
ca0e05e4
DM
7088static int ext4_quota_off(struct super_block *sb, int type)
7089{
21f97697
JK
7090 struct inode *inode = sb_dqopt(sb)->files[type];
7091 handle_t *handle;
957153fc 7092 int err;
21f97697 7093
87009d86
DM
7094 /* Force all delayed allocation blocks to be allocated.
7095 * Caller already holds s_umount sem */
7096 if (test_opt(sb, DELALLOC))
ca0e05e4 7097 sync_filesystem(sb);
ca0e05e4 7098
957153fc 7099 if (!inode || !igrab(inode))
0b268590
AG
7100 goto out;
7101
957153fc 7102 err = dquot_quota_off(sb, type);
964edf66 7103 if (err || ext4_has_feature_quota(sb))
957153fc 7104 goto out_put;
e0e985f3
JK
7105 /*
7106 * When the filesystem was remounted read-only first, we cannot cleanup
7107 * inode flags here. Bad luck but people should be using QUOTA feature
7108 * these days anyway.
7109 */
7110 if (sb_rdonly(sb))
7111 goto out_put;
957153fc
JK
7112
7113 inode_lock(inode);
61a92987
JK
7114 /*
7115 * Update modification times of quota files when userspace can
7116 * start looking at them. If we fail, we return success anyway since
7117 * this is not a hard failure and quotas are already disabled.
7118 */
9924a92a 7119 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
4209ae12
HS
7120 if (IS_ERR(handle)) {
7121 err = PTR_ERR(handle);
957153fc 7122 goto out_unlock;
4209ae12 7123 }
957153fc
JK
7124 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
7125 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
b898ab23 7126 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
4209ae12 7127 err = ext4_mark_inode_dirty(handle, inode);
21f97697 7128 ext4_journal_stop(handle);
957153fc
JK
7129out_unlock:
7130 inode_unlock(inode);
7131out_put:
964edf66 7132 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
957153fc
JK
7133 iput(inode);
7134 return err;
21f97697 7135out:
ca0e05e4
DM
7136 return dquot_quota_off(sb, type);
7137}
7138
ac27a0ec
DK
7139/* Read data from quotafile - avoid pagecache and such because we cannot afford
7140 * acquiring the locks... As quota files are never truncated and quota code
25985edc 7141 * itself serializes the operations (and no one else should touch the files)
ac27a0ec 7142 * we don't have to be afraid of races */
617ba13b 7143static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
ac27a0ec
DK
7144 size_t len, loff_t off)
7145{
7146 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 7147 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
ac27a0ec
DK
7148 int offset = off & (sb->s_blocksize - 1);
7149 int tocopy;
7150 size_t toread;
7151 struct buffer_head *bh;
7152 loff_t i_size = i_size_read(inode);
7153
7154 if (off > i_size)
7155 return 0;
7156 if (off+len > i_size)
7157 len = i_size-off;
7158 toread = len;
7159 while (toread > 0) {
66267814 7160 tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
1c215028
TT
7161 bh = ext4_bread(NULL, inode, blk, 0);
7162 if (IS_ERR(bh))
7163 return PTR_ERR(bh);
ac27a0ec
DK
7164 if (!bh) /* A hole? */
7165 memset(data, 0, tocopy);
7166 else
7167 memcpy(data, bh->b_data+offset, tocopy);
7168 brelse(bh);
7169 offset = 0;
7170 toread -= tocopy;
7171 data += tocopy;
7172 blk++;
7173 }
7174 return len;
7175}
7176
7177/* Write to quotafile (we know the transaction is already started and has
7178 * enough credits) */
617ba13b 7179static ssize_t ext4_quota_write(struct super_block *sb, int type,
ac27a0ec
DK
7180 const char *data, size_t len, loff_t off)
7181{
7182 struct inode *inode = sb_dqopt(sb)->files[type];
725d26d3 7183 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
4209ae12 7184 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
c5e298ae 7185 int retries = 0;
ac27a0ec
DK
7186 struct buffer_head *bh;
7187 handle_t *handle = journal_current_handle();
7188
380a0091 7189 if (!handle) {
b31e1552
ES
7190 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
7191 " cancelled because transaction is not started",
9c3013e9
JK
7192 (unsigned long long)off, (unsigned long long)len);
7193 return -EIO;
7194 }
67eeb568
DM
7195 /*
7196 * Since we account only one data block in transaction credits,
7197 * then it is impossible to cross a block boundary.
7198 */
7199 if (sb->s_blocksize - offset < len) {
7200 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
7201 " cancelled because not block aligned",
7202 (unsigned long long)off, (unsigned long long)len);
7203 return -EIO;
7204 }
7205
c5e298ae
TT
7206 do {
7207 bh = ext4_bread(handle, inode, blk,
7208 EXT4_GET_BLOCKS_CREATE |
7209 EXT4_GET_BLOCKS_METADATA_NOFAIL);
45586c70 7210 } while (PTR_ERR(bh) == -ENOSPC &&
c5e298ae 7211 ext4_should_retry_alloc(inode->i_sb, &retries));
1c215028
TT
7212 if (IS_ERR(bh))
7213 return PTR_ERR(bh);
67eeb568
DM
7214 if (!bh)
7215 goto out;
5d601255 7216 BUFFER_TRACE(bh, "get write access");
188c299e 7217 err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
62d2b5f2
JK
7218 if (err) {
7219 brelse(bh);
1c215028 7220 return err;
ac27a0ec 7221 }
67eeb568
DM
7222 lock_buffer(bh);
7223 memcpy(bh->b_data+offset, data, len);
7224 flush_dcache_page(bh->b_page);
7225 unlock_buffer(bh);
62d2b5f2 7226 err = ext4_handle_dirty_metadata(handle, NULL, bh);
67eeb568 7227 brelse(bh);
ac27a0ec 7228out:
67eeb568
DM
7229 if (inode->i_size < off + len) {
7230 i_size_write(inode, off + len);
617ba13b 7231 EXT4_I(inode)->i_disksize = inode->i_size;
4209ae12
HS
7232 err2 = ext4_mark_inode_dirty(handle, inode);
7233 if (unlikely(err2 && !err))
7234 err = err2;
ac27a0ec 7235 }
4209ae12 7236 return err ? err : len;
ac27a0ec 7237}
ac27a0ec
DK
7238#endif
7239
c290ea01 7240#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
24b58424
TT
7241static inline void register_as_ext2(void)
7242{
7243 int err = register_filesystem(&ext2_fs_type);
7244 if (err)
7245 printk(KERN_WARNING
7246 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
7247}
7248
7249static inline void unregister_as_ext2(void)
7250{
7251 unregister_filesystem(&ext2_fs_type);
7252}
2035e776
TT
7253
7254static inline int ext2_feature_set_ok(struct super_block *sb)
7255{
e2b911c5 7256 if (ext4_has_unknown_ext2_incompat_features(sb))
2035e776 7257 return 0;
bc98a42c 7258 if (sb_rdonly(sb))
2035e776 7259 return 1;
e2b911c5 7260 if (ext4_has_unknown_ext2_ro_compat_features(sb))
2035e776
TT
7261 return 0;
7262 return 1;
7263}
24b58424
TT
7264#else
7265static inline void register_as_ext2(void) { }
7266static inline void unregister_as_ext2(void) { }
2035e776 7267static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
24b58424
TT
7268#endif
7269
24b58424
TT
7270static inline void register_as_ext3(void)
7271{
7272 int err = register_filesystem(&ext3_fs_type);
7273 if (err)
7274 printk(KERN_WARNING
7275 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
7276}
7277
7278static inline void unregister_as_ext3(void)
7279{
7280 unregister_filesystem(&ext3_fs_type);
7281}
2035e776
TT
7282
7283static inline int ext3_feature_set_ok(struct super_block *sb)
7284{
e2b911c5 7285 if (ext4_has_unknown_ext3_incompat_features(sb))
2035e776 7286 return 0;
e2b911c5 7287 if (!ext4_has_feature_journal(sb))
2035e776 7288 return 0;
bc98a42c 7289 if (sb_rdonly(sb))
2035e776 7290 return 1;
e2b911c5 7291 if (ext4_has_unknown_ext3_ro_compat_features(sb))
2035e776
TT
7292 return 0;
7293 return 1;
7294}
24b58424 7295
1489dffd
CH
7296static void ext4_kill_sb(struct super_block *sb)
7297{
7298 struct ext4_sb_info *sbi = EXT4_SB(sb);
61ead714 7299 struct file *bdev_file = sbi ? sbi->s_journal_bdev_file : NULL;
1489dffd
CH
7300
7301 kill_block_super(sb);
7302
61ead714 7303 if (bdev_file)
22650a99 7304 bdev_fput(bdev_file);
1489dffd
CH
7305}
7306
03010a33 7307static struct file_system_type ext4_fs_type = {
cebe85d5
LC
7308 .owner = THIS_MODULE,
7309 .name = "ext4",
7310 .init_fs_context = ext4_init_fs_context,
7311 .parameters = ext4_param_specs,
1489dffd 7312 .kill_sb = ext4_kill_sb,
50ec1d72 7313 .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
03010a33 7314};
7f78e035 7315MODULE_ALIAS_FS("ext4");
03010a33 7316
e9e3bcec
ES
7317/* Shared across all ext4 file systems */
7318wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
e9e3bcec 7319
5dabfc78 7320static int __init ext4_init_fs(void)
ac27a0ec 7321{
e9e3bcec 7322 int i, err;
c9de560d 7323
e294a537 7324 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
07c0c5d8 7325 ext4_li_info = NULL;
07c0c5d8 7326
9a4c8019 7327 /* Build-time check for flags consistency */
12e9b892 7328 ext4_check_flag_values();
e9e3bcec 7329
e142d052 7330 for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
e9e3bcec 7331 init_waitqueue_head(&ext4__ioend_wq[i]);
e9e3bcec 7332
51865fda 7333 err = ext4_init_es();
6fd058f7
TT
7334 if (err)
7335 return err;
51865fda 7336
1dc0aa46 7337 err = ext4_init_pending();
22cfe4b4
EB
7338 if (err)
7339 goto out7;
7340
7341 err = ext4_init_post_read_processing();
1dc0aa46
EW
7342 if (err)
7343 goto out6;
7344
51865fda
ZL
7345 err = ext4_init_pageio();
7346 if (err)
b5799018 7347 goto out5;
51865fda 7348
5dabfc78 7349 err = ext4_init_system_zone();
bd2d0210 7350 if (err)
b5799018 7351 goto out4;
857ac889 7352
b5799018 7353 err = ext4_init_sysfs();
dd68314c 7354 if (err)
b5799018 7355 goto out3;
857ac889 7356
5dabfc78 7357 err = ext4_init_mballoc();
c9de560d
AT
7358 if (err)
7359 goto out2;
ac27a0ec
DK
7360 err = init_inodecache();
7361 if (err)
7362 goto out1;
aa75f4d3
HS
7363
7364 err = ext4_fc_init_dentry_cache();
7365 if (err)
7366 goto out05;
7367
24b58424 7368 register_as_ext3();
2035e776 7369 register_as_ext2();
03010a33 7370 err = register_filesystem(&ext4_fs_type);
ac27a0ec
DK
7371 if (err)
7372 goto out;
bfff6873 7373
ac27a0ec
DK
7374 return 0;
7375out:
24b58424
TT
7376 unregister_as_ext2();
7377 unregister_as_ext3();
ab047d51 7378 ext4_fc_destroy_dentry_cache();
aa75f4d3 7379out05:
ac27a0ec
DK
7380 destroy_inodecache();
7381out1:
5dabfc78 7382 ext4_exit_mballoc();
9c191f70 7383out2:
b5799018
TT
7384 ext4_exit_sysfs();
7385out3:
5dabfc78 7386 ext4_exit_system_zone();
b5799018 7387out4:
5dabfc78 7388 ext4_exit_pageio();
b5799018 7389out5:
22cfe4b4 7390 ext4_exit_post_read_processing();
1dc0aa46 7391out6:
22cfe4b4
EB
7392 ext4_exit_pending();
7393out7:
51865fda
ZL
7394 ext4_exit_es();
7395
ac27a0ec
DK
7396 return err;
7397}
7398
5dabfc78 7399static void __exit ext4_exit_fs(void)
ac27a0ec 7400{
bfff6873 7401 ext4_destroy_lazyinit_thread();
24b58424
TT
7402 unregister_as_ext2();
7403 unregister_as_ext3();
03010a33 7404 unregister_filesystem(&ext4_fs_type);
ab047d51 7405 ext4_fc_destroy_dentry_cache();
ac27a0ec 7406 destroy_inodecache();
5dabfc78 7407 ext4_exit_mballoc();
b5799018 7408 ext4_exit_sysfs();
5dabfc78
TT
7409 ext4_exit_system_zone();
7410 ext4_exit_pageio();
22cfe4b4 7411 ext4_exit_post_read_processing();
dd12ed14 7412 ext4_exit_es();
1dc0aa46 7413 ext4_exit_pending();
ac27a0ec
DK
7414}
7415
7416MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
83982b6f 7417MODULE_DESCRIPTION("Fourth Extended Filesystem");
ac27a0ec 7418MODULE_LICENSE("GPL");
7ef79ad5 7419MODULE_SOFTDEP("pre: crc32c");
5dabfc78
TT
7420module_init(ext4_init_fs)
7421module_exit(ext4_exit_fs)