blob: e5e896b6200ac0d6115a48a40775fca85178c935 [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
zhangyi (F)fa491b12020-09-24 15:33:32 +0800144
145static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
146 bh_end_io_t *end_io)
147{
148 /*
149 * buffer's verified bit is no longer valid after reading from
150 * disk again due to write out error, clear it to make sure we
151 * recheck the buffer contents.
152 */
153 clear_buffer_verified(bh);
154
155 bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
156 get_bh(bh);
157 submit_bh(REQ_OP_READ, op_flags, bh);
158}
159
160void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
161 bh_end_io_t *end_io)
162{
163 BUG_ON(!buffer_locked(bh));
164
165 if (ext4_buffer_uptodate(bh)) {
166 unlock_buffer(bh);
167 return;
168 }
169 __ext4_read_bh(bh, op_flags, end_io);
170}
171
172int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
173{
174 BUG_ON(!buffer_locked(bh));
175
176 if (ext4_buffer_uptodate(bh)) {
177 unlock_buffer(bh);
178 return 0;
179 }
180
181 __ext4_read_bh(bh, op_flags, end_io);
182
183 wait_on_buffer(bh);
184 if (buffer_uptodate(bh))
185 return 0;
186 return -EIO;
187}
188
189int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
190{
191 if (trylock_buffer(bh)) {
192 if (wait)
193 return ext4_read_bh(bh, op_flags, NULL);
194 ext4_read_bh_nowait(bh, op_flags, NULL);
195 return 0;
196 }
197 if (wait) {
198 wait_on_buffer(bh);
199 if (buffer_uptodate(bh))
200 return 0;
201 return -EIO;
202 }
203 return 0;
204}
205
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500206/*
zhangyi (F)8394a6a2020-09-24 15:33:37 +0800207 * This works like __bread_gfp() except it uses ERR_PTR for error
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500208 * returns. Currently with sb_bread it's impossible to distinguish
209 * between ENOMEM and EIO situations (since both result in a NULL
210 * return.
211 */
zhangyi (F)8394a6a2020-09-24 15:33:37 +0800212static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
213 sector_t block, int op_flags,
214 gfp_t gfp)
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500215{
zhangyi (F)2d069c02020-09-24 15:33:33 +0800216 struct buffer_head *bh;
217 int ret;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500218
zhangyi (F)8394a6a2020-09-24 15:33:37 +0800219 bh = sb_getblk_gfp(sb, block, gfp);
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500220 if (bh == NULL)
221 return ERR_PTR(-ENOMEM);
Theodore Ts'ocf2834a2019-12-14 16:42:52 -0500222 if (ext4_buffer_uptodate(bh))
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500223 return bh;
zhangyi (F)2d069c02020-09-24 15:33:33 +0800224
225 ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
226 if (ret) {
227 put_bh(bh);
228 return ERR_PTR(ret);
229 }
230 return bh;
Theodore Ts'ofb265c92018-11-25 17:20:31 -0500231}
232
zhangyi (F)8394a6a2020-09-24 15:33:37 +0800233struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
234 int op_flags)
235{
236 return __ext4_sb_bread_gfp(sb, block, op_flags, __GFP_MOVABLE);
237}
238
239struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
240 sector_t block)
241{
242 return __ext4_sb_bread_gfp(sb, block, 0, 0);
243}
244
zhangyi (F)5df1d412020-09-24 15:33:35 +0800245void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
246{
247 struct buffer_head *bh = sb_getblk_gfp(sb, block, 0);
248
249 if (likely(bh)) {
250 ext4_read_bh_lock(bh, REQ_RAHEAD, false);
251 brelse(bh);
252 }
Stephen Hemmingerc1978552014-05-12 10:50:23 -0400253}
254
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400255static int ext4_verify_csum_type(struct super_block *sb,
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400256 struct ext4_super_block *es)
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400257{
258 if (!ext4_has_feature_metadata_csum(sb))
259 return 1;
260
261 return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400262}
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400263
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400264static __le32 ext4_superblock_csum(struct super_block *sb,
265 struct ext4_super_block *es)
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -0400266{
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400267 struct ext4_sb_info *sbi = EXT4_SB(sb);
268 int offset = offsetof(struct ext4_super_block, s_checksum);
269 __u32 csum;
270
271 csum = ext4_chksum(sbi, ~0, (char *)es, offset);
Theodore Ts'o9933fc02011-08-01 08:45:02 -0400272
273 return cpu_to_le32(csum);
274}
275
Joe Perches8be04b92013-06-19 12:15:53 -0700276static int ext4_superblock_csum_verify(struct super_block *sb,
Theodore Ts'o9933fc02011-08-01 08:45:02 -0400277 struct ext4_super_block *es)
278{
279 if (!ext4_has_metadata_csum(sb))
280 return 1;
281
282 return es->s_checksum == ext4_superblock_csum(sb, es);
283}
284
285void ext4_superblock_csum_set(struct super_block *sb)
Joe Perches8be04b92013-06-19 12:15:53 -0700286{
Theodore Ts'o9933fc02011-08-01 08:45:02 -0400287 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
288
289 if (!ext4_has_metadata_csum(sb))
290 return;
291
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700292 es->s_checksum = ext4_superblock_csum(sb, es);
293}
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700294
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700295ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
296 struct ext4_group_desc *bg)
297{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400298 return le32_to_cpu(bg->bg_block_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700299 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400300 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700301}
302
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700303ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
304 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700305{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400306 return le32_to_cpu(bg->bg_inode_bitmap_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700307 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400308 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700309}
310
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700311ext4_fsblk_t ext4_inode_table(struct super_block *sb,
312 struct ext4_group_desc *bg)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700313{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400314 return le32_to_cpu(bg->bg_inode_table_lo) |
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700315 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400316 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700317}
318
Theodore Ts'o021b65b2011-09-09 19:08:51 -0400319__u32 ext4_free_group_clusters(struct super_block *sb,
320 struct ext4_group_desc *bg)
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500321{
322 return le16_to_cpu(bg->bg_free_blocks_count_lo) |
323 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400324 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500325}
326
327__u32 ext4_free_inodes_count(struct super_block *sb,
328 struct ext4_group_desc *bg)
329{
330 return le16_to_cpu(bg->bg_free_inodes_count_lo) |
331 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400332 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500333}
334
335__u32 ext4_used_dirs_count(struct super_block *sb,
336 struct ext4_group_desc *bg)
337{
338 return le16_to_cpu(bg->bg_used_dirs_count_lo) |
339 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400340 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500341}
342
343__u32 ext4_itable_unused_count(struct super_block *sb,
344 struct ext4_group_desc *bg)
345{
346 return le16_to_cpu(bg->bg_itable_unused_lo) |
347 (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
Andreas Dilger0b8e58a2009-06-03 17:59:28 -0400348 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500349}
350
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700351void ext4_block_bitmap_set(struct super_block *sb,
352 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700353{
Aneesh Kumar K.V3a145892007-10-16 18:38:25 -0400354 bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700355 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
356 bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700357}
358
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700359void ext4_inode_bitmap_set(struct super_block *sb,
360 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700361{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400362 bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700363 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
364 bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700365}
366
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700367void ext4_inode_table_set(struct super_block *sb,
368 struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700369{
Aneesh Kumar K.V5272f832007-10-16 18:38:25 -0400370 bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
Alexandre Ratchov8fadc142006-10-11 01:21:15 -0700371 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
372 bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -0700373}
374
Theodore Ts'o021b65b2011-09-09 19:08:51 -0400375void ext4_free_group_clusters_set(struct super_block *sb,
376 struct ext4_group_desc *bg, __u32 count)
Aneesh Kumar K.V560671a2009-01-05 22:20:24 -0500377{
378 bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
379 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
380 bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
381}
382
383void ext4_free_inodes_set(struct super_block *sb,
384 struct ext4_group_desc *bg, __u32 count)
385{
386 bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
387 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
388 bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
389}
390
391void ext4_used_dirs_set(struct super_block *sb,
392 struct ext4_group_desc *bg, __u32 count)
393{
394 bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
395 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
396 bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
397}
398
399void ext4_itable_unused_set(struct super_block *sb,
400 struct ext4_group_desc *bg, __u32 count)
401{
402 bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
403 if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
404 bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
405}
406
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400407static void __ext4_update_tstamp(__le32 *lo, __u8 *hi)
408{
409 time64_t now = ktime_get_real_seconds();
410
411 now = clamp_val(now, 0, (1ull << 40) - 1);
412
413 *lo = cpu_to_le32(lower_32_bits(now));
414 *hi = upper_32_bits(now);
415}
416
417static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
418{
419 return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
420}
421#define ext4_update_tstamp(es, tstamp) \
422 __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
423#define ext4_get_tstamp(es, tstamp) \
424 __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
Curt Wohlgemuthd3d1faf2009-09-29 11:01:03 -0400425
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400426static void __save_error_info(struct super_block *sb, int error,
427 __u32 ino, __u64 block,
428 const char *func, unsigned int line)
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400429{
430 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400431 int err;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400432
433 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
Theodore Ts'o1b466172015-05-14 18:37:30 -0400434 if (bdev_read_only(sb->s_bdev))
435 return;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400436 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400437 ext4_update_tstamp(es, s_last_error_time);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400438 strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
439 es->s_last_error_line = cpu_to_le32(line);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400440 es->s_last_error_ino = cpu_to_le32(ino);
441 es->s_last_error_block = cpu_to_le64(block);
442 switch (error) {
443 case EIO:
444 err = EXT4_ERR_EIO;
445 break;
446 case ENOMEM:
447 err = EXT4_ERR_ENOMEM;
448 break;
449 case EFSBADCRC:
450 err = EXT4_ERR_EFSBADCRC;
451 break;
452 case 0:
453 case EFSCORRUPTED:
454 err = EXT4_ERR_EFSCORRUPTED;
455 break;
456 case ENOSPC:
457 err = EXT4_ERR_ENOSPC;
458 break;
459 case ENOKEY:
460 err = EXT4_ERR_ENOKEY;
461 break;
462 case EROFS:
463 err = EXT4_ERR_EROFS;
464 break;
465 case EFBIG:
466 err = EXT4_ERR_EFBIG;
467 break;
468 case EEXIST:
469 err = EXT4_ERR_EEXIST;
470 break;
471 case ERANGE:
472 err = EXT4_ERR_ERANGE;
473 break;
474 case EOVERFLOW:
475 err = EXT4_ERR_EOVERFLOW;
476 break;
477 case EBUSY:
478 err = EXT4_ERR_EBUSY;
479 break;
480 case ENOTDIR:
481 err = EXT4_ERR_ENOTDIR;
482 break;
483 case ENOTEMPTY:
484 err = EXT4_ERR_ENOTEMPTY;
485 break;
486 case ESHUTDOWN:
487 err = EXT4_ERR_ESHUTDOWN;
488 break;
489 case EFAULT:
490 err = EXT4_ERR_EFAULT;
491 break;
492 default:
493 err = EXT4_ERR_UNKNOWN;
494 }
495 es->s_last_error_errcode = err;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400496 if (!es->s_first_error_time) {
497 es->s_first_error_time = es->s_last_error_time;
Arnd Bergmann6a0678a2018-07-29 15:51:48 -0400498 es->s_first_error_time_hi = es->s_last_error_time_hi;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400499 strncpy(es->s_first_error_func, func,
500 sizeof(es->s_first_error_func));
501 es->s_first_error_line = cpu_to_le32(line);
502 es->s_first_error_ino = es->s_last_error_ino;
503 es->s_first_error_block = es->s_last_error_block;
Theodore Ts'o878520a2019-11-19 21:54:15 -0500504 es->s_first_error_errcode = es->s_last_error_errcode;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400505 }
Theodore Ts'o66e61a92010-07-27 11:56:04 -0400506 /*
507 * Start the daily error reporting function if it hasn't been
508 * started already
509 */
510 if (!es->s_error_count)
511 mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
Wei Yongjunba39ebb2012-09-27 09:37:53 -0400512 le32_add_cpu(&es->s_error_count, 1);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400513}
514
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400515static void save_error_info(struct super_block *sb, int error,
516 __u32 ino, __u64 block,
517 const char *func, unsigned int line)
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400518{
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400519 __save_error_info(sb, error, ino, block, func, line);
Eric Sandeenc96e2b82020-03-18 14:19:38 -0500520 if (!bdev_read_only(sb->s_bdev))
521 ext4_commit_super(sb, 1);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400522}
523
Theodore Ts'obdfe0cbd2015-08-16 10:03:57 -0400524/*
525 * The del_gendisk() function uninitializes the disk-specific data
526 * structures, including the bdi structure, without telling anyone
527 * else. Once this happens, any attempt to call mark_buffer_dirty()
528 * (for example, by ext4_commit_super), will cause a kernel OOPS.
529 * This is a kludge to prevent these oops until we can put in a proper
530 * hook in del_gendisk() to inform the VFS and file system layers.
531 */
532static int block_device_ejected(struct super_block *sb)
533{
534 struct inode *bd_inode = sb->s_bdev->bd_inode;
535 struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
536
537 return bdi->dev == NULL;
538}
539
Bobi Jam18aadd42012-02-20 17:53:02 -0500540static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
541{
542 struct super_block *sb = journal->j_private;
543 struct ext4_sb_info *sbi = EXT4_SB(sb);
544 int error = is_journal_aborted(journal);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400545 struct ext4_journal_cb_entry *jce;
Bobi Jam18aadd42012-02-20 17:53:02 -0500546
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400547 BUG_ON(txn->t_state == T_FINISHED);
Daeho Jeonga0154342017-06-22 23:54:33 -0400548
549 ext4_process_freed_data(sb, txn->t_tid);
550
Bobi Jam18aadd42012-02-20 17:53:02 -0500551 spin_lock(&sbi->s_md_lock);
Dmitry Monakhov5d3ee202013-04-03 22:08:52 -0400552 while (!list_empty(&txn->t_private_list)) {
553 jce = list_entry(txn->t_private_list.next,
554 struct ext4_journal_cb_entry, jce_list);
Bobi Jam18aadd42012-02-20 17:53:02 -0500555 list_del_init(&jce->jce_list);
556 spin_unlock(&sbi->s_md_lock);
557 jce->jce_func(sb, jce, error);
558 spin_lock(&sbi->s_md_lock);
559 }
560 spin_unlock(&sbi->s_md_lock);
561}
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400562
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -0300563/*
564 * This writepage callback for write_cache_pages()
565 * takes care of a few cases after page cleaning.
566 *
567 * write_cache_pages() already checks for dirty pages
568 * and calls clear_page_dirty_for_io(), which we want,
569 * to write protect the pages.
570 *
571 * However, we may have to redirty a page (see below.)
572 */
573static int ext4_journalled_writepage_callback(struct page *page,
574 struct writeback_control *wbc,
575 void *data)
576{
577 transaction_t *transaction = (transaction_t *) data;
578 struct buffer_head *bh, *head;
579 struct journal_head *jh;
580
581 bh = head = page_buffers(page);
582 do {
583 /*
584 * We have to redirty a page in these cases:
585 * 1) If buffer is dirty, it means the page was dirty because it
586 * contains a buffer that needs checkpointing. So the dirty bit
587 * needs to be preserved so that checkpointing writes the buffer
588 * properly.
589 * 2) If buffer is not part of the committing transaction
590 * (we may have just accidentally come across this buffer because
591 * inode range tracking is not exact) or if the currently running
592 * transaction already contains this buffer as well, dirty bit
593 * needs to be preserved so that the buffer gets writeprotected
594 * properly on running transaction's commit.
595 */
596 jh = bh2jh(bh);
597 if (buffer_dirty(bh) ||
598 (jh && (jh->b_transaction != transaction ||
599 jh->b_next_transaction))) {
600 redirty_page_for_writepage(wbc, page);
601 goto out;
602 }
603 } while ((bh = bh->b_this_page) != head);
604
605out:
606 return AOP_WRITEPAGE_ACTIVATE;
607}
608
609static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
610{
611 struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
612 struct writeback_control wbc = {
613 .sync_mode = WB_SYNC_ALL,
614 .nr_to_write = LONG_MAX,
615 .range_start = jinode->i_dirty_start,
616 .range_end = jinode->i_dirty_end,
617 };
618
619 return write_cache_pages(mapping, &wbc,
620 ext4_journalled_writepage_callback,
621 jinode->i_transaction);
622}
623
624static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
625{
626 int ret;
627
628 if (ext4_should_journal_data(jinode->i_vfs_inode))
629 ret = ext4_journalled_submit_inode_data_buffers(jinode);
630 else
631 ret = jbd2_journal_submit_inode_data_buffers(jinode);
632
633 return ret;
634}
635
636static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
637{
638 int ret = 0;
639
640 if (!ext4_should_journal_data(jinode->i_vfs_inode))
641 ret = jbd2_journal_finish_inode_data_buffers(jinode);
642
643 return ret;
644}
645
Jan Kara1dc10972019-03-14 23:46:05 -0400646static bool system_going_down(void)
647{
648 return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
649 || system_state == SYSTEM_RESTART;
650}
651
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700652/* Deal with the reporting of failure conditions on a filesystem such as
653 * inconsistencies detected or read IO failures.
654 *
655 * On ext2, we can store the error state of the filesystem in the
Mingming Cao617ba132006-10-11 01:20:53 -0700656 * superblock. That is not possible on ext4, because we may have other
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700657 * write ordering constraints on the superblock which prevent us from
658 * writing it out straight away; and given that the journal is about to
659 * be aborted, we can't rely on the current, or future, transactions to
660 * write out the superblock safely.
661 *
Mingming Caodab291a2006-10-11 01:21:01 -0700662 * 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 -0200663 * the journal instead. On recovery, the journal will complain about
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700664 * that error until we've noted it down and cleared it.
665 */
666
Mingming Cao617ba132006-10-11 01:20:53 -0700667static void ext4_handle_error(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700668{
Jan Kara0b3ade02020-11-27 12:33:54 +0100669 journal_t *journal = EXT4_SB(sb)->s_journal;
670
Theodore Ts'o327eaf72018-06-12 23:34:57 -0400671 if (test_opt(sb, WARN_ON_ERROR))
672 WARN_ON_ONCE(1);
673
Jan Kara0b3ade02020-11-27 12:33:54 +0100674 if (sb_rdonly(sb) || test_opt(sb, ERRORS_CONT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700675 return;
676
Jan Kara0b3ade02020-11-27 12:33:54 +0100677 ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
678 if (journal)
679 jbd2_journal_abort(journal, -EIO);
Jan Kara1dc10972019-03-14 23:46:05 -0400680 /*
681 * We force ERRORS_RO behavior when system is rebooting. Otherwise we
682 * could panic during 'reboot -f' as the underlying device got already
683 * disabled.
684 */
685 if (test_opt(sb, ERRORS_RO) || system_going_down()) {
Eric Sandeenb31e1552009-06-04 17:36:36 -0400686 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dmitry Monakhov4418e1412013-06-12 22:38:04 -0400687 /*
688 * Make sure updated value of ->s_mount_flags will be visible
689 * before ->s_flags update
690 */
691 smp_wmb();
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800692 sb->s_flags |= SB_RDONLY;
Jan Kara1dc10972019-03-14 23:46:05 -0400693 } else if (test_opt(sb, ERRORS_PANIC)) {
Mingming Cao617ba132006-10-11 01:20:53 -0700694 panic("EXT4-fs (device %s): panic forced after error\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700695 sb->s_id);
Daeho Jeong4327ba52015-10-18 17:02:56 -0400696 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700697}
698
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400699#define ext4_error_ratelimit(sb) \
700 ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
701 "EXT4-fs error")
702
Eric Sandeen12062dd2010-02-15 14:19:27 -0500703void __ext4_error(struct super_block *sb, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400704 unsigned int line, int error, __u64 block,
705 const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700706{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500707 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700708 va_list args;
709
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500710 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
711 return;
712
Theodore Ts'occf0f322018-02-18 20:53:23 -0500713 trace_ext4_error(sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400714 if (ext4_error_ratelimit(sb)) {
715 va_start(args, fmt);
716 vaf.fmt = fmt;
717 vaf.va = &args;
718 printk(KERN_CRIT
719 "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
720 sb->s_id, function, line, current->comm, &vaf);
721 va_end(args);
722 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400723 save_error_info(sb, error, 0, block, function, line);
Mingming Cao617ba132006-10-11 01:20:53 -0700724 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700725}
726
Joe Perchese7c96e82013-07-01 08:12:37 -0400727void __ext4_error_inode(struct inode *inode, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400728 unsigned int line, ext4_fsblk_t block, int error,
Joe Perchese7c96e82013-07-01 08:12:37 -0400729 const char *fmt, ...)
Frank Mayhar273df552010-03-02 11:46:09 -0500730{
731 va_list args;
Theodore Ts'of7c21172011-01-10 12:10:55 -0500732 struct va_format vaf;
Frank Mayhar273df552010-03-02 11:46:09 -0500733
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500734 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
735 return;
736
Theodore Ts'occf0f322018-02-18 20:53:23 -0500737 trace_ext4_error(inode->i_sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400738 if (ext4_error_ratelimit(inode->i_sb)) {
739 va_start(args, fmt);
740 vaf.fmt = fmt;
741 vaf.va = &args;
742 if (block)
743 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
744 "inode #%lu: block %llu: comm %s: %pV\n",
745 inode->i_sb->s_id, function, line, inode->i_ino,
746 block, current->comm, &vaf);
747 else
748 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
749 "inode #%lu: comm %s: %pV\n",
750 inode->i_sb->s_id, function, line, inode->i_ino,
751 current->comm, &vaf);
752 va_end(args);
753 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400754 save_error_info(inode->i_sb, error, inode->i_ino, block,
755 function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500756 ext4_handle_error(inode->i_sb);
757}
758
Joe Perchese7c96e82013-07-01 08:12:37 -0400759void __ext4_error_file(struct file *file, const char *function,
760 unsigned int line, ext4_fsblk_t block,
761 const char *fmt, ...)
Frank Mayhar273df552010-03-02 11:46:09 -0500762{
763 va_list args;
Theodore Ts'of7c21172011-01-10 12:10:55 -0500764 struct va_format vaf;
Al Viro496ad9a2013-01-23 17:07:38 -0500765 struct inode *inode = file_inode(file);
Frank Mayhar273df552010-03-02 11:46:09 -0500766 char pathname[80], *path;
767
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500768 if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
769 return;
770
Theodore Ts'occf0f322018-02-18 20:53:23 -0500771 trace_ext4_error(inode->i_sb, function, line);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400772 if (ext4_error_ratelimit(inode->i_sb)) {
Miklos Szeredi9bf39ab2015-06-19 10:29:13 +0200773 path = file_path(file, pathname, sizeof(pathname));
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400774 if (IS_ERR(path))
775 path = "(unknown)";
776 va_start(args, fmt);
777 vaf.fmt = fmt;
778 vaf.va = &args;
779 if (block)
780 printk(KERN_CRIT
781 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
782 "block %llu: comm %s: path %s: %pV\n",
783 inode->i_sb->s_id, function, line, inode->i_ino,
784 block, current->comm, path, &vaf);
785 else
786 printk(KERN_CRIT
787 "EXT4-fs error (device %s): %s:%d: inode #%lu: "
788 "comm %s: path %s: %pV\n",
789 inode->i_sb->s_id, function, line, inode->i_ino,
790 current->comm, path, &vaf);
791 va_end(args);
792 }
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400793 save_error_info(inode->i_sb, EFSCORRUPTED, inode->i_ino, block,
794 function, line);
Frank Mayhar273df552010-03-02 11:46:09 -0500795 ext4_handle_error(inode->i_sb);
796}
797
Theodore Ts'o722887d2013-02-08 13:00:31 -0500798const char *ext4_decode_error(struct super_block *sb, int errno,
799 char nbuf[16])
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700800{
801 char *errstr = NULL;
802
803 switch (errno) {
Darrick J. Wong6a797d22015-10-17 16:16:04 -0400804 case -EFSCORRUPTED:
805 errstr = "Corrupt filesystem";
806 break;
807 case -EFSBADCRC:
808 errstr = "Filesystem failed CRC";
809 break;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700810 case -EIO:
811 errstr = "IO failure";
812 break;
813 case -ENOMEM:
814 errstr = "Out of memory";
815 break;
816 case -EROFS:
Theodore Ts'o78f1ddb2009-07-27 23:09:47 -0400817 if (!sb || (EXT4_SB(sb)->s_journal &&
818 EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700819 errstr = "Journal has aborted";
820 else
821 errstr = "Readonly filesystem";
822 break;
823 default:
824 /* If the caller passed in an extra buffer for unknown
825 * errors, textualise them now. Else we just return
826 * NULL. */
827 if (nbuf) {
828 /* Check for truncated error codes... */
829 if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
830 errstr = nbuf;
831 }
832 break;
833 }
834
835 return errstr;
836}
837
Mingming Cao617ba132006-10-11 01:20:53 -0700838/* __ext4_std_error decodes expected errors from journaling functions
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700839 * automatically and invokes the appropriate error response. */
840
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400841void __ext4_std_error(struct super_block *sb, const char *function,
842 unsigned int line, int errno)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700843{
844 char nbuf[16];
845 const char *errstr;
846
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500847 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
848 return;
849
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700850 /* Special case: if the error is EROFS, and we're not already
851 * inside a transaction, then there's really no point in logging
852 * an error. */
David Howellsbc98a422017-07-17 08:45:34 +0100853 if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700854 return;
855
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400856 if (ext4_error_ratelimit(sb)) {
857 errstr = ext4_decode_error(sb, errno, nbuf);
858 printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
859 sb->s_id, function, line, errstr);
860 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700861
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400862 save_error_info(sb, -errno, 0, 0, function, line);
Mingming Cao617ba132006-10-11 01:20:53 -0700863 ext4_handle_error(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700864}
865
866/*
Mingming Cao617ba132006-10-11 01:20:53 -0700867 * ext4_abort is a much stronger failure handler than ext4_error. The
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700868 * abort function may be used to deal with unrecoverable failures such
869 * as journal IO errors or ENOMEM at a critical moment in log management.
870 *
871 * We unconditionally force the filesystem into an ABORT|READONLY state,
872 * unless the error response on the fs has been set to panic in which
873 * case we take the easy way out and panic immediately.
874 */
875
Theodore Ts'oc67d8592010-06-29 11:07:07 -0400876void __ext4_abort(struct super_block *sb, const char *function,
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400877 unsigned int line, int error, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700878{
Joe Perches651e1c32016-10-12 23:12:53 -0400879 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700880 va_list args;
881
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500882 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
883 return;
884
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400885 save_error_info(sb, error, 0, 0, function, line);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700886 va_start(args, fmt);
Joe Perches651e1c32016-10-12 23:12:53 -0400887 vaf.fmt = fmt;
888 vaf.va = &args;
889 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
890 sb->s_id, function, line, &vaf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700891 va_end(args);
892
David Howellsbc98a422017-07-17 08:45:34 +0100893 if (sb_rdonly(sb) == 0) {
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -0800894 ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
zhangyi (F)7b97d862020-06-09 15:35:40 +0800895 if (EXT4_SB(sb)->s_journal)
896 jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
897
898 ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
Dmitry Monakhov4418e1412013-06-12 22:38:04 -0400899 /*
900 * Make sure updated value of ->s_mount_flags will be visible
901 * before ->s_flags update
902 */
903 smp_wmb();
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800904 sb->s_flags |= SB_RDONLY;
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400905 }
zhangyi (F)7b97d862020-06-09 15:35:40 +0800906 if (test_opt(sb, ERRORS_PANIC) && !system_going_down())
Mingming Cao617ba132006-10-11 01:20:53 -0700907 panic("EXT4-fs panic from previous error\n");
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700908}
909
Joe Perchese7c96e82013-07-01 08:12:37 -0400910void __ext4_msg(struct super_block *sb,
911 const char *prefix, const char *fmt, ...)
Eric Sandeenb31e1552009-06-04 17:36:36 -0400912{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500913 struct va_format vaf;
Eric Sandeenb31e1552009-06-04 17:36:36 -0400914 va_list args;
915
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +0000916 atomic_inc(&EXT4_SB(sb)->s_msg_count);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400917 if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
918 return;
919
Eric Sandeenb31e1552009-06-04 17:36:36 -0400920 va_start(args, fmt);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500921 vaf.fmt = fmt;
922 vaf.va = &args;
923 printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
Eric Sandeenb31e1552009-06-04 17:36:36 -0400924 va_end(args);
925}
926
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +0000927static int ext4_warning_ratelimit(struct super_block *sb)
928{
929 atomic_inc(&EXT4_SB(sb)->s_warning_count);
930 return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
931 "EXT4-fs warning");
932}
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400933
Eric Sandeen12062dd2010-02-15 14:19:27 -0500934void __ext4_warning(struct super_block *sb, const char *function,
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400935 unsigned int line, const char *fmt, ...)
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700936{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500937 struct va_format vaf;
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700938 va_list args;
939
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400940 if (!ext4_warning_ratelimit(sb))
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400941 return;
942
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700943 va_start(args, fmt);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500944 vaf.fmt = fmt;
945 vaf.va = &args;
946 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
947 sb->s_id, function, line, &vaf);
Dave Kleikampac27a0e2006-10-11 01:20:50 -0700948 va_end(args);
949}
950
Andreas Dilgerb03a2f72015-06-15 14:50:26 -0400951void __ext4_warning_inode(const struct inode *inode, const char *function,
952 unsigned int line, const char *fmt, ...)
953{
954 struct va_format vaf;
955 va_list args;
956
957 if (!ext4_warning_ratelimit(inode->i_sb))
958 return;
959
960 va_start(args, fmt);
961 vaf.fmt = fmt;
962 vaf.va = &args;
963 printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
964 "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
965 function, line, inode->i_ino, current->comm, &vaf);
966 va_end(args);
967}
968
Theodore Ts'oe29136f2010-06-29 12:54:28 -0400969void __ext4_grp_locked_error(const char *function, unsigned int line,
970 struct super_block *sb, ext4_group_t grp,
971 unsigned long ino, ext4_fsblk_t block,
972 const char *fmt, ...)
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500973__releases(bitlock)
974__acquires(bitlock)
975{
Joe Perches0ff2ea72010-12-19 22:43:19 -0500976 struct va_format vaf;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500977 va_list args;
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500978
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500979 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
980 return;
981
Theodore Ts'occf0f322018-02-18 20:53:23 -0500982 trace_ext4_error(sb, function, line);
Theodore Ts'o54d3adb2020-03-28 19:33:43 -0400983 __save_error_info(sb, EFSCORRUPTED, ino, block, function, line);
Joe Perches0ff2ea72010-12-19 22:43:19 -0500984
Theodore Ts'oefbed4d2013-10-17 21:11:01 -0400985 if (ext4_error_ratelimit(sb)) {
986 va_start(args, fmt);
987 vaf.fmt = fmt;
988 vaf.va = &args;
989 printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
990 sb->s_id, function, line, grp);
991 if (ino)
992 printk(KERN_CONT "inode %lu: ", ino);
993 if (block)
994 printk(KERN_CONT "block %llu:",
995 (unsigned long long) block);
996 printk(KERN_CONT "%pV\n", &vaf);
997 va_end(args);
998 }
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -0500999
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001000 if (test_opt(sb, WARN_ON_ERROR))
1001 WARN_ON_ONCE(1);
1002
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001003 if (test_opt(sb, ERRORS_CONT)) {
Theodore Ts'oe2d67052009-05-01 00:33:44 -04001004 ext4_commit_super(sb, 0);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001005 return;
1006 }
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04001007
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001008 ext4_unlock_group(sb, grp);
Zhouyi Zhou06f29cc2018-01-10 00:34:19 -05001009 ext4_commit_super(sb, 1);
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001010 ext4_handle_error(sb);
1011 /*
1012 * We only get here in the ERRORS_RO case; relocking the group
1013 * may be dangerous, but nothing bad will happen since the
1014 * filesystem will have already been marked read/only and the
1015 * journal has been aborted. We return 1 as a hint to callers
1016 * who might what to use the return value from
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001017 * ext4_grp_locked_error() to distinguish between the
Aneesh Kumar K.V5d1b1b32009-01-05 22:19:52 -05001018 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
1019 * aggressively from the ext4 function in question, with a
1020 * more appropriate error code.
1021 */
1022 ext4_lock_group(sb, grp);
1023 return;
1024}
1025
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001026void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
1027 ext4_group_t group,
1028 unsigned int flags)
1029{
1030 struct ext4_sb_info *sbi = EXT4_SB(sb);
1031 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
1032 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
Wang Shilong9af0b3d2018-07-29 17:27:45 -04001033 int ret;
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001034
Wang Shilong9af0b3d2018-07-29 17:27:45 -04001035 if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
1036 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
1037 &grp->bb_state);
1038 if (!ret)
1039 percpu_counter_sub(&sbi->s_freeclusters_counter,
1040 grp->bb_free);
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001041 }
1042
Wang Shilong9af0b3d2018-07-29 17:27:45 -04001043 if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
1044 ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
1045 &grp->bb_state);
1046 if (!ret && gdp) {
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001047 int count;
1048
1049 count = ext4_free_inodes_count(sb, gdp);
1050 percpu_counter_sub(&sbi->s_freeinodes_counter,
1051 count);
1052 }
Wang Shilongdb79e6d2018-05-12 11:39:40 -04001053 }
1054}
1055
Mingming Cao617ba132006-10-11 01:20:53 -07001056void ext4_update_dynamic_rev(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001057{
Mingming Cao617ba132006-10-11 01:20:53 -07001058 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001059
Mingming Cao617ba132006-10-11 01:20:53 -07001060 if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001061 return;
1062
Eric Sandeen12062dd2010-02-15 14:19:27 -05001063 ext4_warning(sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001064 "updating to rev %d because of new feature flag, "
1065 "running e2fsck is recommended",
Mingming Cao617ba132006-10-11 01:20:53 -07001066 EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001067
Mingming Cao617ba132006-10-11 01:20:53 -07001068 es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
1069 es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
1070 es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001071 /* leave es->s_feature_*compat flags alone */
1072 /* es->s_uuid will be set by e2fsck if empty */
1073
1074 /*
1075 * The rest of the superblock fields should be zero, and if not it
1076 * means they are likely already in use, so leave them alone. We
1077 * can leave it up to e2fsck to clean up any inconsistencies there.
1078 */
1079}
1080
1081/*
1082 * Open the external journal device
1083 */
Eric Sandeenb31e1552009-06-04 17:36:36 -04001084static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001085{
1086 struct block_device *bdev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001087
Tejun Heod4d77622010-11-13 11:55:18 +01001088 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001089 if (IS_ERR(bdev))
1090 goto fail;
1091 return bdev;
1092
1093fail:
Christoph Hellwigea3edd42020-03-24 08:25:11 +01001094 ext4_msg(sb, KERN_ERR,
1095 "failed to open journal device unknown-block(%u,%u) %ld",
1096 MAJOR(dev), MINOR(dev), PTR_ERR(bdev));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001097 return NULL;
1098}
1099
1100/*
1101 * Release the journal device
1102 */
Al Viro4385bab2013-05-05 22:11:03 -04001103static void ext4_blkdev_put(struct block_device *bdev)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001104{
Al Viro4385bab2013-05-05 22:11:03 -04001105 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001106}
1107
Al Viro4385bab2013-05-05 22:11:03 -04001108static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001109{
1110 struct block_device *bdev;
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001111 bdev = sbi->s_journal_bdev;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001112 if (bdev) {
Al Viro4385bab2013-05-05 22:11:03 -04001113 ext4_blkdev_put(bdev);
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001114 sbi->s_journal_bdev = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001115 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001116}
1117
1118static inline struct inode *orphan_list_entry(struct list_head *l)
1119{
Mingming Cao617ba132006-10-11 01:20:53 -07001120 return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001121}
1122
Mingming Cao617ba132006-10-11 01:20:53 -07001123static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001124{
1125 struct list_head *l;
1126
Eric Sandeenb31e1552009-06-04 17:36:36 -04001127 ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
1128 le32_to_cpu(sbi->s_es->s_last_orphan));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001129
1130 printk(KERN_ERR "sb_info orphan list:\n");
1131 list_for_each(l, &sbi->s_orphan) {
1132 struct inode *inode = orphan_list_entry(l);
1133 printk(KERN_ERR " "
1134 "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
1135 inode->i_sb->s_id, inode->i_ino, inode,
1136 inode->i_mode, inode->i_nlink,
1137 NEXT_ORPHAN(inode));
1138 }
1139}
1140
Jan Kara957153f2017-04-06 15:40:06 +02001141#ifdef CONFIG_QUOTA
1142static int ext4_quota_off(struct super_block *sb, int type);
1143
1144static inline void ext4_quota_off_umount(struct super_block *sb)
1145{
1146 int type;
1147
Jan Kara964edf62017-05-21 22:31:23 -04001148 /* Use our quota_off function to clear inode flags etc. */
1149 for (type = 0; type < EXT4_MAXQUOTAS; type++)
1150 ext4_quota_off(sb, type);
Jan Kara957153f2017-04-06 15:40:06 +02001151}
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001152
1153/*
1154 * This is a helper function which is used in the mount/remount
1155 * codepaths (which holds s_umount) to fetch the quota file name.
1156 */
1157static inline char *get_qf_name(struct super_block *sb,
1158 struct ext4_sb_info *sbi,
1159 int type)
1160{
1161 return rcu_dereference_protected(sbi->s_qf_names[type],
1162 lockdep_is_held(&sb->s_umount));
1163}
Jan Kara957153f2017-04-06 15:40:06 +02001164#else
1165static inline void ext4_quota_off_umount(struct super_block *sb)
1166{
1167}
1168#endif
1169
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04001170static void ext4_put_super(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001171{
Mingming Cao617ba132006-10-11 01:20:53 -07001172 struct ext4_sb_info *sbi = EXT4_SB(sb);
1173 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001174 struct buffer_head **group_desc;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001175 struct flex_groups **flex_groups;
Theodore Ts'o97abd7d2017-02-04 23:38:06 -05001176 int aborted = 0;
Hidehiro Kawaief2cabf2008-10-27 22:53:05 -04001177 int i, err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001178
Lukas Czerner857ac882010-10-27 21:30:05 -04001179 ext4_unregister_li_request(sb);
Jan Kara957153f2017-04-06 15:40:06 +02001180 ext4_quota_off_umount(sb);
Christoph Hellwige0ccfd92010-05-19 07:16:42 -04001181
Jan Kara2e8fa542013-06-04 14:21:02 -04001182 destroy_workqueue(sbi->rsv_conversion_wq);
Mingming Cao4c0425f2009-09-28 15:48:41 -04001183
Ritesh Harjani5e478682020-03-18 11:43:01 +05301184 /*
1185 * Unregister sysfs before destroying jbd2 journal.
1186 * Since we could still access attr_journal_task attribute via sysfs
1187 * path which could have sbi->s_journal->j_task as NULL
1188 */
1189 ext4_unregister_sysfs(sb);
1190
Frank Mayhar03901312009-01-07 00:06:22 -05001191 if (sbi->s_journal) {
Theodore Ts'o97abd7d2017-02-04 23:38:06 -05001192 aborted = is_journal_aborted(sbi->s_journal);
Frank Mayhar03901312009-01-07 00:06:22 -05001193 err = jbd2_journal_destroy(sbi->s_journal);
1194 sbi->s_journal = NULL;
Theodore Ts'o878520a2019-11-19 21:54:15 -05001195 if ((err < 0) && !aborted) {
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04001196 ext4_abort(sb, -err, "Couldn't clean up the journal");
Theodore Ts'o878520a2019-11-19 21:54:15 -05001197 }
Frank Mayhar03901312009-01-07 00:06:22 -05001198 }
Josef Bacikd4edac32009-12-08 21:48:58 -05001199
Zheng Liud3922a72013-07-01 08:12:37 -04001200 ext4_es_unregister_shrinker(sbi);
Al Viro9105bb12013-12-08 20:52:31 -05001201 del_timer_sync(&sbi->s_err_report);
Josef Bacikd4edac32009-12-08 21:48:58 -05001202 ext4_release_system_zone(sb);
1203 ext4_mb_release(sb);
1204 ext4_ext_release(sb);
Josef Bacikd4edac32009-12-08 21:48:58 -05001205
David Howellsbc98a422017-07-17 08:45:34 +01001206 if (!sb_rdonly(sb) && !aborted) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001207 ext4_clear_feature_journal_needs_recovery(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001208 es->s_state = cpu_to_le16(sbi->s_mount_state);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001209 }
David Howellsbc98a422017-07-17 08:45:34 +01001210 if (!sb_rdonly(sb))
Artem Bityutskiya8e25a82012-03-21 22:29:15 -04001211 ext4_commit_super(sb, 1);
1212
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001213 rcu_read_lock();
1214 group_desc = rcu_dereference(sbi->s_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001215 for (i = 0; i < sbi->s_gdb_count; i++)
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001216 brelse(group_desc[i]);
1217 kvfree(group_desc);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08001218 flex_groups = rcu_dereference(sbi->s_flex_groups);
1219 if (flex_groups) {
1220 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
1221 kvfree(flex_groups[i]);
1222 kvfree(flex_groups);
1223 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05001224 rcu_read_unlock();
Theodore Ts'o57042652011-09-09 18:56:51 -04001225 percpu_counter_destroy(&sbi->s_freeclusters_counter);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001226 percpu_counter_destroy(&sbi->s_freeinodes_counter);
1227 percpu_counter_destroy(&sbi->s_dirs_counter);
Theodore Ts'o57042652011-09-09 18:56:51 -04001228 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
Eric Whitney4b313952021-02-18 10:11:32 -05001229 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
Eric Biggersbbd55932020-02-19 10:30:46 -08001230 percpu_free_rwsem(&sbi->s_writepages_rwsem);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001231#ifdef CONFIG_QUOTA
Jan Karaa2d4a642014-09-11 11:15:15 -04001232 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001233 kfree(get_qf_name(sb, sbi, i));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001234#endif
1235
1236 /* Debugging code just in case the in-memory inode orphan list
1237 * isn't empty. The on-disk one can be non-empty if we've
1238 * detected an error and taken the fs readonly, but the
1239 * in-memory list had better be clean by this point. */
1240 if (!list_empty(&sbi->s_orphan))
1241 dump_orphan_list(sb, sbi);
1242 J_ASSERT(list_empty(&sbi->s_orphan));
1243
Theodore Ts'o89d96a62015-06-20 22:50:33 -04001244 sync_blockdev(sb->s_bdev);
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001245 invalidate_bdev(sb->s_bdev);
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001246 if (sbi->s_journal_bdev && sbi->s_journal_bdev != sb->s_bdev) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001247 /*
1248 * Invalidate the journal device's buffers. We don't want them
1249 * floating about in memory - the physical journal device may
1250 * hotswapped, and it breaks the `ro-after' testing code.
1251 */
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08001252 sync_blockdev(sbi->s_journal_bdev);
1253 invalidate_bdev(sbi->s_journal_bdev);
Mingming Cao617ba132006-10-11 01:20:53 -07001254 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001255 }
Chengguang Xu50c15df2018-12-04 00:24:42 -05001256
1257 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
1258 sbi->s_ea_inode_cache = NULL;
1259
1260 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
1261 sbi->s_ea_block_cache = NULL;
1262
Pavel Skripkin5e4f5132021-04-30 21:50:46 +03001263 ext4_stop_mmpd(sbi);
1264
Eric Sandeen9060dd22016-11-26 14:24:51 -05001265 brelse(sbi->s_sbh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001266 sb->s_fs_info = NULL;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001267 /*
1268 * Now that we are completely done shutting down the
1269 * superblock, we need to actually destroy the kobject.
1270 */
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04001271 kobject_put(&sbi->s_kobj);
1272 wait_for_completion(&sbi->s_kobj_unregister);
Darrick J. Wong04419842012-04-29 18:27:10 -04001273 if (sbi->s_chksum_driver)
1274 crypto_free_shash(sbi->s_chksum_driver);
Pekka Enberg705895b2009-02-15 18:07:52 -05001275 kfree(sbi->s_blockgroup_lock);
Dan Williams5e405592017-08-24 16:42:48 -07001276 fs_put_dax(sbi->s_daxdev);
Eric Biggersac4acb12020-09-16 21:11:35 -07001277 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04001278#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00001279 utf8_unload(sb->s_encoding);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04001280#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001281 kfree(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001282}
1283
Christoph Lametere18b8902006-12-06 20:33:20 -08001284static struct kmem_cache *ext4_inode_cachep;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001285
1286/*
1287 * Called inside transaction, so use GFP_NOFS
1288 */
Mingming Cao617ba132006-10-11 01:20:53 -07001289static struct inode *ext4_alloc_inode(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001290{
Mingming Cao617ba132006-10-11 01:20:53 -07001291 struct ext4_inode_info *ei;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001292
Christoph Lametere6b4f8d2006-12-06 20:33:14 -08001293 ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001294 if (!ei)
1295 return NULL;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001296
Jeff Laytonee73f9a2018-01-09 08:21:39 -05001297 inode_set_iversion(&ei->vfs_inode, 1);
Theodore Ts'o202ee5d2014-04-21 14:37:55 -04001298 spin_lock_init(&ei->i_raw_lock);
Alex Tomasc9de5602008-01-29 00:19:52 -05001299 INIT_LIST_HEAD(&ei->i_prealloc_list);
brookxu27bc4462020-08-17 15:36:15 +08001300 atomic_set(&ei->i_prealloc_active, 0);
Alex Tomasc9de5602008-01-29 00:19:52 -05001301 spin_lock_init(&ei->i_prealloc_lock);
Zheng Liu9a26b662012-11-08 21:57:30 -05001302 ext4_es_init_tree(&ei->i_es_tree);
1303 rwlock_init(&ei->i_es_lock);
Zheng Liuedaa53c2014-11-25 11:45:37 -05001304 INIT_LIST_HEAD(&ei->i_es_list);
Zheng Liueb68d0e2014-09-01 22:26:49 -04001305 ei->i_es_all_nr = 0;
Zheng Liuedaa53c2014-11-25 11:45:37 -05001306 ei->i_es_shk_nr = 0;
Jan Karadd475922014-11-25 11:51:23 -05001307 ei->i_es_shrink_lblk = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001308 ei->i_reserved_data_blocks = 0;
Mingming Caod2a17632008-07-14 17:52:37 -04001309 spin_lock_init(&(ei->i_block_reservation_lock));
Eric Whitney1dc0aa42018-10-01 14:17:41 -04001310 ext4_init_pending_tree(&ei->i_pending_tree);
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001311#ifdef CONFIG_QUOTA
1312 ei->i_reserved_quota = 0;
Jan Kara96c7e0d2014-09-29 14:58:25 +02001313 memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
Dmitry Monakhova9e7f442009-12-14 15:21:14 +03001314#endif
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05001315 ei->jinode = NULL;
Jan Kara2e8fa542013-06-04 14:21:02 -04001316 INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
Jiaying Zhang744692d2010-03-04 16:14:02 -05001317 spin_lock_init(&ei->i_completed_io_lock);
Jan Karab436b9b2009-12-08 23:51:10 -05001318 ei->i_sync_tid = 0;
1319 ei->i_datasync_tid = 0;
Dmitry Monakhove27f41e2012-09-28 23:24:52 -04001320 atomic_set(&ei->i_unwritten, 0);
Jan Kara2e8fa542013-06-04 14:21:02 -04001321 INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07001322 ext4_fc_init_inode(&ei->vfs_inode);
1323 mutex_init(&ei->i_fc_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001324 return &ei->vfs_inode;
1325}
1326
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001327static int ext4_drop_inode(struct inode *inode)
1328{
1329 int drop = generic_drop_inode(inode);
1330
Eric Biggers29b36922019-08-04 19:35:48 -07001331 if (!drop)
1332 drop = fscrypt_drop_inode(inode);
1333
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001334 trace_ext4_drop_inode(inode, drop);
1335 return drop;
1336}
1337
Al Viro94053132019-04-15 19:28:34 -04001338static void ext4_free_in_core_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001339{
Eric Biggers2c58d542019-04-10 13:21:15 -07001340 fscrypt_free_inode(inode);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07001341 if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
1342 pr_warn("%s: inode %ld still in fc list",
1343 __func__, inode->i_ino);
1344 }
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001345 kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
1346}
1347
Mingming Cao617ba132006-10-11 01:20:53 -07001348static void ext4_destroy_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001349{
Vasily Averin9f7dd932007-07-15 23:40:45 -07001350 if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04001351 ext4_msg(inode->i_sb, KERN_ERR,
1352 "Inode %lu (%p): orphan list check failed!",
1353 inode->i_ino, EXT4_I(inode));
Vasily Averin9f7dd932007-07-15 23:40:45 -07001354 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
1355 EXT4_I(inode), sizeof(struct ext4_inode_info),
1356 true);
1357 dump_stack();
1358 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001359}
1360
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07001361static void init_once(void *foo)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001362{
Mingming Cao617ba132006-10-11 01:20:53 -07001363 struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001364
Christoph Lametera35afb82007-05-16 22:10:57 -07001365 INIT_LIST_HEAD(&ei->i_orphan);
Christoph Lametera35afb82007-05-16 22:10:57 -07001366 init_rwsem(&ei->xattr_sem);
Aneesh Kumar K.V0e855ac2008-01-28 23:58:26 -05001367 init_rwsem(&ei->i_data_sem);
Jan Karaea3d7202015-12-07 14:28:03 -05001368 init_rwsem(&ei->i_mmap_sem);
Christoph Lametera35afb82007-05-16 22:10:57 -07001369 inode_init_once(&ei->vfs_inode);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07001370 ext4_fc_init_inode(&ei->vfs_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001371}
1372
Fabian Fredericke67bc2b2014-02-17 20:34:53 -05001373static int __init init_inodecache(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001374{
David Windsorf8dd7c72017-06-10 22:50:36 -04001375 ext4_inode_cachep = kmem_cache_create_usercopy("ext4_inode_cache",
1376 sizeof(struct ext4_inode_info), 0,
1377 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
1378 SLAB_ACCOUNT),
1379 offsetof(struct ext4_inode_info, i_data),
1380 sizeof_field(struct ext4_inode_info, i_data),
1381 init_once);
Mingming Cao617ba132006-10-11 01:20:53 -07001382 if (ext4_inode_cachep == NULL)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001383 return -ENOMEM;
1384 return 0;
1385}
1386
1387static void destroy_inodecache(void)
1388{
Kirill A. Shutemov8c0a8532012-09-26 11:33:07 +10001389 /*
1390 * Make sure all delayed rcu free inodes are flushed before we
1391 * destroy cache.
1392 */
1393 rcu_barrier();
Mingming Cao617ba132006-10-11 01:20:53 -07001394 kmem_cache_destroy(ext4_inode_cachep);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001395}
1396
Al Viro0930fcc2010-06-07 13:16:22 -04001397void ext4_clear_inode(struct inode *inode)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001398{
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07001399 ext4_fc_del(inode);
Al Viro0930fcc2010-06-07 13:16:22 -04001400 invalidate_inode_buffers(inode);
Jan Karadbd57682012-05-03 14:48:02 +02001401 clear_inode(inode);
brookxu27bc4462020-08-17 15:36:15 +08001402 ext4_discard_preallocations(inode, 0);
Zheng Liu51865fd2012-11-08 21:57:32 -05001403 ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
Jan Karaf4c2d372019-11-08 12:45:11 +01001404 dquot_drop(inode);
Theodore Ts'o8aefcd52011-01-10 12:29:43 -05001405 if (EXT4_I(inode)->jinode) {
1406 jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
1407 EXT4_I(inode)->jinode);
1408 jbd2_free_inode(EXT4_I(inode)->jinode);
1409 EXT4_I(inode)->jinode = NULL;
1410 }
Eric Biggers3d204e22018-01-11 23:30:13 -05001411 fscrypt_put_encryption_info(inode);
Eric Biggersc93d8f82019-07-22 09:26:24 -07001412 fsverity_cleanup_inode(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001413}
1414
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001415static struct inode *ext4_nfs_get_inode(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001416 u64 ino, u32 generation)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001417{
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001418 struct inode *inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001419
Theodore Ts'o8a363972018-12-19 12:29:13 -05001420 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001421 * Currently we don't know the generation for parent directory, so
1422 * a generation of 0 means "accept any"
1423 */
Theodore Ts'o8a363972018-12-19 12:29:13 -05001424 inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
David Howells1d1fe1e2008-02-07 00:15:37 -08001425 if (IS_ERR(inode))
1426 return ERR_CAST(inode);
1427 if (generation && inode->i_generation != generation) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001428 iput(inode);
1429 return ERR_PTR(-ESTALE);
1430 }
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001431
1432 return inode;
1433}
1434
1435static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001436 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001437{
1438 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1439 ext4_nfs_get_inode);
1440}
1441
1442static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001443 int fh_len, int fh_type)
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001444{
1445 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1446 ext4_nfs_get_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001447}
1448
Theodore Ts'ofde87262018-12-19 14:07:58 -05001449static int ext4_nfs_commit_metadata(struct inode *inode)
1450{
1451 struct writeback_control wbc = {
1452 .sync_mode = WB_SYNC_ALL
1453 };
1454
1455 trace_ext4_nfs_commit_metadata(inode);
1456 return ext4_write_inode(inode, &wbc);
1457}
1458
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001459/*
1460 * Try to release metadata pages (indirect blocks, directories) which are
1461 * mapped via the block device. Since these pages could have journal heads
1462 * which would prevent try_to_free_buffers() from freeing them, we must use
1463 * jbd2 layer's try_to_free_buffers() function to release them.
1464 */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001465static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1466 gfp_t wait)
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001467{
1468 journal_t *journal = EXT4_SB(sb)->s_journal;
1469
1470 WARN_ON(PageChecked(page));
1471 if (!page_has_buffers(page))
1472 return 0;
1473 if (journal)
zhangyi (F)529a7812020-06-20 10:54:27 +08001474 return jbd2_journal_try_to_free_buffers(journal, page);
1475
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001476 return try_to_free_buffers(page);
1477}
1478
Chandan Rajendra643fa962018-12-12 15:20:12 +05301479#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001480static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
1481{
1482 return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
1483 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
1484}
1485
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001486static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
1487 void *fs_data)
1488{
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001489 handle_t *handle = fs_data;
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04001490 int res, res2, credits, retries = 0;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001491
Eric Biggers9ce01512017-06-23 00:10:36 -04001492 /*
1493 * Encrypting the root directory is not allowed because e2fsck expects
1494 * lost+found to exist and be unencrypted, and encrypting the root
1495 * directory would imply encrypting the lost+found directory as well as
1496 * the filename "lost+found" itself.
1497 */
1498 if (inode->i_ino == EXT4_ROOT_INO)
1499 return -EPERM;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001500
Ross Zwisler7d3e06a2017-10-12 11:58:05 -04001501 if (WARN_ON_ONCE(IS_DAX(inode) && i_size_read(inode)))
1502 return -EINVAL;
1503
Ira Weinyb383a732020-05-28 08:00:02 -07001504 if (ext4_test_inode_flag(inode, EXT4_INODE_DAX))
1505 return -EOPNOTSUPP;
1506
Eric Biggers94840e32017-02-22 13:25:14 -08001507 res = ext4_convert_inline_data(inode);
1508 if (res)
1509 return res;
1510
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001511 /*
1512 * If a journal handle was specified, then the encryption context is
1513 * being set on a new inode via inheritance and is part of a larger
1514 * transaction to create the inode. Otherwise the encryption context is
1515 * being set on an existing inode in its own transaction. Only in the
1516 * latter case should the "retry on ENOSPC" logic be used.
1517 */
1518
1519 if (handle) {
1520 res = ext4_xattr_set_handle(handle, inode,
1521 EXT4_XATTR_INDEX_ENCRYPTION,
1522 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1523 ctx, len, 0);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001524 if (!res) {
1525 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
1526 ext4_clear_inode_state(inode,
1527 EXT4_STATE_MAY_INLINE_DATA);
Jan Karaa3caa242016-11-20 17:32:59 -05001528 /*
Eric Biggers2ee6a572017-10-09 12:15:35 -07001529 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1530 * S_DAX may be disabled
Jan Karaa3caa242016-11-20 17:32:59 -05001531 */
Ira Weiny043546e2020-05-28 07:59:59 -07001532 ext4_set_inode_flags(inode, false);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001533 }
1534 return res;
1535 }
1536
Tahsin Erdoganb8cb5a52017-05-24 18:24:07 -04001537 res = dquot_initialize(inode);
1538 if (res)
1539 return res;
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001540retry:
Tahsin Erdoganaf652072017-07-06 00:01:59 -04001541 res = ext4_xattr_set_credits(inode, len, false /* is_create */,
1542 &credits);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04001543 if (res)
1544 return res;
1545
Tahsin Erdoganc1a5d5f2017-06-21 22:28:40 -04001546 handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001547 if (IS_ERR(handle))
1548 return PTR_ERR(handle);
1549
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001550 res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
1551 EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
1552 ctx, len, 0);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001553 if (!res) {
1554 ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
Eric Biggers2ee6a572017-10-09 12:15:35 -07001555 /*
1556 * Update inode->i_flags - S_ENCRYPTED will be enabled,
1557 * S_DAX may be disabled
1558 */
Ira Weiny043546e2020-05-28 07:59:59 -07001559 ext4_set_inode_flags(inode, false);
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001560 res = ext4_mark_inode_dirty(handle, inode);
1561 if (res)
1562 EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
1563 }
1564 res2 = ext4_journal_stop(handle);
Eric Biggers2f8f5e72016-11-21 11:52:44 -05001565
1566 if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
1567 goto retry;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001568 if (!res)
1569 res = res2;
1570 return res;
1571}
1572
Eric Biggersac4acb12020-09-16 21:11:35 -07001573static const union fscrypt_policy *ext4_get_dummy_policy(struct super_block *sb)
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001574{
Eric Biggersac4acb12020-09-16 21:11:35 -07001575 return EXT4_SB(sb)->s_dummy_enc_policy.policy;
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001576}
1577
Eric Biggersb925acb2019-10-24 14:54:37 -07001578static bool ext4_has_stable_inodes(struct super_block *sb)
1579{
1580 return ext4_has_feature_stable_inodes(sb);
1581}
1582
1583static void ext4_get_ino_and_lblk_bits(struct super_block *sb,
1584 int *ino_bits_ret, int *lblk_bits_ret)
1585{
1586 *ino_bits_ret = 8 * sizeof(EXT4_SB(sb)->s_es->s_inodes_count);
1587 *lblk_bits_ret = 8 * sizeof(ext4_lblk_t);
1588}
1589
Eric Biggers6f69f0e2017-02-07 12:42:10 -08001590static const struct fscrypt_operations ext4_cryptops = {
Eric Biggersa5d431e2017-01-05 13:51:18 -08001591 .key_prefix = "ext4:",
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001592 .get_context = ext4_get_context,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001593 .set_context = ext4_set_context,
Eric Biggersac4acb12020-09-16 21:11:35 -07001594 .get_dummy_policy = ext4_get_dummy_policy,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001595 .empty_dir = ext4_empty_dir,
Eric Biggerse12ee682018-04-30 15:51:44 -07001596 .max_namelen = EXT4_NAME_LEN,
Eric Biggersb925acb2019-10-24 14:54:37 -07001597 .has_stable_inodes = ext4_has_stable_inodes,
1598 .get_ino_and_lblk_bits = ext4_get_ino_and_lblk_bits,
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001599};
Jaegeuk Kima7550b32016-07-10 14:01:03 -04001600#endif
1601
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001602#ifdef CONFIG_QUOTA
Eric Biggersd6006182017-04-29 23:47:50 -04001603static const char * const quotatypes[] = INITQFNAMES;
Li Xi689c9582016-01-08 16:01:22 -05001604#define QTYPE2NAME(t) (quotatypes[t])
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001605
Mingming Cao617ba132006-10-11 01:20:53 -07001606static int ext4_write_dquot(struct dquot *dquot);
1607static int ext4_acquire_dquot(struct dquot *dquot);
1608static int ext4_release_dquot(struct dquot *dquot);
1609static int ext4_mark_dquot_dirty(struct dquot *dquot);
1610static int ext4_write_info(struct super_block *sb, int type);
Jan Kara6f28e082008-04-28 02:14:34 -07001611static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro8c54ca92016-11-20 19:49:34 -05001612 const struct path *path);
Mingming Cao617ba132006-10-11 01:20:53 -07001613static int ext4_quota_on_mount(struct super_block *sb, int type);
1614static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001615 size_t len, loff_t off);
Mingming Cao617ba132006-10-11 01:20:53 -07001616static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001617 const char *data, size_t len, loff_t off);
Aditya Kali7c319d32012-07-22 20:21:31 -04001618static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1619 unsigned int flags);
1620static int ext4_enable_quotas(struct super_block *sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001621
Jan Kara96c7e0d2014-09-29 14:58:25 +02001622static struct dquot **ext4_get_dquots(struct inode *inode)
1623{
1624 return EXT4_I(inode)->i_dquot;
1625}
1626
Alexey Dobriyan61e225d2009-09-21 17:01:08 -07001627static const struct dquot_operations ext4_quota_operations = {
Tahsin Erdogan7a9ca532017-06-22 11:46:48 -04001628 .get_reserved_space = ext4_get_reserved_space,
1629 .write_dquot = ext4_write_dquot,
1630 .acquire_dquot = ext4_acquire_dquot,
1631 .release_dquot = ext4_release_dquot,
1632 .mark_dirty = ext4_mark_dquot_dirty,
1633 .write_info = ext4_write_info,
1634 .alloc_dquot = dquot_alloc,
1635 .destroy_dquot = dquot_destroy,
1636 .get_projid = ext4_get_projid,
1637 .get_inode_usage = ext4_get_inode_usage,
Chengguang Xuebc11f72019-10-06 18:30:28 +08001638 .get_next_id = dquot_get_next_id,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001639};
1640
Alexey Dobriyan0d54b212009-09-21 17:01:09 -07001641static const struct quotactl_ops ext4_qctl_operations = {
Mingming Cao617ba132006-10-11 01:20:53 -07001642 .quota_on = ext4_quota_on,
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04001643 .quota_off = ext4_quota_off,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001644 .quota_sync = dquot_quota_sync,
Jan Kara0a2403392014-11-19 00:42:09 +01001645 .get_state = dquot_get_state,
Christoph Hellwig287a8092010-05-19 07:16:45 -04001646 .set_info = dquot_set_dqinfo,
1647 .get_dqblk = dquot_get_dqblk,
Eric Sandeen6332b9b2016-02-19 19:19:01 +01001648 .set_dqblk = dquot_set_dqblk,
1649 .get_nextdqblk = dquot_get_next_dqblk,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001650};
1651#endif
1652
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -08001653static const struct super_operations ext4_sops = {
Mingming Cao617ba132006-10-11 01:20:53 -07001654 .alloc_inode = ext4_alloc_inode,
Al Viro94053132019-04-15 19:28:34 -04001655 .free_inode = ext4_free_in_core_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001656 .destroy_inode = ext4_destroy_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001657 .write_inode = ext4_write_inode,
1658 .dirty_inode = ext4_dirty_inode,
Theodore Ts'o7ff9c072010-11-08 13:51:33 -05001659 .drop_inode = ext4_drop_inode,
Al Viro0930fcc2010-06-07 13:16:22 -04001660 .evict_inode = ext4_evict_inode,
Mingming Cao617ba132006-10-11 01:20:53 -07001661 .put_super = ext4_put_super,
Mingming Cao617ba132006-10-11 01:20:53 -07001662 .sync_fs = ext4_sync_fs,
Takashi Satoc4be0c12009-01-09 16:40:58 -08001663 .freeze_fs = ext4_freeze,
1664 .unfreeze_fs = ext4_unfreeze,
Mingming Cao617ba132006-10-11 01:20:53 -07001665 .statfs = ext4_statfs,
1666 .remount_fs = ext4_remount,
Mingming Cao617ba132006-10-11 01:20:53 -07001667 .show_options = ext4_show_options,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001668#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07001669 .quota_read = ext4_quota_read,
1670 .quota_write = ext4_quota_write,
Jan Kara96c7e0d2014-09-29 14:58:25 +02001671 .get_dquots = ext4_get_dquots,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001672#endif
Toshiyuki Okajimac39a7f82009-01-05 22:38:48 -05001673 .bdev_try_to_free_page = bdev_try_to_free_page,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001674};
1675
Christoph Hellwig39655162007-10-21 16:42:17 -07001676static const struct export_operations ext4_export_ops = {
Christoph Hellwig1b961ac2007-10-21 16:42:08 -07001677 .fh_to_dentry = ext4_fh_to_dentry,
1678 .fh_to_parent = ext4_fh_to_parent,
Mingming Cao617ba132006-10-11 01:20:53 -07001679 .get_parent = ext4_get_parent,
Theodore Ts'ofde87262018-12-19 14:07:58 -05001680 .commit_metadata = ext4_nfs_commit_metadata,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001681};
1682
1683enum {
1684 Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1685 Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
Theodore Ts'o72578c32012-03-03 18:04:40 -05001686 Opt_nouid32, Opt_debug, Opt_removed,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001687 Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
Theodore Ts'o72578c32012-03-03 18:04:40 -05001688 Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
Eric Sandeenad4eec62013-08-28 19:05:07 -04001689 Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1690 Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001691 Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04001692 Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
Eric Biggers4f74d152020-07-02 01:56:07 +00001693 Opt_inlinecrypt,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001694 Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
Jan Kara5a20bdf2009-11-30 23:58:32 +01001695 Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
Theodore Ts'oee4a3fc2012-03-02 12:14:24 -05001696 Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
Ira Weiny9cb20f92020-05-28 08:00:00 -07001697 Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version,
1698 Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001699 Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
1700 Opt_nowarn_on_error, Opt_mblk_io_submit,
Theodore Ts'o670e9872017-01-11 15:32:22 -05001701 Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
Theodore Ts'o14490322010-12-14 15:27:50 -05001702 Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
Eric Sandeen5328e632009-11-19 14:25:42 -05001703 Opt_inode_readahead_blks, Opt_journal_ioprio,
Jiaying Zhang744692d2010-03-04 16:14:02 -05001704 Opt_dioread_nolock, Opt_dioread_lock,
Theodore Ts'ofc6cb1c2011-12-12 22:06:18 -05001705 Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001706 Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
Harshad Shirwadkar99c880d2020-11-05 19:59:11 -08001707 Opt_prefetch_block_bitmaps,
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001708#ifdef CONFIG_EXT4_DEBUG
Harshad Shirwadkar99c880d2020-11-05 19:59:11 -08001709 Opt_fc_debug_max_replay, Opt_fc_debug_force
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001710#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001711};
1712
Steven Whitehousea447c092008-10-13 10:46:57 +01001713static const match_table_t tokens = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001714 {Opt_bsd_df, "bsddf"},
1715 {Opt_minix_df, "minixdf"},
1716 {Opt_grpid, "grpid"},
1717 {Opt_grpid, "bsdgroups"},
1718 {Opt_nogrpid, "nogrpid"},
1719 {Opt_nogrpid, "sysvgroups"},
1720 {Opt_resgid, "resgid=%u"},
1721 {Opt_resuid, "resuid=%u"},
1722 {Opt_sb, "sb=%u"},
1723 {Opt_err_cont, "errors=continue"},
1724 {Opt_err_panic, "errors=panic"},
1725 {Opt_err_ro, "errors=remount-ro"},
1726 {Opt_nouid32, "nouid32"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001727 {Opt_debug, "debug"},
Theodore Ts'o72578c32012-03-03 18:04:40 -05001728 {Opt_removed, "oldalloc"},
1729 {Opt_removed, "orlov"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001730 {Opt_user_xattr, "user_xattr"},
1731 {Opt_nouser_xattr, "nouser_xattr"},
1732 {Opt_acl, "acl"},
1733 {Opt_noacl, "noacl"},
Eric Sandeene3bb52a2009-11-19 14:28:50 -05001734 {Opt_noload, "norecovery"},
Theodore Ts'o5a916be2012-03-04 19:27:31 -05001735 {Opt_noload, "noload"},
Theodore Ts'o72578c32012-03-03 18:04:40 -05001736 {Opt_removed, "nobh"},
1737 {Opt_removed, "bh"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001738 {Opt_commit, "commit=%u"},
Theodore Ts'o30773842009-01-03 20:27:38 -05001739 {Opt_min_batch_time, "min_batch_time=%u"},
1740 {Opt_max_batch_time, "max_batch_time=%u"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001741 {Opt_journal_dev, "journal_dev=%u"},
Eric Sandeenad4eec62013-08-28 19:05:07 -04001742 {Opt_journal_path, "journal_path=%s"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001743 {Opt_journal_checksum, "journal_checksum"},
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05001744 {Opt_nojournal_checksum, "nojournal_checksum"},
Girish Shilamkar818d2762008-01-28 23:58:27 -05001745 {Opt_journal_async_commit, "journal_async_commit"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001746 {Opt_abort, "abort"},
1747 {Opt_data_journal, "data=journal"},
1748 {Opt_data_ordered, "data=ordered"},
1749 {Opt_data_writeback, "data=writeback"},
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04001750 {Opt_data_err_abort, "data_err=abort"},
1751 {Opt_data_err_ignore, "data_err=ignore"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001752 {Opt_offusrjquota, "usrjquota="},
1753 {Opt_usrjquota, "usrjquota=%s"},
1754 {Opt_offgrpjquota, "grpjquota="},
1755 {Opt_grpjquota, "grpjquota=%s"},
1756 {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1757 {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
Jan Kara5a20bdf2009-11-30 23:58:32 +01001758 {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001759 {Opt_grpquota, "grpquota"},
1760 {Opt_noquota, "noquota"},
1761 {Opt_quota, "quota"},
1762 {Opt_usrquota, "usrquota"},
Jan Kara49da9392016-09-05 23:08:16 -04001763 {Opt_prjquota, "prjquota"},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001764 {Opt_barrier, "barrier=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001765 {Opt_barrier, "barrier"},
1766 {Opt_nobarrier, "nobarrier"},
Jean Noel Cordenner25ec56b2008-01-28 23:58:27 -05001767 {Opt_i_version, "i_version"},
Ross Zwisler923ae0f2015-02-16 15:59:38 -08001768 {Opt_dax, "dax"},
Ira Weiny9cb20f92020-05-28 08:00:00 -07001769 {Opt_dax_always, "dax=always"},
1770 {Opt_dax_inode, "dax=inode"},
1771 {Opt_dax_never, "dax=never"},
Alex Tomasc9de5602008-01-29 00:19:52 -05001772 {Opt_stripe, "stripe=%u"},
Alex Tomas64769242008-07-11 19:27:31 -04001773 {Opt_delalloc, "delalloc"},
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001774 {Opt_warn_on_error, "warn_on_error"},
1775 {Opt_nowarn_on_error, "nowarn_on_error"},
Theodore Ts'oa26f4992015-02-02 00:37:02 -05001776 {Opt_lazytime, "lazytime"},
1777 {Opt_nolazytime, "nolazytime"},
Theodore Ts'o670e9872017-01-11 15:32:22 -05001778 {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04001779 {Opt_nodelalloc, "nodelalloc"},
Jan Kara36ade452013-01-28 09:30:52 -05001780 {Opt_removed, "mblk_io_submit"},
1781 {Opt_removed, "nomblk_io_submit"},
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04001782 {Opt_block_validity, "block_validity"},
1783 {Opt_noblock_validity, "noblock_validity"},
Theodore Ts'o240799c2008-10-09 23:53:47 -04001784 {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001785 {Opt_journal_ioprio, "journal_ioprio=%u"},
Theodore Ts'oafd46722009-03-16 23:12:23 -04001786 {Opt_auto_da_alloc, "auto_da_alloc=%u"},
Theodore Ts'o06705bf2009-03-28 10:59:57 -04001787 {Opt_auto_da_alloc, "auto_da_alloc"},
1788 {Opt_noauto_da_alloc, "noauto_da_alloc"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001789 {Opt_dioread_nolock, "dioread_nolock"},
Theodore Ts'o244adf62020-01-23 12:23:17 -05001790 {Opt_dioread_lock, "nodioread_nolock"},
Jiaying Zhang744692d2010-03-04 16:14:02 -05001791 {Opt_dioread_lock, "dioread_lock"},
Eric Sandeen5328e632009-11-19 14:25:42 -05001792 {Opt_discard, "discard"},
1793 {Opt_nodiscard, "nodiscard"},
Theodore Ts'ofc6cb1c2011-12-12 22:06:18 -05001794 {Opt_init_itable, "init_itable=%u"},
1795 {Opt_init_itable, "init_itable"},
1796 {Opt_noinit_itable, "noinit_itable"},
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001797#ifdef CONFIG_EXT4_DEBUG
Harshad Shirwadkar99c880d2020-11-05 19:59:11 -08001798 {Opt_fc_debug_force, "fc_debug_force"},
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07001799 {Opt_fc_debug_max_replay, "fc_debug_max_replay=%u"},
1800#endif
Theodore Ts'odf981d02012-08-17 09:48:17 -04001801 {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
Eric Biggersed318a62020-05-12 16:32:50 -07001802 {Opt_test_dummy_encryption, "test_dummy_encryption=%s"},
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04001803 {Opt_test_dummy_encryption, "test_dummy_encryption"},
Eric Biggers4f74d152020-07-02 01:56:07 +00001804 {Opt_inlinecrypt, "inlinecrypt"},
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04001805 {Opt_nombcache, "nombcache"},
1806 {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */
Theodore Ts'o3d392b22020-07-17 00:14:40 -04001807 {Opt_prefetch_block_bitmaps, "prefetch_block_bitmaps"},
Theodore Ts'oc7198b92012-03-04 22:00:53 -05001808 {Opt_removed, "check=none"}, /* mount option from ext2/3 */
1809 {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
1810 {Opt_removed, "reservation"}, /* mount option from ext2/3 */
1811 {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1812 {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */
Josef Bacikf3f12fa2008-04-29 22:05:28 -04001813 {Opt_err, NULL},
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001814};
1815
Mingming Cao617ba132006-10-11 01:20:53 -07001816static ext4_fsblk_t get_sb_block(void **data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001817{
Mingming Cao617ba132006-10-11 01:20:53 -07001818 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001819 char *options = (char *) *data;
1820
1821 if (!options || strncmp(options, "sb=", 3) != 0)
1822 return 1; /* Default location */
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001823
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001824 options += 3;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001825 /* TODO: use simple_strtoll with >32bit ext4 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001826 sb_block = simple_strtoul(options, &options, 0);
1827 if (*options && *options != ',') {
Theodore Ts'o4776004f2008-09-08 23:00:52 -04001828 printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001829 (char *) *data);
1830 return 1;
1831 }
1832 if (*options == ',')
1833 options++;
1834 *data = (void *) options;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04001835
Dave Kleikampac27a0e2006-10-11 01:20:50 -07001836 return sb_block;
1837}
1838
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001839#define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
Eric Biggersd6006182017-04-29 23:47:50 -04001840static const char deprecated_msg[] =
1841 "Mount option \"%s\" will be removed by %s\n"
Dmitry Monakhov437ca0f2010-03-01 22:29:21 -05001842 "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
Theodore Ts'ob3881f72009-01-05 22:46:26 -05001843
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001844#ifdef CONFIG_QUOTA
1845static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1846{
1847 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001848 char *qname, *old_qname = get_qf_name(sb, sbi, qtype);
Chen Gang03dafb52013-01-24 23:24:58 -05001849 int ret = -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001850
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001851 if (sb_any_quota_loaded(sb) && !old_qname) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001852 ext4_msg(sb, KERN_ERR,
1853 "Cannot change journaled "
1854 "quota options when quota turned on");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001855 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001856 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04001857 if (ext4_has_feature_quota(sb)) {
Theodore Ts'oc325a672016-04-03 17:03:37 -04001858 ext4_msg(sb, KERN_INFO, "Journaled quota options "
1859 "ignored when QUOTA feature is enabled");
1860 return 1;
Jan Kara262b4662013-03-02 17:57:08 -05001861 }
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001862 qname = match_strdup(args);
1863 if (!qname) {
1864 ext4_msg(sb, KERN_ERR,
1865 "Not enough memory for storing quotafile name");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001866 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001867 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001868 if (old_qname) {
1869 if (strcmp(old_qname, qname) == 0)
Chen Gang03dafb52013-01-24 23:24:58 -05001870 ret = 1;
1871 else
1872 ext4_msg(sb, KERN_ERR,
1873 "%s quota file already specified",
1874 QTYPE2NAME(qtype));
1875 goto errout;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001876 }
Chen Gang03dafb52013-01-24 23:24:58 -05001877 if (strchr(qname, '/')) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001878 ext4_msg(sb, KERN_ERR,
1879 "quotafile must be on filesystem root");
Chen Gang03dafb52013-01-24 23:24:58 -05001880 goto errout;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001881 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001882 rcu_assign_pointer(sbi->s_qf_names[qtype], qname);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05001883 set_opt(sb, QUOTA);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001884 return 1;
Chen Gang03dafb52013-01-24 23:24:58 -05001885errout:
1886 kfree(qname);
1887 return ret;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001888}
1889
1890static int clear_qf_name(struct super_block *sb, int qtype)
1891{
1892
1893 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001894 char *old_qname = get_qf_name(sb, sbi, qtype);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001895
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001896 if (sb_any_quota_loaded(sb) && old_qname) {
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001897 ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1898 " when quota turned on");
Theodore Ts'o57f73c22012-04-16 18:55:26 -04001899 return -1;
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001900 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04001901 rcu_assign_pointer(sbi->s_qf_names[qtype], NULL);
1902 synchronize_rcu();
1903 kfree(old_qname);
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05001904 return 1;
1905}
1906#endif
1907
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001908#define MOPT_SET 0x0001
1909#define MOPT_CLEAR 0x0002
1910#define MOPT_NOSUPPORT 0x0004
1911#define MOPT_EXPLICIT 0x0008
1912#define MOPT_CLEAR_ERR 0x0010
1913#define MOPT_GTE0 0x0020
1914#ifdef CONFIG_QUOTA
1915#define MOPT_Q 0
1916#define MOPT_QFMT 0x0040
1917#else
1918#define MOPT_Q MOPT_NOSUPPORT
1919#define MOPT_QFMT MOPT_NOSUPPORT
1920#endif
1921#define MOPT_DATAJ 0x0080
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001922#define MOPT_NO_EXT2 0x0100
1923#define MOPT_NO_EXT3 0x0200
1924#define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
Eric Sandeenad4eec62013-08-28 19:05:07 -04001925#define MOPT_STRING 0x0400
Ira Weiny9cb20f92020-05-28 08:00:00 -07001926#define MOPT_SKIP 0x0800
Harshad Shirwadkar995a3ed2020-10-15 13:37:54 -07001927#define MOPT_2 0x1000
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001928
1929static const struct mount_opts {
1930 int token;
1931 int mount_opt;
1932 int flags;
1933} ext4_mount_opts[] = {
1934 {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1935 {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1936 {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1937 {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001938 {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1939 {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001940 {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1941 MOPT_EXT4_ONLY | MOPT_SET},
1942 {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1943 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001944 {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1945 {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001946 {Opt_delalloc, EXT4_MOUNT_DELALLOC,
1947 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1948 {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
Theodore Ts'o59d9fa52013-08-08 23:01:24 -04001949 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o327eaf72018-06-12 23:34:57 -04001950 {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
1951 {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05001952 {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1953 MOPT_EXT4_ONLY | MOPT_CLEAR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001954 {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04001955 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001956 {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001957 EXT4_MOUNT_JOURNAL_CHECKSUM),
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04001958 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001959 {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001960 {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1961 {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1962 {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001963 {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
Ales Novak7915a862016-03-12 21:55:50 -05001964 MOPT_NO_EXT2},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001965 {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
Ales Novak7915a862016-03-12 21:55:50 -05001966 MOPT_NO_EXT2},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001967 {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1968 {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1969 {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1970 {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1971 {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1972 {Opt_commit, 0, MOPT_GTE0},
1973 {Opt_max_batch_time, 0, MOPT_GTE0},
1974 {Opt_min_batch_time, 0, MOPT_GTE0},
1975 {Opt_inode_readahead_blks, 0, MOPT_GTE0},
1976 {Opt_init_itable, 0, MOPT_GTE0},
Ira Weiny9cb20f92020-05-28 08:00:00 -07001977 {Opt_dax, EXT4_MOUNT_DAX_ALWAYS, MOPT_SET | MOPT_SKIP},
1978 {Opt_dax_always, EXT4_MOUNT_DAX_ALWAYS,
1979 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1980 {Opt_dax_inode, EXT4_MOUNT2_DAX_INODE,
1981 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
1982 {Opt_dax_never, EXT4_MOUNT2_DAX_NEVER,
1983 MOPT_EXT4_ONLY | MOPT_SET | MOPT_SKIP},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001984 {Opt_stripe, 0, MOPT_GTE0},
Jan Kara0efb3b22013-02-02 22:52:19 -05001985 {Opt_resuid, 0, MOPT_GTE0},
1986 {Opt_resgid, 0, MOPT_GTE0},
Carlos Maiolino5ba92bc2015-07-21 23:57:59 -04001987 {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
1988 {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
1989 {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05001990 {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1991 {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1992 {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
1993 MOPT_NO_EXT2 | MOPT_DATAJ},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001994 {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1995 {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05001996#ifdef CONFIG_EXT4_FS_POSIX_ACL
1997 {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1998 {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
1999#else
2000 {Opt_acl, 0, MOPT_NOSUPPORT},
2001 {Opt_noacl, 0, MOPT_NOSUPPORT},
2002#endif
2003 {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
2004 {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
Theodore Ts'o670e9872017-01-11 15:32:22 -05002005 {Opt_debug_want_extra_isize, 0, MOPT_GTE0},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002006 {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
2007 {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
2008 MOPT_SET | MOPT_Q},
2009 {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
2010 MOPT_SET | MOPT_Q},
Jan Kara49da9392016-09-05 23:08:16 -04002011 {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
2012 MOPT_SET | MOPT_Q},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002013 {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
Jan Kara49da9392016-09-05 23:08:16 -04002014 EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
2015 MOPT_CLEAR | MOPT_Q},
Kaixu Xia174fe5b2020-10-29 23:46:36 +08002016 {Opt_usrjquota, 0, MOPT_Q | MOPT_STRING},
2017 {Opt_grpjquota, 0, MOPT_Q | MOPT_STRING},
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002018 {Opt_offusrjquota, 0, MOPT_Q},
2019 {Opt_offgrpjquota, 0, MOPT_Q},
2020 {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
2021 {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
2022 {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
Theodore Ts'odf981d02012-08-17 09:48:17 -04002023 {Opt_max_dir_size_kb, 0, MOPT_GTE0},
Eric Biggersed318a62020-05-12 16:32:50 -07002024 {Opt_test_dummy_encryption, 0, MOPT_STRING},
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04002025 {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
Theodore Ts'o3d392b22020-07-17 00:14:40 -04002026 {Opt_prefetch_block_bitmaps, EXT4_MOUNT_PREFETCH_BLOCK_BITMAPS,
2027 MOPT_SET},
Harshad Shirwadkar99c880d2020-11-05 19:59:11 -08002028#ifdef CONFIG_EXT4_DEBUG
Harshad Shirwadkar0f0672f2020-10-15 13:38:00 -07002029 {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
2030 MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07002031 {Opt_fc_debug_max_replay, 0, MOPT_GTE0},
2032#endif
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002033 {Opt_err, 0, 0}
2034};
2035
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04002036#ifdef CONFIG_UNICODE
2037static const struct ext4_sb_encodings {
2038 __u16 magic;
2039 char *name;
2040 char *version;
2041} ext4_sb_encoding_map[] = {
2042 {EXT4_ENC_UTF8_12_1, "utf8", "12.1.0"},
2043};
2044
2045static int ext4_sb_read_encoding(const struct ext4_super_block *es,
2046 const struct ext4_sb_encodings **encoding,
2047 __u16 *flags)
2048{
2049 __u16 magic = le16_to_cpu(es->s_encoding);
2050 int i;
2051
2052 for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
2053 if (magic == ext4_sb_encoding_map[i].magic)
2054 break;
2055
2056 if (i >= ARRAY_SIZE(ext4_sb_encoding_map))
2057 return -EINVAL;
2058
2059 *encoding = &ext4_sb_encoding_map[i];
2060 *flags = le16_to_cpu(es->s_encoding_flags);
2061
2062 return 0;
2063}
2064#endif
2065
Eric Biggersed318a62020-05-12 16:32:50 -07002066static int ext4_set_test_dummy_encryption(struct super_block *sb,
2067 const char *opt,
2068 const substring_t *arg,
2069 bool is_remount)
2070{
2071#ifdef CONFIG_FS_ENCRYPTION
2072 struct ext4_sb_info *sbi = EXT4_SB(sb);
2073 int err;
2074
2075 /*
2076 * This mount option is just for testing, and it's not worthwhile to
2077 * implement the extra complexity (e.g. RCU protection) that would be
2078 * needed to allow it to be set or changed during remount. We do allow
2079 * it to be specified during remount, but only if there is no change.
2080 */
Eric Biggersac4acb12020-09-16 21:11:35 -07002081 if (is_remount && !sbi->s_dummy_enc_policy.policy) {
Eric Biggersed318a62020-05-12 16:32:50 -07002082 ext4_msg(sb, KERN_WARNING,
2083 "Can't set test_dummy_encryption on remount");
2084 return -1;
2085 }
Eric Biggersc8c868a2020-09-16 21:11:36 -07002086 err = fscrypt_set_test_dummy_encryption(sb, arg->from,
Eric Biggersac4acb12020-09-16 21:11:35 -07002087 &sbi->s_dummy_enc_policy);
Eric Biggersed318a62020-05-12 16:32:50 -07002088 if (err) {
2089 if (err == -EEXIST)
2090 ext4_msg(sb, KERN_WARNING,
2091 "Can't change test_dummy_encryption on remount");
2092 else if (err == -EINVAL)
2093 ext4_msg(sb, KERN_WARNING,
2094 "Value of option \"%s\" is unrecognized", opt);
2095 else
2096 ext4_msg(sb, KERN_WARNING,
2097 "Error processing option \"%s\" [%d]",
2098 opt, err);
2099 return -1;
2100 }
2101 ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
2102#else
2103 ext4_msg(sb, KERN_WARNING,
2104 "Test dummy encryption mount option ignored");
2105#endif
2106 return 1;
2107}
2108
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002109static int handle_mount_opt(struct super_block *sb, char *opt, int token,
2110 substring_t *args, unsigned long *journal_devnum,
2111 unsigned int *journal_ioprio, int is_remount)
2112{
2113 struct ext4_sb_info *sbi = EXT4_SB(sb);
2114 const struct mount_opts *m;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002115 kuid_t uid;
2116 kgid_t gid;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002117 int arg = 0;
2118
Theodore Ts'o57f73c22012-04-16 18:55:26 -04002119#ifdef CONFIG_QUOTA
2120 if (token == Opt_usrjquota)
2121 return set_qf_name(sb, USRQUOTA, &args[0]);
2122 else if (token == Opt_grpjquota)
2123 return set_qf_name(sb, GRPQUOTA, &args[0]);
2124 else if (token == Opt_offusrjquota)
2125 return clear_qf_name(sb, USRQUOTA);
2126 else if (token == Opt_offgrpjquota)
2127 return clear_qf_name(sb, GRPQUOTA);
2128#endif
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002129 switch (token) {
Theodore Ts'of7048602012-03-04 22:06:20 -05002130 case Opt_noacl:
2131 case Opt_nouser_xattr:
2132 ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
2133 break;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002134 case Opt_sb:
2135 return 1; /* handled by get_sb_block() */
2136 case Opt_removed:
Jan Kara5f3633e2013-02-02 23:09:36 -05002137 ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002138 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002139 case Opt_abort:
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08002140 ext4_set_mount_flag(sb, EXT4_MF_FS_ABORTED);
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002141 return 1;
2142 case Opt_i_version:
Matthew Garrett357fdad2017-10-18 13:56:26 -07002143 sb->s_flags |= SB_I_VERSION;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002144 return 1;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05002145 case Opt_lazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002146 sb->s_flags |= SB_LAZYTIME;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05002147 return 1;
2148 case Opt_nolazytime:
Linus Torvalds1751e8a2017-11-27 13:05:09 -08002149 sb->s_flags &= ~SB_LAZYTIME;
Theodore Ts'oa26f4992015-02-02 00:37:02 -05002150 return 1;
Eric Biggers4f74d152020-07-02 01:56:07 +00002151 case Opt_inlinecrypt:
2152#ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
2153 sb->s_flags |= SB_INLINECRYPT;
2154#else
2155 ext4_msg(sb, KERN_ERR, "inline encryption not supported");
2156#endif
2157 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002158 }
2159
Jan Kara5f3633e2013-02-02 23:09:36 -05002160 for (m = ext4_mount_opts; m->token != Opt_err; m++)
2161 if (token == m->token)
2162 break;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002163
Jan Kara5f3633e2013-02-02 23:09:36 -05002164 if (m->token == Opt_err) {
2165 ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
2166 "or missing value", opt);
2167 return -1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002168 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002169
Theodore Ts'o8dc0aa82013-02-02 23:38:39 -05002170 if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
2171 ext4_msg(sb, KERN_ERR,
2172 "Mount option \"%s\" incompatible with ext2", opt);
2173 return -1;
2174 }
2175 if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
2176 ext4_msg(sb, KERN_ERR,
2177 "Mount option \"%s\" incompatible with ext3", opt);
2178 return -1;
2179 }
2180
Eric Sandeenad4eec62013-08-28 19:05:07 -04002181 if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
Jan Kara5f3633e2013-02-02 23:09:36 -05002182 return -1;
2183 if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
2184 return -1;
Dmitry Monakhovc93cf2d2015-10-18 23:35:32 -04002185 if (m->flags & MOPT_EXPLICIT) {
2186 if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
2187 set_opt2(sb, EXPLICIT_DELALLOC);
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04002188 } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
2189 set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
Dmitry Monakhovc93cf2d2015-10-18 23:35:32 -04002190 } else
2191 return -1;
2192 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002193 if (m->flags & MOPT_CLEAR_ERR)
2194 clear_opt(sb, ERRORS_MASK);
2195 if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
2196 ext4_msg(sb, KERN_ERR, "Cannot change quota "
2197 "options when quota turned on");
2198 return -1;
2199 }
2200
2201 if (m->flags & MOPT_NOSUPPORT) {
2202 ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
2203 } else if (token == Opt_commit) {
2204 if (arg == 0)
2205 arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
zhangyi (F)9ba55542019-08-28 11:25:01 -04002206 else if (arg > INT_MAX / HZ) {
2207 ext4_msg(sb, KERN_ERR,
2208 "Invalid commit interval %d, "
2209 "must be smaller than %d",
2210 arg, INT_MAX / HZ);
2211 return -1;
2212 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002213 sbi->s_commit_interval = HZ * arg;
Theodore Ts'o670e9872017-01-11 15:32:22 -05002214 } else if (token == Opt_debug_want_extra_isize) {
Theodore Ts'o98033872019-12-15 01:09:03 -05002215 if ((arg & 1) ||
2216 (arg < 4) ||
2217 (arg > (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE))) {
2218 ext4_msg(sb, KERN_ERR,
2219 "Invalid want_extra_isize %d", arg);
2220 return -1;
2221 }
Theodore Ts'o670e9872017-01-11 15:32:22 -05002222 sbi->s_want_extra_isize = arg;
Jan Kara5f3633e2013-02-02 23:09:36 -05002223 } else if (token == Opt_max_batch_time) {
Jan Kara5f3633e2013-02-02 23:09:36 -05002224 sbi->s_max_batch_time = arg;
2225 } else if (token == Opt_min_batch_time) {
2226 sbi->s_min_batch_time = arg;
2227 } else if (token == Opt_inode_readahead_blks) {
Jan Karae33e60e2013-02-02 23:14:31 -05002228 if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
2229 ext4_msg(sb, KERN_ERR,
2230 "EXT4-fs: inode_readahead_blks must be "
2231 "0 or a power of 2 smaller than 2^31");
Jan Kara5f3633e2013-02-02 23:09:36 -05002232 return -1;
2233 }
2234 sbi->s_inode_readahead_blks = arg;
2235 } else if (token == Opt_init_itable) {
2236 set_opt(sb, INIT_INODE_TABLE);
2237 if (!args->from)
2238 arg = EXT4_DEF_LI_WAIT_MULT;
2239 sbi->s_li_wait_mult = arg;
2240 } else if (token == Opt_max_dir_size_kb) {
2241 sbi->s_max_dir_size_kb = arg;
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07002242#ifdef CONFIG_EXT4_DEBUG
2243 } else if (token == Opt_fc_debug_max_replay) {
2244 sbi->s_fc_debug_max_replay = arg;
2245#endif
Jan Kara5f3633e2013-02-02 23:09:36 -05002246 } else if (token == Opt_stripe) {
2247 sbi->s_stripe = arg;
2248 } else if (token == Opt_resuid) {
2249 uid = make_kuid(current_user_ns(), arg);
2250 if (!uid_valid(uid)) {
2251 ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
2252 return -1;
2253 }
2254 sbi->s_resuid = uid;
2255 } else if (token == Opt_resgid) {
2256 gid = make_kgid(current_user_ns(), arg);
2257 if (!gid_valid(gid)) {
2258 ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
2259 return -1;
2260 }
2261 sbi->s_resgid = gid;
2262 } else if (token == Opt_journal_dev) {
2263 if (is_remount) {
2264 ext4_msg(sb, KERN_ERR,
2265 "Cannot specify journal on remount");
2266 return -1;
2267 }
2268 *journal_devnum = arg;
Eric Sandeenad4eec62013-08-28 19:05:07 -04002269 } else if (token == Opt_journal_path) {
2270 char *journal_path;
2271 struct inode *journal_inode;
2272 struct path path;
2273 int error;
2274
2275 if (is_remount) {
2276 ext4_msg(sb, KERN_ERR,
2277 "Cannot specify journal on remount");
2278 return -1;
2279 }
2280 journal_path = match_strdup(&args[0]);
2281 if (!journal_path) {
2282 ext4_msg(sb, KERN_ERR, "error: could not dup "
2283 "journal device string");
2284 return -1;
2285 }
2286
2287 error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
2288 if (error) {
2289 ext4_msg(sb, KERN_ERR, "error: could not find "
2290 "journal device path: error %d", error);
2291 kfree(journal_path);
2292 return -1;
2293 }
2294
David Howells2b0143b2015-03-17 22:25:59 +00002295 journal_inode = d_inode(path.dentry);
Eric Sandeenad4eec62013-08-28 19:05:07 -04002296 if (!S_ISBLK(journal_inode->i_mode)) {
2297 ext4_msg(sb, KERN_ERR, "error: journal path %s "
2298 "is not a block device", journal_path);
2299 path_put(&path);
2300 kfree(journal_path);
2301 return -1;
2302 }
2303
2304 *journal_devnum = new_encode_dev(journal_inode->i_rdev);
2305 path_put(&path);
2306 kfree(journal_path);
Jan Kara5f3633e2013-02-02 23:09:36 -05002307 } else if (token == Opt_journal_ioprio) {
2308 if (arg > 7) {
2309 ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
2310 " (must be 0-7)");
2311 return -1;
2312 }
2313 *journal_ioprio =
2314 IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04002315 } else if (token == Opt_test_dummy_encryption) {
Eric Biggersed318a62020-05-12 16:32:50 -07002316 return ext4_set_test_dummy_encryption(sb, opt, &args[0],
2317 is_remount);
Jan Kara5f3633e2013-02-02 23:09:36 -05002318 } else if (m->flags & MOPT_DATAJ) {
2319 if (is_remount) {
2320 if (!sbi->s_journal)
2321 ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
2322 else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
2323 ext4_msg(sb, KERN_ERR,
2324 "Cannot change data mode on remount");
2325 return -1;
2326 }
2327 } else {
2328 clear_opt(sb, DATA_FLAGS);
2329 sbi->s_mount_opt |= m->mount_opt;
2330 }
2331#ifdef CONFIG_QUOTA
2332 } else if (m->flags & MOPT_QFMT) {
2333 if (sb_any_quota_loaded(sb) &&
2334 sbi->s_jquota_fmt != m->mount_opt) {
2335 ext4_msg(sb, KERN_ERR, "Cannot change journaled "
2336 "quota options when quota turned on");
2337 return -1;
2338 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002339 if (ext4_has_feature_quota(sb)) {
Theodore Ts'oc325a672016-04-03 17:03:37 -04002340 ext4_msg(sb, KERN_INFO,
2341 "Quota format mount options ignored "
Jan Kara262b4662013-03-02 17:57:08 -05002342 "when QUOTA feature is enabled");
Theodore Ts'oc325a672016-04-03 17:03:37 -04002343 return 1;
Jan Kara262b4662013-03-02 17:57:08 -05002344 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002345 sbi->s_jquota_fmt = m->mount_opt;
2346#endif
Ira Weiny9cb20f92020-05-28 08:00:00 -07002347 } else if (token == Opt_dax || token == Opt_dax_always ||
2348 token == Opt_dax_inode || token == Opt_dax_never) {
Dan Williamsef83b6e2015-09-29 15:48:11 -04002349#ifdef CONFIG_FS_DAX
Ira Weiny9cb20f92020-05-28 08:00:00 -07002350 switch (token) {
2351 case Opt_dax:
2352 case Opt_dax_always:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002353 if (is_remount &&
2354 (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2355 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
2356 fail_dax_change_remount:
2357 ext4_msg(sb, KERN_ERR, "can't change "
2358 "dax mount option while remounting");
2359 return -1;
2360 }
2361 if (is_remount &&
2362 (test_opt(sb, DATA_FLAGS) ==
2363 EXT4_MOUNT_JOURNAL_DATA)) {
2364 ext4_msg(sb, KERN_ERR, "can't mount with "
2365 "both data=journal and dax");
2366 return -1;
2367 }
Ira Weiny9cb20f92020-05-28 08:00:00 -07002368 ext4_msg(sb, KERN_WARNING,
2369 "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
2370 sbi->s_mount_opt |= EXT4_MOUNT_DAX_ALWAYS;
2371 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2372 break;
2373 case Opt_dax_never:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002374 if (is_remount &&
2375 (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2376 (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS)))
2377 goto fail_dax_change_remount;
Ira Weiny9cb20f92020-05-28 08:00:00 -07002378 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2379 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2380 break;
2381 case Opt_dax_inode:
Theodore Ts'o829b37b2020-06-10 11:16:37 -04002382 if (is_remount &&
2383 ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
2384 (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
2385 !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE)))
2386 goto fail_dax_change_remount;
Ira Weiny9cb20f92020-05-28 08:00:00 -07002387 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
2388 sbi->s_mount_opt2 &= ~EXT4_MOUNT2_DAX_NEVER;
2389 /* Strictly for printing options */
2390 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_INODE;
2391 break;
2392 }
Dan Williamsef83b6e2015-09-29 15:48:11 -04002393#else
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002394 ext4_msg(sb, KERN_INFO, "dax option not supported");
Ira Weiny9cb20f92020-05-28 08:00:00 -07002395 sbi->s_mount_opt2 |= EXT4_MOUNT2_DAX_NEVER;
2396 sbi->s_mount_opt &= ~EXT4_MOUNT_DAX_ALWAYS;
Ross Zwisler923ae0f2015-02-16 15:59:38 -08002397 return -1;
2398#endif
Ales Novak7915a862016-03-12 21:55:50 -05002399 } else if (token == Opt_data_err_abort) {
2400 sbi->s_mount_opt |= m->mount_opt;
2401 } else if (token == Opt_data_err_ignore) {
2402 sbi->s_mount_opt &= ~m->mount_opt;
Jan Kara5f3633e2013-02-02 23:09:36 -05002403 } else {
2404 if (!args->from)
2405 arg = 1;
2406 if (m->flags & MOPT_CLEAR)
2407 arg = !arg;
2408 else if (unlikely(!(m->flags & MOPT_SET))) {
2409 ext4_msg(sb, KERN_WARNING,
2410 "buggy handling of option %s", opt);
2411 WARN_ON(1);
2412 return -1;
2413 }
Harshad Shirwadkar995a3ed2020-10-15 13:37:54 -07002414 if (m->flags & MOPT_2) {
2415 if (arg != 0)
2416 sbi->s_mount_opt2 |= m->mount_opt;
2417 else
2418 sbi->s_mount_opt2 &= ~m->mount_opt;
2419 } else {
2420 if (arg != 0)
2421 sbi->s_mount_opt |= m->mount_opt;
2422 else
2423 sbi->s_mount_opt &= ~m->mount_opt;
2424 }
Jan Kara5f3633e2013-02-02 23:09:36 -05002425 }
2426 return 1;
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002427}
2428
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002429static int parse_options(char *options, struct super_block *sb,
Theodore Ts'oc3191062009-01-06 11:14:25 -05002430 unsigned long *journal_devnum,
Theodore Ts'ob3881f72009-01-05 22:46:26 -05002431 unsigned int *journal_ioprio,
Eric Sandeen661aa522012-02-20 17:53:04 -05002432 int is_remount)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002433{
Olof Johansson1e1a76e2019-11-10 18:25:23 -08002434 struct ext4_sb_info __maybe_unused *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002435 char *p, __maybe_unused *usr_qf_name, __maybe_unused *grp_qf_name;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002436 substring_t args[MAX_OPT_ARGS];
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002437 int token;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002438
2439 if (!options)
2440 return 1;
2441
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002442 while ((p = strsep(&options, ",")) != NULL) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002443 if (!*p)
2444 continue;
Eric Sandeen15121c12010-02-15 20:17:55 -05002445 /*
2446 * Initialize args struct so we know whether arg was
2447 * found; some options take optional arguments.
2448 */
Sachin Kamatcaecd0a2012-08-18 22:29:18 -04002449 args[0].to = args[0].from = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002450 token = match_token(p, tokens, args);
Theodore Ts'o26092bf2012-03-03 23:20:47 -05002451 if (handle_mount_opt(sb, p, token, args, journal_devnum,
2452 journal_ioprio, is_remount) < 0)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002453 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002454 }
2455#ifdef CONFIG_QUOTA
Jan Kara49da9392016-09-05 23:08:16 -04002456 /*
2457 * We do the test below only for project quotas. 'usrquota' and
2458 * 'grpquota' mount options are allowed even without quota feature
2459 * to support legacy quotas in quota files.
2460 */
2461 if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
2462 ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
2463 "Cannot enable project quota enforcement.");
2464 return 0;
2465 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002466 usr_qf_name = get_qf_name(sb, sbi, USRQUOTA);
2467 grp_qf_name = get_qf_name(sb, sbi, GRPQUOTA);
2468 if (usr_qf_name || grp_qf_name) {
2469 if (test_opt(sb, USRQUOTA) && usr_qf_name)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05002470 clear_opt(sb, USRQUOTA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002471
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002472 if (test_opt(sb, GRPQUOTA) && grp_qf_name)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05002473 clear_opt(sb, GRPQUOTA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002474
Dmitry Monakhov56c50f12010-03-01 23:28:41 -05002475 if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002476 ext4_msg(sb, KERN_ERR, "old and new quota "
2477 "format mixing");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002478 return 0;
2479 }
2480
2481 if (!sbi->s_jquota_fmt) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002482 ext4_msg(sb, KERN_ERR, "journaled quota format "
2483 "not specified");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002484 return 0;
2485 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002486 }
2487#endif
Ritesh Harjani626b0352020-03-28 01:37:44 +05302488 if (test_opt(sb, DIOREAD_NOLOCK)) {
2489 int blocksize =
2490 BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
2491 if (blocksize < PAGE_SIZE)
2492 ext4_msg(sb, KERN_WARNING, "Warning: mounting with an "
2493 "experimental mount option 'dioread_nolock' "
2494 "for blocksize < PAGE_SIZE");
2495 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002496 return 1;
2497}
2498
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002499static inline void ext4_show_quota_options(struct seq_file *seq,
2500 struct super_block *sb)
2501{
2502#if defined(CONFIG_QUOTA)
2503 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002504 char *usr_qf_name, *grp_qf_name;
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002505
2506 if (sbi->s_jquota_fmt) {
2507 char *fmtname = "";
2508
2509 switch (sbi->s_jquota_fmt) {
2510 case QFMT_VFS_OLD:
2511 fmtname = "vfsold";
2512 break;
2513 case QFMT_VFS_V0:
2514 fmtname = "vfsv0";
2515 break;
2516 case QFMT_VFS_V1:
2517 fmtname = "vfsv1";
2518 break;
2519 }
2520 seq_printf(seq, ",jqfmt=%s", fmtname);
2521 }
2522
Theodore Ts'o33458ea2018-10-12 09:28:09 -04002523 rcu_read_lock();
2524 usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
2525 grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
2526 if (usr_qf_name)
2527 seq_show_option(seq, "usrjquota", usr_qf_name);
2528 if (grp_qf_name)
2529 seq_show_option(seq, "grpjquota", grp_qf_name);
2530 rcu_read_unlock();
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002531#endif
2532}
2533
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002534static const char *token2str(int token)
2535{
Herton Ronaldo Krzesinski50df9fd2012-09-23 22:49:12 -04002536 const struct match_token *t;
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002537
2538 for (t = tokens; t->token != Opt_err; t++)
2539 if (t->token == token && !strchr(t->pattern, '='))
2540 break;
2541 return t->pattern;
2542}
2543
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002544/*
2545 * Show an option if
2546 * - it's set to a non-default value OR
2547 * - if the per-sb default is different from the global default
2548 */
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002549static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
2550 int nodefs)
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002551{
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002552 struct ext4_sb_info *sbi = EXT4_SB(sb);
2553 struct ext4_super_block *es = sbi->s_es;
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002554 int def_errors, def_mount_opt = sbi->s_def_mount_opt;
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002555 const struct mount_opts *m;
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002556 char sep = nodefs ? '\n' : ',';
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002557
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002558#define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
2559#define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002560
2561 if (sbi->s_sb_block != 1)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002562 SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
2563
2564 for (m = ext4_mount_opts; m->token != Opt_err; m++) {
2565 int want_set = m->flags & MOPT_SET;
2566 if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
Ira Weiny9cb20f92020-05-28 08:00:00 -07002567 (m->flags & MOPT_CLEAR_ERR) || m->flags & MOPT_SKIP)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002568 continue;
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002569 if (!nodefs && !(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002570 continue; /* skip if same as the default */
2571 if ((want_set &&
2572 (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
2573 (!want_set && (sbi->s_mount_opt & m->mount_opt)))
2574 continue; /* select Opt_noFoo vs Opt_Foo */
2575 SEQ_OPTS_PRINT("%s", token2str(m->token));
2576 }
2577
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002578 if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002579 le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002580 SEQ_OPTS_PRINT("resuid=%u",
2581 from_kuid_munged(&init_user_ns, sbi->s_resuid));
2582 if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002583 le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
Eric W. Biederman08cefc72012-02-07 15:41:49 -08002584 SEQ_OPTS_PRINT("resgid=%u",
2585 from_kgid_munged(&init_user_ns, sbi->s_resgid));
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002586 def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002587 if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
2588 SEQ_OPTS_PUTS("errors=remount-ro");
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002589 if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002590 SEQ_OPTS_PUTS("errors=continue");
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002591 if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002592 SEQ_OPTS_PUTS("errors=panic");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002593 if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002594 SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002595 if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002596 SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002597 if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002598 SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
Matthew Garrett357fdad2017-10-18 13:56:26 -07002599 if (sb->s_flags & SB_I_VERSION)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002600 SEQ_OPTS_PUTS("i_version");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002601 if (nodefs || sbi->s_stripe)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002602 SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
Tyson Nottingham68afa7e2018-03-30 00:51:10 -04002603 if (nodefs || EXT4_MOUNT_DATA_FLAGS &
2604 (sbi->s_mount_opt ^ def_mount_opt)) {
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002605 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
2606 SEQ_OPTS_PUTS("data=journal");
2607 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
2608 SEQ_OPTS_PUTS("data=ordered");
2609 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
2610 SEQ_OPTS_PUTS("data=writeback");
2611 }
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002612 if (nodefs ||
2613 sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002614 SEQ_OPTS_PRINT("inode_readahead_blks=%u",
2615 sbi->s_inode_readahead_blks);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002616
Tyson Nottinghamceec0372018-03-30 00:53:33 -04002617 if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002618 (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
Theodore Ts'o5a916be2012-03-04 19:27:31 -05002619 SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
Theodore Ts'odf981d02012-08-17 09:48:17 -04002620 if (nodefs || sbi->s_max_dir_size_kb)
2621 SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
Ales Novak7915a862016-03-12 21:55:50 -05002622 if (test_opt(sb, DATA_ERR_ABORT))
2623 SEQ_OPTS_PUTS("data_err=abort");
Eric Biggersed318a62020-05-12 16:32:50 -07002624
2625 fscrypt_show_test_dummy_encryption(seq, sep, sb);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002626
Eric Biggers4f74d152020-07-02 01:56:07 +00002627 if (sb->s_flags & SB_INLINECRYPT)
2628 SEQ_OPTS_PUTS("inlinecrypt");
2629
Ira Weiny9cb20f92020-05-28 08:00:00 -07002630 if (test_opt(sb, DAX_ALWAYS)) {
2631 if (IS_EXT2_SB(sb))
2632 SEQ_OPTS_PUTS("dax");
2633 else
2634 SEQ_OPTS_PUTS("dax=always");
2635 } else if (test_opt2(sb, DAX_NEVER)) {
2636 SEQ_OPTS_PUTS("dax=never");
2637 } else if (test_opt2(sb, DAX_INODE)) {
2638 SEQ_OPTS_PUTS("dax=inode");
2639 }
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002640 ext4_show_quota_options(seq, sb);
Theodore Ts'o2adf6da2012-03-03 23:20:50 -05002641 return 0;
2642}
2643
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002644static int ext4_show_options(struct seq_file *seq, struct dentry *root)
2645{
2646 return _ext4_show_options(seq, root->d_sb, 0);
2647}
2648
Theodore Ts'oebd173b2015-09-23 12:46:17 -04002649int ext4_seq_options_show(struct seq_file *seq, void *offset)
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002650{
2651 struct super_block *sb = seq->private;
2652 int rc;
2653
David Howellsbc98a422017-07-17 08:45:34 +01002654 seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
Theodore Ts'o66acdcf2012-03-04 20:21:38 -05002655 rc = _ext4_show_options(seq, sb, 1);
2656 seq_puts(seq, "\n");
2657 return rc;
2658}
2659
Mingming Cao617ba132006-10-11 01:20:53 -07002660static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002661 int read_only)
2662{
Mingming Cao617ba132006-10-11 01:20:53 -07002663 struct ext4_sb_info *sbi = EXT4_SB(sb);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002664 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002665
Mingming Cao617ba132006-10-11 01:20:53 -07002666 if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002667 ext4_msg(sb, KERN_ERR, "revision level too high, "
2668 "forcing read-only mode");
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002669 err = -EROFS;
yangerkun5adacca2020-06-01 15:34:04 +08002670 goto done;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002671 }
2672 if (read_only)
Theodore Ts'o281b5992011-09-09 18:34:51 -04002673 goto done;
Mingming Cao617ba132006-10-11 01:20:53 -07002674 if (!(sbi->s_mount_state & EXT4_VALID_FS))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002675 ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
2676 "running e2fsck is recommended");
Lukas Czernerc8b459f2014-05-12 12:55:07 -04002677 else if (sbi->s_mount_state & EXT4_ERROR_FS)
Eric Sandeenb31e1552009-06-04 17:36:36 -04002678 ext4_msg(sb, KERN_WARNING,
2679 "warning: mounting fs with errors, "
2680 "running e2fsck is recommended");
Tao Maed3ce802011-05-18 13:29:57 -04002681 else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002682 le16_to_cpu(es->s_mnt_count) >=
2683 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002684 ext4_msg(sb, KERN_WARNING,
2685 "warning: maximal mount count reached, "
2686 "running e2fsck is recommended");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002687 else if (le32_to_cpu(es->s_checkinterval) &&
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04002688 (ext4_get_tstamp(es, s_lastcheck) +
2689 le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
Eric Sandeenb31e1552009-06-04 17:36:36 -04002690 ext4_msg(sb, KERN_WARNING,
2691 "warning: checktime reached, "
2692 "running e2fsck is recommended");
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04002693 if (!sbi->s_journal)
Frank Mayhar03901312009-01-07 00:06:22 -05002694 es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002695 if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
Mingming Cao617ba132006-10-11 01:20:53 -07002696 es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
Marcin Slusarze8546d02008-04-17 10:38:59 -04002697 le16_add_cpu(&es->s_mnt_count, 1);
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04002698 ext4_update_tstamp(es, s_mtime);
Frank Mayhar03901312009-01-07 00:06:22 -05002699 if (sbi->s_journal)
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002700 ext4_set_feature_journal_needs_recovery(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002701
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002702 err = ext4_commit_super(sb, 1);
Theodore Ts'o281b5992011-09-09 18:34:51 -04002703done:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002704 if (test_opt(sb, DEBUG))
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002705 printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
Theodore Ts'oa2595b82010-12-15 20:30:48 -05002706 "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002707 sb->s_blocksize,
2708 sbi->s_groups_count,
Mingming Cao617ba132006-10-11 01:20:53 -07002709 EXT4_BLOCKS_PER_GROUP(sb),
2710 EXT4_INODES_PER_GROUP(sb),
Theodore Ts'oa2595b82010-12-15 20:30:48 -05002711 sbi->s_mount_opt, sbi->s_mount_opt2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002712
Dan Magenheimer7abc52c2011-05-26 10:02:03 -06002713 cleancache_init_fs(sb);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04002714 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002715}
2716
Theodore Ts'o117fff12012-09-05 01:29:50 -04002717int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
2718{
2719 struct ext4_sb_info *sbi = EXT4_SB(sb);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002720 struct flex_groups **old_groups, **new_groups;
Dan Carpenter37b0b6b2020-02-28 12:22:56 +03002721 int size, i, j;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002722
2723 if (!sbi->s_log_groups_per_flex)
2724 return 0;
2725
2726 size = ext4_flex_group(sbi, ngroup - 1) + 1;
2727 if (size <= sbi->s_flex_groups_allocated)
2728 return 0;
2729
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002730 new_groups = kvzalloc(roundup_pow_of_two(size *
2731 sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002732 if (!new_groups) {
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002733 ext4_msg(sb, KERN_ERR,
2734 "not enough memory for %d flex group pointers", size);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002735 return -ENOMEM;
2736 }
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002737 for (i = sbi->s_flex_groups_allocated; i < size; i++) {
2738 new_groups[i] = kvzalloc(roundup_pow_of_two(
2739 sizeof(struct flex_groups)),
2740 GFP_KERNEL);
2741 if (!new_groups[i]) {
Dan Carpenter37b0b6b2020-02-28 12:22:56 +03002742 for (j = sbi->s_flex_groups_allocated; j < i; j++)
2743 kvfree(new_groups[j]);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002744 kvfree(new_groups);
2745 ext4_msg(sb, KERN_ERR,
2746 "not enough memory for %d flex groups", size);
2747 return -ENOMEM;
2748 }
Theodore Ts'o117fff12012-09-05 01:29:50 -04002749 }
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002750 rcu_read_lock();
2751 old_groups = rcu_dereference(sbi->s_flex_groups);
2752 if (old_groups)
2753 memcpy(new_groups, old_groups,
2754 (sbi->s_flex_groups_allocated *
2755 sizeof(struct flex_groups *)));
2756 rcu_read_unlock();
2757 rcu_assign_pointer(sbi->s_flex_groups, new_groups);
2758 sbi->s_flex_groups_allocated = size;
2759 if (old_groups)
2760 ext4_kvfree_array_rcu(old_groups);
Theodore Ts'o117fff12012-09-05 01:29:50 -04002761 return 0;
2762}
2763
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002764static int ext4_fill_flex_info(struct super_block *sb)
2765{
2766 struct ext4_sb_info *sbi = EXT4_SB(sb);
2767 struct ext4_group_desc *gdp = NULL;
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002768 struct flex_groups *fg;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002769 ext4_group_t flex_group;
Theodore Ts'o117fff12012-09-05 01:29:50 -04002770 int i, err;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002771
Theodore Ts'o503358a2009-11-23 07:24:46 -05002772 sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
Xi Wangd50f2ab2012-01-10 11:51:10 -05002773 if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002774 sbi->s_log_groups_per_flex = 0;
2775 return 1;
2776 }
2777
Theodore Ts'o117fff12012-09-05 01:29:50 -04002778 err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
2779 if (err)
Theodore Ts'o9933fc02011-08-01 08:45:02 -04002780 goto failed;
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002781
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002782 for (i = 0; i < sbi->s_groups_count; i++) {
Theodore Ts'o88b6edd2009-05-25 11:50:39 -04002783 gdp = ext4_get_group_desc(sb, i, NULL);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002784
2785 flex_group = ext4_flex_group(sbi, i);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002786 fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
2787 atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
Theodore Ts'o90ba9832013-03-11 23:39:59 -04002788 atomic64_add(ext4_free_group_clusters(sb, gdp),
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08002789 &fg->free_clusters);
2790 atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
Jose R. Santos772cb7c2008-07-11 19:27:31 -04002791 }
2792
2793 return 1;
2794failed:
2795 return 0;
2796}
2797
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002798static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002799 struct ext4_group_desc *gdp)
Andreas Dilger717d50e2007-10-16 18:38:25 -04002800{
Daeho Jeongb47820e2016-07-03 17:51:39 -04002801 int offset = offsetof(struct ext4_group_desc, bg_checksum);
Andreas Dilger717d50e2007-10-16 18:38:25 -04002802 __u16 crc = 0;
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002803 __le32 le_group = cpu_to_le32(block_group);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002804 struct ext4_sb_info *sbi = EXT4_SB(sb);
Andreas Dilger717d50e2007-10-16 18:38:25 -04002805
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04002806 if (ext4_has_metadata_csum(sbi->s_sb)) {
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002807 /* Use new metadata_csum algorithm */
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002808 __u32 csum32;
Daeho Jeongb47820e2016-07-03 17:51:39 -04002809 __u16 dummy_csum = 0;
Andreas Dilger717d50e2007-10-16 18:38:25 -04002810
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002811 csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2812 sizeof(le_group));
Daeho Jeongb47820e2016-07-03 17:51:39 -04002813 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
2814 csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
2815 sizeof(dummy_csum));
2816 offset += sizeof(dummy_csum);
2817 if (offset < sbi->s_desc_size)
2818 csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
2819 sbi->s_desc_size - offset);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002820
2821 crc = csum32 & 0xFFFF;
2822 goto out;
Andreas Dilger717d50e2007-10-16 18:38:25 -04002823 }
2824
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002825 /* old crc16 code */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002826 if (!ext4_has_feature_gdt_csum(sb))
Darrick J. Wong813d32f2014-10-14 02:35:49 -04002827 return 0;
2828
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002829 crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2830 crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2831 crc = crc16(crc, (__u8 *)gdp, offset);
2832 offset += sizeof(gdp->bg_checksum); /* skip checksum */
2833 /* for checksum of struct ext4_group_desc do the rest...*/
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002834 if (ext4_has_feature_64bit(sb) &&
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002835 offset < le16_to_cpu(sbi->s_es->s_desc_size))
2836 crc = crc16(crc, (__u8 *)gdp + offset,
2837 le16_to_cpu(sbi->s_es->s_desc_size) -
2838 offset);
2839
2840out:
Andreas Dilger717d50e2007-10-16 18:38:25 -04002841 return cpu_to_le16(crc);
2842}
2843
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002844int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
Andreas Dilger717d50e2007-10-16 18:38:25 -04002845 struct ext4_group_desc *gdp)
2846{
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002847 if (ext4_has_group_desc_csum(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002848 (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
Andreas Dilger717d50e2007-10-16 18:38:25 -04002849 return 0;
2850
2851 return 1;
2852}
2853
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002854void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2855 struct ext4_group_desc *gdp)
2856{
2857 if (!ext4_has_group_desc_csum(sb))
2858 return;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002859 gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002860}
2861
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002862/* Called at mount-time, super-block is locked */
Lukas Czernerbfff6872010-10-27 21:30:05 -04002863static int ext4_check_descriptors(struct super_block *sb,
Theodore Ts'o829fa702016-08-01 00:51:02 -04002864 ext4_fsblk_t sb_block,
Lukas Czernerbfff6872010-10-27 21:30:05 -04002865 ext4_group_t *first_not_zeroed)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002866{
Mingming Cao617ba132006-10-11 01:20:53 -07002867 struct ext4_sb_info *sbi = EXT4_SB(sb);
2868 ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2869 ext4_fsblk_t last_block;
Theodore Ts'o44de0222018-07-08 19:35:02 -04002870 ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002871 ext4_fsblk_t block_bitmap;
2872 ext4_fsblk_t inode_bitmap;
2873 ext4_fsblk_t inode_table;
Jose R. Santosce421582007-10-16 18:38:25 -04002874 int flexbg_flag = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04002875 ext4_group_t i, grp = sbi->s_groups_count;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002876
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002877 if (ext4_has_feature_flex_bg(sb))
Jose R. Santosce421582007-10-16 18:38:25 -04002878 flexbg_flag = 1;
2879
Theodore Ts'oaf5bc922008-09-08 22:25:24 -04002880 ext4_debug("Checking group descriptors");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002881
Akinobu Mita197cd65a2008-02-06 01:40:16 -08002882 for (i = 0; i < sbi->s_groups_count; i++) {
2883 struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2884
Jose R. Santosce421582007-10-16 18:38:25 -04002885 if (i == sbi->s_groups_count - 1 || flexbg_flag)
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002886 last_block = ext4_blocks_count(sbi->s_es) - 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002887 else
2888 last_block = first_block +
Mingming Cao617ba132006-10-11 01:20:53 -07002889 (EXT4_BLOCKS_PER_GROUP(sb) - 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002890
Lukas Czernerbfff6872010-10-27 21:30:05 -04002891 if ((grp == sbi->s_groups_count) &&
2892 !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2893 grp = i;
2894
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002895 block_bitmap = ext4_block_bitmap(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002896 if (block_bitmap == sb_block) {
2897 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2898 "Block bitmap for group %u overlaps "
2899 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002900 if (!sb_rdonly(sb))
2901 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002902 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002903 if (block_bitmap >= sb_block + 1 &&
2904 block_bitmap <= last_bg_block) {
2905 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2906 "Block bitmap for group %u overlaps "
2907 "block group descriptors", i);
2908 if (!sb_rdonly(sb))
2909 return 0;
2910 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002911 if (block_bitmap < first_block || block_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002912 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002913 "Block bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002914 "(block %llu)!", i, block_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002915 return 0;
2916 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002917 inode_bitmap = ext4_inode_bitmap(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002918 if (inode_bitmap == sb_block) {
2919 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2920 "Inode bitmap for group %u overlaps "
2921 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002922 if (!sb_rdonly(sb))
2923 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002924 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002925 if (inode_bitmap >= sb_block + 1 &&
2926 inode_bitmap <= last_bg_block) {
2927 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2928 "Inode bitmap for group %u overlaps "
2929 "block group descriptors", i);
2930 if (!sb_rdonly(sb))
2931 return 0;
2932 }
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002933 if (inode_bitmap < first_block || inode_bitmap > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002934 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002935 "Inode bitmap for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002936 "(block %llu)!", i, inode_bitmap);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002937 return 0;
2938 }
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07002939 inode_table = ext4_inode_table(sb, gdp);
Theodore Ts'o829fa702016-08-01 00:51:02 -04002940 if (inode_table == sb_block) {
2941 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2942 "Inode table for group %u overlaps "
2943 "superblock", i);
Theodore Ts'o18db4b42018-03-29 22:10:35 -04002944 if (!sb_rdonly(sb))
2945 return 0;
Theodore Ts'o829fa702016-08-01 00:51:02 -04002946 }
Theodore Ts'o77260802018-06-13 23:08:26 -04002947 if (inode_table >= sb_block + 1 &&
2948 inode_table <= last_bg_block) {
2949 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2950 "Inode table for group %u overlaps "
2951 "block group descriptors", i);
2952 if (!sb_rdonly(sb))
2953 return 0;
2954 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07002955 if (inode_table < first_block ||
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002956 inode_table + sbi->s_itb_per_group - 1 > last_block) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002957 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
Theodore Ts'oa9df9a42009-01-05 22:18:16 -05002958 "Inode table for group %u not in group "
Eric Sandeenb31e1552009-06-04 17:36:36 -04002959 "(block %llu)!", i, inode_table);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002960 return 0;
2961 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002962 ext4_lock_group(sb, i);
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04002963 if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04002964 ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2965 "Checksum for group %u failed (%u!=%u)",
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04002966 i, le16_to_cpu(ext4_group_desc_csum(sb, i,
Eric Sandeenb31e1552009-06-04 17:36:36 -04002967 gdp)), le16_to_cpu(gdp->bg_checksum));
David Howellsbc98a422017-07-17 08:45:34 +01002968 if (!sb_rdonly(sb)) {
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002969 ext4_unlock_group(sb, i);
Theodore Ts'o8a266462008-07-26 14:34:21 -04002970 return 0;
Li Zefan7ee1ec42008-09-08 10:47:19 -04002971 }
Andreas Dilger717d50e2007-10-16 18:38:25 -04002972 }
Aneesh Kumar K.V955ce5f2009-05-02 20:35:09 -04002973 ext4_unlock_group(sb, i);
Jose R. Santosce421582007-10-16 18:38:25 -04002974 if (!flexbg_flag)
2975 first_block += EXT4_BLOCKS_PER_GROUP(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002976 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04002977 if (NULL != first_not_zeroed)
2978 *first_not_zeroed = grp;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002979 return 1;
2980}
2981
Mingming Cao617ba132006-10-11 01:20:53 -07002982/* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002983 * the superblock) which were deleted from all directories, but held open by
2984 * a process at the time of a crash. We walk the list and try to delete these
2985 * inodes at recovery time (only with a read-write filesystem).
2986 *
2987 * In order to keep the orphan inode chain consistent during traversal (in
2988 * case of crash during recovery), we link each inode into the superblock
2989 * orphan list_head and handle it the same way as an inode deletion during
2990 * normal operation (which journals the operations for us).
2991 *
2992 * We only do an iget() and an iput() on each inode, which is very safe if we
2993 * accidentally point at an in-use or already deleted inode. The worst that
2994 * can happen in this case is that we get a "bit already cleared" message from
Mingming Cao617ba132006-10-11 01:20:53 -07002995 * ext4_free_inode(). The only reason we would point at a wrong inode is if
Dave Kleikampac27a0e2006-10-11 01:20:50 -07002996 * e2fsck was run on this filesystem, and it must have already done the orphan
2997 * inode cleanup for us, so we can safely abort without any further action.
2998 */
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04002999static void ext4_orphan_cleanup(struct super_block *sb,
3000 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003001{
3002 unsigned int s_flags = sb->s_flags;
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05003003 int ret, nr_orphans = 0, nr_truncates = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003004#ifdef CONFIG_QUOTA
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003005 int quota_update = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003006 int i;
3007#endif
3008 if (!es->s_last_orphan) {
3009 jbd_debug(4, "no orphan inodes to clean up\n");
3010 return;
3011 }
3012
Eric Sandeena8f48a92006-12-06 20:40:13 -08003013 if (bdev_read_only(sb->s_bdev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003014 ext4_msg(sb, KERN_ERR, "write access "
3015 "unavailable, skipping orphan cleanup");
Eric Sandeena8f48a92006-12-06 20:40:13 -08003016 return;
3017 }
3018
Amir Goldsteind39195c2011-02-28 00:53:45 -05003019 /* Check if feature set would not allow a r/w mount */
3020 if (!ext4_feature_set_ok(sb, 0)) {
3021 ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
3022 "unknown ROCOMPAT features");
3023 return;
3024 }
3025
Mingming Cao617ba132006-10-11 01:20:53 -07003026 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
Eric Sandeenc25f9bc2012-09-26 23:30:12 -04003027 /* don't clear list on RO mount w/ errors */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003028 if (es->s_last_orphan && !(s_flags & SB_RDONLY)) {
Dmitry Monakhov84474972014-09-16 14:52:03 -04003029 ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003030 "clearing orphan list.\n");
Eric Sandeenc25f9bc2012-09-26 23:30:12 -04003031 es->s_last_orphan = 0;
3032 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003033 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3034 return;
3035 }
3036
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003037 if (s_flags & SB_RDONLY) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04003038 ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003039 sb->s_flags &= ~SB_RDONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003040 }
3041#ifdef CONFIG_QUOTA
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003042 /*
3043 * Turn on quotas which were not enabled for read-only mounts if
3044 * filesystem has quota feature, so that they are updated correctly.
3045 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003046 if (ext4_has_feature_quota(sb) && (s_flags & SB_RDONLY)) {
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003047 int ret = ext4_enable_quotas(sb);
3048
3049 if (!ret)
3050 quota_update = 1;
3051 else
3052 ext4_msg(sb, KERN_ERR,
3053 "Cannot turn on quotas: error %d", ret);
3054 }
3055
3056 /* Turn on journaled quotas used for old sytle */
Jan Karaa2d4a642014-09-11 11:15:15 -04003057 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
Mingming Cao617ba132006-10-11 01:20:53 -07003058 if (EXT4_SB(sb)->s_qf_names[i]) {
3059 int ret = ext4_quota_on_mount(sb, i);
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003060
3061 if (!ret)
3062 quota_update = 1;
3063 else
Eric Sandeenb31e1552009-06-04 17:36:36 -04003064 ext4_msg(sb, KERN_ERR,
3065 "Cannot turn on journaled "
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003066 "quota: type %d: error %d", i, ret);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003067 }
3068 }
3069#endif
3070
3071 while (es->s_last_orphan) {
3072 struct inode *inode;
3073
Vegard Nossumc65d5c62016-07-14 23:21:35 -04003074 /*
3075 * We may have encountered an error during cleanup; if
3076 * so, skip the rest.
3077 */
3078 if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
3079 jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
3080 es->s_last_orphan = 0;
3081 break;
3082 }
3083
Josef Bacik97bd42b2008-04-29 22:04:56 -04003084 inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
3085 if (IS_ERR(inode)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003086 es->s_last_orphan = 0;
3087 break;
3088 }
3089
Mingming Cao617ba132006-10-11 01:20:53 -07003090 list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
Christoph Hellwig871a2932010-03-03 09:05:07 -05003091 dquot_initialize(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003092 if (inode->i_nlink) {
Paul Taysom566370a2013-05-28 07:51:21 -04003093 if (test_opt(sb, DEBUG))
3094 ext4_msg(sb, KERN_DEBUG,
3095 "%s: truncating inode %lu to %lld bytes",
3096 __func__, inode->i_ino, inode->i_size);
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04003097 jbd_debug(2, "truncating inode %lu to %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003098 inode->i_ino, inode->i_size);
Al Viro59551022016-01-22 15:40:57 -05003099 inode_lock(inode);
Lukas Czerner55f252c2013-05-27 23:32:35 -04003100 truncate_inode_pages(inode->i_mapping, inode->i_size);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05003101 ret = ext4_truncate(inode);
Zhang Yi076d9b02021-05-07 15:19:04 +08003102 if (ret) {
3103 /*
3104 * We need to clean up the in-core orphan list
3105 * manually if ext4_truncate() failed to get a
3106 * transaction handle.
3107 */
3108 ext4_orphan_del(NULL, inode);
Theodore Ts'o2c98eb52016-11-13 22:02:26 -05003109 ext4_std_error(inode->i_sb, ret);
Zhang Yi076d9b02021-05-07 15:19:04 +08003110 }
Al Viro59551022016-01-22 15:40:57 -05003111 inode_unlock(inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003112 nr_truncates++;
3113 } else {
Paul Taysom566370a2013-05-28 07:51:21 -04003114 if (test_opt(sb, DEBUG))
3115 ext4_msg(sb, KERN_DEBUG,
3116 "%s: deleting unreferenced inode %lu",
3117 __func__, inode->i_ino);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003118 jbd_debug(2, "deleting unreferenced inode %lu\n",
3119 inode->i_ino);
3120 nr_orphans++;
3121 }
3122 iput(inode); /* The delete magic happens here! */
3123 }
3124
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04003125#define PLURAL(x) (x), ((x) == 1) ? "" : "s"
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003126
3127 if (nr_orphans)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003128 ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
3129 PLURAL(nr_orphans));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003130 if (nr_truncates)
Eric Sandeenb31e1552009-06-04 17:36:36 -04003131 ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
3132 PLURAL(nr_truncates));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003133#ifdef CONFIG_QUOTA
zhangyi (F)95f1fda2017-08-24 15:21:50 -04003134 /* Turn off quotas if they were enabled for orphan cleanup */
3135 if (quota_update) {
3136 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
3137 if (sb_dqopt(sb)->files[i])
3138 dquot_quota_off(sb, i);
3139 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003140 }
3141#endif
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003142 sb->s_flags = s_flags; /* Restore SB_RDONLY status */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003143}
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003144
Eric Sandeencd2291a2008-01-28 23:58:27 -05003145/*
3146 * Maximal extent format file size.
3147 * Resulting logical blkno at s_maxbytes must fit in our on-disk
3148 * extent format containers, within a sector_t, and within i_blocks
3149 * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
3150 * so that won't be a limiting factor.
3151 *
Lukas Czernerf17722f2011-06-06 00:05:17 -04003152 * However there is other limiting factor. We do store extents in the form
3153 * of starting block and length, hence the resulting length of the extent
3154 * covering maximum file size must fit into on-disk format containers as
3155 * well. Given that length is always by 1 unit bigger than max unit (because
3156 * we count 0 as well) we have to lower the s_maxbytes by one fs block.
3157 *
Eric Sandeencd2291a2008-01-28 23:58:27 -05003158 * Note, this does *not* consider any metadata overhead for vfs i_blocks.
3159 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003160static loff_t ext4_max_size(int blkbits, int has_huge_files)
Eric Sandeencd2291a2008-01-28 23:58:27 -05003161{
3162 loff_t res;
3163 loff_t upper_limit = MAX_LFS_FILESIZE;
3164
Christoph Hellwig72deb452019-04-05 18:08:59 +02003165 BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
3166
3167 if (!has_huge_files) {
Eric Sandeencd2291a2008-01-28 23:58:27 -05003168 upper_limit = (1LL << 32) - 1;
3169
3170 /* total blocks in file system block size */
3171 upper_limit >>= (blkbits - 9);
3172 upper_limit <<= blkbits;
3173 }
3174
Lukas Czernerf17722f2011-06-06 00:05:17 -04003175 /*
3176 * 32-bit extent-start container, ee_block. We lower the maxbytes
3177 * by one fs block, so ee_len can cover the extent of maximum file
3178 * size
3179 */
3180 res = (1LL << 32) - 1;
Eric Sandeencd2291a2008-01-28 23:58:27 -05003181 res <<= blkbits;
Eric Sandeencd2291a2008-01-28 23:58:27 -05003182
3183 /* Sanity check against vm- & vfs- imposed limits */
3184 if (res > upper_limit)
3185 res = upper_limit;
3186
3187 return res;
3188}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003189
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003190/*
Eric Sandeencd2291a2008-01-28 23:58:27 -05003191 * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003192 * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
3193 * We need to be 1 filesystem block less than the 2^48 sector limit.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003194 */
Theodore Ts'of287a1a2008-10-16 22:50:48 -04003195static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003196{
Ritesh Harjanid11502f2021-06-05 10:39:32 +05303197 unsigned long long upper_limit, res = EXT4_NDIR_BLOCKS;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003198 int meta_blocks;
Ritesh Harjanid11502f2021-06-05 10:39:32 +05303199
3200 /*
3201 * This is calculated to be the largest file size for a dense, block
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003202 * mapped file such that the file's total number of 512-byte sectors,
3203 * including data and all indirect blocks, does not exceed (2^48 - 1).
3204 *
3205 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
3206 * number of 512-byte sectors of the file.
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003207 */
Christoph Hellwig72deb452019-04-05 18:08:59 +02003208 if (!has_huge_files) {
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003209 /*
Christoph Hellwig72deb452019-04-05 18:08:59 +02003210 * !has_huge_files or implies that the inode i_block field
3211 * represents total file blocks in 2^32 512-byte sectors ==
3212 * size of vfs inode i_blocks * 8
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003213 */
3214 upper_limit = (1LL << 32) - 1;
3215
3216 /* total blocks in file system block size */
3217 upper_limit >>= (bits - 9);
3218
3219 } else {
Aneesh Kumar K.V8180a562008-01-28 23:58:27 -05003220 /*
3221 * We use 48 bit ext4_inode i_blocks
3222 * With EXT4_HUGE_FILE_FL set the i_blocks
3223 * represent total number of blocks in
3224 * file system block size
3225 */
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003226 upper_limit = (1LL << 48) - 1;
3227
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003228 }
3229
3230 /* indirect blocks */
3231 meta_blocks = 1;
3232 /* double indirect blocks */
3233 meta_blocks += 1 + (1LL << (bits-2));
3234 /* tripple indirect blocks */
3235 meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
3236
3237 upper_limit -= meta_blocks;
3238 upper_limit <<= bits;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003239
3240 res += 1LL << (bits-2);
3241 res += 1LL << (2*(bits-2));
3242 res += 1LL << (3*(bits-2));
3243 res <<= bits;
3244 if (res > upper_limit)
3245 res = upper_limit;
Aneesh Kumar K.V0fc1b452008-01-28 23:58:26 -05003246
3247 if (res > MAX_LFS_FILESIZE)
3248 res = MAX_LFS_FILESIZE;
3249
Ritesh Harjanid11502f2021-06-05 10:39:32 +05303250 return (loff_t)res;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003251}
3252
Mingming Cao617ba132006-10-11 01:20:53 -07003253static ext4_fsblk_t descriptor_loc(struct super_block *sb,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003254 ext4_fsblk_t logical_sb_block, int nr)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003255{
Mingming Cao617ba132006-10-11 01:20:53 -07003256 struct ext4_sb_info *sbi = EXT4_SB(sb);
Avantika Mathurfd2d4292008-01-28 23:58:27 -05003257 ext4_group_t bg, first_meta_bg;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003258 int has_super = 0;
3259
3260 first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
3261
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003262 if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
Andrew Morton70bbb3e2006-10-11 01:21:20 -07003263 return logical_sb_block + nr + 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003264 bg = sbi->s_desc_per_block * nr;
Mingming Cao617ba132006-10-11 01:20:53 -07003265 if (ext4_bg_has_super(sb, bg))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003266 has_super = 1;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04003267
Darrick J. Wongbd63f6b2014-05-12 10:06:27 -04003268 /*
3269 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
3270 * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
3271 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
3272 * compensate.
3273 */
3274 if (sb->s_blocksize == 1024 && nr == 0 &&
Jun Piao49598e02018-01-11 13:17:49 -05003275 le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
Darrick J. Wongbd63f6b2014-05-12 10:06:27 -04003276 has_super++;
3277
Mingming Cao617ba132006-10-11 01:20:53 -07003278 return (has_super + ext4_group_first_block_no(sb, bg));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003279}
3280
Alex Tomasc9de5602008-01-29 00:19:52 -05003281/**
3282 * ext4_get_stripe_size: Get the stripe size.
3283 * @sbi: In memory super block info
3284 *
3285 * If we have specified it via mount option, then
3286 * use the mount option value. If the value specified at mount time is
3287 * greater than the blocks per group use the super block value.
3288 * If the super block value is greater than blocks per group return 0.
3289 * Allocator needs it be less than blocks per group.
3290 *
3291 */
3292static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
3293{
3294 unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
3295 unsigned long stripe_width =
3296 le32_to_cpu(sbi->s_es->s_raid_stripe_width);
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003297 int ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05003298
3299 if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003300 ret = sbi->s_stripe;
Jan Kara5469d7c2017-02-10 00:56:09 -05003301 else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003302 ret = stripe_width;
Jan Kara5469d7c2017-02-10 00:56:09 -05003303 else if (stride && stride <= sbi->s_blocks_per_group)
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003304 ret = stride;
3305 else
3306 ret = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003307
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003308 /*
3309 * If the stripe width is 1, this makes no sense and
3310 * we set it to 0 to turn off stripe handling code.
3311 */
3312 if (ret <= 1)
3313 ret = 0;
Alex Tomasc9de5602008-01-29 00:19:52 -05003314
Dan Ehrenberg3eb08652011-07-17 21:18:51 -04003315 return ret;
Alex Tomasc9de5602008-01-29 00:19:52 -05003316}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07003317
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003318/*
3319 * Check whether this filesystem can be mounted based on
3320 * the features present and the RDONLY/RDWR mount requested.
3321 * Returns 1 if this filesystem can be mounted as requested,
3322 * 0 if it cannot be.
3323 */
3324static int ext4_feature_set_ok(struct super_block *sb, int readonly)
3325{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003326 if (ext4_has_unknown_ext4_incompat_features(sb)) {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003327 ext4_msg(sb, KERN_ERR,
3328 "Couldn't mount because of "
3329 "unsupported optional features (%x)",
3330 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
3331 ~EXT4_FEATURE_INCOMPAT_SUPP));
3332 return 0;
3333 }
3334
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04003335#ifndef CONFIG_UNICODE
3336 if (ext4_has_feature_casefold(sb)) {
3337 ext4_msg(sb, KERN_ERR,
3338 "Filesystem with casefold feature cannot be "
3339 "mounted without CONFIG_UNICODE");
3340 return 0;
3341 }
3342#endif
3343
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003344 if (readonly)
3345 return 1;
3346
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003347 if (ext4_has_feature_readonly(sb)) {
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05003348 ext4_msg(sb, KERN_INFO, "filesystem is read-only");
Linus Torvalds1751e8a2017-11-27 13:05:09 -08003349 sb->s_flags |= SB_RDONLY;
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05003350 return 1;
3351 }
3352
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003353 /* Check that feature set is OK for a read-write mount */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003354 if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003355 ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
3356 "unsupported optional features (%x)",
3357 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
3358 ~EXT4_FEATURE_RO_COMPAT_SUPP));
3359 return 0;
3360 }
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003361 if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
Theodore Ts'obab08ab2011-09-09 18:36:51 -04003362 ext4_msg(sb, KERN_ERR,
3363 "Can't support bigalloc feature without "
3364 "extents feature\n");
3365 return 0;
3366 }
Aditya Kali7c319d32012-07-22 20:21:31 -04003367
Jan Kara9db176b2020-02-21 11:08:35 +01003368#if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
Theodore Ts'od65d87a2020-02-14 18:11:19 -05003369 if (!readonly && (ext4_has_feature_quota(sb) ||
3370 ext4_has_feature_project(sb))) {
Aditya Kali7c319d32012-07-22 20:21:31 -04003371 ext4_msg(sb, KERN_ERR,
Theodore Ts'od65d87a2020-02-14 18:11:19 -05003372 "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
Li Xi689c9582016-01-08 16:01:22 -05003373 return 0;
3374 }
Aditya Kali7c319d32012-07-22 20:21:31 -04003375#endif /* CONFIG_QUOTA */
Eric Sandeena13fb1a2009-08-18 00:20:23 -04003376 return 1;
3377}
3378
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003379/*
3380 * This function is called once a day if we have errors logged
3381 * on the file system
3382 */
Kees Cook235699a2017-10-18 12:45:17 -04003383static void print_daily_error_info(struct timer_list *t)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003384{
Kees Cook235699a2017-10-18 12:45:17 -04003385 struct ext4_sb_info *sbi = from_timer(sbi, t, s_err_report);
3386 struct super_block *sb = sbi->s_sb;
3387 struct ext4_super_block *es = sbi->s_es;
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003388
3389 if (es->s_error_count)
Theodore Ts'oae0f78d2014-07-05 18:40:52 -04003390 /* fsck newer than v1.41.13 is needed to clean this condition. */
3391 ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003392 le32_to_cpu(es->s_error_count));
3393 if (es->s_first_error_time) {
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04003394 printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
3395 sb->s_id,
3396 ext4_get_tstamp(es, s_first_error_time),
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003397 (int) sizeof(es->s_first_error_func),
3398 es->s_first_error_func,
3399 le32_to_cpu(es->s_first_error_line));
3400 if (es->s_first_error_ino)
Joe Perches651e1c32016-10-12 23:12:53 -04003401 printk(KERN_CONT ": inode %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003402 le32_to_cpu(es->s_first_error_ino));
3403 if (es->s_first_error_block)
Joe Perches651e1c32016-10-12 23:12:53 -04003404 printk(KERN_CONT ": block %llu", (unsigned long long)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003405 le64_to_cpu(es->s_first_error_block));
Joe Perches651e1c32016-10-12 23:12:53 -04003406 printk(KERN_CONT "\n");
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003407 }
3408 if (es->s_last_error_time) {
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04003409 printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
3410 sb->s_id,
3411 ext4_get_tstamp(es, s_last_error_time),
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003412 (int) sizeof(es->s_last_error_func),
3413 es->s_last_error_func,
3414 le32_to_cpu(es->s_last_error_line));
3415 if (es->s_last_error_ino)
Joe Perches651e1c32016-10-12 23:12:53 -04003416 printk(KERN_CONT ": inode %u",
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003417 le32_to_cpu(es->s_last_error_ino));
3418 if (es->s_last_error_block)
Joe Perches651e1c32016-10-12 23:12:53 -04003419 printk(KERN_CONT ": block %llu", (unsigned long long)
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003420 le64_to_cpu(es->s_last_error_block));
Joe Perches651e1c32016-10-12 23:12:53 -04003421 printk(KERN_CONT "\n");
Theodore Ts'o66e61a92010-07-27 11:56:04 -04003422 }
3423 mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
3424}
3425
Lukas Czernerbfff6872010-10-27 21:30:05 -04003426/* Find next suitable group and run ext4_init_inode_table */
3427static int ext4_run_li_request(struct ext4_li_request *elr)
3428{
3429 struct ext4_group_desc *gdp = NULL;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003430 struct super_block *sb = elr->lr_super;
3431 ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3432 ext4_group_t group = elr->lr_next_group;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003433 unsigned long timeout = 0;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003434 unsigned int prefetch_ios = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003435 int ret = 0;
3436
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003437 if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
3438 elr->lr_next_group = ext4_mb_prefetch(sb, group,
3439 EXT4_SB(sb)->s_mb_prefetch, &prefetch_ios);
3440 if (prefetch_ios)
3441 ext4_mb_prefetch_fini(sb, elr->lr_next_group,
3442 prefetch_ios);
3443 trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group,
3444 prefetch_ios);
3445 if (group >= elr->lr_next_group) {
3446 ret = 1;
3447 if (elr->lr_first_not_zeroed != ngroups &&
3448 !sb_rdonly(sb) && test_opt(sb, INIT_INODE_TABLE)) {
3449 elr->lr_next_group = elr->lr_first_not_zeroed;
3450 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3451 ret = 0;
3452 }
3453 }
3454 return ret;
3455 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003456
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003457 for (; group < ngroups; group++) {
Lukas Czernerbfff6872010-10-27 21:30:05 -04003458 gdp = ext4_get_group_desc(sb, group, NULL);
3459 if (!gdp) {
3460 ret = 1;
3461 break;
3462 }
3463
3464 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3465 break;
3466 }
3467
Theodore Ts'o7f511862013-01-13 08:41:45 -05003468 if (group >= ngroups)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003469 ret = 1;
3470
3471 if (!ret) {
3472 timeout = jiffies;
3473 ret = ext4_init_inode_table(sb, group,
3474 elr->lr_timeout ? 0 : 1);
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003475 trace_ext4_lazy_itable_init(sb, group);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003476 if (elr->lr_timeout == 0) {
Lukas Czerner51ce6512011-05-20 13:55:16 -04003477 timeout = (jiffies - timeout) *
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003478 EXT4_SB(elr->lr_super)->s_li_wait_mult;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003479 elr->lr_timeout = timeout;
3480 }
3481 elr->lr_next_sched = jiffies + elr->lr_timeout;
3482 elr->lr_next_group = group + 1;
3483 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003484 return ret;
3485}
3486
3487/*
3488 * Remove lr_request from the list_request and free the
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003489 * request structure. Should be called with li_list_mtx held
Lukas Czernerbfff6872010-10-27 21:30:05 -04003490 */
3491static void ext4_remove_li_request(struct ext4_li_request *elr)
3492{
Lukas Czernerbfff6872010-10-27 21:30:05 -04003493 if (!elr)
3494 return;
3495
Lukas Czernerbfff6872010-10-27 21:30:05 -04003496 list_del(&elr->lr_request);
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003497 EXT4_SB(elr->lr_super)->s_li_request = NULL;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003498 kfree(elr);
3499}
3500
3501static void ext4_unregister_li_request(struct super_block *sb)
3502{
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003503 mutex_lock(&ext4_li_mtx);
3504 if (!ext4_li_info) {
3505 mutex_unlock(&ext4_li_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003506 return;
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003507 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003508
3509 mutex_lock(&ext4_li_info->li_list_mtx);
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003510 ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003511 mutex_unlock(&ext4_li_info->li_list_mtx);
Lukas Czerner1bb933f2011-05-20 13:55:29 -04003512 mutex_unlock(&ext4_li_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003513}
3514
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003515static struct task_struct *ext4_lazyinit_task;
3516
Lukas Czernerbfff6872010-10-27 21:30:05 -04003517/*
3518 * This is the function where ext4lazyinit thread lives. It walks
3519 * through the request list searching for next scheduled filesystem.
3520 * When such a fs is found, run the lazy initialization request
3521 * (ext4_rn_li_request) and keep track of the time spend in this
3522 * function. Based on that time we compute next schedule time of
3523 * the request. When walking through the list is complete, compute
3524 * next waking time and put itself into sleep.
3525 */
3526static int ext4_lazyinit_thread(void *arg)
3527{
3528 struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3529 struct list_head *pos, *n;
3530 struct ext4_li_request *elr;
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003531 unsigned long next_wakeup, cur;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003532
3533 BUG_ON(NULL == eli);
3534
Lukas Czernerbfff6872010-10-27 21:30:05 -04003535cont_thread:
3536 while (true) {
3537 next_wakeup = MAX_JIFFY_OFFSET;
3538
3539 mutex_lock(&eli->li_list_mtx);
3540 if (list_empty(&eli->li_request_list)) {
3541 mutex_unlock(&eli->li_list_mtx);
3542 goto exit_thread;
3543 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003544 list_for_each_safe(pos, n, &eli->li_request_list) {
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003545 int err = 0;
3546 int progress = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003547 elr = list_entry(pos, struct ext4_li_request,
3548 lr_request);
3549
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003550 if (time_before(jiffies, elr->lr_next_sched)) {
3551 if (time_before(elr->lr_next_sched, next_wakeup))
3552 next_wakeup = elr->lr_next_sched;
3553 continue;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003554 }
Dmitry Monakhove22834f2016-09-05 23:38:36 -04003555 if (down_read_trylock(&elr->lr_super->s_umount)) {
3556 if (sb_start_write_trylock(elr->lr_super)) {
3557 progress = 1;
3558 /*
3559 * We hold sb->s_umount, sb can not
3560 * be removed from the list, it is
3561 * now safe to drop li_list_mtx
3562 */
3563 mutex_unlock(&eli->li_list_mtx);
3564 err = ext4_run_li_request(elr);
3565 sb_end_write(elr->lr_super);
3566 mutex_lock(&eli->li_list_mtx);
3567 n = pos->next;
3568 }
3569 up_read((&elr->lr_super->s_umount));
3570 }
3571 /* error, remove the lazy_init job */
3572 if (err) {
3573 ext4_remove_li_request(elr);
3574 continue;
3575 }
3576 if (!progress) {
3577 elr->lr_next_sched = jiffies +
3578 (prandom_u32()
3579 % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
3580 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003581 if (time_before(elr->lr_next_sched, next_wakeup))
3582 next_wakeup = elr->lr_next_sched;
3583 }
3584 mutex_unlock(&eli->li_list_mtx);
3585
Tejun Heoa0acae02011-11-21 12:32:22 -08003586 try_to_freeze();
Lukas Czernerbfff6872010-10-27 21:30:05 -04003587
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003588 cur = jiffies;
3589 if ((time_after_eq(cur, next_wakeup)) ||
Lukas Czernerf4245bd2010-11-02 14:07:17 -04003590 (MAX_JIFFY_OFFSET == next_wakeup)) {
Lukas Czernerbfff6872010-10-27 21:30:05 -04003591 cond_resched();
3592 continue;
3593 }
3594
Lukas Czerner4ed5c032011-05-20 13:49:04 -04003595 schedule_timeout_interruptible(next_wakeup - cur);
3596
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003597 if (kthread_should_stop()) {
3598 ext4_clear_request_list();
3599 goto exit_thread;
3600 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003601 }
3602
3603exit_thread:
3604 /*
3605 * It looks like the request list is empty, but we need
3606 * to check it under the li_list_mtx lock, to prevent any
3607 * additions into it, and of course we should lock ext4_li_mtx
3608 * to atomically free the list and ext4_li_info, because at
3609 * this point another ext4 filesystem could be registering
3610 * new one.
3611 */
3612 mutex_lock(&ext4_li_mtx);
3613 mutex_lock(&eli->li_list_mtx);
3614 if (!list_empty(&eli->li_request_list)) {
3615 mutex_unlock(&eli->li_list_mtx);
3616 mutex_unlock(&ext4_li_mtx);
3617 goto cont_thread;
3618 }
3619 mutex_unlock(&eli->li_list_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003620 kfree(ext4_li_info);
3621 ext4_li_info = NULL;
3622 mutex_unlock(&ext4_li_mtx);
3623
3624 return 0;
3625}
3626
3627static void ext4_clear_request_list(void)
3628{
3629 struct list_head *pos, *n;
3630 struct ext4_li_request *elr;
3631
3632 mutex_lock(&ext4_li_info->li_list_mtx);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003633 list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3634 elr = list_entry(pos, struct ext4_li_request,
3635 lr_request);
3636 ext4_remove_li_request(elr);
3637 }
3638 mutex_unlock(&ext4_li_info->li_list_mtx);
3639}
3640
3641static int ext4_run_lazyinit_thread(void)
3642{
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003643 ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3644 ext4_li_info, "ext4lazyinit");
3645 if (IS_ERR(ext4_lazyinit_task)) {
3646 int err = PTR_ERR(ext4_lazyinit_task);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003647 ext4_clear_request_list();
Lukas Czernerbfff6872010-10-27 21:30:05 -04003648 kfree(ext4_li_info);
3649 ext4_li_info = NULL;
Theodore Ts'o92b97812012-03-19 23:41:49 -04003650 printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
Lukas Czernerbfff6872010-10-27 21:30:05 -04003651 "initialization thread\n",
3652 err);
3653 return err;
3654 }
3655 ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003656 return 0;
3657}
3658
3659/*
3660 * Check whether it make sense to run itable init. thread or not.
3661 * If there is at least one uninitialized inode table, return
3662 * corresponding group number, else the loop goes through all
3663 * groups and return total number of groups.
3664 */
3665static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3666{
3667 ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3668 struct ext4_group_desc *gdp = NULL;
3669
Theodore Ts'o88446182018-06-14 00:58:00 -04003670 if (!ext4_has_group_desc_csum(sb))
3671 return ngroups;
3672
Lukas Czernerbfff6872010-10-27 21:30:05 -04003673 for (group = 0; group < ngroups; group++) {
3674 gdp = ext4_get_group_desc(sb, group, NULL);
3675 if (!gdp)
3676 continue;
3677
Theodore Ts'o50122842018-07-28 08:12:04 -04003678 if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
Lukas Czernerbfff6872010-10-27 21:30:05 -04003679 break;
3680 }
3681
3682 return group;
3683}
3684
3685static int ext4_li_info_new(void)
3686{
3687 struct ext4_lazy_init *eli = NULL;
3688
3689 eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3690 if (!eli)
3691 return -ENOMEM;
3692
Lukas Czernerbfff6872010-10-27 21:30:05 -04003693 INIT_LIST_HEAD(&eli->li_request_list);
3694 mutex_init(&eli->li_list_mtx);
3695
Lukas Czernerbfff6872010-10-27 21:30:05 -04003696 eli->li_state |= EXT4_LAZYINIT_QUIT;
3697
3698 ext4_li_info = eli;
3699
3700 return 0;
3701}
3702
3703static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3704 ext4_group_t start)
3705{
Lukas Czernerbfff6872010-10-27 21:30:05 -04003706 struct ext4_li_request *elr;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003707
3708 elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3709 if (!elr)
3710 return NULL;
3711
3712 elr->lr_super = sb;
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003713 elr->lr_first_not_zeroed = start;
3714 if (test_opt(sb, PREFETCH_BLOCK_BITMAPS))
3715 elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
3716 else {
3717 elr->lr_mode = EXT4_LI_MODE_ITABLE;
3718 elr->lr_next_group = start;
3719 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003720
3721 /*
3722 * Randomize first schedule time of the request to
3723 * spread the inode table initialization requests
3724 * better.
3725 */
Theodore Ts'odd1f7232013-11-08 00:14:53 -05003726 elr->lr_next_sched = jiffies + (prandom_u32() %
3727 (EXT4_DEF_LI_MAX_START_DELAY * HZ));
Lukas Czernerbfff6872010-10-27 21:30:05 -04003728 return elr;
3729}
3730
Theodore Ts'o7f511862013-01-13 08:41:45 -05003731int ext4_register_li_request(struct super_block *sb,
3732 ext4_group_t first_not_zeroed)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003733{
3734 struct ext4_sb_info *sbi = EXT4_SB(sb);
Theodore Ts'o7f511862013-01-13 08:41:45 -05003735 struct ext4_li_request *elr = NULL;
Jun Piao49598e02018-01-11 13:17:49 -05003736 ext4_group_t ngroups = sbi->s_groups_count;
Andrew Morton6c5a6cb2011-01-10 12:30:17 -05003737 int ret = 0;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003738
Theodore Ts'o7f511862013-01-13 08:41:45 -05003739 mutex_lock(&ext4_li_mtx);
Lukas Czerner51ce6512011-05-20 13:55:16 -04003740 if (sbi->s_li_request != NULL) {
3741 /*
3742 * Reset timeout so it can be computed again, because
3743 * s_li_wait_mult might have changed.
3744 */
3745 sbi->s_li_request->lr_timeout = 0;
Theodore Ts'o7f511862013-01-13 08:41:45 -05003746 goto out;
Lukas Czerner51ce6512011-05-20 13:55:16 -04003747 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003748
Theodore Ts'o3d392b22020-07-17 00:14:40 -04003749 if (!test_opt(sb, PREFETCH_BLOCK_BITMAPS) &&
3750 (first_not_zeroed == ngroups || sb_rdonly(sb) ||
3751 !test_opt(sb, INIT_INODE_TABLE)))
Theodore Ts'o7f511862013-01-13 08:41:45 -05003752 goto out;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003753
3754 elr = ext4_li_request_new(sb, first_not_zeroed);
Theodore Ts'o7f511862013-01-13 08:41:45 -05003755 if (!elr) {
3756 ret = -ENOMEM;
3757 goto out;
3758 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003759
3760 if (NULL == ext4_li_info) {
3761 ret = ext4_li_info_new();
3762 if (ret)
3763 goto out;
3764 }
3765
3766 mutex_lock(&ext4_li_info->li_list_mtx);
3767 list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3768 mutex_unlock(&ext4_li_info->li_list_mtx);
3769
3770 sbi->s_li_request = elr;
Tao Ma46e46902011-04-04 16:00:49 -04003771 /*
3772 * set elr to NULL here since it has been inserted to
3773 * the request_list and the removal and free of it is
3774 * handled by ext4_clear_request_list from now on.
3775 */
3776 elr = NULL;
Lukas Czernerbfff6872010-10-27 21:30:05 -04003777
3778 if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3779 ret = ext4_run_lazyinit_thread();
3780 if (ret)
3781 goto out;
3782 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04003783out:
Nicolas Kaiserbeed5ecb2010-10-27 22:08:42 -04003784 mutex_unlock(&ext4_li_mtx);
3785 if (ret)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003786 kfree(elr);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003787 return ret;
3788}
3789
3790/*
3791 * We do not need to lock anything since this is called on
3792 * module unload.
3793 */
3794static void ext4_destroy_lazyinit_thread(void)
3795{
3796 /*
3797 * If thread exited earlier
3798 * there's nothing to be done.
3799 */
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003800 if (!ext4_li_info || !ext4_lazyinit_task)
Lukas Czernerbfff6872010-10-27 21:30:05 -04003801 return;
3802
Eric Sandeen8f1f7452011-02-03 14:33:15 -05003803 kthread_stop(ext4_lazyinit_task);
Lukas Czernerbfff6872010-10-27 21:30:05 -04003804}
3805
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003806static int set_journal_csum_feature_set(struct super_block *sb)
3807{
3808 int ret = 1;
3809 int compat, incompat;
3810 struct ext4_sb_info *sbi = EXT4_SB(sb);
3811
Dmitry Monakhov9aa5d32b2014-10-13 03:36:16 -04003812 if (ext4_has_metadata_csum(sb)) {
Darrick J. Wongdb9ee222014-08-27 18:40:07 -04003813 /* journal checksum v3 */
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003814 compat = 0;
Darrick J. Wongdb9ee222014-08-27 18:40:07 -04003815 incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003816 } else {
3817 /* journal checksum v1 */
3818 compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3819 incompat = 0;
3820 }
3821
Darrick J. Wongfeb8c6d2014-09-11 11:38:21 -04003822 jbd2_journal_clear_features(sbi->s_journal,
3823 JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3824 JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3825 JBD2_FEATURE_INCOMPAT_CSUM_V2);
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003826 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3827 ret = jbd2_journal_set_features(sbi->s_journal,
3828 compat, 0,
3829 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3830 incompat);
3831 } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3832 ret = jbd2_journal_set_features(sbi->s_journal,
3833 compat, 0,
3834 incompat);
3835 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3836 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3837 } else {
Darrick J. Wongfeb8c6d2014-09-11 11:38:21 -04003838 jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3839 JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04003840 }
3841
3842 return ret;
3843}
3844
Theodore Ts'o952fc182012-07-09 16:27:05 -04003845/*
3846 * Note: calculating the overhead so we can be compatible with
3847 * historical BSD practice is quite difficult in the face of
3848 * clusters/bigalloc. This is because multiple metadata blocks from
3849 * different block group can end up in the same allocation cluster.
3850 * Calculating the exact overhead in the face of clustered allocation
3851 * requires either O(all block bitmaps) in memory or O(number of block
3852 * groups**2) in time. We will still calculate the superblock for
3853 * older file systems --- and if we come across with a bigalloc file
3854 * system with zero in s_overhead_clusters the estimate will be close to
3855 * correct especially for very large cluster sizes --- but for newer
3856 * file systems, it's better to calculate this figure once at mkfs
3857 * time, and store it in the superblock. If the superblock value is
3858 * present (even for non-bigalloc file systems), we will use it.
3859 */
3860static int count_overhead(struct super_block *sb, ext4_group_t grp,
3861 char *buf)
3862{
3863 struct ext4_sb_info *sbi = EXT4_SB(sb);
3864 struct ext4_group_desc *gdp;
3865 ext4_fsblk_t first_block, last_block, b;
3866 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3867 int s, j, count = 0;
3868
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003869 if (!ext4_has_feature_bigalloc(sb))
Theodore Ts'o0548bbb2012-08-16 11:59:04 -04003870 return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3871 sbi->s_itb_per_group + 2);
3872
Theodore Ts'o952fc182012-07-09 16:27:05 -04003873 first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3874 (grp * EXT4_BLOCKS_PER_GROUP(sb));
3875 last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3876 for (i = 0; i < ngroups; i++) {
3877 gdp = ext4_get_group_desc(sb, i, NULL);
3878 b = ext4_block_bitmap(sb, gdp);
3879 if (b >= first_block && b <= last_block) {
3880 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3881 count++;
3882 }
3883 b = ext4_inode_bitmap(sb, gdp);
3884 if (b >= first_block && b <= last_block) {
3885 ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3886 count++;
3887 }
3888 b = ext4_inode_table(sb, gdp);
3889 if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3890 for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3891 int c = EXT4_B2C(sbi, b - first_block);
3892 ext4_set_bit(c, buf);
3893 count++;
3894 }
3895 if (i != grp)
3896 continue;
3897 s = 0;
3898 if (ext4_bg_has_super(sb, grp)) {
3899 ext4_set_bit(s++, buf);
3900 count++;
3901 }
Theodore Ts'oc48ae412016-11-18 13:37:47 -05003902 j = ext4_bg_num_gdb(sb, grp);
3903 if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3904 ext4_error(sb, "Invalid number of block group "
3905 "descriptor blocks: %d", j);
3906 j = EXT4_BLOCKS_PER_GROUP(sb) - s;
Theodore Ts'o952fc182012-07-09 16:27:05 -04003907 }
Theodore Ts'oc48ae412016-11-18 13:37:47 -05003908 count += j;
3909 for (; j > 0; j--)
3910 ext4_set_bit(EXT4_B2C(sbi, s++), buf);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003911 }
3912 if (!count)
3913 return 0;
3914 return EXT4_CLUSTERS_PER_GROUP(sb) -
3915 ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3916}
3917
3918/*
3919 * Compute the overhead and stash it in sbi->s_overhead
3920 */
3921int ext4_calculate_overhead(struct super_block *sb)
3922{
3923 struct ext4_sb_info *sbi = EXT4_SB(sb);
3924 struct ext4_super_block *es = sbi->s_es;
Eric Whitney3c816de2016-09-30 02:08:49 -04003925 struct inode *j_inode;
3926 unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003927 ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3928 ext4_fsblk_t overhead = 0;
Dmitry Monakhov4fdb5542014-11-25 13:08:04 -05003929 char *buf = (char *) get_zeroed_page(GFP_NOFS);
Theodore Ts'o952fc182012-07-09 16:27:05 -04003930
Theodore Ts'o952fc182012-07-09 16:27:05 -04003931 if (!buf)
3932 return -ENOMEM;
3933
3934 /*
3935 * Compute the overhead (FS structures). This is constant
3936 * for a given filesystem unless the number of block groups
3937 * changes so we cache the previous value until it does.
3938 */
3939
3940 /*
3941 * All of the blocks before first_data_block are overhead
3942 */
3943 overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3944
3945 /*
3946 * Add the overhead found in each block group
3947 */
3948 for (i = 0; i < ngroups; i++) {
3949 int blks;
3950
3951 blks = count_overhead(sb, i, buf);
3952 overhead += blks;
3953 if (blks)
3954 memset(buf, 0, PAGE_SIZE);
3955 cond_resched();
3956 }
Eric Whitney3c816de2016-09-30 02:08:49 -04003957
3958 /*
3959 * Add the internal journal blocks whether the journal has been
3960 * loaded or not
3961 */
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08003962 if (sbi->s_journal && !sbi->s_journal_bdev)
Harshad Shirwadkarede7dc72020-11-05 19:58:54 -08003963 overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
Ritesh Harjanif1eec3b2020-03-16 15:00:38 +05303964 else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
3965 /* j_inum for internal journal is non-zero */
Eric Whitney3c816de2016-09-30 02:08:49 -04003966 j_inode = ext4_get_journal_inode(sb, j_inum);
3967 if (j_inode) {
3968 j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
3969 overhead += EXT4_NUM_B2C(sbi, j_blocks);
3970 iput(j_inode);
3971 } else {
3972 ext4_msg(sb, KERN_ERR, "can't get journal size");
3973 }
3974 }
Theodore Ts'o952fc182012-07-09 16:27:05 -04003975 sbi->s_overhead = overhead;
3976 smp_wmb();
3977 free_page((unsigned long) buf);
3978 return 0;
3979}
3980
Theodore Ts'ob5799012015-09-23 12:44:17 -04003981static void ext4_set_resv_clusters(struct super_block *sb)
Lukas Czerner27dd4382013-04-09 22:11:22 -04003982{
3983 ext4_fsblk_t resv_clusters;
Theodore Ts'ob5799012015-09-23 12:44:17 -04003984 struct ext4_sb_info *sbi = EXT4_SB(sb);
Lukas Czerner27dd4382013-04-09 22:11:22 -04003985
3986 /*
Jan Kara30fac0f2013-12-08 21:11:59 -05003987 * There's no need to reserve anything when we aren't using extents.
3988 * The space estimates are exact, there are no unwritten extents,
3989 * hole punching doesn't need new metadata... This is needed especially
3990 * to keep ext2/3 backward compatibility.
3991 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04003992 if (!ext4_has_feature_extents(sb))
Theodore Ts'ob5799012015-09-23 12:44:17 -04003993 return;
Jan Kara30fac0f2013-12-08 21:11:59 -05003994 /*
Lukas Czerner27dd4382013-04-09 22:11:22 -04003995 * By default we reserve 2% or 4096 clusters, whichever is smaller.
3996 * This should cover the situations where we can not afford to run
3997 * out of space like for example punch hole, or converting
Lukas Czerner556615d2014-04-20 23:45:47 -04003998 * unwritten extents in delalloc path. In most cases such
Lukas Czerner27dd4382013-04-09 22:11:22 -04003999 * allocation would require 1, or 2 blocks, higher numbers are
4000 * very rare.
4001 */
Theodore Ts'ob5799012015-09-23 12:44:17 -04004002 resv_clusters = (ext4_blocks_count(sbi->s_es) >>
4003 sbi->s_cluster_bits);
Lukas Czerner27dd4382013-04-09 22:11:22 -04004004
4005 do_div(resv_clusters, 50);
4006 resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
4007
Theodore Ts'ob5799012015-09-23 12:44:17 -04004008 atomic64_set(&sbi->s_resv_clusters, resv_clusters);
Lukas Czerner27dd4382013-04-09 22:11:22 -04004009}
4010
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004011static int ext4_fill_super(struct super_block *sb, void *data, int silent)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004012{
Dan Williams5e405592017-08-24 16:42:48 -07004013 struct dax_device *dax_dev = fs_dax_get_by_bdev(sb->s_bdev);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04004014 char *orig_data = kstrdup(data, GFP_KERNEL);
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004015 struct buffer_head *bh, **group_desc;
Mingming Cao617ba132006-10-11 01:20:53 -07004016 struct ext4_super_block *es = NULL;
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004017 struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08004018 struct flex_groups **flex_groups;
Mingming Cao617ba132006-10-11 01:20:53 -07004019 ext4_fsblk_t block;
4020 ext4_fsblk_t sb_block = get_sb_block(&data);
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004021 ext4_fsblk_t logical_sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004022 unsigned long offset = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004023 unsigned long journal_devnum = 0;
4024 unsigned long def_mount_opts;
4025 struct inode *root;
Frank Mayhar03901312009-01-07 00:06:22 -05004026 const char *descr;
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04004027 int ret = -ENOMEM;
Theodore Ts'o281b5992011-09-09 18:34:51 -04004028 int blocksize, clustersize;
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004029 unsigned int db_count;
4030 unsigned int i;
Kaixu Xiaef5fd682020-04-15 15:25:42 +08004031 int needs_recovery, has_huge_files;
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07004032 __u64 blocks_count;
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004033 int err = 0;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05004034 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Lukas Czernerbfff6872010-10-27 21:30:05 -04004035 ext4_group_t first_not_zeroed;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004036
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004037 if ((data && !orig_data) || !sbi)
4038 goto out_free_base;
Pekka Enberg705895b2009-02-15 18:07:52 -05004039
Colin Ian Kingaed9eb12017-09-05 17:51:23 +01004040 sbi->s_daxdev = dax_dev;
Pekka Enberg705895b2009-02-15 18:07:52 -05004041 sbi->s_blockgroup_lock =
4042 kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004043 if (!sbi->s_blockgroup_lock)
4044 goto out_free_base;
4045
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004046 sb->s_fs_info = sbi;
Theodore Ts'o2c0544b2012-05-30 22:56:46 -04004047 sbi->s_sb = sb;
Theodore Ts'o240799c2008-10-09 23:53:47 -04004048 sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
Miklos Szeredid9c9bef12007-10-16 23:26:27 -07004049 sbi->s_sb_block = sb_block;
Theodore Ts'of613dfc2010-07-27 11:56:08 -04004050 if (sb->s_bdev->bd_part)
4051 sbi->s_sectors_written_start =
Michael Callahandbae2c52018-07-18 04:47:38 -07004052 part_stat_read(sb->s_bdev->bd_part, sectors[STAT_WRITE]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004053
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004054 /* Cleanup superblock name */
Rasmus Villemoesec3904d2015-06-25 15:02:41 -07004055 strreplace(sb->s_id, '/', '!');
Theodore Ts'o9f6200b2008-09-23 09:18:24 -04004056
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004057 /* -EINVAL is default */
Cyrill Gorcunovdcc7dae2010-07-27 11:56:07 -04004058 ret = -EINVAL;
Mingming Cao617ba132006-10-11 01:20:53 -07004059 blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004060 if (!blocksize) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004061 ext4_msg(sb, KERN_ERR, "unable to set blocksize");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004062 goto out_fail;
4063 }
4064
4065 /*
Mingming Cao617ba132006-10-11 01:20:53 -07004066 * The ext4 superblock will not be buffer aligned for other than 1kB
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004067 * block sizes. We need to calculate the offset from buffer start.
4068 */
Mingming Cao617ba132006-10-11 01:20:53 -07004069 if (blocksize != EXT4_MIN_BLOCK_SIZE) {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004070 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
4071 offset = do_div(logical_sb_block, blocksize);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004072 } else {
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004073 logical_sb_block = sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004074 }
4075
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004076 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
4077 if (IS_ERR(bh)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004078 ext4_msg(sb, KERN_ERR, "unable to read superblock");
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004079 ret = PTR_ERR(bh);
4080 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004081 goto out_fail;
4082 }
4083 /*
4084 * Note: s_es must be initialized as soon as possible because
Mingming Cao617ba132006-10-11 01:20:53 -07004085 * some ext4 macro-instructions depend on its value
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004086 */
Theodore Ts'o2716b802012-05-28 17:47:52 -04004087 es = (struct ext4_super_block *) (bh->b_data + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004088 sbi->s_es = es;
4089 sb->s_magic = le16_to_cpu(es->s_magic);
Mingming Cao617ba132006-10-11 01:20:53 -07004090 if (sb->s_magic != EXT4_SUPER_MAGIC)
4091 goto cantfind_ext4;
Theodore Ts'oafc32f72009-02-28 19:39:58 -05004092 sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004093
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004094 /* Warn if metadata_csum and gdt_csum are both set. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004095 if (ext4_has_feature_metadata_csum(sb) &&
4096 ext4_has_feature_gdt_csum(sb))
Jakub Wilk363307e2015-01-02 15:31:14 -05004097 ext4_warning(sb, "metadata_csum and uninit_bg are "
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04004098 "redundant flags; please run fsck.");
4099
Darrick J. Wongd25425f2012-04-29 18:25:10 -04004100 /* Check for a known checksum algorithm */
4101 if (!ext4_verify_csum_type(sb, es)) {
4102 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4103 "unknown checksum algorithm.");
4104 silent = 1;
4105 goto cantfind_ext4;
4106 }
4107
Darrick J. Wong04419842012-04-29 18:27:10 -04004108 /* Load the checksum driver */
Theodore Ts'oa45403b2018-03-29 22:10:31 -04004109 sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
4110 if (IS_ERR(sbi->s_chksum_driver)) {
4111 ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
4112 ret = PTR_ERR(sbi->s_chksum_driver);
4113 sbi->s_chksum_driver = NULL;
4114 goto failed_mount;
Darrick J. Wong04419842012-04-29 18:27:10 -04004115 }
4116
Darrick J. Wonga9c473172012-04-29 18:29:10 -04004117 /* Check superblock checksum */
4118 if (!ext4_superblock_csum_verify(sb, es)) {
4119 ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
4120 "invalid superblock checksum. Run e2fsck?");
4121 silent = 1;
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004122 ret = -EFSBADCRC;
Darrick J. Wonga9c473172012-04-29 18:29:10 -04004123 goto cantfind_ext4;
4124 }
4125
4126 /* Precompute checksum seed for all metadata */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004127 if (ext4_has_feature_csum_seed(sb))
Darrick J. Wong8c81bd82015-10-17 16:16:02 -04004128 sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004129 else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
Darrick J. Wonga9c473172012-04-29 18:29:10 -04004130 sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
4131 sizeof(es->s_uuid));
4132
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004133 /* Set defaults before we parse the mount options */
4134 def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004135 set_opt(sb, INIT_INODE_TABLE);
Mingming Cao617ba132006-10-11 01:20:53 -07004136 if (def_mount_opts & EXT4_DEFM_DEBUG)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004137 set_opt(sb, DEBUG);
Theodore Ts'o87f26802012-03-02 00:03:21 -05004138 if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004139 set_opt(sb, GRPID);
Mingming Cao617ba132006-10-11 01:20:53 -07004140 if (def_mount_opts & EXT4_DEFM_UID16)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004141 set_opt(sb, NO_UID32);
Eric Sandeenea663332011-02-23 17:51:51 -05004142 /* xattr user namespace & acls are now defaulted on */
Eric Sandeenea663332011-02-23 17:51:51 -05004143 set_opt(sb, XATTR_USER);
Theodore Ts'o03010a32008-10-10 20:02:48 -04004144#ifdef CONFIG_EXT4_FS_POSIX_ACL
Eric Sandeenea663332011-02-23 17:51:51 -05004145 set_opt(sb, POSIX_ACL);
Hugh Dickins2e7842b2007-02-10 01:46:13 -08004146#endif
Harshad Shirwadkar995a3ed2020-10-15 13:37:54 -07004147 if (ext4_has_feature_fast_commit(sb))
4148 set_opt2(sb, JOURNAL_FAST_COMMIT);
Darrick J. Wong98c1a752014-10-30 10:53:16 -04004149 /* don't forget to enable journal_csum when metadata_csum is enabled. */
4150 if (ext4_has_metadata_csum(sb))
4151 set_opt(sb, JOURNAL_CHECKSUM);
4152
Mingming Cao617ba132006-10-11 01:20:53 -07004153 if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004154 set_opt(sb, JOURNAL_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07004155 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004156 set_opt(sb, ORDERED_DATA);
Mingming Cao617ba132006-10-11 01:20:53 -07004157 else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004158 set_opt(sb, WRITEBACK_DATA);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004159
Mingming Cao617ba132006-10-11 01:20:53 -07004160 if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004161 set_opt(sb, ERRORS_PANIC);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05004162 else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004163 set_opt(sb, ERRORS_CONT);
Aneesh Kumar K.Vbb4f3972008-01-28 23:58:26 -05004164 else
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004165 set_opt(sb, ERRORS_RO);
Darrick J. Wong45f1a9c2014-09-01 21:34:09 -04004166 /* block_validity enabled by default; disable with noblock_validity */
4167 set_opt(sb, BLOCK_VALIDITY);
Theodore Ts'o8b67f042010-08-01 23:14:20 -04004168 if (def_mount_opts & EXT4_DEFM_DISCARD)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004169 set_opt(sb, DISCARD);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004170
Eric W. Biederman08cefc72012-02-07 15:41:49 -08004171 sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
4172 sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
Theodore Ts'o30773842009-01-03 20:27:38 -05004173 sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
4174 sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
4175 sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004176
Theodore Ts'o8b67f042010-08-01 23:14:20 -04004177 if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004178 set_opt(sb, BARRIER);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004179
Mingming Cao1e2462f2007-07-18 09:00:55 -04004180 /*
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04004181 * enable delayed allocation by default
4182 * Use -o nodelalloc to turn it off
4183 */
Theodore Ts'obc0b75f2012-09-17 22:54:36 -04004184 if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
Theodore Ts'o8b67f042010-08-01 23:14:20 -04004185 ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004186 set_opt(sb, DELALLOC);
Aneesh Kumar K.Vdd919b92008-07-11 19:27:31 -04004187
Lukas Czerner51ce6512011-05-20 13:55:16 -04004188 /*
4189 * set default s_li_wait_mult for lazyinit, for the case there is
4190 * no mount option specified.
4191 */
4192 sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
4193
Theodore Ts'o721972b2020-12-09 15:59:11 -05004194 if (le32_to_cpu(es->s_log_block_size) >
4195 (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4196 ext4_msg(sb, KERN_ERR,
4197 "Invalid log block size: %u",
4198 le32_to_cpu(es->s_log_block_size));
4199 goto failed_mount;
4200 }
4201 if (le32_to_cpu(es->s_log_cluster_size) >
4202 (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
4203 ext4_msg(sb, KERN_ERR,
4204 "Invalid log cluster size: %u",
4205 le32_to_cpu(es->s_log_cluster_size));
4206 goto failed_mount;
4207 }
4208
4209 blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
Ritesh Harjani626b0352020-03-28 01:37:44 +05304210
4211 if (blocksize == PAGE_SIZE)
4212 set_opt(sb, DIOREAD_NOLOCK);
4213
Theodore Ts'o98033872019-12-15 01:09:03 -05004214 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
4215 sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
4216 sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
4217 } else {
4218 sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
4219 sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
4220 if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
4221 ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
4222 sbi->s_first_ino);
4223 goto failed_mount;
4224 }
4225 if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
4226 (!is_power_of_2(sbi->s_inode_size)) ||
4227 (sbi->s_inode_size > blocksize)) {
4228 ext4_msg(sb, KERN_ERR,
4229 "unsupported inode size: %d",
4230 sbi->s_inode_size);
Theodore Ts'o4f97a682020-02-06 17:35:01 -05004231 ext4_msg(sb, KERN_ERR, "blocksize: %d", blocksize);
Theodore Ts'o98033872019-12-15 01:09:03 -05004232 goto failed_mount;
4233 }
4234 /*
4235 * i_atime_extra is the last extra field available for
4236 * [acm]times in struct ext4_inode. Checking for that
4237 * field should suffice to ensure we have extra space
4238 * for all three.
4239 */
4240 if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
4241 sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
4242 sb->s_time_gran = 1;
4243 sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
4244 } else {
4245 sb->s_time_gran = NSEC_PER_SEC;
4246 sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
4247 }
4248 sb->s_time_min = EXT4_TIMESTAMP_MIN;
4249 }
4250 if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4251 sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4252 EXT4_GOOD_OLD_INODE_SIZE;
4253 if (ext4_has_feature_extra_isize(sb)) {
4254 unsigned v, max = (sbi->s_inode_size -
4255 EXT4_GOOD_OLD_INODE_SIZE);
4256
4257 v = le16_to_cpu(es->s_want_extra_isize);
4258 if (v > max) {
4259 ext4_msg(sb, KERN_ERR,
4260 "bad s_want_extra_isize: %d", v);
4261 goto failed_mount;
4262 }
4263 if (sbi->s_want_extra_isize < v)
4264 sbi->s_want_extra_isize = v;
4265
4266 v = le16_to_cpu(es->s_min_extra_isize);
4267 if (v > max) {
4268 ext4_msg(sb, KERN_ERR,
4269 "bad s_min_extra_isize: %d", v);
4270 goto failed_mount;
4271 }
4272 if (sbi->s_want_extra_isize < v)
4273 sbi->s_want_extra_isize = v;
4274 }
4275 }
4276
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05004277 if (sbi->s_es->s_mount_opts[0]) {
4278 char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
4279 sizeof(sbi->s_es->s_mount_opts),
4280 GFP_KERNEL);
4281 if (!s_mount_opts)
4282 goto failed_mount;
4283 if (!parse_options(s_mount_opts, sb, &journal_devnum,
4284 &journal_ioprio, 0)) {
4285 ext4_msg(sb, KERN_WARNING,
4286 "failed to parse options in superblock: %s",
4287 s_mount_opts);
4288 }
4289 kfree(s_mount_opts);
Theodore Ts'o8b67f042010-08-01 23:14:20 -04004290 }
Theodore Ts'o5a916be2012-03-04 19:27:31 -05004291 sbi->s_def_mount_opt = sbi->s_mount_opt;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05004292 if (!parse_options((char *) data, sb, &journal_devnum,
Eric Sandeen661aa522012-02-20 17:53:04 -05004293 &journal_ioprio, 0))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004294 goto failed_mount;
4295
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04004296#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00004297 if (ext4_has_feature_casefold(sb) && !sb->s_encoding) {
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04004298 const struct ext4_sb_encodings *encoding_info;
4299 struct unicode_map *encoding;
4300 __u16 encoding_flags;
4301
4302 if (ext4_has_feature_encrypt(sb)) {
4303 ext4_msg(sb, KERN_ERR,
4304 "Can't mount with encoding and encryption");
4305 goto failed_mount;
4306 }
4307
4308 if (ext4_sb_read_encoding(es, &encoding_info,
4309 &encoding_flags)) {
4310 ext4_msg(sb, KERN_ERR,
4311 "Encoding requested by superblock is unknown");
4312 goto failed_mount;
4313 }
4314
4315 encoding = utf8_load(encoding_info->version);
4316 if (IS_ERR(encoding)) {
4317 ext4_msg(sb, KERN_ERR,
4318 "can't mount with superblock charset: %s-%s "
4319 "not supported by the kernel. flags: 0x%x.",
4320 encoding_info->name, encoding_info->version,
4321 encoding_flags);
4322 goto failed_mount;
4323 }
4324 ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
4325 "%s-%s with flags 0x%hx", encoding_info->name,
4326 encoding_info->version?:"\b", encoding_flags);
4327
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00004328 sb->s_encoding = encoding;
4329 sb->s_encoding_flags = encoding_flags;
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04004330 }
4331#endif
4332
Theodore Ts'o56889782011-09-03 18:22:38 -04004333 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
Harshad Shirwadkar556e0312020-11-05 19:59:07 -08004334 printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with data=journal disables delayed allocation, dioread_nolock, O_DIRECT and fast_commit support!\n");
Kaixu Xia781c0362020-04-13 12:24:22 +08004335 /* can't mount with both data=journal and dioread_nolock. */
Theodore Ts'o244adf62020-01-23 12:23:17 -05004336 clear_opt(sb, DIOREAD_NOLOCK);
Harshad Shirwadkar556e0312020-11-05 19:59:07 -08004337 clear_opt2(sb, JOURNAL_FAST_COMMIT);
Theodore Ts'o56889782011-09-03 18:22:38 -04004338 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4339 ext4_msg(sb, KERN_ERR, "can't mount with "
4340 "both data=journal and delalloc");
4341 goto failed_mount;
4342 }
Ira Weinyfc626fe2020-05-28 07:59:57 -07004343 if (test_opt(sb, DAX_ALWAYS)) {
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004344 ext4_msg(sb, KERN_ERR, "can't mount with "
4345 "both data=journal and dax");
4346 goto failed_mount;
4347 }
Sergey Karamov73b92a22016-12-10 17:54:58 -05004348 if (ext4_has_feature_encrypt(sb)) {
4349 ext4_msg(sb, KERN_WARNING,
4350 "encrypted files will use data=ordered "
4351 "instead of data journaling mode");
4352 }
Theodore Ts'o56889782011-09-03 18:22:38 -04004353 if (test_opt(sb, DELALLOC))
4354 clear_opt(sb, DELALLOC);
Tejun Heo001e4a82015-07-21 23:51:26 -04004355 } else {
4356 sb->s_iflags |= SB_I_CGROUPWB;
Theodore Ts'o56889782011-09-03 18:22:38 -04004357 }
4358
Linus Torvalds1751e8a2017-11-27 13:05:09 -08004359 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
4360 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004361
Mingming Cao617ba132006-10-11 01:20:53 -07004362 if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004363 (ext4_has_compat_features(sb) ||
4364 ext4_has_ro_compat_features(sb) ||
4365 ext4_has_incompat_features(sb)))
Eric Sandeenb31e1552009-06-04 17:36:36 -04004366 ext4_msg(sb, KERN_WARNING,
4367 "feature flags set on rev 0 fs, "
4368 "running e2fsck is recommended");
Theodore Tso469108f2008-02-10 01:11:44 -05004369
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004370 if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
4371 set_opt2(sb, HURD_COMPAT);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004372 if (ext4_has_feature_64bit(sb)) {
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004373 ext4_msg(sb, KERN_ERR,
4374 "The Hurd can't support 64-bit file systems");
4375 goto failed_mount;
4376 }
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004377
4378 /*
4379 * ea_inode feature uses l_i_version field which is not
4380 * available in HURD_COMPAT mode.
4381 */
4382 if (ext4_has_feature_ea_inode(sb)) {
4383 ext4_msg(sb, KERN_ERR,
4384 "ea_inode feature is not supported for Hurd");
4385 goto failed_mount;
4386 }
Theodore Ts'oed3654e2014-03-24 14:09:06 -04004387 }
4388
Theodore Ts'o2035e772011-04-18 17:29:14 -04004389 if (IS_EXT2_SB(sb)) {
4390 if (ext2_feature_set_ok(sb))
4391 ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
4392 "using the ext4 subsystem");
4393 else {
Eric Sandeen0d9366d2018-03-22 11:59:00 -04004394 /*
4395 * If we're probing be silent, if this looks like
4396 * it's actually an ext[34] filesystem.
4397 */
4398 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4399 goto failed_mount;
Theodore Ts'o2035e772011-04-18 17:29:14 -04004400 ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
4401 "to feature incompatibilities");
4402 goto failed_mount;
4403 }
4404 }
4405
4406 if (IS_EXT3_SB(sb)) {
4407 if (ext3_feature_set_ok(sb))
4408 ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
4409 "using the ext4 subsystem");
4410 else {
Eric Sandeen0d9366d2018-03-22 11:59:00 -04004411 /*
4412 * If we're probing be silent, if this looks like
4413 * it's actually an ext4 filesystem.
4414 */
4415 if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
4416 goto failed_mount;
Theodore Ts'o2035e772011-04-18 17:29:14 -04004417 ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
4418 "to feature incompatibilities");
4419 goto failed_mount;
4420 }
4421 }
4422
Theodore Tso469108f2008-02-10 01:11:44 -05004423 /*
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004424 * Check feature flags regardless of the revision level, since we
4425 * previously didn't change the revision level when setting the flags,
4426 * so there is a chance incompat flags are set on a rev 0 filesystem.
4427 */
David Howellsbc98a422017-07-17 08:45:34 +01004428 if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004429 goto failed_mount;
Eric Sandeena13fb1a2009-08-18 00:20:23 -04004430
Theodore Ts'o5b9554d2016-07-05 20:01:52 -04004431 if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
4432 ext4_msg(sb, KERN_ERR,
4433 "Number of reserved GDT blocks insanely large: %d",
4434 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
4435 goto failed_mount;
4436 }
4437
Ira Weinya8ab6d32020-05-28 07:59:58 -07004438 if (bdev_dax_supported(sb->s_bdev, blocksize))
4439 set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
4440
Ira Weinyfc626fe2020-05-28 07:59:57 -07004441 if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
Ross Zwisler559db4c2017-10-12 11:52:34 -04004442 if (ext4_has_feature_inline_data(sb)) {
4443 ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
4444 " that may contain inline data");
Eric Sandeen361d24d2018-12-04 00:46:39 -05004445 goto failed_mount;
Ross Zwisler559db4c2017-10-12 11:52:34 -04004446 }
Ira Weinya8ab6d32020-05-28 07:59:58 -07004447 if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
Dan Williams24f34782017-12-21 17:04:07 -08004448 ext4_msg(sb, KERN_ERR,
Eric Sandeen361d24d2018-12-04 00:46:39 -05004449 "DAX unsupported by block device.");
4450 goto failed_mount;
Dan Williams24f34782017-12-21 17:04:07 -08004451 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08004452 }
4453
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004454 if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04004455 ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
4456 es->s_encryption_level);
4457 goto failed_mount;
4458 }
4459
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004460 if (sb->s_blocksize != blocksize) {
Alexey Makhalov01d349a2021-05-21 07:55:33 +00004461 /*
4462 * bh must be released before kill_bdev(), otherwise
4463 * it won't be freed and its page also. kill_bdev()
4464 * is called by sb_set_blocksize().
4465 */
4466 brelse(bh);
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05004467 /* Validate the filesystem blocksize */
4468 if (!sb_set_blocksize(sb, blocksize)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004469 ext4_msg(sb, KERN_ERR, "bad block size %d",
Aneesh Kumar K.Vce407332008-01-28 23:58:27 -05004470 blocksize);
Alexey Makhalov01d349a2021-05-21 07:55:33 +00004471 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004472 goto failed_mount;
4473 }
4474
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004475 logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
4476 offset = do_div(logical_sb_block, blocksize);
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004477 bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
4478 if (IS_ERR(bh)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004479 ext4_msg(sb, KERN_ERR,
4480 "Can't read superblock on 2nd try");
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004481 ret = PTR_ERR(bh);
4482 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004483 goto failed_mount;
4484 }
Theodore Ts'o2716b802012-05-28 17:47:52 -04004485 es = (struct ext4_super_block *)(bh->b_data + offset);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004486 sbi->s_es = es;
Mingming Cao617ba132006-10-11 01:20:53 -07004487 if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004488 ext4_msg(sb, KERN_ERR,
4489 "Magic mismatch, very weird!");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004490 goto failed_mount;
4491 }
4492 }
4493
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004494 has_huge_files = ext4_has_feature_huge_file(sb);
Theodore Ts'of287a1a2008-10-16 22:50:48 -04004495 sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
4496 has_huge_files);
4497 sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004498
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004499 sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004500 if (ext4_has_feature_64bit(sb)) {
Alexandre Ratchov8fadc142006-10-11 01:21:15 -07004501 if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004502 sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
vignesh babud8ea6cf2007-10-16 23:27:14 -07004503 !is_power_of_2(sbi->s_desc_size)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004504 ext4_msg(sb, KERN_ERR,
4505 "unsupported descriptor size %lu",
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004506 sbi->s_desc_size);
4507 goto failed_mount;
4508 }
4509 } else
4510 sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004511
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004512 sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004513 sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004514
Mingming Cao617ba132006-10-11 01:20:53 -07004515 sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004516 if (sbi->s_inodes_per_block == 0)
Mingming Cao617ba132006-10-11 01:20:53 -07004517 goto cantfind_ext4;
Theodore Ts'ocd6bb352016-11-18 13:28:30 -05004518 if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
4519 sbi->s_inodes_per_group > blocksize * 8) {
4520 ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
Josh Triplettb9c538d2020-03-28 15:34:15 -07004521 sbi->s_inodes_per_group);
Theodore Ts'ocd6bb352016-11-18 13:28:30 -05004522 goto failed_mount;
4523 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004524 sbi->s_itb_per_group = sbi->s_inodes_per_group /
4525 sbi->s_inodes_per_block;
Alexandre Ratchov0d1ee422006-10-11 01:21:14 -07004526 sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004527 sbi->s_sbh = bh;
4528 sbi->s_mount_state = le16_to_cpu(es->s_state);
Fengguang Wue57aa832007-10-16 23:26:25 -07004529 sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
4530 sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004531
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04004532 for (i = 0; i < 4; i++)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004533 sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
4534 sbi->s_def_hash_version = es->s_def_hash_version;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004535 if (ext4_has_feature_dir_index(sb)) {
Theodore Ts'o23301412014-02-12 12:16:04 -05004536 i = le32_to_cpu(es->s_flags);
4537 if (i & EXT2_FLAGS_UNSIGNED_HASH)
4538 sbi->s_hash_unsigned = 3;
4539 else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
Theodore Ts'of99b2582008-10-28 13:21:44 -04004540#ifdef __CHAR_UNSIGNED__
David Howellsbc98a422017-07-17 08:45:34 +01004541 if (!sb_rdonly(sb))
Theodore Ts'o23301412014-02-12 12:16:04 -05004542 es->s_flags |=
4543 cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
4544 sbi->s_hash_unsigned = 3;
Theodore Ts'of99b2582008-10-28 13:21:44 -04004545#else
David Howellsbc98a422017-07-17 08:45:34 +01004546 if (!sb_rdonly(sb))
Theodore Ts'o23301412014-02-12 12:16:04 -05004547 es->s_flags |=
4548 cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
Theodore Ts'of99b2582008-10-28 13:21:44 -04004549#endif
Theodore Ts'o23301412014-02-12 12:16:04 -05004550 }
Theodore Ts'of99b2582008-10-28 13:21:44 -04004551 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004552
Theodore Ts'o281b5992011-09-09 18:34:51 -04004553 /* Handle clustersize */
4554 clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
Kaixu Xiaef5fd682020-04-15 15:25:42 +08004555 if (ext4_has_feature_bigalloc(sb)) {
Theodore Ts'o281b5992011-09-09 18:34:51 -04004556 if (clustersize < blocksize) {
4557 ext4_msg(sb, KERN_ERR,
4558 "cluster size (%d) smaller than "
4559 "block size (%d)", clustersize, blocksize);
4560 goto failed_mount;
4561 }
4562 sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
4563 le32_to_cpu(es->s_log_block_size);
4564 sbi->s_clusters_per_group =
4565 le32_to_cpu(es->s_clusters_per_group);
4566 if (sbi->s_clusters_per_group > blocksize * 8) {
4567 ext4_msg(sb, KERN_ERR,
4568 "#clusters per group too big: %lu",
4569 sbi->s_clusters_per_group);
4570 goto failed_mount;
4571 }
4572 if (sbi->s_blocks_per_group !=
4573 (sbi->s_clusters_per_group * (clustersize / blocksize))) {
4574 ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
4575 "clusters per group (%lu) inconsistent",
4576 sbi->s_blocks_per_group,
4577 sbi->s_clusters_per_group);
4578 goto failed_mount;
4579 }
4580 } else {
4581 if (clustersize != blocksize) {
Theodore Ts'obfe0a5f2018-06-17 18:11:20 -04004582 ext4_msg(sb, KERN_ERR,
4583 "fragment/cluster size (%d) != "
4584 "block size (%d)", clustersize, blocksize);
4585 goto failed_mount;
Theodore Ts'o281b5992011-09-09 18:34:51 -04004586 }
4587 if (sbi->s_blocks_per_group > blocksize * 8) {
4588 ext4_msg(sb, KERN_ERR,
4589 "#blocks per group too big: %lu",
4590 sbi->s_blocks_per_group);
4591 goto failed_mount;
4592 }
4593 sbi->s_clusters_per_group = sbi->s_blocks_per_group;
4594 sbi->s_cluster_bits = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004595 }
Theodore Ts'o281b5992011-09-09 18:34:51 -04004596 sbi->s_cluster_ratio = clustersize / blocksize;
4597
Theodore Ts'o960fd8562013-07-05 23:11:16 -04004598 /* Do we have standard group size of clustersize * 8 blocks ? */
4599 if (sbi->s_blocks_per_group == clustersize << 3)
4600 set_opt2(sb, STD_GROUP_SIZE);
4601
Eric Sandeenbf43d842009-08-17 23:48:51 -04004602 /*
4603 * Test whether we have more sectors than will fit in sector_t,
4604 * and whether the max offset is addressable by the page cache.
4605 */
Darrick J. Wong5a9ae682010-11-19 09:56:44 -05004606 err = generic_check_addressable(sb->s_blocksize_bits,
Patrick J. LoPresti30ca22c2010-07-22 15:03:41 -07004607 ext4_blocks_count(es));
Darrick J. Wong5a9ae682010-11-19 09:56:44 -05004608 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004609 ext4_msg(sb, KERN_ERR, "filesystem"
Eric Sandeenbf43d842009-08-17 23:48:51 -04004610 " too large to mount safely on this system");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004611 goto failed_mount;
4612 }
4613
Mingming Cao617ba132006-10-11 01:20:53 -07004614 if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
4615 goto cantfind_ext4;
Eric Sandeene7c95592008-01-28 23:58:27 -05004616
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04004617 /* check blocks count against device size */
4618 blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
4619 if (blocks_count && ext4_blocks_count(es) > blocks_count) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004620 ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
4621 "exceeds size of device (%llu blocks)",
From: Thiemo Nagel0f2ddca2009-04-07 14:07:47 -04004622 ext4_blocks_count(es), blocks_count);
4623 goto failed_mount;
4624 }
4625
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04004626 /*
4627 * It makes no sense for the first data block to be beyond the end
4628 * of the filesystem.
4629 */
4630 if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
Zheng Liu5635a622011-12-18 16:13:58 -05004631 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
Eric Sandeenb31e1552009-06-04 17:36:36 -04004632 "block %u is beyond end of filesystem (%llu)",
4633 le32_to_cpu(es->s_first_data_block),
4634 ext4_blocks_count(es));
Eric Sandeene7c95592008-01-28 23:58:27 -05004635 goto failed_mount;
4636 }
Theodore Ts'obfe0a5f2018-06-17 18:11:20 -04004637 if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
4638 (sbi->s_cluster_ratio == 1)) {
4639 ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
4640 "block is 0 with a 1k block and cluster size");
4641 goto failed_mount;
4642 }
4643
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07004644 blocks_count = (ext4_blocks_count(es) -
4645 le32_to_cpu(es->s_first_data_block) +
4646 EXT4_BLOCKS_PER_GROUP(sb) - 1);
4647 do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004648 if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
Josh Triplettdf414602020-03-28 14:54:01 -07004649 ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004650 "(block count %llu, first data block %u, "
Josh Triplettdf414602020-03-28 14:54:01 -07004651 "blocks per group %lu)", blocks_count,
Theodore Ts'o4ec11022009-01-06 14:53:26 -05004652 ext4_blocks_count(es),
4653 le32_to_cpu(es->s_first_data_block),
4654 EXT4_BLOCKS_PER_GROUP(sb));
4655 goto failed_mount;
4656 }
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07004657 sbi->s_groups_count = blocks_count;
Eric Sandeenfb0a3872009-09-16 14:45:10 -04004658 sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
4659 (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
Theodore Ts'o9e463082018-11-07 10:32:53 -05004660 if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
4661 le32_to_cpu(es->s_inodes_count)) {
4662 ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
4663 le32_to_cpu(es->s_inodes_count),
4664 ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
4665 ret = -EINVAL;
4666 goto failed_mount;
4667 }
Mingming Cao617ba132006-10-11 01:20:53 -07004668 db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
4669 EXT4_DESC_PER_BLOCK(sb);
Eryu Guan3a4b77c2016-12-01 15:08:37 -05004670 if (ext4_has_feature_meta_bg(sb)) {
Theodore Ts'o2ba3e6e2017-02-15 01:26:39 -05004671 if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
Eryu Guan3a4b77c2016-12-01 15:08:37 -05004672 ext4_msg(sb, KERN_WARNING,
4673 "first meta block group too large: %u "
4674 "(group descriptor block count %u)",
4675 le32_to_cpu(es->s_first_meta_bg), db_count);
4676 goto failed_mount;
4677 }
4678 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004679 rcu_assign_pointer(sbi->s_group_desc,
4680 kvmalloc_array(db_count,
4681 sizeof(struct buffer_head *),
4682 GFP_KERNEL));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004683 if (sbi->s_group_desc == NULL) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004684 ext4_msg(sb, KERN_ERR, "not enough memory");
Theodore Ts'o2cde4172012-05-28 17:49:54 -04004685 ret = -ENOMEM;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004686 goto failed_mount;
4687 }
4688
Pekka Enberg705895b2009-02-15 18:07:52 -05004689 bgl_lock_init(sbi->s_blockgroup_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004690
Andrew Perepechko85c8f172017-04-30 00:46:35 -04004691 /* Pre-read the descriptors into the buffer cache */
4692 for (i = 0; i < db_count; i++) {
4693 block = descriptor_loc(sb, logical_sb_block, i);
zhangyi (F)5df1d412020-09-24 15:33:35 +08004694 ext4_sb_breadahead_unmovable(sb, block);
Andrew Perepechko85c8f172017-04-30 00:46:35 -04004695 }
4696
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004697 for (i = 0; i < db_count; i++) {
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004698 struct buffer_head *bh;
4699
Andrew Morton70bbb3e2006-10-11 01:21:20 -07004700 block = descriptor_loc(sb, logical_sb_block, i);
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004701 bh = ext4_sb_bread_unmovable(sb, block);
4702 if (IS_ERR(bh)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004703 ext4_msg(sb, KERN_ERR,
4704 "can't read group descriptor %d", i);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004705 db_count = i;
zhangyi (F)8394a6a2020-09-24 15:33:37 +08004706 ret = PTR_ERR(bh);
4707 bh = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004708 goto failed_mount2;
4709 }
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05004710 rcu_read_lock();
4711 rcu_dereference(sbi->s_group_desc)[i] = bh;
4712 rcu_read_unlock();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004713 }
Theodore Ts'o44de0222018-07-08 19:35:02 -04004714 sbi->s_gdb_count = db_count;
Theodore Ts'o829fa702016-08-01 00:51:02 -04004715 if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004716 ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
Darrick J. Wong6a797d22015-10-17 16:16:04 -04004717 ret = -EFSCORRUPTED;
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004718 goto failed_mount2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004719 }
Jose R. Santos772cb7c2008-07-11 19:27:31 -04004720
Kees Cook235699a2017-10-18 12:45:17 -04004721 timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
Tao Ma04496412011-04-05 19:55:28 -04004722
Dmitry Monakhova75ae78f2013-04-03 22:10:52 -04004723 /* Register extent status tree shrinker */
Zheng Liueb68d0e2014-09-01 22:26:49 -04004724 if (ext4_es_register_shrinker(sbi))
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004725 goto failed_mount3;
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004726
Alex Tomasc9de5602008-01-29 00:19:52 -05004727 sbi->s_stripe = ext4_get_stripe_size(sbi);
Zheng Liu67a5da52012-08-17 09:54:17 -04004728 sbi->s_extent_max_zeroout_kb = 32;
Alex Tomasc9de5602008-01-29 00:19:52 -05004729
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004730 /*
4731 * set up enough so that it can read an inode
4732 */
Theodore Ts'of6e63f92014-09-18 17:12:30 -04004733 sb->s_op = &ext4_sops;
Mingming Cao617ba132006-10-11 01:20:53 -07004734 sb->s_export_op = &ext4_export_ops;
4735 sb->s_xattr = ext4_xattr_handlers;
Chandan Rajendra643fa962018-12-12 15:20:12 +05304736#ifdef CONFIG_FS_ENCRYPTION
Jaegeuk Kima7550b32016-07-10 14:01:03 -04004737 sb->s_cop = &ext4_cryptops;
Eric Biggersffcc41822017-10-09 12:15:38 -07004738#endif
Eric Biggersc93d8f82019-07-22 09:26:24 -07004739#ifdef CONFIG_FS_VERITY
4740 sb->s_vop = &ext4_verityops;
4741#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004742#ifdef CONFIG_QUOTA
Mingming Cao617ba132006-10-11 01:20:53 -07004743 sb->dq_op = &ext4_quota_operations;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004744 if (ext4_has_feature_quota(sb))
Jan Kara1fa5efe2014-10-08 18:26:54 +02004745 sb->s_qcop = &dquot_quotactl_sysfile_ops;
Jan Kara262b4662013-03-02 17:57:08 -05004746 else
4747 sb->s_qcop = &ext4_qctl_operations;
Li Xi689c9582016-01-08 16:01:22 -05004748 sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004749#endif
Christoph Hellwig85787092017-05-10 15:06:33 +02004750 memcpy(&sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
Aneesh Kumar K.Vf2fa2ff2011-01-29 18:43:40 +05304751
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004752 INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
Theodore Ts'o3b9d4ed2009-04-25 22:54:04 -04004753 mutex_init(&sbi->s_orphan_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004754
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004755 /* Initialize fast commit stuff */
4756 atomic_set(&sbi->s_fc_subtid, 0);
4757 atomic_set(&sbi->s_fc_ineligible_updates, 0);
4758 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
4759 INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
4760 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
4761 INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
4762 sbi->s_fc_bytes = 0;
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08004763 ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
4764 ext4_clear_mount_flag(sb, EXT4_MF_FC_COMMITTING);
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004765 spin_lock_init(&sbi->s_fc_lock);
4766 memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004767 sbi->s_fc_replay_state.fc_regions = NULL;
4768 sbi->s_fc_replay_state.fc_regions_size = 0;
4769 sbi->s_fc_replay_state.fc_regions_used = 0;
4770 sbi->s_fc_replay_state.fc_regions_valid = 0;
4771 sbi->s_fc_replay_state.fc_modified_inodes = NULL;
4772 sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
4773 sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07004774
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004775 sb->s_root = NULL;
4776
4777 needs_recovery = (es->s_last_orphan != 0 ||
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004778 ext4_has_feature_journal_needs_recovery(sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004779
David Howellsbc98a422017-07-17 08:45:34 +01004780 if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb))
Johann Lombardic5e06d12011-05-24 18:31:25 -04004781 if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
Darrick J. Wong50460fe2014-10-30 10:53:16 -04004782 goto failed_mount3a;
Johann Lombardic5e06d12011-05-24 18:31:25 -04004783
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004784 /*
4785 * The first inode we look at is the journal inode. Don't try
4786 * root first: it may be modified in the journal!
4787 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004788 if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
Theodore Ts'o4753d8a2017-02-05 01:26:48 -05004789 err = ext4_load_journal(sb, es, journal_devnum);
4790 if (err)
Darrick J. Wong50460fe2014-10-30 10:53:16 -04004791 goto failed_mount3a;
David Howellsbc98a422017-07-17 08:45:34 +01004792 } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004793 ext4_has_feature_journal_needs_recovery(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004794 ext4_msg(sb, KERN_ERR, "required journal recovery "
4795 "suppressed and not mounted read-only");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004796 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004797 } else {
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04004798 /* Nojournal mode, all journal mount options are illegal */
4799 if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
4800 ext4_msg(sb, KERN_ERR, "can't mount with "
4801 "journal_checksum, fs mounted w/o journal");
4802 goto failed_mount_wq;
4803 }
4804 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4805 ext4_msg(sb, KERN_ERR, "can't mount with "
4806 "journal_async_commit, fs mounted w/o journal");
4807 goto failed_mount_wq;
4808 }
4809 if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
4810 ext4_msg(sb, KERN_ERR, "can't mount with "
4811 "commit=%lu, fs mounted w/o journal",
4812 sbi->s_commit_interval / HZ);
4813 goto failed_mount_wq;
4814 }
4815 if (EXT4_MOUNT_DATA_FLAGS &
4816 (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
4817 ext4_msg(sb, KERN_ERR, "can't mount with "
4818 "data=, fs mounted w/o journal");
4819 goto failed_mount_wq;
4820 }
Debabrata Banerjee50b29d82019-04-30 23:08:15 -04004821 sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
Dmitry Monakhov1e381f62015-10-18 23:50:26 -04004822 clear_opt(sb, JOURNAL_CHECKSUM);
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004823 clear_opt(sb, DATA_FLAGS);
Harshad Shirwadkar995a3ed2020-10-15 13:37:54 -07004824 clear_opt2(sb, JOURNAL_FAST_COMMIT);
Frank Mayhar03901312009-01-07 00:06:22 -05004825 sbi->s_journal = NULL;
4826 needs_recovery = 0;
4827 goto no_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004828 }
4829
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004830 if (ext4_has_feature_64bit(sb) &&
Jose R. Santoseb40a092007-07-18 08:37:25 -04004831 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4832 JBD2_FEATURE_INCOMPAT_64BIT)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004833 ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004834 goto failed_mount_wq;
Jose R. Santoseb40a092007-07-18 08:37:25 -04004835 }
4836
Darrick J. Wong25ed6e82012-05-27 07:48:56 -04004837 if (!set_journal_csum_feature_set(sb)) {
4838 ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4839 "feature set");
4840 goto failed_mount_wq;
Linus Torvaldsd4da6c92009-11-02 10:15:27 -08004841 }
Girish Shilamkar818d2762008-01-28 23:58:27 -05004842
Harshad Shirwadkara1e5e4652020-11-05 19:58:55 -08004843 if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
4844 !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4845 JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
4846 ext4_msg(sb, KERN_ERR,
4847 "Failed to set fast commit journal feature");
4848 goto failed_mount_wq;
4849 }
4850
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004851 /* We have now updated the journal if required, so we can
4852 * validate the data journaling mode. */
4853 switch (test_opt(sb, DATA_FLAGS)) {
4854 case 0:
4855 /* No mode set, assume a default based on the journal
Andrew Morton63f57932006-10-11 01:21:24 -07004856 * capabilities: ORDERED_DATA if the journal can
4857 * cope, else JOURNAL_DATA
4858 */
Mingming Caodab291a2006-10-11 01:21:01 -07004859 if (jbd2_journal_check_available_features
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004860 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004861 set_opt(sb, ORDERED_DATA);
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004862 sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
4863 } else {
Theodore Ts'ofd8c37e2010-12-15 20:26:48 -05004864 set_opt(sb, JOURNAL_DATA);
Tyson Nottingham27f394a72018-03-30 00:56:10 -04004865 sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
4866 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004867 break;
4868
Mingming Cao617ba132006-10-11 01:20:53 -07004869 case EXT4_MOUNT_ORDERED_DATA:
4870 case EXT4_MOUNT_WRITEBACK_DATA:
Mingming Caodab291a2006-10-11 01:21:01 -07004871 if (!jbd2_journal_check_available_features
4872 (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004873 ext4_msg(sb, KERN_ERR, "Journal does not support "
4874 "requested data journaling mode");
Jiaying Zhang744692d2010-03-04 16:14:02 -05004875 goto failed_mount_wq;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004876 }
4877 default:
4878 break;
4879 }
Jan Karaab04df72016-12-03 16:20:53 -05004880
4881 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
4882 test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
4883 ext4_msg(sb, KERN_ERR, "can't mount with "
4884 "journal_async_commit in data=ordered mode");
4885 goto failed_mount_wq;
4886 }
4887
Theodore Ts'ob3881f72009-01-05 22:46:26 -05004888 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004889
Mauricio Faria de Oliveira342af942020-10-05 21:48:39 -03004890 sbi->s_journal->j_submit_inode_data_buffers =
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03004891 ext4_journal_submit_inode_data_buffers;
Mauricio Faria de Oliveira342af942020-10-05 21:48:39 -03004892 sbi->s_journal->j_finish_inode_data_buffers =
Mauricio Faria de Oliveiraafb585a2020-10-05 21:48:41 -03004893 ext4_journal_finish_inode_data_buffers;
Bobi Jam18aadd42012-02-20 17:53:02 -05004894
Theodore Ts'oce7e0102010-11-03 12:03:21 -04004895no_journal:
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004896 if (!test_opt(sb, NO_MBCACHE)) {
4897 sbi->s_ea_block_cache = ext4_xattr_create_cache();
4898 if (!sbi->s_ea_block_cache) {
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004899 ext4_msg(sb, KERN_ERR,
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004900 "Failed to create ea_block_cache");
Tahsin Erdogandec214d2017-06-22 11:44:55 -04004901 goto failed_mount_wq;
4902 }
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04004903
4904 if (ext4_has_feature_ea_inode(sb)) {
4905 sbi->s_ea_inode_cache = ext4_xattr_create_cache();
4906 if (!sbi->s_ea_inode_cache) {
4907 ext4_msg(sb, KERN_ERR,
4908 "Failed to create ea_inode_cache");
4909 goto failed_mount_wq;
4910 }
4911 }
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04004912 }
4913
Eric Biggersc93d8f82019-07-22 09:26:24 -07004914 if (ext4_has_feature_verity(sb) && blocksize != PAGE_SIZE) {
4915 ext4_msg(sb, KERN_ERR, "Unsupported blocksize for fs-verity");
4916 goto failed_mount_wq;
4917 }
4918
David Howellsbc98a422017-07-17 08:45:34 +01004919 if (DUMMY_ENCRYPTION_ENABLED(sbi) && !sb_rdonly(sb) &&
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04004920 !ext4_has_feature_encrypt(sb)) {
4921 ext4_set_feature_encrypt(sb);
Theodore Ts'o6ddb2442015-04-16 01:56:00 -04004922 ext4_commit_super(sb, 1);
4923 }
4924
Tejun Heofd89d5f2011-02-01 11:42:42 +01004925 /*
Theodore Ts'o952fc182012-07-09 16:27:05 -04004926 * Get the # of file system overhead blocks from the
4927 * superblock if present.
4928 */
4929 if (es->s_overhead_clusters)
4930 sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4931 else {
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004932 err = ext4_calculate_overhead(sb);
4933 if (err)
Theodore Ts'o952fc182012-07-09 16:27:05 -04004934 goto failed_mount_wq;
4935 }
4936
4937 /*
Tejun Heofd89d5f2011-02-01 11:42:42 +01004938 * The maximum number of concurrent works can be high and
4939 * concurrency isn't really necessary. Limit it to 1.
4940 */
Jan Kara2e8fa542013-06-04 14:21:02 -04004941 EXT4_SB(sb)->rsv_conversion_wq =
4942 alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4943 if (!EXT4_SB(sb)->rsv_conversion_wq) {
4944 printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05004945 ret = -ENOMEM;
Jan Kara2e8fa542013-06-04 14:21:02 -04004946 goto failed_mount4;
4947 }
4948
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004949 /*
Mingming Caodab291a2006-10-11 01:21:01 -07004950 * The jbd2_journal_load will have done any necessary log recovery,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004951 * so we can safely mount the rest of the filesystem now.
4952 */
4953
Theodore Ts'o8a363972018-12-19 12:29:13 -05004954 root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08004955 if (IS_ERR(root)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004956 ext4_msg(sb, KERN_ERR, "get root inode failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08004957 ret = PTR_ERR(root);
Manish Katiyar32a9bb52011-02-27 20:42:06 -05004958 root = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004959 goto failed_mount4;
4960 }
4961 if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004962 ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
Al Viro94bf6082012-01-09 15:53:24 -05004963 iput(root);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004964 goto failed_mount4;
4965 }
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004966
4967#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00004968 if (sb->s_encoding)
Gabriel Krisman Bertazib886ee32019-04-25 14:12:08 -04004969 sb->s_d_op = &ext4_dentry_ops;
4970#endif
4971
Al Viro48fde702012-01-08 22:15:13 -05004972 sb->s_root = d_make_root(root);
David Howells1d1fe1e2008-02-07 00:15:37 -08004973 if (!sb->s_root) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04004974 ext4_msg(sb, KERN_ERR, "get root dentry failed");
David Howells1d1fe1e2008-02-07 00:15:37 -08004975 ret = -ENOMEM;
4976 goto failed_mount4;
4977 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07004978
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04004979 ret = ext4_setup_super(sb, es, sb_rdonly(sb));
4980 if (ret == -EROFS) {
Linus Torvalds1751e8a2017-11-27 13:05:09 -08004981 sb->s_flags |= SB_RDONLY;
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04004982 ret = 0;
4983 } else if (ret)
4984 goto failed_mount4a;
Kalpak Shahef7f3832007-07-18 09:15:20 -04004985
Theodore Ts'ob5799012015-09-23 12:44:17 -04004986 ext4_set_resv_clusters(sb);
Lukas Czerner27dd4382013-04-09 22:11:22 -04004987
Jan Kara0f5bde12020-07-28 15:04:37 +02004988 if (test_opt(sb, BLOCK_VALIDITY)) {
4989 err = ext4_setup_system_zone(sb);
4990 if (err) {
4991 ext4_msg(sb, KERN_ERR, "failed to initialize system "
4992 "zone (%d)", err);
4993 goto failed_mount4a;
4994 }
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004995 }
Harshad Shirwadkar8016e292020-10-15 13:37:59 -07004996 ext4_fc_replay_cleanup(sb);
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04004997
4998 ext4_ext_init(sb);
4999 err = ext4_mb_init(sb);
5000 if (err) {
5001 ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
5002 err);
Tao Madcf2d802011-10-06 12:10:11 -04005003 goto failed_mount5;
Aneesh Kumar K.Vc2774d82008-10-10 20:07:20 -04005004 }
5005
Theodore Ts'o64578f92021-01-21 12:33:20 -05005006 /*
5007 * We can only set up the journal commit callback once
5008 * mballoc is initialized
5009 */
5010 if (sbi->s_journal)
5011 sbi->s_journal->j_commit_callback =
5012 ext4_journal_commit_callback;
5013
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005014 block = ext4_count_free_clusters(sb);
5015 ext4_free_blocks_count_set(sbi->s_es,
5016 EXT4_C2B(sbi, block));
Theodore Ts'o4274f512018-09-01 14:42:14 -04005017 ext4_superblock_csum_set(sb);
Tejun Heo908c7f12014-09-08 09:51:29 +09005018 err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
5019 GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005020 if (!err) {
5021 unsigned long freei = ext4_count_free_inodes(sb);
5022 sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
Theodore Ts'o4274f512018-09-01 14:42:14 -04005023 ext4_superblock_csum_set(sb);
Tejun Heo908c7f12014-09-08 09:51:29 +09005024 err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
5025 GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005026 }
5027 if (!err)
5028 err = percpu_counter_init(&sbi->s_dirs_counter,
Tejun Heo908c7f12014-09-08 09:51:29 +09005029 ext4_count_dirs(sb), GFP_KERNEL);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005030 if (!err)
Tejun Heo908c7f12014-09-08 09:51:29 +09005031 err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
5032 GFP_KERNEL);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005033 if (!err)
Eric Whitney4b313952021-02-18 10:11:32 -05005034 err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
5035 GFP_KERNEL);
5036 if (!err)
Eric Biggersbbd55932020-02-19 10:30:46 -08005037 err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
Daeho Jeongc8585c62016-04-25 23:22:35 -04005038
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005039 if (err) {
5040 ext4_msg(sb, KERN_ERR, "insufficient memory");
5041 goto failed_mount6;
5042 }
5043
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005044 if (ext4_has_feature_flex_bg(sb))
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005045 if (!ext4_fill_flex_info(sb)) {
5046 ext4_msg(sb, KERN_ERR,
5047 "unable to initialize "
5048 "flex_bg meta info!");
Yang Yingliangf4e91a42021-05-10 19:10:51 +08005049 ret = -ENOMEM;
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005050 goto failed_mount6;
5051 }
5052
Lukas Czernerbfff6872010-10-27 21:30:05 -04005053 err = ext4_register_li_request(sb, first_not_zeroed);
5054 if (err)
Tao Madcf2d802011-10-06 12:10:11 -04005055 goto failed_mount6;
Lukas Czernerbfff6872010-10-27 21:30:05 -04005056
Theodore Ts'ob5799012015-09-23 12:44:17 -04005057 err = ext4_register_sysfs(sb);
Tao Madcf2d802011-10-06 12:10:11 -04005058 if (err)
5059 goto failed_mount7;
Theodore Ts'o3197ebd2009-03-31 09:10:09 -04005060
Jan Kara9b2ff3572013-03-02 18:22:38 -05005061#ifdef CONFIG_QUOTA
5062 /* Enable quota usage during mount. */
David Howellsbc98a422017-07-17 08:45:34 +01005063 if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
Jan Kara9b2ff3572013-03-02 18:22:38 -05005064 err = ext4_enable_quotas(sb);
5065 if (err)
5066 goto failed_mount8;
5067 }
5068#endif /* CONFIG_QUOTA */
5069
zhangyi (F)bc717262020-06-20 10:54:23 +08005070 /*
5071 * Save the original bdev mapping's wb_err value which could be
5072 * used to detect the metadata async write error.
5073 */
5074 spin_lock_init(&sbi->s_bdev_wb_lock);
Zhang Xiaoxu9704a322020-09-27 22:05:56 -04005075 errseq_check_and_advance(&sb->s_bdev->bd_inode->i_mapping->wb_err,
5076 &sbi->s_bdev_wb_err);
zhangyi (F)bc717262020-06-20 10:54:23 +08005077 sb->s_bdev->bd_super = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07005078 EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
5079 ext4_orphan_cleanup(sb, es);
5080 EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
Frank Mayhar03901312009-01-07 00:06:22 -05005081 if (needs_recovery) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005082 ext4_msg(sb, KERN_INFO, "recovery complete");
Jan Kara11215632020-07-10 16:07:59 +02005083 err = ext4_mark_recovery_complete(sb, es);
5084 if (err)
5085 goto failed_mount8;
Frank Mayhar03901312009-01-07 00:06:22 -05005086 }
5087 if (EXT4_SB(sb)->s_journal) {
5088 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
5089 descr = " journalled data mode";
5090 else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
5091 descr = " ordered data mode";
5092 else
5093 descr = " writeback data mode";
5094 } else
5095 descr = "out journal";
5096
Lukas Czerner79add3a2012-11-08 13:28:29 -05005097 if (test_opt(sb, DISCARD)) {
5098 struct request_queue *q = bdev_get_queue(sb->s_bdev);
5099 if (!blk_queue_discard(q))
5100 ext4_msg(sb, KERN_WARNING,
5101 "mounting with \"discard\" option, but "
5102 "the device does not support discard");
5103 }
5104
Theodore Ts'oe294a532015-08-15 14:59:44 -04005105 if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
5106 ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05005107 "Opts: %.*s%s%s", descr,
5108 (int) sizeof(sbi->s_es->s_mount_opts),
5109 sbi->s_es->s_mount_opts,
Theodore Ts'oe294a532015-08-15 14:59:44 -04005110 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005111
Theodore Ts'o66e61a92010-07-27 11:56:04 -04005112 if (es->s_error_count)
5113 mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005114
Theodore Ts'oefbed4d2013-10-17 21:11:01 -04005115 /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
5116 ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
5117 ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
5118 ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
Dmitry Monakhov1cf006e2020-07-25 12:33:13 +00005119 atomic_set(&sbi->s_warning_count, 0);
5120 atomic_set(&sbi->s_msg_count, 0);
Theodore Ts'oefbed4d2013-10-17 21:11:01 -04005121
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005122 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005123 return 0;
5124
Mingming Cao617ba132006-10-11 01:20:53 -07005125cantfind_ext4:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005126 if (!silent)
Eric Sandeenb31e1552009-06-04 17:36:36 -04005127 ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005128 goto failed_mount;
5129
Theodore Ts'o72ba7452013-01-24 23:24:54 -05005130failed_mount8:
Theodore Ts'oebd173b2015-09-23 12:46:17 -04005131 ext4_unregister_sysfs(sb);
Eric Biggerscb8d53d2020-09-22 09:24:56 -07005132 kobject_put(&sbi->s_kobj);
Tao Madcf2d802011-10-06 12:10:11 -04005133failed_mount7:
5134 ext4_unregister_li_request(sb);
5135failed_mount6:
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04005136 ext4_mb_release(sb);
Suraj Jitindar Singh7c990722020-02-18 19:08:51 -08005137 rcu_read_lock();
5138 flex_groups = rcu_dereference(sbi->s_flex_groups);
5139 if (flex_groups) {
5140 for (i = 0; i < sbi->s_flex_groups_allocated; i++)
5141 kvfree(flex_groups[i]);
5142 kvfree(flex_groups);
5143 }
5144 rcu_read_unlock();
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005145 percpu_counter_destroy(&sbi->s_freeclusters_counter);
5146 percpu_counter_destroy(&sbi->s_freeinodes_counter);
5147 percpu_counter_destroy(&sbi->s_dirs_counter);
5148 percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
Eric Whitney4b313952021-02-18 10:11:32 -05005149 percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
Eric Biggersbbd55932020-02-19 10:30:46 -08005150 percpu_free_rwsem(&sbi->s_writepages_rwsem);
Azat Khuzhin00764932014-04-07 10:54:20 -04005151failed_mount5:
Theodore Ts'of9ae9cf2014-07-11 13:55:40 -04005152 ext4_ext_release(sb);
5153 ext4_release_system_zone(sb);
5154failed_mount4a:
Al Viro94bf6082012-01-09 15:53:24 -05005155 dput(sb->s_root);
Manish Katiyar32a9bb52011-02-27 20:42:06 -05005156 sb->s_root = NULL;
Al Viro94bf6082012-01-09 15:53:24 -05005157failed_mount4:
Eric Sandeenb31e1552009-06-04 17:36:36 -04005158 ext4_msg(sb, KERN_ERR, "mount failed");
Jan Kara2e8fa542013-06-04 14:21:02 -04005159 if (EXT4_SB(sb)->rsv_conversion_wq)
5160 destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
Mingming Cao4c0425f2009-09-28 15:48:41 -04005161failed_mount_wq:
Chengguang Xu50c15df2018-12-04 00:24:42 -05005162 ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
5163 sbi->s_ea_inode_cache = NULL;
5164
5165 ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
5166 sbi->s_ea_block_cache = NULL;
5167
Frank Mayhar03901312009-01-07 00:06:22 -05005168 if (sbi->s_journal) {
5169 jbd2_journal_destroy(sbi->s_journal);
5170 sbi->s_journal = NULL;
5171 }
Darrick J. Wong50460fe2014-10-30 10:53:16 -04005172failed_mount3a:
Zheng Liud3922a72013-07-01 08:12:37 -04005173 ext4_es_unregister_shrinker(sbi);
Zheng Liueb68d0e2014-09-01 22:26:49 -04005174failed_mount3:
Al Viro9105bb12013-12-08 20:52:31 -05005175 del_timer_sync(&sbi->s_err_report);
Pavel Skripkin5e4f5132021-04-30 21:50:46 +03005176 ext4_stop_mmpd(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005177failed_mount2:
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05005178 rcu_read_lock();
5179 group_desc = rcu_dereference(sbi->s_group_desc);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005180 for (i = 0; i < db_count; i++)
Theodore Ts'o1d0c3922020-02-15 16:40:37 -05005181 brelse(group_desc[i]);
5182 kvfree(group_desc);
5183 rcu_read_unlock();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005184failed_mount:
Darrick J. Wong04419842012-04-29 18:27:10 -04005185 if (sbi->s_chksum_driver)
5186 crypto_free_shash(sbi->s_chksum_driver);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04005187
5188#ifdef CONFIG_UNICODE
Daniel Rosenbergf8f4acb2020-10-28 05:08:20 +00005189 utf8_unload(sb->s_encoding);
Gabriel Krisman Bertazic83ad552019-04-25 14:05:42 -04005190#endif
5191
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005192#ifdef CONFIG_QUOTA
Jan Karaa2d4a642014-09-11 11:15:15 -04005193 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Theodore Ts'o0ba33fa2019-05-12 04:49:47 -04005194 kfree(get_qf_name(sb, sbi, i));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005195#endif
Eric Biggersac4acb12020-09-16 21:11:35 -07005196 fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
Alexey Makhalov01d349a2021-05-21 07:55:33 +00005197 /* ext4_blkdev_remove() calls kill_bdev(), release bh before it. */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005198 brelse(bh);
Alexey Makhalov01d349a2021-05-21 07:55:33 +00005199 ext4_blkdev_remove(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005200out_fail:
5201 sb->s_fs_info = NULL;
Manish Katiyarf6830162009-05-17 23:52:44 -04005202 kfree(sbi->s_blockgroup_lock);
Theodore Ts'o5aee0f82016-11-18 13:24:26 -05005203out_free_base:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005204 kfree(sbi);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005205 kfree(orig_data);
Dan Williams5e405592017-08-24 16:42:48 -07005206 fs_put_dax(dax_dev);
Lukas Czerner07aa2ea2012-11-08 15:16:54 -05005207 return err ? err : ret;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005208}
5209
5210/*
5211 * Setup any per-fs journal parameters now. We'll do this both on
5212 * initial mount, once the journal has been initialised but before we've
5213 * done any recovery; and again on any subsequent remount.
5214 */
Mingming Cao617ba132006-10-11 01:20:53 -07005215static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005216{
Mingming Cao617ba132006-10-11 01:20:53 -07005217 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005218
Theodore Ts'o30773842009-01-03 20:27:38 -05005219 journal->j_commit_interval = sbi->s_commit_interval;
5220 journal->j_min_batch_time = sbi->s_min_batch_time;
5221 journal->j_max_batch_time = sbi->s_max_batch_time;
Harshad Shirwadkar6866d7b2020-10-15 13:37:55 -07005222 ext4_fc_init(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005223
Theodore Ts'oa931da62010-08-03 21:35:12 -04005224 write_lock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005225 if (test_opt(sb, BARRIER))
Mingming Caodab291a2006-10-11 01:21:01 -07005226 journal->j_flags |= JBD2_BARRIER;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005227 else
Mingming Caodab291a2006-10-11 01:21:01 -07005228 journal->j_flags &= ~JBD2_BARRIER;
Hidehiro Kawai5bf56832008-10-10 22:12:43 -04005229 if (test_opt(sb, DATA_ERR_ABORT))
5230 journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
5231 else
5232 journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
Theodore Ts'oa931da62010-08-03 21:35:12 -04005233 write_unlock(&journal->j_state_lock);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005234}
5235
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005236static struct inode *ext4_get_journal_inode(struct super_block *sb,
5237 unsigned int journal_inum)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005238{
5239 struct inode *journal_inode;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005240
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005241 /*
5242 * Test for the existence of a valid inode on disk. Bad things
5243 * happen if we iget() an unused inode, as the subsequent iput()
5244 * will try to delete it.
5245 */
Theodore Ts'o8a363972018-12-19 12:29:13 -05005246 journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
David Howells1d1fe1e2008-02-07 00:15:37 -08005247 if (IS_ERR(journal_inode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005248 ext4_msg(sb, KERN_ERR, "no journal found");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005249 return NULL;
5250 }
5251 if (!journal_inode->i_nlink) {
5252 make_bad_inode(journal_inode);
5253 iput(journal_inode);
Eric Sandeenb31e1552009-06-04 17:36:36 -04005254 ext4_msg(sb, KERN_ERR, "journal inode is deleted");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005255 return NULL;
5256 }
5257
Theodore Ts'oe5f8eab82008-09-08 22:25:04 -04005258 jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005259 journal_inode, journal_inode->i_size);
David Howells1d1fe1e2008-02-07 00:15:37 -08005260 if (!S_ISREG(journal_inode->i_mode)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005261 ext4_msg(sb, KERN_ERR, "invalid journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005262 iput(journal_inode);
5263 return NULL;
5264 }
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005265 return journal_inode;
5266}
5267
5268static journal_t *ext4_get_journal(struct super_block *sb,
5269 unsigned int journal_inum)
5270{
5271 struct inode *journal_inode;
5272 journal_t *journal;
5273
Jan Kara11215632020-07-10 16:07:59 +02005274 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5275 return NULL;
Eric Whitneyc6cb7e72016-09-30 02:05:09 -04005276
5277 journal_inode = ext4_get_journal_inode(sb, journal_inum);
5278 if (!journal_inode)
5279 return NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005280
Mingming Caodab291a2006-10-11 01:21:01 -07005281 journal = jbd2_journal_init_inode(journal_inode);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005282 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005283 ext4_msg(sb, KERN_ERR, "Could not load journal inode");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005284 iput(journal_inode);
5285 return NULL;
5286 }
5287 journal->j_private = sb;
Mingming Cao617ba132006-10-11 01:20:53 -07005288 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005289 return journal;
5290}
5291
Mingming Cao617ba132006-10-11 01:20:53 -07005292static journal_t *ext4_get_dev_journal(struct super_block *sb,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005293 dev_t j_dev)
5294{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005295 struct buffer_head *bh;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005296 journal_t *journal;
Mingming Cao617ba132006-10-11 01:20:53 -07005297 ext4_fsblk_t start;
5298 ext4_fsblk_t len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005299 int hblock, blocksize;
Mingming Cao617ba132006-10-11 01:20:53 -07005300 ext4_fsblk_t sb_block;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005301 unsigned long offset;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005302 struct ext4_super_block *es;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005303 struct block_device *bdev;
5304
Jan Kara11215632020-07-10 16:07:59 +02005305 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5306 return NULL;
Frank Mayhar03901312009-01-07 00:06:22 -05005307
Eric Sandeenb31e1552009-06-04 17:36:36 -04005308 bdev = ext4_blkdev_get(j_dev, sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005309 if (bdev == NULL)
5310 return NULL;
5311
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005312 blocksize = sb->s_blocksize;
Martin K. Petersene1defc42009-05-22 17:17:49 -04005313 hblock = bdev_logical_block_size(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005314 if (blocksize < hblock) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005315 ext4_msg(sb, KERN_ERR,
5316 "blocksize too small for journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005317 goto out_bdev;
5318 }
5319
Mingming Cao617ba132006-10-11 01:20:53 -07005320 sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
5321 offset = EXT4_MIN_BLOCK_SIZE % blocksize;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005322 set_blocksize(bdev, blocksize);
5323 if (!(bh = __bread(bdev, sb_block, blocksize))) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005324 ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
5325 "external journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005326 goto out_bdev;
5327 }
5328
Theodore Ts'o2716b802012-05-28 17:47:52 -04005329 es = (struct ext4_super_block *) (bh->b_data + offset);
Mingming Cao617ba132006-10-11 01:20:53 -07005330 if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005331 !(le32_to_cpu(es->s_feature_incompat) &
Mingming Cao617ba132006-10-11 01:20:53 -07005332 EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005333 ext4_msg(sb, KERN_ERR, "external journal has "
5334 "bad superblock");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005335 brelse(bh);
5336 goto out_bdev;
5337 }
5338
Darrick J. Wongdf4763b2014-09-11 11:44:36 -04005339 if ((le32_to_cpu(es->s_feature_ro_compat) &
5340 EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
5341 es->s_checksum != ext4_superblock_csum(sb, es)) {
5342 ext4_msg(sb, KERN_ERR, "external journal has "
5343 "corrupt superblock");
5344 brelse(bh);
5345 goto out_bdev;
5346 }
5347
Mingming Cao617ba132006-10-11 01:20:53 -07005348 if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005349 ext4_msg(sb, KERN_ERR, "journal UUID does not match");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005350 brelse(bh);
5351 goto out_bdev;
5352 }
5353
Laurent Vivierbd81d8e2006-10-11 01:21:10 -07005354 len = ext4_blocks_count(es);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005355 start = sb_block + 1;
5356 brelse(bh); /* we're done with the superblock */
5357
Mingming Caodab291a2006-10-11 01:21:01 -07005358 journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005359 start, len, blocksize);
5360 if (!journal) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005361 ext4_msg(sb, KERN_ERR, "failed to create device journal");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005362 goto out_bdev;
5363 }
5364 journal->j_private = sb;
zhangyi (F)2d069c02020-09-24 15:33:33 +08005365 if (ext4_read_bh_lock(journal->j_sb_buffer, REQ_META | REQ_PRIO, true)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005366 ext4_msg(sb, KERN_ERR, "I/O error on journal device");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005367 goto out_journal;
5368 }
5369 if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005370 ext4_msg(sb, KERN_ERR, "External journal has more than one "
5371 "user (unsupported) - %d",
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005372 be32_to_cpu(journal->j_superblock->s_nr_users));
5373 goto out_journal;
5374 }
Chunguang Xuee7ed3a2020-09-24 11:03:42 +08005375 EXT4_SB(sb)->s_journal_bdev = bdev;
Mingming Cao617ba132006-10-11 01:20:53 -07005376 ext4_init_journal_params(sb, journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005377 return journal;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005378
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005379out_journal:
Mingming Caodab291a2006-10-11 01:21:01 -07005380 jbd2_journal_destroy(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005381out_bdev:
Mingming Cao617ba132006-10-11 01:20:53 -07005382 ext4_blkdev_put(bdev);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005383 return NULL;
5384}
5385
Mingming Cao617ba132006-10-11 01:20:53 -07005386static int ext4_load_journal(struct super_block *sb,
5387 struct ext4_super_block *es,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005388 unsigned long journal_devnum)
5389{
5390 journal_t *journal;
5391 unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
5392 dev_t journal_dev;
5393 int err = 0;
5394 int really_read_only;
Lukas Czerner273108f2020-07-17 11:06:05 +02005395 int journal_dev_ro;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005396
Jan Kara11215632020-07-10 16:07:59 +02005397 if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
5398 return -EFSCORRUPTED;
Frank Mayhar03901312009-01-07 00:06:22 -05005399
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005400 if (journal_devnum &&
5401 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005402 ext4_msg(sb, KERN_INFO, "external journal device major/minor "
5403 "numbers have changed");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005404 journal_dev = new_decode_dev(journal_devnum);
5405 } else
5406 journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
5407
Lukas Czerner273108f2020-07-17 11:06:05 +02005408 if (journal_inum && journal_dev) {
5409 ext4_msg(sb, KERN_ERR,
5410 "filesystem has both journal inode and journal device!");
5411 return -EINVAL;
5412 }
5413
5414 if (journal_inum) {
5415 journal = ext4_get_journal(sb, journal_inum);
5416 if (!journal)
5417 return -EINVAL;
5418 } else {
5419 journal = ext4_get_dev_journal(sb, journal_dev);
5420 if (!journal)
5421 return -EINVAL;
5422 }
5423
5424 journal_dev_ro = bdev_read_only(journal->j_dev);
5425 really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
5426
5427 if (journal_dev_ro && !sb_rdonly(sb)) {
5428 ext4_msg(sb, KERN_ERR,
5429 "journal device read-only, try mounting with '-o ro'");
5430 err = -EROFS;
5431 goto err_out;
5432 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005433
5434 /*
5435 * Are we loading a blank journal or performing recovery after a
5436 * crash? For recovery, we need to check in advance whether we
5437 * can get read-write access to the device.
5438 */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005439 if (ext4_has_feature_journal_needs_recovery(sb)) {
David Howellsbc98a422017-07-17 08:45:34 +01005440 if (sb_rdonly(sb)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005441 ext4_msg(sb, KERN_INFO, "INFO: recovery "
5442 "required on readonly filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005443 if (really_read_only) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005444 ext4_msg(sb, KERN_ERR, "write access "
Simon Ruderichd98bf8c2017-10-18 13:06:37 -04005445 "unavailable, cannot proceed "
5446 "(try mounting with noload)");
Lukas Czerner273108f2020-07-17 11:06:05 +02005447 err = -EROFS;
5448 goto err_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005449 }
Eric Sandeenb31e1552009-06-04 17:36:36 -04005450 ext4_msg(sb, KERN_INFO, "write access will "
5451 "be enabled during recovery");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005452 }
5453 }
5454
Theodore Ts'o90576c02009-09-29 15:51:30 -04005455 if (!(journal->j_flags & JBD2_BARRIER))
Eric Sandeenb31e1552009-06-04 17:36:36 -04005456 ext4_msg(sb, KERN_INFO, "barriers disabled");
Theodore Ts'o4776004f2008-09-08 23:00:52 -04005457
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005458 if (!ext4_has_feature_journal_needs_recovery(sb))
Mingming Caodab291a2006-10-11 01:21:01 -07005459 err = jbd2_journal_wipe(journal, !really_read_only);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04005460 if (!err) {
5461 char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
5462 if (save)
5463 memcpy(save, ((char *) es) +
5464 EXT4_S_ERR_START, EXT4_S_ERR_LEN);
Mingming Caodab291a2006-10-11 01:21:01 -07005465 err = jbd2_journal_load(journal);
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -04005466 if (save)
5467 memcpy(((char *) es) + EXT4_S_ERR_START,
5468 save, EXT4_S_ERR_LEN);
5469 kfree(save);
5470 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005471
5472 if (err) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005473 ext4_msg(sb, KERN_ERR, "error loading journal");
Lukas Czerner273108f2020-07-17 11:06:05 +02005474 goto err_out;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005475 }
5476
Mingming Cao617ba132006-10-11 01:20:53 -07005477 EXT4_SB(sb)->s_journal = journal;
Jan Kara11215632020-07-10 16:07:59 +02005478 err = ext4_clear_journal_err(sb, es);
5479 if (err) {
5480 EXT4_SB(sb)->s_journal = NULL;
5481 jbd2_journal_destroy(journal);
5482 return err;
5483 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005484
Maciej Żenczykowskic41303c2010-10-27 21:30:06 -04005485 if (!really_read_only && journal_devnum &&
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005486 journal_devnum != le32_to_cpu(es->s_journal_dev)) {
5487 es->s_journal_dev = cpu_to_le32(journal_devnum);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005488
5489 /* Make sure we flush the recovery flag to disk. */
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005490 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005491 }
5492
5493 return 0;
Lukas Czerner273108f2020-07-17 11:06:05 +02005494
5495err_out:
5496 jbd2_journal_destroy(journal);
5497 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005498}
5499
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005500static int ext4_commit_super(struct super_block *sb, int sync)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005501{
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005502 struct ext4_super_block *es = EXT4_SB(sb)->s_es;
Mingming Cao617ba132006-10-11 01:20:53 -07005503 struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
Takashi Satoc4be0c12009-01-09 16:40:58 -08005504 int error = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005505
Fengnan Chang12905cf2021-04-02 18:16:31 +08005506 if (!sbh)
5507 return -EINVAL;
5508 if (block_device_ejected(sb))
5509 return -ENODEV;
Jon Derricka17712c2018-07-02 18:45:18 -04005510
5511 /*
Theodore Ts'o71290b32009-09-10 17:31:04 -04005512 * If the file system is mounted read-only, don't update the
5513 * superblock write time. This avoids updating the superblock
5514 * write time when we are mounting the root file system
5515 * read/only but we need to replay the journal; at that point,
5516 * for people who are east of GMT and who make their clock
5517 * tick in localtime for Windows bug-for-bug compatibility,
5518 * the clock is set in the future, and this will cause e2fsck
5519 * to complain and force a full file system check.
5520 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005521 if (!(sb->s_flags & SB_RDONLY))
Arnd Bergmann6a0678a2018-07-29 15:51:48 -04005522 ext4_update_tstamp(es, s_wtime);
Theodore Ts'of613dfc2010-07-27 11:56:08 -04005523 if (sb->s_bdev->bd_part)
5524 es->s_kbytes_written =
5525 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
Michael Callahandbae2c52018-07-18 04:47:38 -07005526 ((part_stat_read(sb->s_bdev->bd_part,
5527 sectors[STAT_WRITE]) -
Theodore Ts'oafc32f72009-02-28 19:39:58 -05005528 EXT4_SB(sb)->s_sectors_written_start) >> 1));
Theodore Ts'of613dfc2010-07-27 11:56:08 -04005529 else
5530 es->s_kbytes_written =
5531 cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005532 if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
5533 ext4_free_blocks_count_set(es,
Theodore Ts'o57042652011-09-09 18:56:51 -04005534 EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
5535 &EXT4_SB(sb)->s_freeclusters_counter)));
Theodore Ts'od5e03cb2014-07-15 06:01:38 -04005536 if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
5537 es->s_free_inodes_count =
5538 cpu_to_le32(percpu_counter_sum_positive(
Theodore Ts'oce7e0102010-11-03 12:03:21 -04005539 &EXT4_SB(sb)->s_freeinodes_counter));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005540 BUFFER_TRACE(sbh, "marking dirty");
Theodore Ts'o06db49e2012-10-10 01:06:58 -04005541 ext4_superblock_csum_set(sb);
Theodore Ts'o1566a482016-11-13 22:02:29 -05005542 if (sync)
5543 lock_buffer(sbh);
Theodore Ts'oe8680782018-12-30 23:20:39 -05005544 if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
Pranay Kr. Srivastava4743f832016-07-04 10:24:52 -04005545 /*
5546 * Oh, dear. A previous attempt to write the
5547 * superblock failed. This could happen because the
5548 * USB device was yanked out. Or it could happen to
5549 * be a transient write error and maybe the block will
5550 * be remapped. Nothing we can do but to retry the
5551 * write and hope for the best.
5552 */
5553 ext4_msg(sb, KERN_ERR, "previous I/O error to "
5554 "superblock detected");
5555 clear_buffer_write_io_error(sbh);
5556 set_buffer_uptodate(sbh);
5557 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005558 mark_buffer_dirty(sbh);
Theodore Ts'o914258b2008-10-06 21:35:40 -04005559 if (sync) {
Theodore Ts'o1566a482016-11-13 22:02:29 -05005560 unlock_buffer(sbh);
Daeho Jeong564bc402015-07-23 09:46:11 -04005561 error = __sync_dirty_buffer(sbh,
Jan Kara00473372017-05-04 10:58:03 -04005562 REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04005563 if (buffer_write_io_error(sbh)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005564 ext4_msg(sb, KERN_ERR, "I/O error while writing "
5565 "superblock");
Theodore Ts'o914258b2008-10-06 21:35:40 -04005566 clear_buffer_write_io_error(sbh);
5567 set_buffer_uptodate(sbh);
5568 }
5569 }
Takashi Satoc4be0c12009-01-09 16:40:58 -08005570 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005571}
5572
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005573/*
5574 * Have we just finished recovery? If so, and if we are mounting (or
5575 * remounting) the filesystem readonly, then we will end up with a
5576 * consistent fs on disk. Record that fact.
5577 */
Jan Kara11215632020-07-10 16:07:59 +02005578static int ext4_mark_recovery_complete(struct super_block *sb,
5579 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005580{
Jan Kara11215632020-07-10 16:07:59 +02005581 int err;
Mingming Cao617ba132006-10-11 01:20:53 -07005582 journal_t *journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005583
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005584 if (!ext4_has_feature_journal(sb)) {
Jan Kara11215632020-07-10 16:07:59 +02005585 if (journal != NULL) {
5586 ext4_error(sb, "Journal got removed while the fs was "
5587 "mounted!");
5588 return -EFSCORRUPTED;
5589 }
5590 return 0;
Frank Mayhar03901312009-01-07 00:06:22 -05005591 }
Mingming Caodab291a2006-10-11 01:21:01 -07005592 jbd2_journal_lock_updates(journal);
Jan Kara11215632020-07-10 16:07:59 +02005593 err = jbd2_journal_flush(journal);
5594 if (err < 0)
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005595 goto out;
5596
David Howellsbc98a422017-07-17 08:45:34 +01005597 if (ext4_has_feature_journal_needs_recovery(sb) && sb_rdonly(sb)) {
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005598 ext4_clear_feature_journal_needs_recovery(sb);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005599 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005600 }
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005601out:
Mingming Caodab291a2006-10-11 01:21:01 -07005602 jbd2_journal_unlock_updates(journal);
Jan Kara11215632020-07-10 16:07:59 +02005603 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005604}
5605
5606/*
5607 * If we are mounting (or read-write remounting) a filesystem whose journal
5608 * has recorded an error from a previous lifetime, move that error to the
5609 * main filesystem now.
5610 */
Jan Kara11215632020-07-10 16:07:59 +02005611static int ext4_clear_journal_err(struct super_block *sb,
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005612 struct ext4_super_block *es)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005613{
5614 journal_t *journal;
5615 int j_errno;
5616 const char *errstr;
5617
Jan Kara11215632020-07-10 16:07:59 +02005618 if (!ext4_has_feature_journal(sb)) {
5619 ext4_error(sb, "Journal got removed while the fs was mounted!");
5620 return -EFSCORRUPTED;
5621 }
Frank Mayhar03901312009-01-07 00:06:22 -05005622
Mingming Cao617ba132006-10-11 01:20:53 -07005623 journal = EXT4_SB(sb)->s_journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005624
5625 /*
5626 * Now check for any error status which may have been recorded in the
Mingming Cao617ba132006-10-11 01:20:53 -07005627 * journal by a prior ext4_error() or ext4_abort()
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005628 */
5629
Mingming Caodab291a2006-10-11 01:21:01 -07005630 j_errno = jbd2_journal_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005631 if (j_errno) {
5632 char nbuf[16];
5633
Mingming Cao617ba132006-10-11 01:20:53 -07005634 errstr = ext4_decode_error(sb, j_errno, nbuf);
Eric Sandeen12062dd2010-02-15 14:19:27 -05005635 ext4_warning(sb, "Filesystem error recorded "
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005636 "from previous mount: %s", errstr);
Eric Sandeen12062dd2010-02-15 14:19:27 -05005637 ext4_warning(sb, "Marking fs in need of filesystem check.");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005638
Mingming Cao617ba132006-10-11 01:20:53 -07005639 EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
5640 es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
Theodore Ts'oe2d67052009-05-01 00:33:44 -04005641 ext4_commit_super(sb, 1);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005642
Mingming Caodab291a2006-10-11 01:21:01 -07005643 jbd2_journal_clear_err(journal);
Theodore Ts'od796c522012-08-05 19:04:57 -04005644 jbd2_journal_update_sb_errno(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005645 }
Jan Kara11215632020-07-10 16:07:59 +02005646 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005647}
5648
5649/*
5650 * Force the running and committing transactions to commit,
5651 * and wait on the commit.
5652 */
Mingming Cao617ba132006-10-11 01:20:53 -07005653int ext4_force_commit(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005654{
5655 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005656
David Howellsbc98a422017-07-17 08:45:34 +01005657 if (sb_rdonly(sb))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005658 return 0;
5659
Mingming Cao617ba132006-10-11 01:20:53 -07005660 journal = EXT4_SB(sb)->s_journal;
Guo Chaob1deefc2013-01-28 21:41:02 -05005661 return ext4_journal_force_commit(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005662}
5663
Mingming Cao617ba132006-10-11 01:20:53 -07005664static int ext4_sync_fs(struct super_block *sb, int wait)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005665{
Theodore Ts'o14ce0cb2008-11-03 18:10:55 -05005666 int ret = 0;
Jan Kara9eddacf2009-02-10 06:46:05 -05005667 tid_t target;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005668 bool needs_barrier = false;
Mingming Cao8d5d02e2009-09-28 15:48:29 -04005669 struct ext4_sb_info *sbi = EXT4_SB(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005670
Jun Piao49598e02018-01-11 13:17:49 -05005671 if (unlikely(ext4_forced_shutdown(sbi)))
Theodore Ts'o0db1ff22017-02-05 01:28:48 -05005672 return 0;
5673
Theodore Ts'o9bffad12009-06-17 11:48:11 -04005674 trace_ext4_sync_fs(sb, wait);
Jan Kara2e8fa542013-06-04 14:21:02 -04005675 flush_workqueue(sbi->rsv_conversion_wq);
Jan Karaa1177822012-07-03 16:45:29 +02005676 /*
5677 * Writeback quota in non-journalled quota case - journalled quota has
5678 * no dirty dquots
5679 */
5680 dquot_writeback_dquots(sb, -1);
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005681 /*
5682 * Data writeback is possible w/o journal transaction, so barrier must
5683 * being sent at the end of the function. But we can skip it if
5684 * transaction_commit will do it for us.
5685 */
Theodore Ts'obda32532014-09-18 16:12:37 -04005686 if (sbi->s_journal) {
5687 target = jbd2_get_latest_transaction(sbi->s_journal);
5688 if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
5689 !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
5690 needs_barrier = true;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005691
Theodore Ts'obda32532014-09-18 16:12:37 -04005692 if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
5693 if (wait)
5694 ret = jbd2_log_wait_commit(sbi->s_journal,
5695 target);
5696 }
5697 } else if (wait && test_opt(sb, BARRIER))
5698 needs_barrier = true;
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005699 if (needs_barrier) {
5700 int err;
Christoph Hellwig93985542020-05-13 14:36:00 +02005701 err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL);
Dmitry Monakhov06a407f2013-06-12 22:25:07 -04005702 if (!ret)
5703 ret = err;
5704 }
5705
5706 return ret;
5707}
5708
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005709/*
5710 * LVM calls this function before a (read-only) snapshot is created. This
5711 * gives us a chance to flush the journal completely and mark the fs clean.
Yongqiang Yangbe4f27d2011-04-10 22:06:07 -04005712 *
5713 * Note that only this function cannot bring a filesystem to be in a clean
Jan Kara8e8ad8a2012-06-12 16:20:38 +02005714 * state independently. It relies on upper layer to stop all data & metadata
5715 * modifications.
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005716 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08005717static int ext4_freeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005718{
Takashi Satoc4be0c12009-01-09 16:40:58 -08005719 int error = 0;
5720 journal_t *journal;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005721
David Howellsbc98a422017-07-17 08:45:34 +01005722 if (sb_rdonly(sb))
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005723 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005724
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005725 journal = EXT4_SB(sb)->s_journal;
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04005726
Theodore Ts'obb044572014-09-18 17:12:02 -04005727 if (journal) {
5728 /* Now we set up the journal barrier. */
5729 jbd2_journal_lock_updates(journal);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005730
Theodore Ts'obb044572014-09-18 17:12:02 -04005731 /*
5732 * Don't clear the needs_recovery flag if we failed to
5733 * flush the journal.
5734 */
5735 error = jbd2_journal_flush(journal);
5736 if (error < 0)
5737 goto out;
Eric Sandeenc642dc92015-08-15 10:45:06 -04005738
5739 /* Journal blocked and flushed, clear needs_recovery flag. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005740 ext4_clear_feature_journal_needs_recovery(sb);
Theodore Ts'obb044572014-09-18 17:12:02 -04005741 }
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005742
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005743 error = ext4_commit_super(sb, 1);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04005744out:
Theodore Ts'obb044572014-09-18 17:12:02 -04005745 if (journal)
5746 /* we rely on upper layer to stop further updates */
5747 jbd2_journal_unlock_updates(journal);
Eric Sandeen6b0310f2010-05-16 02:00:00 -04005748 return error;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005749}
5750
5751/*
5752 * Called by LVM after the snapshot is done. We need to reset the RECOVER
5753 * flag here, even though the filesystem is not technically dirty yet.
5754 */
Takashi Satoc4be0c12009-01-09 16:40:58 -08005755static int ext4_unfreeze(struct super_block *sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005756{
David Howellsbc98a422017-07-17 08:45:34 +01005757 if (sb_rdonly(sb) || ext4_forced_shutdown(EXT4_SB(sb)))
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005758 return 0;
5759
Eric Sandeenc642dc92015-08-15 10:45:06 -04005760 if (EXT4_SB(sb)->s_journal) {
5761 /* Reset the needs_recovery flag before the fs is unlocked. */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005762 ext4_set_feature_journal_needs_recovery(sb);
Eric Sandeenc642dc92015-08-15 10:45:06 -04005763 }
5764
Theodore Ts'o9ca92382009-05-01 12:52:25 -04005765 ext4_commit_super(sb, 1);
Takashi Satoc4be0c12009-01-09 16:40:58 -08005766 return 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005767}
5768
Theodore Ts'o673c6102010-12-15 20:28:48 -05005769/*
5770 * Structure to save mount options for ext4_remount's benefit
5771 */
5772struct ext4_mount_options {
5773 unsigned long s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05005774 unsigned long s_mount_opt2;
Eric W. Biederman08cefc72012-02-07 15:41:49 -08005775 kuid_t s_resuid;
5776 kgid_t s_resgid;
Theodore Ts'o673c6102010-12-15 20:28:48 -05005777 unsigned long s_commit_interval;
5778 u32 s_min_batch_time, s_max_batch_time;
5779#ifdef CONFIG_QUOTA
5780 int s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04005781 char *s_qf_names[EXT4_MAXQUOTAS];
Theodore Ts'o673c6102010-12-15 20:28:48 -05005782#endif
5783};
5784
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005785static int ext4_remount(struct super_block *sb, int *flags, char *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005786{
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04005787 struct ext4_super_block *es;
Mingming Cao617ba132006-10-11 01:20:53 -07005788 struct ext4_sb_info *sbi = EXT4_SB(sb);
Lukas Czernerf25391e2020-07-23 17:05:26 +02005789 unsigned long old_sb_flags, vfs_flags;
Mingming Cao617ba132006-10-11 01:20:53 -07005790 struct ext4_mount_options old_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005791 int enable_quota = 0;
Theodore Ts'o8a266462008-07-26 14:34:21 -04005792 ext4_group_t g;
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005793 unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
Johann Lombardic5e06d12011-05-24 18:31:25 -04005794 int err = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005795#ifdef CONFIG_QUOTA
Chen Gang03dafb52013-01-24 23:24:58 -05005796 int i, j;
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005797 char *to_free[EXT4_MAXQUOTAS];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005798#endif
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04005799 char *orig_data = kstrdup(data, GFP_KERNEL);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005800
Chengguang Xu21ac7382018-07-29 15:51:54 -04005801 if (data && !orig_data)
5802 return -ENOMEM;
5803
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005804 /* Store the original options */
5805 old_sb_flags = sb->s_flags;
5806 old_opts.s_mount_opt = sbi->s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05005807 old_opts.s_mount_opt2 = sbi->s_mount_opt2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005808 old_opts.s_resuid = sbi->s_resuid;
5809 old_opts.s_resgid = sbi->s_resgid;
5810 old_opts.s_commit_interval = sbi->s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05005811 old_opts.s_min_batch_time = sbi->s_min_batch_time;
5812 old_opts.s_max_batch_time = sbi->s_max_batch_time;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005813#ifdef CONFIG_QUOTA
5814 old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04005815 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Chen Gang03dafb52013-01-24 23:24:58 -05005816 if (sbi->s_qf_names[i]) {
Theodore Ts'o33458ea2018-10-12 09:28:09 -04005817 char *qf_name = get_qf_name(sb, sbi, i);
5818
5819 old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
Chen Gang03dafb52013-01-24 23:24:58 -05005820 if (!old_opts.s_qf_names[i]) {
5821 for (j = 0; j < i; j++)
5822 kfree(old_opts.s_qf_names[j]);
Wei Yongjun3e36a162013-03-02 17:13:55 -05005823 kfree(orig_data);
Chen Gang03dafb52013-01-24 23:24:58 -05005824 return -ENOMEM;
5825 }
5826 } else
5827 old_opts.s_qf_names[i] = NULL;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005828#endif
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005829 if (sbi->s_journal && sbi->s_journal->j_task->io_context)
5830 journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005831
Lukas Czernerf25391e2020-07-23 17:05:26 +02005832 /*
5833 * Some options can be enabled by ext4 and/or by VFS mount flag
5834 * either way we need to make sure it matches in both *flags and
5835 * s_flags. Copy those selected flags from *flags to s_flags
5836 */
5837 vfs_flags = SB_LAZYTIME | SB_I_VERSION;
5838 sb->s_flags = (sb->s_flags & ~vfs_flags) | (*flags & vfs_flags);
5839
Eric Sandeen661aa522012-02-20 17:53:04 -05005840 if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005841 err = -EINVAL;
5842 goto restore_opts;
5843 }
5844
Darrick J. Wong6b992ff2014-10-30 10:53:16 -04005845 if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
5846 test_opt(sb, JOURNAL_CHECKSUM)) {
5847 ext4_msg(sb, KERN_ERR, "changing journal_checksum "
Eric Sandeen2d5b86e2015-02-12 23:07:37 -05005848 "during remount not supported; ignoring");
5849 sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
Darrick J. Wongc6d3d562014-11-25 16:20:50 -05005850 }
5851
Piotr Sarna6ae65142013-08-08 23:02:24 -04005852 if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
5853 if (test_opt2(sb, EXPLICIT_DELALLOC)) {
5854 ext4_msg(sb, KERN_ERR, "can't mount with "
5855 "both data=journal and delalloc");
5856 err = -EINVAL;
5857 goto restore_opts;
5858 }
5859 if (test_opt(sb, DIOREAD_NOLOCK)) {
5860 ext4_msg(sb, KERN_ERR, "can't mount with "
5861 "both data=journal and dioread_nolock");
5862 err = -EINVAL;
5863 goto restore_opts;
5864 }
Jan Karaab04df72016-12-03 16:20:53 -05005865 } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
5866 if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
5867 ext4_msg(sb, KERN_ERR, "can't mount with "
5868 "journal_async_commit in data=ordered mode");
5869 err = -EINVAL;
5870 goto restore_opts;
5871 }
Ross Zwisler923ae0f2015-02-16 15:59:38 -08005872 }
5873
Tahsin Erdogancdb7ee42017-06-22 11:55:14 -04005874 if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
5875 ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
5876 err = -EINVAL;
5877 goto restore_opts;
5878 }
5879
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08005880 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED))
Theodore Ts'o54d3adb2020-03-28 19:33:43 -04005881 ext4_abort(sb, EXT4_ERR_ESHUTDOWN, "Abort forced by user");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005882
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005883 sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
5884 (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005885
5886 es = sbi->s_es;
5887
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005888 if (sbi->s_journal) {
Frank Mayhar03901312009-01-07 00:06:22 -05005889 ext4_init_journal_params(sb, sbi->s_journal);
Theodore Ts'ob3881f72009-01-05 22:46:26 -05005890 set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
5891 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005892
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005893 if ((bool)(*flags & SB_RDONLY) != sb_rdonly(sb)) {
Harshad Shirwadkar9b5f6c92020-11-05 19:59:09 -08005894 if (ext4_test_mount_flag(sb, EXT4_MF_FS_ABORTED)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005895 err = -EROFS;
5896 goto restore_opts;
5897 }
5898
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005899 if (*flags & SB_RDONLY) {
Theodore Ts'o38c03b32014-03-13 22:49:42 -04005900 err = sync_filesystem(sb);
5901 if (err < 0)
5902 goto restore_opts;
Christoph Hellwig0f0dd622010-05-19 07:16:41 -04005903 err = dquot_suspend(sb, -1);
5904 if (err < 0)
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005905 goto restore_opts;
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005906
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005907 /*
5908 * First of all, the unconditional stuff we have to do
5909 * to disable replay of the journal when we next remount
5910 */
Linus Torvalds1751e8a2017-11-27 13:05:09 -08005911 sb->s_flags |= SB_RDONLY;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005912
5913 /*
5914 * OK, test if we are remounting a valid rw partition
5915 * readonly, and if so set the rdonly flag and then
5916 * mark the partition as valid again.
5917 */
Mingming Cao617ba132006-10-11 01:20:53 -07005918 if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5919 (sbi->s_mount_state & EXT4_VALID_FS))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005920 es->s_state = cpu_to_le16(sbi->s_mount_state);
5921
Jan Kara11215632020-07-10 16:07:59 +02005922 if (sbi->s_journal) {
5923 /*
5924 * We let remount-ro finish even if marking fs
5925 * as clean failed...
5926 */
Frank Mayhar03901312009-01-07 00:06:22 -05005927 ext4_mark_recovery_complete(sb, es);
Jan Kara11215632020-07-10 16:07:59 +02005928 }
Pavel Skripkin5e4f5132021-04-30 21:50:46 +03005929 ext4_stop_mmpd(sbi);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005930 } else {
Eric Sandeena13fb1a2009-08-18 00:20:23 -04005931 /* Make sure we can mount this feature set readwrite */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005932 if (ext4_has_feature_readonly(sb) ||
Darrick J. Wong2cb5cc82015-02-12 22:31:21 -05005933 !ext4_feature_set_ok(sb, 0)) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005934 err = -EROFS;
5935 goto restore_opts;
5936 }
Eric Sandeenead65962007-02-10 01:46:08 -08005937 /*
Theodore Ts'o8a266462008-07-26 14:34:21 -04005938 * Make sure the group descriptor checksums
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04005939 * are sane. If they aren't, refuse to remount r/w.
Theodore Ts'o8a266462008-07-26 14:34:21 -04005940 */
5941 for (g = 0; g < sbi->s_groups_count; g++) {
5942 struct ext4_group_desc *gdp =
5943 ext4_get_group_desc(sb, g, NULL);
5944
Darrick J. Wongfeb0ab32012-04-29 18:45:10 -04005945 if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005946 ext4_msg(sb, KERN_ERR,
5947 "ext4_remount: Checksum for group %u failed (%u!=%u)",
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005948 g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
Theodore Ts'o8a266462008-07-26 14:34:21 -04005949 le16_to_cpu(gdp->bg_checksum));
Darrick J. Wong6a797d22015-10-17 16:16:04 -04005950 err = -EFSBADCRC;
Theodore Ts'o8a266462008-07-26 14:34:21 -04005951 goto restore_opts;
5952 }
5953 }
5954
5955 /*
Eric Sandeenead65962007-02-10 01:46:08 -08005956 * If we have an unprocessed orphan list hanging
5957 * around from a previously readonly bdev mount,
5958 * require a full umount/remount for now.
5959 */
5960 if (es->s_last_orphan) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04005961 ext4_msg(sb, KERN_WARNING, "Couldn't "
Eric Sandeenead65962007-02-10 01:46:08 -08005962 "remount RDWR because of unprocessed "
5963 "orphan inode list. Please "
Eric Sandeenb31e1552009-06-04 17:36:36 -04005964 "umount/remount instead");
Eric Sandeenead65962007-02-10 01:46:08 -08005965 err = -EINVAL;
5966 goto restore_opts;
5967 }
5968
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005969 /*
5970 * Mounting a RDONLY partition read-write, so reread
5971 * and store the current valid flag. (It may have
5972 * been changed by e2fsck since we originally mounted
5973 * the partition.)
5974 */
Jan Kara11215632020-07-10 16:07:59 +02005975 if (sbi->s_journal) {
5976 err = ext4_clear_journal_err(sb, es);
5977 if (err)
5978 goto restore_opts;
5979 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005980 sbi->s_mount_state = le16_to_cpu(es->s_state);
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04005981
5982 err = ext4_setup_super(sb, es, 0);
5983 if (err)
5984 goto restore_opts;
5985
5986 sb->s_flags &= ~SB_RDONLY;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04005987 if (ext4_has_feature_mmp(sb))
Johann Lombardic5e06d12011-05-24 18:31:25 -04005988 if (ext4_multi_mount_protect(sb,
5989 le64_to_cpu(es->s_mmp_block))) {
5990 err = -EROFS;
5991 goto restore_opts;
5992 }
Christoph Hellwigc79d9672010-05-19 07:16:40 -04005993 enable_quota = 1;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07005994 }
5995 }
Lukas Czernerbfff6872010-10-27 21:30:05 -04005996
5997 /*
5998 * Reinitialize lazy itable initialization thread based on
5999 * current settings
6000 */
David Howellsbc98a422017-07-17 08:45:34 +01006001 if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
Lukas Czernerbfff6872010-10-27 21:30:05 -04006002 ext4_unregister_li_request(sb);
6003 else {
6004 ext4_group_t first_not_zeroed;
6005 first_not_zeroed = ext4_has_uninit_itable(sb);
6006 ext4_register_li_request(sb, first_not_zeroed);
6007 }
6008
Jan Kara0f5bde12020-07-28 15:04:37 +02006009 /*
6010 * Handle creation of system zone data early because it can fail.
6011 * Releasing of existing data is done when we are sure remount will
6012 * succeed.
6013 */
Chunguang Xudd0db942020-09-24 11:03:43 +08006014 if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
Jan Kara0f5bde12020-07-28 15:04:37 +02006015 err = ext4_setup_system_zone(sb);
6016 if (err)
6017 goto restore_opts;
6018 }
Jan Karad176b1f2020-07-28 15:04:32 +02006019
Jaegeuk Kimc89128a2018-05-13 23:02:19 -04006020 if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
6021 err = ext4_commit_super(sb, 1);
6022 if (err)
6023 goto restore_opts;
6024 }
Frank Mayhar03901312009-01-07 00:06:22 -05006025
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006026#ifdef CONFIG_QUOTA
6027 /* Release old quota file names */
Jan Karaa2d4a642014-09-11 11:15:15 -04006028 for (i = 0; i < EXT4_MAXQUOTAS; i++)
Chen Gang03dafb52013-01-24 23:24:58 -05006029 kfree(old_opts.s_qf_names[i]);
Aditya Kali7c319d32012-07-22 20:21:31 -04006030 if (enable_quota) {
6031 if (sb_any_quota_suspended(sb))
6032 dquot_resume(sb, -1);
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006033 else if (ext4_has_feature_quota(sb)) {
Aditya Kali7c319d32012-07-22 20:21:31 -04006034 err = ext4_enable_quotas(sb);
Theodore Ts'o07724f92012-08-17 19:08:42 -04006035 if (err)
Aditya Kali7c319d32012-07-22 20:21:31 -04006036 goto restore_opts;
Aditya Kali7c319d32012-07-22 20:21:31 -04006037 }
6038 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006039#endif
Chunguang Xudd0db942020-09-24 11:03:43 +08006040 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
Jan Kara0f5bde12020-07-28 15:04:37 +02006041 ext4_release_system_zone(sb);
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04006042
Lukas Czernerf25391e2020-07-23 17:05:26 +02006043 /*
6044 * Some options can be enabled by ext4 and/or by VFS mount flag
6045 * either way we need to make sure it matches in both *flags and
6046 * s_flags. Copy those selected flags from s_flags to *flags
6047 */
6048 *flags = (*flags & ~vfs_flags) | (sb->s_flags & vfs_flags);
6049
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04006050 ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
6051 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006052 return 0;
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04006053
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006054restore_opts:
6055 sb->s_flags = old_sb_flags;
6056 sbi->s_mount_opt = old_opts.s_mount_opt;
Theodore Ts'oa2595b82010-12-15 20:30:48 -05006057 sbi->s_mount_opt2 = old_opts.s_mount_opt2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006058 sbi->s_resuid = old_opts.s_resuid;
6059 sbi->s_resgid = old_opts.s_resgid;
6060 sbi->s_commit_interval = old_opts.s_commit_interval;
Theodore Ts'o30773842009-01-03 20:27:38 -05006061 sbi->s_min_batch_time = old_opts.s_min_batch_time;
6062 sbi->s_max_batch_time = old_opts.s_max_batch_time;
Chunguang Xudd0db942020-09-24 11:03:43 +08006063 if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
Jan Kara0f5bde12020-07-28 15:04:37 +02006064 ext4_release_system_zone(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006065#ifdef CONFIG_QUOTA
6066 sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
Jan Karaa2d4a642014-09-11 11:15:15 -04006067 for (i = 0; i < EXT4_MAXQUOTAS; i++) {
Theodore Ts'o33458ea2018-10-12 09:28:09 -04006068 to_free[i] = get_qf_name(sb, sbi, i);
6069 rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006070 }
Theodore Ts'o33458ea2018-10-12 09:28:09 -04006071 synchronize_rcu();
6072 for (i = 0; i < EXT4_MAXQUOTAS; i++)
6073 kfree(to_free[i]);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006074#endif
Curt Wohlgemuthd4c402d2010-05-16 12:00:00 -04006075 kfree(orig_data);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006076 return err;
6077}
6078
Li Xi689c9582016-01-08 16:01:22 -05006079#ifdef CONFIG_QUOTA
6080static int ext4_statfs_project(struct super_block *sb,
6081 kprojid_t projid, struct kstatfs *buf)
6082{
6083 struct kqid qid;
6084 struct dquot *dquot;
6085 u64 limit;
6086 u64 curblock;
6087
6088 qid = make_kqid_projid(projid);
6089 dquot = dqget(sb, qid);
6090 if (IS_ERR(dquot))
6091 return PTR_ERR(dquot);
Jan Kara7b9ca4c2017-08-07 13:19:50 +02006092 spin_lock(&dquot->dq_dqb_lock);
Li Xi689c9582016-01-08 16:01:22 -05006093
Chengguang Xua08fe662020-02-10 16:24:45 +08006094 limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
6095 dquot->dq_dqb.dqb_bhardlimit);
Chengguang Xu57c32ea2019-10-16 10:25:01 +08006096 limit >>= sb->s_blocksize_bits;
6097
Li Xi689c9582016-01-08 16:01:22 -05006098 if (limit && buf->f_blocks > limit) {
Konstantin Khlebnikovf06925c2018-05-20 22:49:54 -04006099 curblock = (dquot->dq_dqb.dqb_curspace +
6100 dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
Li Xi689c9582016-01-08 16:01:22 -05006101 buf->f_blocks = limit;
6102 buf->f_bfree = buf->f_bavail =
6103 (buf->f_blocks > curblock) ?
6104 (buf->f_blocks - curblock) : 0;
6105 }
6106
Chengguang Xua08fe662020-02-10 16:24:45 +08006107 limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
6108 dquot->dq_dqb.dqb_ihardlimit);
Li Xi689c9582016-01-08 16:01:22 -05006109 if (limit && buf->f_files > limit) {
6110 buf->f_files = limit;
6111 buf->f_ffree =
6112 (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
6113 (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
6114 }
6115
Jan Kara7b9ca4c2017-08-07 13:19:50 +02006116 spin_unlock(&dquot->dq_dqb_lock);
Li Xi689c9582016-01-08 16:01:22 -05006117 dqput(dquot);
6118 return 0;
6119}
6120#endif
6121
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04006122static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006123{
6124 struct super_block *sb = dentry->d_sb;
Mingming Cao617ba132006-10-11 01:20:53 -07006125 struct ext4_sb_info *sbi = EXT4_SB(sb);
6126 struct ext4_super_block *es = sbi->s_es;
Lukas Czerner27dd4382013-04-09 22:11:22 -04006127 ext4_fsblk_t overhead = 0, resv_blocks;
Pekka Enberg960cc392006-12-06 20:35:29 -08006128 u64 fsid;
Kazuya Miod02a9392011-05-24 18:30:07 -04006129 s64 bfree;
Lukas Czerner27dd4382013-04-09 22:11:22 -04006130 resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006131
Theodore Ts'o952fc182012-07-09 16:27:05 -04006132 if (!test_opt(sb, MINIX_DF))
6133 overhead = sbi->s_overhead;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006134
Mingming Cao617ba132006-10-11 01:20:53 -07006135 buf->f_type = EXT4_SUPER_MAGIC;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006136 buf->f_bsize = sb->s_blocksize;
Eric Sandeenb72f78c2012-11-08 10:33:36 -05006137 buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
Theodore Ts'o57042652011-09-09 18:56:51 -04006138 bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
6139 percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
Kazuya Miod02a9392011-05-24 18:30:07 -04006140 /* prevent underflow in case that few free space is available */
Theodore Ts'o57042652011-09-09 18:56:51 -04006141 buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
Lukas Czerner27dd4382013-04-09 22:11:22 -04006142 buf->f_bavail = buf->f_bfree -
6143 (ext4_r_blocks_count(es) + resv_blocks);
6144 if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006145 buf->f_bavail = 0;
6146 buf->f_files = le32_to_cpu(es->s_inodes_count);
Peter Zijlstra52d9f3b2007-10-16 23:25:44 -07006147 buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
Mingming Cao617ba132006-10-11 01:20:53 -07006148 buf->f_namelen = EXT4_NAME_LEN;
Pekka Enberg960cc392006-12-06 20:35:29 -08006149 fsid = le64_to_cpup((void *)es->s_uuid) ^
6150 le64_to_cpup((void *)es->s_uuid + sizeof(u64));
Al Viro6d1349c2020-09-18 16:45:50 -04006151 buf->f_fsid = u64_to_fsid(fsid);
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04006152
Li Xi689c9582016-01-08 16:01:22 -05006153#ifdef CONFIG_QUOTA
6154 if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
6155 sb_has_quota_limits_enabled(sb, PRJQUOTA))
6156 ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
6157#endif
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006158 return 0;
6159}
6160
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006161
6162#ifdef CONFIG_QUOTA
6163
Jan Karabc8230e2017-06-08 14:39:48 +02006164/*
6165 * Helper functions so that transaction is started before we acquire dqio_sem
6166 * to keep correct lock ordering of transaction > dqio_sem
6167 */
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006168static inline struct inode *dquot_to_inode(struct dquot *dquot)
6169{
Eric W. Biederman4c376dc2012-09-16 03:56:19 -07006170 return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006171}
6172
Mingming Cao617ba132006-10-11 01:20:53 -07006173static int ext4_write_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006174{
6175 int ret, err;
6176 handle_t *handle;
6177 struct inode *inode;
6178
6179 inode = dquot_to_inode(dquot);
Theodore Ts'o9924a922013-02-08 21:59:22 -05006180 handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04006181 EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006182 if (IS_ERR(handle))
6183 return PTR_ERR(handle);
6184 ret = dquot_commit(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07006185 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006186 if (!ret)
6187 ret = err;
6188 return ret;
6189}
6190
Mingming Cao617ba132006-10-11 01:20:53 -07006191static int ext4_acquire_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006192{
6193 int ret, err;
6194 handle_t *handle;
6195
Theodore Ts'o9924a922013-02-08 21:59:22 -05006196 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04006197 EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006198 if (IS_ERR(handle))
6199 return PTR_ERR(handle);
6200 ret = dquot_acquire(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07006201 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006202 if (!ret)
6203 ret = err;
6204 return ret;
6205}
6206
Mingming Cao617ba132006-10-11 01:20:53 -07006207static int ext4_release_dquot(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006208{
6209 int ret, err;
6210 handle_t *handle;
6211
Theodore Ts'o9924a922013-02-08 21:59:22 -05006212 handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
Andreas Dilger0b8e58a2009-06-03 17:59:28 -04006213 EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
Jan Kara9c3013e2007-09-11 15:23:29 -07006214 if (IS_ERR(handle)) {
6215 /* Release dquot anyway to avoid endless cycle in dqput() */
6216 dquot_release(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006217 return PTR_ERR(handle);
Jan Kara9c3013e2007-09-11 15:23:29 -07006218 }
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006219 ret = dquot_release(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07006220 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006221 if (!ret)
6222 ret = err;
6223 return ret;
6224}
6225
Mingming Cao617ba132006-10-11 01:20:53 -07006226static int ext4_mark_dquot_dirty(struct dquot *dquot)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006227{
Jan Kara262b4662013-03-02 17:57:08 -05006228 struct super_block *sb = dquot->dq_sb;
6229 struct ext4_sb_info *sbi = EXT4_SB(sb);
6230
Jan Kara2c8be6b2008-05-13 21:27:55 -04006231 /* Are we journaling quotas? */
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006232 if (ext4_has_feature_quota(sb) ||
Jan Kara262b4662013-03-02 17:57:08 -05006233 sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006234 dquot_mark_dquot_dirty(dquot);
Mingming Cao617ba132006-10-11 01:20:53 -07006235 return ext4_write_dquot(dquot);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006236 } else {
6237 return dquot_mark_dquot_dirty(dquot);
6238 }
6239}
6240
Mingming Cao617ba132006-10-11 01:20:53 -07006241static int ext4_write_info(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006242{
6243 int ret, err;
6244 handle_t *handle;
6245
6246 /* Data block + inode block */
David Howells2b0143b2015-03-17 22:25:59 +00006247 handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006248 if (IS_ERR(handle))
6249 return PTR_ERR(handle);
6250 ret = dquot_commit_info(sb, type);
Mingming Cao617ba132006-10-11 01:20:53 -07006251 err = ext4_journal_stop(handle);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006252 if (!ret)
6253 ret = err;
6254 return ret;
6255}
6256
6257/*
6258 * Turn on quotas during mount time - we need to find
6259 * the quota file and such...
6260 */
Mingming Cao617ba132006-10-11 01:20:53 -07006261static int ext4_quota_on_mount(struct super_block *sb, int type)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006262{
Theodore Ts'o33458ea2018-10-12 09:28:09 -04006263 return dquot_quota_on_mount(sb, get_qf_name(sb, EXT4_SB(sb), type),
Christoph Hellwig287a8092010-05-19 07:16:45 -04006264 EXT4_SB(sb)->s_jquota_fmt, type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006265}
6266
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006267static void lockdep_set_quota_inode(struct inode *inode, int subclass)
6268{
6269 struct ext4_inode_info *ei = EXT4_I(inode);
6270
6271 /* The first argument of lockdep_set_subclass has to be
6272 * *exactly* the same as the argument to init_rwsem() --- in
6273 * this case, in init_once() --- or lockdep gets unhappy
6274 * because the name of the lock is set using the
6275 * stringification of the argument to init_rwsem().
6276 */
6277 (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
6278 lockdep_set_subclass(&ei->i_data_sem, subclass);
6279}
6280
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006281/*
6282 * Standard function to be called on quota_on
6283 */
Mingming Cao617ba132006-10-11 01:20:53 -07006284static int ext4_quota_on(struct super_block *sb, int type, int format_id,
Al Viro8c54ca92016-11-20 19:49:34 -05006285 const struct path *path)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006286{
6287 int err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006288
6289 if (!test_opt(sb, QUOTA))
6290 return -EINVAL;
Jan Kara06235432008-05-13 19:11:51 -04006291
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006292 /* Quotafile not on the same filesystem? */
Al Virod8c95842011-12-07 18:16:57 -05006293 if (path->dentry->d_sb != sb)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006294 return -EXDEV;
Jan Karae0770e92020-10-15 13:03:30 +02006295
6296 /* Quota already enabled for this file? */
6297 if (IS_NOQUOTA(d_inode(path->dentry)))
6298 return -EBUSY;
6299
Jan Kara06235432008-05-13 19:11:51 -04006300 /* Journaling quota? */
6301 if (EXT4_SB(sb)->s_qf_names[type]) {
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04006302 /* Quotafile not in fs root? */
Jan Karaf00c9e42010-09-15 17:38:58 +02006303 if (path->dentry->d_parent != sb->s_root)
Eric Sandeenb31e1552009-06-04 17:36:36 -04006304 ext4_msg(sb, KERN_WARNING,
6305 "Quota file not on filesystem root. "
6306 "Journaled quota will not work");
Jan Kara91389242017-08-03 11:25:55 +02006307 sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
6308 } else {
6309 /*
6310 * Clear the flag just in case mount options changed since
6311 * last time.
6312 */
6313 sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
Theodore Ts'o2b2d6d02008-07-26 16:15:44 -04006314 }
Jan Kara06235432008-05-13 19:11:51 -04006315
6316 /*
6317 * When we journal data on quota file, we have to flush journal to see
6318 * all updates to the file when we bypass pagecache...
6319 */
Frank Mayhar03901312009-01-07 00:06:22 -05006320 if (EXT4_SB(sb)->s_journal &&
David Howells2b0143b2015-03-17 22:25:59 +00006321 ext4_should_journal_data(d_inode(path->dentry))) {
Jan Kara06235432008-05-13 19:11:51 -04006322 /*
6323 * We don't need to lock updates but journal_flush() could
6324 * otherwise be livelocked...
6325 */
6326 jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04006327 err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
Jan Kara06235432008-05-13 19:11:51 -04006328 jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
Jan Karaf00c9e42010-09-15 17:38:58 +02006329 if (err)
Hidehiro Kawai7ffe1ea2008-10-10 20:29:21 -04006330 return err;
Jan Kara06235432008-05-13 19:11:51 -04006331 }
Jan Kara957153f2017-04-06 15:40:06 +02006332
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006333 lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
6334 err = dquot_quota_on(sb, type, format_id, path);
Jan Kara957153f2017-04-06 15:40:06 +02006335 if (err) {
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006336 lockdep_set_quota_inode(path->dentry->d_inode,
6337 I_DATA_SEM_NORMAL);
Jan Kara957153f2017-04-06 15:40:06 +02006338 } else {
6339 struct inode *inode = d_inode(path->dentry);
6340 handle_t *handle;
6341
Jan Kara61a92982017-04-24 16:49:16 +02006342 /*
6343 * Set inode flags to prevent userspace from messing with quota
6344 * files. If this fails, we return success anyway since quotas
6345 * are already enabled and this is not a hard failure.
6346 */
Jan Kara957153f2017-04-06 15:40:06 +02006347 inode_lock(inode);
6348 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
6349 if (IS_ERR(handle))
6350 goto unlock_inode;
6351 EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
6352 inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
6353 S_NOATIME | S_IMMUTABLE);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006354 err = ext4_mark_inode_dirty(handle, inode);
Jan Kara957153f2017-04-06 15:40:06 +02006355 ext4_journal_stop(handle);
6356 unlock_inode:
6357 inode_unlock(inode);
6358 }
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006359 return err;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006360}
6361
Aditya Kali7c319d32012-07-22 20:21:31 -04006362static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
6363 unsigned int flags)
6364{
6365 int err;
6366 struct inode *qf_inode;
Jan Karaa2d4a642014-09-11 11:15:15 -04006367 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
Aditya Kali7c319d32012-07-22 20:21:31 -04006368 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
Li Xi689c9582016-01-08 16:01:22 -05006369 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6370 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
Aditya Kali7c319d32012-07-22 20:21:31 -04006371 };
6372
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006373 BUG_ON(!ext4_has_feature_quota(sb));
Aditya Kali7c319d32012-07-22 20:21:31 -04006374
6375 if (!qf_inums[type])
6376 return -EPERM;
6377
Theodore Ts'o8a363972018-12-19 12:29:13 -05006378 qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
Aditya Kali7c319d32012-07-22 20:21:31 -04006379 if (IS_ERR(qf_inode)) {
6380 ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
6381 return PTR_ERR(qf_inode);
6382 }
6383
Jan Karabcb13852013-04-09 09:21:41 -04006384 /* Don't account quota for quota files to avoid recursion */
6385 qf_inode->i_flags |= S_NOQUOTA;
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006386 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
Jan Kara7212b952019-11-01 18:55:38 +01006387 err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
Theodore Ts'odaf647d2016-04-01 01:31:28 -04006388 if (err)
6389 lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
Pan Bian61157b22018-12-03 23:28:02 -05006390 iput(qf_inode);
Aditya Kali7c319d32012-07-22 20:21:31 -04006391
6392 return err;
6393}
6394
6395/* Enable usage tracking for all quota types. */
6396static int ext4_enable_quotas(struct super_block *sb)
6397{
6398 int type, err = 0;
Jan Karaa2d4a642014-09-11 11:15:15 -04006399 unsigned long qf_inums[EXT4_MAXQUOTAS] = {
Aditya Kali7c319d32012-07-22 20:21:31 -04006400 le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
Li Xi689c9582016-01-08 16:01:22 -05006401 le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
6402 le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
Aditya Kali7c319d32012-07-22 20:21:31 -04006403 };
Jan Kara49da9392016-09-05 23:08:16 -04006404 bool quota_mopt[EXT4_MAXQUOTAS] = {
6405 test_opt(sb, USRQUOTA),
6406 test_opt(sb, GRPQUOTA),
6407 test_opt(sb, PRJQUOTA),
6408 };
Aditya Kali7c319d32012-07-22 20:21:31 -04006409
Jan Kara91389242017-08-03 11:25:55 +02006410 sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
Jan Karaa2d4a642014-09-11 11:15:15 -04006411 for (type = 0; type < EXT4_MAXQUOTAS; type++) {
Aditya Kali7c319d32012-07-22 20:21:31 -04006412 if (qf_inums[type]) {
6413 err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
Jan Kara49da9392016-09-05 23:08:16 -04006414 DQUOT_USAGE_ENABLED |
6415 (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
Aditya Kali7c319d32012-07-22 20:21:31 -04006416 if (err) {
6417 ext4_warning(sb,
Theodore Ts'o72ba7452013-01-24 23:24:54 -05006418 "Failed to enable quota tracking "
6419 "(type=%d, err=%d). Please run "
6420 "e2fsck to fix.", type, err);
Junichi Uekawa7f144fd2018-07-29 15:51:52 -04006421 for (type--; type >= 0; type--)
6422 dquot_quota_off(sb, type);
6423
Aditya Kali7c319d32012-07-22 20:21:31 -04006424 return err;
6425 }
6426 }
6427 }
6428 return 0;
6429}
6430
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006431static int ext4_quota_off(struct super_block *sb, int type)
6432{
Jan Kara21f97692011-04-04 15:33:39 -04006433 struct inode *inode = sb_dqopt(sb)->files[type];
6434 handle_t *handle;
Jan Kara957153f2017-04-06 15:40:06 +02006435 int err;
Jan Kara21f97692011-04-04 15:33:39 -04006436
Dmitry Monakhov87009d82010-11-08 13:47:33 -05006437 /* Force all delayed allocation blocks to be allocated.
6438 * Caller already holds s_umount sem */
6439 if (test_opt(sb, DELALLOC))
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006440 sync_filesystem(sb);
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006441
Jan Kara957153f2017-04-06 15:40:06 +02006442 if (!inode || !igrab(inode))
Amir Goldstein0b268592011-05-16 09:59:13 -04006443 goto out;
6444
Jan Kara957153f2017-04-06 15:40:06 +02006445 err = dquot_quota_off(sb, type);
Jan Kara964edf62017-05-21 22:31:23 -04006446 if (err || ext4_has_feature_quota(sb))
Jan Kara957153f2017-04-06 15:40:06 +02006447 goto out_put;
6448
6449 inode_lock(inode);
Jan Kara61a92982017-04-24 16:49:16 +02006450 /*
6451 * Update modification times of quota files when userspace can
6452 * start looking at them. If we fail, we return success anyway since
6453 * this is not a hard failure and quotas are already disabled.
6454 */
Theodore Ts'o9924a922013-02-08 21:59:22 -05006455 handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006456 if (IS_ERR(handle)) {
6457 err = PTR_ERR(handle);
Jan Kara957153f2017-04-06 15:40:06 +02006458 goto out_unlock;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006459 }
Jan Kara957153f2017-04-06 15:40:06 +02006460 EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
6461 inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
Deepa Dinamanieeca7ea2016-11-14 21:40:10 -05006462 inode->i_mtime = inode->i_ctime = current_time(inode);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006463 err = ext4_mark_inode_dirty(handle, inode);
Jan Kara21f97692011-04-04 15:33:39 -04006464 ext4_journal_stop(handle);
Jan Kara957153f2017-04-06 15:40:06 +02006465out_unlock:
6466 inode_unlock(inode);
6467out_put:
Jan Kara964edf62017-05-21 22:31:23 -04006468 lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
Jan Kara957153f2017-04-06 15:40:06 +02006469 iput(inode);
6470 return err;
Jan Kara21f97692011-04-04 15:33:39 -04006471out:
Dmitry Monakhovca0e05e2010-08-01 17:48:36 -04006472 return dquot_quota_off(sb, type);
6473}
6474
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006475/* Read data from quotafile - avoid pagecache and such because we cannot afford
6476 * acquiring the locks... As quota files are never truncated and quota code
Lucas De Marchi25985ed2011-03-30 22:57:33 -03006477 * itself serializes the operations (and no one else should touch the files)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006478 * we don't have to be afraid of races */
Mingming Cao617ba132006-10-11 01:20:53 -07006479static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006480 size_t len, loff_t off)
6481{
6482 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05006483 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006484 int offset = off & (sb->s_blocksize - 1);
6485 int tocopy;
6486 size_t toread;
6487 struct buffer_head *bh;
6488 loff_t i_size = i_size_read(inode);
6489
6490 if (off > i_size)
6491 return 0;
6492 if (off+len > i_size)
6493 len = i_size-off;
6494 toread = len;
6495 while (toread > 0) {
6496 tocopy = sb->s_blocksize - offset < toread ?
6497 sb->s_blocksize - offset : toread;
Theodore Ts'o1c215022014-08-29 20:52:15 -04006498 bh = ext4_bread(NULL, inode, blk, 0);
6499 if (IS_ERR(bh))
6500 return PTR_ERR(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006501 if (!bh) /* A hole? */
6502 memset(data, 0, tocopy);
6503 else
6504 memcpy(data, bh->b_data+offset, tocopy);
6505 brelse(bh);
6506 offset = 0;
6507 toread -= tocopy;
6508 data += tocopy;
6509 blk++;
6510 }
6511 return len;
6512}
6513
6514/* Write to quotafile (we know the transaction is already started and has
6515 * enough credits) */
Mingming Cao617ba132006-10-11 01:20:53 -07006516static ssize_t ext4_quota_write(struct super_block *sb, int type,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006517 const char *data, size_t len, loff_t off)
6518{
6519 struct inode *inode = sb_dqopt(sb)->files[type];
Aneesh Kumar K.V725d26d2008-01-28 23:58:27 -05006520 ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006521 int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006522 int retries = 0;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006523 struct buffer_head *bh;
6524 handle_t *handle = journal_current_handle();
6525
Frank Mayhar03901312009-01-07 00:06:22 -05006526 if (EXT4_SB(sb)->s_journal && !handle) {
Eric Sandeenb31e1552009-06-04 17:36:36 -04006527 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6528 " cancelled because transaction is not started",
Jan Kara9c3013e2007-09-11 15:23:29 -07006529 (unsigned long long)off, (unsigned long long)len);
6530 return -EIO;
6531 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006532 /*
6533 * Since we account only one data block in transaction credits,
6534 * then it is impossible to cross a block boundary.
6535 */
6536 if (sb->s_blocksize - offset < len) {
6537 ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
6538 " cancelled because not block aligned",
6539 (unsigned long long)off, (unsigned long long)len);
6540 return -EIO;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006541 }
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006542
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006543 do {
6544 bh = ext4_bread(handle, inode, blk,
6545 EXT4_GET_BLOCKS_CREATE |
6546 EXT4_GET_BLOCKS_METADATA_NOFAIL);
Masahiro Yamada45586c72020-02-03 17:37:45 -08006547 } while (PTR_ERR(bh) == -ENOSPC &&
Theodore Ts'oc5e298a2015-06-21 01:25:29 -04006548 ext4_should_retry_alloc(inode->i_sb, &retries));
Theodore Ts'o1c215022014-08-29 20:52:15 -04006549 if (IS_ERR(bh))
6550 return PTR_ERR(bh);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006551 if (!bh)
6552 goto out;
liang xie5d601252014-05-12 22:06:43 -04006553 BUFFER_TRACE(bh, "get write access");
Jan Kara62d2b5f2010-07-27 11:56:07 -04006554 err = ext4_journal_get_write_access(handle, bh);
6555 if (err) {
6556 brelse(bh);
Theodore Ts'o1c215022014-08-29 20:52:15 -04006557 return err;
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006558 }
6559 lock_buffer(bh);
6560 memcpy(bh->b_data+offset, data, len);
6561 flush_dcache_page(bh->b_page);
6562 unlock_buffer(bh);
Jan Kara62d2b5f2010-07-27 11:56:07 -04006563 err = ext4_handle_dirty_metadata(handle, NULL, bh);
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006564 brelse(bh);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006565out:
Dmitry Monakhov67eeb562010-03-02 08:08:51 -05006566 if (inode->i_size < off + len) {
6567 i_size_write(inode, off + len);
Mingming Cao617ba132006-10-11 01:20:53 -07006568 EXT4_I(inode)->i_disksize = inode->i_size;
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006569 err2 = ext4_mark_inode_dirty(handle, inode);
6570 if (unlikely(err2 && !err))
6571 err = err2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006572 }
Harshad Shirwadkar4209ae12020-04-26 18:34:37 -07006573 return err ? err : len;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006574}
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006575#endif
6576
Al Viro152a0832010-07-25 00:46:55 +04006577static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
6578 const char *dev_name, void *data)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006579{
Al Viro152a0832010-07-25 00:46:55 +04006580 return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006581}
6582
Jan Karac290ea02015-06-18 16:52:29 +02006583#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
Theodore Ts'o24b58422009-12-07 14:08:51 -05006584static inline void register_as_ext2(void)
6585{
6586 int err = register_filesystem(&ext2_fs_type);
6587 if (err)
6588 printk(KERN_WARNING
6589 "EXT4-fs: Unable to register as ext2 (%d)\n", err);
6590}
6591
6592static inline void unregister_as_ext2(void)
6593{
6594 unregister_filesystem(&ext2_fs_type);
6595}
Theodore Ts'o2035e772011-04-18 17:29:14 -04006596
6597static inline int ext2_feature_set_ok(struct super_block *sb)
6598{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006599 if (ext4_has_unknown_ext2_incompat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006600 return 0;
David Howellsbc98a422017-07-17 08:45:34 +01006601 if (sb_rdonly(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006602 return 1;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006603 if (ext4_has_unknown_ext2_ro_compat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006604 return 0;
6605 return 1;
6606}
Theodore Ts'o24b58422009-12-07 14:08:51 -05006607#else
6608static inline void register_as_ext2(void) { }
6609static inline void unregister_as_ext2(void) { }
Theodore Ts'o2035e772011-04-18 17:29:14 -04006610static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
Theodore Ts'o24b58422009-12-07 14:08:51 -05006611#endif
6612
Theodore Ts'o24b58422009-12-07 14:08:51 -05006613static inline void register_as_ext3(void)
6614{
6615 int err = register_filesystem(&ext3_fs_type);
6616 if (err)
6617 printk(KERN_WARNING
6618 "EXT4-fs: Unable to register as ext3 (%d)\n", err);
6619}
6620
6621static inline void unregister_as_ext3(void)
6622{
6623 unregister_filesystem(&ext3_fs_type);
6624}
Theodore Ts'o2035e772011-04-18 17:29:14 -04006625
6626static inline int ext3_feature_set_ok(struct super_block *sb)
6627{
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006628 if (ext4_has_unknown_ext3_incompat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006629 return 0;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006630 if (!ext4_has_feature_journal(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006631 return 0;
David Howellsbc98a422017-07-17 08:45:34 +01006632 if (sb_rdonly(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006633 return 1;
Darrick J. Wonge2b911c2015-10-17 16:18:43 -04006634 if (ext4_has_unknown_ext3_ro_compat_features(sb))
Theodore Ts'o2035e772011-04-18 17:29:14 -04006635 return 0;
6636 return 1;
6637}
Theodore Ts'o24b58422009-12-07 14:08:51 -05006638
Theodore Ts'o03010a32008-10-10 20:02:48 -04006639static struct file_system_type ext4_fs_type = {
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006640 .owner = THIS_MODULE,
Theodore Ts'o03010a32008-10-10 20:02:48 -04006641 .name = "ext4",
Al Viro152a0832010-07-25 00:46:55 +04006642 .mount = ext4_mount,
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006643 .kill_sb = kill_block_super,
6644 .fs_flags = FS_REQUIRES_DEV,
6645};
Eric W. Biederman7f78e032013-03-02 19:39:14 -08006646MODULE_ALIAS_FS("ext4");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006647
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006648/* Shared across all ext4 file systems */
6649wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006650
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006651static int __init ext4_init_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006652{
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006653 int i, err;
Alex Tomasc9de5602008-01-29 00:19:52 -05006654
Theodore Ts'oe294a532015-08-15 14:59:44 -04006655 ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
Al Viro07c0c5d82012-03-20 22:05:02 -04006656 ext4_li_info = NULL;
6657 mutex_init(&ext4_li_mtx);
6658
Carlos Maiolino9a4c8012012-12-10 16:30:45 -05006659 /* Build-time check for flags consistency */
Dmitry Monakhov12e9b892010-05-16 22:00:00 -04006660 ext4_check_flag_values();
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006661
Jan Karae142d052016-03-08 22:44:50 -05006662 for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006663 init_waitqueue_head(&ext4__ioend_wq[i]);
Eric Sandeene9e3bce2011-02-12 08:17:34 -05006664
Zheng Liu51865fd2012-11-08 21:57:32 -05006665 err = ext4_init_es();
Theodore Ts'o6fd058f2009-05-17 15:38:01 -04006666 if (err)
6667 return err;
Zheng Liu51865fd2012-11-08 21:57:32 -05006668
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006669 err = ext4_init_pending();
6670 if (err)
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006671 goto out7;
6672
6673 err = ext4_init_post_read_processing();
6674 if (err)
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006675 goto out6;
6676
Zheng Liu51865fd2012-11-08 21:57:32 -05006677 err = ext4_init_pageio();
6678 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006679 goto out5;
Zheng Liu51865fd2012-11-08 21:57:32 -05006680
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006681 err = ext4_init_system_zone();
Theodore Ts'obd2d0212010-10-27 21:30:10 -04006682 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006683 goto out4;
Lukas Czerner857ac882010-10-27 21:30:05 -04006684
Theodore Ts'ob5799012015-09-23 12:44:17 -04006685 err = ext4_init_sysfs();
Theodore Ts'odd683142011-02-03 14:33:49 -05006686 if (err)
Theodore Ts'ob5799012015-09-23 12:44:17 -04006687 goto out3;
Lukas Czerner857ac882010-10-27 21:30:05 -04006688
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006689 err = ext4_init_mballoc();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006690 if (err)
Alex Tomasc9de5602008-01-29 00:19:52 -05006691 goto out2;
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006692 err = init_inodecache();
6693 if (err)
6694 goto out1;
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07006695
6696 err = ext4_fc_init_dentry_cache();
6697 if (err)
6698 goto out05;
6699
Theodore Ts'o24b58422009-12-07 14:08:51 -05006700 register_as_ext3();
Theodore Ts'o2035e772011-04-18 17:29:14 -04006701 register_as_ext2();
Theodore Ts'o03010a32008-10-10 20:02:48 -04006702 err = register_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006703 if (err)
6704 goto out;
Lukas Czernerbfff6872010-10-27 21:30:05 -04006705
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006706 return 0;
6707out:
Theodore Ts'o24b58422009-12-07 14:08:51 -05006708 unregister_as_ext2();
6709 unregister_as_ext3();
Harshad Shirwadkaraa75f4d2020-10-15 13:37:57 -07006710out05:
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006711 destroy_inodecache();
6712out1:
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006713 ext4_exit_mballoc();
T Makphaibulchoke9c191f72014-03-18 19:24:49 -04006714out2:
Theodore Ts'ob5799012015-09-23 12:44:17 -04006715 ext4_exit_sysfs();
6716out3:
Theodore Ts'odd683142011-02-03 14:33:49 -05006717 ext4_exit_system_zone();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006718out4:
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006719 ext4_exit_pageio();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006720out5:
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006721 ext4_exit_post_read_processing();
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006722out6:
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006723 ext4_exit_pending();
6724out7:
Zheng Liu51865fd2012-11-08 21:57:32 -05006725 ext4_exit_es();
6726
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006727 return err;
6728}
6729
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006730static void __exit ext4_exit_fs(void)
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006731{
Lukas Czernerbfff6872010-10-27 21:30:05 -04006732 ext4_destroy_lazyinit_thread();
Theodore Ts'o24b58422009-12-07 14:08:51 -05006733 unregister_as_ext2();
6734 unregister_as_ext3();
Theodore Ts'o03010a32008-10-10 20:02:48 -04006735 unregister_filesystem(&ext4_fs_type);
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006736 destroy_inodecache();
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006737 ext4_exit_mballoc();
Theodore Ts'ob5799012015-09-23 12:44:17 -04006738 ext4_exit_sysfs();
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006739 ext4_exit_system_zone();
6740 ext4_exit_pageio();
Eric Biggers22cfe4b2019-07-22 09:26:24 -07006741 ext4_exit_post_read_processing();
Eric Sandeendd12ed142013-07-26 15:21:11 -04006742 ext4_exit_es();
Eric Whitney1dc0aa42018-10-01 14:17:41 -04006743 ext4_exit_pending();
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006744}
6745
6746MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
Theodore Ts'o83982b62009-01-06 14:53:16 -05006747MODULE_DESCRIPTION("Fourth Extended Filesystem");
Dave Kleikampac27a0e2006-10-11 01:20:50 -07006748MODULE_LICENSE("GPL");
Theodore Ts'o7ef79ad2018-04-26 00:44:46 -04006749MODULE_SOFTDEP("pre: crc32c");
Theodore Ts'o5dabfc72010-10-27 21:30:14 -04006750module_init(ext4_init_fs)
6751module_exit(ext4_exit_fs)