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