super.c 166 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2
/*
3
 *  linux/fs/ext4/super.c
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 *
 * Copyright (C) 1992, 1993, 1994, 1995
 * Remy Card (card@masi.ibp.fr)
 * Laboratoire MASI - Institut Blaise Pascal
 * Universite Pierre et Marie Curie (Paris VI)
 *
 *  from
 *
 *  linux/fs/minix/inode.c
 *
 *  Copyright (C) 1991, 1992  Linus Torvalds
 *
 *  Big-endian to little-endian byte-swapping/bitmaps by
 *        David S. Miller (davem@caip.rutgers.edu), 1995
 */

#include <linux/module.h>
#include <linux/string.h>
#include <linux/fs.h>
#include <linux/time.h>
24
#include <linux/vmalloc.h>
25
26
27
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
28
#include <linux/backing-dev.h>
29
30
#include <linux/parser.h>
#include <linux/buffer_head.h>
31
#include <linux/exportfs.h>
32
33
34
35
36
37
#include <linux/vfs.h>
#include <linux/random.h>
#include <linux/mount.h>
#include <linux/namei.h>
#include <linux/quotaops.h>
#include <linux/seq_file.h>
Theodore Ts'o's avatar
Theodore Ts'o committed
38
#include <linux/ctype.h>
Vignesh Babu's avatar
Vignesh Babu committed
39
#include <linux/log2.h>
40
#include <linux/crc16.h>
41
#include <linux/dax.h>
Dan Magenheimer's avatar
Dan Magenheimer committed
42
#include <linux/cleancache.h>
43
#include <linux/uaccess.h>
44
#include <linux/iversion.h>
45

46
47
48
#include <linux/kthread.h>
#include <linux/freezer.h>

49
#include "ext4.h"
50
#include "ext4_extents.h"	/* Needed for trace points definition */
51
#include "ext4_jbd2.h"
52
53
#include "xattr.h"
#include "acl.h"
54
#include "mballoc.h"
55
#include "fsmap.h"
56

57
58
59
#define CREATE_TRACE_POINTS
#include <trace/events/ext4.h>

60
61
static struct ext4_lazy_init *ext4_li_info;
static struct mutex ext4_li_mtx;
62
static struct ratelimit_state ext4_mount_msg_ratelimit;
63

64
static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
65
			     unsigned long journal_devnum);
66
static int ext4_show_options(struct seq_file *seq, struct dentry *root);
67
static int ext4_commit_super(struct super_block *sb, int sync);
68
69
70
71
static void ext4_mark_recovery_complete(struct super_block *sb,
					struct ext4_super_block *es);
static void ext4_clear_journal_err(struct super_block *sb,
				   struct ext4_super_block *es);
72
static int ext4_sync_fs(struct super_block *sb, int wait);
73
74
static int ext4_remount(struct super_block *sb, int *flags, char *data);
static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
75
76
static int ext4_unfreeze(struct super_block *sb);
static int ext4_freeze(struct super_block *sb);
Al Viro's avatar
Al Viro committed
77
78
static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
		       const char *dev_name, void *data);
79
80
static inline int ext2_feature_set_ok(struct super_block *sb);
static inline int ext3_feature_set_ok(struct super_block *sb);
81
static int ext4_feature_set_ok(struct super_block *sb, int readonly);
82
83
static void ext4_destroy_lazyinit_thread(void);
static void ext4_unregister_li_request(struct super_block *sb);
84
static void ext4_clear_request_list(void);
85
86
static struct inode *ext4_get_journal_inode(struct super_block *sb,
					    unsigned int journal_inum);
87

Jan Kara's avatar
Jan Kara committed
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Lock ordering
 *
 * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
 * i_mmap_rwsem (inode->i_mmap_rwsem)!
 *
 * page fault path:
 * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
 *   page lock -> i_data_sem (rw)
 *
 * buffered write path:
 * sb_start_write -> i_mutex -> mmap_sem
 * sb_start_write -> i_mutex -> transaction start -> page lock ->
 *   i_data_sem (rw)
 *
 * truncate:
 * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
 *   i_mmap_rwsem (w) -> page lock
 * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
 *   transaction start -> i_data_sem (rw)
 *
 * direct IO:
 * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) -> mmap_sem
 * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) ->
 *   transaction start -> i_data_sem (rw)
 *
 * writepages:
 * transaction start -> page lock(s) -> i_data_sem (rw)
 */

