blob: 6937ab4dfa67652ce74fa1685f92c6dbbd890f9b [file] [log] [blame]
Theodore Ts'of5166762017-12-17 22:00:59 -05001// SPDX-License-Identifier: GPL-2.0
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002/*
Mingming Cao617ba132006-10-11 01:20:53 -07003 * linux/fs/ext4/super.c
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004 *
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>
Theodore Ts'oc5ca7c72009-04-27 22:48:48 -040024#include <linux/vmalloc.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070025#include <linux/slab.h>
26#include <linux/init.h>
27#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040028#include <linux/backing-dev.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070029#include <linux/parser.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070030#include <linux/buffer_head.h>
Christoph Hellwiga5694252007-07-17 04:04:28 -070031#include <linux/exportfs.h>
Dave Kleikampac27a0e2006-10-11 01:20:50 -070032#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>
Theodore Ts'o3197ebd2009-03-31 09:10:09 -040038#include <linux/ctype.h>
Vignesh Babu13305932007-07-18 09:11:02 -040039#include <linux/log2.h>
Andreas Dilger717d50e2007-10-16 18:38:25 -040040#include <linux/crc16.h>
Dan Williamsef510422017-05-08 10:55:27 -070041#include <linux/dax.h>
Dan Magenheimer7abc52c2011-05-26 10:02:03 -060042#include <linux/cleancache.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080043#include <linux/uaccess.h>
Jeff Laytonee73f9a2018-01-09 08:21:39 -050044#include <linux/iversion.h>
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -040045#include <linux/unicode.h>
Christoph Hellwigc6a564ff2020-03-25 16:48:42 +010046#include <linux/part_stat.h>
Lukas Czernerbfff6872010-10-27 21:30:05 -040047#include <linux/kthread.h>
48#include <linux/freezer.h>
49
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040050#include "ext4.h"
Theodore Ts'o4a092d72012-11-28 13:03:30 -050051#include "ext4_extents.h" /* Needed for trace points definition */
Christoph Hellwig3dcf5452008-04-29 18:13:32 -040052#include "ext4_jbd2.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070053#include "xattr.h"
54#include "acl.h"
Theodore Ts'o3661d282009-09-14 22:59:50 -040055#include "mballoc.h"
Darrick J. Wong0c9ec4b2017-04-30 00:36:53 -040056#include "fsmap.h"
Dave Kleikampac27a0e2006-10-11 01:20:50 -070057
Theodore Ts'o9bffad12009-06-17 11:48:11 -040058#define CREATE_TRACE_POINTS
59#include <trace/events/ext4.h>
60
Lukas Czerner0b75a842011-02-23 12:22:49 -050061static struct ext4_lazy_init *ext4_li_info;
62static struct mutex ext4_li_mtx;
Theodore Ts'oe294a532015-08-15 14:59:44 -040063static struct ratelimit_state ext4_mount_msg_ratelimit;
Theodore Ts'o9f6200b2008-09-23 09:18:24 -040064
Mingming Cao617ba132006-10-11 01:20:53 -070065static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
Dave Kleikampac27a0e2006-10-11 01:20:50 -070066 unsigned long journal_devnum);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -050067static int ext4_show_options(struct seq_file *seq, struct dentry *root);
Theodore Ts'oe2d67052009-05-01 00:33:44 -040068static int ext4_commit_super(struct super_block *sb, int sync);
Jan Kara11215632020-07-10 16:07:59 +020069static int ext4_mark_recovery_complete(struct super_block *sb,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040070 struct ext4_super_block *es);
Jan Kara11215632020-07-10 16:07:59 +020071static int ext4_clear_journal_err(struct super_block *sb,
72 struct ext4_super_block *es);
Mingming Cao617ba132006-10-11 01:20:53 -070073static int ext4_sync_fs(struct super_block *sb, int wait);
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -040074static int ext4_remount(struct super_block *sb, int *flags, char *data);
75static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
Takashi Satoc4be0c12009-01-09 16:40:58 -080076static int ext4_unfreeze(struct super_block *sb);
Takashi Satoc4be0c12009-01-09 16:40:58 -080077static int ext4_freeze(struct super_block *sb);
Al Viro152a0832010-07-25 00:46:55 +040078static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
79 const char *dev_name, void *data);
Theodore Ts'o2035e772011-04-18 17:29:14 -040080static inline int ext2_feature_set_ok(struct super_block *sb);
81static inline int ext3_feature_set_ok(struct super_block *sb);
Amir Goldsteind39195c2011-02-28 00:53:45 -050082static int ext4_feature_set_ok(struct super_block *sb, int readonly);
Lukas Czernerbfff6872010-10-27 21:30:05 -040083static void ext4_destroy_lazyinit_thread(void);
84static void ext4_unregister_li_request(struct super_block *sb);
Eric Sandeen8f1f7452011-02-03 14:33:15 -050085static void ext4_clear_request_list(void);
Eric Whitneyc6cb7e72016-09-30 02:05:09 -040086static struct inode *ext4_get_journal_inode(struct super_block *sb,
87 unsigned int journal_inum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -070088
Jan Karae74031f2015-12-07 14:35:49 -050089/*
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:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -070096 * mmap_lock -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
Jan Karae74031f2015-12-07 14:35:49 -050097 * page lock -> i_data_sem (rw)
98 *
99 * buffered write path:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700100 * sb_start_write -> i_mutex -> mmap_lock
Jan Karae74031f2015-12-07 14:35:49 -0500101 * sb_start_write -> i_mutex -> transaction start -> page lock ->
102 * i_data_sem (rw)
103 *
104 * truncate:
Nikolay Borisov1d398342018-03-22 11:52:10 -0400105 * 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)
Jan Karae74031f2015-12-07 14:35:49 -0500108 *
109 * direct IO:
Michel Lespinassec1e8d7c2020-06-08 21:33:54 -0700110 * sb_start_write -> i_mutex -> mmap_lock
Nikolay Borisov1d398342018-03-22 11:52:10 -0400111 * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
Jan Karae74031f2015-12-07 14:35:49 -0500112 *
113 * writepages:
114 * transaction start -> page lock(s) -> i_data_sem (rw)
115 */
116
Jan Karac290ea02015-06-18 16:52:29 +0200117#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
Theodore Ts'o2035e772011-04-18 17:29:14 -0400118static 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};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800125MODULE_ALIAS_FS("ext2");
Eric W. Biedermanfa7614dd2013-03-12 18:27:41 -0700126MODULE_ALIAS("ext2");
Theodore Ts'o2035e772011-04-18 17:29:14 -0400127#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
Jan Karaba69f9a2010-03-24 20:18:37 -0400133static struct file_system_type ext3_fs_type = {
134 .owner = THIS_MODULE,
135 .name = "ext3",
Al Viro152a0832010-07-25 00:46:55 +0400136 .mount = ext4_mount,
Jan Karaba69f9a2010-03-24 20:18:37 -0400137 .kill_sb = kill_block_super,
138 .fs_flags = FS_REQUIRES_DEV,
139};
Eric W. Biederman7f78e032013-03-02 19:39:14 -0800140MODULE_ALIAS_FS("ext3");
Eric W. Biedermanfa7614dd2013-03-12 18:27:41 -0700141MODULE_ALIAS("ext3");
Jan Karaba69f9a2010-03-24 20:18:37 -0400142#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700143
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500144/*
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);
Theodore Ts'ocf2834a2019-12-14 16:42:52 -0500157 if (ext4_buffer_uptodate(bh))
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500158 return bh;
zhangyi (F)d9befed2020-09-24 15:33:31 +0800159 clear_buffer_verified(bh);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500160 ll_rw_block(REQ_OP_READ, REQ_META | op_flags, 1, &bh);
161 wait_on_buffer(bh);
162 if (buffer_uptodate(bh))
163 return bh;
164 put_bh(bh);
165 return ERR_PTR(-EIO);
166}
167
Darrick J. Wongd25425f2012-04-29 18:25:10 -0400168static int ext4_verify_csum_type(struct super_block *sb,
169 struct ext4_super_block *es)
170{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -0400171 if (!ext4_has_feature_metadata_csum(sb))
Darrick J. Wongd25425f2012-04-29 18:25:10 -0400172 return 1;
173
174 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
175}
176
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400177static __le32 ext4_superblock_csum(struct super_block *sb,
178 struct ext4_super_block *es)
179{
180 struct ext4_sb_info *sbi = EXT4_SB(sb);
181 int offset = offsetof(struct ext4_super_block, s_checksum);
182 __u32 csum;
183
184 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
185
186 return cpu_to_le32(csum);
187}
188
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400189static int ext4_superblock_csum_verify(struct super_block *sb,
190 struct ext4_super_block *es)
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400191{
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400192 if (!ext4_has_metadata_csum(sb))
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400193 return 1;
194
195 return es->s_checksum == ext4_superblock_csum(sb, es);
196}
197
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400198void ext4_superblock_csum_set(struct super_block *sb)
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400199{
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400200 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
201
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400202 if (!ext4_has_metadata_csum(sb))
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400203 return;
204
Constantine Sapuntzakisacaa5322020-09-14 10:10:14 -0600205 /*
206 * Locking the superblock prevents the scenario
207 * where:
208 * 1) a first thread pauses during checksum calculation.
209 * 2) a second thread updates the superblock, recalculates
210 * the checksum, and updates s_checksum
211 * 3) the first thread resumes and finishes its checksum calculation
212 * and updates s_checksum with a potentially stale or torn value.
213 */
214 lock_buffer(EXT4_SB(sb)->s_sbh);
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400215 es->s_checksum = ext4_superblock_csum(sb, es);
Constantine Sapuntzakisacaa5322020-09-14 10:10:14 -0600216 unlock_buffer(EXT4_SB(sb)->s_sbh);
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400217}
218
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700219ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
220 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700221{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400222 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700223 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400224 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700225}
226
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700227ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
228 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700229{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400230 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700231 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400232 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700233}
234
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700235ext4_fsblk_t ext4_inode_table(struct super_block *sb,
236 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700237{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400238 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700239 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400240 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700241}
242
Theodore Ts'o021b65b2011-09-09 19:08:51 -0400243__u32 ext4_free_group_clusters(struct super_block *sb,
244 struct ext4_group_desc *bg)
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500245{
246 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
247 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400248 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500249}
250
251__u32 ext4_free_inodes_count(struct super_block *sb,
252 struct ext4_group_desc *bg)
253{
254 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
255 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400256 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500257}
258
259__u32 ext4_used_dirs_count(struct super_block *sb,
260 struct ext4_group_desc *bg)
261{
262 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
263 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400264 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500265}
266
267__u32 ext4_itable_unused_count(struct super_block *sb,
268 struct ext4_group_desc *bg)
269{
270 return le16_to_cpu(bg->bg_itable_unused_lo) |
271 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400272 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500273}
274
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700275void ext4_block_bitmap_set(struct super_block *sb,
276 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700277{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400278 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700279 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
280 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700281}
282
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700283void ext4_inode_bitmap_set(struct super_block *sb,
284 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700285{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400286 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700287 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
288 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700289}
290
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700291void ext4_inode_table_set(struct super_block *sb,
292 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700293{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400294 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700295 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
296 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700297}
298
Theodore Ts'o021b65b2011-09-09 19:08:51 -0400299void ext4_free_group_clusters_set(struct super_block *sb,
300 struct ext4_group_desc *bg, __u32 count)
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500301{
302 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
303 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
304 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
305}
306
307void ext4_free_inodes_set(struct super_block *sb,
308 struct ext4_group_desc *bg, __u32 count)
309{
310 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
311 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
312 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
313}
314
315void ext4_used_dirs_set(struct super_block *sb,
316 struct ext4_group_desc *bg, __u32 count)
317{
318 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
319 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
320 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
321}
322
323void ext4_itable_unused_set(struct super_block *sb,
324 struct ext4_group_desc *bg, __u32 count)
325{
326 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
327 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
328 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
329}
330
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400331static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
332{
333 time64_t now = ktime_get_real_seconds();
334
335 now = clamp_val(now, 0, (1ull << 40) - 1);
336
337 *lo = cpu_to_le32(lower_32_bits(now));
338 *hi = upper_32_bits(now);
339}
340
341static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
342{
343 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
344}
345#define ext4_update_tstamp(es, tstamp) \
346 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
347#define ext4_get_tstamp(es, tstamp) \
348 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400349
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400350static void __save_error_info(struct super_block *sb, int error,
351 __u32 ino, __u64 block,
352 const char *func, unsigned int line)
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400353{
354 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400355 int err;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400356
357 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Theodore Ts'o1b466172015-05-14 18:37:30 -0400358 if (bdev_read_only(sb->s_bdev))
359 return;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400360 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400361 ext4_update_tstamp(es, s_last_error_time);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400362 strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
363 es->s_last_error_line = cpu_to_le32(line);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400364 es->s_last_error_ino = cpu_to_le32(ino);
365 es->s_last_error_block = cpu_to_le64(block);
366 switch (error) {
367 case EIO:
368 err = EXT4_ERR_EIO;
369 break;
370 case ENOMEM:
371 err = EXT4_ERR_ENOMEM;
372 break;
373 case EFSBADCRC:
374 err = EXT4_ERR_EFSBADCRC;
375 break;
376 case 0:
377 case EFSCORRUPTED:
378 err = EXT4_ERR_EFSCORRUPTED;
379 break;
380 case ENOSPC:
381 err = EXT4_ERR_ENOSPC;
382 break;
383 case ENOKEY:
384 err = EXT4_ERR_ENOKEY;
385 break;
386 case EROFS:
387 err = EXT4_ERR_EROFS;
388 break;
389 case EFBIG:
390 err = EXT4_ERR_EFBIG;
391 break;
392 case EEXIST:
393 err = EXT4_ERR_EEXIST;
394 break;
395 case ERANGE:
396 err = EXT4_ERR_ERANGE;
397 break;
398 case EOVERFLOW:
399 err = EXT4_ERR_EOVERFLOW;
400 break;
401 case EBUSY:
402 err = EXT4_ERR_EBUSY;
403 break;
404 case ENOTDIR:
405 err = EXT4_ERR_ENOTDIR;
406 break;
407 case ENOTEMPTY:
408 err = EXT4_ERR_ENOTEMPTY;
409 break;
410 case ESHUTDOWN:
411 err = EXT4_ERR_ESHUTDOWN;
412 break;
413 case EFAULT:
414 err = EXT4_ERR_EFAULT;
415 break;
416 default:
417 err = EXT4_ERR_UNKNOWN;
418 }
419 es->s_last_error_errcode = err;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400420 if (!es->s_first_error_time) {
421 es->s_first_error_time = es->s_last_error_time;
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400422 es->s_first_error_time_hi = es->s_last_error_time_hi;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400423 strncpy(es->s_first_error_func, func,
424 sizeof(es->s_first_error_func));
425 es->s_first_error_line = cpu_to_le32(line);
426 es->s_first_error_ino = es->s_last_error_ino;
427 es->s_first_error_block = es->s_last_error_block;
Theodore Ts'o878520a2019-11-19 21:54:15 -0500428 es->s_first_error_errcode = es->s_last_error_errcode;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400429 }
Theodore Ts'o66e61a92010-07-27 11:56:04 -0400430 /*
431 * Start the daily error reporting function if it hasn't been
432 * started already
433 */
434 if (!es->s_error_count)
435 mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
Wei Yongjunba39ebb2012-09-27 09:37:53 -0400436 le32_add_cpu(&es->s_error_count, 1);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400437}
438
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400439static void save_error_info(struct super_block *sb, int error,
440 __u32 ino, __u64 block,
441 const char *func, unsigned int line)
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400442{
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400443 __save_error_info(sb, error, ino, block, func, line);
Eric Sandeenc96e2b82020-03-18 14:19:38 -0500444 if (!bdev_read_only(sb->s_bdev))
445 ext4_commit_super(sb, 1);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400446}
447
Theodore Ts'obdfe0cbd2015-08-16 10:03:57 -0400448/*
449 * The del_gendisk() function uninitializes the disk-specific data
450 * structures, including the bdi structure, without telling anyone
451 * else. Once this happens, any attempt to call mark_buffer_dirty()
452 * (for example, by ext4_commit_super), will cause a kernel OOPS.
453 * This is a kludge to prevent these oops until we can put in a proper
454 * hook in del_gendisk() to inform the VFS and file system layers.
455 */
456static int block_device_ejected(struct super_block *sb)
457{
458 struct inode *bd_inode = sb->s_bdev->bd_inode;
459 struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
460
461 return bdi->dev == NULL;
462}
463
Bobi Jam18aadd42012-02-20 17:53:02 -0500464static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
465{
466 struct super_block *sb = journal->j_private;
467 struct ext4_sb_info *sbi = EXT4_SB(sb);
468 int error = is_journal_aborted(journal);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400469 struct ext4_journal_cb_entry *jce;
Bobi Jam18aadd42012-02-20 17:53:02 -0500470
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400471 BUG_ON(txn->t_state == T_FINISHED);
Daeho Jeonga0154342017-06-22 23:54:33 -0400472
473 ext4_process_freed_data(sb, txn->t_tid);
474
Bobi Jam18aadd42012-02-20 17:53:02 -0500475 spin_lock(&sbi->s_md_lock);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400476 while (!list_empty(&txn->t_private_list)) {
477 jce = list_entry(txn->t_private_list.next,
478 struct ext4_journal_cb_entry, jce_list);
Bobi Jam18aadd42012-02-20 17:53:02 -0500479 list_del_init(&jce->jce_list);
480 spin_unlock(&sbi->s_md_lock);
481 jce->jce_func(sb, jce, error);
482 spin_lock(&sbi->s_md_lock);
483 }
484 spin_unlock(&sbi->s_md_lock);
485}
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400486
Jan Kara1dc10972019-03-14 23:46:05 -0400487static bool system_going_down(void)
488{
489 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
490 || system_state == SYSTEM_RESTART;
491}
492
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700493/* Deal with the reporting of failure conditions on a filesystem such as
494 * inconsistencies detected or read IO failures.
495 *
496 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700497 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700498 * write ordering constraints on the superblock which prevent us from
499 * writing it out straight away; and given that the journal is about to
500 * be aborted, we can't rely on the current, or future, transactions to
501 * write out the superblock safely.
502 *
Mingming Caodab291a2006-10-11 01:21:01 -0700503 * We'll just use the jbd2_journal_abort() error code to record an error in
Thadeu Lima de Souza Cascardod6b198b2010-01-17 19:10:07 -0200504 * the journal instead. On recovery, the journal will complain about
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700505 * that error until we've noted it down and cleared it.
506 */
507
Mingming Cao617ba132006-10-11 01:20:53 -0700508static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700509{
Theodore Ts'o327eaf72018-06-12 23:34:57 -0400510 if (test_opt(sb, WARN_ON_ERROR))
511 WARN_ON_ONCE(1);
512
David Howellsbc98a422017-07-17 08:45:34 +0100513 if (sb_rdonly(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700514 return;
515
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -0400516 if (!test_opt(sb, ERRORS_CONT)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700517 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700518
Theodore Ts'o4ab2f152009-06-13 10:09:36 -0400519 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700520 if (journal)
Mingming Caodab291a2006-10-11 01:21:01 -0700521 jbd2_journal_abort(journal, -EIO);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700522 }
Jan Kara1dc10972019-03-14 23:46:05 -0400523 /*
524 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
525 * could panic during 'reboot -f' as the underlying device got already
526 * disabled.
527 */
528 if (test_opt(sb, ERRORS_RO) || system_going_down()) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400529 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dmitry Monakhov4418e1412013-06-12 22:38:04 -0400530 /*
531 * Make sure updated value of ->s_mount_flags will be visible
532 * before ->s_flags update
533 */
534 smp_wmb();
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800535 sb->s_flags |= SB_RDONLY;
Jan Kara1dc10972019-03-14 23:46:05 -0400536 } else if (test_opt(sb, ERRORS_PANIC)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700537 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700538 sb->s_id);
Daeho Jeong4327ba52015-10-18 17:02:56 -0400539 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700540}
541
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400542#define ext4_error_ratelimit(sb) \
543 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
544 "EXT4-fs error")
545
Eric Sandeen12062dd2010-02-15 14:19:27 -0500546void __ext4_error(struct super_block *sb, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400547 unsigned int line, int error, __u64 block,
548 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700549{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500550 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700551 va_list args;
552
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500553 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
554 return;
555
Theodore Ts'occf0f322018-02-18 20:53:23 -0500556 trace_ext4_error(sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400557 if (ext4_error_ratelimit(sb)) {
558 va_start(args, fmt);
559 vaf.fmt = fmt;
560 vaf.va = &args;
561 printk(KERN_CRIT
562 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
563 sb->s_id, function, line, current->comm, &vaf);
564 va_end(args);
565 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400566 save_error_info(sb, error, 0, block, function, line);
Mingming Cao617ba132006-10-11 01:20:53 -0700567 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700568}
569
Joe Perchese7c96e82013-07-01 08:12:37 -0400570void __ext4_error_inode(struct inode *inode, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400571 unsigned int line, ext4_fsblk_t block, int error,
Joe Perchese7c96e82013-07-01 08:12:37 -0400572 const char *fmt, ...)
Frank Mayhar273df552010-03-02 11:46:09 -0500573{
574 va_list args;
Theodore Ts'of7c21172011-01-10 12:10:55 -0500575 struct va_format vaf;
Frank Mayhar273df552010-03-02 11:46:09 -0500576
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500577 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
578 return;
579
Theodore Ts'occf0f322018-02-18 20:53:23 -0500580 trace_ext4_error(inode->i_sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400581 if (ext4_error_ratelimit(inode->i_sb)) {
582 va_start(args, fmt);
583 vaf.fmt = fmt;
584 vaf.va = &args;
585 if (block)
586 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
587 "inode #%lu: block %llu: comm %s: %pV\n",
588 inode->i_sb->s_id, function, line, inode->i_ino,
589 block, current->comm, &vaf);
590 else
591 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
592 "inode #%lu: comm %s: %pV\n",
593 inode->i_sb->s_id, function, line, inode->i_ino,
594 current->comm, &vaf);
595 va_end(args);
596 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400597 save_error_info(inode->i_sb, error, inode->i_ino, block,
598 function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500599 ext4_handle_error(inode->i_sb);
600}
601
Joe Perchese7c96e82013-07-01 08:12:37 -0400602void __ext4_error_file(struct file *file, const char *function,
603 unsigned int line, ext4_fsblk_t block,
604 const char *fmt, ...)
Frank Mayhar273df552010-03-02 11:46:09 -0500605{
606 va_list args;
Theodore Ts'of7c21172011-01-10 12:10:55 -0500607 struct va_format vaf;
Al Viro496ad9a2013-01-23 17:07:38 -0500608 struct inode *inode = file_inode(file);
Frank Mayhar273df552010-03-02 11:46:09 -0500609 char pathname[80], *path;
610
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500611 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
612 return;
613
Theodore Ts'occf0f322018-02-18 20:53:23 -0500614 trace_ext4_error(inode->i_sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400615 if (ext4_error_ratelimit(inode->i_sb)) {
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200616 path = file_path(file, pathname, sizeof(pathname));
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400617 if (IS_ERR(path))
618 path = "(unknown)";
619 va_start(args, fmt);
620 vaf.fmt = fmt;
621 vaf.va = &args;
622 if (block)
623 printk(KERN_CRIT
624 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
625 "block %llu: comm %s: path %s: %pV\n",
626 inode->i_sb->s_id, function, line, inode->i_ino,
627 block, current->comm, path, &vaf);
628 else
629 printk(KERN_CRIT
630 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
631 "comm %s: path %s: %pV\n",
632 inode->i_sb->s_id, function, line, inode->i_ino,
633 current->comm, path, &vaf);
634 va_end(args);
635 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400636 save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
637 function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500638 ext4_handle_error(inode->i_sb);
639}
640
Theodore Ts'o722887d2013-02-08 13:00:31 -0500641const char *ext4_decode_error(struct super_block *sb, int errno,
642 char nbuf[16])
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700643{
644 char *errstr = NULL;
645
646 switch (errno) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400647 case -EFSCORRUPTED:
648 errstr = "Corrupt filesystem";
649 break;
650 case -EFSBADCRC:
651 errstr = "Filesystem failed CRC";
652 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700653 case -EIO:
654 errstr = "IO failure";
655 break;
656 case -ENOMEM:
657 errstr = "Out of memory";
658 break;
659 case -EROFS:
Theodore Ts'o78f1ddb2009-07-27 23:09:47 -0400660 if (!sb || (EXT4_SB(sb)->s_journal &&
661 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700662 errstr = "Journal has aborted";
663 else
664 errstr = "Readonly filesystem";
665 break;
666 default:
667 /* If the caller passed in an extra buffer for unknown
668 * errors, textualise them now. Else we just return
669 * NULL. */
670 if (nbuf) {
671 /* Check for truncated error codes... */
672 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
673 errstr = nbuf;
674 }
675 break;
676 }
677
678 return errstr;
679}
680
Mingming Cao617ba132006-10-11 01:20:53 -0700681/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700682 * automatically and invokes the appropriate error response. */
683
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400684void __ext4_std_error(struct super_block *sb, const char *function,
685 unsigned int line, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700686{
687 char nbuf[16];
688 const char *errstr;
689
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500690 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
691 return;
692
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700693 /* Special case: if the error is EROFS, and we're not already
694 * inside a transaction, then there's really no point in logging
695 * an error. */
David Howellsbc98a422017-07-17 08:45:34 +0100696 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697 return;
698
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400699 if (ext4_error_ratelimit(sb)) {
700 errstr = ext4_decode_error(sb, errno, nbuf);
701 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
702 sb->s_id, function, line, errstr);
703 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700704
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400705 save_error_info(sb, -errno, 0, 0, function, line);
Mingming Cao617ba132006-10-11 01:20:53 -0700706 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700707}
708
709/*
Mingming Cao617ba132006-10-11 01:20:53 -0700710 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700711 * abort function may be used to deal with unrecoverable failures such
712 * as journal IO errors or ENOMEM at a critical moment in log management.
713 *
714 * We unconditionally force the filesystem into an ABORT|READONLY state,
715 * unless the error response on the fs has been set to panic in which
716 * case we take the easy way out and panic immediately.
717 */
718
Theodore Ts'oc67d8592010-06-29 11:07:07 -0400719void __ext4_abort(struct super_block *sb, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400720 unsigned int line, int error, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700721{
Joe Perches651e1c32016-10-12 23:12:53 -0400722 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700723 va_list args;
724
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500725 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
726 return;
727
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400728 save_error_info(sb, error, 0, 0, function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700729 va_start(args, fmt);
Joe Perches651e1c32016-10-12 23:12:53 -0400730 vaf.fmt = fmt;
731 vaf.va = &args;
732 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
733 sb->s_id, function, line, &vaf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700734 va_end(args);
735
David Howellsbc98a422017-07-17 08:45:34 +0100736 if (sb_rdonly(sb) == 0) {
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400737 EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
zhangyi (F)7b97d862020-06-09 15:35:40 +0800738 if (EXT4_SB(sb)->s_journal)
739 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
740
741 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dmitry Monakhov4418e1412013-06-12 22:38:04 -0400742 /*
743 * Make sure updated value of ->s_mount_flags will be visible
744 * before ->s_flags update
745 */
746 smp_wmb();
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800747 sb->s_flags |= SB_RDONLY;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400748 }
zhangyi (F)7b97d862020-06-09 15:35:40 +0800749 if (test_opt(sb, ERRORS_PANIC) && !system_going_down())
Mingming Cao617ba132006-10-11 01:20:53 -0700750 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700751}
752
Joe Perchese7c96e82013-07-01 08:12:37 -0400753void __ext4_msg(struct super_block *sb,
754 const char *prefix, const char *fmt, ...)
Eric Sandeenb31e1552009-06-04 17:36:36 -0400755{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500756 struct va_format vaf;
Eric Sandeenb31e1552009-06-04 17:36:36 -0400757 va_list args;
758
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +0000759 atomic_inc(&EXT4_SB(sb)->s_msg_count);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400760 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
761 return;
762
Eric Sandeenb31e1552009-06-04 17:36:36 -0400763 va_start(args, fmt);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500764 vaf.fmt = fmt;
765 vaf.va = &args;
766 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
Eric Sandeenb31e1552009-06-04 17:36:36 -0400767 va_end(args);
768}
769
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +0000770static int ext4_warning_ratelimit(struct super_block *sb)
771{
772 atomic_inc(&EXT4_SB(sb)->s_warning_count);
773 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
774 "EXT4-fs warning");
775}
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400776
Eric Sandeen12062dd2010-02-15 14:19:27 -0500777void __ext4_warning(struct super_block *sb, const char *function,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400778 unsigned int line, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700779{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500780 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700781 va_list args;
782
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400783 if (!ext4_warning_ratelimit(sb))
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400784 return;
785
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700786 va_start(args, fmt);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500787 vaf.fmt = fmt;
788 vaf.va = &args;
789 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
790 sb->s_id, function, line, &vaf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700791 va_end(args);
792}
793
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400794void __ext4_warning_inode(const struct inode *inode, const char *function,
795 unsigned int line, const char *fmt, ...)
796{
797 struct va_format vaf;
798 va_list args;
799
800 if (!ext4_warning_ratelimit(inode->i_sb))
801 return;
802
803 va_start(args, fmt);
804 vaf.fmt = fmt;
805 vaf.va = &args;
806 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
807 "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
808 function, line, inode->i_ino, current->comm, &vaf);
809 va_end(args);
810}
811
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400812void __ext4_grp_locked_error(const char *function, unsigned int line,
813 struct super_block *sb, ext4_group_t grp,
814 unsigned long ino, ext4_fsblk_t block,
815 const char *fmt, ...)
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500816__releases(bitlock)
817__acquires(bitlock)
818{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500819 struct va_format vaf;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500820 va_list args;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500821
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500822 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
823 return;
824
Theodore Ts'occf0f322018-02-18 20:53:23 -0500825 trace_ext4_error(sb, function, line);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400826 __save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500827
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400828 if (ext4_error_ratelimit(sb)) {
829 va_start(args, fmt);
830 vaf.fmt = fmt;
831 vaf.va = &args;
832 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
833 sb->s_id, function, line, grp);
834 if (ino)
835 printk(KERN_CONT "inode %lu: ", ino);
836 if (block)
837 printk(KERN_CONT "block %llu:",
838 (unsigned long long) block);
839 printk(KERN_CONT "%pV\n", &vaf);
840 va_end(args);
841 }
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500842
Theodore Ts'o327eaf72018-06-12 23:34:57 -0400843 if (test_opt(sb, WARN_ON_ERROR))
844 WARN_ON_ONCE(1);
845
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500846 if (test_opt(sb, ERRORS_CONT)) {
Theodore Ts'oe2d67052009-05-01 00:33:44 -0400847 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500848 return;
849 }
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400850
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500851 ext4_unlock_group(sb, grp);
Zhouyi Zhou06f29cc2018-01-10 00:34:19 -0500852 ext4_commit_super(sb, 1);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500853 ext4_handle_error(sb);
854 /*
855 * We only get here in the ERRORS_RO case; relocking the group
856 * may be dangerous, but nothing bad will happen since the
857 * filesystem will have already been marked read/only and the
858 * journal has been aborted. We return 1 as a hint to callers
859 * who might what to use the return value from
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300860 * ext4_grp_locked_error() to distinguish between the
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500861 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
862 * aggressively from the ext4 function in question, with a
863 * more appropriate error code.
864 */
865 ext4_lock_group(sb, grp);
866 return;
867}
868
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400869void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
870 ext4_group_t group,
871 unsigned int flags)
872{
873 struct ext4_sb_info *sbi = EXT4_SB(sb);
874 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
875 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
Wang Shilong9af0b3d2018-07-29 17:27:45 -0400876 int ret;
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400877
Wang Shilong9af0b3d2018-07-29 17:27:45 -0400878 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
879 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
880 &grp->bb_state);
881 if (!ret)
882 percpu_counter_sub(&sbi->s_freeclusters_counter,
883 grp->bb_free);
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400884 }
885
Wang Shilong9af0b3d2018-07-29 17:27:45 -0400886 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
887 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
888 &grp->bb_state);
889 if (!ret && gdp) {
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400890 int count;
891
892 count = ext4_free_inodes_count(sb, gdp);
893 percpu_counter_sub(&sbi->s_freeinodes_counter,
894 count);
895 }
Wang Shilongdb79e6d2018-05-12 11:39:40 -0400896 }
897}
898
Mingming Cao617ba132006-10-11 01:20:53 -0700899void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700900{
Mingming Cao617ba132006-10-11 01:20:53 -0700901 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700902
Mingming Cao617ba132006-10-11 01:20:53 -0700903 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700904 return;
905
Eric Sandeen12062dd2010-02-15 14:19:27 -0500906 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700907 "updating to rev %d because of new feature flag, "
908 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -0700909 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700910
Mingming Cao617ba132006-10-11 01:20:53 -0700911 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
912 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
913 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700914 /* leave es->s_feature_*compat flags alone */
915 /* es->s_uuid will be set by e2fsck if empty */
916
917 /*
918 * The rest of the superblock fields should be zero, and if not it
919 * means they are likely already in use, so leave them alone. We
920 * can leave it up to e2fsck to clean up any inconsistencies there.
921 */
922}
923
924/*
925 * Open the external journal device
926 */
Eric Sandeenb31e1552009-06-04 17:36:36 -0400927static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700928{
929 struct block_device *bdev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700930
Tejun Heod4d77622010-11-13 11:55:18 +0100931 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700932 if (IS_ERR(bdev))
933 goto fail;
934 return bdev;
935
936fail:
Christoph Hellwigea3edd42020-03-24 08:25:11 +0100937 ext4_msg(sb, KERN_ERR,
938 "failed to open journal device unknown-block(%u,%u) %ld",
939 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700940 return NULL;
941}
942
943/*
944 * Release the journal device
945 */
Al Viro4385bab2013-05-05 22:11:03 -0400946static void ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700947{
Al Viro4385bab2013-05-05 22:11:03 -0400948 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700949}
950
Al Viro4385bab2013-05-05 22:11:03 -0400951static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700952{
953 struct block_device *bdev;
Chunguang Xuee7ed3a2020-09-24 11:03:42 +0800954 bdev = sbi->s_journal_bdev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700955 if (bdev) {
Al Viro4385bab2013-05-05 22:11:03 -0400956 ext4_blkdev_put(bdev);
Chunguang Xuee7ed3a2020-09-24 11:03:42 +0800957 sbi->s_journal_bdev = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700958 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700959}
960
961static inline struct inode *orphan_list_entry(struct list_head *l)
962{
Mingming Cao617ba132006-10-11 01:20:53 -0700963 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700964}
965
Mingming Cao617ba132006-10-11 01:20:53 -0700966static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700967{
968 struct list_head *l;
969
Eric Sandeenb31e1552009-06-04 17:36:36 -0400970 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
971 le32_to_cpu(sbi->s_es->s_last_orphan));
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700972
973 printk(KERN_ERR "sb_info orphan list:\n");
974 list_for_each(l, &sbi->s_orphan) {
975 struct inode *inode = orphan_list_entry(l);
976 printk(KERN_ERR " "
977 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
978 inode->i_sb->s_id, inode->i_ino, inode,
979 inode->i_mode, inode->i_nlink,
980 NEXT_ORPHAN(inode));
981 }
982}
983
Jan Kara957153f2017-04-06 15:40:06 +0200984#ifdef CONFIG_QUOTA
985static int ext4_quota_off(struct super_block *sb, int type);
986
987static inline void ext4_quota_off_umount(struct super_block *sb)
988{
989 int type;
990
Jan Kara964edf62017-05-21 22:31:23 -0400991 /* Use our quota_off function to clear inode flags etc. */
992 for (type = 0; type < EXT4_MAXQUOTAS; type++)
993 ext4_quota_off(sb, type);
Jan Kara957153f2017-04-06 15:40:06 +0200994}
Theodore Ts'o33458ea2018-10-12 09:28:09 -0400995
996/*
997 * This is a helper function which is used in the mount/remount
998 * codepaths (which holds s_umount) to fetch the quota file name.
999 */
1000static inline char *get_qf_name(struct super_block *sb,
1001 struct ext4_sb_info *sbi,
1002 int type)
1003{
1004 return rcu_dereference_protected(sbi->s_qf_names[type],
1005 lockdep_is_held(&sb->s_umount));
1006}
Jan Kara957153f2017-04-06 15:40:06 +02001007#else
1008static inline void ext4_quota_off_umount(struct super_block *sb)
1009{
1010}
1011#endif
1012
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001013static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001014{
Mingming Cao617ba132006-10-11 01:20:53 -07001015 struct ext4_sb_info *sbi = EXT4_SB(sb);
1016 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001017 struct buffer_head **group_desc;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001018 struct flex_groups **flex_groups;
Theodore Ts'o97abd7d2017-02-04 23:38:06 -05001019 int aborted = 0;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -04001020 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001021
Lukas Czerner857ac882010-10-27 21:30:05 -04001022 ext4_unregister_li_request(sb);
Jan Kara957153f2017-04-06 15:40:06 +02001023 ext4_quota_off_umount(sb);
Christoph Hellwige0ccfd92010-05-19 07:16:42 -04001024
Jan Kara2e8fa542013-06-04 14:21:02 -04001025 destroy_workqueue(sbi->rsv_conversion_wq);
Mingming Cao4c0425f2009-09-28 15:48:41 -04001026
Ritesh Harjani5e478682020-03-18 11:43:01 +05301027 /*
1028 * Unregister sysfs before destroying jbd2 journal.
1029 * Since we could still access attr_journal_task attribute via sysfs
1030 * path which could have sbi->s_journal->j_task as NULL
1031 */
1032 ext4_unregister_sysfs(sb);
1033
Frank Mayhar03901312009-01-07 00:06:22 -05001034 if (sbi->s_journal) {
Theodore Ts'o97abd7d2017-02-04 23:38:06 -05001035 aborted = is_journal_aborted(sbi->s_journal);
Frank Mayhar03901312009-01-07 00:06:22 -05001036 err = jbd2_journal_destroy(sbi->s_journal);
1037 sbi->s_journal = NULL;
Theodore Ts'o878520a2019-11-19 21:54:15 -05001038 if ((err < 0) && !aborted) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04001039 ext4_abort(sb, -err, "Couldn't clean up the journal");
Theodore Ts'o878520a2019-11-19 21:54:15 -05001040 }
Frank Mayhar03901312009-01-07 00:06:22 -05001041 }
Josef Bacikd4edac32009-12-08 21:48:58 -05001042
Zheng Liud3922a72013-07-01 08:12:37 -04001043 ext4_es_unregister_shrinker(sbi);
Al Viro9105bb12013-12-08 20:52:31 -05001044 del_timer_sync(&sbi->s_err_report);
Josef Bacikd4edac32009-12-08 21:48:58 -05001045 ext4_release_system_zone(sb);
1046 ext4_mb_release(sb);
1047 ext4_ext_release(sb);
Josef Bacikd4edac32009-12-08 21:48:58 -05001048
David Howellsbc98a422017-07-17 08:45:34 +01001049 if (!sb_rdonly(sb) && !aborted) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001050 ext4_clear_feature_journal_needs_recovery(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001051 es->s_state = cpu_to_le16(sbi->s_mount_state);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001052 }
David Howellsbc98a422017-07-17 08:45:34 +01001053 if (!sb_rdonly(sb))
Artem Bityutskiya8e25a82012-03-21 22:29:15 -04001054 ext4_commit_super(sb, 1);
1055
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001056 rcu_read_lock();
1057 group_desc = rcu_dereference(sbi->s_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001058 for (i = 0; i < sbi->s_gdb_count; i++)
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001059 brelse(group_desc[i]);
1060 kvfree(group_desc);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001061 flex_groups = rcu_dereference(sbi->s_flex_groups);
1062 if (flex_groups) {
1063 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1064 kvfree(flex_groups[i]);
1065 kvfree(flex_groups);
1066 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001067 rcu_read_unlock();
Theodore Ts'o57042652011-09-09 18:56:51 -04001068 percpu_counter_destroy(&sbi->s_freeclusters_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001069 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1070 percpu_counter_destroy(&sbi->s_dirs_counter);
Theodore Ts'o57042652011-09-09 18:56:51 -04001071 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
Eric Biggersbbd55932020-02-19 10:30:46 -08001072 percpu_free_rwsem(&sbi->s_writepages_rwsem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001073#ifdef CONFIG_QUOTA
Jan Karaa2d4a642014-09-11 11:15:15 -04001074 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001075 kfree(get_qf_name(sb, sbi, i));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001076#endif
1077
1078 /* Debugging code just in case the in-memory inode orphan list
1079 * isn't empty. The on-disk one can be non-empty if we've
1080 * detected an error and taken the fs readonly, but the
1081 * in-memory list had better be clean by this point. */
1082 if (!list_empty(&sbi->s_orphan))
1083 dump_orphan_list(sb, sbi);
1084 J_ASSERT(list_empty(&sbi->s_orphan));
1085
Theodore Ts'o89d96a62015-06-20 22:50:33 -04001086 sync_blockdev(sb->s_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001087 invalidate_bdev(sb->s_bdev);
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001088 if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 /*
1090 * Invalidate the journal device's buffers. We don't want them
1091 * floating about in memory - the physical journal device may
1092 * hotswapped, and it breaks the `ro-after' testing code.
1093 */
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001094 sync_blockdev(sbi->s_journal_bdev);
1095 invalidate_bdev(sbi->s_journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -07001096 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097 }
Chengguang Xu50c15df2018-12-04 00:24:42 -05001098
1099 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1100 sbi->s_ea_inode_cache = NULL;
1101
1102 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1103 sbi->s_ea_block_cache = NULL;
1104
Johann Lombardic5e06d12011-05-24 18:31:25 -04001105 if (sbi->s_mmp_tsk)
1106 kthread_stop(sbi->s_mmp_tsk);
Eric Sandeen9060dd22016-11-26 14:24:51 -05001107 brelse(sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001108 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001109 /*
1110 * Now that we are completely done shutting down the
1111 * superblock, we need to actually destroy the kobject.
1112 */
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001113 kobject_put(&sbi->s_kobj);
1114 wait_for_completion(&sbi->s_kobj_unregister);
Darrick J. Wong04419842012-04-29 18:27:10 -04001115 if (sbi->s_chksum_driver)
1116 crypto_free_shash(sbi->s_chksum_driver);
Pekka Enberg705895b2009-02-15 18:07:52 -05001117 kfree(sbi->s_blockgroup_lock);
Dan Williams5e405592017-08-24 16:42:48 -07001118 fs_put_dax(sbi->s_daxdev);
Eric Biggersed318a62020-05-12 16:32:50 -07001119 fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04001120#ifdef CONFIG_UNICODE
1121 utf8_unload(sbi->s_encoding);
1122#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001123 kfree(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124}
1125
Christoph Lametere18b8902006-12-06 20:33:20 -08001126static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001127
1128/*
1129 * Called inside transaction, so use GFP_NOFS
1130 */
Mingming Cao617ba132006-10-11 01:20:53 -07001131static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001132{
Mingming Cao617ba132006-10-11 01:20:53 -07001133 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001134
Christoph Lametere6b4f8d2006-12-06 20:33:14 -08001135 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001136 if (!ei)
1137 return NULL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001138
Jeff Laytonee73f9a2018-01-09 08:21:39 -05001139 inode_set_iversion(&ei->vfs_inode, 1);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04001140 spin_lock_init(&ei->i_raw_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05001141 INIT_LIST_HEAD(&ei->i_prealloc_list);
brookxu27bc4462020-08-17 15:36:15 +08001142 atomic_set(&ei->i_prealloc_active, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001143 spin_lock_init(&ei->i_prealloc_lock);
Zheng Liu9a26b662012-11-08 21:57:30 -05001144 ext4_es_init_tree(&ei->i_es_tree);
1145 rwlock_init(&ei->i_es_lock);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001146 INIT_LIST_HEAD(&ei->i_es_list);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001147 ei->i_es_all_nr = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001148 ei->i_es_shk_nr = 0;
Jan Karadd475922014-11-25 11:51:23 -05001149 ei->i_es_shrink_lblk = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001150 ei->i_reserved_data_blocks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001151 spin_lock_init(&(ei->i_block_reservation_lock));
Eric Whitney1dc0aa42018-10-01 14:17:41 -04001152 ext4_init_pending_tree(&ei->i_pending_tree);
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001153#ifdef CONFIG_QUOTA
1154 ei->i_reserved_quota = 0;
Jan Kara96c7e0d2014-09-29 14:58:25 +02001155 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001156#endif
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05001157 ei->jinode = NULL;
Jan Kara2e8fa542013-06-04 14:21:02 -04001158 INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001159 spin_lock_init(&ei->i_completed_io_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05001160 ei->i_sync_tid = 0;
1161 ei->i_datasync_tid = 0;
Dmitry Monakhove27f41e2012-09-28 23:24:52 -04001162 atomic_set(&ei->i_unwritten, 0);
Jan Kara2e8fa542013-06-04 14:21:02 -04001163 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001164 return &ei->vfs_inode;
1165}
1166
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001167static int ext4_drop_inode(struct inode *inode)
1168{
1169 int drop = generic_drop_inode(inode);
1170
Eric Biggers29b36922019-08-04 19:35:48 -07001171 if (!drop)
1172 drop = fscrypt_drop_inode(inode);
1173
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001174 trace_ext4_drop_inode(inode, drop);
1175 return drop;
1176}
1177
Al Viro94053132019-04-15 19:28:34 -04001178static void ext4_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001179{
Eric Biggers2c58d542019-04-10 13:21:15 -07001180 fscrypt_free_inode(inode);
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001181 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1182}
1183
Mingming Cao617ba132006-10-11 01:20:53 -07001184static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001185{
Vasily Averin9f7dd932007-07-15 23:40:45 -07001186 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001187 ext4_msg(inode->i_sb, KERN_ERR,
1188 "Inode %lu (%p): orphan list check failed!",
1189 inode->i_ino, EXT4_I(inode));
Vasily Averin9f7dd932007-07-15 23:40:45 -07001190 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1191 EXT4_I(inode), sizeof(struct ext4_inode_info),
1192 true);
1193 dump_stack();
1194 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001195}
1196
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001197static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001198{
Mingming Cao617ba132006-10-11 01:20:53 -07001199 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001200
Christoph Lametera35afb82007-05-16 22:10:57 -07001201 INIT_LIST_HEAD(&ei->i_orphan);
Christoph Lametera35afb82007-05-16 22:10:57 -07001202 init_rwsem(&ei->xattr_sem);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001203 init_rwsem(&ei->i_data_sem);
Jan Karaea3d7202015-12-07 14:28:03 -05001204 init_rwsem(&ei->i_mmap_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001205 inode_init_once(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001206}
1207
Fabian Fredericke67bc2b2014-02-17 20:34:53 -05001208static int __init init_inodecache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209{
David Windsorf8dd7c72017-06-10 22:50:36 -04001210 ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1211 sizeof(struct ext4_inode_info), 0,
1212 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1213 SLAB_ACCOUNT),
1214 offsetof(struct ext4_inode_info, i_data),
1215 sizeof_field(struct ext4_inode_info, i_data),
1216 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -07001217 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001218 return -ENOMEM;
1219 return 0;
1220}
1221
1222static void destroy_inodecache(void)
1223{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001224 /*
1225 * Make sure all delayed rcu free inodes are flushed before we
1226 * destroy cache.
1227 */
1228 rcu_barrier();
Mingming Cao617ba132006-10-11 01:20:53 -07001229 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001230}
1231
Al Viro0930fcc2010-06-07 13:16:22 -04001232void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001233{
Al Viro0930fcc2010-06-07 13:16:22 -04001234 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +02001235 clear_inode(inode);
brookxu27bc4462020-08-17 15:36:15 +08001236 ext4_discard_preallocations(inode, 0);
Zheng Liu51865fd2012-11-08 21:57:32 -05001237 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
Jan Karaf4c2d372019-11-08 12:45:11 +01001238 dquot_drop(inode);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05001239 if (EXT4_I(inode)->jinode) {
1240 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1241 EXT4_I(inode)->jinode);
1242 jbd2_free_inode(EXT4_I(inode)->jinode);
1243 EXT4_I(inode)->jinode = NULL;
1244 }
Eric Biggers3d204e22018-01-11 23:30:13 -05001245 fscrypt_put_encryption_info(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001246 fsverity_cleanup_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247}
1248
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001249static struct inode *ext4_nfs_get_inode(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001250 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001251{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001252 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001253
Theodore Ts'o8a363972018-12-19 12:29:13 -05001254 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 * Currently we don't know the generation for parent directory, so
1256 * a generation of 0 means "accept any"
1257 */
Theodore Ts'o8a363972018-12-19 12:29:13 -05001258 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
David Howells1d1fe1e2008-02-07 00:15:37 -08001259 if (IS_ERR(inode))
1260 return ERR_CAST(inode);
1261 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001262 iput(inode);
1263 return ERR_PTR(-ESTALE);
1264 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001265
1266 return inode;
1267}
1268
1269static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001270 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001271{
1272 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1273 ext4_nfs_get_inode);
1274}
1275
1276static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001277 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001278{
1279 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1280 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281}
1282
Theodore Ts'ofde87262018-12-19 14:07:58 -05001283static int ext4_nfs_commit_metadata(struct inode *inode)
1284{
1285 struct writeback_control wbc = {
1286 .sync_mode = WB_SYNC_ALL
1287 };
1288
1289 trace_ext4_nfs_commit_metadata(inode);
1290 return ext4_write_inode(inode, &wbc);
1291}
1292
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001293/*
1294 * Try to release metadata pages (indirect blocks, directories) which are
1295 * mapped via the block device. Since these pages could have journal heads
1296 * which would prevent try_to_free_buffers() from freeing them, we must use
1297 * jbd2 layer's try_to_free_buffers() function to release them.
1298 */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001299static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1300 gfp_t wait)
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001301{
1302 journal_t *journal = EXT4_SB(sb)->s_journal;
1303
1304 WARN_ON(PageChecked(page));
1305 if (!page_has_buffers(page))
1306 return 0;
1307 if (journal)
zhangyi (F)529a7812020-06-20 10:54:27 +08001308 return jbd2_journal_try_to_free_buffers(journal, page);
1309
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001310 return try_to_free_buffers(page);
1311}
1312
Chandan Rajendra643fa962018-12-12 15:20:12 +05301313#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001314static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1315{
1316 return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1317 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1318}
1319
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001320static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1321 void *fs_data)
1322{
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001323 handle_t *handle = fs_data;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04001324 int res, res2, credits, retries = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001325
Eric Biggers9ce01512017-06-23 00:10:36 -04001326 /*
1327 * Encrypting the root directory is not allowed because e2fsck expects
1328 * lost+found to exist and be unencrypted, and encrypting the root
1329 * directory would imply encrypting the lost+found directory as well as
1330 * the filename "lost+found" itself.
1331 */
1332 if (inode->i_ino == EXT4_ROOT_INO)
1333 return -EPERM;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001334
Ross Zwisler7d3e06a2017-10-12 11:58:05 -04001335 if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
1336 return -EINVAL;
1337
Ira Weinyb383a732020-05-28 08:00:02 -07001338 if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1339 return -EOPNOTSUPP;
1340
Eric Biggers94840e32017-02-22 13:25:14 -08001341 res = ext4_convert_inline_data(inode);
1342 if (res)
1343 return res;
1344
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001345 /*
1346 * If a journal handle was specified, then the encryption context is
1347 * being set on a new inode via inheritance and is part of a larger
1348 * transaction to create the inode. Otherwise the encryption context is
1349 * being set on an existing inode in its own transaction. Only in the
1350 * latter case should the "retry on ENOSPC" logic be used.
1351 */
1352
1353 if (handle) {
1354 res = ext4_xattr_set_handle(handle, inode,
1355 EXT4_XATTR_INDEX_ENCRYPTION,
1356 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1357 ctx, len, 0);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001358 if (!res) {
1359 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1360 ext4_clear_inode_state(inode,
1361 EXT4_STATE_MAY_INLINE_DATA);
Jan Karaa3caa242016-11-20 17:32:59 -05001362 /*
Eric Biggers2ee6a572017-10-09 12:15:35 -07001363 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1364 * S_DAX may be disabled
Jan Karaa3caa242016-11-20 17:32:59 -05001365 */
Ira Weiny043546e2020-05-28 07:59:59 -07001366 ext4_set_inode_flags(inode, false);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001367 }
1368 return res;
1369 }
1370
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001371 res = dquot_initialize(inode);
1372 if (res)
1373 return res;
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001374retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04001375 res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1376 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001377 if (res)
1378 return res;
1379
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04001380 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001381 if (IS_ERR(handle))
1382 return PTR_ERR(handle);
1383
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001384 res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
1385 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1386 ctx, len, 0);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001387 if (!res) {
1388 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
Eric Biggers2ee6a572017-10-09 12:15:35 -07001389 /*
1390 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1391 * S_DAX may be disabled
1392 */
Ira Weiny043546e2020-05-28 07:59:59 -07001393 ext4_set_inode_flags(inode, false);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001394 res = ext4_mark_inode_dirty(handle, inode);
1395 if (res)
1396 EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1397 }
1398 res2 = ext4_journal_stop(handle);
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001399
1400 if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1401 goto retry;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001402 if (!res)
1403 res = res2;
1404 return res;
1405}
1406
Eric Biggersed318a62020-05-12 16:32:50 -07001407static const union fscrypt_context *
1408ext4_get_dummy_context(struct super_block *sb)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001409{
Eric Biggersed318a62020-05-12 16:32:50 -07001410 return EXT4_SB(sb)->s_dummy_enc_ctx.ctx;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001411}
1412
Eric Biggersb925acb2019-10-24 14:54:37 -07001413static bool ext4_has_stable_inodes(struct super_block *sb)
1414{
1415 return ext4_has_feature_stable_inodes(sb);
1416}
1417
1418static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1419 int *ino_bits_ret, int *lblk_bits_ret)
1420{
1421 *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1422 *lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1423}
1424
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001425static const struct fscrypt_operations ext4_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001426 .key_prefix = "ext4:",
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001427 .get_context = ext4_get_context,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001428 .set_context = ext4_set_context,
Eric Biggersed318a62020-05-12 16:32:50 -07001429 .get_dummy_context = ext4_get_dummy_context,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001430 .empty_dir = ext4_empty_dir,
Eric Biggerse12ee682018-04-30 15:51:44 -07001431 .max_namelen = EXT4_NAME_LEN,
Eric Biggersb925acb2019-10-24 14:54:37 -07001432 .has_stable_inodes = ext4_has_stable_inodes,
1433 .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001434};
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001435#endif
1436
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001437#ifdef CONFIG_QUOTA
Eric Biggersd6006182017-04-29 23:47:50 -04001438static const char * const quotatypes[] = INITQFNAMES;
Li Xi689c9582016-01-08 16:01:22 -05001439#define QTYPE2NAME(t) (quotatypes[t])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001440
Mingming Cao617ba132006-10-11 01:20:53 -07001441static int ext4_write_dquot(struct dquot *dquot);
1442static int ext4_acquire_dquot(struct dquot *dquot);
1443static int ext4_release_dquot(struct dquot *dquot);
1444static int ext4_mark_dquot_dirty(struct dquot *dquot);
1445static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -07001446static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro8c54ca92016-11-20 19:49:34 -05001447 const struct path *path);
Mingming Cao617ba132006-10-11 01:20:53 -07001448static int ext4_quota_on_mount(struct super_block *sb, int type);
1449static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001450 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -07001451static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001452 const char *data, size_t len, loff_t off);
Aditya Kali7c319d32012-07-22 20:21:31 -04001453static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1454 unsigned int flags);
1455static int ext4_enable_quotas(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001456
Jan Kara96c7e0d2014-09-29 14:58:25 +02001457static struct dquot **ext4_get_dquots(struct inode *inode)
1458{
1459 return EXT4_I(inode)->i_dquot;
1460}
1461
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001462static const struct dquot_operations ext4_quota_operations = {
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001463 .get_reserved_space = ext4_get_reserved_space,
1464 .write_dquot = ext4_write_dquot,
1465 .acquire_dquot = ext4_acquire_dquot,
1466 .release_dquot = ext4_release_dquot,
1467 .mark_dirty = ext4_mark_dquot_dirty,
1468 .write_info = ext4_write_info,
1469 .alloc_dquot = dquot_alloc,
1470 .destroy_dquot = dquot_destroy,
1471 .get_projid = ext4_get_projid,
1472 .get_inode_usage = ext4_get_inode_usage,
Chengguang Xuebc11f72019-10-06 18:30:28 +08001473 .get_next_id = dquot_get_next_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001474};
1475
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001476static const struct quotactl_ops ext4_qctl_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07001477 .quota_on = ext4_quota_on,
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04001478 .quota_off = ext4_quota_off,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001479 .quota_sync = dquot_quota_sync,
Jan Kara0a2403392014-11-19 00:42:09 +01001480 .get_state = dquot_get_state,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001481 .set_info = dquot_set_dqinfo,
1482 .get_dqblk = dquot_get_dqblk,
Eric Sandeen6332b9b2016-02-19 19:19:01 +01001483 .set_dqblk = dquot_set_dqblk,
1484 .get_nextdqblk = dquot_get_next_dqblk,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001485};
1486#endif
1487
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001488static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -07001489 .alloc_inode = ext4_alloc_inode,
Al Viro94053132019-04-15 19:28:34 -04001490 .free_inode = ext4_free_in_core_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001491 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001492 .write_inode = ext4_write_inode,
1493 .dirty_inode = ext4_dirty_inode,
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001494 .drop_inode = ext4_drop_inode,
Al Viro0930fcc2010-06-07 13:16:22 -04001495 .evict_inode = ext4_evict_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001496 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001497 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001498 .freeze_fs = ext4_freeze,
1499 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001500 .statfs = ext4_statfs,
1501 .remount_fs = ext4_remount,
Mingming Cao617ba132006-10-11 01:20:53 -07001502 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001503#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001504 .quota_read = ext4_quota_read,
1505 .quota_write = ext4_quota_write,
Jan Kara96c7e0d2014-09-29 14:58:25 +02001506 .get_dquots = ext4_get_dquots,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001507#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001508 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001509};
1510
Christoph Hellwig39655162007-10-21 16:42:17 -07001511static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001512 .fh_to_dentry = ext4_fh_to_dentry,
1513 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001514 .get_parent = ext4_get_parent,
Theodore Ts'ofde87262018-12-19 14:07:58 -05001515 .commit_metadata = ext4_nfs_commit_metadata,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001516};
1517
1518enum {
1519 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1520 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o72578c32012-03-03 18:04:40 -05001521 Opt_nouid32, Opt_debug, Opt_removed,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001522 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o72578c32012-03-03 18:04:40 -05001523 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
Eric Sandeenad4eec62013-08-28 19:05:07 -04001524 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1525 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001526 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04001527 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
Eric Biggers4f74d152020-07-02 01:56:07 +00001528 Opt_inlinecrypt,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001529 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara5a20bdf2009-11-30 23:58:32 +01001530 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
Theodore Ts'oee4a3fc2012-03-02 12:14:24 -05001531 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
Ira Weiny9cb20f92020-05-28 08:00:00 -07001532 Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
1533 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001534 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1535 Opt_nowarn_on_error, Opt_mblk_io_submit,
Theodore Ts'o670e9872017-01-11 15:32:22 -05001536 Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
Theodore Ts'o14490322010-12-14 15:27:50 -05001537 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
Eric Sandeen5328e632009-11-19 14:25:42 -05001538 Opt_inode_readahead_blks, Opt_journal_ioprio,
Jiaying Zhang744692d2010-03-04 16:14:02 -05001539 Opt_dioread_nolock, Opt_dioread_lock,
Theodore Ts'ofc6cb1c2011-12-12 22:06:18 -05001540 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001541 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
Theodore Ts'o3d392b22020-07-17 00:14:40 -04001542 Opt_prefetch_block_bitmaps,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001543};
1544
Steven Whitehousea447c092008-10-13 10:46:57 +01001545static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001546 {Opt_bsd_df, "bsddf"},
1547 {Opt_minix_df, "minixdf"},
1548 {Opt_grpid, "grpid"},
1549 {Opt_grpid, "bsdgroups"},
1550 {Opt_nogrpid, "nogrpid"},
1551 {Opt_nogrpid, "sysvgroups"},
1552 {Opt_resgid, "resgid=%u"},
1553 {Opt_resuid, "resuid=%u"},
1554 {Opt_sb, "sb=%u"},
1555 {Opt_err_cont, "errors=continue"},
1556 {Opt_err_panic, "errors=panic"},
1557 {Opt_err_ro, "errors=remount-ro"},
1558 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001559 {Opt_debug, "debug"},
Theodore Ts'o72578c32012-03-03 18:04:40 -05001560 {Opt_removed, "oldalloc"},
1561 {Opt_removed, "orlov"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001562 {Opt_user_xattr, "user_xattr"},
1563 {Opt_nouser_xattr, "nouser_xattr"},
1564 {Opt_acl, "acl"},
1565 {Opt_noacl, "noacl"},
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001566 {Opt_noload, "norecovery"},
Theodore Ts'o5a916be2012-03-04 19:27:31 -05001567 {Opt_noload, "noload"},
Theodore Ts'o72578c32012-03-03 18:04:40 -05001568 {Opt_removed, "nobh"},
1569 {Opt_removed, "bh"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001570 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001571 {Opt_min_batch_time, "min_batch_time=%u"},
1572 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001573 {Opt_journal_dev, "journal_dev=%u"},
Eric Sandeenad4eec62013-08-28 19:05:07 -04001574 {Opt_journal_path, "journal_path=%s"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001575 {Opt_journal_checksum, "journal_checksum"},
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05001576 {Opt_nojournal_checksum, "nojournal_checksum"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001577 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001578 {Opt_abort, "abort"},
1579 {Opt_data_journal, "data=journal"},
1580 {Opt_data_ordered, "data=ordered"},
1581 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001582 {Opt_data_err_abort, "data_err=abort"},
1583 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001584 {Opt_offusrjquota, "usrjquota="},
1585 {Opt_usrjquota, "usrjquota=%s"},
1586 {Opt_offgrpjquota, "grpjquota="},
1587 {Opt_grpjquota, "grpjquota=%s"},
1588 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1589 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara5a20bdf2009-11-30 23:58:32 +01001590 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001591 {Opt_grpquota, "grpquota"},
1592 {Opt_noquota, "noquota"},
1593 {Opt_quota, "quota"},
1594 {Opt_usrquota, "usrquota"},
Jan Kara49da9392016-09-05 23:08:16 -04001595 {Opt_prjquota, "prjquota"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001596 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001597 {Opt_barrier, "barrier"},
1598 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001599 {Opt_i_version, "i_version"},
Ross Zwisler923ae0f2015-02-16 15:59:38 -08001600 {Opt_dax, "dax"},
Ira Weiny9cb20f92020-05-28 08:00:00 -07001601 {Opt_dax_always, "dax=always"},
1602 {Opt_dax_inode, "dax=inode"},
1603 {Opt_dax_never, "dax=never"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001604 {Opt_stripe, "stripe=%u"},
Alex Tomas64769242008-07-11 19:27:31 -04001605 {Opt_delalloc, "delalloc"},
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001606 {Opt_warn_on_error, "warn_on_error"},
1607 {Opt_nowarn_on_error, "nowarn_on_error"},
Theodore Ts'oa26f4992015-02-02 00:37:02 -05001608 {Opt_lazytime, "lazytime"},
1609 {Opt_nolazytime, "nolazytime"},
Theodore Ts'o670e9872017-01-11 15:32:22 -05001610 {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001611 {Opt_nodelalloc, "nodelalloc"},
Jan Kara36ade452013-01-28 09:30:52 -05001612 {Opt_removed, "mblk_io_submit"},
1613 {Opt_removed, "nomblk_io_submit"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001614 {Opt_block_validity, "block_validity"},
1615 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001616 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001617 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001618 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001619 {Opt_auto_da_alloc, "auto_da_alloc"},
1620 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001621 {Opt_dioread_nolock, "dioread_nolock"},
Theodore Ts'o244adf62020-01-23 12:23:17 -05001622 {Opt_dioread_lock, "nodioread_nolock"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001623 {Opt_dioread_lock, "dioread_lock"},
Eric Sandeen5328e632009-11-19 14:25:42 -05001624 {Opt_discard, "discard"},
1625 {Opt_nodiscard, "nodiscard"},
Theodore Ts'ofc6cb1c2011-12-12 22:06:18 -05001626 {Opt_init_itable, "init_itable=%u"},
1627 {Opt_init_itable, "init_itable"},
1628 {Opt_noinit_itable, "noinit_itable"},
Theodore Ts'odf981d02012-08-17 09:48:17 -04001629 {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
Eric Biggersed318a62020-05-12 16:32:50 -07001630 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04001631 {Opt_test_dummy_encryption, "test_dummy_encryption"},
Eric Biggers4f74d152020-07-02 01:56:07 +00001632 {Opt_inlinecrypt, "inlinecrypt"},
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001633 {Opt_nombcache, "nombcache"},
1634 {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */
Theodore Ts'o3d392b22020-07-17 00:14:40 -04001635 {Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"},
Theodore Ts'oc7198b92012-03-04 22:00:53 -05001636 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
1637 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
1638 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
1639 {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1640 {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001641 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001642};
1643
Mingming Cao617ba132006-10-11 01:20:53 -07001644static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001645{
Mingming Cao617ba132006-10-11 01:20:53 -07001646 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001647 char *options = (char *) *data;
1648
1649 if (!options || strncmp(options, "sb=", 3) != 0)
1650 return 1; /* Default location */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001651
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001652 options += 3;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001653 /* TODO: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001654 sb_block = simple_strtoul(options, &options, 0);
1655 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001656 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001657 (char *) *data);
1658 return 1;
1659 }
1660 if (*options == ',')
1661 options++;
1662 *data = (void *) options;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001663
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001664 return sb_block;
1665}
1666
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001667#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
Eric Biggersd6006182017-04-29 23:47:50 -04001668static const char deprecated_msg[] =
1669 "Mount option \"%s\" will be removed by %s\n"
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001670 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001671
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001672#ifdef CONFIG_QUOTA
1673static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1674{
1675 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001676 char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
Chen Gang03dafb52013-01-24 23:24:58 -05001677 int ret = -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001678
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001679 if (sb_any_quota_loaded(sb) && !old_qname) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001680 ext4_msg(sb, KERN_ERR,
1681 "Cannot change journaled "
1682 "quota options when quota turned on");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001683 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001684 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001685 if (ext4_has_feature_quota(sb)) {
Theodore Ts'oc325a672016-04-03 17:03:37 -04001686 ext4_msg(sb, KERN_INFO, "Journaled quota options "
1687 "ignored when QUOTA feature is enabled");
1688 return 1;
Jan Kara262b4662013-03-02 17:57:08 -05001689 }
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001690 qname = match_strdup(args);
1691 if (!qname) {
1692 ext4_msg(sb, KERN_ERR,
1693 "Not enough memory for storing quotafile name");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001694 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001695 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001696 if (old_qname) {
1697 if (strcmp(old_qname, qname) == 0)
Chen Gang03dafb52013-01-24 23:24:58 -05001698 ret = 1;
1699 else
1700 ext4_msg(sb, KERN_ERR,
1701 "%s quota file already specified",
1702 QTYPE2NAME(qtype));
1703 goto errout;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001704 }
Chen Gang03dafb52013-01-24 23:24:58 -05001705 if (strchr(qname, '/')) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001706 ext4_msg(sb, KERN_ERR,
1707 "quotafile must be on filesystem root");
Chen Gang03dafb52013-01-24 23:24:58 -05001708 goto errout;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001709 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001710 rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05001711 set_opt(sb, QUOTA);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001712 return 1;
Chen Gang03dafb52013-01-24 23:24:58 -05001713errout:
1714 kfree(qname);
1715 return ret;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001716}
1717
1718static int clear_qf_name(struct super_block *sb, int qtype)
1719{
1720
1721 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001722 char *old_qname = get_qf_name(sb, sbi, qtype);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001723
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001724 if (sb_any_quota_loaded(sb) && old_qname) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001725 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1726 " when quota turned on");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001727 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001728 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001729 rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
1730 synchronize_rcu();
1731 kfree(old_qname);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001732 return 1;
1733}
1734#endif
1735
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001736#define MOPT_SET 0x0001
1737#define MOPT_CLEAR 0x0002
1738#define MOPT_NOSUPPORT 0x0004
1739#define MOPT_EXPLICIT 0x0008
1740#define MOPT_CLEAR_ERR 0x0010
1741#define MOPT_GTE0 0x0020
1742#ifdef CONFIG_QUOTA
1743#define MOPT_Q 0
1744#define MOPT_QFMT 0x0040
1745#else
1746#define MOPT_Q MOPT_NOSUPPORT
1747#define MOPT_QFMT MOPT_NOSUPPORT
1748#endif
1749#define MOPT_DATAJ 0x0080
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001750#define MOPT_NO_EXT2 0x0100
1751#define MOPT_NO_EXT3 0x0200
1752#define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
Eric Sandeenad4eec62013-08-28 19:05:07 -04001753#define MOPT_STRING 0x0400
Ira Weiny9cb20f92020-05-28 08:00:00 -07001754#define MOPT_SKIP 0x0800
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001755
1756static const struct mount_opts {
1757 int token;
1758 int mount_opt;
1759 int flags;
1760} ext4_mount_opts[] = {
1761 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1762 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1763 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1764 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001765 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1766 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001767 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1768 MOPT_EXT4_ONLY | MOPT_SET},
1769 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1770 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001771 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1772 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001773 {Opt_delalloc, EXT4_MOUNT_DELALLOC,
1774 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1775 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
Theodore Ts'o59d9fa52013-08-08 23:01:24 -04001776 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001777 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1778 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05001779 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1780 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001781 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04001782 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001783 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001784 EXT4_MOUNT_JOURNAL_CHECKSUM),
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04001785 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001786 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001787 {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1788 {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1789 {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001790 {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
Ales Novak7915a862016-03-12 21:55:50 -05001791 MOPT_NO_EXT2},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001792 {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
Ales Novak7915a862016-03-12 21:55:50 -05001793 MOPT_NO_EXT2},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001794 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1795 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1796 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1797 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1798 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1799 {Opt_commit, 0, MOPT_GTE0},
1800 {Opt_max_batch_time, 0, MOPT_GTE0},
1801 {Opt_min_batch_time, 0, MOPT_GTE0},
1802 {Opt_inode_readahead_blks, 0, MOPT_GTE0},
1803 {Opt_init_itable, 0, MOPT_GTE0},
Ira Weiny9cb20f92020-05-28 08:00:00 -07001804 {Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
1805 {Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
1806 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1807 {Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
1808 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1809 {Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
1810 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001811 {Opt_stripe, 0, MOPT_GTE0},
Jan Kara0efb3b22013-02-02 22:52:19 -05001812 {Opt_resuid, 0, MOPT_GTE0},
1813 {Opt_resgid, 0, MOPT_GTE0},
Carlos Maiolino5ba92bc2015-07-21 23:57:59 -04001814 {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1815 {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
1816 {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001817 {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1818 {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1819 {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
1820 MOPT_NO_EXT2 | MOPT_DATAJ},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001821 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1822 {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001823#ifdef CONFIG_EXT4_FS_POSIX_ACL
1824 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1825 {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
1826#else
1827 {Opt_acl, 0, MOPT_NOSUPPORT},
1828 {Opt_noacl, 0, MOPT_NOSUPPORT},
1829#endif
1830 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1831 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
Theodore Ts'o670e9872017-01-11 15:32:22 -05001832 {Opt_debug_want_extra_isize, 0, MOPT_GTE0},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001833 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1834 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1835 MOPT_SET | MOPT_Q},
1836 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1837 MOPT_SET | MOPT_Q},
Jan Kara49da9392016-09-05 23:08:16 -04001838 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
1839 MOPT_SET | MOPT_Q},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001840 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
Jan Kara49da9392016-09-05 23:08:16 -04001841 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
1842 MOPT_CLEAR | MOPT_Q},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001843 {Opt_usrjquota, 0, MOPT_Q},
1844 {Opt_grpjquota, 0, MOPT_Q},
1845 {Opt_offusrjquota, 0, MOPT_Q},
1846 {Opt_offgrpjquota, 0, MOPT_Q},
1847 {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
1848 {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
1849 {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
Theodore Ts'odf981d02012-08-17 09:48:17 -04001850 {Opt_max_dir_size_kb, 0, MOPT_GTE0},
Eric Biggersed318a62020-05-12 16:32:50 -07001851 {Opt_test_dummy_encryption, 0, MOPT_STRING},
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001852 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
Theodore Ts'o3d392b22020-07-17 00:14:40 -04001853 {Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS,
1854 MOPT_SET},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001855 {Opt_err, 0, 0}
1856};
1857
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04001858#ifdef CONFIG_UNICODE
1859static const struct ext4_sb_encodings {
1860 __u16 magic;
1861 char *name;
1862 char *version;
1863} ext4_sb_encoding_map[] = {
1864 {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
1865};
1866
1867static int ext4_sb_read_encoding(const struct ext4_super_block *es,
1868 const struct ext4_sb_encodings **encoding,
1869 __u16 *flags)
1870{
1871 __u16 magic = le16_to_cpu(es->s_encoding);
1872 int i;
1873
1874 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
1875 if (magic == ext4_sb_encoding_map[i].magic)
1876 break;
1877
1878 if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
1879 return -EINVAL;
1880
1881 *encoding = &ext4_sb_encoding_map[i];
1882 *flags = le16_to_cpu(es->s_encoding_flags);
1883
1884 return 0;
1885}
1886#endif
1887
Eric Biggersed318a62020-05-12 16:32:50 -07001888static int ext4_set_test_dummy_encryption(struct super_block *sb,
1889 const char *opt,
1890 const substring_t *arg,
1891 bool is_remount)
1892{
1893#ifdef CONFIG_FS_ENCRYPTION
1894 struct ext4_sb_info *sbi = EXT4_SB(sb);
1895 int err;
1896
1897 /*
1898 * This mount option is just for testing, and it's not worthwhile to
1899 * implement the extra complexity (e.g. RCU protection) that would be
1900 * needed to allow it to be set or changed during remount. We do allow
1901 * it to be specified during remount, but only if there is no change.
1902 */
1903 if (is_remount && !sbi->s_dummy_enc_ctx.ctx) {
1904 ext4_msg(sb, KERN_WARNING,
1905 "Can't set test_dummy_encryption on remount");
1906 return -1;
1907 }
1908 err = fscrypt_set_test_dummy_encryption(sb, arg, &sbi->s_dummy_enc_ctx);
1909 if (err) {
1910 if (err == -EEXIST)
1911 ext4_msg(sb, KERN_WARNING,
1912 "Can't change test_dummy_encryption on remount");
1913 else if (err == -EINVAL)
1914 ext4_msg(sb, KERN_WARNING,
1915 "Value of option \"%s\" is unrecognized", opt);
1916 else
1917 ext4_msg(sb, KERN_WARNING,
1918 "Error processing option \"%s\" [%d]",
1919 opt, err);
1920 return -1;
1921 }
1922 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
1923#else
1924 ext4_msg(sb, KERN_WARNING,
1925 "Test dummy encryption mount option ignored");
1926#endif
1927 return 1;
1928}
1929
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001930static int handle_mount_opt(struct super_block *sb, char *opt, int token,
1931 substring_t *args, unsigned long *journal_devnum,
1932 unsigned int *journal_ioprio, int is_remount)
1933{
1934 struct ext4_sb_info *sbi = EXT4_SB(sb);
1935 const struct mount_opts *m;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08001936 kuid_t uid;
1937 kgid_t gid;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001938 int arg = 0;
1939
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001940#ifdef CONFIG_QUOTA
1941 if (token == Opt_usrjquota)
1942 return set_qf_name(sb, USRQUOTA, &args[0]);
1943 else if (token == Opt_grpjquota)
1944 return set_qf_name(sb, GRPQUOTA, &args[0]);
1945 else if (token == Opt_offusrjquota)
1946 return clear_qf_name(sb, USRQUOTA);
1947 else if (token == Opt_offgrpjquota)
1948 return clear_qf_name(sb, GRPQUOTA);
1949#endif
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001950 switch (token) {
Theodore Ts'of7048602012-03-04 22:06:20 -05001951 case Opt_noacl:
1952 case Opt_nouser_xattr:
1953 ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1954 break;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001955 case Opt_sb:
1956 return 1; /* handled by get_sb_block() */
1957 case Opt_removed:
Jan Kara5f3633e2013-02-02 23:09:36 -05001958 ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001959 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001960 case Opt_abort:
1961 sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1962 return 1;
1963 case Opt_i_version:
Matthew Garrett357fdad2017-10-18 13:56:26 -07001964 sb->s_flags |= SB_I_VERSION;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001965 return 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05001966 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001967 sb->s_flags |= SB_LAZYTIME;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05001968 return 1;
1969 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -08001970 sb->s_flags &= ~SB_LAZYTIME;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05001971 return 1;
Eric Biggers4f74d152020-07-02 01:56:07 +00001972 case Opt_inlinecrypt:
1973#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
1974 sb->s_flags |= SB_INLINECRYPT;
1975#else
1976 ext4_msg(sb, KERN_ERR, "inline encryption not supported");
1977#endif
1978 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001979 }
1980
Jan Kara5f3633e2013-02-02 23:09:36 -05001981 for (m = ext4_mount_opts; m->token != Opt_err; m++)
1982 if (token == m->token)
1983 break;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001984
Jan Kara5f3633e2013-02-02 23:09:36 -05001985 if (m->token == Opt_err) {
1986 ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
1987 "or missing value", opt);
1988 return -1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001989 }
Jan Kara5f3633e2013-02-02 23:09:36 -05001990
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001991 if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
1992 ext4_msg(sb, KERN_ERR,
1993 "Mount option \"%s\" incompatible with ext2", opt);
1994 return -1;
1995 }
1996 if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
1997 ext4_msg(sb, KERN_ERR,
1998 "Mount option \"%s\" incompatible with ext3", opt);
1999 return -1;
2000 }
2001
Eric Sandeenad4eec62013-08-28 19:05:07 -04002002 if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
Jan Kara5f3633e2013-02-02 23:09:36 -05002003 return -1;
2004 if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
2005 return -1;
Dmitry Monakhovc93cf2d2015-10-18 23:35:32 -04002006 if (m->flags & MOPT_EXPLICIT) {
2007 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2008 set_opt2(sb, EXPLICIT_DELALLOC);
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04002009 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2010 set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
Dmitry Monakhovc93cf2d2015-10-18 23:35:32 -04002011 } else
2012 return -1;
2013 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002014 if (m->flags & MOPT_CLEAR_ERR)
2015 clear_opt(sb, ERRORS_MASK);
2016 if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
2017 ext4_msg(sb, KERN_ERR, "Cannot change quota "
2018 "options when quota turned on");
2019 return -1;
2020 }
2021
2022 if (m->flags & MOPT_NOSUPPORT) {
2023 ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
2024 } else if (token == Opt_commit) {
2025 if (arg == 0)
2026 arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
zhangyi (F)9ba55542019-08-28 11:25:01 -04002027 else if (arg > INT_MAX / HZ) {
2028 ext4_msg(sb, KERN_ERR,
2029 "Invalid commit interval %d, "
2030 "must be smaller than %d",
2031 arg, INT_MAX / HZ);
2032 return -1;
2033 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002034 sbi->s_commit_interval = HZ * arg;
Theodore Ts'o670e9872017-01-11 15:32:22 -05002035 } else if (token == Opt_debug_want_extra_isize) {
Theodore Ts'o98033872019-12-15 01:09:03 -05002036 if ((arg & 1) ||
2037 (arg < 4) ||
2038 (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
2039 ext4_msg(sb, KERN_ERR,
2040 "Invalid want_extra_isize %d", arg);
2041 return -1;
2042 }
Theodore Ts'o670e9872017-01-11 15:32:22 -05002043 sbi->s_want_extra_isize = arg;
Jan Kara5f3633e2013-02-02 23:09:36 -05002044 } else if (token == Opt_max_batch_time) {
Jan Kara5f3633e2013-02-02 23:09:36 -05002045 sbi->s_max_batch_time = arg;
2046 } else if (token == Opt_min_batch_time) {
2047 sbi->s_min_batch_time = arg;
2048 } else if (token == Opt_inode_readahead_blks) {
Jan Karae33e60e2013-02-02 23:14:31 -05002049 if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2050 ext4_msg(sb, KERN_ERR,
2051 "EXT4-fs: inode_readahead_blks must be "
2052 "0 or a power of 2 smaller than 2^31");
Jan Kara5f3633e2013-02-02 23:09:36 -05002053 return -1;
2054 }
2055 sbi->s_inode_readahead_blks = arg;
2056 } else if (token == Opt_init_itable) {
2057 set_opt(sb, INIT_INODE_TABLE);
2058 if (!args->from)
2059 arg = EXT4_DEF_LI_WAIT_MULT;
2060 sbi->s_li_wait_mult = arg;
2061 } else if (token == Opt_max_dir_size_kb) {
2062 sbi->s_max_dir_size_kb = arg;
2063 } else if (token == Opt_stripe) {
2064 sbi->s_stripe = arg;
2065 } else if (token == Opt_resuid) {
2066 uid = make_kuid(current_user_ns(), arg);
2067 if (!uid_valid(uid)) {
2068 ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
2069 return -1;
2070 }
2071 sbi->s_resuid = uid;
2072 } else if (token == Opt_resgid) {
2073 gid = make_kgid(current_user_ns(), arg);
2074 if (!gid_valid(gid)) {
2075 ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
2076 return -1;
2077 }
2078 sbi->s_resgid = gid;
2079 } else if (token == Opt_journal_dev) {
2080 if (is_remount) {
2081 ext4_msg(sb, KERN_ERR,
2082 "Cannot specify journal on remount");
2083 return -1;
2084 }
2085 *journal_devnum = arg;
Eric Sandeenad4eec62013-08-28 19:05:07 -04002086 } else if (token == Opt_journal_path) {
2087 char *journal_path;
2088 struct inode *journal_inode;
2089 struct path path;
2090 int error;
2091
2092 if (is_remount) {
2093 ext4_msg(sb, KERN_ERR,
2094 "Cannot specify journal on remount");
2095 return -1;
2096 }
2097 journal_path = match_strdup(&args[0]);
2098 if (!journal_path) {
2099 ext4_msg(sb, KERN_ERR, "error: could not dup "
2100 "journal device string");
2101 return -1;
2102 }
2103
2104 error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2105 if (error) {
2106 ext4_msg(sb, KERN_ERR, "error: could not find "
2107 "journal device path: error %d", error);
2108 kfree(journal_path);
2109 return -1;
2110 }
2111
David Howells2b0143b2015-03-17 22:25:59 +00002112 journal_inode = d_inode(path.dentry);
Eric Sandeenad4eec62013-08-28 19:05:07 -04002113 if (!S_ISBLK(journal_inode->i_mode)) {
2114 ext4_msg(sb, KERN_ERR, "error: journal path %s "
2115 "is not a block device", journal_path);
2116 path_put(&path);
2117 kfree(journal_path);
2118 return -1;
2119 }
2120
2121 *journal_devnum = new_encode_dev(journal_inode->i_rdev);
2122 path_put(&path);
2123 kfree(journal_path);
Jan Kara5f3633e2013-02-02 23:09:36 -05002124 } else if (token == Opt_journal_ioprio) {
2125 if (arg > 7) {
2126 ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
2127 " (must be 0-7)");
2128 return -1;
2129 }
2130 *journal_ioprio =
2131 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002132 } else if (token == Opt_test_dummy_encryption) {
Eric Biggersed318a62020-05-12 16:32:50 -07002133 return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2134 is_remount);
Jan Kara5f3633e2013-02-02 23:09:36 -05002135 } else if (m->flags & MOPT_DATAJ) {
2136 if (is_remount) {
2137 if (!sbi->s_journal)
2138 ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
2139 else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
2140 ext4_msg(sb, KERN_ERR,
2141 "Cannot change data mode on remount");
2142 return -1;
2143 }
2144 } else {
2145 clear_opt(sb, DATA_FLAGS);
2146 sbi->s_mount_opt |= m->mount_opt;
2147 }
2148#ifdef CONFIG_QUOTA
2149 } else if (m->flags & MOPT_QFMT) {
2150 if (sb_any_quota_loaded(sb) &&
2151 sbi->s_jquota_fmt != m->mount_opt) {
2152 ext4_msg(sb, KERN_ERR, "Cannot change journaled "
2153 "quota options when quota turned on");
2154 return -1;
2155 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002156 if (ext4_has_feature_quota(sb)) {
Theodore Ts'oc325a672016-04-03 17:03:37 -04002157 ext4_msg(sb, KERN_INFO,
2158 "Quota format mount options ignored "
Jan Kara262b4662013-03-02 17:57:08 -05002159 "when QUOTA feature is enabled");
Theodore Ts'oc325a672016-04-03 17:03:37 -04002160 return 1;
Jan Kara262b4662013-03-02 17:57:08 -05002161 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002162 sbi->s_jquota_fmt = m->mount_opt;
2163#endif
Ira Weiny9cb20f92020-05-28 08:00:00 -07002164 } else if (token == Opt_dax || token == Opt_dax_always ||
2165 token == Opt_dax_inode || token == Opt_dax_never) {
Dan Williamsef83b6e2015-09-29 15:48:11 -04002166#ifdef CONFIG_FS_DAX
Ira Weiny9cb20f92020-05-28 08:00:00 -07002167 switch (token) {
2168 case Opt_dax:
2169 case Opt_dax_always:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002170 if (is_remount &&
2171 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2172 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2173 fail_dax_change_remount:
2174 ext4_msg(sb, KERN_ERR, "can't change "
2175 "dax mount option while remounting");
2176 return -1;
2177 }
2178 if (is_remount &&
2179 (test_opt(sb, DATA_FLAGS) ==
2180 EXT4_MOUNT_JOURNAL_DATA)) {
2181 ext4_msg(sb, KERN_ERR, "can't mount with "
2182 "both data=journal and dax");
2183 return -1;
2184 }
Ira Weiny9cb20f92020-05-28 08:00:00 -07002185 ext4_msg(sb, KERN_WARNING,
2186 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2187 sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
2188 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2189 break;
2190 case Opt_dax_never:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002191 if (is_remount &&
2192 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2193 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2194 goto fail_dax_change_remount;
Ira Weiny9cb20f92020-05-28 08:00:00 -07002195 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2196 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2197 break;
2198 case Opt_dax_inode:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002199 if (is_remount &&
2200 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2201 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2202 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2203 goto fail_dax_change_remount;
Ira Weiny9cb20f92020-05-28 08:00:00 -07002204 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2205 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2206 /* Strictly for printing options */
2207 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
2208 break;
2209 }
Dan Williamsef83b6e2015-09-29 15:48:11 -04002210#else
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002211 ext4_msg(sb, KERN_INFO, "dax option not supported");
Ira Weiny9cb20f92020-05-28 08:00:00 -07002212 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2213 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002214 return -1;
2215#endif
Ales Novak7915a862016-03-12 21:55:50 -05002216 } else if (token == Opt_data_err_abort) {
2217 sbi->s_mount_opt |= m->mount_opt;
2218 } else if (token == Opt_data_err_ignore) {
2219 sbi->s_mount_opt &= ~m->mount_opt;
Jan Kara5f3633e2013-02-02 23:09:36 -05002220 } else {
2221 if (!args->from)
2222 arg = 1;
2223 if (m->flags & MOPT_CLEAR)
2224 arg = !arg;
2225 else if (unlikely(!(m->flags & MOPT_SET))) {
2226 ext4_msg(sb, KERN_WARNING,
2227 "buggy handling of option %s", opt);
2228 WARN_ON(1);
2229 return -1;
2230 }
2231 if (arg != 0)
2232 sbi->s_mount_opt |= m->mount_opt;
2233 else
2234 sbi->s_mount_opt &= ~m->mount_opt;
2235 }
2236 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002237}
2238
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002239static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05002240 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002241 unsigned int *journal_ioprio,
Eric Sandeen661aa522012-02-20 17:53:04 -05002242 int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002243{
Olof Johansson1e1a76e2019-11-10 18:25:23 -08002244 struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002245 char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002246 substring_t args[MAX_OPT_ARGS];
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002247 int token;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002248
2249 if (!options)
2250 return 1;
2251
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002252 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002253 if (!*p)
2254 continue;
Eric Sandeen15121c12010-02-15 20:17:55 -05002255 /*
2256 * Initialize args struct so we know whether arg was
2257 * found; some options take optional arguments.
2258 */
Sachin Kamatcaecd0a2012-08-18 22:29:18 -04002259 args[0].to = args[0].from = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002260 token = match_token(p, tokens, args);
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002261 if (handle_mount_opt(sb, p, token, args, journal_devnum,
2262 journal_ioprio, is_remount) < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002263 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002264 }
2265#ifdef CONFIG_QUOTA
Jan Kara49da9392016-09-05 23:08:16 -04002266 /*
2267 * We do the test below only for project quotas. 'usrquota' and
2268 * 'grpquota' mount options are allowed even without quota feature
2269 * to support legacy quotas in quota files.
2270 */
2271 if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
2272 ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
2273 "Cannot enable project quota enforcement.");
2274 return 0;
2275 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002276 usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
2277 grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
2278 if (usr_qf_name || grp_qf_name) {
2279 if (test_opt(sb, USRQUOTA) && usr_qf_name)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05002280 clear_opt(sb, USRQUOTA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002281
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002282 if (test_opt(sb, GRPQUOTA) && grp_qf_name)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05002283 clear_opt(sb, GRPQUOTA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002284
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05002285 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002286 ext4_msg(sb, KERN_ERR, "old and new quota "
2287 "format mixing");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002288 return 0;
2289 }
2290
2291 if (!sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002292 ext4_msg(sb, KERN_ERR, "journaled quota format "
2293 "not specified");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002294 return 0;
2295 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002296 }
2297#endif
Ritesh Harjani626b0352020-03-28 01:37:44 +05302298 if (test_opt(sb, DIOREAD_NOLOCK)) {
2299 int blocksize =
2300 BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2301 if (blocksize < PAGE_SIZE)
2302 ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2303 "experimental mount option 'dioread_nolock' "
2304 "for blocksize < PAGE_SIZE");
2305 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002306 return 1;
2307}
2308
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002309static inline void ext4_show_quota_options(struct seq_file *seq,
2310 struct super_block *sb)
2311{
2312#if defined(CONFIG_QUOTA)
2313 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002314 char *usr_qf_name, *grp_qf_name;
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002315
2316 if (sbi->s_jquota_fmt) {
2317 char *fmtname = "";
2318
2319 switch (sbi->s_jquota_fmt) {
2320 case QFMT_VFS_OLD:
2321 fmtname = "vfsold";
2322 break;
2323 case QFMT_VFS_V0:
2324 fmtname = "vfsv0";
2325 break;
2326 case QFMT_VFS_V1:
2327 fmtname = "vfsv1";
2328 break;
2329 }
2330 seq_printf(seq, ",jqfmt=%s", fmtname);
2331 }
2332
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002333 rcu_read_lock();
2334 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2335 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2336 if (usr_qf_name)
2337 seq_show_option(seq, "usrjquota", usr_qf_name);
2338 if (grp_qf_name)
2339 seq_show_option(seq, "grpjquota", grp_qf_name);
2340 rcu_read_unlock();
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002341#endif
2342}
2343
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002344static const char *token2str(int token)
2345{
Herton Ronaldo Krzesinski50df9fd2012-09-23 22:49:12 -04002346 const struct match_token *t;
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002347
2348 for (t = tokens; t->token != Opt_err; t++)
2349 if (t->token == token && !strchr(t->pattern, '='))
2350 break;
2351 return t->pattern;
2352}
2353
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002354/*
2355 * Show an option if
2356 * - it's set to a non-default value OR
2357 * - if the per-sb default is different from the global default
2358 */
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002359static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2360 int nodefs)
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002361{
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002362 struct ext4_sb_info *sbi = EXT4_SB(sb);
2363 struct ext4_super_block *es = sbi->s_es;
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002364 int def_errors, def_mount_opt = sbi->s_def_mount_opt;
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002365 const struct mount_opts *m;
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002366 char sep = nodefs ? '\n' : ',';
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002367
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002368#define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2369#define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002370
2371 if (sbi->s_sb_block != 1)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002372 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2373
2374 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2375 int want_set = m->flags & MOPT_SET;
2376 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
Ira Weiny9cb20f92020-05-28 08:00:00 -07002377 (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002378 continue;
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002379 if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002380 continue; /* skip if same as the default */
2381 if ((want_set &&
2382 (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
2383 (!want_set && (sbi->s_mount_opt & m->mount_opt)))
2384 continue; /* select Opt_noFoo vs Opt_Foo */
2385 SEQ_OPTS_PRINT("%s", token2str(m->token));
2386 }
2387
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002388 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002389 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002390 SEQ_OPTS_PRINT("resuid=%u",
2391 from_kuid_munged(&init_user_ns, sbi->s_resuid));
2392 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002393 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002394 SEQ_OPTS_PRINT("resgid=%u",
2395 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002396 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002397 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2398 SEQ_OPTS_PUTS("errors=remount-ro");
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002399 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002400 SEQ_OPTS_PUTS("errors=continue");
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002401 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002402 SEQ_OPTS_PUTS("errors=panic");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002403 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002404 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002405 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002406 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002407 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002408 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
Matthew Garrett357fdad2017-10-18 13:56:26 -07002409 if (sb->s_flags & SB_I_VERSION)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002410 SEQ_OPTS_PUTS("i_version");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002411 if (nodefs || sbi->s_stripe)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002412 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002413 if (nodefs || EXT4_MOUNT_DATA_FLAGS &
2414 (sbi->s_mount_opt ^ def_mount_opt)) {
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002415 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2416 SEQ_OPTS_PUTS("data=journal");
2417 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2418 SEQ_OPTS_PUTS("data=ordered");
2419 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
2420 SEQ_OPTS_PUTS("data=writeback");
2421 }
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002422 if (nodefs ||
2423 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002424 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
2425 sbi->s_inode_readahead_blks);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002426
Tyson Nottinghamceec0372018-03-30 00:53:33 -04002427 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002428 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002429 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
Theodore Ts'odf981d02012-08-17 09:48:17 -04002430 if (nodefs || sbi->s_max_dir_size_kb)
2431 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
Ales Novak7915a862016-03-12 21:55:50 -05002432 if (test_opt(sb, DATA_ERR_ABORT))
2433 SEQ_OPTS_PUTS("data_err=abort");
Eric Biggersed318a62020-05-12 16:32:50 -07002434
2435 fscrypt_show_test_dummy_encryption(seq, sep, sb);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002436
Eric Biggers4f74d152020-07-02 01:56:07 +00002437 if (sb->s_flags & SB_INLINECRYPT)
2438 SEQ_OPTS_PUTS("inlinecrypt");
2439
Ira Weiny9cb20f92020-05-28 08:00:00 -07002440 if (test_opt(sb, DAX_ALWAYS)) {
2441 if (IS_EXT2_SB(sb))
2442 SEQ_OPTS_PUTS("dax");
2443 else
2444 SEQ_OPTS_PUTS("dax=always");
2445 } else if (test_opt2(sb, DAX_NEVER)) {
2446 SEQ_OPTS_PUTS("dax=never");
2447 } else if (test_opt2(sb, DAX_INODE)) {
2448 SEQ_OPTS_PUTS("dax=inode");
2449 }
2450
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002451 ext4_show_quota_options(seq, sb);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002452 return 0;
2453}
2454
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002455static int ext4_show_options(struct seq_file *seq, struct dentry *root)
2456{
2457 return _ext4_show_options(seq, root->d_sb, 0);
2458}
2459
Theodore Ts'oebd173b2015-09-23 12:46:17 -04002460int ext4_seq_options_show(struct seq_file *seq, void *offset)
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002461{
2462 struct super_block *sb = seq->private;
2463 int rc;
2464
David Howellsbc98a422017-07-17 08:45:34 +01002465 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002466 rc = _ext4_show_options(seq, sb, 1);
2467 seq_puts(seq, "\n");
2468 return rc;
2469}
2470
Mingming Cao617ba132006-10-11 01:20:53 -07002471static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002472 int read_only)
2473{
Mingming Cao617ba132006-10-11 01:20:53 -07002474 struct ext4_sb_info *sbi = EXT4_SB(sb);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002475 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002476
Mingming Cao617ba132006-10-11 01:20:53 -07002477 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002478 ext4_msg(sb, KERN_ERR, "revision level too high, "
2479 "forcing read-only mode");
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002480 err = -EROFS;
yangerkun5adacca2020-06-01 15:34:04 +08002481 goto done;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002482 }
2483 if (read_only)
Theodore Ts'o281b5992011-09-09 18:34:51 -04002484 goto done;
Mingming Cao617ba132006-10-11 01:20:53 -07002485 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002486 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2487 "running e2fsck is recommended");
Lukas Czernerc8b459f2014-05-12 12:55:07 -04002488 else if (sbi->s_mount_state & EXT4_ERROR_FS)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002489 ext4_msg(sb, KERN_WARNING,
2490 "warning: mounting fs with errors, "
2491 "running e2fsck is recommended");
Tao Maed3ce802011-05-18 13:29:57 -04002492 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002493 le16_to_cpu(es->s_mnt_count) >=
2494 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002495 ext4_msg(sb, KERN_WARNING,
2496 "warning: maximal mount count reached, "
2497 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002498 else if (le32_to_cpu(es->s_checkinterval) &&
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04002499 (ext4_get_tstamp(es, s_lastcheck) +
2500 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002501 ext4_msg(sb, KERN_WARNING,
2502 "warning: checktime reached, "
2503 "running e2fsck is recommended");
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002504 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05002505 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002506 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07002507 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002508 le16_add_cpu(&es->s_mnt_count, 1);
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04002509 ext4_update_tstamp(es, s_mtime);
Frank Mayhar03901312009-01-07 00:06:22 -05002510 if (sbi->s_journal)
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002511 ext4_set_feature_journal_needs_recovery(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002512
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002513 err = ext4_commit_super(sb, 1);
Theodore Ts'o281b5992011-09-09 18:34:51 -04002514done:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002515 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002516 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Theodore Ts'oa2595b82010-12-15 20:30:48 -05002517 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002518 sb->s_blocksize,
2519 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07002520 EXT4_BLOCKS_PER_GROUP(sb),
2521 EXT4_INODES_PER_GROUP(sb),
Theodore Ts'oa2595b82010-12-15 20:30:48 -05002522 sbi->s_mount_opt, sbi->s_mount_opt2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002523
Dan Magenheimer7abc52c2011-05-26 10:02:03 -06002524 cleancache_init_fs(sb);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002525 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002526}
2527
Theodore Ts'o117fff12012-09-05 01:29:50 -04002528int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2529{
2530 struct ext4_sb_info *sbi = EXT4_SB(sb);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002531 struct flex_groups **old_groups, **new_groups;
Dan Carpenter37b0b6b2020-02-28 12:22:56 +03002532 int size, i, j;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002533
2534 if (!sbi->s_log_groups_per_flex)
2535 return 0;
2536
2537 size = ext4_flex_group(sbi, ngroup - 1) + 1;
2538 if (size <= sbi->s_flex_groups_allocated)
2539 return 0;
2540
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002541 new_groups = kvzalloc(roundup_pow_of_two(size *
2542 sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002543 if (!new_groups) {
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002544 ext4_msg(sb, KERN_ERR,
2545 "not enough memory for %d flex group pointers", size);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002546 return -ENOMEM;
2547 }
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002548 for (i = sbi->s_flex_groups_allocated; i < size; i++) {
2549 new_groups[i] = kvzalloc(roundup_pow_of_two(
2550 sizeof(struct flex_groups)),
2551 GFP_KERNEL);
2552 if (!new_groups[i]) {
Dan Carpenter37b0b6b2020-02-28 12:22:56 +03002553 for (j = sbi->s_flex_groups_allocated; j < i; j++)
2554 kvfree(new_groups[j]);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002555 kvfree(new_groups);
2556 ext4_msg(sb, KERN_ERR,
2557 "not enough memory for %d flex groups", size);
2558 return -ENOMEM;
2559 }
Theodore Ts'o117fff12012-09-05 01:29:50 -04002560 }
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002561 rcu_read_lock();
2562 old_groups = rcu_dereference(sbi->s_flex_groups);
2563 if (old_groups)
2564 memcpy(new_groups, old_groups,
2565 (sbi->s_flex_groups_allocated *
2566 sizeof(struct flex_groups *)));
2567 rcu_read_unlock();
2568 rcu_assign_pointer(sbi->s_flex_groups, new_groups);
2569 sbi->s_flex_groups_allocated = size;
2570 if (old_groups)
2571 ext4_kvfree_array_rcu(old_groups);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002572 return 0;
2573}
2574
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002575static int ext4_fill_flex_info(struct super_block *sb)
2576{
2577 struct ext4_sb_info *sbi = EXT4_SB(sb);
2578 struct ext4_group_desc *gdp = NULL;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002579 struct flex_groups *fg;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002580 ext4_group_t flex_group;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002581 int i, err;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002582
Theodore Ts'o503358a2009-11-23 07:24:46 -05002583 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
Xi Wangd50f2ab2012-01-10 11:51:10 -05002584 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002585 sbi->s_log_groups_per_flex = 0;
2586 return 1;
2587 }
2588
Theodore Ts'o117fff12012-09-05 01:29:50 -04002589 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2590 if (err)
Theodore Ts'o9933fc02011-08-01 08:45:02 -04002591 goto failed;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002592
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002593 for (i = 0; i < sbi->s_groups_count; i++) {
Theodore Ts'o88b6edd2009-05-25 11:50:39 -04002594 gdp = ext4_get_group_desc(sb, i, NULL);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002595
2596 flex_group = ext4_flex_group(sbi, i);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002597 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
2598 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04002599 atomic64_add(ext4_free_group_clusters(sb, gdp),
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002600 &fg->free_clusters);
2601 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002602 }
2603
2604 return 1;
2605failed:
2606 return 0;
2607}
2608
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002609static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002610 struct ext4_group_desc *gdp)
Andreas Dilger717d50e2007-10-16 18:38:25 -04002611{
Daeho Jeongb47820e2016-07-03 17:51:39 -04002612 int offset = offsetof(struct ext4_group_desc, bg_checksum);
Andreas Dilger717d50e2007-10-16 18:38:25 -04002613 __u16 crc = 0;
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002614 __le32 le_group = cpu_to_le32(block_group);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002615 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andreas Dilger717d50e2007-10-16 18:38:25 -04002616
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002617 if (ext4_has_metadata_csum(sbi->s_sb)) {
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002618 /* Use new metadata_csum algorithm */
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002619 __u32 csum32;
Daeho Jeongb47820e2016-07-03 17:51:39 -04002620 __u16 dummy_csum = 0;
Andreas Dilger717d50e2007-10-16 18:38:25 -04002621
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002622 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2623 sizeof(le_group));
Daeho Jeongb47820e2016-07-03 17:51:39 -04002624 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2625 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2626 sizeof(dummy_csum));
2627 offset += sizeof(dummy_csum);
2628 if (offset < sbi->s_desc_size)
2629 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2630 sbi->s_desc_size - offset);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002631
2632 crc = csum32 & 0xFFFF;
2633 goto out;
Andreas Dilger717d50e2007-10-16 18:38:25 -04002634 }
2635
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002636 /* old crc16 code */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002637 if (!ext4_has_feature_gdt_csum(sb))
Darrick J. Wong813d32f2014-10-14 02:35:49 -04002638 return 0;
2639
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002640 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2641 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2642 crc = crc16(crc, (__u8 *)gdp, offset);
2643 offset += sizeof(gdp->bg_checksum); /* skip checksum */
2644 /* for checksum of struct ext4_group_desc do the rest...*/
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002645 if (ext4_has_feature_64bit(sb) &&
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002646 offset < le16_to_cpu(sbi->s_es->s_desc_size))
2647 crc = crc16(crc, (__u8 *)gdp + offset,
2648 le16_to_cpu(sbi->s_es->s_desc_size) -
2649 offset);
2650
2651out:
Andreas Dilger717d50e2007-10-16 18:38:25 -04002652 return cpu_to_le16(crc);
2653}
2654
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002655int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
Andreas Dilger717d50e2007-10-16 18:38:25 -04002656 struct ext4_group_desc *gdp)
2657{
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002658 if (ext4_has_group_desc_csum(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002659 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
Andreas Dilger717d50e2007-10-16 18:38:25 -04002660 return 0;
2661
2662 return 1;
2663}
2664
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002665void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2666 struct ext4_group_desc *gdp)
2667{
2668 if (!ext4_has_group_desc_csum(sb))
2669 return;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002670 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002671}
2672
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002673/* Called at mount-time, super-block is locked */
Lukas Czernerbfff6872010-10-27 21:30:05 -04002674static int ext4_check_descriptors(struct super_block *sb,
Theodore Ts'o829fa702016-08-01 00:51:02 -04002675 ext4_fsblk_t sb_block,
Lukas Czernerbfff6872010-10-27 21:30:05 -04002676 ext4_group_t *first_not_zeroed)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002677{
Mingming Cao617ba132006-10-11 01:20:53 -07002678 struct ext4_sb_info *sbi = EXT4_SB(sb);
2679 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2680 ext4_fsblk_t last_block;
Theodore Ts'o44de0222018-07-08 19:35:02 -04002681 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002682 ext4_fsblk_t block_bitmap;
2683 ext4_fsblk_t inode_bitmap;
2684 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04002685 int flexbg_flag = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04002686 ext4_group_t i, grp = sbi->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002687
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002688 if (ext4_has_feature_flex_bg(sb))
Jose R. Santosce421582007-10-16 18:38:25 -04002689 flexbg_flag = 1;
2690
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002691 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002692
Akinobu Mita197cd65a2008-02-06 01:40:16 -08002693 for (i = 0; i < sbi->s_groups_count; i++) {
2694 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2695
Jose R. Santosce421582007-10-16 18:38:25 -04002696 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002697 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002698 else
2699 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07002700 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701
Lukas Czernerbfff6872010-10-27 21:30:05 -04002702 if ((grp == sbi->s_groups_count) &&
2703 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2704 grp = i;
2705
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002706 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002707 if (block_bitmap == sb_block) {
2708 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2709 "Block bitmap for group %u overlaps "
2710 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002711 if (!sb_rdonly(sb))
2712 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002713 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002714 if (block_bitmap >= sb_block + 1 &&
2715 block_bitmap <= last_bg_block) {
2716 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2717 "Block bitmap for group %u overlaps "
2718 "block group descriptors", i);
2719 if (!sb_rdonly(sb))
2720 return 0;
2721 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002722 if (block_bitmap < first_block || block_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002723 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002724 "Block bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002725 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002726 return 0;
2727 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002728 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002729 if (inode_bitmap == sb_block) {
2730 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2731 "Inode bitmap for group %u overlaps "
2732 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002733 if (!sb_rdonly(sb))
2734 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002735 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002736 if (inode_bitmap >= sb_block + 1 &&
2737 inode_bitmap <= last_bg_block) {
2738 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2739 "Inode bitmap for group %u overlaps "
2740 "block group descriptors", i);
2741 if (!sb_rdonly(sb))
2742 return 0;
2743 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002744 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002745 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002746 "Inode bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002747 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002748 return 0;
2749 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002750 inode_table = ext4_inode_table(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002751 if (inode_table == sb_block) {
2752 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2753 "Inode table for group %u overlaps "
2754 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002755 if (!sb_rdonly(sb))
2756 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002757 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002758 if (inode_table >= sb_block + 1 &&
2759 inode_table <= last_bg_block) {
2760 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2761 "Inode table for group %u overlaps "
2762 "block group descriptors", i);
2763 if (!sb_rdonly(sb))
2764 return 0;
2765 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002766 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002767 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002768 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002769 "Inode table for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002770 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002771 return 0;
2772 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002773 ext4_lock_group(sb, i);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002774 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002775 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2776 "Checksum for group %u failed (%u!=%u)",
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002777 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
Eric Sandeenb31e1552009-06-04 17:36:36 -04002778 gdp)), le16_to_cpu(gdp->bg_checksum));
David Howellsbc98a422017-07-17 08:45:34 +01002779 if (!sb_rdonly(sb)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002780 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04002781 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04002782 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04002783 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002784 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04002785 if (!flexbg_flag)
2786 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002787 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04002788 if (NULL != first_not_zeroed)
2789 *first_not_zeroed = grp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002790 return 1;
2791}
2792
Mingming Cao617ba132006-10-11 01:20:53 -07002793/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002794 * the superblock) which were deleted from all directories, but held open by
2795 * a process at the time of a crash. We walk the list and try to delete these
2796 * inodes at recovery time (only with a read-write filesystem).
2797 *
2798 * In order to keep the orphan inode chain consistent during traversal (in
2799 * case of crash during recovery), we link each inode into the superblock
2800 * orphan list_head and handle it the same way as an inode deletion during
2801 * normal operation (which journals the operations for us).
2802 *
2803 * We only do an iget() and an iput() on each inode, which is very safe if we
2804 * accidentally point at an in-use or already deleted inode. The worst that
2805 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07002806 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002807 * e2fsck was run on this filesystem, and it must have already done the orphan
2808 * inode cleanup for us, so we can safely abort without any further action.
2809 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002810static void ext4_orphan_cleanup(struct super_block *sb,
2811 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002812{
2813 unsigned int s_flags = sb->s_flags;
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05002814 int ret, nr_orphans = 0, nr_truncates = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002815#ifdef CONFIG_QUOTA
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002816 int quota_update = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002817 int i;
2818#endif
2819 if (!es->s_last_orphan) {
2820 jbd_debug(4, "no orphan inodes to clean up\n");
2821 return;
2822 }
2823
Eric Sandeena8f48a92006-12-06 20:40:13 -08002824 if (bdev_read_only(sb->s_bdev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002825 ext4_msg(sb, KERN_ERR, "write access "
2826 "unavailable, skipping orphan cleanup");
Eric Sandeena8f48a92006-12-06 20:40:13 -08002827 return;
2828 }
2829
Amir Goldsteind39195c2011-02-28 00:53:45 -05002830 /* Check if feature set would not allow a r/w mount */
2831 if (!ext4_feature_set_ok(sb, 0)) {
2832 ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2833 "unknown ROCOMPAT features");
2834 return;
2835 }
2836
Mingming Cao617ba132006-10-11 01:20:53 -07002837 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Eric Sandeenc25f9bc2012-09-26 23:30:12 -04002838 /* don't clear list on RO mount w/ errors */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002839 if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
Dmitry Monakhov84474972014-09-16 14:52:03 -04002840 ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002841 "clearing orphan list.\n");
Eric Sandeenc25f9bc2012-09-26 23:30:12 -04002842 es->s_last_orphan = 0;
2843 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002844 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2845 return;
2846 }
2847
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002848 if (s_flags & SB_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002849 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002850 sb->s_flags &= ~SB_RDONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002851 }
2852#ifdef CONFIG_QUOTA
2853 /* Needed for iput() to work correctly and not trash data */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002854 sb->s_flags |= SB_ACTIVE;
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002855
2856 /*
2857 * Turn on quotas which were not enabled for read-only mounts if
2858 * filesystem has quota feature, so that they are updated correctly.
2859 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002860 if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002861 int ret = ext4_enable_quotas(sb);
2862
2863 if (!ret)
2864 quota_update = 1;
2865 else
2866 ext4_msg(sb, KERN_ERR,
2867 "Cannot turn on quotas: error %d", ret);
2868 }
2869
2870 /* Turn on journaled quotas used for old sytle */
Jan Karaa2d4a642014-09-11 11:15:15 -04002871 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07002872 if (EXT4_SB(sb)->s_qf_names[i]) {
2873 int ret = ext4_quota_on_mount(sb, i);
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002874
2875 if (!ret)
2876 quota_update = 1;
2877 else
Eric Sandeenb31e1552009-06-04 17:36:36 -04002878 ext4_msg(sb, KERN_ERR,
2879 "Cannot turn on journaled "
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002880 "quota: type %d: error %d", i, ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881 }
2882 }
2883#endif
2884
2885 while (es->s_last_orphan) {
2886 struct inode *inode;
2887
Vegard Nossumc65d5c62016-07-14 23:21:35 -04002888 /*
2889 * We may have encountered an error during cleanup; if
2890 * so, skip the rest.
2891 */
2892 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2893 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2894 es->s_last_orphan = 0;
2895 break;
2896 }
2897
Josef Bacik97bd42b2008-04-29 22:04:56 -04002898 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2899 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002900 es->s_last_orphan = 0;
2901 break;
2902 }
2903
Mingming Cao617ba132006-10-11 01:20:53 -07002904 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Christoph Hellwig871a2932010-03-03 09:05:07 -05002905 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002906 if (inode->i_nlink) {
Paul Taysom566370a2013-05-28 07:51:21 -04002907 if (test_opt(sb, DEBUG))
2908 ext4_msg(sb, KERN_DEBUG,
2909 "%s: truncating inode %lu to %lld bytes",
2910 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04002911 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002912 inode->i_ino, inode->i_size);
Al Viro59551022016-01-22 15:40:57 -05002913 inode_lock(inode);
Lukas Czerner55f252c2013-05-27 23:32:35 -04002914 truncate_inode_pages(inode->i_mapping, inode->i_size);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05002915 ret = ext4_truncate(inode);
2916 if (ret)
2917 ext4_std_error(inode->i_sb, ret);
Al Viro59551022016-01-22 15:40:57 -05002918 inode_unlock(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002919 nr_truncates++;
2920 } else {
Paul Taysom566370a2013-05-28 07:51:21 -04002921 if (test_opt(sb, DEBUG))
2922 ext4_msg(sb, KERN_DEBUG,
2923 "%s: deleting unreferenced inode %lu",
2924 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002925 jbd_debug(2, "deleting unreferenced inode %lu\n",
2926 inode->i_ino);
2927 nr_orphans++;
2928 }
2929 iput(inode); /* The delete magic happens here! */
2930 }
2931
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002932#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002933
2934 if (nr_orphans)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002935 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2936 PLURAL(nr_orphans));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 if (nr_truncates)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002938 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2939 PLURAL(nr_truncates));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002940#ifdef CONFIG_QUOTA
zhangyi (F)95f1fda2017-08-24 15:21:50 -04002941 /* Turn off quotas if they were enabled for orphan cleanup */
2942 if (quota_update) {
2943 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2944 if (sb_dqopt(sb)->files[i])
2945 dquot_quota_off(sb, i);
2946 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002947 }
2948#endif
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002949 sb->s_flags = s_flags; /* Restore SB_RDONLY status */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002950}
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002951
Eric Sandeencd2291a2008-01-28 23:58:27 -05002952/*
2953 * Maximal extent format file size.
2954 * Resulting logical blkno at s_maxbytes must fit in our on-disk
2955 * extent format containers, within a sector_t, and within i_blocks
2956 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
2957 * so that won't be a limiting factor.
2958 *
Lukas Czernerf17722f2011-06-06 00:05:17 -04002959 * However there is other limiting factor. We do store extents in the form
2960 * of starting block and length, hence the resulting length of the extent
2961 * covering maximum file size must fit into on-disk format containers as
2962 * well. Given that length is always by 1 unit bigger than max unit (because
2963 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2964 *
Eric Sandeencd2291a2008-01-28 23:58:27 -05002965 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2966 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04002967static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05002968{
2969 loff_t res;
2970 loff_t upper_limit = MAX_LFS_FILESIZE;
2971
Christoph Hellwig72deb452019-04-05 18:08:59 +02002972 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
2973
2974 if (!has_huge_files) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05002975 upper_limit = (1LL << 32) - 1;
2976
2977 /* total blocks in file system block size */
2978 upper_limit >>= (blkbits - 9);
2979 upper_limit <<= blkbits;
2980 }
2981
Lukas Czernerf17722f2011-06-06 00:05:17 -04002982 /*
2983 * 32-bit extent-start container, ee_block. We lower the maxbytes
2984 * by one fs block, so ee_len can cover the extent of maximum file
2985 * size
2986 */
2987 res = (1LL << 32) - 1;
Eric Sandeencd2291a2008-01-28 23:58:27 -05002988 res <<= blkbits;
Eric Sandeencd2291a2008-01-28 23:58:27 -05002989
2990 /* Sanity check against vm- & vfs- imposed limits */
2991 if (res > upper_limit)
2992 res = upper_limit;
2993
2994 return res;
2995}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002997/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05002998 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05002999 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3000 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003002static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003003{
Mingming Cao617ba132006-10-11 01:20:53 -07003004 loff_t res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003005 int meta_blocks;
3006 loff_t upper_limit;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003007 /* This is calculated to be the largest file size for a dense, block
3008 * mapped file such that the file's total number of 512-byte sectors,
3009 * including data and all indirect blocks, does not exceed (2^48 - 1).
3010 *
3011 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3012 * number of 512-byte sectors of the file.
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003013 */
3014
Christoph Hellwig72deb452019-04-05 18:08:59 +02003015 if (!has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003016 /*
Christoph Hellwig72deb452019-04-05 18:08:59 +02003017 * !has_huge_files or implies that the inode i_block field
3018 * represents total file blocks in 2^32 512-byte sectors ==
3019 * size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003020 */
3021 upper_limit = (1LL << 32) - 1;
3022
3023 /* total blocks in file system block size */
3024 upper_limit >>= (bits - 9);
3025
3026 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003027 /*
3028 * We use 48 bit ext4_inode i_blocks
3029 * With EXT4_HUGE_FILE_FL set the i_blocks
3030 * represent total number of blocks in
3031 * file system block size
3032 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003033 upper_limit = (1LL << 48) - 1;
3034
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003035 }
3036
3037 /* indirect blocks */
3038 meta_blocks = 1;
3039 /* double indirect blocks */
3040 meta_blocks += 1 + (1LL << (bits-2));
3041 /* tripple indirect blocks */
3042 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
3043
3044 upper_limit -= meta_blocks;
3045 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003046
3047 res += 1LL << (bits-2);
3048 res += 1LL << (2*(bits-2));
3049 res += 1LL << (3*(bits-2));
3050 res <<= bits;
3051 if (res > upper_limit)
3052 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003053
3054 if (res > MAX_LFS_FILESIZE)
3055 res = MAX_LFS_FILESIZE;
3056
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003057 return res;
3058}
3059
Mingming Cao617ba132006-10-11 01:20:53 -07003060static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003061 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003062{
Mingming Cao617ba132006-10-11 01:20:53 -07003063 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003064 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003065 int has_super = 0;
3066
3067 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3068
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003069 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07003070 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003071 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07003072 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003073 has_super = 1;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003074
Darrick J. Wongbd63f6b2014-05-12 10:06:27 -04003075 /*
3076 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3077 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
3078 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3079 * compensate.
3080 */
3081 if (sb->s_blocksize == 1024 && nr == 0 &&
Jun Piao49598e02018-01-11 13:17:49 -05003082 le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
Darrick J. Wongbd63f6b2014-05-12 10:06:27 -04003083 has_super++;
3084
Mingming Cao617ba132006-10-11 01:20:53 -07003085 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003086}
3087
Alex Tomasc9de5602008-01-29 00:19:52 -05003088/**
3089 * ext4_get_stripe_size: Get the stripe size.
3090 * @sbi: In memory super block info
3091 *
3092 * If we have specified it via mount option, then
3093 * use the mount option value. If the value specified at mount time is
3094 * greater than the blocks per group use the super block value.
3095 * If the super block value is greater than blocks per group return 0.
3096 * Allocator needs it be less than blocks per group.
3097 *
3098 */
3099static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3100{
3101 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3102 unsigned long stripe_width =
3103 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003104 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05003105
3106 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003107 ret = sbi->s_stripe;
Jan Kara5469d7c2017-02-10 00:56:09 -05003108 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003109 ret = stripe_width;
Jan Kara5469d7c2017-02-10 00:56:09 -05003110 else if (stride && stride <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003111 ret = stride;
3112 else
3113 ret = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003114
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003115 /*
3116 * If the stripe width is 1, this makes no sense and
3117 * we set it to 0 to turn off stripe handling code.
3118 */
3119 if (ret <= 1)
3120 ret = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003121
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003122 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05003123}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003124
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003125/*
3126 * Check whether this filesystem can be mounted based on
3127 * the features present and the RDONLY/RDWR mount requested.
3128 * Returns 1 if this filesystem can be mounted as requested,
3129 * 0 if it cannot be.
3130 */
3131static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3132{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003133 if (ext4_has_unknown_ext4_incompat_features(sb)) {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003134 ext4_msg(sb, KERN_ERR,
3135 "Couldn't mount because of "
3136 "unsupported optional features (%x)",
3137 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3138 ~EXT4_FEATURE_INCOMPAT_SUPP));
3139 return 0;
3140 }
3141
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04003142#ifndef CONFIG_UNICODE
3143 if (ext4_has_feature_casefold(sb)) {
3144 ext4_msg(sb, KERN_ERR,
3145 "Filesystem with casefold feature cannot be "
3146 "mounted without CONFIG_UNICODE");
3147 return 0;
3148 }
3149#endif
3150
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003151 if (readonly)
3152 return 1;
3153
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003154 if (ext4_has_feature_readonly(sb)) {
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05003155 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003156 sb->s_flags |= SB_RDONLY;
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05003157 return 1;
3158 }
3159
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003160 /* Check that feature set is OK for a read-write mount */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003161 if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003162 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3163 "unsupported optional features (%x)",
3164 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3165 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3166 return 0;
3167 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003168 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -04003169 ext4_msg(sb, KERN_ERR,
3170 "Can't support bigalloc feature without "
3171 "extents feature\n");
3172 return 0;
3173 }
Aditya Kali7c319d32012-07-22 20:21:31 -04003174
Jan Kara9db176b2020-02-21 11:08:35 +01003175#if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
Theodore Ts'od65d87a2020-02-14 18:11:19 -05003176 if (!readonly && (ext4_has_feature_quota(sb) ||
3177 ext4_has_feature_project(sb))) {
Aditya Kali7c319d32012-07-22 20:21:31 -04003178 ext4_msg(sb, KERN_ERR,
Theodore Ts'od65d87a2020-02-14 18:11:19 -05003179 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
Li Xi689c9582016-01-08 16:01:22 -05003180 return 0;
3181 }
Aditya Kali7c319d32012-07-22 20:21:31 -04003182#endif /* CONFIG_QUOTA */
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003183 return 1;
3184}
3185
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003186/*
3187 * This function is called once a day if we have errors logged
3188 * on the file system
3189 */
Kees Cook235699a2017-10-18 12:45:17 -04003190static void print_daily_error_info(struct timer_list *t)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003191{
Kees Cook235699a2017-10-18 12:45:17 -04003192 struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3193 struct super_block *sb = sbi->s_sb;
3194 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003195
3196 if (es->s_error_count)
Theodore Ts'oae0f78d2014-07-05 18:40:52 -04003197 /* fsck newer than v1.41.13 is needed to clean this condition. */
3198 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003199 le32_to_cpu(es->s_error_count));
3200 if (es->s_first_error_time) {
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04003201 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3202 sb->s_id,
3203 ext4_get_tstamp(es, s_first_error_time),
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003204 (int) sizeof(es->s_first_error_func),
3205 es->s_first_error_func,
3206 le32_to_cpu(es->s_first_error_line));
3207 if (es->s_first_error_ino)
Joe Perches651e1c32016-10-12 23:12:53 -04003208 printk(KERN_CONT ": inode %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003209 le32_to_cpu(es->s_first_error_ino));
3210 if (es->s_first_error_block)
Joe Perches651e1c32016-10-12 23:12:53 -04003211 printk(KERN_CONT ": block %llu", (unsigned long long)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003212 le64_to_cpu(es->s_first_error_block));
Joe Perches651e1c32016-10-12 23:12:53 -04003213 printk(KERN_CONT "\n");
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003214 }
3215 if (es->s_last_error_time) {
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04003216 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3217 sb->s_id,
3218 ext4_get_tstamp(es, s_last_error_time),
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003219 (int) sizeof(es->s_last_error_func),
3220 es->s_last_error_func,
3221 le32_to_cpu(es->s_last_error_line));
3222 if (es->s_last_error_ino)
Joe Perches651e1c32016-10-12 23:12:53 -04003223 printk(KERN_CONT ": inode %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003224 le32_to_cpu(es->s_last_error_ino));
3225 if (es->s_last_error_block)
Joe Perches651e1c32016-10-12 23:12:53 -04003226 printk(KERN_CONT ": block %llu", (unsigned long long)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003227 le64_to_cpu(es->s_last_error_block));
Joe Perches651e1c32016-10-12 23:12:53 -04003228 printk(KERN_CONT "\n");
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003229 }
3230 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
3231}
3232
Lukas Czernerbfff6872010-10-27 21:30:05 -04003233/* Find next suitable group and run ext4_init_inode_table */
3234static int ext4_run_li_request(struct ext4_li_request *elr)
3235{
3236 struct ext4_group_desc *gdp = NULL;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003237 struct super_block *sb = elr->lr_super;
3238 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3239 ext4_group_t group = elr->lr_next_group;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003240 unsigned long timeout = 0;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003241 unsigned int prefetch_ios = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003242 int ret = 0;
3243
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003244 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
3245 elr->lr_next_group = ext4_mb_prefetch(sb, group,
3246 EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
3247 if (prefetch_ios)
3248 ext4_mb_prefetch_fini(sb, elr->lr_next_group,
3249 prefetch_ios);
3250 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
3251 prefetch_ios);
3252 if (group >= elr->lr_next_group) {
3253 ret = 1;
3254 if (elr->lr_first_not_zeroed != ngroups &&
3255 !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3256 elr->lr_next_group = elr->lr_first_not_zeroed;
3257 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3258 ret = 0;
3259 }
3260 }
3261 return ret;
3262 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003263
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003264 for (; group < ngroups; group++) {
Lukas Czernerbfff6872010-10-27 21:30:05 -04003265 gdp = ext4_get_group_desc(sb, group, NULL);
3266 if (!gdp) {
3267 ret = 1;
3268 break;
3269 }
3270
3271 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3272 break;
3273 }
3274
Theodore Ts'o7f511862013-01-13 08:41:45 -05003275 if (group >= ngroups)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003276 ret = 1;
3277
3278 if (!ret) {
3279 timeout = jiffies;
3280 ret = ext4_init_inode_table(sb, group,
3281 elr->lr_timeout ? 0 : 1);
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003282 trace_ext4_lazy_itable_init(sb, group);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003283 if (elr->lr_timeout == 0) {
Lukas Czerner51ce6512011-05-20 13:55:16 -04003284 timeout = (jiffies - timeout) *
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003285 EXT4_SB(elr->lr_super)->s_li_wait_mult;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003286 elr->lr_timeout = timeout;
3287 }
3288 elr->lr_next_sched = jiffies + elr->lr_timeout;
3289 elr->lr_next_group = group + 1;
3290 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003291 return ret;
3292}
3293
3294/*
3295 * Remove lr_request from the list_request and free the
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003296 * request structure. Should be called with li_list_mtx held
Lukas Czernerbfff6872010-10-27 21:30:05 -04003297 */
3298static void ext4_remove_li_request(struct ext4_li_request *elr)
3299{
Lukas Czernerbfff6872010-10-27 21:30:05 -04003300 if (!elr)
3301 return;
3302
Lukas Czernerbfff6872010-10-27 21:30:05 -04003303 list_del(&elr->lr_request);
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003304 EXT4_SB(elr->lr_super)->s_li_request = NULL;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003305 kfree(elr);
3306}
3307
3308static void ext4_unregister_li_request(struct super_block *sb)
3309{
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003310 mutex_lock(&ext4_li_mtx);
3311 if (!ext4_li_info) {
3312 mutex_unlock(&ext4_li_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003313 return;
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003314 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003315
3316 mutex_lock(&ext4_li_info->li_list_mtx);
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003317 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003318 mutex_unlock(&ext4_li_info->li_list_mtx);
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003319 mutex_unlock(&ext4_li_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003320}
3321
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003322static struct task_struct *ext4_lazyinit_task;
3323
Lukas Czernerbfff6872010-10-27 21:30:05 -04003324/*
3325 * This is the function where ext4lazyinit thread lives. It walks
3326 * through the request list searching for next scheduled filesystem.
3327 * When such a fs is found, run the lazy initialization request
3328 * (ext4_rn_li_request) and keep track of the time spend in this
3329 * function. Based on that time we compute next schedule time of
3330 * the request. When walking through the list is complete, compute
3331 * next waking time and put itself into sleep.
3332 */
3333static int ext4_lazyinit_thread(void *arg)
3334{
3335 struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3336 struct list_head *pos, *n;
3337 struct ext4_li_request *elr;
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003338 unsigned long next_wakeup, cur;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003339
3340 BUG_ON(NULL == eli);
3341
Lukas Czernerbfff6872010-10-27 21:30:05 -04003342cont_thread:
3343 while (true) {
3344 next_wakeup = MAX_JIFFY_OFFSET;
3345
3346 mutex_lock(&eli->li_list_mtx);
3347 if (list_empty(&eli->li_request_list)) {
3348 mutex_unlock(&eli->li_list_mtx);
3349 goto exit_thread;
3350 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003351 list_for_each_safe(pos, n, &eli->li_request_list) {
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003352 int err = 0;
3353 int progress = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003354 elr = list_entry(pos, struct ext4_li_request,
3355 lr_request);
3356
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003357 if (time_before(jiffies, elr->lr_next_sched)) {
3358 if (time_before(elr->lr_next_sched, next_wakeup))
3359 next_wakeup = elr->lr_next_sched;
3360 continue;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003361 }
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003362 if (down_read_trylock(&elr->lr_super->s_umount)) {
3363 if (sb_start_write_trylock(elr->lr_super)) {
3364 progress = 1;
3365 /*
3366 * We hold sb->s_umount, sb can not
3367 * be removed from the list, it is
3368 * now safe to drop li_list_mtx
3369 */
3370 mutex_unlock(&eli->li_list_mtx);
3371 err = ext4_run_li_request(elr);
3372 sb_end_write(elr->lr_super);
3373 mutex_lock(&eli->li_list_mtx);
3374 n = pos->next;
3375 }
3376 up_read((&elr->lr_super->s_umount));
3377 }
3378 /* error, remove the lazy_init job */
3379 if (err) {
3380 ext4_remove_li_request(elr);
3381 continue;
3382 }
3383 if (!progress) {
3384 elr->lr_next_sched = jiffies +
3385 (prandom_u32()
3386 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3387 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003388 if (time_before(elr->lr_next_sched, next_wakeup))
3389 next_wakeup = elr->lr_next_sched;
3390 }
3391 mutex_unlock(&eli->li_list_mtx);
3392
Tejun Heoa0acae02011-11-21 12:32:22 -08003393 try_to_freeze();
Lukas Czernerbfff6872010-10-27 21:30:05 -04003394
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003395 cur = jiffies;
3396 if ((time_after_eq(cur, next_wakeup)) ||
Lukas Czernerf4245bd2010-11-02 14:07:17 -04003397 (MAX_JIFFY_OFFSET == next_wakeup)) {
Lukas Czernerbfff6872010-10-27 21:30:05 -04003398 cond_resched();
3399 continue;
3400 }
3401
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003402 schedule_timeout_interruptible(next_wakeup - cur);
3403
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003404 if (kthread_should_stop()) {
3405 ext4_clear_request_list();
3406 goto exit_thread;
3407 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003408 }
3409
3410exit_thread:
3411 /*
3412 * It looks like the request list is empty, but we need
3413 * to check it under the li_list_mtx lock, to prevent any
3414 * additions into it, and of course we should lock ext4_li_mtx
3415 * to atomically free the list and ext4_li_info, because at
3416 * this point another ext4 filesystem could be registering
3417 * new one.
3418 */
3419 mutex_lock(&ext4_li_mtx);
3420 mutex_lock(&eli->li_list_mtx);
3421 if (!list_empty(&eli->li_request_list)) {
3422 mutex_unlock(&eli->li_list_mtx);
3423 mutex_unlock(&ext4_li_mtx);
3424 goto cont_thread;
3425 }
3426 mutex_unlock(&eli->li_list_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003427 kfree(ext4_li_info);
3428 ext4_li_info = NULL;
3429 mutex_unlock(&ext4_li_mtx);
3430
3431 return 0;
3432}
3433
3434static void ext4_clear_request_list(void)
3435{
3436 struct list_head *pos, *n;
3437 struct ext4_li_request *elr;
3438
3439 mutex_lock(&ext4_li_info->li_list_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003440 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3441 elr = list_entry(pos, struct ext4_li_request,
3442 lr_request);
3443 ext4_remove_li_request(elr);
3444 }
3445 mutex_unlock(&ext4_li_info->li_list_mtx);
3446}
3447
3448static int ext4_run_lazyinit_thread(void)
3449{
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003450 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3451 ext4_li_info, "ext4lazyinit");
3452 if (IS_ERR(ext4_lazyinit_task)) {
3453 int err = PTR_ERR(ext4_lazyinit_task);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003454 ext4_clear_request_list();
Lukas Czernerbfff6872010-10-27 21:30:05 -04003455 kfree(ext4_li_info);
3456 ext4_li_info = NULL;
Theodore Ts'o92b97812012-03-19 23:41:49 -04003457 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
Lukas Czernerbfff6872010-10-27 21:30:05 -04003458 "initialization thread\n",
3459 err);
3460 return err;
3461 }
3462 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003463 return 0;
3464}
3465
3466/*
3467 * Check whether it make sense to run itable init. thread or not.
3468 * If there is at least one uninitialized inode table, return
3469 * corresponding group number, else the loop goes through all
3470 * groups and return total number of groups.
3471 */
3472static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3473{
3474 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3475 struct ext4_group_desc *gdp = NULL;
3476
Theodore Ts'o88446182018-06-14 00:58:00 -04003477 if (!ext4_has_group_desc_csum(sb))
3478 return ngroups;
3479
Lukas Czernerbfff6872010-10-27 21:30:05 -04003480 for (group = 0; group < ngroups; group++) {
3481 gdp = ext4_get_group_desc(sb, group, NULL);
3482 if (!gdp)
3483 continue;
3484
Theodore Ts'o50122842018-07-28 08:12:04 -04003485 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
Lukas Czernerbfff6872010-10-27 21:30:05 -04003486 break;
3487 }
3488
3489 return group;
3490}
3491
3492static int ext4_li_info_new(void)
3493{
3494 struct ext4_lazy_init *eli = NULL;
3495
3496 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3497 if (!eli)
3498 return -ENOMEM;
3499
Lukas Czernerbfff6872010-10-27 21:30:05 -04003500 INIT_LIST_HEAD(&eli->li_request_list);
3501 mutex_init(&eli->li_list_mtx);
3502
Lukas Czernerbfff6872010-10-27 21:30:05 -04003503 eli->li_state |= EXT4_LAZYINIT_QUIT;
3504
3505 ext4_li_info = eli;
3506
3507 return 0;
3508}
3509
3510static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3511 ext4_group_t start)
3512{
Lukas Czernerbfff6872010-10-27 21:30:05 -04003513 struct ext4_li_request *elr;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003514
3515 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3516 if (!elr)
3517 return NULL;
3518
3519 elr->lr_super = sb;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003520 elr->lr_first_not_zeroed = start;
3521 if (test_opt(sb, PREFETCH_BLOCK_BITMAPS))
3522 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3523 else {
3524 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3525 elr->lr_next_group = start;
3526 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003527
3528 /*
3529 * Randomize first schedule time of the request to
3530 * spread the inode table initialization requests
3531 * better.
3532 */
Theodore Ts'odd1f7232013-11-08 00:14:53 -05003533 elr->lr_next_sched = jiffies + (prandom_u32() %
3534 (EXT4_DEF_LI_MAX_START_DELAY * HZ));
Lukas Czernerbfff6872010-10-27 21:30:05 -04003535 return elr;
3536}
3537
Theodore Ts'o7f511862013-01-13 08:41:45 -05003538int ext4_register_li_request(struct super_block *sb,
3539 ext4_group_t first_not_zeroed)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003540{
3541 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7f511862013-01-13 08:41:45 -05003542 struct ext4_li_request *elr = NULL;
Jun Piao49598e02018-01-11 13:17:49 -05003543 ext4_group_t ngroups = sbi->s_groups_count;
Andrew Morton6c5a6cb2011-01-10 12:30:17 -05003544 int ret = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003545
Theodore Ts'o7f511862013-01-13 08:41:45 -05003546 mutex_lock(&ext4_li_mtx);
Lukas Czerner51ce6512011-05-20 13:55:16 -04003547 if (sbi->s_li_request != NULL) {
3548 /*
3549 * Reset timeout so it can be computed again, because
3550 * s_li_wait_mult might have changed.
3551 */
3552 sbi->s_li_request->lr_timeout = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -05003553 goto out;
Lukas Czerner51ce6512011-05-20 13:55:16 -04003554 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003555
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003556 if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) &&
3557 (first_not_zeroed == ngroups || sb_rdonly(sb) ||
3558 !test_opt(sb, INIT_INODE_TABLE)))
Theodore Ts'o7f511862013-01-13 08:41:45 -05003559 goto out;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003560
3561 elr = ext4_li_request_new(sb, first_not_zeroed);
Theodore Ts'o7f511862013-01-13 08:41:45 -05003562 if (!elr) {
3563 ret = -ENOMEM;
3564 goto out;
3565 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003566
3567 if (NULL == ext4_li_info) {
3568 ret = ext4_li_info_new();
3569 if (ret)
3570 goto out;
3571 }
3572
3573 mutex_lock(&ext4_li_info->li_list_mtx);
3574 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3575 mutex_unlock(&ext4_li_info->li_list_mtx);
3576
3577 sbi->s_li_request = elr;
Tao Ma46e46902011-04-04 16:00:49 -04003578 /*
3579 * set elr to NULL here since it has been inserted to
3580 * the request_list and the removal and free of it is
3581 * handled by ext4_clear_request_list from now on.
3582 */
3583 elr = NULL;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003584
3585 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3586 ret = ext4_run_lazyinit_thread();
3587 if (ret)
3588 goto out;
3589 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003590out:
Nicolas Kaiserbeed5ecb2010-10-27 22:08:42 -04003591 mutex_unlock(&ext4_li_mtx);
3592 if (ret)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003593 kfree(elr);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003594 return ret;
3595}
3596
3597/*
3598 * We do not need to lock anything since this is called on
3599 * module unload.
3600 */
3601static void ext4_destroy_lazyinit_thread(void)
3602{
3603 /*
3604 * If thread exited earlier
3605 * there's nothing to be done.
3606 */
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003607 if (!ext4_li_info || !ext4_lazyinit_task)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003608 return;
3609
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003610 kthread_stop(ext4_lazyinit_task);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003611}
3612
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003613static int set_journal_csum_feature_set(struct super_block *sb)
3614{
3615 int ret = 1;
3616 int compat, incompat;
3617 struct ext4_sb_info *sbi = EXT4_SB(sb);
3618
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04003619 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wongdb9ee222014-08-27 18:40:07 -04003620 /* journal checksum v3 */
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003621 compat = 0;
Darrick J. Wongdb9ee222014-08-27 18:40:07 -04003622 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003623 } else {
3624 /* journal checksum v1 */
3625 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3626 incompat = 0;
3627 }
3628
Darrick J. Wongfeb8c6d2014-09-11 11:38:21 -04003629 jbd2_journal_clear_features(sbi->s_journal,
3630 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3631 JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3632 JBD2_FEATURE_INCOMPAT_CSUM_V2);
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003633 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3634 ret = jbd2_journal_set_features(sbi->s_journal,
3635 compat, 0,
3636 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3637 incompat);
3638 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3639 ret = jbd2_journal_set_features(sbi->s_journal,
3640 compat, 0,
3641 incompat);
3642 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3643 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3644 } else {
Darrick J. Wongfeb8c6d2014-09-11 11:38:21 -04003645 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3646 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003647 }
3648
3649 return ret;
3650}
3651
Theodore Ts'o952fc182012-07-09 16:27:05 -04003652/*
3653 * Note: calculating the overhead so we can be compatible with
3654 * historical BSD practice is quite difficult in the face of
3655 * clusters/bigalloc. This is because multiple metadata blocks from
3656 * different block group can end up in the same allocation cluster.
3657 * Calculating the exact overhead in the face of clustered allocation
3658 * requires either O(all block bitmaps) in memory or O(number of block
3659 * groups**2) in time. We will still calculate the superblock for
3660 * older file systems --- and if we come across with a bigalloc file
3661 * system with zero in s_overhead_clusters the estimate will be close to
3662 * correct especially for very large cluster sizes --- but for newer
3663 * file systems, it's better to calculate this figure once at mkfs
3664 * time, and store it in the superblock. If the superblock value is
3665 * present (even for non-bigalloc file systems), we will use it.
3666 */
3667static int count_overhead(struct super_block *sb, ext4_group_t grp,
3668 char *buf)
3669{
3670 struct ext4_sb_info *sbi = EXT4_SB(sb);
3671 struct ext4_group_desc *gdp;
3672 ext4_fsblk_t first_block, last_block, b;
3673 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3674 int s, j, count = 0;
3675
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003676 if (!ext4_has_feature_bigalloc(sb))
Theodore Ts'o0548bbb2012-08-16 11:59:04 -04003677 return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3678 sbi->s_itb_per_group + 2);
3679
Theodore Ts'o952fc182012-07-09 16:27:05 -04003680 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3681 (grp * EXT4_BLOCKS_PER_GROUP(sb));
3682 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3683 for (i = 0; i < ngroups; i++) {
3684 gdp = ext4_get_group_desc(sb, i, NULL);
3685 b = ext4_block_bitmap(sb, gdp);
3686 if (b >= first_block && b <= last_block) {
3687 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3688 count++;
3689 }
3690 b = ext4_inode_bitmap(sb, gdp);
3691 if (b >= first_block && b <= last_block) {
3692 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3693 count++;
3694 }
3695 b = ext4_inode_table(sb, gdp);
3696 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3697 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3698 int c = EXT4_B2C(sbi, b - first_block);
3699 ext4_set_bit(c, buf);
3700 count++;
3701 }
3702 if (i != grp)
3703 continue;
3704 s = 0;
3705 if (ext4_bg_has_super(sb, grp)) {
3706 ext4_set_bit(s++, buf);
3707 count++;
3708 }
Theodore Ts'oc48ae412016-11-18 13:37:47 -05003709 j = ext4_bg_num_gdb(sb, grp);
3710 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3711 ext4_error(sb, "Invalid number of block group "
3712 "descriptor blocks: %d", j);
3713 j = EXT4_BLOCKS_PER_GROUP(sb) - s;
Theodore Ts'o952fc182012-07-09 16:27:05 -04003714 }
Theodore Ts'oc48ae412016-11-18 13:37:47 -05003715 count += j;
3716 for (; j > 0; j--)
3717 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003718 }
3719 if (!count)
3720 return 0;
3721 return EXT4_CLUSTERS_PER_GROUP(sb) -
3722 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3723}
3724
3725/*
3726 * Compute the overhead and stash it in sbi->s_overhead
3727 */
3728int ext4_calculate_overhead(struct super_block *sb)
3729{
3730 struct ext4_sb_info *sbi = EXT4_SB(sb);
3731 struct ext4_super_block *es = sbi->s_es;
Eric Whitney3c816de2016-09-30 02:08:49 -04003732 struct inode *j_inode;
3733 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003734 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3735 ext4_fsblk_t overhead = 0;
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05003736 char *buf = (char *) get_zeroed_page(GFP_NOFS);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003737
Theodore Ts'o952fc182012-07-09 16:27:05 -04003738 if (!buf)
3739 return -ENOMEM;
3740
3741 /*
3742 * Compute the overhead (FS structures). This is constant
3743 * for a given filesystem unless the number of block groups
3744 * changes so we cache the previous value until it does.
3745 */
3746
3747 /*
3748 * All of the blocks before first_data_block are overhead
3749 */
3750 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3751
3752 /*
3753 * Add the overhead found in each block group
3754 */
3755 for (i = 0; i < ngroups; i++) {
3756 int blks;
3757
3758 blks = count_overhead(sb, i, buf);
3759 overhead += blks;
3760 if (blks)
3761 memset(buf, 0, PAGE_SIZE);
3762 cond_resched();
3763 }
Eric Whitney3c816de2016-09-30 02:08:49 -04003764
3765 /*
3766 * Add the internal journal blocks whether the journal has been
3767 * loaded or not
3768 */
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08003769 if (sbi->s_journal && !sbi->s_journal_bdev)
Lukas Czerner810da242013-03-02 17:18:58 -05003770 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
Ritesh Harjanif1eec3b2020-03-16 15:00:38 +05303771 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3772 /* j_inum for internal journal is non-zero */
Eric Whitney3c816de2016-09-30 02:08:49 -04003773 j_inode = ext4_get_journal_inode(sb, j_inum);
3774 if (j_inode) {
3775 j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
3776 overhead += EXT4_NUM_B2C(sbi, j_blocks);
3777 iput(j_inode);
3778 } else {
3779 ext4_msg(sb, KERN_ERR, "can't get journal size");
3780 }
3781 }
Theodore Ts'o952fc182012-07-09 16:27:05 -04003782 sbi->s_overhead = overhead;
3783 smp_wmb();
3784 free_page((unsigned long) buf);
3785 return 0;
3786}
3787
Theodore Ts'ob5799012015-09-23 12:44:17 -04003788static void ext4_set_resv_clusters(struct super_block *sb)
Lukas Czerner27dd4382013-04-09 22:11:22 -04003789{
3790 ext4_fsblk_t resv_clusters;
Theodore Ts'ob5799012015-09-23 12:44:17 -04003791 struct ext4_sb_info *sbi = EXT4_SB(sb);
Lukas Czerner27dd4382013-04-09 22:11:22 -04003792
3793 /*
Jan Kara30fac0f2013-12-08 21:11:59 -05003794 * There's no need to reserve anything when we aren't using extents.
3795 * The space estimates are exact, there are no unwritten extents,
3796 * hole punching doesn't need new metadata... This is needed especially
3797 * to keep ext2/3 backward compatibility.
3798 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003799 if (!ext4_has_feature_extents(sb))
Theodore Ts'ob5799012015-09-23 12:44:17 -04003800 return;
Jan Kara30fac0f2013-12-08 21:11:59 -05003801 /*
Lukas Czerner27dd4382013-04-09 22:11:22 -04003802 * By default we reserve 2% or 4096 clusters, whichever is smaller.
3803 * This should cover the situations where we can not afford to run
3804 * out of space like for example punch hole, or converting
Lukas Czerner556615d2014-04-20 23:45:47 -04003805 * unwritten extents in delalloc path. In most cases such
Lukas Czerner27dd4382013-04-09 22:11:22 -04003806 * allocation would require 1, or 2 blocks, higher numbers are
3807 * very rare.
3808 */
Theodore Ts'ob5799012015-09-23 12:44:17 -04003809 resv_clusters = (ext4_blocks_count(sbi->s_es) >>
3810 sbi->s_cluster_bits);
Lukas Czerner27dd4382013-04-09 22:11:22 -04003811
3812 do_div(resv_clusters, 50);
3813 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
3814
Theodore Ts'ob5799012015-09-23 12:44:17 -04003815 atomic64_set(&sbi->s_resv_clusters, resv_clusters);
Lukas Czerner27dd4382013-04-09 22:11:22 -04003816}
3817
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003818static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003819{
Dan Williams5e405592017-08-24 16:42:48 -07003820 struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04003821 char *orig_data = kstrdup(data, GFP_KERNEL);
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05003822 struct buffer_head *bh, **group_desc;
Mingming Cao617ba132006-10-11 01:20:53 -07003823 struct ext4_super_block *es = NULL;
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05003824 struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08003825 struct flex_groups **flex_groups;
Mingming Cao617ba132006-10-11 01:20:53 -07003826 ext4_fsblk_t block;
3827 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07003828 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003829 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003830 unsigned long journal_devnum = 0;
3831 unsigned long def_mount_opts;
3832 struct inode *root;
Frank Mayhar03901312009-01-07 00:06:22 -05003833 const char *descr;
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04003834 int ret = -ENOMEM;
Theodore Ts'o281b5992011-09-09 18:34:51 -04003835 int blocksize, clustersize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05003836 unsigned int db_count;
3837 unsigned int i;
Kaixu Xiaef5fd682020-04-15 15:25:42 +08003838 int needs_recovery, has_huge_files;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07003839 __u64 blocks_count;
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05003840 int err = 0;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05003841 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003842 ext4_group_t first_not_zeroed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003843
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05003844 if ((data && !orig_data) || !sbi)
3845 goto out_free_base;
Pekka Enberg705895b2009-02-15 18:07:52 -05003846
Colin Ian Kingaed9eb12017-09-05 17:51:23 +01003847 sbi->s_daxdev = dax_dev;
Pekka Enberg705895b2009-02-15 18:07:52 -05003848 sbi->s_blockgroup_lock =
3849 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05003850 if (!sbi->s_blockgroup_lock)
3851 goto out_free_base;
3852
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003853 sb->s_fs_info = sbi;
Theodore Ts'o2c0544b2012-05-30 22:56:46 -04003854 sbi->s_sb = sb;
Theodore Ts'o240799c2008-10-09 23:53:47 -04003855 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07003856 sbi->s_sb_block = sb_block;
Theodore Ts'of613dfc2010-07-27 11:56:08 -04003857 if (sb->s_bdev->bd_part)
3858 sbi->s_sectors_written_start =
Michael Callahandbae2c52018-07-18 04:47:38 -07003859 part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003860
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003861 /* Cleanup superblock name */
Rasmus Villemoesec3904d2015-06-25 15:02:41 -07003862 strreplace(sb->s_id, '/', '!');
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04003863
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05003864 /* -EINVAL is default */
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04003865 ret = -EINVAL;
Mingming Cao617ba132006-10-11 01:20:53 -07003866 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003867 if (!blocksize) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003868 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003869 goto out_fail;
3870 }
3871
3872 /*
Mingming Cao617ba132006-10-11 01:20:53 -07003873 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003874 * block sizes. We need to calculate the offset from buffer start.
3875 */
Mingming Cao617ba132006-10-11 01:20:53 -07003876 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07003877 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3878 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003879 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07003880 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003881 }
3882
Gioh Kima8ac9002014-09-04 22:36:15 -04003883 if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003884 ext4_msg(sb, KERN_ERR, "unable to read superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003885 goto out_fail;
3886 }
3887 /*
3888 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07003889 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003890 */
Theodore Ts'o2716b802012-05-28 17:47:52 -04003891 es = (struct ext4_super_block *) (bh->b_data + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003892 sbi->s_es = es;
3893 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07003894 if (sb->s_magic != EXT4_SUPER_MAGIC)
3895 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05003896 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003897
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003898 /* Warn if metadata_csum and gdt_csum are both set. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003899 if (ext4_has_feature_metadata_csum(sb) &&
3900 ext4_has_feature_gdt_csum(sb))
Jakub Wilk363307e2015-01-02 15:31:14 -05003901 ext4_warning(sb, "metadata_csum and uninit_bg are "
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04003902 "redundant flags; please run fsck.");
3903
Darrick J. Wongd25425f2012-04-29 18:25:10 -04003904 /* Check for a known checksum algorithm */
3905 if (!ext4_verify_csum_type(sb, es)) {
3906 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3907 "unknown checksum algorithm.");
3908 silent = 1;
3909 goto cantfind_ext4;
3910 }
3911
Darrick J. Wong04419842012-04-29 18:27:10 -04003912 /* Load the checksum driver */
Theodore Ts'oa45403b2018-03-29 22:10:31 -04003913 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3914 if (IS_ERR(sbi->s_chksum_driver)) {
3915 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3916 ret = PTR_ERR(sbi->s_chksum_driver);
3917 sbi->s_chksum_driver = NULL;
3918 goto failed_mount;
Darrick J. Wong04419842012-04-29 18:27:10 -04003919 }
3920
Darrick J. Wonga9c473172012-04-29 18:29:10 -04003921 /* Check superblock checksum */
3922 if (!ext4_superblock_csum_verify(sb, es)) {
3923 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3924 "invalid superblock checksum. Run e2fsck?");
3925 silent = 1;
Darrick J. Wong6a797d22015-10-17 16:16:04 -04003926 ret = -EFSBADCRC;
Darrick J. Wonga9c473172012-04-29 18:29:10 -04003927 goto cantfind_ext4;
3928 }
3929
3930 /* Precompute checksum seed for all metadata */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003931 if (ext4_has_feature_csum_seed(sb))
Darrick J. Wong8c81bd82015-10-17 16:16:02 -04003932 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04003933 else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
Darrick J. Wonga9c473172012-04-29 18:29:10 -04003934 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3935 sizeof(es->s_uuid));
3936
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003937 /* Set defaults before we parse the mount options */
3938 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003939 set_opt(sb, INIT_INODE_TABLE);
Mingming Cao617ba132006-10-11 01:20:53 -07003940 if (def_mount_opts & EXT4_DEFM_DEBUG)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003941 set_opt(sb, DEBUG);
Theodore Ts'o87f26802012-03-02 00:03:21 -05003942 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003943 set_opt(sb, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07003944 if (def_mount_opts & EXT4_DEFM_UID16)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003945 set_opt(sb, NO_UID32);
Eric Sandeenea663332011-02-23 17:51:51 -05003946 /* xattr user namespace & acls are now defaulted on */
Eric Sandeenea663332011-02-23 17:51:51 -05003947 set_opt(sb, XATTR_USER);
Theodore Ts'o03010a32008-10-10 20:02:48 -04003948#ifdef CONFIG_EXT4_FS_POSIX_ACL
Eric Sandeenea663332011-02-23 17:51:51 -05003949 set_opt(sb, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08003950#endif
Darrick J. Wong98c1a752014-10-30 10:53:16 -04003951 /* don't forget to enable journal_csum when metadata_csum is enabled. */
3952 if (ext4_has_metadata_csum(sb))
3953 set_opt(sb, JOURNAL_CHECKSUM);
3954
Mingming Cao617ba132006-10-11 01:20:53 -07003955 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003956 set_opt(sb, JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003957 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003958 set_opt(sb, ORDERED_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07003959 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003960 set_opt(sb, WRITEBACK_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003961
Mingming Cao617ba132006-10-11 01:20:53 -07003962 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003963 set_opt(sb, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05003964 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003965 set_opt(sb, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05003966 else
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003967 set_opt(sb, ERRORS_RO);
Darrick J. Wong45f1a9c2014-09-01 21:34:09 -04003968 /* block_validity enabled by default; disable with noblock_validity */
3969 set_opt(sb, BLOCK_VALIDITY);
Theodore Ts'o8b67f042010-08-01 23:14:20 -04003970 if (def_mount_opts & EXT4_DEFM_DISCARD)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003971 set_opt(sb, DISCARD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003972
Eric W. Biederman08cefc72012-02-07 15:41:49 -08003973 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
3974 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
Theodore Ts'o30773842009-01-03 20:27:38 -05003975 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
3976 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
3977 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003978
Theodore Ts'o8b67f042010-08-01 23:14:20 -04003979 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003980 set_opt(sb, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003981
Mingming Cao1e2462f2007-07-18 09:00:55 -04003982 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04003983 * enable delayed allocation by default
3984 * Use -o nodelalloc to turn it off
3985 */
Theodore Ts'obc0b75f2012-09-17 22:54:36 -04003986 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
Theodore Ts'o8b67f042010-08-01 23:14:20 -04003987 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05003988 set_opt(sb, DELALLOC);
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04003989
Lukas Czerner51ce6512011-05-20 13:55:16 -04003990 /*
3991 * set default s_li_wait_mult for lazyinit, for the case there is
3992 * no mount option specified.
3993 */
3994 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
3995
Theodore Ts'o4f97a682020-02-06 17:35:01 -05003996 blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
Ritesh Harjani626b0352020-03-28 01:37:44 +05303997
3998 if (blocksize == PAGE_SIZE)
3999 set_opt(sb, DIOREAD_NOLOCK);
4000
Theodore Ts'o4f97a682020-02-06 17:35:01 -05004001 if (blocksize < EXT4_MIN_BLOCK_SIZE ||
4002 blocksize > EXT4_MAX_BLOCK_SIZE) {
4003 ext4_msg(sb, KERN_ERR,
4004 "Unsupported filesystem blocksize %d (%d log_block_size)",
4005 blocksize, le32_to_cpu(es->s_log_block_size));
4006 goto failed_mount;
4007 }
4008
Theodore Ts'o98033872019-12-15 01:09:03 -05004009 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4010 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4011 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4012 } else {
4013 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4014 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4015 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4016 ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4017 sbi->s_first_ino);
4018 goto failed_mount;
4019 }
4020 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4021 (!is_power_of_2(sbi->s_inode_size)) ||
4022 (sbi->s_inode_size > blocksize)) {
4023 ext4_msg(sb, KERN_ERR,
4024 "unsupported inode size: %d",
4025 sbi->s_inode_size);
Theodore Ts'o4f97a682020-02-06 17:35:01 -05004026 ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
Theodore Ts'o98033872019-12-15 01:09:03 -05004027 goto failed_mount;
4028 }
4029 /*
4030 * i_atime_extra is the last extra field available for
4031 * [acm]times in struct ext4_inode. Checking for that
4032 * field should suffice to ensure we have extra space
4033 * for all three.
4034 */
4035 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4036 sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4037 sb->s_time_gran = 1;
4038 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4039 } else {
4040 sb->s_time_gran = NSEC_PER_SEC;
4041 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4042 }
4043 sb->s_time_min = EXT4_TIMESTAMP_MIN;
4044 }
4045 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4046 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4047 EXT4_GOOD_OLD_INODE_SIZE;
4048 if (ext4_has_feature_extra_isize(sb)) {
4049 unsigned v, max = (sbi->s_inode_size -
4050 EXT4_GOOD_OLD_INODE_SIZE);
4051
4052 v = le16_to_cpu(es->s_want_extra_isize);
4053 if (v > max) {
4054 ext4_msg(sb, KERN_ERR,
4055 "bad s_want_extra_isize: %d", v);
4056 goto failed_mount;
4057 }
4058 if (sbi->s_want_extra_isize < v)
4059 sbi->s_want_extra_isize = v;
4060
4061 v = le16_to_cpu(es->s_min_extra_isize);
4062 if (v > max) {
4063 ext4_msg(sb, KERN_ERR,
4064 "bad s_min_extra_isize: %d", v);
4065 goto failed_mount;
4066 }
4067 if (sbi->s_want_extra_isize < v)
4068 sbi->s_want_extra_isize = v;
4069 }
4070 }
4071
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004072 if (sbi->s_es->s_mount_opts[0]) {
4073 char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
4074 sizeof(sbi->s_es->s_mount_opts),
4075 GFP_KERNEL);
4076 if (!s_mount_opts)
4077 goto failed_mount;
4078 if (!parse_options(s_mount_opts, sb, &journal_devnum,
4079 &journal_ioprio, 0)) {
4080 ext4_msg(sb, KERN_WARNING,
4081 "failed to parse options in superblock: %s",
4082 s_mount_opts);
4083 }
4084 kfree(s_mount_opts);
Theodore Ts'o8b67f042010-08-01 23:14:20 -04004085 }
Theodore Ts'o5a916be2012-03-04 19:27:31 -05004086 sbi->s_def_mount_opt = sbi->s_mount_opt;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05004087 if (!parse_options((char *) data, sb, &journal_devnum,
Eric Sandeen661aa522012-02-20 17:53:04 -05004088 &journal_ioprio, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004089 goto failed_mount;
4090
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04004091#ifdef CONFIG_UNICODE
4092 if (ext4_has_feature_casefold(sb) && !sbi->s_encoding) {
4093 const struct ext4_sb_encodings *encoding_info;
4094 struct unicode_map *encoding;
4095 __u16 encoding_flags;
4096
4097 if (ext4_has_feature_encrypt(sb)) {
4098 ext4_msg(sb, KERN_ERR,
4099 "Can't mount with encoding and encryption");
4100 goto failed_mount;
4101 }
4102
4103 if (ext4_sb_read_encoding(es, &encoding_info,
4104 &encoding_flags)) {
4105 ext4_msg(sb, KERN_ERR,
4106 "Encoding requested by superblock is unknown");
4107 goto failed_mount;
4108 }
4109
4110 encoding = utf8_load(encoding_info->version);
4111 if (IS_ERR(encoding)) {
4112 ext4_msg(sb, KERN_ERR,
4113 "can't mount with superblock charset: %s-%s "
4114 "not supported by the kernel. flags: 0x%x.",
4115 encoding_info->name, encoding_info->version,
4116 encoding_flags);
4117 goto failed_mount;
4118 }
4119 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4120 "%s-%s with flags 0x%hx", encoding_info->name,
4121 encoding_info->version?:"\b", encoding_flags);
4122
4123 sbi->s_encoding = encoding;
4124 sbi->s_encoding_flags = encoding_flags;
4125 }
4126#endif
4127
Theodore Ts'o56889782011-09-03 18:22:38 -04004128 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
Theodore Ts'o244adf62020-01-23 12:23:17 -05004129 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, and O_DIRECT support!\n");
Kaixu Xia781c0362020-04-13 12:24:22 +08004130 /* can't mount with both data=journal and dioread_nolock. */
Theodore Ts'o244adf62020-01-23 12:23:17 -05004131 clear_opt(sb, DIOREAD_NOLOCK);
Theodore Ts'o56889782011-09-03 18:22:38 -04004132 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4133 ext4_msg(sb, KERN_ERR, "can't mount with "
4134 "both data=journal and delalloc");
4135 goto failed_mount;
4136 }
Ira Weinyfc626fe2020-05-28 07:59:57 -07004137 if (test_opt(sb, DAX_ALWAYS)) {
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004138 ext4_msg(sb, KERN_ERR, "can't mount with "
4139 "both data=journal and dax");
4140 goto failed_mount;
4141 }
Sergey Karamov73b92a22016-12-10 17:54:58 -05004142 if (ext4_has_feature_encrypt(sb)) {
4143 ext4_msg(sb, KERN_WARNING,
4144 "encrypted files will use data=ordered "
4145 "instead of data journaling mode");
4146 }
Theodore Ts'o56889782011-09-03 18:22:38 -04004147 if (test_opt(sb, DELALLOC))
4148 clear_opt(sb, DELALLOC);
Tejun Heo001e4a82015-07-21 23:51:26 -04004149 } else {
4150 sb->s_iflags |= SB_I_CGROUPWB;
Theodore Ts'o56889782011-09-03 18:22:38 -04004151 }
4152
Linus Torvalds1751e8a2017-11-27 13:05:09 -08004153 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4154 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004155
Mingming Cao617ba132006-10-11 01:20:53 -07004156 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004157 (ext4_has_compat_features(sb) ||
4158 ext4_has_ro_compat_features(sb) ||
4159 ext4_has_incompat_features(sb)))
Eric Sandeenb31e1552009-06-04 17:36:36 -04004160 ext4_msg(sb, KERN_WARNING,
4161 "feature flags set on rev 0 fs, "
4162 "running e2fsck is recommended");
Theodore Tso469108f2008-02-10 01:11:44 -05004163
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004164 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4165 set_opt2(sb, HURD_COMPAT);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004166 if (ext4_has_feature_64bit(sb)) {
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004167 ext4_msg(sb, KERN_ERR,
4168 "The Hurd can't support 64-bit file systems");
4169 goto failed_mount;
4170 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004171
4172 /*
4173 * ea_inode feature uses l_i_version field which is not
4174 * available in HURD_COMPAT mode.
4175 */
4176 if (ext4_has_feature_ea_inode(sb)) {
4177 ext4_msg(sb, KERN_ERR,
4178 "ea_inode feature is not supported for Hurd");
4179 goto failed_mount;
4180 }
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004181 }
4182
Theodore Ts'o2035e772011-04-18 17:29:14 -04004183 if (IS_EXT2_SB(sb)) {
4184 if (ext2_feature_set_ok(sb))
4185 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4186 "using the ext4 subsystem");
4187 else {
Eric Sandeen0d9366d2018-03-22 11:59:00 -04004188 /*
4189 * If we're probing be silent, if this looks like
4190 * it's actually an ext[34] filesystem.
4191 */
4192 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4193 goto failed_mount;
Theodore Ts'o2035e772011-04-18 17:29:14 -04004194 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4195 "to feature incompatibilities");
4196 goto failed_mount;
4197 }
4198 }
4199
4200 if (IS_EXT3_SB(sb)) {
4201 if (ext3_feature_set_ok(sb))
4202 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4203 "using the ext4 subsystem");
4204 else {
Eric Sandeen0d9366d2018-03-22 11:59:00 -04004205 /*
4206 * If we're probing be silent, if this looks like
4207 * it's actually an ext4 filesystem.
4208 */
4209 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4210 goto failed_mount;
Theodore Ts'o2035e772011-04-18 17:29:14 -04004211 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4212 "to feature incompatibilities");
4213 goto failed_mount;
4214 }
4215 }
4216
Theodore Tso469108f2008-02-10 01:11:44 -05004217 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004218 * Check feature flags regardless of the revision level, since we
4219 * previously didn't change the revision level when setting the flags,
4220 * so there is a chance incompat flags are set on a rev 0 filesystem.
4221 */
David Howellsbc98a422017-07-17 08:45:34 +01004222 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004223 goto failed_mount;
Eric Sandeena13fb1a2009-08-18 00:20:23 -04004224
Theodore Ts'o8cdf3372016-11-18 13:00:24 -05004225 if (le32_to_cpu(es->s_log_block_size) >
4226 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4227 ext4_msg(sb, KERN_ERR,
4228 "Invalid log block size: %u",
4229 le32_to_cpu(es->s_log_block_size));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004230 goto failed_mount;
4231 }
Theodore Ts'obfe0a5f2018-06-17 18:11:20 -04004232 if (le32_to_cpu(es->s_log_cluster_size) >
4233 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4234 ext4_msg(sb, KERN_ERR,
4235 "Invalid log cluster size: %u",
4236 le32_to_cpu(es->s_log_cluster_size));
4237 goto failed_mount;
4238 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004239
Theodore Ts'o5b9554d2016-07-05 20:01:52 -04004240 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
4241 ext4_msg(sb, KERN_ERR,
4242 "Number of reserved GDT blocks insanely large: %d",
4243 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4244 goto failed_mount;
4245 }
4246
Ira Weinya8ab6d32020-05-28 07:59:58 -07004247 if (bdev_dax_supported(sb->s_bdev, blocksize))
4248 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4249
Ira Weinyfc626fe2020-05-28 07:59:57 -07004250 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
Ross Zwisler559db4c2017-10-12 11:52:34 -04004251 if (ext4_has_feature_inline_data(sb)) {
4252 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4253 " that may contain inline data");
Eric Sandeen361d24d2018-12-04 00:46:39 -05004254 goto failed_mount;
Ross Zwisler559db4c2017-10-12 11:52:34 -04004255 }
Ira Weinya8ab6d32020-05-28 07:59:58 -07004256 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
Dan Williams24f34782017-12-21 17:04:07 -08004257 ext4_msg(sb, KERN_ERR,
Eric Sandeen361d24d2018-12-04 00:46:39 -05004258 "DAX unsupported by block device.");
4259 goto failed_mount;
Dan Williams24f34782017-12-21 17:04:07 -08004260 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004261 }
4262
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004263 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04004264 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4265 es->s_encryption_level);
4266 goto failed_mount;
4267 }
4268
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004269 if (sb->s_blocksize != blocksize) {
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05004270 /* Validate the filesystem blocksize */
4271 if (!sb_set_blocksize(sb, blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004272 ext4_msg(sb, KERN_ERR, "bad block size %d",
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05004273 blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004274 goto failed_mount;
4275 }
4276
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004277 brelse(bh);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004278 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
4279 offset = do_div(logical_sb_block, blocksize);
Gioh Kima8ac9002014-09-04 22:36:15 -04004280 bh = sb_bread_unmovable(sb, logical_sb_block);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004281 if (!bh) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004282 ext4_msg(sb, KERN_ERR,
4283 "Can't read superblock on 2nd try");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004284 goto failed_mount;
4285 }
Theodore Ts'o2716b802012-05-28 17:47:52 -04004286 es = (struct ext4_super_block *)(bh->b_data + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004287 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07004288 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004289 ext4_msg(sb, KERN_ERR,
4290 "Magic mismatch, very weird!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004291 goto failed_mount;
4292 }
4293 }
4294
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004295 has_huge_files = ext4_has_feature_huge_file(sb);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004296 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4297 has_huge_files);
4298 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004299
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004300 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004301 if (ext4_has_feature_64bit(sb)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07004302 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004303 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07004304 !is_power_of_2(sbi->s_desc_size)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004305 ext4_msg(sb, KERN_ERR,
4306 "unsupported descriptor size %lu",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004307 sbi->s_desc_size);
4308 goto failed_mount;
4309 }
4310 } else
4311 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004312
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004313 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004314 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004315
Mingming Cao617ba132006-10-11 01:20:53 -07004316 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004317 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07004318 goto cantfind_ext4;
Theodore Ts'ocd6bb352016-11-18 13:28:30 -05004319 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4320 sbi->s_inodes_per_group > blocksize * 8) {
4321 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
Josh Triplettb9c538d2020-03-28 15:34:15 -07004322 sbi->s_inodes_per_group);
Theodore Ts'ocd6bb352016-11-18 13:28:30 -05004323 goto failed_mount;
4324 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004325 sbi->s_itb_per_group = sbi->s_inodes_per_group /
4326 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004327 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004328 sbi->s_sbh = bh;
4329 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07004330 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4331 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004332
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004333 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004334 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4335 sbi->s_def_hash_version = es->s_def_hash_version;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004336 if (ext4_has_feature_dir_index(sb)) {
Theodore Ts'o23301412014-02-12 12:16:04 -05004337 i = le32_to_cpu(es->s_flags);
4338 if (i & EXT2_FLAGS_UNSIGNED_HASH)
4339 sbi->s_hash_unsigned = 3;
4340 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
Theodore Ts'of99b2582008-10-28 13:21:44 -04004341#ifdef __CHAR_UNSIGNED__
David Howellsbc98a422017-07-17 08:45:34 +01004342 if (!sb_rdonly(sb))
Theodore Ts'o23301412014-02-12 12:16:04 -05004343 es->s_flags |=
4344 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4345 sbi->s_hash_unsigned = 3;
Theodore Ts'of99b2582008-10-28 13:21:44 -04004346#else
David Howellsbc98a422017-07-17 08:45:34 +01004347 if (!sb_rdonly(sb))
Theodore Ts'o23301412014-02-12 12:16:04 -05004348 es->s_flags |=
4349 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
Theodore Ts'of99b2582008-10-28 13:21:44 -04004350#endif
Theodore Ts'o23301412014-02-12 12:16:04 -05004351 }
Theodore Ts'of99b2582008-10-28 13:21:44 -04004352 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004353
Theodore Ts'o281b5992011-09-09 18:34:51 -04004354 /* Handle clustersize */
4355 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
Kaixu Xiaef5fd682020-04-15 15:25:42 +08004356 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'o281b5992011-09-09 18:34:51 -04004357 if (clustersize < blocksize) {
4358 ext4_msg(sb, KERN_ERR,
4359 "cluster size (%d) smaller than "
4360 "block size (%d)", clustersize, blocksize);
4361 goto failed_mount;
4362 }
4363 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4364 le32_to_cpu(es->s_log_block_size);
4365 sbi->s_clusters_per_group =
4366 le32_to_cpu(es->s_clusters_per_group);
4367 if (sbi->s_clusters_per_group > blocksize * 8) {
4368 ext4_msg(sb, KERN_ERR,
4369 "#clusters per group too big: %lu",
4370 sbi->s_clusters_per_group);
4371 goto failed_mount;
4372 }
4373 if (sbi->s_blocks_per_group !=
4374 (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4375 ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4376 "clusters per group (%lu) inconsistent",
4377 sbi->s_blocks_per_group,
4378 sbi->s_clusters_per_group);
4379 goto failed_mount;
4380 }
4381 } else {
4382 if (clustersize != blocksize) {
Theodore Ts'obfe0a5f2018-06-17 18:11:20 -04004383 ext4_msg(sb, KERN_ERR,
4384 "fragment/cluster size (%d) != "
4385 "block size (%d)", clustersize, blocksize);
4386 goto failed_mount;
Theodore Ts'o281b5992011-09-09 18:34:51 -04004387 }
4388 if (sbi->s_blocks_per_group > blocksize * 8) {
4389 ext4_msg(sb, KERN_ERR,
4390 "#blocks per group too big: %lu",
4391 sbi->s_blocks_per_group);
4392 goto failed_mount;
4393 }
4394 sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4395 sbi->s_cluster_bits = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004396 }
Theodore Ts'o281b5992011-09-09 18:34:51 -04004397 sbi->s_cluster_ratio = clustersize / blocksize;
4398
Theodore Ts'o960fd8562013-07-05 23:11:16 -04004399 /* Do we have standard group size of clustersize * 8 blocks ? */
4400 if (sbi->s_blocks_per_group == clustersize << 3)
4401 set_opt2(sb, STD_GROUP_SIZE);
4402
Eric Sandeenbf43d842009-08-17 23:48:51 -04004403 /*
4404 * Test whether we have more sectors than will fit in sector_t,
4405 * and whether the max offset is addressable by the page cache.
4406 */
Darrick J. Wong5a9ae682010-11-19 09:56:44 -05004407 err = generic_check_addressable(sb->s_blocksize_bits,
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07004408 ext4_blocks_count(es));
Darrick J. Wong5a9ae682010-11-19 09:56:44 -05004409 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004410 ext4_msg(sb, KERN_ERR, "filesystem"
Eric Sandeenbf43d842009-08-17 23:48:51 -04004411 " too large to mount safely on this system");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004412 goto failed_mount;
4413 }
4414
Mingming Cao617ba132006-10-11 01:20:53 -07004415 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4416 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05004417
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04004418 /* check blocks count against device size */
4419 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
4420 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004421 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4422 "exceeds size of device (%llu blocks)",
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04004423 ext4_blocks_count(es), blocks_count);
4424 goto failed_mount;
4425 }
4426
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004427 /*
4428 * It makes no sense for the first data block to be beyond the end
4429 * of the filesystem.
4430 */
4431 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
Zheng Liu5635a622011-12-18 16:13:58 -05004432 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
Eric Sandeenb31e1552009-06-04 17:36:36 -04004433 "block %u is beyond end of filesystem (%llu)",
4434 le32_to_cpu(es->s_first_data_block),
4435 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05004436 goto failed_mount;
4437 }
Theodore Ts'obfe0a5f2018-06-17 18:11:20 -04004438 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4439 (sbi->s_cluster_ratio == 1)) {
4440 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4441 "block is 0 with a 1k block and cluster size");
4442 goto failed_mount;
4443 }
4444
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07004445 blocks_count = (ext4_blocks_count(es) -
4446 le32_to_cpu(es->s_first_data_block) +
4447 EXT4_BLOCKS_PER_GROUP(sb) - 1);
4448 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004449 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
Josh Triplettdf414602020-03-28 14:54:01 -07004450 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004451 "(block count %llu, first data block %u, "
Josh Triplettdf414602020-03-28 14:54:01 -07004452 "blocks per group %lu)", blocks_count,
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004453 ext4_blocks_count(es),
4454 le32_to_cpu(es->s_first_data_block),
4455 EXT4_BLOCKS_PER_GROUP(sb));
4456 goto failed_mount;
4457 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07004458 sbi->s_groups_count = blocks_count;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04004459 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4460 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Theodore Ts'o9e463082018-11-07 10:32:53 -05004461 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4462 le32_to_cpu(es->s_inodes_count)) {
4463 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4464 le32_to_cpu(es->s_inodes_count),
4465 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4466 ret = -EINVAL;
4467 goto failed_mount;
4468 }
Mingming Cao617ba132006-10-11 01:20:53 -07004469 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4470 EXT4_DESC_PER_BLOCK(sb);
Eryu Guan3a4b77c2016-12-01 15:08:37 -05004471 if (ext4_has_feature_meta_bg(sb)) {
Theodore Ts'o2ba3e6e2017-02-15 01:26:39 -05004472 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
Eryu Guan3a4b77c2016-12-01 15:08:37 -05004473 ext4_msg(sb, KERN_WARNING,
4474 "first meta block group too large: %u "
4475 "(group descriptor block count %u)",
4476 le32_to_cpu(es->s_first_meta_bg), db_count);
4477 goto failed_mount;
4478 }
4479 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004480 rcu_assign_pointer(sbi->s_group_desc,
4481 kvmalloc_array(db_count,
4482 sizeof(struct buffer_head *),
4483 GFP_KERNEL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004484 if (sbi->s_group_desc == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004485 ext4_msg(sb, KERN_ERR, "not enough memory");
Theodore Ts'o2cde4172012-05-28 17:49:54 -04004486 ret = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004487 goto failed_mount;
4488 }
4489
Pekka Enberg705895b2009-02-15 18:07:52 -05004490 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004491
Andrew Perepechko85c8f172017-04-30 00:46:35 -04004492 /* Pre-read the descriptors into the buffer cache */
4493 for (i = 0; i < db_count; i++) {
4494 block = descriptor_loc(sb, logical_sb_block, i);
Roman Gushchind87f6392020-02-28 16:14:11 -08004495 sb_breadahead_unmovable(sb, block);
Andrew Perepechko85c8f172017-04-30 00:46:35 -04004496 }
4497
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004498 for (i = 0; i < db_count; i++) {
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004499 struct buffer_head *bh;
4500
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004501 block = descriptor_loc(sb, logical_sb_block, i);
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004502 bh = sb_bread_unmovable(sb, block);
4503 if (!bh) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004504 ext4_msg(sb, KERN_ERR,
4505 "can't read group descriptor %d", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004506 db_count = i;
4507 goto failed_mount2;
4508 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004509 rcu_read_lock();
4510 rcu_dereference(sbi->s_group_desc)[i] = bh;
4511 rcu_read_unlock();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004512 }
Theodore Ts'o44de0222018-07-08 19:35:02 -04004513 sbi->s_gdb_count = db_count;
Theodore Ts'o829fa702016-08-01 00:51:02 -04004514 if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004515 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004516 ret = -EFSCORRUPTED;
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004517 goto failed_mount2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004518 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004519
Kees Cook235699a2017-10-18 12:45:17 -04004520 timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
Tao Ma04496412011-04-05 19:55:28 -04004521
Dmitry Monakhova75ae78f2013-04-03 22:10:52 -04004522 /* Register extent status tree shrinker */
Zheng Liueb68d0e2014-09-01 22:26:49 -04004523 if (ext4_es_register_shrinker(sbi))
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004524 goto failed_mount3;
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004525
Alex Tomasc9de5602008-01-29 00:19:52 -05004526 sbi->s_stripe = ext4_get_stripe_size(sbi);
Zheng Liu67a5da52012-08-17 09:54:17 -04004527 sbi->s_extent_max_zeroout_kb = 32;
Alex Tomasc9de5602008-01-29 00:19:52 -05004528
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004529 /*
4530 * set up enough so that it can read an inode
4531 */
Theodore Ts'of6e63f92014-09-18 17:12:30 -04004532 sb->s_op = &ext4_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07004533 sb->s_export_op = &ext4_export_ops;
4534 sb->s_xattr = ext4_xattr_handlers;
Chandan Rajendra643fa962018-12-12 15:20:12 +05304535#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04004536 sb->s_cop = &ext4_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07004537#endif
Eric Biggersc93d8f82019-07-22 09:26:24 -07004538#ifdef CONFIG_FS_VERITY
4539 sb->s_vop = &ext4_verityops;
4540#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004541#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07004542 sb->dq_op = &ext4_quota_operations;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004543 if (ext4_has_feature_quota(sb))
Jan Kara1fa5efe2014-10-08 18:26:54 +02004544 sb->s_qcop = &dquot_quotactl_sysfile_ops;
Jan Kara262b4662013-03-02 17:57:08 -05004545 else
4546 sb->s_qcop = &ext4_qctl_operations;
Li Xi689c9582016-01-08 16:01:22 -05004547 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004548#endif
Christoph Hellwig85787092017-05-10 15:06:33 +02004549 memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
Aneesh Kumar K.Vf2fa2ff2011-01-29 18:43:40 +05304550
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004551 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04004552 mutex_init(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004553
4554 sb->s_root = NULL;
4555
4556 needs_recovery = (es->s_last_orphan != 0 ||
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004557 ext4_has_feature_journal_needs_recovery(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004558
David Howellsbc98a422017-07-17 08:45:34 +01004559 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
Johann Lombardic5e06d12011-05-24 18:31:25 -04004560 if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
Darrick J. Wong50460fe2014-10-30 10:53:16 -04004561 goto failed_mount3a;
Johann Lombardic5e06d12011-05-24 18:31:25 -04004562
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004563 /*
4564 * The first inode we look at is the journal inode. Don't try
4565 * root first: it may be modified in the journal!
4566 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004567 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
Theodore Ts'o4753d8a2017-02-05 01:26:48 -05004568 err = ext4_load_journal(sb, es, journal_devnum);
4569 if (err)
Darrick J. Wong50460fe2014-10-30 10:53:16 -04004570 goto failed_mount3a;
David Howellsbc98a422017-07-17 08:45:34 +01004571 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004572 ext4_has_feature_journal_needs_recovery(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004573 ext4_msg(sb, KERN_ERR, "required journal recovery "
4574 "suppressed and not mounted read-only");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004575 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004576 } else {
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04004577 /* Nojournal mode, all journal mount options are illegal */
4578 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
4579 ext4_msg(sb, KERN_ERR, "can't mount with "
4580 "journal_checksum, fs mounted w/o journal");
4581 goto failed_mount_wq;
4582 }
4583 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4584 ext4_msg(sb, KERN_ERR, "can't mount with "
4585 "journal_async_commit, fs mounted w/o journal");
4586 goto failed_mount_wq;
4587 }
4588 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
4589 ext4_msg(sb, KERN_ERR, "can't mount with "
4590 "commit=%lu, fs mounted w/o journal",
4591 sbi->s_commit_interval / HZ);
4592 goto failed_mount_wq;
4593 }
4594 if (EXT4_MOUNT_DATA_FLAGS &
4595 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
4596 ext4_msg(sb, KERN_ERR, "can't mount with "
4597 "data=, fs mounted w/o journal");
4598 goto failed_mount_wq;
4599 }
Debabrata Banerjee50b29d82019-04-30 23:08:15 -04004600 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04004601 clear_opt(sb, JOURNAL_CHECKSUM);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004602 clear_opt(sb, DATA_FLAGS);
Frank Mayhar03901312009-01-07 00:06:22 -05004603 sbi->s_journal = NULL;
4604 needs_recovery = 0;
4605 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004606 }
4607
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004608 if (ext4_has_feature_64bit(sb) &&
Jose R. Santoseb40a092007-07-18 08:37:25 -04004609 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4610 JBD2_FEATURE_INCOMPAT_64BIT)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004611 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004612 goto failed_mount_wq;
Jose R. Santoseb40a092007-07-18 08:37:25 -04004613 }
4614
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04004615 if (!set_journal_csum_feature_set(sb)) {
4616 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4617 "feature set");
4618 goto failed_mount_wq;
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08004619 }
Girish Shilamkar818d2762008-01-28 23:58:27 -05004620
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004621 /* We have now updated the journal if required, so we can
4622 * validate the data journaling mode. */
4623 switch (test_opt(sb, DATA_FLAGS)) {
4624 case 0:
4625 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07004626 * capabilities: ORDERED_DATA if the journal can
4627 * cope, else JOURNAL_DATA
4628 */
Mingming Caodab291a2006-10-11 01:21:01 -07004629 if (jbd2_journal_check_available_features
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004630 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004631 set_opt(sb, ORDERED_DATA);
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004632 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4633 } else {
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004634 set_opt(sb, JOURNAL_DATA);
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004635 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4636 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004637 break;
4638
Mingming Cao617ba132006-10-11 01:20:53 -07004639 case EXT4_MOUNT_ORDERED_DATA:
4640 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07004641 if (!jbd2_journal_check_available_features
4642 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004643 ext4_msg(sb, KERN_ERR, "Journal does not support "
4644 "requested data journaling mode");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004645 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004646 }
4647 default:
4648 break;
4649 }
Jan Karaab04df72016-12-03 16:20:53 -05004650
4651 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4652 test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4653 ext4_msg(sb, KERN_ERR, "can't mount with "
4654 "journal_async_commit in data=ordered mode");
4655 goto failed_mount_wq;
4656 }
4657
Theodore Ts'ob3881f72009-01-05 22:46:26 -05004658 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004659
Bobi Jam18aadd42012-02-20 17:53:02 -05004660 sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
4661
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004662no_journal:
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004663 if (!test_opt(sb, NO_MBCACHE)) {
4664 sbi->s_ea_block_cache = ext4_xattr_create_cache();
4665 if (!sbi->s_ea_block_cache) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004666 ext4_msg(sb, KERN_ERR,
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004667 "Failed to create ea_block_cache");
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004668 goto failed_mount_wq;
4669 }
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004670
4671 if (ext4_has_feature_ea_inode(sb)) {
4672 sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4673 if (!sbi->s_ea_inode_cache) {
4674 ext4_msg(sb, KERN_ERR,
4675 "Failed to create ea_inode_cache");
4676 goto failed_mount_wq;
4677 }
4678 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04004679 }
4680
Eric Biggersc93d8f82019-07-22 09:26:24 -07004681 if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4682 ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4683 goto failed_mount_wq;
4684 }
4685
David Howellsbc98a422017-07-17 08:45:34 +01004686 if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004687 !ext4_has_feature_encrypt(sb)) {
4688 ext4_set_feature_encrypt(sb);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04004689 ext4_commit_super(sb, 1);
4690 }
4691
Tejun Heofd89d5f2011-02-01 11:42:42 +01004692 /*
Theodore Ts'o952fc182012-07-09 16:27:05 -04004693 * Get the # of file system overhead blocks from the
4694 * superblock if present.
4695 */
4696 if (es->s_overhead_clusters)
4697 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4698 else {
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004699 err = ext4_calculate_overhead(sb);
4700 if (err)
Theodore Ts'o952fc182012-07-09 16:27:05 -04004701 goto failed_mount_wq;
4702 }
4703
4704 /*
Tejun Heofd89d5f2011-02-01 11:42:42 +01004705 * The maximum number of concurrent works can be high and
4706 * concurrency isn't really necessary. Limit it to 1.
4707 */
Jan Kara2e8fa542013-06-04 14:21:02 -04004708 EXT4_SB(sb)->rsv_conversion_wq =
4709 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4710 if (!EXT4_SB(sb)->rsv_conversion_wq) {
4711 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004712 ret = -ENOMEM;
Jan Kara2e8fa542013-06-04 14:21:02 -04004713 goto failed_mount4;
4714 }
4715
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004716 /*
Mingming Caodab291a2006-10-11 01:21:01 -07004717 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004718 * so we can safely mount the rest of the filesystem now.
4719 */
4720
Theodore Ts'o8a363972018-12-19 12:29:13 -05004721 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08004722 if (IS_ERR(root)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004723 ext4_msg(sb, KERN_ERR, "get root inode failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08004724 ret = PTR_ERR(root);
Manish Katiyar32a9bb52011-02-27 20:42:06 -05004725 root = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004726 goto failed_mount4;
4727 }
4728 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004729 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
Al Viro94bf6082012-01-09 15:53:24 -05004730 iput(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004731 goto failed_mount4;
4732 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004733
4734#ifdef CONFIG_UNICODE
4735 if (sbi->s_encoding)
4736 sb->s_d_op = &ext4_dentry_ops;
4737#endif
4738
Al Viro48fde702012-01-08 22:15:13 -05004739 sb->s_root = d_make_root(root);
David Howells1d1fe1e2008-02-07 00:15:37 -08004740 if (!sb->s_root) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004741 ext4_msg(sb, KERN_ERR, "get root dentry failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08004742 ret = -ENOMEM;
4743 goto failed_mount4;
4744 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004745
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04004746 ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4747 if (ret == -EROFS) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08004748 sb->s_flags |= SB_RDONLY;
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04004749 ret = 0;
4750 } else if (ret)
4751 goto failed_mount4a;
Kalpak Shahef7f3832007-07-18 09:15:20 -04004752
Theodore Ts'ob5799012015-09-23 12:44:17 -04004753 ext4_set_resv_clusters(sb);
Lukas Czerner27dd4382013-04-09 22:11:22 -04004754
Jan Kara0f5bde12020-07-28 15:04:37 +02004755 if (test_opt(sb, BLOCK_VALIDITY)) {
4756 err = ext4_setup_system_zone(sb);
4757 if (err) {
4758 ext4_msg(sb, KERN_ERR, "failed to initialize system "
4759 "zone (%d)", err);
4760 goto failed_mount4a;
4761 }
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004762 }
4763
4764 ext4_ext_init(sb);
4765 err = ext4_mb_init(sb);
4766 if (err) {
4767 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4768 err);
Tao Madcf2d802011-10-06 12:10:11 -04004769 goto failed_mount5;
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04004770 }
4771
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004772 block = ext4_count_free_clusters(sb);
4773 ext4_free_blocks_count_set(sbi->s_es,
4774 EXT4_C2B(sbi, block));
Theodore Ts'o4274f512018-09-01 14:42:14 -04004775 ext4_superblock_csum_set(sb);
Tejun Heo908c7f12014-09-08 09:51:29 +09004776 err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4777 GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004778 if (!err) {
4779 unsigned long freei = ext4_count_free_inodes(sb);
4780 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
Theodore Ts'o4274f512018-09-01 14:42:14 -04004781 ext4_superblock_csum_set(sb);
Tejun Heo908c7f12014-09-08 09:51:29 +09004782 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4783 GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004784 }
4785 if (!err)
4786 err = percpu_counter_init(&sbi->s_dirs_counter,
Tejun Heo908c7f12014-09-08 09:51:29 +09004787 ext4_count_dirs(sb), GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004788 if (!err)
Tejun Heo908c7f12014-09-08 09:51:29 +09004789 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4790 GFP_KERNEL);
Daeho Jeongc8585c62016-04-25 23:22:35 -04004791 if (!err)
Eric Biggersbbd55932020-02-19 10:30:46 -08004792 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
Daeho Jeongc8585c62016-04-25 23:22:35 -04004793
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004794 if (err) {
4795 ext4_msg(sb, KERN_ERR, "insufficient memory");
4796 goto failed_mount6;
4797 }
4798
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004799 if (ext4_has_feature_flex_bg(sb))
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004800 if (!ext4_fill_flex_info(sb)) {
4801 ext4_msg(sb, KERN_ERR,
4802 "unable to initialize "
4803 "flex_bg meta info!");
4804 goto failed_mount6;
4805 }
4806
Lukas Czernerbfff6872010-10-27 21:30:05 -04004807 err = ext4_register_li_request(sb, first_not_zeroed);
4808 if (err)
Tao Madcf2d802011-10-06 12:10:11 -04004809 goto failed_mount6;
Lukas Czernerbfff6872010-10-27 21:30:05 -04004810
Theodore Ts'ob5799012015-09-23 12:44:17 -04004811 err = ext4_register_sysfs(sb);
Tao Madcf2d802011-10-06 12:10:11 -04004812 if (err)
4813 goto failed_mount7;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04004814
Jan Kara9b2ff3572013-03-02 18:22:38 -05004815#ifdef CONFIG_QUOTA
4816 /* Enable quota usage during mount. */
David Howellsbc98a422017-07-17 08:45:34 +01004817 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
Jan Kara9b2ff3572013-03-02 18:22:38 -05004818 err = ext4_enable_quotas(sb);
4819 if (err)
4820 goto failed_mount8;
4821 }
4822#endif /* CONFIG_QUOTA */
4823
zhangyi (F)bc717262020-06-20 10:54:23 +08004824 /*
4825 * Save the original bdev mapping's wb_err value which could be
4826 * used to detect the metadata async write error.
4827 */
4828 spin_lock_init(&sbi->s_bdev_wb_lock);
Zhang Xiaoxu9704a322020-09-27 22:05:56 -04004829 errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
4830 &sbi->s_bdev_wb_err);
zhangyi (F)bc717262020-06-20 10:54:23 +08004831 sb->s_bdev->bd_super = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07004832 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4833 ext4_orphan_cleanup(sb, es);
4834 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05004835 if (needs_recovery) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004836 ext4_msg(sb, KERN_INFO, "recovery complete");
Jan Kara11215632020-07-10 16:07:59 +02004837 err = ext4_mark_recovery_complete(sb, es);
4838 if (err)
4839 goto failed_mount8;
Frank Mayhar03901312009-01-07 00:06:22 -05004840 }
4841 if (EXT4_SB(sb)->s_journal) {
4842 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
4843 descr = " journalled data mode";
4844 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4845 descr = " ordered data mode";
4846 else
4847 descr = " writeback data mode";
4848 } else
4849 descr = "out journal";
4850
Lukas Czerner79add3a2012-11-08 13:28:29 -05004851 if (test_opt(sb, DISCARD)) {
4852 struct request_queue *q = bdev_get_queue(sb->s_bdev);
4853 if (!blk_queue_discard(q))
4854 ext4_msg(sb, KERN_WARNING,
4855 "mounting with \"discard\" option, but "
4856 "the device does not support discard");
4857 }
4858
Theodore Ts'oe294a532015-08-15 14:59:44 -04004859 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
4860 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004861 "Opts: %.*s%s%s", descr,
4862 (int) sizeof(sbi->s_es->s_mount_opts),
4863 sbi->s_es->s_mount_opts,
Theodore Ts'oe294a532015-08-15 14:59:44 -04004864 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004865
Theodore Ts'o66e61a92010-07-27 11:56:04 -04004866 if (es->s_error_count)
4867 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004868
Theodore Ts'oefbed4d2013-10-17 21:11:01 -04004869 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4870 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4871 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4872 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +00004873 atomic_set(&sbi->s_warning_count, 0);
4874 atomic_set(&sbi->s_msg_count, 0);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -04004875
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04004876 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004877 return 0;
4878
Mingming Cao617ba132006-10-11 01:20:53 -07004879cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004880 if (!silent)
Eric Sandeenb31e1552009-06-04 17:36:36 -04004881 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004882 goto failed_mount;
4883
Theodore Ts'o72ba7452013-01-24 23:24:54 -05004884failed_mount8:
Theodore Ts'oebd173b2015-09-23 12:46:17 -04004885 ext4_unregister_sysfs(sb);
Eric Biggerscb8d53d2020-09-22 09:24:56 -07004886 kobject_put(&sbi->s_kobj);
Tao Madcf2d802011-10-06 12:10:11 -04004887failed_mount7:
4888 ext4_unregister_li_request(sb);
4889failed_mount6:
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004890 ext4_mb_release(sb);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08004891 rcu_read_lock();
4892 flex_groups = rcu_dereference(sbi->s_flex_groups);
4893 if (flex_groups) {
4894 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
4895 kvfree(flex_groups[i]);
4896 kvfree(flex_groups);
4897 }
4898 rcu_read_unlock();
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04004899 percpu_counter_destroy(&sbi->s_freeclusters_counter);
4900 percpu_counter_destroy(&sbi->s_freeinodes_counter);
4901 percpu_counter_destroy(&sbi->s_dirs_counter);
4902 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
Eric Biggersbbd55932020-02-19 10:30:46 -08004903 percpu_free_rwsem(&sbi->s_writepages_rwsem);
Azat Khuzhin00764932014-04-07 10:54:20 -04004904failed_mount5:
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004905 ext4_ext_release(sb);
4906 ext4_release_system_zone(sb);
4907failed_mount4a:
Al Viro94bf6082012-01-09 15:53:24 -05004908 dput(sb->s_root);
Manish Katiyar32a9bb52011-02-27 20:42:06 -05004909 sb->s_root = NULL;
Al Viro94bf6082012-01-09 15:53:24 -05004910failed_mount4:
Eric Sandeenb31e1552009-06-04 17:36:36 -04004911 ext4_msg(sb, KERN_ERR, "mount failed");
Jan Kara2e8fa542013-06-04 14:21:02 -04004912 if (EXT4_SB(sb)->rsv_conversion_wq)
4913 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
Mingming Cao4c0425f2009-09-28 15:48:41 -04004914failed_mount_wq:
Chengguang Xu50c15df2018-12-04 00:24:42 -05004915 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
4916 sbi->s_ea_inode_cache = NULL;
4917
4918 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
4919 sbi->s_ea_block_cache = NULL;
4920
Frank Mayhar03901312009-01-07 00:06:22 -05004921 if (sbi->s_journal) {
4922 jbd2_journal_destroy(sbi->s_journal);
4923 sbi->s_journal = NULL;
4924 }
Darrick J. Wong50460fe2014-10-30 10:53:16 -04004925failed_mount3a:
Zheng Liud3922a72013-07-01 08:12:37 -04004926 ext4_es_unregister_shrinker(sbi);
Zheng Liueb68d0e2014-09-01 22:26:49 -04004927failed_mount3:
Al Viro9105bb12013-12-08 20:52:31 -05004928 del_timer_sync(&sbi->s_err_report);
Johann Lombardic5e06d12011-05-24 18:31:25 -04004929 if (sbi->s_mmp_tsk)
4930 kthread_stop(sbi->s_mmp_tsk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004931failed_mount2:
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004932 rcu_read_lock();
4933 group_desc = rcu_dereference(sbi->s_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004934 for (i = 0; i < db_count; i++)
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004935 brelse(group_desc[i]);
4936 kvfree(group_desc);
4937 rcu_read_unlock();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004938failed_mount:
Darrick J. Wong04419842012-04-29 18:27:10 -04004939 if (sbi->s_chksum_driver)
4940 crypto_free_shash(sbi->s_chksum_driver);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04004941
4942#ifdef CONFIG_UNICODE
4943 utf8_unload(sbi->s_encoding);
4944#endif
4945
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004946#ifdef CONFIG_QUOTA
Jan Karaa2d4a642014-09-11 11:15:15 -04004947 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Theodore Ts'o0ba33fa2019-05-12 04:49:47 -04004948 kfree(get_qf_name(sb, sbi, i));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949#endif
Eric Biggersed318a62020-05-12 16:32:50 -07004950 fscrypt_free_dummy_context(&sbi->s_dummy_enc_ctx);
Mingming Cao617ba132006-10-11 01:20:53 -07004951 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004952 brelse(bh);
4953out_fail:
4954 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04004955 kfree(sbi->s_blockgroup_lock);
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004956out_free_base:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004957 kfree(sbi);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04004958 kfree(orig_data);
Dan Williams5e405592017-08-24 16:42:48 -07004959 fs_put_dax(dax_dev);
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004960 return err ? err : ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004961}
4962
4963/*
4964 * Setup any per-fs journal parameters now. We'll do this both on
4965 * initial mount, once the journal has been initialised but before we've
4966 * done any recovery; and again on any subsequent remount.
4967 */
Mingming Cao617ba132006-10-11 01:20:53 -07004968static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004969{
Mingming Cao617ba132006-10-11 01:20:53 -07004970 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004971
Theodore Ts'o30773842009-01-03 20:27:38 -05004972 journal->j_commit_interval = sbi->s_commit_interval;
4973 journal->j_min_batch_time = sbi->s_min_batch_time;
4974 journal->j_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004975
Theodore Ts'oa931da62010-08-03 21:35:12 -04004976 write_lock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004977 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07004978 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004979 else
Mingming Caodab291a2006-10-11 01:21:01 -07004980 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04004981 if (test_opt(sb, DATA_ERR_ABORT))
4982 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
4983 else
4984 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04004985 write_unlock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004986}
4987
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04004988static struct inode *ext4_get_journal_inode(struct super_block *sb,
4989 unsigned int journal_inum)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004990{
4991 struct inode *journal_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004992
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04004993 /*
4994 * Test for the existence of a valid inode on disk. Bad things
4995 * happen if we iget() an unused inode, as the subsequent iput()
4996 * will try to delete it.
4997 */
Theodore Ts'o8a363972018-12-19 12:29:13 -05004998 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08004999 if (IS_ERR(journal_inode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005000 ext4_msg(sb, KERN_ERR, "no journal found");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005001 return NULL;
5002 }
5003 if (!journal_inode->i_nlink) {
5004 make_bad_inode(journal_inode);
5005 iput(journal_inode);
Eric Sandeenb31e1552009-06-04 17:36:36 -04005006 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005007 return NULL;
5008 }
5009
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04005010 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005011 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08005012 if (!S_ISREG(journal_inode->i_mode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005013 ext4_msg(sb, KERN_ERR, "invalid journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005014 iput(journal_inode);
5015 return NULL;
5016 }
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005017 return journal_inode;
5018}
5019
5020static journal_t *ext4_get_journal(struct super_block *sb,
5021 unsigned int journal_inum)
5022{
5023 struct inode *journal_inode;
5024 journal_t *journal;
5025
Jan Kara11215632020-07-10 16:07:59 +02005026 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5027 return NULL;
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005028
5029 journal_inode = ext4_get_journal_inode(sb, journal_inum);
5030 if (!journal_inode)
5031 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005032
Mingming Caodab291a2006-10-11 01:21:01 -07005033 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005034 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005035 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005036 iput(journal_inode);
5037 return NULL;
5038 }
5039 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07005040 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005041 return journal;
5042}
5043
Mingming Cao617ba132006-10-11 01:20:53 -07005044static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005045 dev_t j_dev)
5046{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005047 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005048 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07005049 ext4_fsblk_t start;
5050 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005051 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07005052 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005053 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005054 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005055 struct block_device *bdev;
5056
Jan Kara11215632020-07-10 16:07:59 +02005057 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5058 return NULL;
Frank Mayhar03901312009-01-07 00:06:22 -05005059
Eric Sandeenb31e1552009-06-04 17:36:36 -04005060 bdev = ext4_blkdev_get(j_dev, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005061 if (bdev == NULL)
5062 return NULL;
5063
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005064 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04005065 hblock = bdev_logical_block_size(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005066 if (blocksize < hblock) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005067 ext4_msg(sb, KERN_ERR,
5068 "blocksize too small for journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005069 goto out_bdev;
5070 }
5071
Mingming Cao617ba132006-10-11 01:20:53 -07005072 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5073 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005074 set_blocksize(bdev, blocksize);
5075 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005076 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5077 "external journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005078 goto out_bdev;
5079 }
5080
Theodore Ts'o2716b802012-05-28 17:47:52 -04005081 es = (struct ext4_super_block *) (bh->b_data + offset);
Mingming Cao617ba132006-10-11 01:20:53 -07005082 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005083 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07005084 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005085 ext4_msg(sb, KERN_ERR, "external journal has "
5086 "bad superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005087 brelse(bh);
5088 goto out_bdev;
5089 }
5090
Darrick J. Wongdf4763b2014-09-11 11:44:36 -04005091 if ((le32_to_cpu(es->s_feature_ro_compat) &
5092 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5093 es->s_checksum != ext4_superblock_csum(sb, es)) {
5094 ext4_msg(sb, KERN_ERR, "external journal has "
5095 "corrupt superblock");
5096 brelse(bh);
5097 goto out_bdev;
5098 }
5099
Mingming Cao617ba132006-10-11 01:20:53 -07005100 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005101 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005102 brelse(bh);
5103 goto out_bdev;
5104 }
5105
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07005106 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005107 start = sb_block + 1;
5108 brelse(bh); /* we're done with the superblock */
5109
Mingming Caodab291a2006-10-11 01:21:01 -07005110 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111 start, len, blocksize);
5112 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005113 ext4_msg(sb, KERN_ERR, "failed to create device journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005114 goto out_bdev;
5115 }
5116 journal->j_private = sb;
Mike Christiedfec8a12016-06-05 14:31:44 -05005117 ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005118 wait_on_buffer(journal->j_sb_buffer);
5119 if (!buffer_uptodate(journal->j_sb_buffer)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005120 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005121 goto out_journal;
5122 }
5123 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005124 ext4_msg(sb, KERN_ERR, "External journal has more than one "
5125 "user (unsupported) - %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005126 be32_to_cpu(journal->j_superblock->s_nr_users));
5127 goto out_journal;
5128 }
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08005129 EXT4_SB(sb)->s_journal_bdev = bdev;
Mingming Cao617ba132006-10-11 01:20:53 -07005130 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005131 return journal;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005132
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005133out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07005134 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005135out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07005136 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005137 return NULL;
5138}
5139
Mingming Cao617ba132006-10-11 01:20:53 -07005140static int ext4_load_journal(struct super_block *sb,
5141 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005142 unsigned long journal_devnum)
5143{
5144 journal_t *journal;
5145 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5146 dev_t journal_dev;
5147 int err = 0;
5148 int really_read_only;
Lukas Czerner273108f2020-07-17 11:06:05 +02005149 int journal_dev_ro;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005150
Jan Kara11215632020-07-10 16:07:59 +02005151 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5152 return -EFSCORRUPTED;
Frank Mayhar03901312009-01-07 00:06:22 -05005153
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005154 if (journal_devnum &&
5155 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005156 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5157 "numbers have changed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005158 journal_dev = new_decode_dev(journal_devnum);
5159 } else
5160 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5161
Lukas Czerner273108f2020-07-17 11:06:05 +02005162 if (journal_inum && journal_dev) {
5163 ext4_msg(sb, KERN_ERR,
5164 "filesystem has both journal inode and journal device!");
5165 return -EINVAL;
5166 }
5167
5168 if (journal_inum) {
5169 journal = ext4_get_journal(sb, journal_inum);
5170 if (!journal)
5171 return -EINVAL;
5172 } else {
5173 journal = ext4_get_dev_journal(sb, journal_dev);
5174 if (!journal)
5175 return -EINVAL;
5176 }
5177
5178 journal_dev_ro = bdev_read_only(journal->j_dev);
5179 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5180
5181 if (journal_dev_ro && !sb_rdonly(sb)) {
5182 ext4_msg(sb, KERN_ERR,
5183 "journal device read-only, try mounting with '-o ro'");
5184 err = -EROFS;
5185 goto err_out;
5186 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005187
5188 /*
5189 * Are we loading a blank journal or performing recovery after a
5190 * crash? For recovery, we need to check in advance whether we
5191 * can get read-write access to the device.
5192 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005193 if (ext4_has_feature_journal_needs_recovery(sb)) {
David Howellsbc98a422017-07-17 08:45:34 +01005194 if (sb_rdonly(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005195 ext4_msg(sb, KERN_INFO, "INFO: recovery "
5196 "required on readonly filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005197 if (really_read_only) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005198 ext4_msg(sb, KERN_ERR, "write access "
Simon Ruderichd98bf8c2017-10-18 13:06:37 -04005199 "unavailable, cannot proceed "
5200 "(try mounting with noload)");
Lukas Czerner273108f2020-07-17 11:06:05 +02005201 err = -EROFS;
5202 goto err_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005203 }
Eric Sandeenb31e1552009-06-04 17:36:36 -04005204 ext4_msg(sb, KERN_INFO, "write access will "
5205 "be enabled during recovery");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005206 }
5207 }
5208
Theodore Ts'o90576c02009-09-29 15:51:30 -04005209 if (!(journal->j_flags & JBD2_BARRIER))
Eric Sandeenb31e1552009-06-04 17:36:36 -04005210 ext4_msg(sb, KERN_INFO, "barriers disabled");
Theodore Ts'o4776004f2008-09-08 23:00:52 -04005211
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005212 if (!ext4_has_feature_journal_needs_recovery(sb))
Mingming Caodab291a2006-10-11 01:21:01 -07005213 err = jbd2_journal_wipe(journal, !really_read_only);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04005214 if (!err) {
5215 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
5216 if (save)
5217 memcpy(save, ((char *) es) +
5218 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
Mingming Caodab291a2006-10-11 01:21:01 -07005219 err = jbd2_journal_load(journal);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04005220 if (save)
5221 memcpy(((char *) es) + EXT4_S_ERR_START,
5222 save, EXT4_S_ERR_LEN);
5223 kfree(save);
5224 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005225
5226 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005227 ext4_msg(sb, KERN_ERR, "error loading journal");
Lukas Czerner273108f2020-07-17 11:06:05 +02005228 goto err_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005229 }
5230
Mingming Cao617ba132006-10-11 01:20:53 -07005231 EXT4_SB(sb)->s_journal = journal;
Jan Kara11215632020-07-10 16:07:59 +02005232 err = ext4_clear_journal_err(sb, es);
5233 if (err) {
5234 EXT4_SB(sb)->s_journal = NULL;
5235 jbd2_journal_destroy(journal);
5236 return err;
5237 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005238
Maciej Żenczykowskic41303c2010-10-27 21:30:06 -04005239 if (!really_read_only && journal_devnum &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005240 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5241 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005242
5243 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005244 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005245 }
5246
5247 return 0;
Lukas Czerner273108f2020-07-17 11:06:05 +02005248
5249err_out:
5250 jbd2_journal_destroy(journal);
5251 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005252}
5253
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005254static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005255{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005256 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07005257 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08005258 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005259
Theodore Ts'obdfe0cbd2015-08-16 10:03:57 -04005260 if (!sbh || block_device_ejected(sb))
Takashi Satoc4be0c12009-01-09 16:40:58 -08005261 return error;
Jon Derricka17712c2018-07-02 18:45:18 -04005262
5263 /*
Theodore Ts'o71290b32009-09-10 17:31:04 -04005264 * If the file system is mounted read-only, don't update the
5265 * superblock write time. This avoids updating the superblock
5266 * write time when we are mounting the root file system
5267 * read/only but we need to replay the journal; at that point,
5268 * for people who are east of GMT and who make their clock
5269 * tick in localtime for Windows bug-for-bug compatibility,
5270 * the clock is set in the future, and this will cause e2fsck
5271 * to complain and force a full file system check.
5272 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005273 if (!(sb->s_flags & SB_RDONLY))
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04005274 ext4_update_tstamp(es, s_wtime);
Theodore Ts'of613dfc2010-07-27 11:56:08 -04005275 if (sb->s_bdev->bd_part)
5276 es->s_kbytes_written =
5277 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
Michael Callahandbae2c52018-07-18 04:47:38 -07005278 ((part_stat_read(sb->s_bdev->bd_part,
5279 sectors[STAT_WRITE]) -
Theodore Ts'oafc32f72009-02-28 19:39:58 -05005280 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Theodore Ts'of613dfc2010-07-27 11:56:08 -04005281 else
5282 es->s_kbytes_written =
5283 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005284 if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
5285 ext4_free_blocks_count_set(es,
Theodore Ts'o57042652011-09-09 18:56:51 -04005286 EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
5287 &EXT4_SB(sb)->s_freeclusters_counter)));
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005288 if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
5289 es->s_free_inodes_count =
5290 cpu_to_le32(percpu_counter_sum_positive(
Theodore Ts'oce7e0102010-11-03 12:03:21 -04005291 &EXT4_SB(sb)->s_freeinodes_counter));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005292 BUFFER_TRACE(sbh, "marking dirty");
Theodore Ts'o06db49e2012-10-10 01:06:58 -04005293 ext4_superblock_csum_set(sb);
Theodore Ts'o1566a482016-11-13 22:02:29 -05005294 if (sync)
5295 lock_buffer(sbh);
Theodore Ts'oe8680782018-12-30 23:20:39 -05005296 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
Pranay Kr. Srivastava4743f832016-07-04 10:24:52 -04005297 /*
5298 * Oh, dear. A previous attempt to write the
5299 * superblock failed. This could happen because the
5300 * USB device was yanked out. Or it could happen to
5301 * be a transient write error and maybe the block will
5302 * be remapped. Nothing we can do but to retry the
5303 * write and hope for the best.
5304 */
5305 ext4_msg(sb, KERN_ERR, "previous I/O error to "
5306 "superblock detected");
5307 clear_buffer_write_io_error(sbh);
5308 set_buffer_uptodate(sbh);
5309 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005310 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04005311 if (sync) {
Theodore Ts'o1566a482016-11-13 22:02:29 -05005312 unlock_buffer(sbh);
Daeho Jeong564bc402015-07-23 09:46:11 -04005313 error = __sync_dirty_buffer(sbh,
Jan Kara00473372017-05-04 10:58:03 -04005314 REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04005315 if (buffer_write_io_error(sbh)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005316 ext4_msg(sb, KERN_ERR, "I/O error while writing "
5317 "superblock");
Theodore Ts'o914258b2008-10-06 21:35:40 -04005318 clear_buffer_write_io_error(sbh);
5319 set_buffer_uptodate(sbh);
5320 }
5321 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08005322 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005323}
5324
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005325/*
5326 * Have we just finished recovery? If so, and if we are mounting (or
5327 * remounting) the filesystem readonly, then we will end up with a
5328 * consistent fs on disk. Record that fact.
5329 */
Jan Kara11215632020-07-10 16:07:59 +02005330static int ext4_mark_recovery_complete(struct super_block *sb,
5331 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005332{
Jan Kara11215632020-07-10 16:07:59 +02005333 int err;
Mingming Cao617ba132006-10-11 01:20:53 -07005334 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005335
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005336 if (!ext4_has_feature_journal(sb)) {
Jan Kara11215632020-07-10 16:07:59 +02005337 if (journal != NULL) {
5338 ext4_error(sb, "Journal got removed while the fs was "
5339 "mounted!");
5340 return -EFSCORRUPTED;
5341 }
5342 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05005343 }
Mingming Caodab291a2006-10-11 01:21:01 -07005344 jbd2_journal_lock_updates(journal);
Jan Kara11215632020-07-10 16:07:59 +02005345 err = jbd2_journal_flush(journal);
5346 if (err < 0)
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005347 goto out;
5348
David Howellsbc98a422017-07-17 08:45:34 +01005349 if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005350 ext4_clear_feature_journal_needs_recovery(sb);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005351 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005352 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005353out:
Mingming Caodab291a2006-10-11 01:21:01 -07005354 jbd2_journal_unlock_updates(journal);
Jan Kara11215632020-07-10 16:07:59 +02005355 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005356}
5357
5358/*
5359 * If we are mounting (or read-write remounting) a filesystem whose journal
5360 * has recorded an error from a previous lifetime, move that error to the
5361 * main filesystem now.
5362 */
Jan Kara11215632020-07-10 16:07:59 +02005363static int ext4_clear_journal_err(struct super_block *sb,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005364 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005365{
5366 journal_t *journal;
5367 int j_errno;
5368 const char *errstr;
5369
Jan Kara11215632020-07-10 16:07:59 +02005370 if (!ext4_has_feature_journal(sb)) {
5371 ext4_error(sb, "Journal got removed while the fs was mounted!");
5372 return -EFSCORRUPTED;
5373 }
Frank Mayhar03901312009-01-07 00:06:22 -05005374
Mingming Cao617ba132006-10-11 01:20:53 -07005375 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005376
5377 /*
5378 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07005379 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005380 */
5381
Mingming Caodab291a2006-10-11 01:21:01 -07005382 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005383 if (j_errno) {
5384 char nbuf[16];
5385
Mingming Cao617ba132006-10-11 01:20:53 -07005386 errstr = ext4_decode_error(sb, j_errno, nbuf);
Eric Sandeen12062dd2010-02-15 14:19:27 -05005387 ext4_warning(sb, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005388 "from previous mount: %s", errstr);
Eric Sandeen12062dd2010-02-15 14:19:27 -05005389 ext4_warning(sb, "Marking fs in need of filesystem check.");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005390
Mingming Cao617ba132006-10-11 01:20:53 -07005391 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5392 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005393 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005394
Mingming Caodab291a2006-10-11 01:21:01 -07005395 jbd2_journal_clear_err(journal);
Theodore Ts'od796c522012-08-05 19:04:57 -04005396 jbd2_journal_update_sb_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005397 }
Jan Kara11215632020-07-10 16:07:59 +02005398 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005399}
5400
5401/*
5402 * Force the running and committing transactions to commit,
5403 * and wait on the commit.
5404 */
Mingming Cao617ba132006-10-11 01:20:53 -07005405int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005406{
5407 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005408
David Howellsbc98a422017-07-17 08:45:34 +01005409 if (sb_rdonly(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005410 return 0;
5411
Mingming Cao617ba132006-10-11 01:20:53 -07005412 journal = EXT4_SB(sb)->s_journal;
Guo Chaob1deefc2013-01-28 21:41:02 -05005413 return ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005414}
5415
Mingming Cao617ba132006-10-11 01:20:53 -07005416static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005417{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05005418 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05005419 tid_t target;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005420 bool needs_barrier = false;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04005421 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005422
Jun Piao49598e02018-01-11 13:17:49 -05005423 if (unlikely(ext4_forced_shutdown(sbi)))
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005424 return 0;
5425
Theodore Ts'o9bffad12009-06-17 11:48:11 -04005426 trace_ext4_sync_fs(sb, wait);
Jan Kara2e8fa542013-06-04 14:21:02 -04005427 flush_workqueue(sbi->rsv_conversion_wq);
Jan Karaa1177822012-07-03 16:45:29 +02005428 /*
5429 * Writeback quota in non-journalled quota case - journalled quota has
5430 * no dirty dquots
5431 */
5432 dquot_writeback_dquots(sb, -1);
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005433 /*
5434 * Data writeback is possible w/o journal transaction, so barrier must
5435 * being sent at the end of the function. But we can skip it if
5436 * transaction_commit will do it for us.
5437 */
Theodore Ts'obda32532014-09-18 16:12:37 -04005438 if (sbi->s_journal) {
5439 target = jbd2_get_latest_transaction(sbi->s_journal);
5440 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
5441 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
5442 needs_barrier = true;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005443
Theodore Ts'obda32532014-09-18 16:12:37 -04005444 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5445 if (wait)
5446 ret = jbd2_log_wait_commit(sbi->s_journal,
5447 target);
5448 }
5449 } else if (wait && test_opt(sb, BARRIER))
5450 needs_barrier = true;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005451 if (needs_barrier) {
5452 int err;
Christoph Hellwig93985542020-05-13 14:36:00 +02005453 err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005454 if (!ret)
5455 ret = err;
5456 }
5457
5458 return ret;
5459}
5460
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005461/*
5462 * LVM calls this function before a (read-only) snapshot is created. This
5463 * gives us a chance to flush the journal completely and mark the fs clean.
Yongqiang Yangbe4f27d2011-04-10 22:06:07 -04005464 *
5465 * Note that only this function cannot bring a filesystem to be in a clean
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005466 * state independently. It relies on upper layer to stop all data & metadata
5467 * modifications.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005468 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08005469static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005470{
Takashi Satoc4be0c12009-01-09 16:40:58 -08005471 int error = 0;
5472 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005473
David Howellsbc98a422017-07-17 08:45:34 +01005474 if (sb_rdonly(sb))
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005475 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005476
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005477 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005478
Theodore Ts'obb044572014-09-18 17:12:02 -04005479 if (journal) {
5480 /* Now we set up the journal barrier. */
5481 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005482
Theodore Ts'obb044572014-09-18 17:12:02 -04005483 /*
5484 * Don't clear the needs_recovery flag if we failed to
5485 * flush the journal.
5486 */
5487 error = jbd2_journal_flush(journal);
5488 if (error < 0)
5489 goto out;
Eric Sandeenc642dc92015-08-15 10:45:06 -04005490
5491 /* Journal blocked and flushed, clear needs_recovery flag. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005492 ext4_clear_feature_journal_needs_recovery(sb);
Theodore Ts'obb044572014-09-18 17:12:02 -04005493 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005494
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005495 error = ext4_commit_super(sb, 1);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04005496out:
Theodore Ts'obb044572014-09-18 17:12:02 -04005497 if (journal)
5498 /* we rely on upper layer to stop further updates */
5499 jbd2_journal_unlock_updates(journal);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04005500 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005501}
5502
5503/*
5504 * Called by LVM after the snapshot is done. We need to reset the RECOVER
5505 * flag here, even though the filesystem is not technically dirty yet.
5506 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08005507static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005508{
David Howellsbc98a422017-07-17 08:45:34 +01005509 if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005510 return 0;
5511
Eric Sandeenc642dc92015-08-15 10:45:06 -04005512 if (EXT4_SB(sb)->s_journal) {
5513 /* Reset the needs_recovery flag before the fs is unlocked. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005514 ext4_set_feature_journal_needs_recovery(sb);
Eric Sandeenc642dc92015-08-15 10:45:06 -04005515 }
5516
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005517 ext4_commit_super(sb, 1);
Takashi Satoc4be0c12009-01-09 16:40:58 -08005518 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005519}
5520
Theodore Ts'o673c6102010-12-15 20:28:48 -05005521/*
5522 * Structure to save mount options for ext4_remount's benefit
5523 */
5524struct ext4_mount_options {
5525 unsigned long s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05005526 unsigned long s_mount_opt2;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005527 kuid_t s_resuid;
5528 kgid_t s_resgid;
Theodore Ts'o673c6102010-12-15 20:28:48 -05005529 unsigned long s_commit_interval;
5530 u32 s_min_batch_time, s_max_batch_time;
5531#ifdef CONFIG_QUOTA
5532 int s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04005533 char *s_qf_names[EXT4_MAXQUOTAS];
Theodore Ts'o673c6102010-12-15 20:28:48 -05005534#endif
5535};
5536
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005537static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005538{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005539 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07005540 struct ext4_sb_info *sbi = EXT4_SB(sb);
Lukas Czernerf25391e2020-07-23 17:05:26 +02005541 unsigned long old_sb_flags, vfs_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07005542 struct ext4_mount_options old_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005543 int enable_quota = 0;
Theodore Ts'o8a266462008-07-26 14:34:21 -04005544 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005545 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Johann Lombardic5e06d12011-05-24 18:31:25 -04005546 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005547#ifdef CONFIG_QUOTA
Chen Gang03dafb52013-01-24 23:24:58 -05005548 int i, j;
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005549 char *to_free[EXT4_MAXQUOTAS];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005550#endif
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005551 char *orig_data = kstrdup(data, GFP_KERNEL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005552
Chengguang Xu21ac7382018-07-29 15:51:54 -04005553 if (data && !orig_data)
5554 return -ENOMEM;
5555
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005556 /* Store the original options */
5557 old_sb_flags = sb->s_flags;
5558 old_opts.s_mount_opt = sbi->s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05005559 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005560 old_opts.s_resuid = sbi->s_resuid;
5561 old_opts.s_resgid = sbi->s_resgid;
5562 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05005563 old_opts.s_min_batch_time = sbi->s_min_batch_time;
5564 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005565#ifdef CONFIG_QUOTA
5566 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04005567 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Chen Gang03dafb52013-01-24 23:24:58 -05005568 if (sbi->s_qf_names[i]) {
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005569 char *qf_name = get_qf_name(sb, sbi, i);
5570
5571 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
Chen Gang03dafb52013-01-24 23:24:58 -05005572 if (!old_opts.s_qf_names[i]) {
5573 for (j = 0; j < i; j++)
5574 kfree(old_opts.s_qf_names[j]);
Wei Yongjun3e36a162013-03-02 17:13:55 -05005575 kfree(orig_data);
Chen Gang03dafb52013-01-24 23:24:58 -05005576 return -ENOMEM;
5577 }
5578 } else
5579 old_opts.s_qf_names[i] = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005580#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005581 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5582 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005583
Lukas Czernerf25391e2020-07-23 17:05:26 +02005584 /*
5585 * Some options can be enabled by ext4 and/or by VFS mount flag
5586 * either way we need to make sure it matches in both *flags and
5587 * s_flags. Copy those selected flags from *flags to s_flags
5588 */
5589 vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5590 sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5591
Eric Sandeen661aa522012-02-20 17:53:04 -05005592 if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005593 err = -EINVAL;
5594 goto restore_opts;
5595 }
5596
Darrick J. Wong6b992ff2014-10-30 10:53:16 -04005597 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
5598 test_opt(sb, JOURNAL_CHECKSUM)) {
5599 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
Eric Sandeen2d5b86e2015-02-12 23:07:37 -05005600 "during remount not supported; ignoring");
5601 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05005602 }
5603
Piotr Sarna6ae65142013-08-08 23:02:24 -04005604 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
5605 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
5606 ext4_msg(sb, KERN_ERR, "can't mount with "
5607 "both data=journal and delalloc");
5608 err = -EINVAL;
5609 goto restore_opts;
5610 }
5611 if (test_opt(sb, DIOREAD_NOLOCK)) {
5612 ext4_msg(sb, KERN_ERR, "can't mount with "
5613 "both data=journal and dioread_nolock");
5614 err = -EINVAL;
5615 goto restore_opts;
5616 }
Jan Karaab04df72016-12-03 16:20:53 -05005617 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5618 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5619 ext4_msg(sb, KERN_ERR, "can't mount with "
5620 "journal_async_commit in data=ordered mode");
5621 err = -EINVAL;
5622 goto restore_opts;
5623 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005624 }
5625
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04005626 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5627 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5628 err = -EINVAL;
5629 goto restore_opts;
5630 }
5631
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04005632 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04005633 ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005634
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005635 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5636 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005637
5638 es = sbi->s_es;
5639
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005640 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05005641 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005642 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5643 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005644
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005645 if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
Theodore Ts'o4ab2f152009-06-13 10:09:36 -04005646 if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005647 err = -EROFS;
5648 goto restore_opts;
5649 }
5650
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005651 if (*flags & SB_RDONLY) {
Theodore Ts'o38c03b32014-03-13 22:49:42 -04005652 err = sync_filesystem(sb);
5653 if (err < 0)
5654 goto restore_opts;
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04005655 err = dquot_suspend(sb, -1);
5656 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005657 goto restore_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005658
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005659 /*
5660 * First of all, the unconditional stuff we have to do
5661 * to disable replay of the journal when we next remount
5662 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005663 sb->s_flags |= SB_RDONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005664
5665 /*
5666 * OK, test if we are remounting a valid rw partition
5667 * readonly, and if so set the rdonly flag and then
5668 * mark the partition as valid again.
5669 */
Mingming Cao617ba132006-10-11 01:20:53 -07005670 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5671 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005672 es->s_state = cpu_to_le16(sbi->s_mount_state);
5673
Jan Kara11215632020-07-10 16:07:59 +02005674 if (sbi->s_journal) {
5675 /*
5676 * We let remount-ro finish even if marking fs
5677 * as clean failed...
5678 */
Frank Mayhar03901312009-01-07 00:06:22 -05005679 ext4_mark_recovery_complete(sb, es);
Jan Kara11215632020-07-10 16:07:59 +02005680 }
Theodore Ts'o2dca60d2018-07-08 19:36:02 -04005681 if (sbi->s_mmp_tsk)
5682 kthread_stop(sbi->s_mmp_tsk);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005683 } else {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04005684 /* Make sure we can mount this feature set readwrite */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005685 if (ext4_has_feature_readonly(sb) ||
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05005686 !ext4_feature_set_ok(sb, 0)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005687 err = -EROFS;
5688 goto restore_opts;
5689 }
Eric Sandeenead65962007-02-10 01:46:08 -08005690 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04005691 * Make sure the group descriptor checksums
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005692 * are sane. If they aren't, refuse to remount r/w.
Theodore Ts'o8a266462008-07-26 14:34:21 -04005693 */
5694 for (g = 0; g < sbi->s_groups_count; g++) {
5695 struct ext4_group_desc *gdp =
5696 ext4_get_group_desc(sb, g, NULL);
5697
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005698 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005699 ext4_msg(sb, KERN_ERR,
5700 "ext4_remount: Checksum for group %u failed (%u!=%u)",
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005701 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
Theodore Ts'o8a266462008-07-26 14:34:21 -04005702 le16_to_cpu(gdp->bg_checksum));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005703 err = -EFSBADCRC;
Theodore Ts'o8a266462008-07-26 14:34:21 -04005704 goto restore_opts;
5705 }
5706 }
5707
5708 /*
Eric Sandeenead65962007-02-10 01:46:08 -08005709 * If we have an unprocessed orphan list hanging
5710 * around from a previously readonly bdev mount,
5711 * require a full umount/remount for now.
5712 */
5713 if (es->s_last_orphan) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005714 ext4_msg(sb, KERN_WARNING, "Couldn't "
Eric Sandeenead65962007-02-10 01:46:08 -08005715 "remount RDWR because of unprocessed "
5716 "orphan inode list. Please "
Eric Sandeenb31e1552009-06-04 17:36:36 -04005717 "umount/remount instead");
Eric Sandeenead65962007-02-10 01:46:08 -08005718 err = -EINVAL;
5719 goto restore_opts;
5720 }
5721
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005722 /*
5723 * Mounting a RDONLY partition read-write, so reread
5724 * and store the current valid flag. (It may have
5725 * been changed by e2fsck since we originally mounted
5726 * the partition.)
5727 */
Jan Kara11215632020-07-10 16:07:59 +02005728 if (sbi->s_journal) {
5729 err = ext4_clear_journal_err(sb, es);
5730 if (err)
5731 goto restore_opts;
5732 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005733 sbi->s_mount_state = le16_to_cpu(es->s_state);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04005734
5735 err = ext4_setup_super(sb, es, 0);
5736 if (err)
5737 goto restore_opts;
5738
5739 sb->s_flags &= ~SB_RDONLY;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005740 if (ext4_has_feature_mmp(sb))
Johann Lombardic5e06d12011-05-24 18:31:25 -04005741 if (ext4_multi_mount_protect(sb,
5742 le64_to_cpu(es->s_mmp_block))) {
5743 err = -EROFS;
5744 goto restore_opts;
5745 }
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005746 enable_quota = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005747 }
5748 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04005749
5750 /*
5751 * Reinitialize lazy itable initialization thread based on
5752 * current settings
5753 */
David Howellsbc98a422017-07-17 08:45:34 +01005754 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
Lukas Czernerbfff6872010-10-27 21:30:05 -04005755 ext4_unregister_li_request(sb);
5756 else {
5757 ext4_group_t first_not_zeroed;
5758 first_not_zeroed = ext4_has_uninit_itable(sb);
5759 ext4_register_li_request(sb, first_not_zeroed);
5760 }
5761
Jan Kara0f5bde12020-07-28 15:04:37 +02005762 /*
5763 * Handle creation of system zone data early because it can fail.
5764 * Releasing of existing data is done when we are sure remount will
5765 * succeed.
5766 */
Chunguang Xudd0db942020-09-24 11:03:43 +08005767 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
Jan Kara0f5bde12020-07-28 15:04:37 +02005768 err = ext4_setup_system_zone(sb);
5769 if (err)
5770 goto restore_opts;
5771 }
Jan Karad176b1f2020-07-28 15:04:32 +02005772
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04005773 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
5774 err = ext4_commit_super(sb, 1);
5775 if (err)
5776 goto restore_opts;
5777 }
Frank Mayhar03901312009-01-07 00:06:22 -05005778
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005779#ifdef CONFIG_QUOTA
5780 /* Release old quota file names */
Jan Karaa2d4a642014-09-11 11:15:15 -04005781 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Chen Gang03dafb52013-01-24 23:24:58 -05005782 kfree(old_opts.s_qf_names[i]);
Aditya Kali7c319d32012-07-22 20:21:31 -04005783 if (enable_quota) {
5784 if (sb_any_quota_suspended(sb))
5785 dquot_resume(sb, -1);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005786 else if (ext4_has_feature_quota(sb)) {
Aditya Kali7c319d32012-07-22 20:21:31 -04005787 err = ext4_enable_quotas(sb);
Theodore Ts'o07724f92012-08-17 19:08:42 -04005788 if (err)
Aditya Kali7c319d32012-07-22 20:21:31 -04005789 goto restore_opts;
Aditya Kali7c319d32012-07-22 20:21:31 -04005790 }
5791 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005792#endif
Chunguang Xudd0db942020-09-24 11:03:43 +08005793 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
Jan Kara0f5bde12020-07-28 15:04:37 +02005794 ext4_release_system_zone(sb);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005795
Lukas Czernerf25391e2020-07-23 17:05:26 +02005796 /*
5797 * Some options can be enabled by ext4 and/or by VFS mount flag
5798 * either way we need to make sure it matches in both *flags and
5799 * s_flags. Copy those selected flags from s_flags to *flags
5800 */
5801 *flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
5802
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005803 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5804 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005805 return 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005806
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005807restore_opts:
5808 sb->s_flags = old_sb_flags;
5809 sbi->s_mount_opt = old_opts.s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05005810 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005811 sbi->s_resuid = old_opts.s_resuid;
5812 sbi->s_resgid = old_opts.s_resgid;
5813 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05005814 sbi->s_min_batch_time = old_opts.s_min_batch_time;
5815 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Chunguang Xudd0db942020-09-24 11:03:43 +08005816 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
Jan Kara0f5bde12020-07-28 15:04:37 +02005817 ext4_release_system_zone(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005818#ifdef CONFIG_QUOTA
5819 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04005820 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005821 to_free[i] = get_qf_name(sb, sbi, i);
5822 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005823 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005824 synchronize_rcu();
5825 for (i = 0; i < EXT4_MAXQUOTAS; i++)
5826 kfree(to_free[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005827#endif
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005828 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005829 return err;
5830}
5831
Li Xi689c9582016-01-08 16:01:22 -05005832#ifdef CONFIG_QUOTA
5833static int ext4_statfs_project(struct super_block *sb,
5834 kprojid_t projid, struct kstatfs *buf)
5835{
5836 struct kqid qid;
5837 struct dquot *dquot;
5838 u64 limit;
5839 u64 curblock;
5840
5841 qid = make_kqid_projid(projid);
5842 dquot = dqget(sb, qid);
5843 if (IS_ERR(dquot))
5844 return PTR_ERR(dquot);
Jan Kara7b9ca4c2017-08-07 13:19:50 +02005845 spin_lock(&dquot->dq_dqb_lock);
Li Xi689c9582016-01-08 16:01:22 -05005846
Chengguang Xua08fe662020-02-10 16:24:45 +08005847 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
5848 dquot->dq_dqb.dqb_bhardlimit);
Chengguang Xu57c32ea2019-10-16 10:25:01 +08005849 limit >>= sb->s_blocksize_bits;
5850
Li Xi689c9582016-01-08 16:01:22 -05005851 if (limit && buf->f_blocks > limit) {
Konstantin Khlebnikovf06925c2018-05-20 22:49:54 -04005852 curblock = (dquot->dq_dqb.dqb_curspace +
5853 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
Li Xi689c9582016-01-08 16:01:22 -05005854 buf->f_blocks = limit;
5855 buf->f_bfree = buf->f_bavail =
5856 (buf->f_blocks > curblock) ?
5857 (buf->f_blocks - curblock) : 0;
5858 }
5859
Chengguang Xua08fe662020-02-10 16:24:45 +08005860 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
5861 dquot->dq_dqb.dqb_ihardlimit);
Li Xi689c9582016-01-08 16:01:22 -05005862 if (limit && buf->f_files > limit) {
5863 buf->f_files = limit;
5864 buf->f_ffree =
5865 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
5866 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
5867 }
5868
Jan Kara7b9ca4c2017-08-07 13:19:50 +02005869 spin_unlock(&dquot->dq_dqb_lock);
Li Xi689c9582016-01-08 16:01:22 -05005870 dqput(dquot);
5871 return 0;
5872}
5873#endif
5874
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005875static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005876{
5877 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07005878 struct ext4_sb_info *sbi = EXT4_SB(sb);
5879 struct ext4_super_block *es = sbi->s_es;
Lukas Czerner27dd4382013-04-09 22:11:22 -04005880 ext4_fsblk_t overhead = 0, resv_blocks;
Pekka Enberg960cc392006-12-06 20:35:29 -08005881 u64 fsid;
Kazuya Miod02a9392011-05-24 18:30:07 -04005882 s64 bfree;
Lukas Czerner27dd4382013-04-09 22:11:22 -04005883 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005884
Theodore Ts'o952fc182012-07-09 16:27:05 -04005885 if (!test_opt(sb, MINIX_DF))
5886 overhead = sbi->s_overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005887
Mingming Cao617ba132006-10-11 01:20:53 -07005888 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005889 buf->f_bsize = sb->s_blocksize;
Eric Sandeenb72f78c2012-11-08 10:33:36 -05005890 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
Theodore Ts'o57042652011-09-09 18:56:51 -04005891 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
5892 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
Kazuya Miod02a9392011-05-24 18:30:07 -04005893 /* prevent underflow in case that few free space is available */
Theodore Ts'o57042652011-09-09 18:56:51 -04005894 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
Lukas Czerner27dd4382013-04-09 22:11:22 -04005895 buf->f_bavail = buf->f_bfree -
5896 (ext4_r_blocks_count(es) + resv_blocks);
5897 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005898 buf->f_bavail = 0;
5899 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07005900 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07005901 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08005902 fsid = le64_to_cpup((void *)es->s_uuid) ^
5903 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5904 buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5905 buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005906
Li Xi689c9582016-01-08 16:01:22 -05005907#ifdef CONFIG_QUOTA
5908 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
5909 sb_has_quota_limits_enabled(sb, PRJQUOTA))
5910 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
5911#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005912 return 0;
5913}
5914
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005915
5916#ifdef CONFIG_QUOTA
5917
Jan Karabc8230e2017-06-08 14:39:48 +02005918/*
5919 * Helper functions so that transaction is started before we acquire dqio_sem
5920 * to keep correct lock ordering of transaction > dqio_sem
5921 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005922static inline struct inode *dquot_to_inode(struct dquot *dquot)
5923{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07005924 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005925}
5926
Mingming Cao617ba132006-10-11 01:20:53 -07005927static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005928{
5929 int ret, err;
5930 handle_t *handle;
5931 struct inode *inode;
5932
5933 inode = dquot_to_inode(dquot);
Theodore Ts'o9924a922013-02-08 21:59:22 -05005934 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005935 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005936 if (IS_ERR(handle))
5937 return PTR_ERR(handle);
5938 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07005939 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005940 if (!ret)
5941 ret = err;
5942 return ret;
5943}
5944
Mingming Cao617ba132006-10-11 01:20:53 -07005945static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005946{
5947 int ret, err;
5948 handle_t *handle;
5949
Theodore Ts'o9924a922013-02-08 21:59:22 -05005950 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005951 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005952 if (IS_ERR(handle))
5953 return PTR_ERR(handle);
5954 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07005955 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005956 if (!ret)
5957 ret = err;
5958 return ret;
5959}
5960
Mingming Cao617ba132006-10-11 01:20:53 -07005961static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005962{
5963 int ret, err;
5964 handle_t *handle;
5965
Theodore Ts'o9924a922013-02-08 21:59:22 -05005966 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005967 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07005968 if (IS_ERR(handle)) {
5969 /* Release dquot anyway to avoid endless cycle in dqput() */
5970 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005971 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07005972 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005973 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07005974 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005975 if (!ret)
5976 ret = err;
5977 return ret;
5978}
5979
Mingming Cao617ba132006-10-11 01:20:53 -07005980static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005981{
Jan Kara262b4662013-03-02 17:57:08 -05005982 struct super_block *sb = dquot->dq_sb;
5983 struct ext4_sb_info *sbi = EXT4_SB(sb);
5984
Jan Kara2c8be6b2008-05-13 21:27:55 -04005985 /* Are we journaling quotas? */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005986 if (ext4_has_feature_quota(sb) ||
Jan Kara262b4662013-03-02 17:57:08 -05005987 sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005988 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07005989 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005990 } else {
5991 return dquot_mark_dquot_dirty(dquot);
5992 }
5993}
5994
Mingming Cao617ba132006-10-11 01:20:53 -07005995static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005996{
5997 int ret, err;
5998 handle_t *handle;
5999
6000 /* Data block + inode block */
David Howells2b0143b2015-03-17 22:25:59 +00006001 handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006002 if (IS_ERR(handle))
6003 return PTR_ERR(handle);
6004 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07006005 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006006 if (!ret)
6007 ret = err;
6008 return ret;
6009}
6010
6011/*
6012 * Turn on quotas during mount time - we need to find
6013 * the quota file and such...
6014 */
Mingming Cao617ba132006-10-11 01:20:53 -07006015static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006016{
Theodore Ts'o33458ea2018-10-12 09:28:09 -04006017 return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
Christoph Hellwig287a8092010-05-19 07:16:45 -04006018 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006019}
6020
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006021static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6022{
6023 struct ext4_inode_info *ei = EXT4_I(inode);
6024
6025 /* The first argument of lockdep_set_subclass has to be
6026 * *exactly* the same as the argument to init_rwsem() --- in
6027 * this case, in init_once() --- or lockdep gets unhappy
6028 * because the name of the lock is set using the
6029 * stringification of the argument to init_rwsem().
6030 */
6031 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
6032 lockdep_set_subclass(&ei->i_data_sem, subclass);
6033}
6034
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006035/*
6036 * Standard function to be called on quota_on
6037 */
Mingming Cao617ba132006-10-11 01:20:53 -07006038static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro8c54ca92016-11-20 19:49:34 -05006039 const struct path *path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006040{
6041 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006042
6043 if (!test_opt(sb, QUOTA))
6044 return -EINVAL;
Jan Kara06235432008-05-13 19:11:51 -04006045
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006046 /* Quotafile not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05006047 if (path->dentry->d_sb != sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006048 return -EXDEV;
Jan Kara06235432008-05-13 19:11:51 -04006049 /* Journaling quota? */
6050 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04006051 /* Quotafile not in fs root? */
Jan Karaf00c9e42010-09-15 17:38:58 +02006052 if (path->dentry->d_parent != sb->s_root)
Eric Sandeenb31e1552009-06-04 17:36:36 -04006053 ext4_msg(sb, KERN_WARNING,
6054 "Quota file not on filesystem root. "
6055 "Journaled quota will not work");
Jan Kara91389242017-08-03 11:25:55 +02006056 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6057 } else {
6058 /*
6059 * Clear the flag just in case mount options changed since
6060 * last time.
6061 */
6062 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04006063 }
Jan Kara06235432008-05-13 19:11:51 -04006064
6065 /*
6066 * When we journal data on quota file, we have to flush journal to see
6067 * all updates to the file when we bypass pagecache...
6068 */
Frank Mayhar03901312009-01-07 00:06:22 -05006069 if (EXT4_SB(sb)->s_journal &&
David Howells2b0143b2015-03-17 22:25:59 +00006070 ext4_should_journal_data(d_inode(path->dentry))) {
Jan Kara06235432008-05-13 19:11:51 -04006071 /*
6072 * We don't need to lock updates but journal_flush() could
6073 * otherwise be livelocked...
6074 */
6075 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04006076 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04006077 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Jan Karaf00c9e42010-09-15 17:38:58 +02006078 if (err)
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04006079 return err;
Jan Kara06235432008-05-13 19:11:51 -04006080 }
Jan Kara957153f2017-04-06 15:40:06 +02006081
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006082 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6083 err = dquot_quota_on(sb, type, format_id, path);
Jan Kara957153f2017-04-06 15:40:06 +02006084 if (err) {
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006085 lockdep_set_quota_inode(path->dentry->d_inode,
6086 I_DATA_SEM_NORMAL);
Jan Kara957153f2017-04-06 15:40:06 +02006087 } else {
6088 struct inode *inode = d_inode(path->dentry);
6089 handle_t *handle;
6090
Jan Kara61a92982017-04-24 16:49:16 +02006091 /*
6092 * Set inode flags to prevent userspace from messing with quota
6093 * files. If this fails, we return success anyway since quotas
6094 * are already enabled and this is not a hard failure.
6095 */
Jan Kara957153f2017-04-06 15:40:06 +02006096 inode_lock(inode);
6097 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6098 if (IS_ERR(handle))
6099 goto unlock_inode;
6100 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6101 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6102 S_NOATIME | S_IMMUTABLE);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006103 err = ext4_mark_inode_dirty(handle, inode);
Jan Kara957153f2017-04-06 15:40:06 +02006104 ext4_journal_stop(handle);
6105 unlock_inode:
6106 inode_unlock(inode);
6107 }
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006108 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006109}
6110
Aditya Kali7c319d32012-07-22 20:21:31 -04006111static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
6112 unsigned int flags)
6113{
6114 int err;
6115 struct inode *qf_inode;
Jan Karaa2d4a642014-09-11 11:15:15 -04006116 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
Aditya Kali7c319d32012-07-22 20:21:31 -04006117 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
Li Xi689c9582016-01-08 16:01:22 -05006118 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6119 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
Aditya Kali7c319d32012-07-22 20:21:31 -04006120 };
6121
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006122 BUG_ON(!ext4_has_feature_quota(sb));
Aditya Kali7c319d32012-07-22 20:21:31 -04006123
6124 if (!qf_inums[type])
6125 return -EPERM;
6126
Theodore Ts'o8a363972018-12-19 12:29:13 -05006127 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
Aditya Kali7c319d32012-07-22 20:21:31 -04006128 if (IS_ERR(qf_inode)) {
6129 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
6130 return PTR_ERR(qf_inode);
6131 }
6132
Jan Karabcb13852013-04-09 09:21:41 -04006133 /* Don't account quota for quota files to avoid recursion */
6134 qf_inode->i_flags |= S_NOQUOTA;
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006135 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
Jan Kara7212b952019-11-01 18:55:38 +01006136 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006137 if (err)
6138 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
Pan Bian61157b22018-12-03 23:28:02 -05006139 iput(qf_inode);
Aditya Kali7c319d32012-07-22 20:21:31 -04006140
6141 return err;
6142}
6143
6144/* Enable usage tracking for all quota types. */
6145static int ext4_enable_quotas(struct super_block *sb)
6146{
6147 int type, err = 0;
Jan Karaa2d4a642014-09-11 11:15:15 -04006148 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
Aditya Kali7c319d32012-07-22 20:21:31 -04006149 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
Li Xi689c9582016-01-08 16:01:22 -05006150 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6151 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
Aditya Kali7c319d32012-07-22 20:21:31 -04006152 };
Jan Kara49da9392016-09-05 23:08:16 -04006153 bool quota_mopt[EXT4_MAXQUOTAS] = {
6154 test_opt(sb, USRQUOTA),
6155 test_opt(sb, GRPQUOTA),
6156 test_opt(sb, PRJQUOTA),
6157 };
Aditya Kali7c319d32012-07-22 20:21:31 -04006158
Jan Kara91389242017-08-03 11:25:55 +02006159 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
Jan Karaa2d4a642014-09-11 11:15:15 -04006160 for (type = 0; type < EXT4_MAXQUOTAS; type++) {
Aditya Kali7c319d32012-07-22 20:21:31 -04006161 if (qf_inums[type]) {
6162 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
Jan Kara49da9392016-09-05 23:08:16 -04006163 DQUOT_USAGE_ENABLED |
6164 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
Aditya Kali7c319d32012-07-22 20:21:31 -04006165 if (err) {
6166 ext4_warning(sb,
Theodore Ts'o72ba7452013-01-24 23:24:54 -05006167 "Failed to enable quota tracking "
6168 "(type=%d, err=%d). Please run "
6169 "e2fsck to fix.", type, err);
Junichi Uekawa7f144fd2018-07-29 15:51:52 -04006170 for (type--; type >= 0; type--)
6171 dquot_quota_off(sb, type);
6172
Aditya Kali7c319d32012-07-22 20:21:31 -04006173 return err;
6174 }
6175 }
6176 }
6177 return 0;
6178}
6179
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006180static int ext4_quota_off(struct super_block *sb, int type)
6181{
Jan Kara21f97692011-04-04 15:33:39 -04006182 struct inode *inode = sb_dqopt(sb)->files[type];
6183 handle_t *handle;
Jan Kara957153f2017-04-06 15:40:06 +02006184 int err;
Jan Kara21f97692011-04-04 15:33:39 -04006185
Dmitry Monakhov87009d82010-11-08 13:47:33 -05006186 /* Force all delayed allocation blocks to be allocated.
6187 * Caller already holds s_umount sem */
6188 if (test_opt(sb, DELALLOC))
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006189 sync_filesystem(sb);
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006190
Jan Kara957153f2017-04-06 15:40:06 +02006191 if (!inode || !igrab(inode))
Amir Goldstein0b268592011-05-16 09:59:13 -04006192 goto out;
6193
Jan Kara957153f2017-04-06 15:40:06 +02006194 err = dquot_quota_off(sb, type);
Jan Kara964edf62017-05-21 22:31:23 -04006195 if (err || ext4_has_feature_quota(sb))
Jan Kara957153f2017-04-06 15:40:06 +02006196 goto out_put;
6197
6198 inode_lock(inode);
Jan Kara61a92982017-04-24 16:49:16 +02006199 /*
6200 * Update modification times of quota files when userspace can
6201 * start looking at them. If we fail, we return success anyway since
6202 * this is not a hard failure and quotas are already disabled.
6203 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05006204 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006205 if (IS_ERR(handle)) {
6206 err = PTR_ERR(handle);
Jan Kara957153f2017-04-06 15:40:06 +02006207 goto out_unlock;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006208 }
Jan Kara957153f2017-04-06 15:40:06 +02006209 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6210 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05006211 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006212 err = ext4_mark_inode_dirty(handle, inode);
Jan Kara21f97692011-04-04 15:33:39 -04006213 ext4_journal_stop(handle);
Jan Kara957153f2017-04-06 15:40:06 +02006214out_unlock:
6215 inode_unlock(inode);
6216out_put:
Jan Kara964edf62017-05-21 22:31:23 -04006217 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
Jan Kara957153f2017-04-06 15:40:06 +02006218 iput(inode);
6219 return err;
Jan Kara21f97692011-04-04 15:33:39 -04006220out:
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006221 return dquot_quota_off(sb, type);
6222}
6223
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006224/* Read data from quotafile - avoid pagecache and such because we cannot afford
6225 * acquiring the locks... As quota files are never truncated and quota code
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006226 * itself serializes the operations (and no one else should touch the files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006227 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07006228static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006229 size_t len, loff_t off)
6230{
6231 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05006232 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006233 int offset = off & (sb->s_blocksize - 1);
6234 int tocopy;
6235 size_t toread;
6236 struct buffer_head *bh;
6237 loff_t i_size = i_size_read(inode);
6238
6239 if (off > i_size)
6240 return 0;
6241 if (off+len > i_size)
6242 len = i_size-off;
6243 toread = len;
6244 while (toread > 0) {
6245 tocopy = sb->s_blocksize - offset < toread ?
6246 sb->s_blocksize - offset : toread;
Theodore Ts'o1c215022014-08-29 20:52:15 -04006247 bh = ext4_bread(NULL, inode, blk, 0);
6248 if (IS_ERR(bh))
6249 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006250 if (!bh) /* A hole? */
6251 memset(data, 0, tocopy);
6252 else
6253 memcpy(data, bh->b_data+offset, tocopy);
6254 brelse(bh);
6255 offset = 0;
6256 toread -= tocopy;
6257 data += tocopy;
6258 blk++;
6259 }
6260 return len;
6261}
6262
6263/* Write to quotafile (we know the transaction is already started and has
6264 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07006265static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006266 const char *data, size_t len, loff_t off)
6267{
6268 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05006269 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006270 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006271 int retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006272 struct buffer_head *bh;
6273 handle_t *handle = journal_current_handle();
6274
Frank Mayhar03901312009-01-07 00:06:22 -05006275 if (EXT4_SB(sb)->s_journal && !handle) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04006276 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6277 " cancelled because transaction is not started",
Jan Kara9c3013e2007-09-11 15:23:29 -07006278 (unsigned long long)off, (unsigned long long)len);
6279 return -EIO;
6280 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006281 /*
6282 * Since we account only one data block in transaction credits,
6283 * then it is impossible to cross a block boundary.
6284 */
6285 if (sb->s_blocksize - offset < len) {
6286 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6287 " cancelled because not block aligned",
6288 (unsigned long long)off, (unsigned long long)len);
6289 return -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006290 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006291
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006292 do {
6293 bh = ext4_bread(handle, inode, blk,
6294 EXT4_GET_BLOCKS_CREATE |
6295 EXT4_GET_BLOCKS_METADATA_NOFAIL);
Masahiro Yamada45586c72020-02-03 17:37:45 -08006296 } while (PTR_ERR(bh) == -ENOSPC &&
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006297 ext4_should_retry_alloc(inode->i_sb, &retries));
Theodore Ts'o1c215022014-08-29 20:52:15 -04006298 if (IS_ERR(bh))
6299 return PTR_ERR(bh);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006300 if (!bh)
6301 goto out;
liang xie5d601252014-05-12 22:06:43 -04006302 BUFFER_TRACE(bh, "get write access");
Jan Kara62d2b5f2010-07-27 11:56:07 -04006303 err = ext4_journal_get_write_access(handle, bh);
6304 if (err) {
6305 brelse(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -04006306 return err;
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006307 }
6308 lock_buffer(bh);
6309 memcpy(bh->b_data+offset, data, len);
6310 flush_dcache_page(bh->b_page);
6311 unlock_buffer(bh);
Jan Kara62d2b5f2010-07-27 11:56:07 -04006312 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006313 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006314out:
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006315 if (inode->i_size < off + len) {
6316 i_size_write(inode, off + len);
Mingming Cao617ba132006-10-11 01:20:53 -07006317 EXT4_I(inode)->i_disksize = inode->i_size;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006318 err2 = ext4_mark_inode_dirty(handle, inode);
6319 if (unlikely(err2 && !err))
6320 err = err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006321 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006322 return err ? err : len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006323}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006324#endif
6325
Al Viro152a0832010-07-25 00:46:55 +04006326static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6327 const char *dev_name, void *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006328{
Al Viro152a0832010-07-25 00:46:55 +04006329 return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006330}
6331
Jan Karac290ea02015-06-18 16:52:29 +02006332#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
Theodore Ts'o24b58422009-12-07 14:08:51 -05006333static inline void register_as_ext2(void)
6334{
6335 int err = register_filesystem(&ext2_fs_type);
6336 if (err)
6337 printk(KERN_WARNING
6338 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
6339}
6340
6341static inline void unregister_as_ext2(void)
6342{
6343 unregister_filesystem(&ext2_fs_type);
6344}
Theodore Ts'o2035e772011-04-18 17:29:14 -04006345
6346static inline int ext2_feature_set_ok(struct super_block *sb)
6347{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006348 if (ext4_has_unknown_ext2_incompat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006349 return 0;
David Howellsbc98a422017-07-17 08:45:34 +01006350 if (sb_rdonly(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006351 return 1;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006352 if (ext4_has_unknown_ext2_ro_compat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006353 return 0;
6354 return 1;
6355}
Theodore Ts'o24b58422009-12-07 14:08:51 -05006356#else
6357static inline void register_as_ext2(void) { }
6358static inline void unregister_as_ext2(void) { }
Theodore Ts'o2035e772011-04-18 17:29:14 -04006359static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
Theodore Ts'o24b58422009-12-07 14:08:51 -05006360#endif
6361
Theodore Ts'o24b58422009-12-07 14:08:51 -05006362static inline void register_as_ext3(void)
6363{
6364 int err = register_filesystem(&ext3_fs_type);
6365 if (err)
6366 printk(KERN_WARNING
6367 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
6368}
6369
6370static inline void unregister_as_ext3(void)
6371{
6372 unregister_filesystem(&ext3_fs_type);
6373}
Theodore Ts'o2035e772011-04-18 17:29:14 -04006374
6375static inline int ext3_feature_set_ok(struct super_block *sb)
6376{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006377 if (ext4_has_unknown_ext3_incompat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006378 return 0;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006379 if (!ext4_has_feature_journal(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006380 return 0;
David Howellsbc98a422017-07-17 08:45:34 +01006381 if (sb_rdonly(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006382 return 1;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006383 if (ext4_has_unknown_ext3_ro_compat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006384 return 0;
6385 return 1;
6386}
Theodore Ts'o24b58422009-12-07 14:08:51 -05006387
Theodore Ts'o03010a32008-10-10 20:02:48 -04006388static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006389 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04006390 .name = "ext4",
Al Viro152a0832010-07-25 00:46:55 +04006391 .mount = ext4_mount,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006392 .kill_sb = kill_block_super,
6393 .fs_flags = FS_REQUIRES_DEV,
6394};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08006395MODULE_ALIAS_FS("ext4");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006396
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006397/* Shared across all ext4 file systems */
6398wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006399
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006400static int __init ext4_init_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006401{
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006402 int i, err;
Alex Tomasc9de5602008-01-29 00:19:52 -05006403
Theodore Ts'oe294a532015-08-15 14:59:44 -04006404 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
Al Viro07c0c5d82012-03-20 22:05:02 -04006405 ext4_li_info = NULL;
6406 mutex_init(&ext4_li_mtx);
6407
Carlos Maiolino9a4c8012012-12-10 16:30:45 -05006408 /* Build-time check for flags consistency */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04006409 ext4_check_flag_values();
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006410
Jan Karae142d052016-03-08 22:44:50 -05006411 for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006412 init_waitqueue_head(&ext4__ioend_wq[i]);
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006413
Zheng Liu51865fd2012-11-08 21:57:32 -05006414 err = ext4_init_es();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04006415 if (err)
6416 return err;
Zheng Liu51865fd2012-11-08 21:57:32 -05006417
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006418 err = ext4_init_pending();
6419 if (err)
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006420 goto out7;
6421
6422 err = ext4_init_post_read_processing();
6423 if (err)
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006424 goto out6;
6425
Zheng Liu51865fd2012-11-08 21:57:32 -05006426 err = ext4_init_pageio();
6427 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006428 goto out5;
Zheng Liu51865fd2012-11-08 21:57:32 -05006429
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006430 err = ext4_init_system_zone();
Theodore Ts'obd2d0212010-10-27 21:30:10 -04006431 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006432 goto out4;
Lukas Czerner857ac882010-10-27 21:30:05 -04006433
Theodore Ts'ob5799012015-09-23 12:44:17 -04006434 err = ext4_init_sysfs();
Theodore Ts'odd683142011-02-03 14:33:49 -05006435 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006436 goto out3;
Lukas Czerner857ac882010-10-27 21:30:05 -04006437
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006438 err = ext4_init_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006439 if (err)
Alex Tomasc9de5602008-01-29 00:19:52 -05006440 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006441 err = init_inodecache();
6442 if (err)
6443 goto out1;
Theodore Ts'o24b58422009-12-07 14:08:51 -05006444 register_as_ext3();
Theodore Ts'o2035e772011-04-18 17:29:14 -04006445 register_as_ext2();
Theodore Ts'o03010a32008-10-10 20:02:48 -04006446 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006447 if (err)
6448 goto out;
Lukas Czernerbfff6872010-10-27 21:30:05 -04006449
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006450 return 0;
6451out:
Theodore Ts'o24b58422009-12-07 14:08:51 -05006452 unregister_as_ext2();
6453 unregister_as_ext3();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006454 destroy_inodecache();
6455out1:
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006456 ext4_exit_mballoc();
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04006457out2:
Theodore Ts'ob5799012015-09-23 12:44:17 -04006458 ext4_exit_sysfs();
6459out3:
Theodore Ts'odd683142011-02-03 14:33:49 -05006460 ext4_exit_system_zone();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006461out4:
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006462 ext4_exit_pageio();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006463out5:
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006464 ext4_exit_post_read_processing();
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006465out6:
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006466 ext4_exit_pending();
6467out7:
Zheng Liu51865fd2012-11-08 21:57:32 -05006468 ext4_exit_es();
6469
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006470 return err;
6471}
6472
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006473static void __exit ext4_exit_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006474{
Lukas Czernerbfff6872010-10-27 21:30:05 -04006475 ext4_destroy_lazyinit_thread();
Theodore Ts'o24b58422009-12-07 14:08:51 -05006476 unregister_as_ext2();
6477 unregister_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04006478 unregister_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006479 destroy_inodecache();
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006480 ext4_exit_mballoc();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006481 ext4_exit_sysfs();
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006482 ext4_exit_system_zone();
6483 ext4_exit_pageio();
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006484 ext4_exit_post_read_processing();
Eric Sandeendd12ed142013-07-26 15:21:11 -04006485 ext4_exit_es();
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006486 ext4_exit_pending();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006487}
6488
6489MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05006490MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006491MODULE_LICENSE("GPL");
Theodore Ts'o7ef79ad2018-04-26 00:44:46 -04006492MODULE_SOFTDEP("pre: crc32c");
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006493module_init(ext4_init_fs)
6494module_exit(ext4_exit_fs)