super.c 169 KB
Newer Older
1
/*
2
 *  linux/fs/ext4/super.c
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 *
 * 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>
23
#include <linux/vmalloc.h>
24
25
26
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
27
#include <linux/backing-dev.h>
28
29
#include <linux/parser.h>
#include <linux/buffer_head.h>
30
#include <linux/exportfs.h>
31
32
33
34
35
36
#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
37
#include <linux/ctype.h>
Vignesh Babu's avatar
Vignesh Babu committed
38
#include <linux/log2.h>
39
#include <linux/crc16.h>
40
#include <linux/dax.h>
Dan Magenheimer's avatar
Dan Magenheimer committed
41
#include <linux/cleancache.h>
42
#include <linux/uaccess.h>
43

44
45
46
#include <linux/kthread.h>
#include <linux/freezer.h>

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

55
56
57
#define CREATE_TRACE_POINTS
#include <trace/events/ext4.h>

58
59
static struct ext4_lazy_init *ext4_li_info;
static struct mutex ext4_li_mtx;
60
static struct ratelimit_state ext4_mount_msg_ratelimit;
61

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

Jan Kara's avatar
Jan Kara committed
86
87
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
/*
 * 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
116
#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
117
118
119
120
121
122
123
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,
};
124
MODULE_ALIAS_FS("ext2");
125
MODULE_ALIAS("ext2");
126
127
128
129
130
131
#define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
#else
#define IS_EXT2_SB(sb) (0)
#endif


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

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

	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
}

152
153
154
155
156
157
158
159
160
161
162
163
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);
}

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

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

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

177
	if (!ext4_has_metadata_csum(sb))
178
179
180
181
182
		return;

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

183
184
185
186
void *ext4_kvmalloc(size_t size, gfp_t flags)
{
	void *ret;

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

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

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

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

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

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

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

__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 ?
240
		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
241
242
243
244
245
246
247
}

__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 ?
248
		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
249
250
251
252
253
254
255
}

__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 ?
256
		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
257
258
}

259
260
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
261
{
262
	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
263
264
	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
265
266
}

267
268
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
269
{
270
	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
271
272
	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
273
274
}

275
276
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
277
{
278
	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
279
280
	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
281
282
}

283
284
void ext4_free_group_clusters_set(struct super_block *sb,
				  struct ext4_group_desc *bg, __u32 count)
285
286
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
{
	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);
}

315

316
317
318
319
320
321
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;
322
323
	if (bdev_read_only(sb->s_bdev))
		return;
324
325
326
327
328
329
330
331
332
333
334
335
	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;
	}
336
337
338
339
340
341
	/*
	 * 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);
342
	le32_add_cpu(&es->s_error_count, 1);
343
344
345
346
347
348
349
350
351
}

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);
}

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * 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
368
369
370
371
372
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);
373
	struct ext4_journal_cb_entry	*jce;
Bobi Jam's avatar
Bobi Jam committed
374

375
	BUG_ON(txn->t_state == T_FINISHED);
376
377
378

	ext4_process_freed_data(sb, txn->t_tid);

Bobi Jam's avatar
Bobi Jam committed
379
	spin_lock(&sbi->s_md_lock);
380
381
382
	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
383
384
385
386
387
388
389
		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);
}
390

391
392
393
394
/* 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
395
 * superblock.  That is not possible on ext4, because we may have other
396
397
398
399
400
 * 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.
 *
401
 * We'll just use the jbd2_journal_abort() error code to record an error in
402
 * the journal instead.  On recovery, the journal will complain about
403
404
405
 * that error until we've noted it down and cleared it.
 */

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

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

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

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

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

446
447
448
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

449
450
451
452
453
454
455
456
457
	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);
	}
458
	save_error_info(sb, function, line);
459
	ext4_handle_error(sb);
460
461
}

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

470
471
472
	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
		return;

473
474
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
	es->s_last_error_block = cpu_to_le64(block);
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
	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);
	}
491
	save_error_info(inode->i_sb, function, line);
492
493
494
	ext4_handle_error(inode->i_sb);
}

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

505
506
507
	if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
		return;

508
509
	es = EXT4_SB(inode->i_sb)->s_es;
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
510
	if (ext4_error_ratelimit(inode->i_sb)) {
Miklos Szeredi's avatar
Miklos Szeredi committed
511
		path = file_path(file, pathname, sizeof(pathname));
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
		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);
	}
531
	save_error_info(inode->i_sb, function, line);
532
533
534
	ext4_handle_error(inode->i_sb);
}

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

	switch (errno) {
541
542
543
544
545
546
	case -EFSCORRUPTED:
		errstr = "Corrupt filesystem";
		break;
	case -EFSBADCRC:
		errstr = "Filesystem failed CRC";
		break;
547
548
549
550
551
552
553
	case -EIO:
		errstr = "IO failure";
		break;
	case -ENOMEM:
		errstr = "Out of memory";
		break;
	case -EROFS:
554
555
		if (!sb || (EXT4_SB(sb)->s_journal &&
			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
			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;
}

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

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

584
585
586
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

587
588
589
	/* 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. */
590
	if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
591
592
		return;

593
594
595
596
597
	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);
	}