Jan Kara's avatar
Jan Kara committed
118
#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
119
120
121
122
123
124
125
static struct file_system_type ext2_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext2",
	.mount		= ext4_mount,
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
126
MODULE_ALIAS_FS("ext2");
127
MODULE_ALIAS("ext2");
128
129
130
131
132
133
#define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
#else
#define IS_EXT2_SB(sb) (0)
#endif


134
135
136
static struct file_system_type ext3_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext3",
Al Viro's avatar
Al Viro committed
137
	.mount		= ext4_mount,
138
139
140
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
141
MODULE_ALIAS_FS("ext3");
142
MODULE_ALIAS("ext3");
143
#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
Laurent Vivier's avatar
Laurent Vivier committed
144

145
146
147
static int ext4_verify_csum_type(struct super_block *sb,
				 struct ext4_super_block *es)
{
148
	if (!ext4_has_feature_metadata_csum(sb))
149
150
151
152
153
		return 1;

	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
}

154
155
156
157
158
159
160
161
162
163
164
165
static __le32 ext4_superblock_csum(struct super_block *sb,
				   struct ext4_super_block *es)
{
	struct ext4_sb_info *sbi = EXT4_SB(sb);
	int offset = offsetof(struct ext4_super_block, s_checksum);
	__u32 csum;

	csum = ext4_chksum(sbi, ~0, (char *)es, offset);

	return cpu_to_le32(csum);
}

166
167
static int ext4_superblock_csum_verify(struct super_block *sb,
				       struct ext4_super_block *es)
168
{
169
	if (!ext4_has_metadata_csum(sb))
170
171
172
173
174
		return 1;

	return es->s_checksum == ext4_superblock_csum(sb, es);
}

175
void ext4_superblock_csum_set(struct super_block *sb)
176
{
177
178
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;

179
	if (!ext4_has_metadata_csum(sb))
180
181
182
183
184
		return;

	es->s_checksum = ext4_superblock_csum(sb, es);
}

185
186
187
188
void *ext4_kvmalloc(size_t size, gfp_t flags)
{
	void *ret;

189
	ret = kmalloc(size, flags | __GFP_NOWARN);
190
191
192
193
194
195
196
197
198
	if (!ret)
		ret = __vmalloc(size, flags, PAGE_KERNEL);
	return ret;
}

void *ext4_kvzalloc(size_t size, gfp_t flags)
{
	void *ret;

199
	ret = kzalloc(size, flags | __GFP_NOWARN);
200
201
202
203
204
	if (!ret)
		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
	return ret;
}

205
206
ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
			       struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
207
{
208
	return le32_to_cpu(bg->bg_block_bitmap_lo) |
209
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
210
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
211
212
}

213
214
ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
			       struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
215
{
216
	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
217
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
218
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
219
220
}

