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