598

599
	save_error_info(sb, function, line);
600
	ext4_handle_error(sb);
601
602
603
}

/*
604
 * ext4_abort is a much stronger failure handler than ext4_error.  The
605
606
607
608
609
610
611
612
 * 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.
 */

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

619
620
621
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

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

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

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

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

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

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

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

677
	if (!ext4_warning_ratelimit(sb))
678
679
		return;

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

688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
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);
}

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

717
718
719
	if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
		return;

720
721
722
	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
723

724
725
726
727
728
729
730
731
732
733
734
735
736
737
	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);
	}
738
739

	if (test_opt(sb, ERRORS_CONT)) {
740
		ext4_commit_super(sb, 0);
741
742
		return;
	}
743

744
	ext4_unlock_group(sb, grp);
745
	ext4_commit_super(sb, 1);
746
747
748
749
750
751
752
	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
753
	 * ext4_grp_locked_error() to distinguish between the
754
755
756
757
758
759
760
761
	 * 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;
}

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

766
	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
767
768
		return;

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

774
775
776
	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);
777
778
779
780
781
782
783
784
785
786
787
788
789
	/* 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
 */
790
static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
791
792
793
794
{
	struct block_device *bdev;
	char b[BDEVNAME_SIZE];

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

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

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

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

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

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

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

	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));
	}
}

847
848
849
850
851
852
853
#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;

854
855
856
	/* Use our quota_off function to clear inode flags etc. */
	for (type = 0; type < EXT4_MAXQUOTAS; type++)
		ext4_quota_off(sb, type);
857
}
858
859
860
861
862
863
864
865
866
867
868
869

/*
 * This is a helper function which is used in the mount/remount
 * codepaths (which holds s_umount) to fetch the quota file name.
 */
static inline char *get_qf_name(struct super_block *sb,
				struct ext4_sb_info *sbi,
				int type)
{
	return rcu_dereference_protected(sbi->s_qf_names[type],
					 lockdep_is_held(&sb->s_umount));
}
870
871
872
873
874
875
#else
static inline void ext4_quota_off_umount(struct super_block *sb)
{
}
#endif

876
static void ext4_put_super(struct super_block *sb)
877
{
878
879
	struct ext4_sb_info *sbi = EXT4_SB(sb);
	struct ext4_super_block *es = sbi->s_es;
880
	int aborted = 0;
881
	int i, err;
882

883
	ext4_unregister_li_request(sb);
884
	ext4_quota_off_umount(sb);
885

886
887
	flush_workqueue(sbi->rsv_conversion_wq);
	destroy_workqueue(sbi->rsv_conversion_wq);
888

889
	if (sbi->s_journal) {
890
		aborted = is_journal_aborted(sbi->s_journal);
891
892
		err = jbd2_journal_destroy(sbi->s_journal);
		sbi->s_journal = NULL;
893
		if ((err < 0) && !aborted)
894
			ext4_abort(sb, "Couldn't clean up the journal");
895
	}
896

897
	ext4_unregister_sysfs(sb);
898
	ext4_es_unregister_shrinker(sbi);
899
	del_timer_sync(&sbi->s_err_report);
900
901
902
903
	ext4_release_system_zone(sb);
	ext4_mb_release(sb);
	ext4_ext_release(sb);

904
	if (!sb_rdonly(sb) && !aborted) {
905
		ext4_clear_feature_journal_needs_recovery(sb);
906
907
		es->s_state = cpu_to_le16(sbi->s_mount_state);
	}
908
	if (!sb_rdonly(sb))
909
910
		ext4_commit_super(sb, 1);

911
912
	for (i = 0; i < sbi->s_gdb_count; i++)
		brelse(sbi->s_group_desc[i]);
Al Viro's avatar
Al Viro committed
913
914
	kvfree(sbi->s_group_desc);
	kvfree(sbi->s_flex_groups);
915
	percpu_counter_destroy(&sbi->s_freeclusters_counter);
916
917
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
918
	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
919
	percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
920
#ifdef CONFIG_QUOTA
Jan Kara's avatar
Jan Kara committed
921
	for (i = 0; i < EXT4_MAXQUOTAS; i++)
922
		kfree(get_qf_name(sb, sbi, i));
923
924
925
926
927
928
929
930
931
932
#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));

933
	sync_blockdev(sb->s_bdev);
934
	invalidate_bdev(sb->s_bdev);
935
936
937
938
939
940
941
	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);
942
		invalidate_bdev(sbi->journal_bdev);
943
		ext4_blkdev_remove(sbi);
944
	}
945
946
947
948
	if (sbi->s_ea_inode_cache) {
		ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
		sbi->s_ea_inode_cache = NULL;
	}
949
950
951
	if (sbi->s_ea_block_cache) {
		ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
		sbi->s_ea_block_cache = NULL;
952
	}