221
222
ext4_fsblk_t ext4_inode_table(struct super_block *sb,
			      struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
223
{
224
	return le32_to_cpu(bg->bg_inode_table_lo) |
225
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
226
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
227
228
}

229
230
__u32 ext4_free_group_clusters(struct super_block *sb,
			       struct ext4_group_desc *bg)
231
232
233
{
	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
234
		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
235
236
237
238
239
240
241
}

__u32 ext4_free_inodes_count(struct super_block *sb,
			      struct ext4_group_desc *bg)
{
	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
242
		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
243
244
245
246
247
248
249
}

__u32 ext4_used_dirs_count(struct super_block *sb,
			      struct ext4_group_desc *bg)
{
	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
250
		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
251
252
253
254
255
256
257
}

__u32 ext4_itable_unused_count(struct super_block *sb,
			      struct ext4_group_desc *bg)
{
	return le16_to_cpu(bg->bg_itable_unused_lo) |
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
258
		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
259
260
}

261
262
void ext4_block_bitmap_set(struct super_block *sb,
			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivier's avatar
Laurent Vivier committed
263
{
264
	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
265
266
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivier's avatar
Laurent Vivier committed
267
268
}

269
270
void ext4_inode_bitmap_set(struct super_block *sb,
			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivier's avatar
Laurent Vivier committed
271
{
272
	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
273
274
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
Laurent Vivier's avatar
Laurent Vivier committed
275
276
}

277
278
void ext4_inode_table_set(struct super_block *sb,
			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
Laurent Vivier's avatar
Laurent Vivier committed
279
{
280
	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
281
282
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
Laurent Vivier's avatar
Laurent Vivier committed
283
284
}

285
286
void ext4_free_group_clusters_set(struct super_block *sb,
				  struct ext4_group_desc *bg, __u32 count)
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
{
	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
}

void ext4_free_inodes_set(struct super_block *sb,
			  struct ext4_group_desc *bg, __u32 count)
{
	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
}

void ext4_used_dirs_set(struct super_block *sb,
			  struct ext4_group_desc *bg, __u32 count)
{
	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
}

void ext4_itable_unused_set(struct super_block *sb,
			  struct ext4_group_desc *bg, __u32 count)
{
	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
}

317

318
319
320
321
322
323
static void __save_error_info(struct super_block *sb, const char *func,
			    unsigned int line)
{
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;

	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
324
325
	if (bdev_read_only(sb->s_bdev))
		return;
326
327
328
329
330
331
332
333
334
335
336
337
	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
	es->s_last_error_time = cpu_to_le32(get_seconds());
	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
	es->s_last_error_line = cpu_to_le32(line);
	if (!es->s_first_error_time) {
		es->s_first_error_time = es->s_last_error_time;
		strncpy(es->s_first_error_func, func,
			sizeof(es->s_first_error_func));
		es->s_first_error_line = cpu_to_le32(line);
		es->s_first_error_ino = es->s_last_error_ino;
		es->s_first_error_block = es->s_last_error_block;
	}
338
339
340
341
342
343
	/*
	 * Start the daily error reporting function if it hasn't been
	 * started already
	 */
	if (!es->s_error_count)
		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
344
	le32_add_cpu(&es->s_error_count, 1);
345
346
347
348
349
350
351
352
353
}

static void save_error_info(struct super_block *sb, const char *func,
			    unsigned int line)
{
	__save_error_info(sb, func, line);
	ext4_commit_super(sb, 1);
}

354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * The del_gendisk() function uninitializes the disk-specific data
 * structures, including the bdi structure, without telling anyone
 * else.  Once this happens, any attempt to call mark_buffer_dirty()
 * (for example, by ext4_commit_super), will cause a kernel OOPS.
 * This is a kludge to prevent these oops until we can put in a proper
 * hook in del_gendisk() to inform the VFS and file system layers.
 */
static int block_device_ejected(struct super_block *sb)
{
	struct inode *bd_inode = sb->s_bdev->bd_inode;
	struct backing_dev_info *bdi = inode_to_bdi(bd_inode);

	return bdi->dev == NULL;
}

Bobi Jam's avatar
Bobi Jam committed
370
371
372
373
374
static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
{
	struct super_block		*sb = journal->j_private;
	struct ext4_sb_info		*sbi = EXT4_SB(sb);
	int				error = is_journal_aborted(journal);
375
	struct ext4_journal_cb_entry	*jce;
Bobi Jam's avatar
Bobi Jam committed
376

377
	BUG_ON(txn->t_state == T_FINISHED);
378
379
380

	ext4_process_freed_data(sb, txn->t_tid);

Bobi Jam's avatar
Bobi Jam committed
381
	spin_lock(&sbi->s_md_lock);
382
383
384
	while (!list_empty(&txn->t_private_list)) {
		jce = list_entry(txn->t_private_list.next,
				 struct ext4_journal_cb_entry, jce_list);
Bobi Jam's avatar
Bobi Jam committed
385
386
387
388
389
390
391
		list_del_init(&jce->jce_list);
		spin_unlock(&sbi->s_md_lock);
		jce->jce_func(sb, jce, error);
		spin_lock(&sbi->s_md_lock);
	}
	spin_unlock(&sbi->s_md_lock);
}
392

393
394
395
396
/* Deal with the reporting of failure conditions on a filesystem such as
 * inconsistencies detected or read IO failures.
 *
 * On ext2, we can store the error state of the filesystem in the
397
 * superblock.  That is not possible on ext4, because we may have other
398
399
400
401
402
 * write ordering constraints on the superblock which prevent us from
 * writing it out straight away; and given that the journal is about to
 * be aborted, we can't rely on the current, or future, transactions to
 * write out the superblock safely.
 *
403
 * We'll just use the jbd2_journal_abort() error code to record an error in
404
 * the journal instead.  On recovery, the journal will complain about
405
406
407
 * that error until we've noted it down and cleared it.
 */

408
static void ext4_handle_error(struct super_block *sb)
409
{
410
	if (sb_rdonly(sb))
411
412
		return;

413
	if (!test_opt(sb, ERRORS_CONT)) {
414
		journal_t *journal = EXT4_SB(sb)->s_journal;
415

416
		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
417
		if (journal)
418
			jbd2_journal_abort(journal, -EIO);
419
	}
420
	if (test_opt(sb, ERRORS_RO)) {
421
		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
422
423
424
425
426
		/*
		 * Make sure updated value of ->s_mount_flags will be visible
		 * before ->s_flags update
		 */
		smp_wmb();
427
		sb->s_flags |= SB_RDONLY;
428
	}
429
430
431
432
	if (test_opt(sb, ERRORS_PANIC)) {
		if (EXT4_SB(sb)->s_journal &&
		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
			return;
433
		panic("EXT4-fs (device %s): panic forced after error\n",
434
			sb->s_id);
435
	}
436
437
}

438
439
440
441
#define ext4_error_ratelimit(sb)					\
		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
			     "EXT4-fs error")

442
void __ext4_error(struct super_block *sb, const char *function,
443
		  unsigned int line, const char *fmt, ...)
444
{
Joe Perches's avatar
Joe Perches committed
445
	struct va_format vaf;
446
447
	va_list args;

448
449
450
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

451
452
453
454
455
456
457
458
459
	if (ext4_error_ratelimit(sb)) {
		va_start(args, fmt);
		vaf.fmt = fmt;
		vaf.va = &args;
		printk(KERN_CRIT
		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
		       sb->s_id, function, line, current->comm, &vaf);
		va_end(args);
	}
460
	save_error_info(sb, function, line);
461
	ext4_handle_error(sb);
462
463
}

464
465
466
void __ext4_error_inode(struct inode *inode, const char *function,
			unsigned int line, ext4_fsblk_t block,
			const char *fmt, ...)
467
468
{
	va_list args;
469
	struct va_format vaf;
470
	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
471

472
473
474
	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
		return;

475
476
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
	es->s_last_error_block = cpu_to_le64(block);
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
	if (ext4_error_ratelimit(inode->i_sb)) {
		va_start(args, fmt);
		vaf.fmt = fmt;
		vaf.va = &args;
		if (block)
			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
			       "inode #%lu: block %llu: comm %s: %pV\n",
			       inode->i_sb->s_id, function, line, inode->i_ino,
			       block, current->comm, &vaf);
		else
			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
			       "inode #%lu: comm %s: %pV\n",
			       inode->i_sb->s_id, function, line, inode->i_ino,
			       current->comm, &vaf);
		va_end(args);
	}
493
	save_error_info(inode->i_sb, function, line);
494
495
496
	ext4_handle_error(inode->i_sb);
}

497
498
499
void __ext4_error_file(struct file *file, const char *function,
		       unsigned int line, ext4_fsblk_t block,
		       const char *fmt, ...)
500
501
{
	va_list args;
502
	struct va_format vaf;
503
	struct ext4_super_block *es;
Al Viro's avatar
Al Viro committed
504
	struct inode *inode = file_inode(file);
505
506
	char pathname[80], *path;

507
508
509
	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
		return;

510
511
	es = EXT4_SB(inode->i_sb)->s_es;
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
512
	if (ext4_error_ratelimit(inode->i_sb)) {
Miklos Szeredi's avatar
Miklos Szeredi committed
513
		path = file_path(file, pathname, sizeof(pathname));
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
		if (IS_ERR(path))
			path = "(unknown)";
		va_start(args, fmt);
		vaf.fmt = fmt;
		vaf.va = &args;
		if (block)
			printk(KERN_CRIT
			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
			       "block %llu: comm %s: path %s: %pV\n",
			       inode->i_sb->s_id, function, line, inode->i_ino,
			       block, current->comm, path, &vaf);
		else
			printk(KERN_CRIT
			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
			       "comm %s: path %s: %pV\n",
			       inode->i_sb->s_id, function, line, inode->i_ino,
			       current->comm, path, &vaf);
		va_end(args);
	}
533
	save_error_info(inode->i_sb, function, line);
534
535
536
	ext4_handle_error(inode->i_sb);
}

537
538
const char *ext4_decode_error(struct super_block *sb, int errno,
			      char nbuf[16])
539
540
541
542
{
	char *errstr = NULL;

	switch (errno) {
543
544
545
546
547
548
	case -EFSCORRUPTED:
		errstr = "Corrupt filesystem";
		break;
	case -EFSBADCRC:
		errstr = "Filesystem failed CRC";
		break;
549
550
551
552
553
554
555
	case -EIO:
		errstr = "IO failure";
		break;
	case -ENOMEM:
		errstr = "Out of memory";
		break;
	case -EROFS:
556
557
		if (!sb || (EXT4_SB(sb)->s_journal &&
			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
			errstr = "Journal has aborted";
		else
			errstr = "Readonly filesystem";
		break;
	default:
		/* If the caller passed in an extra buffer for unknown
		 * errors, textualise them now.  Else we just return
		 * NULL. */
		if (nbuf) {
			/* Check for truncated error codes... */
			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
				errstr = nbuf;
		}
		break;
	}

	return errstr;
}

577
/* __ext4_std_error decodes expected errors from journaling functions
578
579
 * automatically and invokes the appropriate error response.  */

580
581
void __ext4_std_error(struct super_block *sb, const char *function,
		      unsigned int line, int errno)
582
583
584
585
{
	char nbuf[16];
	const char *errstr;

586
587
588
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

589
590
591
	/* Special case: if the error is EROFS, and we're not already
	 * inside a transaction, then there's really no point in logging
	 * an error. */
592
	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
593
594
		return;

595
596
597
598
599
	if (ext4_error_ratelimit(sb)) {
		errstr = ext4_decode_error(sb, errno, nbuf);
		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
		       sb->s_id, function, line, errstr);
	}
600

601
	save_error_info(sb, function, line);
602
	ext4_handle_error(sb);
603
604
605
}

/*
606
 * ext4_abort is a much stronger failure handler than ext4_error.  The
607
608
609
610
611
612
613
614
 * abort function may be used to deal with unrecoverable failures such
 * as journal IO errors or ENOMEM at a critical moment in log management.
 *
 * We unconditionally force the filesystem into an ABORT|READONLY state,
 * unless the error response on the fs has been set to panic in which
 * case we take the easy way out and panic immediately.
 */

615
void __ext4_abort(struct super_block *sb, const char *function,
616
		unsigned int line, const char *fmt, ...)
617
{
618
	struct va_format vaf;
619
620
	va_list args;

621
622
623
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

624
	save_error_info(sb, function, line);
625
	va_start(args, fmt);
626
627
628
629
	vaf.fmt = fmt;
	vaf.va = &args;
	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
	       sb->s_id, function, line, &vaf);
630
631
	va_end(args);

632
	if (sb_rdonly(sb) == 0) {
633
634
		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
635
636
637
638
639
		/*
		 * Make sure updated value of ->s_mount_flags will be visible
		 * before ->s_flags update
		 */
		smp_wmb();
640
		sb->s_flags |= SB_RDONLY;
641
642
643
644
		if (EXT4_SB(sb)->s_journal)
			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
		save_error_info(sb, function, line);
	}
645
646
647
648
	if (test_opt(sb, ERRORS_PANIC)) {
		if (EXT4_SB(sb)->s_journal &&
		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
			return;
649
		panic("EXT4-fs panic from previous error\n");
650
	}
651
652
}

653
654
void __ext4_msg(struct super_block *sb,
		const char *prefix, const char *fmt, ...)
655
{
Joe Perches's avatar
Joe Perches committed
656
	struct va_format vaf;
657
658
	va_list args;

659
660
661
	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
		return;

662
	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
663
664
665
	vaf.fmt = fmt;
	vaf.va = &args;
	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
666
667
668
	va_end(args);
}

669
670
671
672
#define ext4_warning_ratelimit(sb)					\
		___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),	\
			     "EXT4-fs warning")

673
void __ext4_warning(struct super_block *sb, const char *function,
674
		    unsigned int line, const char *fmt, ...)
675
{
Joe Perches's avatar
Joe Perches committed
676
	struct va_format vaf;
677
678
	va_list args;

679
	if (!ext4_warning_ratelimit(sb))
680
681
		return;

682
	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
683
684
685
686
	vaf.fmt = fmt;
	vaf.va = &args;
	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
	       sb->s_id, function, line, &vaf);
687
688
689
	va_end(args);
}

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
void __ext4_warning_inode(const struct inode *inode, const char *function,
			  unsigned int line, const char *fmt, ...)
{
	struct va_format vaf;
	va_list args;

	if (!ext4_warning_ratelimit(inode->i_sb))
		return;

	va_start(args, fmt);
	vaf.fmt = fmt;
	vaf.va = &args;
	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
	       "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
	       function, line, inode->i_ino, current->comm, &vaf);
	va_end(args);
}

