super.c 149 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
#include <linux/jbd2.h>
25
26
27
28
29
#include <linux/slab.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#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>
37
#include <linux/proc_fs.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>
Dan Magenheimer's avatar
Dan Magenheimer committed
41
#include <linux/cleancache.h>
42
43
#include <asm/uaccess.h>

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

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

57
static struct proc_dir_entry *ext4_proc_root;
Theodore Ts'o's avatar
Theodore Ts'o committed
58
static struct kset *ext4_kset;
59
60
61
static struct ext4_lazy_init *ext4_li_info;
static struct mutex ext4_li_mtx;
static struct ext4_features *ext4_feat;
62

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

85
86
87
88
89
90
91
92
93
94
95
96
97
98
#if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
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,
};
#define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
#else
#define IS_EXT2_SB(sb) (0)
#endif


99
100
101
102
#if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
static struct file_system_type ext3_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "ext3",
Al Viro's avatar
Al Viro committed
103
	.mount		= ext4_mount,
104
105
106
107
108
109
110
	.kill_sb	= kill_block_super,
	.fs_flags	= FS_REQUIRES_DEV,
};
#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
#else
#define IS_EXT3_SB(sb) (0)
#endif
Laurent Vivier's avatar
Laurent Vivier committed
111

112
113
114
115
116
117
118
119
120
121
static int ext4_verify_csum_type(struct super_block *sb,
				 struct ext4_super_block *es)
{
	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
		return 1;

	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
}

122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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);
}

int ext4_superblock_csum_verify(struct super_block *sb,
				struct ext4_super_block *es)
{
	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
		return 1;

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

144
void ext4_superblock_csum_set(struct super_block *sb)
145
{
146
147
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;

148
149
150
151
152
153
154
	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
		EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
		return;

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

155
156
157
158
159
160
161
162
163
164
165
166
167
168
void *ext4_kvmalloc(size_t size, gfp_t flags)
{
	void *ret;

	ret = kmalloc(size, flags);
	if (!ret)
		ret = __vmalloc(size, flags, PAGE_KERNEL);
	return ret;
}

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

169
	ret = kzalloc(size, flags);
170
171
172
173
174
175
176
177
178
179
180
181
182
183
	if (!ret)
		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
	return ret;
}

void ext4_kvfree(void *ptr)
{
	if (is_vmalloc_addr(ptr))
		vfree(ptr);
	else
		kfree(ptr);

}

184
185
ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
			       struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
186
{
187
	return le32_to_cpu(bg->bg_block_bitmap_lo) |
188
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
189
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
190
191
}

192
193
ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
			       struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
194
{
195
	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
196
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
197
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
198
199
}

200
201
ext4_fsblk_t ext4_inode_table(struct super_block *sb,
			      struct ext4_group_desc *bg)
Laurent Vivier's avatar
Laurent Vivier committed
202
{
203
	return le32_to_cpu(bg->bg_inode_table_lo) |
204
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
205
		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
Laurent Vivier's avatar
Laurent Vivier committed
206
207
}

208
209
__u32 ext4_free_group_clusters(struct super_block *sb,
			       struct ext4_group_desc *bg)
210
211
212
{
	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
213
		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
214
215
216
217
218
219
220
}

__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 ?
221
		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
222
223
224
225
226
227
228
}

__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 ?
229
		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
230
231
232
233
234
235
236
}

__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 ?
237
		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
238
239
}

240
241
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
242
{
243
	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
244
245
	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
246
247
}

248
249
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
250
{
251
	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
252
253
	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
254
255
}

256
257
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
258
{
259
	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
260
261
	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
262
263
}

264
265
void ext4_free_group_clusters_set(struct super_block *sb,
				  struct ext4_group_desc *bg, __u32 count)
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{
	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);
}

296

297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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;
	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;
	}
315
316
317
318
319
320
	/*
	 * 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);
321
	le32_add_cpu(&es->s_error_count, 1);
322
323
324
325
326
327
328
329
330
}

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

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * 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 = bd_inode->i_mapping->backing_dev_info;

	return bdi->dev == NULL;
}

Bobi Jam's avatar
Bobi Jam committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
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);
	struct ext4_journal_cb_entry	*jce, *tmp;

	spin_lock(&sbi->s_md_lock);
	list_for_each_entry_safe(jce, tmp, &txn->t_private_list, jce_list) {
		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);
}
363

364
365
366
367
/* 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
368
 * superblock.  That is not possible on ext4, because we may have other
369
370
371
372
373
 * 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.
 *
374
 * We'll just use the jbd2_journal_abort() error code to record an error in
375
 * the journal instead.  On recovery, the journal will complain about
376
377
378
 * that error until we've noted it down and cleared it.
 */

