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