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