379
static void ext4_handle_error(struct super_block *sb)
380
381
382
383
{
	if (sb->s_flags & MS_RDONLY)
		return;

384
	if (!test_opt(sb, ERRORS_CONT)) {
385
		journal_t *journal = EXT4_SB(sb)->s_journal;
386

387
		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
388
		if (journal)
389
			jbd2_journal_abort(journal, -EIO);
390
	}
391
	if (test_opt(sb, ERRORS_RO)) {
392
		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
393
394
395
		sb->s_flags |= MS_RDONLY;
	}
	if (test_opt(sb, ERRORS_PANIC))
396
		panic("EXT4-fs (device %s): panic forced after error\n",
397
398
399
			sb->s_id);
}

400
void __ext4_error(struct super_block *sb, const char *function,
401
		  unsigned int line, const char *fmt, ...)
402
{
Joe Perches's avatar
Joe Perches committed
403
	struct va_format vaf;
404
405
406
	va_list args;

	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
407
408
409
410
	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);
411
	va_end(args);
412
	save_error_info(sb, function, line);
413

414
	ext4_handle_error(sb);
415
416
}

417
418
void ext4_error_inode(struct inode *inode, const char *function,
		      unsigned int line, ext4_fsblk_t block,
419
420
421
		      const char *fmt, ...)
{
	va_list args;
422
	struct va_format vaf;
423
	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
424

425
426
427
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
	es->s_last_error_block = cpu_to_le64(block);
	save_error_info(inode->i_sb, function, line);
428
	va_start(args, fmt);
429
430
	vaf.fmt = fmt;
	vaf.va = &args;
431
	if (block)
432
433
434
435
436
437
438
439
440
		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);
441
442
443
444
445
	va_end(args);

	ext4_handle_error(inode->i_sb);
}

446
void ext4_error_file(struct file *file, const char *function,
447
448
		     unsigned int line, ext4_fsblk_t block,
		     const char *fmt, ...)
449
450
{
	va_list args;
451
	struct va_format vaf;
452
	struct ext4_super_block *es;
453
454
455
	struct inode *inode = file->f_dentry->d_inode;
	char pathname[80], *path;

456
457
458
	es = EXT4_SB(inode->i_sb)->s_es;
	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
	save_error_info(inode->i_sb, function, line);
459
	path = d_path(&(file->f_path), pathname, sizeof(pathname));
460
	if (IS_ERR(path))
461
		path = "(unknown)";
462
463
464
	va_start(args, fmt);
	vaf.fmt = fmt;
	vaf.va = &args;
465
466
467
468
469
470
471
472
473
474
475
476
	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);
477
478
479
480
481
	va_end(args);

	ext4_handle_error(inode->i_sb);
}

482
483
const char *ext4_decode_error(struct super_block *sb, int errno,
			      char nbuf[16])
