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