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