484
485
486
487
488
489
490
491
492
493
494
{
	char *errstr = NULL;

	switch (errno) {
	case -EIO:
		errstr = "IO failure";
		break;
	case -ENOMEM:
		errstr = "Out of memory";
		break;
	case -EROFS:
495
496
		if (!sb || (EXT4_SB(sb)->s_journal &&
			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
			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;
}

516
/* __ext4_std_error decodes expected errors from journaling functions
517
518
 * automatically and invokes the appropriate error response.  */

519
520
void __ext4_std_error(struct super_block *sb, const char *function,
		      unsigned int line, int errno)
521
522
523
524
525
526
527
528
529
530
531
{
	char nbuf[16];
	const char *errstr;

	/* 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. */
	if (errno == -EROFS && journal_current_handle() == NULL &&
	    (sb->s_flags & MS_RDONLY))
		return;

532
	errstr = ext4_decode_error(sb, errno, nbuf);
533
534
	printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
	       sb->s_id, function, line, errstr);
535
	save_error_info(sb, function, line);
536

537
	ext4_handle_error(sb);
538
539
540
}

/*
541
 * ext4_abort is a much stronger failure handler than ext4_error.  The
542
543
544
545
546
547
548
549
 * 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.
 */

550
void __ext4_abort(struct super_block *sb, const char *function,
551
		unsigned int line, const char *fmt, ...)
552
553
554
{
	va_list args;

555
	save_error_info(sb, function, line);
556
	va_start(args, fmt);
557
558
	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
	       function, line);
559
560
561
562
	vprintk(fmt, args);
	printk("\n");
	va_end(args);

563
564
565
566
567
568
569
570
	if ((sb->s_flags & MS_RDONLY) == 0) {
		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
		sb->s_flags |= MS_RDONLY;
		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
		if (EXT4_SB(sb)->s_journal)
			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
		save_error_info(sb, function, line);
	}
571
	if (test_opt(sb, ERRORS_PANIC))
572
		panic("EXT4-fs panic from previous error\n");
573
574
}

Joe Perches's avatar
Joe Perches committed
575
void ext4_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
576
{
Joe Perches's avatar
Joe Perches committed
577
	struct va_format vaf;
578
579
580
	va_list args;

	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
581
582
583
	vaf.fmt = fmt;
	vaf.va = &args;
	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
584
585
586
	va_end(args);
}

587
void __ext4_warning(struct super_block *sb, const char *function,
588
		    unsigned int line, const char *fmt, ...)
589
{
Joe Perches's avatar
Joe Perches committed
590
	struct va_format vaf;
591
592
593
	va_list args;

	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
594
595
596
597
	vaf.fmt = fmt;
	vaf.va = &args;
	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
	       sb->s_id, function, line, &vaf);
598
599
600
	va_end(args);
}

601
602
603
604
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, ...)
605
606
607
__releases(bitlock)
__acquires(bitlock)
{
Joe Perches's avatar
Joe Perches committed
608
	struct va_format vaf;
609
610
611
	va_list args;
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;

612
613
614
	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
615

616
	va_start(args, fmt);
Joe Perches's avatar
Joe Perches committed
617
618
619

	vaf.fmt = fmt;
	vaf.va = &args;
620
	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
621
622
	       sb->s_id, function, line, grp);
	if (ino)
Joe Perches's avatar
Joe Perches committed
623
		printk(KERN_CONT "inode %lu: ", ino);
624
	if (block)
Joe Perches's avatar
Joe Perches committed
625
626
		printk(KERN_CONT "block %llu:", (unsigned long long) block);
	printk(KERN_CONT "%pV\n", &vaf);
627
628
629
	va_end(args);

	if (test_opt(sb, ERRORS_CONT)) {
630
		ext4_commit_super(sb, 0);
631
632
		return;
	}
633

634
635
636
637
638
639
640
641
	ext4_unlock_group(sb, grp);
	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
642
	 * ext4_grp_locked_error() to distinguish between the
643
644
645
646
647
648
649
650
	 * 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;
}

651
void ext4_update_dynamic_rev(struct super_block *sb)
652
{
653
	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
654

655
	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
656
657
		return;

658
	ext4_warning(sb,
659
660
		     "updating to rev %d because of new feature flag, "
		     "running e2fsck is recommended",
661
		     EXT4_DYNAMIC_REV);
662

663
664
665
	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);
666
667
668
669
670
671
672
673
674
675
676
677
678
	/* 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
 */
679
static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
680
681
682
683
{
	struct block_device *bdev;
	char b[BDEVNAME_SIZE];

684
	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
685
686
687
688
689
	if (IS_ERR(bdev))
		goto fail;
	return bdev;

fail:
690
	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
691
692
693
694
695
696
697
			__bdevname(dev, b), PTR_ERR(bdev));
	return NULL;
}

/*
 * Release the journal device
 */
698
static int ext4_blkdev_put(struct block_device *bdev)
699
{
700
	return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
701
702
}

703
static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
704
705
706
707
708
709
{
	struct block_device *bdev;
	int ret = -ENODEV;

	bdev = sbi->journal_bdev;
	if (bdev) {
710
		ret = ext4_blkdev_put(bdev);
711
712
713
714
715
716
717
		sbi->journal_bdev = NULL;
	}
	return ret;
}

static inline struct inode *orphan_list_entry(struct list_head *l)
{
718
	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
719
720
}