708
709
710
711
void __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, ...)
712
713
714
__releases(bitlock)
__acquires(bitlock)
{
Joe Perches's avatar
Joe Perches committed
715
	struct va_format vaf;
716
717
718
	va_list args;
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;

719
720
721
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

722
723
724
	es->s_last_error_ino = cpu_to_le32(ino);
	es->s_last_error_block = cpu_to_le64(block);
	__save_error_info(sb, function, line);
Joe Perches's avatar
Joe Perches committed
725

726
727
728
729
730
731
732
733
734
735
736
737
738
739
	if (ext4_error_ratelimit(sb)) {
		va_start(args, fmt);
		vaf.fmt = fmt;
		vaf.va = &args;
		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
		       sb->s_id, function, line, grp);
		if (ino)
			printk(KERN_CONT "inode %lu: ", ino);
		if (block)
			printk(KERN_CONT "block %llu:",
			       (unsigned long long) block);
		printk(KERN_CONT "%pV\n", &vaf);
		va_end(args);
	}
740
741

	if (test_opt(sb, ERRORS_CONT)) {
742
		ext4_commit_super(sb, 0);
743
744
		return;
	}
745

746
	ext4_unlock_group(sb, grp);
747
	ext4_commit_super(sb, 1);
748
749
750
751
752
753
754
	ext4_handle_error(sb);
	/*
	 * We only get here in the ERRORS_RO case; relocking the group
	 * may be dangerous, but nothing bad will happen since the
	 * filesystem will have already been marked read/only and the
	 * journal has been aborted.  We return 1 as a hint to callers
	 * who might what to use the return value from
Lucas De Marchi's avatar
Lucas De Marchi committed
755
	 * ext4_grp_locked_error() to distinguish between the
756
757
758
759
760
761
762
763
	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
	 * aggressively from the ext4 function in question, with a
	 * more appropriate error code.
	 */
	ext4_lock_group(sb, grp);
	return;
}