953
954
	if (sbi->s_mmp_tsk)
		kthread_stop(sbi->s_mmp_tsk);
955
	brelse(sbi->s_sbh);
956
	sb->s_fs_info = NULL;
Theodore Ts'o's avatar
Theodore Ts'o committed
957
958
959
960
961
962
	/*
	 * 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);
963
964
	if (sbi->s_chksum_driver)
		crypto_free_shash(sbi->s_chksum_driver);
965
	kfree(sbi->s_blockgroup_lock);
966
	fs_put_dax(sbi->s_daxdev);
967
968
969
	kfree(sbi);
}

970
static struct kmem_cache *ext4_inode_cachep;
971
972
973
974

/*
 * Called inside transaction, so use GFP_NOFS
 */
975
static struct inode *ext4_alloc_inode(struct super_block *sb)
976
{
977
	struct ext4_inode_info *ei;
978

979
	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
980
981
	if (!ei)
		return NULL;
982

983
	ei->vfs_inode.i_version = 1;
984
	spin_lock_init(&ei->i_raw_lock);
985
986
	INIT_LIST_HEAD(&ei->i_prealloc_list);
	spin_lock_init(&ei->i_prealloc_lock);
987
988
	ext4_es_init_tree(&ei->i_es_tree);
	rwlock_init(&ei->i_es_lock);
989
	INIT_LIST_HEAD(&ei->i_es_list);
990
	ei->i_es_all_nr = 0;
991
	ei->i_es_shk_nr = 0;
992
	ei->i_es_shrink_lblk = 0;
993
	ei->i_reserved_data_blocks = 0;
994
	ei->i_da_metadata_calc_len = 0;
995
	ei->i_da_metadata_calc_last_lblock = 0;
996
	spin_lock_init(&(ei->i_block_reservation_lock));
997
998
#ifdef CONFIG_QUOTA
	ei->i_reserved_quota = 0;
999
	memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
1000
#endif
1001
	ei->jinode = NULL;
1002
	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
1003
	spin_lock_init(&ei->i_completed_io_lock);
1004
1005
	ei->i_sync_tid = 0;
	ei->i_datasync_tid = 0;
1006
	atomic_set(&ei->i_unwritten, 0);
1007
	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
1008
1009
1010
	return &ei->vfs_inode;
}

1011
1012
1013
1014
1015
1016
1017
1018
static int ext4_drop_inode(struct inode *inode)
{
	int drop = generic_drop_inode(inode);

	trace_ext4_drop_inode(inode, drop);
	return drop;
}

Nick Piggin's avatar
Nick Piggin committed
1019
1020
1021
1022
1023
1024
static void ext4_i_callback(struct rcu_head *head)
{
	struct inode *inode = container_of(head, struct inode, i_rcu);
	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
}

1025
static void ext4_destroy_inode(struct inode *inode)
1026
{
1027
	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
1028
1029
1030
		ext4_msg(inode->i_sb, KERN_ERR,
			 "Inode %lu (%p): orphan list check failed!",
			 inode->i_ino, EXT4_I(inode));
1031
1032
1033
1034
1035
		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
				EXT4_I(inode), sizeof(struct ext4_inode_info),
				true);
		dump_stack();
	}
Nick Piggin's avatar
Nick Piggin committed
1036
	call_rcu(&inode->i_rcu, ext4_i_callback);
1037
1038
}

1039
static void init_once(void *foo)
1040
{
1041
	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
1042

1043
1044
	INIT_LIST_HEAD(&ei->i_orphan);
	init_rwsem(&ei->xattr_sem);
1045
	init_rwsem(&ei->i_data_sem);
1046
	init_rwsem(&ei->i_mmap_sem);
1047
	inode_init_once(&ei->vfs_inode);
1048
1049
}

1050
static int __init init_inodecache(void)
1051
{
1052
1053
	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
					     sizeof(struct ext4_inode_info),
1054
					     0, (SLAB_RECLAIM_ACCOUNT|
1055
						SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1056
					     init_once);
1057
	if (ext4_inode_cachep == NULL)
1058
1059
1060
1061
1062
1063
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
1064
1065
1066
1067
1068
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
1069
	kmem_cache_destroy(ext4_inode_cachep);
1070
1071
}

Al Viro's avatar
Al Viro committed
1072
void ext4_clear_inode(struct inode *inode)
1073
{
Al Viro's avatar
Al Viro committed
1074
	invalidate_inode_buffers(inode);
1075
	clear_inode(inode);
1076
	dquot_drop(inode);
1077
	ext4_discard_preallocations(inode);
1078
	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
1079
1080
1081
1082
1083
1084
	if (EXT4_I(inode)->jinode) {
		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
					       EXT4_I(inode)->jinode);
		jbd2_free_inode(EXT4_I(inode)->jinode);
		EXT4_I(inode)->jinode = NULL;
	}
1085
#ifdef CONFIG_EXT4_FS_ENCRYPTION
</