721
static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
722
723
724
{
	struct list_head *l;

725
726
	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
		 le32_to_cpu(sbi->s_es->s_last_orphan));
727
728
729
730
731
732
733
734
735
736
737
738

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

739
static void ext4_put_super(struct super_block *sb)
740
{
741
742
	struct ext4_sb_info *sbi = EXT4_SB(sb);
	struct ext4_super_block *es = sbi->s_es;
743
	int i, err;
744

745
	ext4_unregister_li_request(sb);
746
747
	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);

748
749
750
	flush_workqueue(sbi->dio_unwritten_wq);
	destroy_workqueue(sbi->dio_unwritten_wq);

751
752
753
754
	if (sbi->s_journal) {
		err = jbd2_journal_destroy(sbi->s_journal);
		sbi->s_journal = NULL;
		if (err < 0)
755
			ext4_abort(sb, "Couldn't clean up the journal");
756
	}
757

758
	ext4_es_unregister_shrinker(sb);
759
	del_timer(&sbi->s_err_report);
760
761
762
763
764
	ext4_release_system_zone(sb);
	ext4_mb_release(sb);
	ext4_ext_release(sb);
	ext4_xattr_put_super(sb);

765
	if (!(sb->s_flags & MS_RDONLY)) {
766
		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
767
768
		es->s_state = cpu_to_le16(sbi->s_mount_state);
	}
769
	if (!(sb->s_flags & MS_RDONLY))
770
771
		ext4_commit_super(sb, 1);

772
	if (sbi->s_proc) {
773
		remove_proc_entry("options", sbi->s_proc);
774
		remove_proc_entry(sb->s_id, ext4_proc_root);
775
	}
Theodore Ts'o's avatar
Theodore Ts'o committed
776
	kobject_del(&sbi->s_kobj);
777
778
779

	for (i = 0; i < sbi->s_gdb_count; i++)
		brelse(sbi->s_group_desc[i]);
780
	ext4_kvfree(sbi->s_group_desc);
781
	ext4_kvfree(sbi->s_flex_groups);
782
	percpu_counter_destroy(&sbi->s_freeclusters_counter);
783
784
	percpu_counter_destroy(&sbi->s_freeinodes_counter);
	percpu_counter_destroy(&sbi->s_dirs_counter);
785
	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
786
	percpu_counter_destroy(&sbi->s_extent_cache_cnt);
787
788
789
790
791
792
793
794
795
796
797
798
799
800
	brelse(sbi->s_sbh);
#ifdef CONFIG_QUOTA
	for (i = 0; i < MAXQUOTAS; i++)
		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));

801
	invalidate_bdev(sb->s_bdev);
802
803
804
805
806
807
808
	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);
809
		invalidate_bdev(sbi->journal_bdev);
810
		ext4_blkdev_remove(sbi);
811
	}
812
813
	if (sbi->s_mmp_tsk)
		kthread_stop(sbi->s_mmp_tsk);
814
	sb->s_fs_info = NULL;
Theodore Ts'o's avatar
Theodore Ts'o committed
815
816
817
818
819
820
	/*
	 * 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);
821
822
	if (sbi->s_chksum_driver)
		crypto_free_shash(sbi->s_chksum_driver);
823
	kfree(sbi->s_blockgroup_lock);
824
825
826
	kfree(sbi);
}

827
static struct kmem_cache *ext4_inode_cachep;
828
829
830
831

/*
 * Called inside transaction, so use GFP_NOFS
 */
832
static struct inode *ext4_alloc_inode(struct super_block *sb)
833
{
834
	struct ext4_inode_info *ei;
835

836
	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
837
838
	if (!ei)
		return NULL;
839

840
	ei->vfs_inode.i_version = 1;
841
842
	INIT_LIST_HEAD(&ei->i_prealloc_list);
	spin_lock_init(&ei->i_prealloc_lock);
843
844
	ext4_es_init_tree(&ei->i_es_tree);
	rwlock_init(&ei->i_es_lock);
845
846
	INIT_LIST_HEAD(&ei->i_es_lru);
	ei->i_es_lru_nr = 0;
847
848
849
	ei->i_reserved_data_blocks = 0;
	ei->i_reserved_meta_blocks = 0;
	ei->i_allocated_meta_blocks = 0;
850
	ei->i_da_metadata_calc_len = 0;
851
	ei->i_da_metadata_calc_last_lblock = 0;
852
	spin_lock_init(&(ei->i_block_reservation_lock));
853
854
855
#ifdef CONFIG_QUOTA
	ei->i_reserved_quota = 0;
#endif
856
	ei->jinode = NULL;
857
	INIT_LIST_HEAD(&ei->i_completed_io_list);
858
	spin_lock_init(&ei->i_completed_io_lock);
859
860
	ei->i_sync_tid = 0;
	ei->i_datasync_tid = 0;
861
	atomic_set(&ei->i_ioend_count, 0);
862
	atomic_set(&ei->i_unwritten, 0);
863
	INIT_WORK(&ei->i_unwritten_work, ext4_end_io_work);
864

865
866
867
	return &ei->vfs_inode;
}