764
void ext4_update_dynamic_rev(struct super_block *sb)
765
{
766
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
767

768
	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
769
770
		return;

771
	ext4_warning(sb,
772
773
		     "updating to rev %d because of new feature flag, "
		     "running e2fsck is recommended",
774
		     EXT4_DYNAMIC_REV);
775

776
777
778
	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
779
780
781
782
783
784
785
786
787
788
789
790
791
	/* leave es->s_feature_*compat flags alone */
	/* es->s_uuid will be set by e2fsck if empty */

	/*
	 * The rest of the superblock fields should be zero, and if not it
	 * means they are likely already in use, so leave them alone.  We
	 * can leave it up to e2fsck to clean up any inconsistencies there.
	 */
}

/*
 * Open the external journal device
 */
792
static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
793
794
795
796
{
	struct block_device *bdev;
	char b[BDEVNAME_SIZE];

797
	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
798
799
800
801
802
	if (IS_ERR(bdev))
		goto fail;
	return bdev;

fail:
803
	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
804
805
806
807
808
809
810
			__bdevname(dev, b), PTR_ERR(bdev));
	return NULL;
}

/*
 * Release the journal device
 */
Al Viro's avatar
Al Viro committed
811
static void ext4_blkdev_put(struct block_device *bdev)
812
{
Al Viro's avatar
Al Viro committed
813
	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
814
815
}

