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