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