Al Viro's avatar
Al Viro committed
816
static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
817
818
819
820
{
	struct block_device *bdev;
	bdev = sbi->journal_bdev;
	if (bdev) {
Al Viro's avatar
Al Viro committed
821
		ext4_blkdev_put(bdev);
822
823
824
825
826
827
		sbi->journal_bdev = NULL;
	}
}

static inline struct inode *orphan_list_entry(struct list_head *l)
{
828
	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
829
830
}

831
static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
832
833
834
{
	struct list_head *l;

835
836
	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
		 le32_to_cpu(sbi->s_es->s_last_orphan));
837
838
839
840
841
842
843
844
845
846
847
848

	printk(KERN_ERR "sb_info orphan list:\n");
	list_for_each(l, &sbi->s_orphan) {
		struct inode *inode = orphan_list_entry(l);
		printk(KERN_ERR "  "
		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
		       inode->i_sb->s_id, inode->i_ino, inode,
		       inode->i_mode, inode->i_nlink,
		       NEXT_ORPHAN(inode));
	}
}

849
850
851
852
853
854
855
#ifdef CONFIG_QUOTA
static int ext4_quota_off(struct super_block *sb, int type);

static inline void ext4_quota_off_umount(struct super_block *sb)
{
	int type;

856
857
858
	/* Use our quota_off function to clear inode flags etc. */
	for (type = 0; type < EXT4_MAXQUOTAS; type++)
		ext4_quota_off(sb, type);
859
860
861
862
863
864
865
}
#else
static inline void ext4_quota_off_umount(struct super_block *sb)
{
}
#endif

