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