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