866
static void ext4_put_super(struct super_block *sb)
867
{
868
869
	struct ext4_sb_info *sbi = EXT4_SB(sb);
	struct ext4_super_block *es = sbi->s_es;
870
	int aborted = 0;
871
	int i, err;
872

873
	ext4_unregister_li_request(sb);
874
	ext4_quota_off_umount(sb);
875

876
	destroy_workqueue(sbi->rsv_conversion_wq);
877

878
	if (sbi->s_journal) {
879
		aborted = is_journal_aborted(sbi->s_journal);
880
881
		err = jbd2_journal_destroy(sbi->s_journal);
		sbi->s_journal = NULL;
882
		if ((err < 0) && !aborted)
883
			ext4_abort(sb, "Couldn't clean up the journal");
884
	}
885

886
	ext4_unregister_sysfs(sb);
887
	ext4_es_unregister_shrinker(sbi);
888
	del_timer_sync(&sbi->s_err_report);
889
890
891
892
	ext4_release_system_zone(sb);
	ext4_mb_release(sb);
	ext4_ext_release(sb);

893
	if (!sb_rdonly(sb) && !aborted) {
894
		ext4_clear_feature_journal_needs_recovery(sb);
895
896
		es->s_state = cpu_to_le16(sbi->s_mount_state);
	}
897
	if (!sb_rdonly(sb))
898
899
		ext4_commit_super(sb, 1);

900
901
	for (i = 0; i < sbi->s_gdb_count; i++)
		brelse(sbi->s_group_desc[i]);
Al Viro's avatar
Al Viro committed
902
903
	kvfree(sbi->s_group_desc);
	kvfree(sbi->s_flex_groups);
904
	percpu_counter_destroy(&sbi->s_freeclusters_counter);
905
906
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
907
	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
908
	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
909
#ifdef CONFIG_QUOTA
Jan Kara's avatar
Jan Kara committed
910
	for (i = 0; i < EXT4_MAXQUOTAS; i++)
911
912
913
914
915
916
917
918
919
920
921
		kfree(sbi->s_qf_names[i]);
#endif

	/* Debugging code just in case the in-memory inode orphan list
	 * isn't empty.  The on-disk one can be non-empty if we've
	 * detected an error and taken the fs readonly, but the
	 * in-memory list had better be clean by this point. */
	if (!list_empty(&sbi->s_orphan))
		dump_orphan_list(sb, sbi);
	J_ASSERT(list_empty(&sbi->s_orphan));

922
	sync_blockdev(sb->s_bdev);
923
	invalidate_bdev(sb->s_bdev);
924
925
926
927
928
929
930
	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
		/*
		 * Invalidate the journal device's buffers.  We don't want them
		 * floating about in memory - the physical journal device may
		 * hotswapped, and it breaks the `ro-after' testing code.
		 */
		sync_blockdev(sbi->journal_bdev);
931
		invalidate_bdev(sbi->journal_bdev);
932
		ext4_blkdev_remove(sbi);
933
	}