868
869
870
871
872
873
874
875
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
876
877
878
879
880
881
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));
}

882
static void ext4_destroy_inode(struct inode *inode)
883
{
884
	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
885
886
887
		ext4_msg(inode->i_sb, KERN_ERR,
			 "Inode %lu (%p): orphan list check failed!",
			 inode->i_ino, EXT4_I(inode));
888
889
890
891
892
		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
893
	call_rcu(&inode->i_rcu, ext4_i_callback);
894
895
}

896
static void init_once(void *foo)
897
{
898
	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
899

900
901
	INIT_LIST_HEAD(&ei->i_orphan);
	init_rwsem(&ei->xattr_sem);
902
	init_rwsem(&ei->i_data_sem);
903
	inode_init_once(&ei->vfs_inode);
904
905
906
907
}

static int init_inodecache(void)
{
908
909
	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
					     sizeof(struct ext4_inode_info),
910
911
					     0, (SLAB_RECLAIM_ACCOUNT|
						SLAB_MEM_SPREAD),
912
					     init_once);
913
	if (ext4_inode_cachep == NULL)
914
915
916
917
918
919
		return -ENOMEM;
	return 0;
}

static void destroy_inodecache(void)
{
920
921
922
923
924
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
925
	kmem_cache_destroy(ext4_inode_cachep);
926
927
}

Al Viro's avatar
Al Viro committed
928
void ext4_clear_inode(struct inode *inode)
929
{
Al Viro's avatar
Al Viro committed
930
	invalidate_inode_buffers(inode);
931
	clear_inode(inode);
932
	dquot_drop(inode);
933
	ext4_discard_preallocations(inode);
934
	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
935
	ext4_es_lru_del(inode);
936
937
938
939
940
941
	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;
	}
942
943
}

Christoph Hellwig's avatar
Christoph Hellwig committed
944
static struct inode *ext4_nfs_get_inode(struct super_block *sb,
945
					u64 ino, u32 generation)
946
947
948
{
	struct inode *inode;

949
	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
950
		return ERR_PTR(-ESTALE);
951
	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
952
953
954
955
		return ERR_PTR(-ESTALE);

	/* iget isn't really right if the inode is currently unallocated!!
	 *
956
	 * ext4_read_inode will return a bad_inode if the inode had been
957
958
959
960
961
	 * deleted, so we should be safe.
	 *
	 * Currently we don't know the generation for parent directory, so
	 * a generation of 0 means "accept any"
	 */
962
963
964
965
	inode = ext4_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
	if (generation && inode->i_generation != generation) {
966
967
968
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
Christoph Hellwig's avatar
Christoph Hellwig committed
969
970
971
972
973

	return inode;
}

static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
974
					int fh_len, int fh_type)
Christoph Hellwig's avatar
Christoph Hellwig committed
975
976
977
978
979
980
{
	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
				    ext4_nfs_get_inode);
}

static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
981
					int fh_len, int fh_type)
Christoph Hellwig's avatar
Christoph Hellwig committed
982
983
984
{
	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
				    ext4_nfs_get_inode);
985
986
}

987
988
989
990
991
992
/*
 * Try to release metadata pages (indirect blocks, directories) which are
 * mapped via the block device.  Since these pages could have journal heads
 * which would prevent try_to_free_buffers() from freeing them, we must use
 * jbd2 layer's try_to_free_buffers() function to release them.
 */
993
994
static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
				 gfp_t wait)
995
996
997
998
999
1000
{
	journal_t *journal = EXT4_SB(sb)->s_journal;

	WARN_ON(PageChecked(page));
	if (!page_has_buffers(page))
		return 0;