934
935
936
937
	if (sbi->s_ea_inode_cache) {
		ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
		sbi->s_ea_inode_cache = NULL;
	}
938
939
940
	if (sbi->s_ea_block_cache) {
		ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
		sbi->s_ea_block_cache = NULL;
941
	}
942
943
	if (sbi->s_mmp_tsk)
		kthread_stop(sbi->s_mmp_tsk);
944
	brelse(sbi->s_sbh);
945
	sb->s_fs_info = NULL;
Theodore Ts'o's avatar
Theodore Ts'o committed
946
947
948
949
950
951
	/*
	 * Now that we are completely done shutting down the
	 * superblock, we need to actually destroy the kobject.
	 */
	kobject_put(&sbi->s_kobj);
	wait_for_completion(&sbi->s_kobj_unregister);
952
953
	if (sbi->s_chksum_driver)
		crypto_free_shash(sbi->s_chksum_driver);
954
	kfree(sbi->s_blockgroup_lock);
955
	fs_put_dax(sbi->s_daxdev);
956
957
958
	kfree(sbi);
}

959
static struct kmem_cache *ext4_inode_cachep;
960
961
962
963

/*
 * Called inside transaction, so use GFP_NOFS
 */
964
static struct inode *ext4_alloc_inode(struct super_block *sb)
965
{
966
	struct ext4_inode_info *ei;
967

968
	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
969
970
	if (!ei)
		return NULL;
971

972
	inode_set_iversion(&ei->vfs_inode, 1);
973
	spin_lock_init(&ei->i_raw_lock);
974
975
	INIT_LIST_HEAD(&ei->i_prealloc_list);
	spin_lock_init(&ei->i_prealloc_lock);
976
977
	ext4_es_init_tree(&ei->i_es_tree);
	rwlock_init(&ei->i_es_lock);
978
	INIT_LIST_HEAD(&ei->i_es_list);
979
	ei->i_es_all_nr = 0;
980
	ei->i_es_shk_nr = 0;
981
	ei->i_es_shrink_lblk = 0;
982
	ei->i_reserved_data_blocks = 0;
983
	ei->i_da_metadata_calc_len = 0;
984
	ei->i_da_metadata_calc_last_lblock = 0;
985
	spin_lock_init(&(ei->i_block_reservation_lock));
986
987
#ifdef CONFIG_QUOTA
	ei->i_reserved_quota = 0;
988
	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
989
#endif
990
	ei->jinode = NULL;
991
	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
992
	spin_lock_init(&ei->i_completed_io_lock);
993
994
	ei->i_sync_tid = 0;
	ei->i_datasync_tid = 0;
995
	atomic_set(&ei->i_unwritten, 0);
996
	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
997
998
999
	return &ei->vfs_inode;
}

1000
static int ext4_drop_inode(struct inode *inode)