• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  linux/fs/ext4/super.c
3  *
4  * Copyright (C) 1992, 1993, 1994, 1995
5  * Remy Card (card@masi.ibp.fr)
6  * Laboratoire MASI - Institut Blaise Pascal
7  * Universite Pierre et Marie Curie (Paris VI)
8  *
9  *  from
10  *
11  *  linux/fs/minix/inode.c
12  *
13  *  Copyright (C) 1991, 1992  Linus Torvalds
14  *
15  *  Big-endian to little-endian byte-swapping/bitmaps by
16  *        David S. Miller (davem@caip.rutgers.edu), 1995
17  */
18 
19 #include <linux/module.h>
20 #include <linux/string.h>
21 #include <linux/fs.h>
22 #include <linux/time.h>
23 #include <linux/vmalloc.h>
24 #include <linux/jbd2.h>
25 #include <linux/slab.h>
26 #include <linux/init.h>
27 #include <linux/blkdev.h>
28 #include <linux/parser.h>
29 #include <linux/buffer_head.h>
30 #include <linux/exportfs.h>
31 #include <linux/vfs.h>
32 #include <linux/random.h>
33 #include <linux/mount.h>
34 #include <linux/namei.h>
35 #include <linux/quotaops.h>
36 #include <linux/seq_file.h>
37 #include <linux/proc_fs.h>
38 #include <linux/ctype.h>
39 #include <linux/log2.h>
40 #include <linux/crc16.h>
41 #include <linux/cleancache.h>
42 #include <asm/uaccess.h>
43 
44 #include <linux/kthread.h>
45 #include <linux/freezer.h>
46 
47 #include "ext4.h"
48 #include "ext4_extents.h"	/* Needed for trace points definition */
49 #include "ext4_jbd2.h"
50 #include "xattr.h"
51 #include "acl.h"
52 #include "mballoc.h"
53 
54 #define CREATE_TRACE_POINTS
55 #include <trace/events/ext4.h>
56 
57 static struct proc_dir_entry *ext4_proc_root;
58 static struct kset *ext4_kset;
59 static struct ext4_lazy_init *ext4_li_info;
60 static struct mutex ext4_li_mtx;
61 static struct ext4_features *ext4_feat;
62 static int ext4_mballoc_ready;
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 static void ext4_mark_recovery_complete(struct super_block *sb,
69 					struct ext4_super_block *es);
70 static void ext4_clear_journal_err(struct super_block *sb,
71 				   struct ext4_super_block *es);
72 static int ext4_sync_fs(struct super_block *sb, int wait);
73 static int ext4_remount(struct super_block *sb, int *flags, char *data);
74 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
75 static int ext4_unfreeze(struct super_block *sb);
76 static int ext4_freeze(struct super_block *sb);
77 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
78 		       const char *dev_name, void *data);
79 static inline int ext2_feature_set_ok(struct super_block *sb);
80 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 static void ext4_destroy_lazyinit_thread(void);
83 static void ext4_unregister_li_request(struct super_block *sb);
84 static void ext4_clear_request_list(void);
85 static int ext4_reserve_clusters(struct ext4_sb_info *, ext4_fsblk_t);
86 
87 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
88 static struct file_system_type ext2_fs_type = {
89 	.owner		= THIS_MODULE,
90 	.name		= "ext2",
91 	.mount		= ext4_mount,
92 	.kill_sb	= kill_block_super,
93 	.fs_flags	= FS_REQUIRES_DEV,
94 };
95 MODULE_ALIAS_FS("ext2");
96 MODULE_ALIAS("ext2");
97 #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
98 #else
99 #define IS_EXT2_SB(sb) (0)
100 #endif
101 
102 
103 #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
104 static struct file_system_type ext3_fs_type = {
105 	.owner		= THIS_MODULE,
106 	.name		= "ext3",
107 	.mount		= ext4_mount,
108 	.kill_sb	= kill_block_super,
109 	.fs_flags	= FS_REQUIRES_DEV,
110 };
111 MODULE_ALIAS_FS("ext3");
112 MODULE_ALIAS("ext3");
113 #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
114 #else
115 #define IS_EXT3_SB(sb) (0)
116 #endif
117 
ext4_verify_csum_type(struct super_block * sb,struct ext4_super_block * es)118 static int ext4_verify_csum_type(struct super_block *sb,
119 				 struct ext4_super_block *es)
120 {
121 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb,
122 					EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
123 		return 1;
124 
125 	return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
126 }
127 
ext4_superblock_csum(struct super_block * sb,struct ext4_super_block * es)128 static __le32 ext4_superblock_csum(struct super_block *sb,
129 				   struct ext4_super_block *es)
130 {
131 	struct ext4_sb_info *sbi = EXT4_SB(sb);
132 	int offset = offsetof(struct ext4_super_block, s_checksum);
133 	__u32 csum;
134 
135 	csum = ext4_chksum(sbi, ~0, (char *)es, offset);
136 
137 	return cpu_to_le32(csum);
138 }
139 
ext4_superblock_csum_verify(struct super_block * sb,struct ext4_super_block * es)140 static int ext4_superblock_csum_verify(struct super_block *sb,
141 				       struct ext4_super_block *es)
142 {
143 	if (!ext4_has_metadata_csum(sb))
144 		return 1;
145 
146 	return es->s_checksum == ext4_superblock_csum(sb, es);
147 }
148 
ext4_superblock_csum_set(struct super_block * sb)149 void ext4_superblock_csum_set(struct super_block *sb)
150 {
151 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
152 
153 	if (!ext4_has_metadata_csum(sb))
154 		return;
155 
156 	es->s_checksum = ext4_superblock_csum(sb, es);
157 }
158 
ext4_kvmalloc(size_t size,gfp_t flags)159 void *ext4_kvmalloc(size_t size, gfp_t flags)
160 {
161 	void *ret;
162 
163 	ret = kmalloc(size, flags | __GFP_NOWARN);
164 	if (!ret)
165 		ret = __vmalloc(size, flags, PAGE_KERNEL);
166 	return ret;
167 }
168 
ext4_kvzalloc(size_t size,gfp_t flags)169 void *ext4_kvzalloc(size_t size, gfp_t flags)
170 {
171 	void *ret;
172 
173 	ret = kzalloc(size, flags | __GFP_NOWARN);
174 	if (!ret)
175 		ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
176 	return ret;
177 }
178 
ext4_kvfree(void * ptr)179 void ext4_kvfree(void *ptr)
180 {
181 	if (is_vmalloc_addr(ptr))
182 		vfree(ptr);
183 	else
184 		kfree(ptr);
185 
186 }
187 
ext4_block_bitmap(struct super_block * sb,struct ext4_group_desc * bg)188 ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
189 			       struct ext4_group_desc *bg)
190 {
191 	return le32_to_cpu(bg->bg_block_bitmap_lo) |
192 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
193 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
194 }
195 
ext4_inode_bitmap(struct super_block * sb,struct ext4_group_desc * bg)196 ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
197 			       struct ext4_group_desc *bg)
198 {
199 	return le32_to_cpu(bg->bg_inode_bitmap_lo) |
200 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
201 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
202 }
203 
ext4_inode_table(struct super_block * sb,struct ext4_group_desc * bg)204 ext4_fsblk_t ext4_inode_table(struct super_block *sb,
205 			      struct ext4_group_desc *bg)
206 {
207 	return le32_to_cpu(bg->bg_inode_table_lo) |
208 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
209 		 (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
210 }
211 
ext4_free_group_clusters(struct super_block * sb,struct ext4_group_desc * bg)212 __u32 ext4_free_group_clusters(struct super_block *sb,
213 			       struct ext4_group_desc *bg)
214 {
215 	return le16_to_cpu(bg->bg_free_blocks_count_lo) |
216 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
217 		 (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
218 }
219 
ext4_free_inodes_count(struct super_block * sb,struct ext4_group_desc * bg)220 __u32 ext4_free_inodes_count(struct super_block *sb,
221 			      struct ext4_group_desc *bg)
222 {
223 	return le16_to_cpu(bg->bg_free_inodes_count_lo) |
224 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
225 		 (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
226 }
227 
ext4_used_dirs_count(struct super_block * sb,struct ext4_group_desc * bg)228 __u32 ext4_used_dirs_count(struct super_block *sb,
229 			      struct ext4_group_desc *bg)
230 {
231 	return le16_to_cpu(bg->bg_used_dirs_count_lo) |
232 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
233 		 (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
234 }
235 
ext4_itable_unused_count(struct super_block * sb,struct ext4_group_desc * bg)236 __u32 ext4_itable_unused_count(struct super_block *sb,
237 			      struct ext4_group_desc *bg)
238 {
239 	return le16_to_cpu(bg->bg_itable_unused_lo) |
240 		(EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
241 		 (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
242 }
243 
ext4_block_bitmap_set(struct super_block * sb,struct ext4_group_desc * bg,ext4_fsblk_t blk)244 void ext4_block_bitmap_set(struct super_block *sb,
245 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
246 {
247 	bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
248 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
249 		bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
250 }
251 
ext4_inode_bitmap_set(struct super_block * sb,struct ext4_group_desc * bg,ext4_fsblk_t blk)252 void ext4_inode_bitmap_set(struct super_block *sb,
253 			   struct ext4_group_desc *bg, ext4_fsblk_t blk)
254 {
255 	bg->bg_inode_bitmap_lo  = cpu_to_le32((u32)blk);
256 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
257 		bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
258 }
259 
ext4_inode_table_set(struct super_block * sb,struct ext4_group_desc * bg,ext4_fsblk_t blk)260 void ext4_inode_table_set(struct super_block *sb,
261 			  struct ext4_group_desc *bg, ext4_fsblk_t blk)
262 {
263 	bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
264 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
265 		bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
266 }
267 
ext4_free_group_clusters_set(struct super_block * sb,struct ext4_group_desc * bg,__u32 count)268 void ext4_free_group_clusters_set(struct super_block *sb,
269 				  struct ext4_group_desc *bg, __u32 count)
270 {
271 	bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
272 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
273 		bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
274 }
275 
ext4_free_inodes_set(struct super_block * sb,struct ext4_group_desc * bg,__u32 count)276 void ext4_free_inodes_set(struct super_block *sb,
277 			  struct ext4_group_desc *bg, __u32 count)
278 {
279 	bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
280 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
281 		bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
282 }
283 
ext4_used_dirs_set(struct super_block * sb,struct ext4_group_desc * bg,__u32 count)284 void ext4_used_dirs_set(struct super_block *sb,
285 			  struct ext4_group_desc *bg, __u32 count)
286 {
287 	bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
288 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
289 		bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
290 }
291 
ext4_itable_unused_set(struct super_block * sb,struct ext4_group_desc * bg,__u32 count)292 void ext4_itable_unused_set(struct super_block *sb,
293 			  struct ext4_group_desc *bg, __u32 count)
294 {
295 	bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
296 	if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
297 		bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
298 }
299 
300 
__save_error_info(struct super_block * sb,const char * func,unsigned int line)301 static void __save_error_info(struct super_block *sb, const char *func,
302 			    unsigned int line)
303 {
304 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
305 
306 	EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
307 	if (bdev_read_only(sb->s_bdev))
308 		return;
309 	es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
310 	es->s_last_error_time = cpu_to_le32(get_seconds());
311 	strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
312 	es->s_last_error_line = cpu_to_le32(line);
313 	if (!es->s_first_error_time) {
314 		es->s_first_error_time = es->s_last_error_time;
315 		strncpy(es->s_first_error_func, func,
316 			sizeof(es->s_first_error_func));
317 		es->s_first_error_line = cpu_to_le32(line);
318 		es->s_first_error_ino = es->s_last_error_ino;
319 		es->s_first_error_block = es->s_last_error_block;
320 	}
321 	/*
322 	 * Start the daily error reporting function if it hasn't been
323 	 * started already
324 	 */
325 	if (!es->s_error_count)
326 		mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
327 	le32_add_cpu(&es->s_error_count, 1);
328 }
329 
save_error_info(struct super_block * sb,const char * func,unsigned int line)330 static void save_error_info(struct super_block *sb, const char *func,
331 			    unsigned int line)
332 {
333 	__save_error_info(sb, func, line);
334 	ext4_commit_super(sb, 1);
335 }
336 
337 /*
338  * The del_gendisk() function uninitializes the disk-specific data
339  * structures, including the bdi structure, without telling anyone
340  * else.  Once this happens, any attempt to call mark_buffer_dirty()
341  * (for example, by ext4_commit_super), will cause a kernel OOPS.
342  * This is a kludge to prevent these oops until we can put in a proper
343  * hook in del_gendisk() to inform the VFS and file system layers.
344  */
block_device_ejected(struct super_block * sb)345 static int block_device_ejected(struct super_block *sb)
346 {
347 	struct inode *bd_inode = sb->s_bdev->bd_inode;
348 	struct backing_dev_info *bdi = bd_inode->i_mapping->backing_dev_info;
349 
350 	return bdi->dev == NULL;
351 }
352 
ext4_journal_commit_callback(journal_t * journal,transaction_t * txn)353 static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
354 {
355 	struct super_block		*sb = journal->j_private;
356 	struct ext4_sb_info		*sbi = EXT4_SB(sb);
357 	int				error = is_journal_aborted(journal);
358 	struct ext4_journal_cb_entry	*jce;
359 
360 	BUG_ON(txn->t_state == T_FINISHED);
361 	spin_lock(&sbi->s_md_lock);
362 	while (!list_empty(&txn->t_private_list)) {
363 		jce = list_entry(txn->t_private_list.next,
364 				 struct ext4_journal_cb_entry, jce_list);
365 		list_del_init(&jce->jce_list);
366 		spin_unlock(&sbi->s_md_lock);
367 		jce->jce_func(sb, jce, error);
368 		spin_lock(&sbi->s_md_lock);
369 	}
370 	spin_unlock(&sbi->s_md_lock);
371 }
372 
373 /* Deal with the reporting of failure conditions on a filesystem such as
374  * inconsistencies detected or read IO failures.
375  *
376  * On ext2, we can store the error state of the filesystem in the
377  * superblock.  That is not possible on ext4, because we may have other
378  * write ordering constraints on the superblock which prevent us from
379  * writing it out straight away; and given that the journal is about to
380  * be aborted, we can't rely on the current, or future, transactions to
381  * write out the superblock safely.
382  *
383  * We'll just use the jbd2_journal_abort() error code to record an error in
384  * the journal instead.  On recovery, the journal will complain about
385  * that error until we've noted it down and cleared it.
386  */
387 
ext4_handle_error(struct super_block * sb)388 static void ext4_handle_error(struct super_block *sb)
389 {
390 	if (sb->s_flags & MS_RDONLY)
391 		return;
392 
393 	if (!test_opt(sb, ERRORS_CONT)) {
394 		journal_t *journal = EXT4_SB(sb)->s_journal;
395 
396 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
397 		if (journal)
398 			jbd2_journal_abort(journal, -EIO);
399 	}
400 	if (test_opt(sb, ERRORS_RO)) {
401 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
402 		/*
403 		 * Make sure updated value of ->s_mount_flags will be visible
404 		 * before ->s_flags update
405 		 */
406 		smp_wmb();
407 		sb->s_flags |= MS_RDONLY;
408 	}
409 	if (test_opt(sb, ERRORS_PANIC)) {
410 		if (EXT4_SB(sb)->s_journal &&
411 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
412 			return;
413 		panic("EXT4-fs (device %s): panic forced after error\n",
414 			sb->s_id);
415 	}
416 }
417 
418 #define ext4_error_ratelimit(sb)					\
419 		___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state),	\
420 			     "EXT4-fs error")
421 
__ext4_error(struct super_block * sb,const char * function,unsigned int line,const char * fmt,...)422 void __ext4_error(struct super_block *sb, const char *function,
423 		  unsigned int line, const char *fmt, ...)
424 {
425 	struct va_format vaf;
426 	va_list args;
427 
428 	if (ext4_error_ratelimit(sb)) {
429 		va_start(args, fmt);
430 		vaf.fmt = fmt;
431 		vaf.va = &args;
432 		printk(KERN_CRIT
433 		       "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
434 		       sb->s_id, function, line, current->comm, &vaf);
435 		va_end(args);
436 	}
437 	save_error_info(sb, function, line);
438 	ext4_handle_error(sb);
439 }
440 
__ext4_error_inode(struct inode * inode,const char * function,unsigned int line,ext4_fsblk_t block,const char * fmt,...)441 void __ext4_error_inode(struct inode *inode, const char *function,
442 			unsigned int line, ext4_fsblk_t block,
443 			const char *fmt, ...)
444 {
445 	va_list args;
446 	struct va_format vaf;
447 	struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
448 
449 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
450 	es->s_last_error_block = cpu_to_le64(block);
451 	if (ext4_error_ratelimit(inode->i_sb)) {
452 		va_start(args, fmt);
453 		vaf.fmt = fmt;
454 		vaf.va = &args;
455 		if (block)
456 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
457 			       "inode #%lu: block %llu: comm %s: %pV\n",
458 			       inode->i_sb->s_id, function, line, inode->i_ino,
459 			       block, current->comm, &vaf);
460 		else
461 			printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
462 			       "inode #%lu: comm %s: %pV\n",
463 			       inode->i_sb->s_id, function, line, inode->i_ino,
464 			       current->comm, &vaf);
465 		va_end(args);
466 	}
467 	save_error_info(inode->i_sb, function, line);
468 	ext4_handle_error(inode->i_sb);
469 }
470 
__ext4_error_file(struct file * file,const char * function,unsigned int line,ext4_fsblk_t block,const char * fmt,...)471 void __ext4_error_file(struct file *file, const char *function,
472 		       unsigned int line, ext4_fsblk_t block,
473 		       const char *fmt, ...)
474 {
475 	va_list args;
476 	struct va_format vaf;
477 	struct ext4_super_block *es;
478 	struct inode *inode = file_inode(file);
479 	char pathname[80], *path;
480 
481 	es = EXT4_SB(inode->i_sb)->s_es;
482 	es->s_last_error_ino = cpu_to_le32(inode->i_ino);
483 	if (ext4_error_ratelimit(inode->i_sb)) {
484 		path = d_path(&(file->f_path), pathname, sizeof(pathname));
485 		if (IS_ERR(path))
486 			path = "(unknown)";
487 		va_start(args, fmt);
488 		vaf.fmt = fmt;
489 		vaf.va = &args;
490 		if (block)
491 			printk(KERN_CRIT
492 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
493 			       "block %llu: comm %s: path %s: %pV\n",
494 			       inode->i_sb->s_id, function, line, inode->i_ino,
495 			       block, current->comm, path, &vaf);
496 		else
497 			printk(KERN_CRIT
498 			       "EXT4-fs error (device %s): %s:%d: inode #%lu: "
499 			       "comm %s: path %s: %pV\n",
500 			       inode->i_sb->s_id, function, line, inode->i_ino,
501 			       current->comm, path, &vaf);
502 		va_end(args);
503 	}
504 	save_error_info(inode->i_sb, function, line);
505 	ext4_handle_error(inode->i_sb);
506 }
507 
ext4_decode_error(struct super_block * sb,int errno,char nbuf[16])508 const char *ext4_decode_error(struct super_block *sb, int errno,
509 			      char nbuf[16])
510 {
511 	char *errstr = NULL;
512 
513 	switch (errno) {
514 	case -EIO:
515 		errstr = "IO failure";
516 		break;
517 	case -ENOMEM:
518 		errstr = "Out of memory";
519 		break;
520 	case -EROFS:
521 		if (!sb || (EXT4_SB(sb)->s_journal &&
522 			    EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
523 			errstr = "Journal has aborted";
524 		else
525 			errstr = "Readonly filesystem";
526 		break;
527 	default:
528 		/* If the caller passed in an extra buffer for unknown
529 		 * errors, textualise them now.  Else we just return
530 		 * NULL. */
531 		if (nbuf) {
532 			/* Check for truncated error codes... */
533 			if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
534 				errstr = nbuf;
535 		}
536 		break;
537 	}
538 
539 	return errstr;
540 }
541 
542 /* __ext4_std_error decodes expected errors from journaling functions
543  * automatically and invokes the appropriate error response.  */
544 
__ext4_std_error(struct super_block * sb,const char * function,unsigned int line,int errno)545 void __ext4_std_error(struct super_block *sb, const char *function,
546 		      unsigned int line, int errno)
547 {
548 	char nbuf[16];
549 	const char *errstr;
550 
551 	/* Special case: if the error is EROFS, and we're not already
552 	 * inside a transaction, then there's really no point in logging
553 	 * an error. */
554 	if (errno == -EROFS && journal_current_handle() == NULL &&
555 	    (sb->s_flags & MS_RDONLY))
556 		return;
557 
558 	if (ext4_error_ratelimit(sb)) {
559 		errstr = ext4_decode_error(sb, errno, nbuf);
560 		printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
561 		       sb->s_id, function, line, errstr);
562 	}
563 
564 	save_error_info(sb, function, line);
565 	ext4_handle_error(sb);
566 }
567 
568 /*
569  * ext4_abort is a much stronger failure handler than ext4_error.  The
570  * abort function may be used to deal with unrecoverable failures such
571  * as journal IO errors or ENOMEM at a critical moment in log management.
572  *
573  * We unconditionally force the filesystem into an ABORT|READONLY state,
574  * unless the error response on the fs has been set to panic in which
575  * case we take the easy way out and panic immediately.
576  */
577 
__ext4_abort(struct super_block * sb,const char * function,unsigned int line,const char * fmt,...)578 void __ext4_abort(struct super_block *sb, const char *function,
579 		unsigned int line, const char *fmt, ...)
580 {
581 	va_list args;
582 
583 	save_error_info(sb, function, line);
584 	va_start(args, fmt);
585 	printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: ", sb->s_id,
586 	       function, line);
587 	vprintk(fmt, args);
588 	printk("\n");
589 	va_end(args);
590 
591 	if ((sb->s_flags & MS_RDONLY) == 0) {
592 		ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
593 		EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
594 		/*
595 		 * Make sure updated value of ->s_mount_flags will be visible
596 		 * before ->s_flags update
597 		 */
598 		smp_wmb();
599 		sb->s_flags |= MS_RDONLY;
600 		if (EXT4_SB(sb)->s_journal)
601 			jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
602 		save_error_info(sb, function, line);
603 	}
604 	if (test_opt(sb, ERRORS_PANIC)) {
605 		if (EXT4_SB(sb)->s_journal &&
606 		  !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
607 			return;
608 		panic("EXT4-fs panic from previous error\n");
609 	}
610 }
611 
__ext4_msg(struct super_block * sb,const char * prefix,const char * fmt,...)612 void __ext4_msg(struct super_block *sb,
613 		const char *prefix, const char *fmt, ...)
614 {
615 	struct va_format vaf;
616 	va_list args;
617 
618 	if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
619 		return;
620 
621 	va_start(args, fmt);
622 	vaf.fmt = fmt;
623 	vaf.va = &args;
624 	printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
625 	va_end(args);
626 }
627 
__ext4_warning(struct super_block * sb,const char * function,unsigned int line,const char * fmt,...)628 void __ext4_warning(struct super_block *sb, const char *function,
629 		    unsigned int line, const char *fmt, ...)
630 {
631 	struct va_format vaf;
632 	va_list args;
633 
634 	if (!___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
635 			  "EXT4-fs warning"))
636 		return;
637 
638 	va_start(args, fmt);
639 	vaf.fmt = fmt;
640 	vaf.va = &args;
641 	printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
642 	       sb->s_id, function, line, &vaf);
643 	va_end(args);
644 }
645 
__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,...)646 void __ext4_grp_locked_error(const char *function, unsigned int line,
647 			     struct super_block *sb, ext4_group_t grp,
648 			     unsigned long ino, ext4_fsblk_t block,
649 			     const char *fmt, ...)
650 __releases(bitlock)
651 __acquires(bitlock)
652 {
653 	struct va_format vaf;
654 	va_list args;
655 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
656 
657 	es->s_last_error_ino = cpu_to_le32(ino);
658 	es->s_last_error_block = cpu_to_le64(block);
659 	__save_error_info(sb, function, line);
660 
661 	if (ext4_error_ratelimit(sb)) {
662 		va_start(args, fmt);
663 		vaf.fmt = fmt;
664 		vaf.va = &args;
665 		printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
666 		       sb->s_id, function, line, grp);
667 		if (ino)
668 			printk(KERN_CONT "inode %lu: ", ino);
669 		if (block)
670 			printk(KERN_CONT "block %llu:",
671 			       (unsigned long long) block);
672 		printk(KERN_CONT "%pV\n", &vaf);
673 		va_end(args);
674 	}
675 
676 	if (test_opt(sb, ERRORS_CONT)) {
677 		ext4_commit_super(sb, 0);
678 		return;
679 	}
680 
681 	ext4_unlock_group(sb, grp);
682 	ext4_handle_error(sb);
683 	/*
684 	 * We only get here in the ERRORS_RO case; relocking the group
685 	 * may be dangerous, but nothing bad will happen since the
686 	 * filesystem will have already been marked read/only and the
687 	 * journal has been aborted.  We return 1 as a hint to callers
688 	 * who might what to use the return value from
689 	 * ext4_grp_locked_error() to distinguish between the
690 	 * ERRORS_CONT and ERRORS_RO case, and perhaps return more
691 	 * aggressively from the ext4 function in question, with a
692 	 * more appropriate error code.
693 	 */
694 	ext4_lock_group(sb, grp);
695 	return;
696 }
697 
ext4_update_dynamic_rev(struct super_block * sb)698 void ext4_update_dynamic_rev(struct super_block *sb)
699 {
700 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
701 
702 	if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
703 		return;
704 
705 	ext4_warning(sb,
706 		     "updating to rev %d because of new feature flag, "
707 		     "running e2fsck is recommended",
708 		     EXT4_DYNAMIC_REV);
709 
710 	es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
711 	es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
712 	es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
713 	/* leave es->s_feature_*compat flags alone */
714 	/* es->s_uuid will be set by e2fsck if empty */
715 
716 	/*
717 	 * The rest of the superblock fields should be zero, and if not it
718 	 * means they are likely already in use, so leave them alone.  We
719 	 * can leave it up to e2fsck to clean up any inconsistencies there.
720 	 */
721 }
722 
723 /*
724  * Open the external journal device
725  */
ext4_blkdev_get(dev_t dev,struct super_block * sb)726 static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
727 {
728 	struct block_device *bdev;
729 	char b[BDEVNAME_SIZE];
730 
731 	bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
732 	if (IS_ERR(bdev))
733 		goto fail;
734 	return bdev;
735 
736 fail:
737 	ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
738 			__bdevname(dev, b), PTR_ERR(bdev));
739 	return NULL;
740 }
741 
742 /*
743  * Release the journal device
744  */
ext4_blkdev_put(struct block_device * bdev)745 static void ext4_blkdev_put(struct block_device *bdev)
746 {
747 	blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
748 }
749 
ext4_blkdev_remove(struct ext4_sb_info * sbi)750 static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
751 {
752 	struct block_device *bdev;
753 	bdev = sbi->journal_bdev;
754 	if (bdev) {
755 		ext4_blkdev_put(bdev);
756 		sbi->journal_bdev = NULL;
757 	}
758 }
759 
orphan_list_entry(struct list_head * l)760 static inline struct inode *orphan_list_entry(struct list_head *l)
761 {
762 	return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
763 }
764 
dump_orphan_list(struct super_block * sb,struct ext4_sb_info * sbi)765 static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
766 {
767 	struct list_head *l;
768 
769 	ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
770 		 le32_to_cpu(sbi->s_es->s_last_orphan));
771 
772 	printk(KERN_ERR "sb_info orphan list:\n");
773 	list_for_each(l, &sbi->s_orphan) {
774 		struct inode *inode = orphan_list_entry(l);
775 		printk(KERN_ERR "  "
776 		       "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
777 		       inode->i_sb->s_id, inode->i_ino, inode,
778 		       inode->i_mode, inode->i_nlink,
779 		       NEXT_ORPHAN(inode));
780 	}
781 }
782 
ext4_put_super(struct super_block * sb)783 static void ext4_put_super(struct super_block *sb)
784 {
785 	struct ext4_sb_info *sbi = EXT4_SB(sb);
786 	struct ext4_super_block *es = sbi->s_es;
787 	int i, err;
788 
789 	ext4_unregister_li_request(sb);
790 	dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
791 
792 	flush_workqueue(sbi->rsv_conversion_wq);
793 	destroy_workqueue(sbi->rsv_conversion_wq);
794 
795 	if (sbi->s_journal) {
796 		err = jbd2_journal_destroy(sbi->s_journal);
797 		sbi->s_journal = NULL;
798 		if (err < 0)
799 			ext4_abort(sb, "Couldn't clean up the journal");
800 	}
801 
802 	ext4_es_unregister_shrinker(sbi);
803 	del_timer_sync(&sbi->s_err_report);
804 	ext4_release_system_zone(sb);
805 	ext4_mb_release(sb);
806 	ext4_ext_release(sb);
807 	ext4_xattr_put_super(sb);
808 
809 	if (!(sb->s_flags & MS_RDONLY)) {
810 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
811 		es->s_state = cpu_to_le16(sbi->s_mount_state);
812 	}
813 	if (!(sb->s_flags & MS_RDONLY))
814 		ext4_commit_super(sb, 1);
815 
816 	if (sbi->s_proc) {
817 		remove_proc_entry("options", sbi->s_proc);
818 		remove_proc_entry(sb->s_id, ext4_proc_root);
819 	}
820 	kobject_del(&sbi->s_kobj);
821 
822 	for (i = 0; i < sbi->s_gdb_count; i++)
823 		brelse(sbi->s_group_desc[i]);
824 	ext4_kvfree(sbi->s_group_desc);
825 	ext4_kvfree(sbi->s_flex_groups);
826 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
827 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
828 	percpu_counter_destroy(&sbi->s_dirs_counter);
829 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
830 	brelse(sbi->s_sbh);
831 #ifdef CONFIG_QUOTA
832 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
833 		kfree(sbi->s_qf_names[i]);
834 #endif
835 
836 	/* Debugging code just in case the in-memory inode orphan list
837 	 * isn't empty.  The on-disk one can be non-empty if we've
838 	 * detected an error and taken the fs readonly, but the
839 	 * in-memory list had better be clean by this point. */
840 	if (!list_empty(&sbi->s_orphan))
841 		dump_orphan_list(sb, sbi);
842 	J_ASSERT(list_empty(&sbi->s_orphan));
843 
844 	sync_blockdev(sb->s_bdev);
845 	invalidate_bdev(sb->s_bdev);
846 	if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
847 		/*
848 		 * Invalidate the journal device's buffers.  We don't want them
849 		 * floating about in memory - the physical journal device may
850 		 * hotswapped, and it breaks the `ro-after' testing code.
851 		 */
852 		sync_blockdev(sbi->journal_bdev);
853 		invalidate_bdev(sbi->journal_bdev);
854 		ext4_blkdev_remove(sbi);
855 	}
856 	if (sbi->s_mb_cache) {
857 		ext4_xattr_destroy_cache(sbi->s_mb_cache);
858 		sbi->s_mb_cache = NULL;
859 	}
860 	if (sbi->s_mmp_tsk)
861 		kthread_stop(sbi->s_mmp_tsk);
862 	sb->s_fs_info = NULL;
863 	/*
864 	 * Now that we are completely done shutting down the
865 	 * superblock, we need to actually destroy the kobject.
866 	 */
867 	kobject_put(&sbi->s_kobj);
868 	wait_for_completion(&sbi->s_kobj_unregister);
869 	if (sbi->s_chksum_driver)
870 		crypto_free_shash(sbi->s_chksum_driver);
871 	kfree(sbi->s_blockgroup_lock);
872 	kfree(sbi);
873 }
874 
875 static struct kmem_cache *ext4_inode_cachep;
876 
877 /*
878  * Called inside transaction, so use GFP_NOFS
879  */
ext4_alloc_inode(struct super_block * sb)880 static struct inode *ext4_alloc_inode(struct super_block *sb)
881 {
882 	struct ext4_inode_info *ei;
883 
884 	ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
885 	if (!ei)
886 		return NULL;
887 
888 	ei->vfs_inode.i_version = 1;
889 	spin_lock_init(&ei->i_raw_lock);
890 	INIT_LIST_HEAD(&ei->i_prealloc_list);
891 	spin_lock_init(&ei->i_prealloc_lock);
892 	ext4_es_init_tree(&ei->i_es_tree);
893 	rwlock_init(&ei->i_es_lock);
894 	INIT_LIST_HEAD(&ei->i_es_lru);
895 	ei->i_es_all_nr = 0;
896 	ei->i_es_lru_nr = 0;
897 	ei->i_touch_when = 0;
898 	ei->i_reserved_data_blocks = 0;
899 	ei->i_reserved_meta_blocks = 0;
900 	ei->i_allocated_meta_blocks = 0;
901 	ei->i_da_metadata_calc_len = 0;
902 	ei->i_da_metadata_calc_last_lblock = 0;
903 	spin_lock_init(&(ei->i_block_reservation_lock));
904 #ifdef CONFIG_QUOTA
905 	ei->i_reserved_quota = 0;
906 #endif
907 	ei->jinode = NULL;
908 	INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
909 	spin_lock_init(&ei->i_completed_io_lock);
910 	ei->i_sync_tid = 0;
911 	ei->i_datasync_tid = 0;
912 	atomic_set(&ei->i_ioend_count, 0);
913 	atomic_set(&ei->i_unwritten, 0);
914 	INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
915 #ifdef CONFIG_EXT4_FS_ENCRYPTION
916 	ei->i_crypt_info = NULL;
917 #endif
918 	return &ei->vfs_inode;
919 }
920 
ext4_drop_inode(struct inode * inode)921 static int ext4_drop_inode(struct inode *inode)
922 {
923 	int drop = generic_drop_inode(inode);
924 
925 	trace_ext4_drop_inode(inode, drop);
926 	return drop;
927 }
928 
ext4_i_callback(struct rcu_head * head)929 static void ext4_i_callback(struct rcu_head *head)
930 {
931 	struct inode *inode = container_of(head, struct inode, i_rcu);
932 	kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
933 }
934 
ext4_destroy_inode(struct inode * inode)935 static void ext4_destroy_inode(struct inode *inode)
936 {
937 	if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
938 		ext4_msg(inode->i_sb, KERN_ERR,
939 			 "Inode %lu (%p): orphan list check failed!",
940 			 inode->i_ino, EXT4_I(inode));
941 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
942 				EXT4_I(inode), sizeof(struct ext4_inode_info),
943 				true);
944 		dump_stack();
945 	}
946 	call_rcu(&inode->i_rcu, ext4_i_callback);
947 }
948 
init_once(void * foo)949 static void init_once(void *foo)
950 {
951 	struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
952 
953 	INIT_LIST_HEAD(&ei->i_orphan);
954 	init_rwsem(&ei->xattr_sem);
955 	init_rwsem(&ei->i_data_sem);
956 	inode_init_once(&ei->vfs_inode);
957 }
958 
init_inodecache(void)959 static int __init init_inodecache(void)
960 {
961 	ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
962 					     sizeof(struct ext4_inode_info),
963 					     0, (SLAB_RECLAIM_ACCOUNT|
964 						SLAB_MEM_SPREAD),
965 					     init_once);
966 	if (ext4_inode_cachep == NULL)
967 		return -ENOMEM;
968 	return 0;
969 }
970 
destroy_inodecache(void)971 static void destroy_inodecache(void)
972 {
973 	/*
974 	 * Make sure all delayed rcu free inodes are flushed before we
975 	 * destroy cache.
976 	 */
977 	rcu_barrier();
978 	kmem_cache_destroy(ext4_inode_cachep);
979 }
980 
ext4_clear_inode(struct inode * inode)981 void ext4_clear_inode(struct inode *inode)
982 {
983 	invalidate_inode_buffers(inode);
984 	clear_inode(inode);
985 	dquot_drop(inode);
986 	ext4_discard_preallocations(inode);
987 	ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
988 	ext4_es_lru_del(inode);
989 	if (EXT4_I(inode)->jinode) {
990 		jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
991 					       EXT4_I(inode)->jinode);
992 		jbd2_free_inode(EXT4_I(inode)->jinode);
993 		EXT4_I(inode)->jinode = NULL;
994 	}
995 #ifdef CONFIG_EXT4_FS_ENCRYPTION
996 	if (EXT4_I(inode)->i_crypt_info)
997 		ext4_free_encryption_info(inode, EXT4_I(inode)->i_crypt_info);
998 #endif
999 }
1000 
ext4_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)1001 static struct inode *ext4_nfs_get_inode(struct super_block *sb,
1002 					u64 ino, u32 generation)
1003 {
1004 	struct inode *inode;
1005 
1006 	if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
1007 		return ERR_PTR(-ESTALE);
1008 	if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
1009 		return ERR_PTR(-ESTALE);
1010 
1011 	/* iget isn't really right if the inode is currently unallocated!!
1012 	 *
1013 	 * ext4_read_inode will return a bad_inode if the inode had been
1014 	 * deleted, so we should be safe.
1015 	 *
1016 	 * Currently we don't know the generation for parent directory, so
1017 	 * a generation of 0 means "accept any"
1018 	 */
1019 	inode = ext4_iget_normal(sb, ino);
1020 	if (IS_ERR(inode))
1021 		return ERR_CAST(inode);
1022 	if (generation && inode->i_generation != generation) {
1023 		iput(inode);
1024 		return ERR_PTR(-ESTALE);
1025 	}
1026 
1027 	return inode;
1028 }
1029 
ext4_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1030 static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
1031 					int fh_len, int fh_type)
1032 {
1033 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
1034 				    ext4_nfs_get_inode);
1035 }
1036 
ext4_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)1037 static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
1038 					int fh_len, int fh_type)
1039 {
1040 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
1041 				    ext4_nfs_get_inode);
1042 }
1043 
1044 /*
1045  * Try to release metadata pages (indirect blocks, directories) which are
1046  * mapped via the block device.  Since these pages could have journal heads
1047  * which would prevent try_to_free_buffers() from freeing them, we must use
1048  * jbd2 layer's try_to_free_buffers() function to release them.
1049  */
bdev_try_to_free_page(struct super_block * sb,struct page * page,gfp_t wait)1050 static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
1051 				 gfp_t wait)
1052 {
1053 	journal_t *journal = EXT4_SB(sb)->s_journal;
1054 
1055 	WARN_ON(PageChecked(page));
1056 	if (!page_has_buffers(page))
1057 		return 0;
1058 	if (journal)
1059 		return jbd2_journal_try_to_free_buffers(journal, page,
1060 							wait & ~__GFP_WAIT);
1061 	return try_to_free_buffers(page);
1062 }
1063 
1064 #ifdef CONFIG_QUOTA
1065 #define QTYPE2NAME(t) ((t) == USRQUOTA ? "user" : "group")
1066 #define QTYPE2MOPT(on, t) ((t) == USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
1067 
1068 static int ext4_write_dquot(struct dquot *dquot);
1069 static int ext4_acquire_dquot(struct dquot *dquot);
1070 static int ext4_release_dquot(struct dquot *dquot);
1071 static int ext4_mark_dquot_dirty(struct dquot *dquot);
1072 static int ext4_write_info(struct super_block *sb, int type);
1073 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
1074 			 struct path *path);
1075 static int ext4_quota_on_sysfile(struct super_block *sb, int type,
1076 				 int format_id);
1077 static int ext4_quota_off(struct super_block *sb, int type);
1078 static int ext4_quota_off_sysfile(struct super_block *sb, int type);
1079 static int ext4_quota_on_mount(struct super_block *sb, int type);
1080 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
1081 			       size_t len, loff_t off);
1082 static ssize_t ext4_quota_write(struct super_block *sb, int type,
1083 				const char *data, size_t len, loff_t off);
1084 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
1085 			     unsigned int flags);
1086 static int ext4_enable_quotas(struct super_block *sb);
1087 
1088 static const struct dquot_operations ext4_quota_operations = {
1089 	.get_reserved_space = ext4_get_reserved_space,
1090 	.write_dquot	= ext4_write_dquot,
1091 	.acquire_dquot	= ext4_acquire_dquot,
1092 	.release_dquot	= ext4_release_dquot,
1093 	.mark_dirty	= ext4_mark_dquot_dirty,
1094 	.write_info	= ext4_write_info,
1095 	.alloc_dquot	= dquot_alloc,
1096 	.destroy_dquot	= dquot_destroy,
1097 };
1098 
1099 static const struct quotactl_ops ext4_qctl_operations = {
1100 	.quota_on	= ext4_quota_on,
1101 	.quota_off	= ext4_quota_off,
1102 	.quota_sync	= dquot_quota_sync,
1103 	.get_info	= dquot_get_dqinfo,
1104 	.set_info	= dquot_set_dqinfo,
1105 	.get_dqblk	= dquot_get_dqblk,
1106 	.set_dqblk	= dquot_set_dqblk
1107 };
1108 
1109 static const struct quotactl_ops ext4_qctl_sysfile_operations = {
1110 	.quota_on_meta	= ext4_quota_on_sysfile,
1111 	.quota_off	= ext4_quota_off_sysfile,
1112 	.quota_sync	= dquot_quota_sync,
1113 	.get_info	= dquot_get_dqinfo,
1114 	.set_info	= dquot_set_dqinfo,
1115 	.get_dqblk	= dquot_get_dqblk,
1116 	.set_dqblk	= dquot_set_dqblk
1117 };
1118 #endif
1119 
1120 static const struct super_operations ext4_sops = {
1121 	.alloc_inode	= ext4_alloc_inode,
1122 	.destroy_inode	= ext4_destroy_inode,
1123 	.write_inode	= ext4_write_inode,
1124 	.dirty_inode	= ext4_dirty_inode,
1125 	.drop_inode	= ext4_drop_inode,
1126 	.evict_inode	= ext4_evict_inode,
1127 	.put_super	= ext4_put_super,
1128 	.sync_fs	= ext4_sync_fs,
1129 	.freeze_fs	= ext4_freeze,
1130 	.unfreeze_fs	= ext4_unfreeze,
1131 	.statfs		= ext4_statfs,
1132 	.remount_fs	= ext4_remount,
1133 	.show_options	= ext4_show_options,
1134 #ifdef CONFIG_QUOTA
1135 	.quota_read	= ext4_quota_read,
1136 	.quota_write	= ext4_quota_write,
1137 #endif
1138 	.bdev_try_to_free_page = bdev_try_to_free_page,
1139 };
1140 
1141 static const struct export_operations ext4_export_ops = {
1142 	.fh_to_dentry = ext4_fh_to_dentry,
1143 	.fh_to_parent = ext4_fh_to_parent,
1144 	.get_parent = ext4_get_parent,
1145 };
1146 
1147 enum {
1148 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
1149 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
1150 	Opt_nouid32, Opt_debug, Opt_removed,
1151 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
1152 	Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
1153 	Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
1154 	Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
1155 	Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
1156 	Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
1157 	Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
1158 	Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
1159 	Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
1160 	Opt_usrquota, Opt_grpquota, Opt_i_version,
1161 	Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
1162 	Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
1163 	Opt_inode_readahead_blks, Opt_journal_ioprio,
1164 	Opt_dioread_nolock, Opt_dioread_lock,
1165 	Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
1166 	Opt_max_dir_size_kb,
1167 };
1168 
1169 static const match_table_t tokens = {
1170 	{Opt_bsd_df, "bsddf"},
1171 	{Opt_minix_df, "minixdf"},
1172 	{Opt_grpid, "grpid"},
1173 	{Opt_grpid, "bsdgroups"},
1174 	{Opt_nogrpid, "nogrpid"},
1175 	{Opt_nogrpid, "sysvgroups"},
1176 	{Opt_resgid, "resgid=%u"},
1177 	{Opt_resuid, "resuid=%u"},
1178 	{Opt_sb, "sb=%u"},
1179 	{Opt_err_cont, "errors=continue"},
1180 	{Opt_err_panic, "errors=panic"},
1181 	{Opt_err_ro, "errors=remount-ro"},
1182 	{Opt_nouid32, "nouid32"},
1183 	{Opt_debug, "debug"},
1184 	{Opt_removed, "oldalloc"},
1185 	{Opt_removed, "orlov"},
1186 	{Opt_user_xattr, "user_xattr"},
1187 	{Opt_nouser_xattr, "nouser_xattr"},
1188 	{Opt_acl, "acl"},
1189 	{Opt_noacl, "noacl"},
1190 	{Opt_noload, "norecovery"},
1191 	{Opt_noload, "noload"},
1192 	{Opt_removed, "nobh"},
1193 	{Opt_removed, "bh"},
1194 	{Opt_commit, "commit=%u"},
1195 	{Opt_min_batch_time, "min_batch_time=%u"},
1196 	{Opt_max_batch_time, "max_batch_time=%u"},
1197 	{Opt_journal_dev, "journal_dev=%u"},
1198 	{Opt_journal_path, "journal_path=%s"},
1199 	{Opt_journal_checksum, "journal_checksum"},
1200 	{Opt_journal_async_commit, "journal_async_commit"},
1201 	{Opt_abort, "abort"},
1202 	{Opt_data_journal, "data=journal"},
1203 	{Opt_data_ordered, "data=ordered"},
1204 	{Opt_data_writeback, "data=writeback"},
1205 	{Opt_data_err_abort, "data_err=abort"},
1206 	{Opt_data_err_ignore, "data_err=ignore"},
1207 	{Opt_offusrjquota, "usrjquota="},
1208 	{Opt_usrjquota, "usrjquota=%s"},
1209 	{Opt_offgrpjquota, "grpjquota="},
1210 	{Opt_grpjquota, "grpjquota=%s"},
1211 	{Opt_jqfmt_vfsold, "jqfmt=vfsold"},
1212 	{Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
1213 	{Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
1214 	{Opt_grpquota, "grpquota"},
1215 	{Opt_noquota, "noquota"},
1216 	{Opt_quota, "quota"},
1217 	{Opt_usrquota, "usrquota"},
1218 	{Opt_barrier, "barrier=%u"},
1219 	{Opt_barrier, "barrier"},
1220 	{Opt_nobarrier, "nobarrier"},
1221 	{Opt_i_version, "i_version"},
1222 	{Opt_stripe, "stripe=%u"},
1223 	{Opt_delalloc, "delalloc"},
1224 	{Opt_nodelalloc, "nodelalloc"},
1225 	{Opt_removed, "mblk_io_submit"},
1226 	{Opt_removed, "nomblk_io_submit"},
1227 	{Opt_block_validity, "block_validity"},
1228 	{Opt_noblock_validity, "noblock_validity"},
1229 	{Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
1230 	{Opt_journal_ioprio, "journal_ioprio=%u"},
1231 	{Opt_auto_da_alloc, "auto_da_alloc=%u"},
1232 	{Opt_auto_da_alloc, "auto_da_alloc"},
1233 	{Opt_noauto_da_alloc, "noauto_da_alloc"},
1234 	{Opt_dioread_nolock, "dioread_nolock"},
1235 	{Opt_dioread_lock, "dioread_lock"},
1236 	{Opt_discard, "discard"},
1237 	{Opt_nodiscard, "nodiscard"},
1238 	{Opt_init_itable, "init_itable=%u"},
1239 	{Opt_init_itable, "init_itable"},
1240 	{Opt_noinit_itable, "noinit_itable"},
1241 	{Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
1242 	{Opt_test_dummy_encryption, "test_dummy_encryption"},
1243 	{Opt_removed, "check=none"},	/* mount option from ext2/3 */
1244 	{Opt_removed, "nocheck"},	/* mount option from ext2/3 */
1245 	{Opt_removed, "reservation"},	/* mount option from ext2/3 */
1246 	{Opt_removed, "noreservation"}, /* mount option from ext2/3 */
1247 	{Opt_removed, "journal=%u"},	/* mount option from ext2/3 */
1248 	{Opt_err, NULL},
1249 };
1250 
get_sb_block(void ** data)1251 static ext4_fsblk_t get_sb_block(void **data)
1252 {
1253 	ext4_fsblk_t	sb_block;
1254 	char		*options = (char *) *data;
1255 
1256 	if (!options || strncmp(options, "sb=", 3) != 0)
1257 		return 1;	/* Default location */
1258 
1259 	options += 3;
1260 	/* TODO: use simple_strtoll with >32bit ext4 */
1261 	sb_block = simple_strtoul(options, &options, 0);
1262 	if (*options && *options != ',') {
1263 		printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
1264 		       (char *) *data);
1265 		return 1;
1266 	}
1267 	if (*options == ',')
1268 		options++;
1269 	*data = (void *) options;
1270 
1271 	return sb_block;
1272 }
1273 
1274 #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
1275 static char deprecated_msg[] = "Mount option \"%s\" will be removed by %s\n"
1276 	"Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
1277 
1278 #ifdef CONFIG_QUOTA
set_qf_name(struct super_block * sb,int qtype,substring_t * args)1279 static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
1280 {
1281 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1282 	char *qname;
1283 	int ret = -1;
1284 
1285 	if (sb_any_quota_loaded(sb) &&
1286 		!sbi->s_qf_names[qtype]) {
1287 		ext4_msg(sb, KERN_ERR,
1288 			"Cannot change journaled "
1289 			"quota options when quota turned on");
1290 		return -1;
1291 	}
1292 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1293 		ext4_msg(sb, KERN_INFO, "Journaled quota options "
1294 			 "ignored when QUOTA feature is enabled");
1295 		return 1;
1296 	}
1297 	qname = match_strdup(args);
1298 	if (!qname) {
1299 		ext4_msg(sb, KERN_ERR,
1300 			"Not enough memory for storing quotafile name");
1301 		return -1;
1302 	}
1303 	if (sbi->s_qf_names[qtype]) {
1304 		if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
1305 			ret = 1;
1306 		else
1307 			ext4_msg(sb, KERN_ERR,
1308 				 "%s quota file already specified",
1309 				 QTYPE2NAME(qtype));
1310 		goto errout;
1311 	}
1312 	if (strchr(qname, '/')) {
1313 		ext4_msg(sb, KERN_ERR,
1314 			"quotafile must be on filesystem root");
1315 		goto errout;
1316 	}
1317 	sbi->s_qf_names[qtype] = qname;
1318 	set_opt(sb, QUOTA);
1319 	return 1;
1320 errout:
1321 	kfree(qname);
1322 	return ret;
1323 }
1324 
clear_qf_name(struct super_block * sb,int qtype)1325 static int clear_qf_name(struct super_block *sb, int qtype)
1326 {
1327 
1328 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1329 
1330 	if (sb_any_quota_loaded(sb) &&
1331 		sbi->s_qf_names[qtype]) {
1332 		ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
1333 			" when quota turned on");
1334 		return -1;
1335 	}
1336 	kfree(sbi->s_qf_names[qtype]);
1337 	sbi->s_qf_names[qtype] = NULL;
1338 	return 1;
1339 }
1340 #endif
1341 
1342 #define MOPT_SET	0x0001
1343 #define MOPT_CLEAR	0x0002
1344 #define MOPT_NOSUPPORT	0x0004
1345 #define MOPT_EXPLICIT	0x0008
1346 #define MOPT_CLEAR_ERR	0x0010
1347 #define MOPT_GTE0	0x0020
1348 #ifdef CONFIG_QUOTA
1349 #define MOPT_Q		0
1350 #define MOPT_QFMT	0x0040
1351 #else
1352 #define MOPT_Q		MOPT_NOSUPPORT
1353 #define MOPT_QFMT	MOPT_NOSUPPORT
1354 #endif
1355 #define MOPT_DATAJ	0x0080
1356 #define MOPT_NO_EXT2	0x0100
1357 #define MOPT_NO_EXT3	0x0200
1358 #define MOPT_EXT4_ONLY	(MOPT_NO_EXT2 | MOPT_NO_EXT3)
1359 #define MOPT_STRING	0x0400
1360 
1361 static const struct mount_opts {
1362 	int	token;
1363 	int	mount_opt;
1364 	int	flags;
1365 } ext4_mount_opts[] = {
1366 	{Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
1367 	{Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
1368 	{Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
1369 	{Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
1370 	{Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
1371 	{Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
1372 	{Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
1373 	 MOPT_EXT4_ONLY | MOPT_SET},
1374 	{Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
1375 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1376 	{Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
1377 	{Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
1378 	{Opt_delalloc, EXT4_MOUNT_DELALLOC,
1379 	 MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
1380 	{Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
1381 	 MOPT_EXT4_ONLY | MOPT_CLEAR},
1382 	{Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
1383 	 MOPT_EXT4_ONLY | MOPT_SET},
1384 	{Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
1385 				    EXT4_MOUNT_JOURNAL_CHECKSUM),
1386 	 MOPT_EXT4_ONLY | MOPT_SET},
1387 	{Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
1388 	{Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
1389 	{Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
1390 	{Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
1391 	{Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
1392 	 MOPT_NO_EXT2 | MOPT_SET},
1393 	{Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
1394 	 MOPT_NO_EXT2 | MOPT_CLEAR},
1395 	{Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
1396 	{Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
1397 	{Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
1398 	{Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
1399 	{Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
1400 	{Opt_commit, 0, MOPT_GTE0},
1401 	{Opt_max_batch_time, 0, MOPT_GTE0},
1402 	{Opt_min_batch_time, 0, MOPT_GTE0},
1403 	{Opt_inode_readahead_blks, 0, MOPT_GTE0},
1404 	{Opt_init_itable, 0, MOPT_GTE0},
1405 	{Opt_stripe, 0, MOPT_GTE0},
1406 	{Opt_resuid, 0, MOPT_GTE0},
1407 	{Opt_resgid, 0, MOPT_GTE0},
1408 	{Opt_journal_dev, 0, MOPT_GTE0},
1409 	{Opt_journal_path, 0, MOPT_STRING},
1410 	{Opt_journal_ioprio, 0, MOPT_GTE0},
1411 	{Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1412 	{Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
1413 	{Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
1414 	 MOPT_NO_EXT2 | MOPT_DATAJ},
1415 	{Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
1416 	{Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
1417 #ifdef CONFIG_EXT4_FS_POSIX_ACL
1418 	{Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
1419 	{Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
1420 #else
1421 	{Opt_acl, 0, MOPT_NOSUPPORT},
1422 	{Opt_noacl, 0, MOPT_NOSUPPORT},
1423 #endif
1424 	{Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
1425 	{Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
1426 	{Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
1427 	{Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
1428 							MOPT_SET | MOPT_Q},
1429 	{Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
1430 							MOPT_SET | MOPT_Q},
1431 	{Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
1432 		       EXT4_MOUNT_GRPQUOTA), MOPT_CLEAR | MOPT_Q},
1433 	{Opt_usrjquota, 0, MOPT_Q},
1434 	{Opt_grpjquota, 0, MOPT_Q},
1435 	{Opt_offusrjquota, 0, MOPT_Q},
1436 	{Opt_offgrpjquota, 0, MOPT_Q},
1437 	{Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
1438 	{Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
1439 	{Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
1440 	{Opt_max_dir_size_kb, 0, MOPT_GTE0},
1441 	{Opt_test_dummy_encryption, 0, MOPT_GTE0},
1442 	{Opt_err, 0, 0}
1443 };
1444 
handle_mount_opt(struct super_block * sb,char * opt,int token,substring_t * args,unsigned long * journal_devnum,unsigned int * journal_ioprio,int is_remount)1445 static int handle_mount_opt(struct super_block *sb, char *opt, int token,
1446 			    substring_t *args, unsigned long *journal_devnum,
1447 			    unsigned int *journal_ioprio, int is_remount)
1448 {
1449 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1450 	const struct mount_opts *m;
1451 	kuid_t uid;
1452 	kgid_t gid;
1453 	int arg = 0;
1454 
1455 #ifdef CONFIG_QUOTA
1456 	if (token == Opt_usrjquota)
1457 		return set_qf_name(sb, USRQUOTA, &args[0]);
1458 	else if (token == Opt_grpjquota)
1459 		return set_qf_name(sb, GRPQUOTA, &args[0]);
1460 	else if (token == Opt_offusrjquota)
1461 		return clear_qf_name(sb, USRQUOTA);
1462 	else if (token == Opt_offgrpjquota)
1463 		return clear_qf_name(sb, GRPQUOTA);
1464 #endif
1465 	switch (token) {
1466 	case Opt_noacl:
1467 	case Opt_nouser_xattr:
1468 		ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
1469 		break;
1470 	case Opt_sb:
1471 		return 1;	/* handled by get_sb_block() */
1472 	case Opt_removed:
1473 		ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
1474 		return 1;
1475 	case Opt_abort:
1476 		sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
1477 		return 1;
1478 	case Opt_i_version:
1479 		sb->s_flags |= MS_I_VERSION;
1480 		return 1;
1481 	}
1482 
1483 	for (m = ext4_mount_opts; m->token != Opt_err; m++)
1484 		if (token == m->token)
1485 			break;
1486 
1487 	if (m->token == Opt_err) {
1488 		ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
1489 			 "or missing value", opt);
1490 		return -1;
1491 	}
1492 
1493 	if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
1494 		ext4_msg(sb, KERN_ERR,
1495 			 "Mount option \"%s\" incompatible with ext2", opt);
1496 		return -1;
1497 	}
1498 	if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
1499 		ext4_msg(sb, KERN_ERR,
1500 			 "Mount option \"%s\" incompatible with ext3", opt);
1501 		return -1;
1502 	}
1503 
1504 	if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
1505 		return -1;
1506 	if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
1507 		return -1;
1508 	if (m->flags & MOPT_EXPLICIT)
1509 		set_opt2(sb, EXPLICIT_DELALLOC);
1510 	if (m->flags & MOPT_CLEAR_ERR)
1511 		clear_opt(sb, ERRORS_MASK);
1512 	if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
1513 		ext4_msg(sb, KERN_ERR, "Cannot change quota "
1514 			 "options when quota turned on");
1515 		return -1;
1516 	}
1517 
1518 	if (m->flags & MOPT_NOSUPPORT) {
1519 		ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
1520 	} else if (token == Opt_commit) {
1521 		if (arg == 0)
1522 			arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
1523 		sbi->s_commit_interval = HZ * arg;
1524 	} else if (token == Opt_max_batch_time) {
1525 		sbi->s_max_batch_time = arg;
1526 	} else if (token == Opt_min_batch_time) {
1527 		sbi->s_min_batch_time = arg;
1528 	} else if (token == Opt_inode_readahead_blks) {
1529 		if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
1530 			ext4_msg(sb, KERN_ERR,
1531 				 "EXT4-fs: inode_readahead_blks must be "
1532 				 "0 or a power of 2 smaller than 2^31");
1533 			return -1;
1534 		}
1535 		sbi->s_inode_readahead_blks = arg;
1536 	} else if (token == Opt_init_itable) {
1537 		set_opt(sb, INIT_INODE_TABLE);
1538 		if (!args->from)
1539 			arg = EXT4_DEF_LI_WAIT_MULT;
1540 		sbi->s_li_wait_mult = arg;
1541 	} else if (token == Opt_max_dir_size_kb) {
1542 		sbi->s_max_dir_size_kb = arg;
1543 	} else if (token == Opt_stripe) {
1544 		sbi->s_stripe = arg;
1545 	} else if (token == Opt_resuid) {
1546 		uid = make_kuid(current_user_ns(), arg);
1547 		if (!uid_valid(uid)) {
1548 			ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
1549 			return -1;
1550 		}
1551 		sbi->s_resuid = uid;
1552 	} else if (token == Opt_resgid) {
1553 		gid = make_kgid(current_user_ns(), arg);
1554 		if (!gid_valid(gid)) {
1555 			ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
1556 			return -1;
1557 		}
1558 		sbi->s_resgid = gid;
1559 	} else if (token == Opt_journal_dev) {
1560 		if (is_remount) {
1561 			ext4_msg(sb, KERN_ERR,
1562 				 "Cannot specify journal on remount");
1563 			return -1;
1564 		}
1565 		*journal_devnum = arg;
1566 	} else if (token == Opt_journal_path) {
1567 		char *journal_path;
1568 		struct inode *journal_inode;
1569 		struct path path;
1570 		int error;
1571 
1572 		if (is_remount) {
1573 			ext4_msg(sb, KERN_ERR,
1574 				 "Cannot specify journal on remount");
1575 			return -1;
1576 		}
1577 		journal_path = match_strdup(&args[0]);
1578 		if (!journal_path) {
1579 			ext4_msg(sb, KERN_ERR, "error: could not dup "
1580 				"journal device string");
1581 			return -1;
1582 		}
1583 
1584 		error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
1585 		if (error) {
1586 			ext4_msg(sb, KERN_ERR, "error: could not find "
1587 				"journal device path: error %d", error);
1588 			kfree(journal_path);
1589 			return -1;
1590 		}
1591 
1592 		journal_inode = path.dentry->d_inode;
1593 		if (!S_ISBLK(journal_inode->i_mode)) {
1594 			ext4_msg(sb, KERN_ERR, "error: journal path %s "
1595 				"is not a block device", journal_path);
1596 			path_put(&path);
1597 			kfree(journal_path);
1598 			return -1;
1599 		}
1600 
1601 		*journal_devnum = new_encode_dev(journal_inode->i_rdev);
1602 		path_put(&path);
1603 		kfree(journal_path);
1604 	} else if (token == Opt_journal_ioprio) {
1605 		if (arg > 7) {
1606 			ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
1607 				 " (must be 0-7)");
1608 			return -1;
1609 		}
1610 		*journal_ioprio =
1611 			IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
1612 	} else if (token == Opt_test_dummy_encryption) {
1613 #ifdef CONFIG_EXT4_FS_ENCRYPTION
1614 		sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
1615 		ext4_msg(sb, KERN_WARNING,
1616 			 "Test dummy encryption mode enabled");
1617 #else
1618 		ext4_msg(sb, KERN_WARNING,
1619 			 "Test dummy encryption mount option ignored");
1620 #endif
1621 	} else if (m->flags & MOPT_DATAJ) {
1622 		if (is_remount) {
1623 			if (!sbi->s_journal)
1624 				ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
1625 			else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
1626 				ext4_msg(sb, KERN_ERR,
1627 					 "Cannot change data mode on remount");
1628 				return -1;
1629 			}
1630 		} else {
1631 			clear_opt(sb, DATA_FLAGS);
1632 			sbi->s_mount_opt |= m->mount_opt;
1633 		}
1634 #ifdef CONFIG_QUOTA
1635 	} else if (m->flags & MOPT_QFMT) {
1636 		if (sb_any_quota_loaded(sb) &&
1637 		    sbi->s_jquota_fmt != m->mount_opt) {
1638 			ext4_msg(sb, KERN_ERR, "Cannot change journaled "
1639 				 "quota options when quota turned on");
1640 			return -1;
1641 		}
1642 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
1643 					       EXT4_FEATURE_RO_COMPAT_QUOTA)) {
1644 			ext4_msg(sb, KERN_INFO,
1645 				 "Quota format mount options ignored "
1646 				 "when QUOTA feature is enabled");
1647 			return 1;
1648 		}
1649 		sbi->s_jquota_fmt = m->mount_opt;
1650 #endif
1651 	} else {
1652 		if (!args->from)
1653 			arg = 1;
1654 		if (m->flags & MOPT_CLEAR)
1655 			arg = !arg;
1656 		else if (unlikely(!(m->flags & MOPT_SET))) {
1657 			ext4_msg(sb, KERN_WARNING,
1658 				 "buggy handling of option %s", opt);
1659 			WARN_ON(1);
1660 			return -1;
1661 		}
1662 		if (arg != 0)
1663 			sbi->s_mount_opt |= m->mount_opt;
1664 		else
1665 			sbi->s_mount_opt &= ~m->mount_opt;
1666 	}
1667 	return 1;
1668 }
1669 
parse_options(char * options,struct super_block * sb,unsigned long * journal_devnum,unsigned int * journal_ioprio,int is_remount)1670 static int parse_options(char *options, struct super_block *sb,
1671 			 unsigned long *journal_devnum,
1672 			 unsigned int *journal_ioprio,
1673 			 int is_remount)
1674 {
1675 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1676 	char *p;
1677 	substring_t args[MAX_OPT_ARGS];
1678 	int token;
1679 
1680 	if (!options)
1681 		return 1;
1682 
1683 	while ((p = strsep(&options, ",")) != NULL) {
1684 		if (!*p)
1685 			continue;
1686 		/*
1687 		 * Initialize args struct so we know whether arg was
1688 		 * found; some options take optional arguments.
1689 		 */
1690 		args[0].to = args[0].from = NULL;
1691 		token = match_token(p, tokens, args);
1692 		if (handle_mount_opt(sb, p, token, args, journal_devnum,
1693 				     journal_ioprio, is_remount) < 0)
1694 			return 0;
1695 	}
1696 #ifdef CONFIG_QUOTA
1697 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
1698 	    (test_opt(sb, USRQUOTA) || test_opt(sb, GRPQUOTA))) {
1699 		ext4_msg(sb, KERN_INFO, "Quota feature enabled, usrquota and grpquota "
1700 			 "mount options ignored.");
1701 		clear_opt(sb, USRQUOTA);
1702 		clear_opt(sb, GRPQUOTA);
1703 	} else if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
1704 		if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
1705 			clear_opt(sb, USRQUOTA);
1706 
1707 		if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
1708 			clear_opt(sb, GRPQUOTA);
1709 
1710 		if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
1711 			ext4_msg(sb, KERN_ERR, "old and new quota "
1712 					"format mixing");
1713 			return 0;
1714 		}
1715 
1716 		if (!sbi->s_jquota_fmt) {
1717 			ext4_msg(sb, KERN_ERR, "journaled quota format "
1718 					"not specified");
1719 			return 0;
1720 		}
1721 	}
1722 #endif
1723 	if (test_opt(sb, DIOREAD_NOLOCK)) {
1724 		int blocksize =
1725 			BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
1726 
1727 		if (blocksize < PAGE_CACHE_SIZE) {
1728 			ext4_msg(sb, KERN_ERR, "can't mount with "
1729 				 "dioread_nolock if block size != PAGE_SIZE");
1730 			return 0;
1731 		}
1732 	}
1733 	return 1;
1734 }
1735 
ext4_show_quota_options(struct seq_file * seq,struct super_block * sb)1736 static inline void ext4_show_quota_options(struct seq_file *seq,
1737 					   struct super_block *sb)
1738 {
1739 #if defined(CONFIG_QUOTA)
1740 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1741 
1742 	if (sbi->s_jquota_fmt) {
1743 		char *fmtname = "";
1744 
1745 		switch (sbi->s_jquota_fmt) {
1746 		case QFMT_VFS_OLD:
1747 			fmtname = "vfsold";
1748 			break;
1749 		case QFMT_VFS_V0:
1750 			fmtname = "vfsv0";
1751 			break;
1752 		case QFMT_VFS_V1:
1753 			fmtname = "vfsv1";
1754 			break;
1755 		}
1756 		seq_printf(seq, ",jqfmt=%s", fmtname);
1757 	}
1758 
1759 	if (sbi->s_qf_names[USRQUOTA])
1760 		seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
1761 
1762 	if (sbi->s_qf_names[GRPQUOTA])
1763 		seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
1764 #endif
1765 }
1766 
token2str(int token)1767 static const char *token2str(int token)
1768 {
1769 	const struct match_token *t;
1770 
1771 	for (t = tokens; t->token != Opt_err; t++)
1772 		if (t->token == token && !strchr(t->pattern, '='))
1773 			break;
1774 	return t->pattern;
1775 }
1776 
1777 /*
1778  * Show an option if
1779  *  - it's set to a non-default value OR
1780  *  - if the per-sb default is different from the global default
1781  */
_ext4_show_options(struct seq_file * seq,struct super_block * sb,int nodefs)1782 static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
1783 			      int nodefs)
1784 {
1785 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1786 	struct ext4_super_block *es = sbi->s_es;
1787 	int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
1788 	const struct mount_opts *m;
1789 	char sep = nodefs ? '\n' : ',';
1790 
1791 #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
1792 #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
1793 
1794 	if (sbi->s_sb_block != 1)
1795 		SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
1796 
1797 	for (m = ext4_mount_opts; m->token != Opt_err; m++) {
1798 		int want_set = m->flags & MOPT_SET;
1799 		if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
1800 		    (m->flags & MOPT_CLEAR_ERR))
1801 			continue;
1802 		if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
1803 			continue; /* skip if same as the default */
1804 		if ((want_set &&
1805 		     (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
1806 		    (!want_set && (sbi->s_mount_opt & m->mount_opt)))
1807 			continue; /* select Opt_noFoo vs Opt_Foo */
1808 		SEQ_OPTS_PRINT("%s", token2str(m->token));
1809 	}
1810 
1811 	if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
1812 	    le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
1813 		SEQ_OPTS_PRINT("resuid=%u",
1814 				from_kuid_munged(&init_user_ns, sbi->s_resuid));
1815 	if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
1816 	    le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
1817 		SEQ_OPTS_PRINT("resgid=%u",
1818 				from_kgid_munged(&init_user_ns, sbi->s_resgid));
1819 	def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
1820 	if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
1821 		SEQ_OPTS_PUTS("errors=remount-ro");
1822 	if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
1823 		SEQ_OPTS_PUTS("errors=continue");
1824 	if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
1825 		SEQ_OPTS_PUTS("errors=panic");
1826 	if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
1827 		SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
1828 	if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
1829 		SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
1830 	if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
1831 		SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
1832 	if (sb->s_flags & MS_I_VERSION)
1833 		SEQ_OPTS_PUTS("i_version");
1834 	if (nodefs || sbi->s_stripe)
1835 		SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
1836 	if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
1837 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
1838 			SEQ_OPTS_PUTS("data=journal");
1839 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
1840 			SEQ_OPTS_PUTS("data=ordered");
1841 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
1842 			SEQ_OPTS_PUTS("data=writeback");
1843 	}
1844 	if (nodefs ||
1845 	    sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
1846 		SEQ_OPTS_PRINT("inode_readahead_blks=%u",
1847 			       sbi->s_inode_readahead_blks);
1848 
1849 	if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
1850 		       (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
1851 		SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
1852 	if (nodefs || sbi->s_max_dir_size_kb)
1853 		SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
1854 
1855 	ext4_show_quota_options(seq, sb);
1856 	return 0;
1857 }
1858 
ext4_show_options(struct seq_file * seq,struct dentry * root)1859 static int ext4_show_options(struct seq_file *seq, struct dentry *root)
1860 {
1861 	return _ext4_show_options(seq, root->d_sb, 0);
1862 }
1863 
options_seq_show(struct seq_file * seq,void * offset)1864 static int options_seq_show(struct seq_file *seq, void *offset)
1865 {
1866 	struct super_block *sb = seq->private;
1867 	int rc;
1868 
1869 	seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
1870 	rc = _ext4_show_options(seq, sb, 1);
1871 	seq_puts(seq, "\n");
1872 	return rc;
1873 }
1874 
options_open_fs(struct inode * inode,struct file * file)1875 static int options_open_fs(struct inode *inode, struct file *file)
1876 {
1877 	return single_open(file, options_seq_show, PDE_DATA(inode));
1878 }
1879 
1880 static const struct file_operations ext4_seq_options_fops = {
1881 	.owner = THIS_MODULE,
1882 	.open = options_open_fs,
1883 	.read = seq_read,
1884 	.llseek = seq_lseek,
1885 	.release = single_release,
1886 };
1887 
ext4_setup_super(struct super_block * sb,struct ext4_super_block * es,int read_only)1888 static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
1889 			    int read_only)
1890 {
1891 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1892 	int res = 0;
1893 
1894 	if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
1895 		ext4_msg(sb, KERN_ERR, "revision level too high, "
1896 			 "forcing read-only mode");
1897 		res = MS_RDONLY;
1898 	}
1899 	if (read_only)
1900 		goto done;
1901 	if (!(sbi->s_mount_state & EXT4_VALID_FS))
1902 		ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
1903 			 "running e2fsck is recommended");
1904 	else if (sbi->s_mount_state & EXT4_ERROR_FS)
1905 		ext4_msg(sb, KERN_WARNING,
1906 			 "warning: mounting fs with errors, "
1907 			 "running e2fsck is recommended");
1908 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
1909 		 le16_to_cpu(es->s_mnt_count) >=
1910 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
1911 		ext4_msg(sb, KERN_WARNING,
1912 			 "warning: maximal mount count reached, "
1913 			 "running e2fsck is recommended");
1914 	else if (le32_to_cpu(es->s_checkinterval) &&
1915 		(le32_to_cpu(es->s_lastcheck) +
1916 			le32_to_cpu(es->s_checkinterval) <= get_seconds()))
1917 		ext4_msg(sb, KERN_WARNING,
1918 			 "warning: checktime reached, "
1919 			 "running e2fsck is recommended");
1920 	if (!sbi->s_journal)
1921 		es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
1922 	if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
1923 		es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
1924 	le16_add_cpu(&es->s_mnt_count, 1);
1925 	es->s_mtime = cpu_to_le32(get_seconds());
1926 	ext4_update_dynamic_rev(sb);
1927 	if (sbi->s_journal)
1928 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
1929 
1930 	ext4_commit_super(sb, 1);
1931 done:
1932 	if (test_opt(sb, DEBUG))
1933 		printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
1934 				"bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
1935 			sb->s_blocksize,
1936 			sbi->s_groups_count,
1937 			EXT4_BLOCKS_PER_GROUP(sb),
1938 			EXT4_INODES_PER_GROUP(sb),
1939 			sbi->s_mount_opt, sbi->s_mount_opt2);
1940 
1941 	cleancache_init_fs(sb);
1942 	return res;
1943 }
1944 
ext4_alloc_flex_bg_array(struct super_block * sb,ext4_group_t ngroup)1945 int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
1946 {
1947 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1948 	struct flex_groups *new_groups;
1949 	int size;
1950 
1951 	if (!sbi->s_log_groups_per_flex)
1952 		return 0;
1953 
1954 	size = ext4_flex_group(sbi, ngroup - 1) + 1;
1955 	if (size <= sbi->s_flex_groups_allocated)
1956 		return 0;
1957 
1958 	size = roundup_pow_of_two(size * sizeof(struct flex_groups));
1959 	new_groups = ext4_kvzalloc(size, GFP_KERNEL);
1960 	if (!new_groups) {
1961 		ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
1962 			 size / (int) sizeof(struct flex_groups));
1963 		return -ENOMEM;
1964 	}
1965 
1966 	if (sbi->s_flex_groups) {
1967 		memcpy(new_groups, sbi->s_flex_groups,
1968 		       (sbi->s_flex_groups_allocated *
1969 			sizeof(struct flex_groups)));
1970 		ext4_kvfree(sbi->s_flex_groups);
1971 	}
1972 	sbi->s_flex_groups = new_groups;
1973 	sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
1974 	return 0;
1975 }
1976 
ext4_fill_flex_info(struct super_block * sb)1977 static int ext4_fill_flex_info(struct super_block *sb)
1978 {
1979 	struct ext4_sb_info *sbi = EXT4_SB(sb);
1980 	struct ext4_group_desc *gdp = NULL;
1981 	ext4_group_t flex_group;
1982 	int i, err;
1983 
1984 	sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
1985 	if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
1986 		sbi->s_log_groups_per_flex = 0;
1987 		return 1;
1988 	}
1989 
1990 	err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
1991 	if (err)
1992 		goto failed;
1993 
1994 	for (i = 0; i < sbi->s_groups_count; i++) {
1995 		gdp = ext4_get_group_desc(sb, i, NULL);
1996 
1997 		flex_group = ext4_flex_group(sbi, i);
1998 		atomic_add(ext4_free_inodes_count(sb, gdp),
1999 			   &sbi->s_flex_groups[flex_group].free_inodes);
2000 		atomic64_add(ext4_free_group_clusters(sb, gdp),
2001 			     &sbi->s_flex_groups[flex_group].free_clusters);
2002 		atomic_add(ext4_used_dirs_count(sb, gdp),
2003 			   &sbi->s_flex_groups[flex_group].used_dirs);
2004 	}
2005 
2006 	return 1;
2007 failed:
2008 	return 0;
2009 }
2010 
ext4_group_desc_csum(struct ext4_sb_info * sbi,__u32 block_group,struct ext4_group_desc * gdp)2011 static __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
2012 				   struct ext4_group_desc *gdp)
2013 {
2014 	int offset;
2015 	__u16 crc = 0;
2016 	__le32 le_group = cpu_to_le32(block_group);
2017 
2018 	if (ext4_has_metadata_csum(sbi->s_sb)) {
2019 		/* Use new metadata_csum algorithm */
2020 		__le16 save_csum;
2021 		__u32 csum32;
2022 
2023 		save_csum = gdp->bg_checksum;
2024 		gdp->bg_checksum = 0;
2025 		csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
2026 				     sizeof(le_group));
2027 		csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp,
2028 				     sbi->s_desc_size);
2029 		gdp->bg_checksum = save_csum;
2030 
2031 		crc = csum32 & 0xFFFF;
2032 		goto out;
2033 	}
2034 
2035 	/* old crc16 code */
2036 	if (!(sbi->s_es->s_feature_ro_compat &
2037 	      cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)))
2038 		return 0;
2039 
2040 	offset = offsetof(struct ext4_group_desc, bg_checksum);
2041 
2042 	crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
2043 	crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
2044 	crc = crc16(crc, (__u8 *)gdp, offset);
2045 	offset += sizeof(gdp->bg_checksum); /* skip checksum */
2046 	/* for checksum of struct ext4_group_desc do the rest...*/
2047 	if ((sbi->s_es->s_feature_incompat &
2048 	     cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
2049 	    offset < le16_to_cpu(sbi->s_es->s_desc_size))
2050 		crc = crc16(crc, (__u8 *)gdp + offset,
2051 			    le16_to_cpu(sbi->s_es->s_desc_size) -
2052 				offset);
2053 
2054 out:
2055 	return cpu_to_le16(crc);
2056 }
2057 
ext4_group_desc_csum_verify(struct super_block * sb,__u32 block_group,struct ext4_group_desc * gdp)2058 int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
2059 				struct ext4_group_desc *gdp)
2060 {
2061 	if (ext4_has_group_desc_csum(sb) &&
2062 	    (gdp->bg_checksum != ext4_group_desc_csum(EXT4_SB(sb),
2063 						      block_group, gdp)))
2064 		return 0;
2065 
2066 	return 1;
2067 }
2068 
ext4_group_desc_csum_set(struct super_block * sb,__u32 block_group,struct ext4_group_desc * gdp)2069 void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
2070 			      struct ext4_group_desc *gdp)
2071 {
2072 	if (!ext4_has_group_desc_csum(sb))
2073 		return;
2074 	gdp->bg_checksum = ext4_group_desc_csum(EXT4_SB(sb), block_group, gdp);
2075 }
2076 
2077 /* Called at mount-time, super-block is locked */
ext4_check_descriptors(struct super_block * sb,ext4_fsblk_t sb_block,ext4_group_t * first_not_zeroed)2078 static int ext4_check_descriptors(struct super_block *sb,
2079 				  ext4_fsblk_t sb_block,
2080 				  ext4_group_t *first_not_zeroed)
2081 {
2082 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2083 	ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
2084 	ext4_fsblk_t last_block;
2085 	ext4_fsblk_t block_bitmap;
2086 	ext4_fsblk_t inode_bitmap;
2087 	ext4_fsblk_t inode_table;
2088 	int flexbg_flag = 0;
2089 	ext4_group_t i, grp = sbi->s_groups_count;
2090 
2091 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
2092 		flexbg_flag = 1;
2093 
2094 	ext4_debug("Checking group descriptors");
2095 
2096 	for (i = 0; i < sbi->s_groups_count; i++) {
2097 		struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
2098 
2099 		if (i == sbi->s_groups_count - 1 || flexbg_flag)
2100 			last_block = ext4_blocks_count(sbi->s_es) - 1;
2101 		else
2102 			last_block = first_block +
2103 				(EXT4_BLOCKS_PER_GROUP(sb) - 1);
2104 
2105 		if ((grp == sbi->s_groups_count) &&
2106 		   !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2107 			grp = i;
2108 
2109 		block_bitmap = ext4_block_bitmap(sb, gdp);
2110 		if (block_bitmap == sb_block) {
2111 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2112 				 "Block bitmap for group %u overlaps "
2113 				 "superblock", i);
2114 		}
2115 		if (block_bitmap < first_block || block_bitmap > last_block) {
2116 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2117 			       "Block bitmap for group %u not in group "
2118 			       "(block %llu)!", i, block_bitmap);
2119 			return 0;
2120 		}
2121 		inode_bitmap = ext4_inode_bitmap(sb, gdp);
2122 		if (inode_bitmap == sb_block) {
2123 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2124 				 "Inode bitmap for group %u overlaps "
2125 				 "superblock", i);
2126 		}
2127 		if (inode_bitmap < first_block || inode_bitmap > last_block) {
2128 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2129 			       "Inode bitmap for group %u not in group "
2130 			       "(block %llu)!", i, inode_bitmap);
2131 			return 0;
2132 		}
2133 		inode_table = ext4_inode_table(sb, gdp);
2134 		if (inode_table == sb_block) {
2135 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2136 				 "Inode table for group %u overlaps "
2137 				 "superblock", i);
2138 		}
2139 		if (inode_table < first_block ||
2140 		    inode_table + sbi->s_itb_per_group - 1 > last_block) {
2141 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2142 			       "Inode table for group %u not in group "
2143 			       "(block %llu)!", i, inode_table);
2144 			return 0;
2145 		}
2146 		ext4_lock_group(sb, i);
2147 		if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
2148 			ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
2149 				 "Checksum for group %u failed (%u!=%u)",
2150 				 i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
2151 				     gdp)), le16_to_cpu(gdp->bg_checksum));
2152 			if (!(sb->s_flags & MS_RDONLY)) {
2153 				ext4_unlock_group(sb, i);
2154 				return 0;
2155 			}
2156 		}
2157 		ext4_unlock_group(sb, i);
2158 		if (!flexbg_flag)
2159 			first_block += EXT4_BLOCKS_PER_GROUP(sb);
2160 	}
2161 	if (NULL != first_not_zeroed)
2162 		*first_not_zeroed = grp;
2163 	return 1;
2164 }
2165 
2166 /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
2167  * the superblock) which were deleted from all directories, but held open by
2168  * a process at the time of a crash.  We walk the list and try to delete these
2169  * inodes at recovery time (only with a read-write filesystem).
2170  *
2171  * In order to keep the orphan inode chain consistent during traversal (in
2172  * case of crash during recovery), we link each inode into the superblock
2173  * orphan list_head and handle it the same way as an inode deletion during
2174  * normal operation (which journals the operations for us).
2175  *
2176  * We only do an iget() and an iput() on each inode, which is very safe if we
2177  * accidentally point at an in-use or already deleted inode.  The worst that
2178  * can happen in this case is that we get a "bit already cleared" message from
2179  * ext4_free_inode().  The only reason we would point at a wrong inode is if
2180  * e2fsck was run on this filesystem, and it must have already done the orphan
2181  * inode cleanup for us, so we can safely abort without any further action.
2182  */
ext4_orphan_cleanup(struct super_block * sb,struct ext4_super_block * es)2183 static void ext4_orphan_cleanup(struct super_block *sb,
2184 				struct ext4_super_block *es)
2185 {
2186 	unsigned int s_flags = sb->s_flags;
2187 	int nr_orphans = 0, nr_truncates = 0;
2188 #ifdef CONFIG_QUOTA
2189 	int i;
2190 #endif
2191 	if (!es->s_last_orphan) {
2192 		jbd_debug(4, "no orphan inodes to clean up\n");
2193 		return;
2194 	}
2195 
2196 	if (bdev_read_only(sb->s_bdev)) {
2197 		ext4_msg(sb, KERN_ERR, "write access "
2198 			"unavailable, skipping orphan cleanup");
2199 		return;
2200 	}
2201 
2202 	/* Check if feature set would not allow a r/w mount */
2203 	if (!ext4_feature_set_ok(sb, 0)) {
2204 		ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
2205 			 "unknown ROCOMPAT features");
2206 		return;
2207 	}
2208 
2209 	if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2210 		/* don't clear list on RO mount w/ errors */
2211 		if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
2212 			ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
2213 				  "clearing orphan list.\n");
2214 			es->s_last_orphan = 0;
2215 		}
2216 		jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2217 		return;
2218 	}
2219 
2220 	if (s_flags & MS_RDONLY) {
2221 		ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
2222 		sb->s_flags &= ~MS_RDONLY;
2223 	}
2224 #ifdef CONFIG_QUOTA
2225 	/* Needed for iput() to work correctly and not trash data */
2226 	sb->s_flags |= MS_ACTIVE;
2227 	/* Turn on journaled quotas so that they are updated correctly */
2228 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2229 		if (EXT4_SB(sb)->s_qf_names[i]) {
2230 			int ret = ext4_quota_on_mount(sb, i);
2231 			if (ret < 0)
2232 				ext4_msg(sb, KERN_ERR,
2233 					"Cannot turn on journaled "
2234 					"quota: error %d", ret);
2235 		}
2236 	}
2237 #endif
2238 
2239 	while (es->s_last_orphan) {
2240 		struct inode *inode;
2241 
2242 		/*
2243 		 * We may have encountered an error during cleanup; if
2244 		 * so, skip the rest.
2245 		 */
2246 		if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
2247 			jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
2248 			es->s_last_orphan = 0;
2249 			break;
2250 		}
2251 
2252 		inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
2253 		if (IS_ERR(inode)) {
2254 			es->s_last_orphan = 0;
2255 			break;
2256 		}
2257 
2258 		list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
2259 		dquot_initialize(inode);
2260 		if (inode->i_nlink) {
2261 			if (test_opt(sb, DEBUG))
2262 				ext4_msg(sb, KERN_DEBUG,
2263 					"%s: truncating inode %lu to %lld bytes",
2264 					__func__, inode->i_ino, inode->i_size);
2265 			jbd_debug(2, "truncating inode %lu to %lld bytes\n",
2266 				  inode->i_ino, inode->i_size);
2267 			mutex_lock(&inode->i_mutex);
2268 			truncate_inode_pages(inode->i_mapping, inode->i_size);
2269 			ext4_truncate(inode);
2270 			mutex_unlock(&inode->i_mutex);
2271 			nr_truncates++;
2272 		} else {
2273 			if (test_opt(sb, DEBUG))
2274 				ext4_msg(sb, KERN_DEBUG,
2275 					"%s: deleting unreferenced inode %lu",
2276 					__func__, inode->i_ino);
2277 			jbd_debug(2, "deleting unreferenced inode %lu\n",
2278 				  inode->i_ino);
2279 			nr_orphans++;
2280 		}
2281 		iput(inode);  /* The delete magic happens here! */
2282 	}
2283 
2284 #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
2285 
2286 	if (nr_orphans)
2287 		ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
2288 		       PLURAL(nr_orphans));
2289 	if (nr_truncates)
2290 		ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
2291 		       PLURAL(nr_truncates));
2292 #ifdef CONFIG_QUOTA
2293 	/* Turn off journaled quotas if they were enabled for orphan cleanup */
2294 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
2295 		if (EXT4_SB(sb)->s_qf_names[i] && sb_dqopt(sb)->files[i])
2296 			dquot_quota_off(sb, i);
2297 	}
2298 #endif
2299 	sb->s_flags = s_flags; /* Restore MS_RDONLY status */
2300 }
2301 
2302 /*
2303  * Maximal extent format file size.
2304  * Resulting logical blkno at s_maxbytes must fit in our on-disk
2305  * extent format containers, within a sector_t, and within i_blocks
2306  * in the vfs.  ext4 inode has 48 bits of i_block in fsblock units,
2307  * so that won't be a limiting factor.
2308  *
2309  * However there is other limiting factor. We do store extents in the form
2310  * of starting block and length, hence the resulting length of the extent
2311  * covering maximum file size must fit into on-disk format containers as
2312  * well. Given that length is always by 1 unit bigger than max unit (because
2313  * we count 0 as well) we have to lower the s_maxbytes by one fs block.
2314  *
2315  * Note, this does *not* consider any metadata overhead for vfs i_blocks.
2316  */
ext4_max_size(int blkbits,int has_huge_files)2317 static loff_t ext4_max_size(int blkbits, int has_huge_files)
2318 {
2319 	loff_t res;
2320 	loff_t upper_limit = MAX_LFS_FILESIZE;
2321 
2322 	/* small i_blocks in vfs inode? */
2323 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2324 		/*
2325 		 * CONFIG_LBDAF is not enabled implies the inode
2326 		 * i_block represent total blocks in 512 bytes
2327 		 * 32 == size of vfs inode i_blocks * 8
2328 		 */
2329 		upper_limit = (1LL << 32) - 1;
2330 
2331 		/* total blocks in file system block size */
2332 		upper_limit >>= (blkbits - 9);
2333 		upper_limit <<= blkbits;
2334 	}
2335 
2336 	/*
2337 	 * 32-bit extent-start container, ee_block. We lower the maxbytes
2338 	 * by one fs block, so ee_len can cover the extent of maximum file
2339 	 * size
2340 	 */
2341 	res = (1LL << 32) - 1;
2342 	res <<= blkbits;
2343 
2344 	/* Sanity check against vm- & vfs- imposed limits */
2345 	if (res > upper_limit)
2346 		res = upper_limit;
2347 
2348 	return res;
2349 }
2350 
2351 /*
2352  * Maximal bitmap file size.  There is a direct, and {,double-,triple-}indirect
2353  * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
2354  * We need to be 1 filesystem block less than the 2^48 sector limit.
2355  */
ext4_max_bitmap_size(int bits,int has_huge_files)2356 static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
2357 {
2358 	loff_t res = EXT4_NDIR_BLOCKS;
2359 	int meta_blocks;
2360 	loff_t upper_limit;
2361 	/* This is calculated to be the largest file size for a dense, block
2362 	 * mapped file such that the file's total number of 512-byte sectors,
2363 	 * including data and all indirect blocks, does not exceed (2^48 - 1).
2364 	 *
2365 	 * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
2366 	 * number of 512-byte sectors of the file.
2367 	 */
2368 
2369 	if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
2370 		/*
2371 		 * !has_huge_files or CONFIG_LBDAF not enabled implies that
2372 		 * the inode i_block field represents total file blocks in
2373 		 * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
2374 		 */
2375 		upper_limit = (1LL << 32) - 1;
2376 
2377 		/* total blocks in file system block size */
2378 		upper_limit >>= (bits - 9);
2379 
2380 	} else {
2381 		/*
2382 		 * We use 48 bit ext4_inode i_blocks
2383 		 * With EXT4_HUGE_FILE_FL set the i_blocks
2384 		 * represent total number of blocks in
2385 		 * file system block size
2386 		 */
2387 		upper_limit = (1LL << 48) - 1;
2388 
2389 	}
2390 
2391 	/* indirect blocks */
2392 	meta_blocks = 1;
2393 	/* double indirect blocks */
2394 	meta_blocks += 1 + (1LL << (bits-2));
2395 	/* tripple indirect blocks */
2396 	meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
2397 
2398 	upper_limit -= meta_blocks;
2399 	upper_limit <<= bits;
2400 
2401 	res += 1LL << (bits-2);
2402 	res += 1LL << (2*(bits-2));
2403 	res += 1LL << (3*(bits-2));
2404 	res <<= bits;
2405 	if (res > upper_limit)
2406 		res = upper_limit;
2407 
2408 	if (res > MAX_LFS_FILESIZE)
2409 		res = MAX_LFS_FILESIZE;
2410 
2411 	return res;
2412 }
2413 
descriptor_loc(struct super_block * sb,ext4_fsblk_t logical_sb_block,int nr)2414 static ext4_fsblk_t descriptor_loc(struct super_block *sb,
2415 				   ext4_fsblk_t logical_sb_block, int nr)
2416 {
2417 	struct ext4_sb_info *sbi = EXT4_SB(sb);
2418 	ext4_group_t bg, first_meta_bg;
2419 	int has_super = 0;
2420 
2421 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
2422 
2423 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
2424 	    nr < first_meta_bg)
2425 		return logical_sb_block + nr + 1;
2426 	bg = sbi->s_desc_per_block * nr;
2427 	if (ext4_bg_has_super(sb, bg))
2428 		has_super = 1;
2429 
2430 	/*
2431 	 * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
2432 	 * block 2, not 1.  If s_first_data_block == 0 (bigalloc is enabled
2433 	 * on modern mke2fs or blksize > 1k on older mke2fs) then we must
2434 	 * compensate.
2435 	 */
2436 	if (sb->s_blocksize == 1024 && nr == 0 &&
2437 	    le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
2438 		has_super++;
2439 
2440 	return (has_super + ext4_group_first_block_no(sb, bg));
2441 }
2442 
2443 /**
2444  * ext4_get_stripe_size: Get the stripe size.
2445  * @sbi: In memory super block info
2446  *
2447  * If we have specified it via mount option, then
2448  * use the mount option value. If the value specified at mount time is
2449  * greater than the blocks per group use the super block value.
2450  * If the super block value is greater than blocks per group return 0.
2451  * Allocator needs it be less than blocks per group.
2452  *
2453  */
ext4_get_stripe_size(struct ext4_sb_info * sbi)2454 static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
2455 {
2456 	unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
2457 	unsigned long stripe_width =
2458 			le32_to_cpu(sbi->s_es->s_raid_stripe_width);
2459 	int ret;
2460 
2461 	if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
2462 		ret = sbi->s_stripe;
2463 	else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
2464 		ret = stripe_width;
2465 	else if (stride && stride <= sbi->s_blocks_per_group)
2466 		ret = stride;
2467 	else
2468 		ret = 0;
2469 
2470 	/*
2471 	 * If the stripe width is 1, this makes no sense and
2472 	 * we set it to 0 to turn off stripe handling code.
2473 	 */
2474 	if (ret <= 1)
2475 		ret = 0;
2476 
2477 	return ret;
2478 }
2479 
2480 /* sysfs supprt */
2481 
2482 struct ext4_attr {
2483 	struct attribute attr;
2484 	ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
2485 	ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
2486 			 const char *, size_t);
2487 	union {
2488 		int offset;
2489 		int deprecated_val;
2490 	} u;
2491 };
2492 
parse_strtoull(const char * buf,unsigned long long max,unsigned long long * value)2493 static int parse_strtoull(const char *buf,
2494 		unsigned long long max, unsigned long long *value)
2495 {
2496 	int ret;
2497 
2498 	ret = kstrtoull(skip_spaces(buf), 0, value);
2499 	if (!ret && *value > max)
2500 		ret = -EINVAL;
2501 	return ret;
2502 }
2503 
delayed_allocation_blocks_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2504 static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
2505 					      struct ext4_sb_info *sbi,
2506 					      char *buf)
2507 {
2508 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2509 		(s64) EXT4_C2B(sbi,
2510 			percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
2511 }
2512 
session_write_kbytes_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2513 static ssize_t session_write_kbytes_show(struct ext4_attr *a,
2514 					 struct ext4_sb_info *sbi, char *buf)
2515 {
2516 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
2517 
2518 	if (!sb->s_bdev->bd_part)
2519 		return snprintf(buf, PAGE_SIZE, "0\n");
2520 	return snprintf(buf, PAGE_SIZE, "%lu\n",
2521 			(part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2522 			 sbi->s_sectors_written_start) >> 1);
2523 }
2524 
lifetime_write_kbytes_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2525 static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
2526 					  struct ext4_sb_info *sbi, char *buf)
2527 {
2528 	struct super_block *sb = sbi->s_buddy_cache->i_sb;
2529 
2530 	if (!sb->s_bdev->bd_part)
2531 		return snprintf(buf, PAGE_SIZE, "0\n");
2532 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2533 			(unsigned long long)(sbi->s_kbytes_written +
2534 			((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
2535 			  EXT4_SB(sb)->s_sectors_written_start) >> 1)));
2536 }
2537 
inode_readahead_blks_store(struct ext4_attr * a,struct ext4_sb_info * sbi,const char * buf,size_t count)2538 static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
2539 					  struct ext4_sb_info *sbi,
2540 					  const char *buf, size_t count)
2541 {
2542 	unsigned long t;
2543 	int ret;
2544 
2545 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2546 	if (ret)
2547 		return ret;
2548 
2549 	if (t && (!is_power_of_2(t) || t > 0x40000000))
2550 		return -EINVAL;
2551 
2552 	sbi->s_inode_readahead_blks = t;
2553 	return count;
2554 }
2555 
sbi_ui_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2556 static ssize_t sbi_ui_show(struct ext4_attr *a,
2557 			   struct ext4_sb_info *sbi, char *buf)
2558 {
2559 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
2560 
2561 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2562 }
2563 
sbi_ui_store(struct ext4_attr * a,struct ext4_sb_info * sbi,const char * buf,size_t count)2564 static ssize_t sbi_ui_store(struct ext4_attr *a,
2565 			    struct ext4_sb_info *sbi,
2566 			    const char *buf, size_t count)
2567 {
2568 	unsigned int *ui = (unsigned int *) (((char *) sbi) + a->u.offset);
2569 	unsigned long t;
2570 	int ret;
2571 
2572 	ret = kstrtoul(skip_spaces(buf), 0, &t);
2573 	if (ret)
2574 		return ret;
2575 	*ui = t;
2576 	return count;
2577 }
2578 
es_ui_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2579 static ssize_t es_ui_show(struct ext4_attr *a,
2580 			   struct ext4_sb_info *sbi, char *buf)
2581 {
2582 
2583 	unsigned int *ui = (unsigned int *) (((char *) sbi->s_es) +
2584 			   a->u.offset);
2585 
2586 	return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
2587 }
2588 
reserved_clusters_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2589 static ssize_t reserved_clusters_show(struct ext4_attr *a,
2590 				  struct ext4_sb_info *sbi, char *buf)
2591 {
2592 	return snprintf(buf, PAGE_SIZE, "%llu\n",
2593 		(unsigned long long) atomic64_read(&sbi->s_resv_clusters));
2594 }
2595 
reserved_clusters_store(struct ext4_attr * a,struct ext4_sb_info * sbi,const char * buf,size_t count)2596 static ssize_t reserved_clusters_store(struct ext4_attr *a,
2597 				   struct ext4_sb_info *sbi,
2598 				   const char *buf, size_t count)
2599 {
2600 	unsigned long long val;
2601 	int ret;
2602 
2603 	if (parse_strtoull(buf, -1ULL, &val))
2604 		return -EINVAL;
2605 	ret = ext4_reserve_clusters(sbi, val);
2606 
2607 	return ret ? ret : count;
2608 }
2609 
trigger_test_error(struct ext4_attr * a,struct ext4_sb_info * sbi,const char * buf,size_t count)2610 static ssize_t trigger_test_error(struct ext4_attr *a,
2611 				  struct ext4_sb_info *sbi,
2612 				  const char *buf, size_t count)
2613 {
2614 	int len = count;
2615 
2616 	if (!capable(CAP_SYS_ADMIN))
2617 		return -EPERM;
2618 
2619 	if (len && buf[len-1] == '\n')
2620 		len--;
2621 
2622 	if (len)
2623 		ext4_error(sbi->s_sb, "%.*s", len, buf);
2624 	return count;
2625 }
2626 
sbi_deprecated_show(struct ext4_attr * a,struct ext4_sb_info * sbi,char * buf)2627 static ssize_t sbi_deprecated_show(struct ext4_attr *a,
2628 				   struct ext4_sb_info *sbi, char *buf)
2629 {
2630 	return snprintf(buf, PAGE_SIZE, "%d\n", a->u.deprecated_val);
2631 }
2632 
2633 #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
2634 static struct ext4_attr ext4_attr_##_name = {			\
2635 	.attr = {.name = __stringify(_name), .mode = _mode },	\
2636 	.show	= _show,					\
2637 	.store	= _store,					\
2638 	.u = {							\
2639 		.offset = offsetof(struct ext4_sb_info, _elname),\
2640 	},							\
2641 }
2642 
2643 #define EXT4_ATTR_OFFSET_ES(_name,_mode,_show,_store,_elname)		\
2644 static struct ext4_attr ext4_attr_##_name = {				\
2645 	.attr = {.name = __stringify(_name), .mode = _mode },		\
2646 	.show	= _show,						\
2647 	.store	= _store,						\
2648 	.u = {								\
2649 		.offset = offsetof(struct ext4_super_block, _elname),	\
2650 	},								\
2651 }
2652 
2653 #define EXT4_ATTR(name, mode, show, store) \
2654 static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
2655 
2656 #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
2657 #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
2658 #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
2659 
2660 #define EXT4_RO_ATTR_ES_UI(name, elname)	\
2661 	EXT4_ATTR_OFFSET_ES(name, 0444, es_ui_show, NULL, elname)
2662 #define EXT4_RW_ATTR_SBI_UI(name, elname)	\
2663 	EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
2664 
2665 #define ATTR_LIST(name) &ext4_attr_##name.attr
2666 #define EXT4_DEPRECATED_ATTR(_name, _val)	\
2667 static struct ext4_attr ext4_attr_##_name = {			\
2668 	.attr = {.name = __stringify(_name), .mode = 0444 },	\
2669 	.show	= sbi_deprecated_show,				\
2670 	.u = {							\
2671 		.deprecated_val = _val,				\
2672 	},							\
2673 }
2674 
2675 EXT4_RO_ATTR(delayed_allocation_blocks);
2676 EXT4_RO_ATTR(session_write_kbytes);
2677 EXT4_RO_ATTR(lifetime_write_kbytes);
2678 EXT4_RW_ATTR(reserved_clusters);
2679 EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
2680 		 inode_readahead_blks_store, s_inode_readahead_blks);
2681 EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
2682 EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
2683 EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
2684 EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
2685 EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
2686 EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
2687 EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
2688 EXT4_DEPRECATED_ATTR(max_writeback_mb_bump, 128);
2689 EXT4_RW_ATTR_SBI_UI(extent_max_zeroout_kb, s_extent_max_zeroout_kb);
2690 EXT4_ATTR(trigger_fs_error, 0200, NULL, trigger_test_error);
2691 EXT4_RW_ATTR_SBI_UI(err_ratelimit_interval_ms, s_err_ratelimit_state.interval);
2692 EXT4_RW_ATTR_SBI_UI(err_ratelimit_burst, s_err_ratelimit_state.burst);
2693 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_interval_ms, s_warning_ratelimit_state.interval);
2694 EXT4_RW_ATTR_SBI_UI(warning_ratelimit_burst, s_warning_ratelimit_state.burst);
2695 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_interval_ms, s_msg_ratelimit_state.interval);
2696 EXT4_RW_ATTR_SBI_UI(msg_ratelimit_burst, s_msg_ratelimit_state.burst);
2697 EXT4_RO_ATTR_ES_UI(errors_count, s_error_count);
2698 EXT4_RO_ATTR_ES_UI(first_error_time, s_first_error_time);
2699 EXT4_RO_ATTR_ES_UI(last_error_time, s_last_error_time);
2700 
2701 static struct attribute *ext4_attrs[] = {
2702 	ATTR_LIST(delayed_allocation_blocks),
2703 	ATTR_LIST(session_write_kbytes),
2704 	ATTR_LIST(lifetime_write_kbytes),
2705 	ATTR_LIST(reserved_clusters),
2706 	ATTR_LIST(inode_readahead_blks),
2707 	ATTR_LIST(inode_goal),
2708 	ATTR_LIST(mb_stats),
2709 	ATTR_LIST(mb_max_to_scan),
2710 	ATTR_LIST(mb_min_to_scan),
2711 	ATTR_LIST(mb_order2_req),
2712 	ATTR_LIST(mb_stream_req),
2713 	ATTR_LIST(mb_group_prealloc),
2714 	ATTR_LIST(max_writeback_mb_bump),
2715 	ATTR_LIST(extent_max_zeroout_kb),
2716 	ATTR_LIST(trigger_fs_error),
2717 	ATTR_LIST(err_ratelimit_interval_ms),
2718 	ATTR_LIST(err_ratelimit_burst),
2719 	ATTR_LIST(warning_ratelimit_interval_ms),
2720 	ATTR_LIST(warning_ratelimit_burst),
2721 	ATTR_LIST(msg_ratelimit_interval_ms),
2722 	ATTR_LIST(msg_ratelimit_burst),
2723 	ATTR_LIST(errors_count),
2724 	ATTR_LIST(first_error_time),
2725 	ATTR_LIST(last_error_time),
2726 	NULL,
2727 };
2728 
2729 /* Features this copy of ext4 supports */
2730 EXT4_INFO_ATTR(lazy_itable_init);
2731 EXT4_INFO_ATTR(batched_discard);
2732 EXT4_INFO_ATTR(meta_bg_resize);
2733 EXT4_INFO_ATTR(encryption);
2734 
2735 static struct attribute *ext4_feat_attrs[] = {
2736 	ATTR_LIST(lazy_itable_init),
2737 	ATTR_LIST(batched_discard),
2738 	ATTR_LIST(meta_bg_resize),
2739 	ATTR_LIST(encryption),
2740 	NULL,
2741 };
2742 
ext4_attr_show(struct kobject * kobj,struct attribute * attr,char * buf)2743 static ssize_t ext4_attr_show(struct kobject *kobj,
2744 			      struct attribute *attr, char *buf)
2745 {
2746 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2747 						s_kobj);
2748 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2749 
2750 	return a->show ? a->show(a, sbi, buf) : 0;
2751 }
2752 
ext4_attr_store(struct kobject * kobj,struct attribute * attr,const char * buf,size_t len)2753 static ssize_t ext4_attr_store(struct kobject *kobj,
2754 			       struct attribute *attr,
2755 			       const char *buf, size_t len)
2756 {
2757 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2758 						s_kobj);
2759 	struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
2760 
2761 	return a->store ? a->store(a, sbi, buf, len) : 0;
2762 }
2763 
ext4_sb_release(struct kobject * kobj)2764 static void ext4_sb_release(struct kobject *kobj)
2765 {
2766 	struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
2767 						s_kobj);
2768 	complete(&sbi->s_kobj_unregister);
2769 }
2770 
2771 static const struct sysfs_ops ext4_attr_ops = {
2772 	.show	= ext4_attr_show,
2773 	.store	= ext4_attr_store,
2774 };
2775 
2776 static struct kobj_type ext4_ktype = {
2777 	.default_attrs	= ext4_attrs,
2778 	.sysfs_ops	= &ext4_attr_ops,
2779 	.release	= ext4_sb_release,
2780 };
2781 
ext4_feat_release(struct kobject * kobj)2782 static void ext4_feat_release(struct kobject *kobj)
2783 {
2784 	complete(&ext4_feat->f_kobj_unregister);
2785 }
2786 
ext4_feat_show(struct kobject * kobj,struct attribute * attr,char * buf)2787 static ssize_t ext4_feat_show(struct kobject *kobj,
2788 			      struct attribute *attr, char *buf)
2789 {
2790 	return snprintf(buf, PAGE_SIZE, "supported\n");
2791 }
2792 
2793 /*
2794  * We can not use ext4_attr_show/store because it relies on the kobject
2795  * being embedded in the ext4_sb_info structure which is definitely not
2796  * true in this case.
2797  */
2798 static const struct sysfs_ops ext4_feat_ops = {
2799 	.show	= ext4_feat_show,
2800 	.store	= NULL,
2801 };
2802 
2803 static struct kobj_type ext4_feat_ktype = {
2804 	.default_attrs	= ext4_feat_attrs,
2805 	.sysfs_ops	= &ext4_feat_ops,
2806 	.release	= ext4_feat_release,
2807 };
2808 
2809 /*
2810  * Check whether this filesystem can be mounted based on
2811  * the features present and the RDONLY/RDWR mount requested.
2812  * Returns 1 if this filesystem can be mounted as requested,
2813  * 0 if it cannot be.
2814  */
ext4_feature_set_ok(struct super_block * sb,int readonly)2815 static int ext4_feature_set_ok(struct super_block *sb, int readonly)
2816 {
2817 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
2818 		ext4_msg(sb, KERN_ERR,
2819 			"Couldn't mount because of "
2820 			"unsupported optional features (%x)",
2821 			(le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
2822 			~EXT4_FEATURE_INCOMPAT_SUPP));
2823 		return 0;
2824 	}
2825 
2826 	if (readonly)
2827 		return 1;
2828 
2829 	/* Check that feature set is OK for a read-write mount */
2830 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
2831 		ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
2832 			 "unsupported optional features (%x)",
2833 			 (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
2834 				~EXT4_FEATURE_RO_COMPAT_SUPP));
2835 		return 0;
2836 	}
2837 	/*
2838 	 * Large file size enabled file system can only be mounted
2839 	 * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
2840 	 */
2841 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
2842 		if (sizeof(blkcnt_t) < sizeof(u64)) {
2843 			ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
2844 				 "cannot be mounted RDWR without "
2845 				 "CONFIG_LBDAF");
2846 			return 0;
2847 		}
2848 	}
2849 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
2850 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
2851 		ext4_msg(sb, KERN_ERR,
2852 			 "Can't support bigalloc feature without "
2853 			 "extents feature\n");
2854 		return 0;
2855 	}
2856 
2857 #ifndef CONFIG_QUOTA
2858 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
2859 	    !readonly) {
2860 		ext4_msg(sb, KERN_ERR,
2861 			 "Filesystem with quota feature cannot be mounted RDWR "
2862 			 "without CONFIG_QUOTA");
2863 		return 0;
2864 	}
2865 #endif  /* CONFIG_QUOTA */
2866 	return 1;
2867 }
2868 
2869 /*
2870  * This function is called once a day if we have errors logged
2871  * on the file system
2872  */
print_daily_error_info(unsigned long arg)2873 static void print_daily_error_info(unsigned long arg)
2874 {
2875 	struct super_block *sb = (struct super_block *) arg;
2876 	struct ext4_sb_info *sbi;
2877 	struct ext4_super_block *es;
2878 
2879 	sbi = EXT4_SB(sb);
2880 	es = sbi->s_es;
2881 
2882 	if (es->s_error_count)
2883 		/* fsck newer than v1.41.13 is needed to clean this condition. */
2884 		ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
2885 			 le32_to_cpu(es->s_error_count));
2886 	if (es->s_first_error_time) {
2887 		printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
2888 		       sb->s_id, le32_to_cpu(es->s_first_error_time),
2889 		       (int) sizeof(es->s_first_error_func),
2890 		       es->s_first_error_func,
2891 		       le32_to_cpu(es->s_first_error_line));
2892 		if (es->s_first_error_ino)
2893 			printk(": inode %u",
2894 			       le32_to_cpu(es->s_first_error_ino));
2895 		if (es->s_first_error_block)
2896 			printk(": block %llu", (unsigned long long)
2897 			       le64_to_cpu(es->s_first_error_block));
2898 		printk("\n");
2899 	}
2900 	if (es->s_last_error_time) {
2901 		printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
2902 		       sb->s_id, le32_to_cpu(es->s_last_error_time),
2903 		       (int) sizeof(es->s_last_error_func),
2904 		       es->s_last_error_func,
2905 		       le32_to_cpu(es->s_last_error_line));
2906 		if (es->s_last_error_ino)
2907 			printk(": inode %u",
2908 			       le32_to_cpu(es->s_last_error_ino));
2909 		if (es->s_last_error_block)
2910 			printk(": block %llu", (unsigned long long)
2911 			       le64_to_cpu(es->s_last_error_block));
2912 		printk("\n");
2913 	}
2914 	mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ);  /* Once a day */
2915 }
2916 
2917 /* Find next suitable group and run ext4_init_inode_table */
ext4_run_li_request(struct ext4_li_request * elr)2918 static int ext4_run_li_request(struct ext4_li_request *elr)
2919 {
2920 	struct ext4_group_desc *gdp = NULL;
2921 	ext4_group_t group, ngroups;
2922 	struct super_block *sb;
2923 	unsigned long timeout = 0;
2924 	int ret = 0;
2925 
2926 	sb = elr->lr_super;
2927 	ngroups = EXT4_SB(sb)->s_groups_count;
2928 
2929 	sb_start_write(sb);
2930 	for (group = elr->lr_next_group; group < ngroups; group++) {
2931 		gdp = ext4_get_group_desc(sb, group, NULL);
2932 		if (!gdp) {
2933 			ret = 1;
2934 			break;
2935 		}
2936 
2937 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
2938 			break;
2939 	}
2940 
2941 	if (group >= ngroups)
2942 		ret = 1;
2943 
2944 	if (!ret) {
2945 		timeout = jiffies;
2946 		ret = ext4_init_inode_table(sb, group,
2947 					    elr->lr_timeout ? 0 : 1);
2948 		if (elr->lr_timeout == 0) {
2949 			timeout = (jiffies - timeout) *
2950 				  elr->lr_sbi->s_li_wait_mult;
2951 			elr->lr_timeout = timeout;
2952 		}
2953 		elr->lr_next_sched = jiffies + elr->lr_timeout;
2954 		elr->lr_next_group = group + 1;
2955 	}
2956 	sb_end_write(sb);
2957 
2958 	return ret;
2959 }
2960 
2961 /*
2962  * Remove lr_request from the list_request and free the
2963  * request structure. Should be called with li_list_mtx held
2964  */
ext4_remove_li_request(struct ext4_li_request * elr)2965 static void ext4_remove_li_request(struct ext4_li_request *elr)
2966 {
2967 	struct ext4_sb_info *sbi;
2968 
2969 	if (!elr)
2970 		return;
2971 
2972 	sbi = elr->lr_sbi;
2973 
2974 	list_del(&elr->lr_request);
2975 	sbi->s_li_request = NULL;
2976 	kfree(elr);
2977 }
2978 
ext4_unregister_li_request(struct super_block * sb)2979 static void ext4_unregister_li_request(struct super_block *sb)
2980 {
2981 	mutex_lock(&ext4_li_mtx);
2982 	if (!ext4_li_info) {
2983 		mutex_unlock(&ext4_li_mtx);
2984 		return;
2985 	}
2986 
2987 	mutex_lock(&ext4_li_info->li_list_mtx);
2988 	ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
2989 	mutex_unlock(&ext4_li_info->li_list_mtx);
2990 	mutex_unlock(&ext4_li_mtx);
2991 }
2992 
2993 static struct task_struct *ext4_lazyinit_task;
2994 
2995 /*
2996  * This is the function where ext4lazyinit thread lives. It walks
2997  * through the request list searching for next scheduled filesystem.
2998  * When such a fs is found, run the lazy initialization request
2999  * (ext4_rn_li_request) and keep track of the time spend in this
3000  * function. Based on that time we compute next schedule time of
3001  * the request. When walking through the list is complete, compute
3002  * next waking time and put itself into sleep.
3003  */
ext4_lazyinit_thread(void * arg)3004 static int ext4_lazyinit_thread(void *arg)
3005 {
3006 	struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
3007 	struct list_head *pos, *n;
3008 	struct ext4_li_request *elr;
3009 	unsigned long next_wakeup, cur;
3010 
3011 	BUG_ON(NULL == eli);
3012 
3013 cont_thread:
3014 	while (true) {
3015 		next_wakeup = MAX_JIFFY_OFFSET;
3016 
3017 		mutex_lock(&eli->li_list_mtx);
3018 		if (list_empty(&eli->li_request_list)) {
3019 			mutex_unlock(&eli->li_list_mtx);
3020 			goto exit_thread;
3021 		}
3022 
3023 		list_for_each_safe(pos, n, &eli->li_request_list) {
3024 			elr = list_entry(pos, struct ext4_li_request,
3025 					 lr_request);
3026 
3027 			if (time_after_eq(jiffies, elr->lr_next_sched)) {
3028 				if (ext4_run_li_request(elr) != 0) {
3029 					/* error, remove the lazy_init job */
3030 					ext4_remove_li_request(elr);
3031 					continue;
3032 				}
3033 			}
3034 
3035 			if (time_before(elr->lr_next_sched, next_wakeup))
3036 				next_wakeup = elr->lr_next_sched;
3037 		}
3038 		mutex_unlock(&eli->li_list_mtx);
3039 
3040 		try_to_freeze();
3041 
3042 		cur = jiffies;
3043 		if ((time_after_eq(cur, next_wakeup)) ||
3044 		    (MAX_JIFFY_OFFSET == next_wakeup)) {
3045 			cond_resched();
3046 			continue;
3047 		}
3048 
3049 		schedule_timeout_interruptible(next_wakeup - cur);
3050 
3051 		if (kthread_should_stop()) {
3052 			ext4_clear_request_list();
3053 			goto exit_thread;
3054 		}
3055 	}
3056 
3057 exit_thread:
3058 	/*
3059 	 * It looks like the request list is empty, but we need
3060 	 * to check it under the li_list_mtx lock, to prevent any
3061 	 * additions into it, and of course we should lock ext4_li_mtx
3062 	 * to atomically free the list and ext4_li_info, because at
3063 	 * this point another ext4 filesystem could be registering
3064 	 * new one.
3065 	 */
3066 	mutex_lock(&ext4_li_mtx);
3067 	mutex_lock(&eli->li_list_mtx);
3068 	if (!list_empty(&eli->li_request_list)) {
3069 		mutex_unlock(&eli->li_list_mtx);
3070 		mutex_unlock(&ext4_li_mtx);
3071 		goto cont_thread;
3072 	}
3073 	mutex_unlock(&eli->li_list_mtx);
3074 	kfree(ext4_li_info);
3075 	ext4_li_info = NULL;
3076 	mutex_unlock(&ext4_li_mtx);
3077 
3078 	return 0;
3079 }
3080 
ext4_clear_request_list(void)3081 static void ext4_clear_request_list(void)
3082 {
3083 	struct list_head *pos, *n;
3084 	struct ext4_li_request *elr;
3085 
3086 	mutex_lock(&ext4_li_info->li_list_mtx);
3087 	list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
3088 		elr = list_entry(pos, struct ext4_li_request,
3089 				 lr_request);
3090 		ext4_remove_li_request(elr);
3091 	}
3092 	mutex_unlock(&ext4_li_info->li_list_mtx);
3093 }
3094 
ext4_run_lazyinit_thread(void)3095 static int ext4_run_lazyinit_thread(void)
3096 {
3097 	ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
3098 					 ext4_li_info, "ext4lazyinit");
3099 	if (IS_ERR(ext4_lazyinit_task)) {
3100 		int err = PTR_ERR(ext4_lazyinit_task);
3101 		ext4_clear_request_list();
3102 		kfree(ext4_li_info);
3103 		ext4_li_info = NULL;
3104 		printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
3105 				 "initialization thread\n",
3106 				 err);
3107 		return err;
3108 	}
3109 	ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
3110 	return 0;
3111 }
3112 
3113 /*
3114  * Check whether it make sense to run itable init. thread or not.
3115  * If there is at least one uninitialized inode table, return
3116  * corresponding group number, else the loop goes through all
3117  * groups and return total number of groups.
3118  */
ext4_has_uninit_itable(struct super_block * sb)3119 static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
3120 {
3121 	ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
3122 	struct ext4_group_desc *gdp = NULL;
3123 
3124 	for (group = 0; group < ngroups; group++) {
3125 		gdp = ext4_get_group_desc(sb, group, NULL);
3126 		if (!gdp)
3127 			continue;
3128 
3129 		if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
3130 			break;
3131 	}
3132 
3133 	return group;
3134 }
3135 
ext4_li_info_new(void)3136 static int ext4_li_info_new(void)
3137 {
3138 	struct ext4_lazy_init *eli = NULL;
3139 
3140 	eli = kzalloc(sizeof(*eli), GFP_KERNEL);
3141 	if (!eli)
3142 		return -ENOMEM;
3143 
3144 	INIT_LIST_HEAD(&eli->li_request_list);
3145 	mutex_init(&eli->li_list_mtx);
3146 
3147 	eli->li_state |= EXT4_LAZYINIT_QUIT;
3148 
3149 	ext4_li_info = eli;
3150 
3151 	return 0;
3152 }
3153 
ext4_li_request_new(struct super_block * sb,ext4_group_t start)3154 static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
3155 					    ext4_group_t start)
3156 {
3157 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3158 	struct ext4_li_request *elr;
3159 
3160 	elr = kzalloc(sizeof(*elr), GFP_KERNEL);
3161 	if (!elr)
3162 		return NULL;
3163 
3164 	elr->lr_super = sb;
3165 	elr->lr_sbi = sbi;
3166 	elr->lr_next_group = start;
3167 
3168 	/*
3169 	 * Randomize first schedule time of the request to
3170 	 * spread the inode table initialization requests
3171 	 * better.
3172 	 */
3173 	elr->lr_next_sched = jiffies + (prandom_u32() %
3174 				(EXT4_DEF_LI_MAX_START_DELAY * HZ));
3175 	return elr;
3176 }
3177 
ext4_register_li_request(struct super_block * sb,ext4_group_t first_not_zeroed)3178 int ext4_register_li_request(struct super_block *sb,
3179 			     ext4_group_t first_not_zeroed)
3180 {
3181 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3182 	struct ext4_li_request *elr = NULL;
3183 	ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
3184 	int ret = 0;
3185 
3186 	mutex_lock(&ext4_li_mtx);
3187 	if (sbi->s_li_request != NULL) {
3188 		/*
3189 		 * Reset timeout so it can be computed again, because
3190 		 * s_li_wait_mult might have changed.
3191 		 */
3192 		sbi->s_li_request->lr_timeout = 0;
3193 		goto out;
3194 	}
3195 
3196 	if (first_not_zeroed == ngroups ||
3197 	    (sb->s_flags & MS_RDONLY) ||
3198 	    !test_opt(sb, INIT_INODE_TABLE))
3199 		goto out;
3200 
3201 	elr = ext4_li_request_new(sb, first_not_zeroed);
3202 	if (!elr) {
3203 		ret = -ENOMEM;
3204 		goto out;
3205 	}
3206 
3207 	if (NULL == ext4_li_info) {
3208 		ret = ext4_li_info_new();
3209 		if (ret)
3210 			goto out;
3211 	}
3212 
3213 	mutex_lock(&ext4_li_info->li_list_mtx);
3214 	list_add(&elr->lr_request, &ext4_li_info->li_request_list);
3215 	mutex_unlock(&ext4_li_info->li_list_mtx);
3216 
3217 	sbi->s_li_request = elr;
3218 	/*
3219 	 * set elr to NULL here since it has been inserted to
3220 	 * the request_list and the removal and free of it is
3221 	 * handled by ext4_clear_request_list from now on.
3222 	 */
3223 	elr = NULL;
3224 
3225 	if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
3226 		ret = ext4_run_lazyinit_thread();
3227 		if (ret)
3228 			goto out;
3229 	}
3230 out:
3231 	mutex_unlock(&ext4_li_mtx);
3232 	if (ret)
3233 		kfree(elr);
3234 	return ret;
3235 }
3236 
3237 /*
3238  * We do not need to lock anything since this is called on
3239  * module unload.
3240  */
ext4_destroy_lazyinit_thread(void)3241 static void ext4_destroy_lazyinit_thread(void)
3242 {
3243 	/*
3244 	 * If thread exited earlier
3245 	 * there's nothing to be done.
3246 	 */
3247 	if (!ext4_li_info || !ext4_lazyinit_task)
3248 		return;
3249 
3250 	kthread_stop(ext4_lazyinit_task);
3251 }
3252 
set_journal_csum_feature_set(struct super_block * sb)3253 static int set_journal_csum_feature_set(struct super_block *sb)
3254 {
3255 	int ret = 1;
3256 	int compat, incompat;
3257 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3258 
3259 	if (ext4_has_metadata_csum(sb)) {
3260 		/* journal checksum v3 */
3261 		compat = 0;
3262 		incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
3263 	} else {
3264 		/* journal checksum v1 */
3265 		compat = JBD2_FEATURE_COMPAT_CHECKSUM;
3266 		incompat = 0;
3267 	}
3268 
3269 	jbd2_journal_clear_features(sbi->s_journal,
3270 			JBD2_FEATURE_COMPAT_CHECKSUM, 0,
3271 			JBD2_FEATURE_INCOMPAT_CSUM_V3 |
3272 			JBD2_FEATURE_INCOMPAT_CSUM_V2);
3273 	if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
3274 		ret = jbd2_journal_set_features(sbi->s_journal,
3275 				compat, 0,
3276 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
3277 				incompat);
3278 	} else if (test_opt(sb, JOURNAL_CHECKSUM)) {
3279 		ret = jbd2_journal_set_features(sbi->s_journal,
3280 				compat, 0,
3281 				incompat);
3282 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3283 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3284 	} else {
3285 		jbd2_journal_clear_features(sbi->s_journal, 0, 0,
3286 				JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
3287 	}
3288 
3289 	return ret;
3290 }
3291 
3292 /*
3293  * Note: calculating the overhead so we can be compatible with
3294  * historical BSD practice is quite difficult in the face of
3295  * clusters/bigalloc.  This is because multiple metadata blocks from
3296  * different block group can end up in the same allocation cluster.
3297  * Calculating the exact overhead in the face of clustered allocation
3298  * requires either O(all block bitmaps) in memory or O(number of block
3299  * groups**2) in time.  We will still calculate the superblock for
3300  * older file systems --- and if we come across with a bigalloc file
3301  * system with zero in s_overhead_clusters the estimate will be close to
3302  * correct especially for very large cluster sizes --- but for newer
3303  * file systems, it's better to calculate this figure once at mkfs
3304  * time, and store it in the superblock.  If the superblock value is
3305  * present (even for non-bigalloc file systems), we will use it.
3306  */
count_overhead(struct super_block * sb,ext4_group_t grp,char * buf)3307 static int count_overhead(struct super_block *sb, ext4_group_t grp,
3308 			  char *buf)
3309 {
3310 	struct ext4_sb_info	*sbi = EXT4_SB(sb);
3311 	struct ext4_group_desc	*gdp;
3312 	ext4_fsblk_t		first_block, last_block, b;
3313 	ext4_group_t		i, ngroups = ext4_get_groups_count(sb);
3314 	int			s, j, count = 0;
3315 
3316 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC))
3317 		return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
3318 			sbi->s_itb_per_group + 2);
3319 
3320 	first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
3321 		(grp * EXT4_BLOCKS_PER_GROUP(sb));
3322 	last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
3323 	for (i = 0; i < ngroups; i++) {
3324 		gdp = ext4_get_group_desc(sb, i, NULL);
3325 		b = ext4_block_bitmap(sb, gdp);
3326 		if (b >= first_block && b <= last_block) {
3327 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3328 			count++;
3329 		}
3330 		b = ext4_inode_bitmap(sb, gdp);
3331 		if (b >= first_block && b <= last_block) {
3332 			ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
3333 			count++;
3334 		}
3335 		b = ext4_inode_table(sb, gdp);
3336 		if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
3337 			for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
3338 				int c = EXT4_B2C(sbi, b - first_block);
3339 				ext4_set_bit(c, buf);
3340 				count++;
3341 			}
3342 		if (i != grp)
3343 			continue;
3344 		s = 0;
3345 		if (ext4_bg_has_super(sb, grp)) {
3346 			ext4_set_bit(s++, buf);
3347 			count++;
3348 		}
3349 		j = ext4_bg_num_gdb(sb, grp);
3350 		if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
3351 			ext4_error(sb, "Invalid number of block group "
3352 				   "descriptor blocks: %d", j);
3353 			j = EXT4_BLOCKS_PER_GROUP(sb) - s;
3354 		}
3355 		count += j;
3356 		for (; j > 0; j--)
3357 			ext4_set_bit(EXT4_B2C(sbi, s++), buf);
3358 	}
3359 	if (!count)
3360 		return 0;
3361 	return EXT4_CLUSTERS_PER_GROUP(sb) -
3362 		ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
3363 }
3364 
3365 /*
3366  * Compute the overhead and stash it in sbi->s_overhead
3367  */
ext4_calculate_overhead(struct super_block * sb)3368 int ext4_calculate_overhead(struct super_block *sb)
3369 {
3370 	struct ext4_sb_info *sbi = EXT4_SB(sb);
3371 	struct ext4_super_block *es = sbi->s_es;
3372 	ext4_group_t i, ngroups = ext4_get_groups_count(sb);
3373 	ext4_fsblk_t overhead = 0;
3374 	char *buf = (char *) get_zeroed_page(GFP_KERNEL);
3375 
3376 	if (!buf)
3377 		return -ENOMEM;
3378 
3379 	/*
3380 	 * Compute the overhead (FS structures).  This is constant
3381 	 * for a given filesystem unless the number of block groups
3382 	 * changes so we cache the previous value until it does.
3383 	 */
3384 
3385 	/*
3386 	 * All of the blocks before first_data_block are overhead
3387 	 */
3388 	overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
3389 
3390 	/*
3391 	 * Add the overhead found in each block group
3392 	 */
3393 	for (i = 0; i < ngroups; i++) {
3394 		int blks;
3395 
3396 		blks = count_overhead(sb, i, buf);
3397 		overhead += blks;
3398 		if (blks)
3399 			memset(buf, 0, PAGE_SIZE);
3400 		cond_resched();
3401 	}
3402 	/* Add the journal blocks as well */
3403 	if (sbi->s_journal)
3404 		overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
3405 
3406 	sbi->s_overhead = overhead;
3407 	smp_wmb();
3408 	free_page((unsigned long) buf);
3409 	return 0;
3410 }
3411 
3412 
ext4_calculate_resv_clusters(struct super_block * sb)3413 static ext4_fsblk_t ext4_calculate_resv_clusters(struct super_block *sb)
3414 {
3415 	ext4_fsblk_t resv_clusters;
3416 
3417 	/*
3418 	 * There's no need to reserve anything when we aren't using extents.
3419 	 * The space estimates are exact, there are no unwritten extents,
3420 	 * hole punching doesn't need new metadata... This is needed especially
3421 	 * to keep ext2/3 backward compatibility.
3422 	 */
3423 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
3424 		return 0;
3425 	/*
3426 	 * By default we reserve 2% or 4096 clusters, whichever is smaller.
3427 	 * This should cover the situations where we can not afford to run
3428 	 * out of space like for example punch hole, or converting
3429 	 * unwritten extents in delalloc path. In most cases such
3430 	 * allocation would require 1, or 2 blocks, higher numbers are
3431 	 * very rare.
3432 	 */
3433 	resv_clusters = ext4_blocks_count(EXT4_SB(sb)->s_es) >>
3434 			EXT4_SB(sb)->s_cluster_bits;
3435 
3436 	do_div(resv_clusters, 50);
3437 	resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
3438 
3439 	return resv_clusters;
3440 }
3441 
3442 
ext4_reserve_clusters(struct ext4_sb_info * sbi,ext4_fsblk_t count)3443 static int ext4_reserve_clusters(struct ext4_sb_info *sbi, ext4_fsblk_t count)
3444 {
3445 	ext4_fsblk_t clusters = ext4_blocks_count(sbi->s_es) >>
3446 				sbi->s_cluster_bits;
3447 
3448 	if (count >= clusters)
3449 		return -EINVAL;
3450 
3451 	atomic64_set(&sbi->s_resv_clusters, count);
3452 	return 0;
3453 }
3454 
ext4_fill_super(struct super_block * sb,void * data,int silent)3455 static int ext4_fill_super(struct super_block *sb, void *data, int silent)
3456 {
3457 	char *orig_data = kstrdup(data, GFP_KERNEL);
3458 	struct buffer_head *bh;
3459 	struct ext4_super_block *es = NULL;
3460 	struct ext4_sb_info *sbi;
3461 	ext4_fsblk_t block;
3462 	ext4_fsblk_t sb_block = get_sb_block(&data);
3463 	ext4_fsblk_t logical_sb_block;
3464 	unsigned long offset = 0;
3465 	unsigned long journal_devnum = 0;
3466 	unsigned long def_mount_opts;
3467 	struct inode *root;
3468 	char *cp;
3469 	const char *descr;
3470 	int ret = -ENOMEM;
3471 	int blocksize, clustersize;
3472 	unsigned int db_count;
3473 	unsigned int i;
3474 	int needs_recovery, has_huge_files, has_bigalloc;
3475 	__u64 blocks_count;
3476 	int err = 0;
3477 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
3478 	ext4_group_t first_not_zeroed;
3479 
3480 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
3481 	if (!sbi)
3482 		goto out_free_orig;
3483 
3484 	sbi->s_blockgroup_lock =
3485 		kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
3486 	if (!sbi->s_blockgroup_lock) {
3487 		kfree(sbi);
3488 		goto out_free_orig;
3489 	}
3490 	sb->s_fs_info = sbi;
3491 	sbi->s_sb = sb;
3492 	sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
3493 	sbi->s_sb_block = sb_block;
3494 	if (sb->s_bdev->bd_part)
3495 		sbi->s_sectors_written_start =
3496 			part_stat_read(sb->s_bdev->bd_part, sectors[1]);
3497 
3498 	/* Cleanup superblock name */
3499 	for (cp = sb->s_id; (cp = strchr(cp, '/'));)
3500 		*cp = '!';
3501 
3502 	/* -EINVAL is default */
3503 	ret = -EINVAL;
3504 	blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
3505 	if (!blocksize) {
3506 		ext4_msg(sb, KERN_ERR, "unable to set blocksize");
3507 		goto out_fail;
3508 	}
3509 
3510 	/*
3511 	 * The ext4 superblock will not be buffer aligned for other than 1kB
3512 	 * block sizes.  We need to calculate the offset from buffer start.
3513 	 */
3514 	if (blocksize != EXT4_MIN_BLOCK_SIZE) {
3515 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3516 		offset = do_div(logical_sb_block, blocksize);
3517 	} else {
3518 		logical_sb_block = sb_block;
3519 	}
3520 
3521 	if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
3522 		ext4_msg(sb, KERN_ERR, "unable to read superblock");
3523 		goto out_fail;
3524 	}
3525 	/*
3526 	 * Note: s_es must be initialized as soon as possible because
3527 	 *       some ext4 macro-instructions depend on its value
3528 	 */
3529 	es = (struct ext4_super_block *) (bh->b_data + offset);
3530 	sbi->s_es = es;
3531 	sb->s_magic = le16_to_cpu(es->s_magic);
3532 	if (sb->s_magic != EXT4_SUPER_MAGIC)
3533 		goto cantfind_ext4;
3534 	sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
3535 
3536 	/* Warn if metadata_csum and gdt_csum are both set. */
3537 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3538 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
3539 	    EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
3540 		ext4_warning(sb, KERN_INFO "metadata_csum and uninit_bg are "
3541 			     "redundant flags; please run fsck.");
3542 
3543 	/* Check for a known checksum algorithm */
3544 	if (!ext4_verify_csum_type(sb, es)) {
3545 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3546 			 "unknown checksum algorithm.");
3547 		silent = 1;
3548 		goto cantfind_ext4;
3549 	}
3550 
3551 	/* Load the checksum driver */
3552 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
3553 				       EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) {
3554 		sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
3555 		if (IS_ERR(sbi->s_chksum_driver)) {
3556 			ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
3557 			ret = PTR_ERR(sbi->s_chksum_driver);
3558 			sbi->s_chksum_driver = NULL;
3559 			goto failed_mount;
3560 		}
3561 	}
3562 
3563 	/* Check superblock checksum */
3564 	if (!ext4_superblock_csum_verify(sb, es)) {
3565 		ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
3566 			 "invalid superblock checksum.  Run e2fsck?");
3567 		silent = 1;
3568 		goto cantfind_ext4;
3569 	}
3570 
3571 	/* Precompute checksum seed for all metadata */
3572 	if (ext4_has_metadata_csum(sb))
3573 		sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
3574 					       sizeof(es->s_uuid));
3575 
3576 	/* Set defaults before we parse the mount options */
3577 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
3578 	set_opt(sb, INIT_INODE_TABLE);
3579 	if (def_mount_opts & EXT4_DEFM_DEBUG)
3580 		set_opt(sb, DEBUG);
3581 	if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
3582 		set_opt(sb, GRPID);
3583 	if (def_mount_opts & EXT4_DEFM_UID16)
3584 		set_opt(sb, NO_UID32);
3585 	/* xattr user namespace & acls are now defaulted on */
3586 	set_opt(sb, XATTR_USER);
3587 #ifdef CONFIG_EXT4_FS_POSIX_ACL
3588 	set_opt(sb, POSIX_ACL);
3589 #endif
3590 	/* don't forget to enable journal_csum when metadata_csum is enabled. */
3591 	if (ext4_has_metadata_csum(sb))
3592 		set_opt(sb, JOURNAL_CHECKSUM);
3593 
3594 	if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
3595 		set_opt(sb, JOURNAL_DATA);
3596 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
3597 		set_opt(sb, ORDERED_DATA);
3598 	else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
3599 		set_opt(sb, WRITEBACK_DATA);
3600 
3601 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
3602 		set_opt(sb, ERRORS_PANIC);
3603 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
3604 		set_opt(sb, ERRORS_CONT);
3605 	else
3606 		set_opt(sb, ERRORS_RO);
3607 	/* block_validity enabled by default; disable with noblock_validity */
3608 	set_opt(sb, BLOCK_VALIDITY);
3609 	if (def_mount_opts & EXT4_DEFM_DISCARD)
3610 		set_opt(sb, DISCARD);
3611 
3612 	sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
3613 	sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
3614 	sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
3615 	sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
3616 	sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
3617 
3618 	if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
3619 		set_opt(sb, BARRIER);
3620 
3621 	/*
3622 	 * enable delayed allocation by default
3623 	 * Use -o nodelalloc to turn it off
3624 	 */
3625 	if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
3626 	    ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
3627 		set_opt(sb, DELALLOC);
3628 
3629 	/*
3630 	 * set default s_li_wait_mult for lazyinit, for the case there is
3631 	 * no mount option specified.
3632 	 */
3633 	sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
3634 
3635 	if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
3636 			   &journal_devnum, &journal_ioprio, 0)) {
3637 		ext4_msg(sb, KERN_WARNING,
3638 			 "failed to parse options in superblock: %s",
3639 			 sbi->s_es->s_mount_opts);
3640 	}
3641 	sbi->s_def_mount_opt = sbi->s_mount_opt;
3642 	if (!parse_options((char *) data, sb, &journal_devnum,
3643 			   &journal_ioprio, 0))
3644 		goto failed_mount;
3645 
3646 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
3647 		printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
3648 			    "with data=journal disables delayed "
3649 			    "allocation and O_DIRECT support!\n");
3650 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
3651 			ext4_msg(sb, KERN_ERR, "can't mount with "
3652 				 "both data=journal and delalloc");
3653 			goto failed_mount;
3654 		}
3655 		if (test_opt(sb, DIOREAD_NOLOCK)) {
3656 			ext4_msg(sb, KERN_ERR, "can't mount with "
3657 				 "both data=journal and dioread_nolock");
3658 			goto failed_mount;
3659 		}
3660 		if (test_opt(sb, DELALLOC))
3661 			clear_opt(sb, DELALLOC);
3662 	}
3663 
3664 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
3665 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
3666 
3667 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
3668 	    (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
3669 	     EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
3670 	     EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
3671 		ext4_msg(sb, KERN_WARNING,
3672 		       "feature flags set on rev 0 fs, "
3673 		       "running e2fsck is recommended");
3674 
3675 	if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
3676 		set_opt2(sb, HURD_COMPAT);
3677 		if (EXT4_HAS_INCOMPAT_FEATURE(sb,
3678 					      EXT4_FEATURE_INCOMPAT_64BIT)) {
3679 			ext4_msg(sb, KERN_ERR,
3680 				 "The Hurd can't support 64-bit file systems");
3681 			goto failed_mount;
3682 		}
3683 	}
3684 
3685 	if (IS_EXT2_SB(sb)) {
3686 		if (ext2_feature_set_ok(sb))
3687 			ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
3688 				 "using the ext4 subsystem");
3689 		else {
3690 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
3691 				 "to feature incompatibilities");
3692 			goto failed_mount;
3693 		}
3694 	}
3695 
3696 	if (IS_EXT3_SB(sb)) {
3697 		if (ext3_feature_set_ok(sb))
3698 			ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
3699 				 "using the ext4 subsystem");
3700 		else {
3701 			ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
3702 				 "to feature incompatibilities");
3703 			goto failed_mount;
3704 		}
3705 	}
3706 
3707 	/*
3708 	 * Check feature flags regardless of the revision level, since we
3709 	 * previously didn't change the revision level when setting the flags,
3710 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
3711 	 */
3712 	if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
3713 		goto failed_mount;
3714 
3715 	blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
3716 	if (blocksize < EXT4_MIN_BLOCK_SIZE ||
3717 	    blocksize > EXT4_MAX_BLOCK_SIZE) {
3718 		ext4_msg(sb, KERN_ERR,
3719 		       "Unsupported filesystem blocksize %d (%d log_block_size)",
3720 			 blocksize, le32_to_cpu(es->s_log_block_size));
3721 		goto failed_mount;
3722 	}
3723 	if (le32_to_cpu(es->s_log_block_size) >
3724 	    (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
3725 		ext4_msg(sb, KERN_ERR,
3726 			 "Invalid log block size: %u",
3727 			 le32_to_cpu(es->s_log_block_size));
3728 		goto failed_mount;
3729 	}
3730 
3731 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_ENCRYPT) &&
3732 	    es->s_encryption_level) {
3733 		ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
3734 			 es->s_encryption_level);
3735 		goto failed_mount;
3736 	}
3737 
3738 	if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
3739 		ext4_msg(sb, KERN_ERR,
3740 			 "Number of reserved GDT blocks insanely large: %d",
3741 			 le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
3742 		goto failed_mount;
3743 	}
3744 
3745 	if (sb->s_blocksize != blocksize) {
3746 		/* Validate the filesystem blocksize */
3747 		if (!sb_set_blocksize(sb, blocksize)) {
3748 			ext4_msg(sb, KERN_ERR, "bad block size %d",
3749 					blocksize);
3750 			goto failed_mount;
3751 		}
3752 
3753 		brelse(bh);
3754 		logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
3755 		offset = do_div(logical_sb_block, blocksize);
3756 		bh = sb_bread_unmovable(sb, logical_sb_block);
3757 		if (!bh) {
3758 			ext4_msg(sb, KERN_ERR,
3759 			       "Can't read superblock on 2nd try");
3760 			goto failed_mount;
3761 		}
3762 		es = (struct ext4_super_block *)(bh->b_data + offset);
3763 		sbi->s_es = es;
3764 		if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
3765 			ext4_msg(sb, KERN_ERR,
3766 			       "Magic mismatch, very weird!");
3767 			goto failed_mount;
3768 		}
3769 	}
3770 
3771 	has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3772 				EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
3773 	sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
3774 						      has_huge_files);
3775 	sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
3776 
3777 	if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
3778 		sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
3779 		sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
3780 	} else {
3781 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
3782 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
3783 		if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
3784 		    (!is_power_of_2(sbi->s_inode_size)) ||
3785 		    (sbi->s_inode_size > blocksize)) {
3786 			ext4_msg(sb, KERN_ERR,
3787 			       "unsupported inode size: %d",
3788 			       sbi->s_inode_size);
3789 			goto failed_mount;
3790 		}
3791 		if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
3792 			sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
3793 	}
3794 
3795 	sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
3796 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
3797 		if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
3798 		    sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
3799 		    !is_power_of_2(sbi->s_desc_size)) {
3800 			ext4_msg(sb, KERN_ERR,
3801 			       "unsupported descriptor size %lu",
3802 			       sbi->s_desc_size);
3803 			goto failed_mount;
3804 		}
3805 	} else
3806 		sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
3807 
3808 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
3809 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
3810 
3811 	sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
3812 	if (sbi->s_inodes_per_block == 0)
3813 		goto cantfind_ext4;
3814 	if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
3815 	    sbi->s_inodes_per_group > blocksize * 8) {
3816 		ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
3817 			 sbi->s_blocks_per_group);
3818 		goto failed_mount;
3819 	}
3820 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
3821 					sbi->s_inodes_per_block;
3822 	sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
3823 	sbi->s_sbh = bh;
3824 	sbi->s_mount_state = le16_to_cpu(es->s_state);
3825 	sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
3826 	sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
3827 
3828 	for (i = 0; i < 4; i++)
3829 		sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
3830 	sbi->s_def_hash_version = es->s_def_hash_version;
3831 	if (EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) {
3832 		i = le32_to_cpu(es->s_flags);
3833 		if (i & EXT2_FLAGS_UNSIGNED_HASH)
3834 			sbi->s_hash_unsigned = 3;
3835 		else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
3836 #ifdef __CHAR_UNSIGNED__
3837 			if (!(sb->s_flags & MS_RDONLY))
3838 				es->s_flags |=
3839 					cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
3840 			sbi->s_hash_unsigned = 3;
3841 #else
3842 			if (!(sb->s_flags & MS_RDONLY))
3843 				es->s_flags |=
3844 					cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
3845 #endif
3846 		}
3847 	}
3848 
3849 	/* Handle clustersize */
3850 	clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
3851 	has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
3852 				EXT4_FEATURE_RO_COMPAT_BIGALLOC);
3853 	if (has_bigalloc) {
3854 		if (clustersize < blocksize) {
3855 			ext4_msg(sb, KERN_ERR,
3856 				 "cluster size (%d) smaller than "
3857 				 "block size (%d)", clustersize, blocksize);
3858 			goto failed_mount;
3859 		}
3860 		if (le32_to_cpu(es->s_log_cluster_size) >
3861 		    (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
3862 			ext4_msg(sb, KERN_ERR,
3863 				 "Invalid log cluster size: %u",
3864 				 le32_to_cpu(es->s_log_cluster_size));
3865 			goto failed_mount;
3866 		}
3867 		sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
3868 			le32_to_cpu(es->s_log_block_size);
3869 		sbi->s_clusters_per_group =
3870 			le32_to_cpu(es->s_clusters_per_group);
3871 		if (sbi->s_clusters_per_group > blocksize * 8) {
3872 			ext4_msg(sb, KERN_ERR,
3873 				 "#clusters per group too big: %lu",
3874 				 sbi->s_clusters_per_group);
3875 			goto failed_mount;
3876 		}
3877 		if (sbi->s_blocks_per_group !=
3878 		    (sbi->s_clusters_per_group * (clustersize / blocksize))) {
3879 			ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
3880 				 "clusters per group (%lu) inconsistent",
3881 				 sbi->s_blocks_per_group,
3882 				 sbi->s_clusters_per_group);
3883 			goto failed_mount;
3884 		}
3885 	} else {
3886 		if (clustersize != blocksize) {
3887 			ext4_warning(sb, "fragment/cluster size (%d) != "
3888 				     "block size (%d)", clustersize,
3889 				     blocksize);
3890 			clustersize = blocksize;
3891 		}
3892 		if (sbi->s_blocks_per_group > blocksize * 8) {
3893 			ext4_msg(sb, KERN_ERR,
3894 				 "#blocks per group too big: %lu",
3895 				 sbi->s_blocks_per_group);
3896 			goto failed_mount;
3897 		}
3898 		sbi->s_clusters_per_group = sbi->s_blocks_per_group;
3899 		sbi->s_cluster_bits = 0;
3900 	}
3901 	sbi->s_cluster_ratio = clustersize / blocksize;
3902 
3903 	/* Do we have standard group size of clustersize * 8 blocks ? */
3904 	if (sbi->s_blocks_per_group == clustersize << 3)
3905 		set_opt2(sb, STD_GROUP_SIZE);
3906 
3907 	/*
3908 	 * Test whether we have more sectors than will fit in sector_t,
3909 	 * and whether the max offset is addressable by the page cache.
3910 	 */
3911 	err = generic_check_addressable(sb->s_blocksize_bits,
3912 					ext4_blocks_count(es));
3913 	if (err) {
3914 		ext4_msg(sb, KERN_ERR, "filesystem"
3915 			 " too large to mount safely on this system");
3916 		if (sizeof(sector_t) < 8)
3917 			ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
3918 		goto failed_mount;
3919 	}
3920 
3921 	if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
3922 		goto cantfind_ext4;
3923 
3924 	/* check blocks count against device size */
3925 	blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
3926 	if (blocks_count && ext4_blocks_count(es) > blocks_count) {
3927 		ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
3928 		       "exceeds size of device (%llu blocks)",
3929 		       ext4_blocks_count(es), blocks_count);
3930 		goto failed_mount;
3931 	}
3932 
3933 	/*
3934 	 * It makes no sense for the first data block to be beyond the end
3935 	 * of the filesystem.
3936 	 */
3937 	if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
3938 		ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
3939 			 "block %u is beyond end of filesystem (%llu)",
3940 			 le32_to_cpu(es->s_first_data_block),
3941 			 ext4_blocks_count(es));
3942 		goto failed_mount;
3943 	}
3944 	blocks_count = (ext4_blocks_count(es) -
3945 			le32_to_cpu(es->s_first_data_block) +
3946 			EXT4_BLOCKS_PER_GROUP(sb) - 1);
3947 	do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
3948 	if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
3949 		ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
3950 		       "(block count %llu, first data block %u, "
3951 		       "blocks per group %lu)", sbi->s_groups_count,
3952 		       ext4_blocks_count(es),
3953 		       le32_to_cpu(es->s_first_data_block),
3954 		       EXT4_BLOCKS_PER_GROUP(sb));
3955 		goto failed_mount;
3956 	}
3957 	sbi->s_groups_count = blocks_count;
3958 	sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
3959 			(EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
3960 	db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
3961 		   EXT4_DESC_PER_BLOCK(sb);
3962 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG)) {
3963 		if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
3964 			ext4_msg(sb, KERN_WARNING,
3965 				 "first meta block group too large: %u "
3966 				 "(group descriptor block count %u)",
3967 				 le32_to_cpu(es->s_first_meta_bg), db_count);
3968 			goto failed_mount;
3969 		}
3970 	}
3971 	sbi->s_group_desc = ext4_kvmalloc(db_count *
3972 					  sizeof(struct buffer_head *),
3973 					  GFP_KERNEL);
3974 	if (sbi->s_group_desc == NULL) {
3975 		ext4_msg(sb, KERN_ERR, "not enough memory");
3976 		ret = -ENOMEM;
3977 		goto failed_mount;
3978 	}
3979 
3980 	if (ext4_proc_root)
3981 		sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
3982 
3983 	if (sbi->s_proc)
3984 		proc_create_data("options", S_IRUGO, sbi->s_proc,
3985 				 &ext4_seq_options_fops, sb);
3986 
3987 	bgl_lock_init(sbi->s_blockgroup_lock);
3988 
3989 	for (i = 0; i < db_count; i++) {
3990 		block = descriptor_loc(sb, logical_sb_block, i);
3991 		sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
3992 		if (!sbi->s_group_desc[i]) {
3993 			ext4_msg(sb, KERN_ERR,
3994 			       "can't read group descriptor %d", i);
3995 			db_count = i;
3996 			goto failed_mount2;
3997 		}
3998 	}
3999 	if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
4000 		ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
4001 		goto failed_mount2;
4002 	}
4003 
4004 	sbi->s_gdb_count = db_count;
4005 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
4006 	spin_lock_init(&sbi->s_next_gen_lock);
4007 
4008 	init_timer(&sbi->s_err_report);
4009 	sbi->s_err_report.function = print_daily_error_info;
4010 	sbi->s_err_report.data = (unsigned long) sb;
4011 
4012 	/* Register extent status tree shrinker */
4013 	if (ext4_es_register_shrinker(sbi))
4014 		goto failed_mount3;
4015 
4016 	sbi->s_stripe = ext4_get_stripe_size(sbi);
4017 	sbi->s_extent_max_zeroout_kb = 32;
4018 
4019 	/*
4020 	 * set up enough so that it can read an inode
4021 	 */
4022 	sb->s_op = &ext4_sops;
4023 	sb->s_export_op = &ext4_export_ops;
4024 	sb->s_xattr = ext4_xattr_handlers;
4025 #ifdef CONFIG_QUOTA
4026 	sb->dq_op = &ext4_quota_operations;
4027 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
4028 		sb->s_qcop = &ext4_qctl_sysfile_operations;
4029 	else
4030 		sb->s_qcop = &ext4_qctl_operations;
4031 #endif
4032 	memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
4033 
4034 	INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
4035 	mutex_init(&sbi->s_orphan_lock);
4036 
4037 	sb->s_root = NULL;
4038 
4039 	needs_recovery = (es->s_last_orphan != 0 ||
4040 			  EXT4_HAS_INCOMPAT_FEATURE(sb,
4041 				    EXT4_FEATURE_INCOMPAT_RECOVER));
4042 
4043 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
4044 	    !(sb->s_flags & MS_RDONLY))
4045 		if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
4046 			goto failed_mount3a;
4047 
4048 	/*
4049 	 * The first inode we look at is the journal inode.  Don't try
4050 	 * root first: it may be modified in the journal!
4051 	 */
4052 	if (!test_opt(sb, NOLOAD) &&
4053 	    EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
4054 		if (ext4_load_journal(sb, es, journal_devnum))
4055 			goto failed_mount3a;
4056 	} else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
4057 	      EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4058 		ext4_msg(sb, KERN_ERR, "required journal recovery "
4059 		       "suppressed and not mounted read-only");
4060 		goto failed_mount_wq;
4061 	} else {
4062 		clear_opt(sb, DATA_FLAGS);
4063 		sbi->s_journal = NULL;
4064 		needs_recovery = 0;
4065 		goto no_journal;
4066 	}
4067 
4068 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT) &&
4069 	    !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
4070 				       JBD2_FEATURE_INCOMPAT_64BIT)) {
4071 		ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
4072 		goto failed_mount_wq;
4073 	}
4074 
4075 	if (!set_journal_csum_feature_set(sb)) {
4076 		ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
4077 			 "feature set");
4078 		goto failed_mount_wq;
4079 	}
4080 
4081 	/* We have now updated the journal if required, so we can
4082 	 * validate the data journaling mode. */
4083 	switch (test_opt(sb, DATA_FLAGS)) {
4084 	case 0:
4085 		/* No mode set, assume a default based on the journal
4086 		 * capabilities: ORDERED_DATA if the journal can
4087 		 * cope, else JOURNAL_DATA
4088 		 */
4089 		if (jbd2_journal_check_available_features
4090 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
4091 			set_opt(sb, ORDERED_DATA);
4092 		else
4093 			set_opt(sb, JOURNAL_DATA);
4094 		break;
4095 
4096 	case EXT4_MOUNT_ORDERED_DATA:
4097 	case EXT4_MOUNT_WRITEBACK_DATA:
4098 		if (!jbd2_journal_check_available_features
4099 		    (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
4100 			ext4_msg(sb, KERN_ERR, "Journal does not support "
4101 			       "requested data journaling mode");
4102 			goto failed_mount_wq;
4103 		}
4104 	default:
4105 		break;
4106 	}
4107 	set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4108 
4109 	sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
4110 
4111 no_journal:
4112 	if (ext4_mballoc_ready) {
4113 		sbi->s_mb_cache = ext4_xattr_create_cache(sb->s_id);
4114 		if (!sbi->s_mb_cache) {
4115 			ext4_msg(sb, KERN_ERR, "Failed to create an mb_cache");
4116 			goto failed_mount_wq;
4117 		}
4118 	}
4119 
4120 	if ((DUMMY_ENCRYPTION_ENABLED(sbi) ||
4121 	     EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_ENCRYPT)) &&
4122 	    (blocksize != PAGE_CACHE_SIZE)) {
4123 		ext4_msg(sb, KERN_ERR,
4124 			 "Unsupported blocksize for fs encryption");
4125 		goto failed_mount_wq;
4126 	}
4127 
4128 	if (DUMMY_ENCRYPTION_ENABLED(sbi) &&
4129 	    !(sb->s_flags & MS_RDONLY) &&
4130 	    !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_ENCRYPT)) {
4131 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_ENCRYPT);
4132 		ext4_commit_super(sb, 1);
4133 	}
4134 
4135 	/*
4136 	 * Get the # of file system overhead blocks from the
4137 	 * superblock if present.
4138 	 */
4139 	if (es->s_overhead_clusters)
4140 		sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
4141 	else {
4142 		err = ext4_calculate_overhead(sb);
4143 		if (err)
4144 			goto failed_mount_wq;
4145 	}
4146 
4147 	/*
4148 	 * The maximum number of concurrent works can be high and
4149 	 * concurrency isn't really necessary.  Limit it to 1.
4150 	 */
4151 	EXT4_SB(sb)->rsv_conversion_wq =
4152 		alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
4153 	if (!EXT4_SB(sb)->rsv_conversion_wq) {
4154 		printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
4155 		ret = -ENOMEM;
4156 		goto failed_mount4;
4157 	}
4158 
4159 	/*
4160 	 * The jbd2_journal_load will have done any necessary log recovery,
4161 	 * so we can safely mount the rest of the filesystem now.
4162 	 */
4163 
4164 	root = ext4_iget(sb, EXT4_ROOT_INO);
4165 	if (IS_ERR(root)) {
4166 		ext4_msg(sb, KERN_ERR, "get root inode failed");
4167 		ret = PTR_ERR(root);
4168 		root = NULL;
4169 		goto failed_mount4;
4170 	}
4171 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
4172 		ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
4173 		iput(root);
4174 		goto failed_mount4;
4175 	}
4176 	sb->s_root = d_make_root(root);
4177 	if (!sb->s_root) {
4178 		ext4_msg(sb, KERN_ERR, "get root dentry failed");
4179 		ret = -ENOMEM;
4180 		goto failed_mount4;
4181 	}
4182 
4183 	if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
4184 		sb->s_flags |= MS_RDONLY;
4185 
4186 	/* determine the minimum size of new large inodes, if present */
4187 	if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
4188 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4189 						     EXT4_GOOD_OLD_INODE_SIZE;
4190 		if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
4191 				       EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
4192 			if (sbi->s_want_extra_isize <
4193 			    le16_to_cpu(es->s_want_extra_isize))
4194 				sbi->s_want_extra_isize =
4195 					le16_to_cpu(es->s_want_extra_isize);
4196 			if (sbi->s_want_extra_isize <
4197 			    le16_to_cpu(es->s_min_extra_isize))
4198 				sbi->s_want_extra_isize =
4199 					le16_to_cpu(es->s_min_extra_isize);
4200 		}
4201 	}
4202 	/* Check if enough inode space is available */
4203 	if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
4204 							sbi->s_inode_size) {
4205 		sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
4206 						       EXT4_GOOD_OLD_INODE_SIZE;
4207 		ext4_msg(sb, KERN_INFO, "required extra inode space not"
4208 			 "available");
4209 	}
4210 
4211 	err = ext4_reserve_clusters(sbi, ext4_calculate_resv_clusters(sb));
4212 	if (err) {
4213 		ext4_msg(sb, KERN_ERR, "failed to reserve %llu clusters for "
4214 			 "reserved pool", ext4_calculate_resv_clusters(sb));
4215 		goto failed_mount4a;
4216 	}
4217 
4218 	err = ext4_setup_system_zone(sb);
4219 	if (err) {
4220 		ext4_msg(sb, KERN_ERR, "failed to initialize system "
4221 			 "zone (%d)", err);
4222 		goto failed_mount4a;
4223 	}
4224 
4225 	ext4_ext_init(sb);
4226 	err = ext4_mb_init(sb);
4227 	if (err) {
4228 		ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
4229 			 err);
4230 		goto failed_mount5;
4231 	}
4232 
4233 	block = ext4_count_free_clusters(sb);
4234 	ext4_free_blocks_count_set(sbi->s_es,
4235 				   EXT4_C2B(sbi, block));
4236 	err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
4237 				  GFP_KERNEL);
4238 	if (!err) {
4239 		unsigned long freei = ext4_count_free_inodes(sb);
4240 		sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
4241 		err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
4242 					  GFP_KERNEL);
4243 	}
4244 	if (!err)
4245 		err = percpu_counter_init(&sbi->s_dirs_counter,
4246 					  ext4_count_dirs(sb), GFP_KERNEL);
4247 	if (!err)
4248 		err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
4249 					  GFP_KERNEL);
4250 	if (err) {
4251 		ext4_msg(sb, KERN_ERR, "insufficient memory");
4252 		goto failed_mount6;
4253 	}
4254 
4255 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
4256 		if (!ext4_fill_flex_info(sb)) {
4257 			ext4_msg(sb, KERN_ERR,
4258 			       "unable to initialize "
4259 			       "flex_bg meta info!");
4260 			goto failed_mount6;
4261 		}
4262 
4263 	err = ext4_register_li_request(sb, first_not_zeroed);
4264 	if (err)
4265 		goto failed_mount6;
4266 
4267 	sbi->s_kobj.kset = ext4_kset;
4268 	init_completion(&sbi->s_kobj_unregister);
4269 	err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
4270 				   "%s", sb->s_id);
4271 	if (err)
4272 		goto failed_mount7;
4273 
4274 #ifdef CONFIG_QUOTA
4275 	/* Enable quota usage during mount. */
4276 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) &&
4277 	    !(sb->s_flags & MS_RDONLY)) {
4278 		err = ext4_enable_quotas(sb);
4279 		if (err)
4280 			goto failed_mount8;
4281 	}
4282 #endif  /* CONFIG_QUOTA */
4283 
4284 	EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
4285 	ext4_orphan_cleanup(sb, es);
4286 	EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
4287 	if (needs_recovery) {
4288 		ext4_msg(sb, KERN_INFO, "recovery complete");
4289 		ext4_mark_recovery_complete(sb, es);
4290 	}
4291 	if (EXT4_SB(sb)->s_journal) {
4292 		if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
4293 			descr = " journalled data mode";
4294 		else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
4295 			descr = " ordered data mode";
4296 		else
4297 			descr = " writeback data mode";
4298 	} else
4299 		descr = "out journal";
4300 
4301 	if (test_opt(sb, DISCARD)) {
4302 		struct request_queue *q = bdev_get_queue(sb->s_bdev);
4303 		if (!blk_queue_discard(q))
4304 			ext4_msg(sb, KERN_WARNING,
4305 				 "mounting with \"discard\" option, but "
4306 				 "the device does not support discard");
4307 	}
4308 
4309 	ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
4310 		 "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
4311 		 *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
4312 
4313 	if (es->s_error_count)
4314 		mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
4315 
4316 	/* Enable message ratelimiting. Default is 10 messages per 5 secs. */
4317 	ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
4318 	ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
4319 	ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
4320 
4321 	kfree(orig_data);
4322 	return 0;
4323 
4324 cantfind_ext4:
4325 	if (!silent)
4326 		ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
4327 	goto failed_mount;
4328 
4329 #ifdef CONFIG_QUOTA
4330 failed_mount8:
4331 	kobject_del(&sbi->s_kobj);
4332 #endif
4333 failed_mount7:
4334 	ext4_unregister_li_request(sb);
4335 failed_mount6:
4336 	ext4_mb_release(sb);
4337 	if (sbi->s_flex_groups)
4338 		ext4_kvfree(sbi->s_flex_groups);
4339 	percpu_counter_destroy(&sbi->s_freeclusters_counter);
4340 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
4341 	percpu_counter_destroy(&sbi->s_dirs_counter);
4342 	percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
4343 failed_mount5:
4344 	ext4_ext_release(sb);
4345 	ext4_release_system_zone(sb);
4346 failed_mount4a:
4347 	dput(sb->s_root);
4348 	sb->s_root = NULL;
4349 failed_mount4:
4350 	ext4_msg(sb, KERN_ERR, "mount failed");
4351 	if (EXT4_SB(sb)->rsv_conversion_wq)
4352 		destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
4353 failed_mount_wq:
4354 	if (sbi->s_journal) {
4355 		jbd2_journal_destroy(sbi->s_journal);
4356 		sbi->s_journal = NULL;
4357 	}
4358 failed_mount3a:
4359 	ext4_es_unregister_shrinker(sbi);
4360 failed_mount3:
4361 	del_timer_sync(&sbi->s_err_report);
4362 	if (sbi->s_mmp_tsk)
4363 		kthread_stop(sbi->s_mmp_tsk);
4364 failed_mount2:
4365 	for (i = 0; i < db_count; i++)
4366 		brelse(sbi->s_group_desc[i]);
4367 	ext4_kvfree(sbi->s_group_desc);
4368 failed_mount:
4369 	if (sbi->s_chksum_driver)
4370 		crypto_free_shash(sbi->s_chksum_driver);
4371 	if (sbi->s_proc) {
4372 		remove_proc_entry("options", sbi->s_proc);
4373 		remove_proc_entry(sb->s_id, ext4_proc_root);
4374 	}
4375 #ifdef CONFIG_QUOTA
4376 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4377 		kfree(sbi->s_qf_names[i]);
4378 #endif
4379 	ext4_blkdev_remove(sbi);
4380 	brelse(bh);
4381 out_fail:
4382 	sb->s_fs_info = NULL;
4383 	kfree(sbi->s_blockgroup_lock);
4384 	kfree(sbi);
4385 out_free_orig:
4386 	kfree(orig_data);
4387 	return err ? err : ret;
4388 }
4389 
4390 /*
4391  * Setup any per-fs journal parameters now.  We'll do this both on
4392  * initial mount, once the journal has been initialised but before we've
4393  * done any recovery; and again on any subsequent remount.
4394  */
ext4_init_journal_params(struct super_block * sb,journal_t * journal)4395 static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
4396 {
4397 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4398 
4399 	journal->j_commit_interval = sbi->s_commit_interval;
4400 	journal->j_min_batch_time = sbi->s_min_batch_time;
4401 	journal->j_max_batch_time = sbi->s_max_batch_time;
4402 
4403 	write_lock(&journal->j_state_lock);
4404 	if (test_opt(sb, BARRIER))
4405 		journal->j_flags |= JBD2_BARRIER;
4406 	else
4407 		journal->j_flags &= ~JBD2_BARRIER;
4408 	if (test_opt(sb, DATA_ERR_ABORT))
4409 		journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
4410 	else
4411 		journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
4412 	write_unlock(&journal->j_state_lock);
4413 }
4414 
ext4_get_journal(struct super_block * sb,unsigned int journal_inum)4415 static journal_t *ext4_get_journal(struct super_block *sb,
4416 				   unsigned int journal_inum)
4417 {
4418 	struct inode *journal_inode;
4419 	journal_t *journal;
4420 
4421 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4422 
4423 	/* First, test for the existence of a valid inode on disk.  Bad
4424 	 * things happen if we iget() an unused inode, as the subsequent
4425 	 * iput() will try to delete it. */
4426 
4427 	journal_inode = ext4_iget(sb, journal_inum);
4428 	if (IS_ERR(journal_inode)) {
4429 		ext4_msg(sb, KERN_ERR, "no journal found");
4430 		return NULL;
4431 	}
4432 	if (!journal_inode->i_nlink) {
4433 		make_bad_inode(journal_inode);
4434 		iput(journal_inode);
4435 		ext4_msg(sb, KERN_ERR, "journal inode is deleted");
4436 		return NULL;
4437 	}
4438 
4439 	jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
4440 		  journal_inode, journal_inode->i_size);
4441 	if (!S_ISREG(journal_inode->i_mode)) {
4442 		ext4_msg(sb, KERN_ERR, "invalid journal inode");
4443 		iput(journal_inode);
4444 		return NULL;
4445 	}
4446 
4447 	journal = jbd2_journal_init_inode(journal_inode);
4448 	if (!journal) {
4449 		ext4_msg(sb, KERN_ERR, "Could not load journal inode");
4450 		iput(journal_inode);
4451 		return NULL;
4452 	}
4453 	journal->j_private = sb;
4454 	ext4_init_journal_params(sb, journal);
4455 	return journal;
4456 }
4457 
ext4_get_dev_journal(struct super_block * sb,dev_t j_dev)4458 static journal_t *ext4_get_dev_journal(struct super_block *sb,
4459 				       dev_t j_dev)
4460 {
4461 	struct buffer_head *bh;
4462 	journal_t *journal;
4463 	ext4_fsblk_t start;
4464 	ext4_fsblk_t len;
4465 	int hblock, blocksize;
4466 	ext4_fsblk_t sb_block;
4467 	unsigned long offset;
4468 	struct ext4_super_block *es;
4469 	struct block_device *bdev;
4470 
4471 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4472 
4473 	bdev = ext4_blkdev_get(j_dev, sb);
4474 	if (bdev == NULL)
4475 		return NULL;
4476 
4477 	blocksize = sb->s_blocksize;
4478 	hblock = bdev_logical_block_size(bdev);
4479 	if (blocksize < hblock) {
4480 		ext4_msg(sb, KERN_ERR,
4481 			"blocksize too small for journal device");
4482 		goto out_bdev;
4483 	}
4484 
4485 	sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
4486 	offset = EXT4_MIN_BLOCK_SIZE % blocksize;
4487 	set_blocksize(bdev, blocksize);
4488 	if (!(bh = __bread(bdev, sb_block, blocksize))) {
4489 		ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
4490 		       "external journal");
4491 		goto out_bdev;
4492 	}
4493 
4494 	es = (struct ext4_super_block *) (bh->b_data + offset);
4495 	if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
4496 	    !(le32_to_cpu(es->s_feature_incompat) &
4497 	      EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
4498 		ext4_msg(sb, KERN_ERR, "external journal has "
4499 					"bad superblock");
4500 		brelse(bh);
4501 		goto out_bdev;
4502 	}
4503 
4504 	if ((le32_to_cpu(es->s_feature_ro_compat) &
4505 	     EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
4506 	    es->s_checksum != ext4_superblock_csum(sb, es)) {
4507 		ext4_msg(sb, KERN_ERR, "external journal has "
4508 				       "corrupt superblock");
4509 		brelse(bh);
4510 		goto out_bdev;
4511 	}
4512 
4513 	if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
4514 		ext4_msg(sb, KERN_ERR, "journal UUID does not match");
4515 		brelse(bh);
4516 		goto out_bdev;
4517 	}
4518 
4519 	len = ext4_blocks_count(es);
4520 	start = sb_block + 1;
4521 	brelse(bh);	/* we're done with the superblock */
4522 
4523 	journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
4524 					start, len, blocksize);
4525 	if (!journal) {
4526 		ext4_msg(sb, KERN_ERR, "failed to create device journal");
4527 		goto out_bdev;
4528 	}
4529 	journal->j_private = sb;
4530 	ll_rw_block(READ | REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
4531 	wait_on_buffer(journal->j_sb_buffer);
4532 	if (!buffer_uptodate(journal->j_sb_buffer)) {
4533 		ext4_msg(sb, KERN_ERR, "I/O error on journal device");
4534 		goto out_journal;
4535 	}
4536 	if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
4537 		ext4_msg(sb, KERN_ERR, "External journal has more than one "
4538 					"user (unsupported) - %d",
4539 			be32_to_cpu(journal->j_superblock->s_nr_users));
4540 		goto out_journal;
4541 	}
4542 	EXT4_SB(sb)->journal_bdev = bdev;
4543 	ext4_init_journal_params(sb, journal);
4544 	return journal;
4545 
4546 out_journal:
4547 	jbd2_journal_destroy(journal);
4548 out_bdev:
4549 	ext4_blkdev_put(bdev);
4550 	return NULL;
4551 }
4552 
ext4_load_journal(struct super_block * sb,struct ext4_super_block * es,unsigned long journal_devnum)4553 static int ext4_load_journal(struct super_block *sb,
4554 			     struct ext4_super_block *es,
4555 			     unsigned long journal_devnum)
4556 {
4557 	journal_t *journal;
4558 	unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
4559 	dev_t journal_dev;
4560 	int err = 0;
4561 	int really_read_only;
4562 
4563 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4564 
4565 	if (journal_devnum &&
4566 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4567 		ext4_msg(sb, KERN_INFO, "external journal device major/minor "
4568 			"numbers have changed");
4569 		journal_dev = new_decode_dev(journal_devnum);
4570 	} else
4571 		journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
4572 
4573 	really_read_only = bdev_read_only(sb->s_bdev);
4574 
4575 	/*
4576 	 * Are we loading a blank journal or performing recovery after a
4577 	 * crash?  For recovery, we need to check in advance whether we
4578 	 * can get read-write access to the device.
4579 	 */
4580 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
4581 		if (sb->s_flags & MS_RDONLY) {
4582 			ext4_msg(sb, KERN_INFO, "INFO: recovery "
4583 					"required on readonly filesystem");
4584 			if (really_read_only) {
4585 				ext4_msg(sb, KERN_ERR, "write access "
4586 					"unavailable, cannot proceed");
4587 				return -EROFS;
4588 			}
4589 			ext4_msg(sb, KERN_INFO, "write access will "
4590 			       "be enabled during recovery");
4591 		}
4592 	}
4593 
4594 	if (journal_inum && journal_dev) {
4595 		ext4_msg(sb, KERN_ERR, "filesystem has both journal "
4596 		       "and inode journals!");
4597 		return -EINVAL;
4598 	}
4599 
4600 	if (journal_inum) {
4601 		if (!(journal = ext4_get_journal(sb, journal_inum)))
4602 			return -EINVAL;
4603 	} else {
4604 		if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
4605 			return -EINVAL;
4606 	}
4607 
4608 	if (!(journal->j_flags & JBD2_BARRIER))
4609 		ext4_msg(sb, KERN_INFO, "barriers disabled");
4610 
4611 	if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
4612 		err = jbd2_journal_wipe(journal, !really_read_only);
4613 	if (!err) {
4614 		char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
4615 		if (save)
4616 			memcpy(save, ((char *) es) +
4617 			       EXT4_S_ERR_START, EXT4_S_ERR_LEN);
4618 		err = jbd2_journal_load(journal);
4619 		if (save)
4620 			memcpy(((char *) es) + EXT4_S_ERR_START,
4621 			       save, EXT4_S_ERR_LEN);
4622 		kfree(save);
4623 	}
4624 
4625 	if (err) {
4626 		ext4_msg(sb, KERN_ERR, "error loading journal");
4627 		jbd2_journal_destroy(journal);
4628 		return err;
4629 	}
4630 
4631 	EXT4_SB(sb)->s_journal = journal;
4632 	ext4_clear_journal_err(sb, es);
4633 
4634 	if (!really_read_only && journal_devnum &&
4635 	    journal_devnum != le32_to_cpu(es->s_journal_dev)) {
4636 		es->s_journal_dev = cpu_to_le32(journal_devnum);
4637 
4638 		/* Make sure we flush the recovery flag to disk. */
4639 		ext4_commit_super(sb, 1);
4640 	}
4641 
4642 	return 0;
4643 }
4644 
ext4_commit_super(struct super_block * sb,int sync)4645 static int ext4_commit_super(struct super_block *sb, int sync)
4646 {
4647 	struct ext4_super_block *es = EXT4_SB(sb)->s_es;
4648 	struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
4649 	int error = 0;
4650 
4651 	if (!sbh || block_device_ejected(sb))
4652 		return error;
4653 	if (buffer_write_io_error(sbh)) {
4654 		/*
4655 		 * Oh, dear.  A previous attempt to write the
4656 		 * superblock failed.  This could happen because the
4657 		 * USB device was yanked out.  Or it could happen to
4658 		 * be a transient write error and maybe the block will
4659 		 * be remapped.  Nothing we can do but to retry the
4660 		 * write and hope for the best.
4661 		 */
4662 		ext4_msg(sb, KERN_ERR, "previous I/O error to "
4663 		       "superblock detected");
4664 		clear_buffer_write_io_error(sbh);
4665 		set_buffer_uptodate(sbh);
4666 	}
4667 	/*
4668 	 * If the file system is mounted read-only, don't update the
4669 	 * superblock write time.  This avoids updating the superblock
4670 	 * write time when we are mounting the root file system
4671 	 * read/only but we need to replay the journal; at that point,
4672 	 * for people who are east of GMT and who make their clock
4673 	 * tick in localtime for Windows bug-for-bug compatibility,
4674 	 * the clock is set in the future, and this will cause e2fsck
4675 	 * to complain and force a full file system check.
4676 	 */
4677 	if (!(sb->s_flags & MS_RDONLY))
4678 		es->s_wtime = cpu_to_le32(get_seconds());
4679 	if (sb->s_bdev->bd_part)
4680 		es->s_kbytes_written =
4681 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
4682 			    ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
4683 			      EXT4_SB(sb)->s_sectors_written_start) >> 1));
4684 	else
4685 		es->s_kbytes_written =
4686 			cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
4687 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
4688 		ext4_free_blocks_count_set(es,
4689 			EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
4690 				&EXT4_SB(sb)->s_freeclusters_counter)));
4691 	if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
4692 		es->s_free_inodes_count =
4693 			cpu_to_le32(percpu_counter_sum_positive(
4694 				&EXT4_SB(sb)->s_freeinodes_counter));
4695 	BUFFER_TRACE(sbh, "marking dirty");
4696 	ext4_superblock_csum_set(sb);
4697 	mark_buffer_dirty(sbh);
4698 	if (sync) {
4699 		error = sync_dirty_buffer(sbh);
4700 		if (error)
4701 			return error;
4702 
4703 		error = buffer_write_io_error(sbh);
4704 		if (error) {
4705 			ext4_msg(sb, KERN_ERR, "I/O error while writing "
4706 			       "superblock");
4707 			clear_buffer_write_io_error(sbh);
4708 			set_buffer_uptodate(sbh);
4709 		}
4710 	}
4711 	return error;
4712 }
4713 
4714 /*
4715  * Have we just finished recovery?  If so, and if we are mounting (or
4716  * remounting) the filesystem readonly, then we will end up with a
4717  * consistent fs on disk.  Record that fact.
4718  */
ext4_mark_recovery_complete(struct super_block * sb,struct ext4_super_block * es)4719 static void ext4_mark_recovery_complete(struct super_block *sb,
4720 					struct ext4_super_block *es)
4721 {
4722 	journal_t *journal = EXT4_SB(sb)->s_journal;
4723 
4724 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
4725 		BUG_ON(journal != NULL);
4726 		return;
4727 	}
4728 	jbd2_journal_lock_updates(journal);
4729 	if (jbd2_journal_flush(journal) < 0)
4730 		goto out;
4731 
4732 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
4733 	    sb->s_flags & MS_RDONLY) {
4734 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4735 		ext4_commit_super(sb, 1);
4736 	}
4737 
4738 out:
4739 	jbd2_journal_unlock_updates(journal);
4740 }
4741 
4742 /*
4743  * If we are mounting (or read-write remounting) a filesystem whose journal
4744  * has recorded an error from a previous lifetime, move that error to the
4745  * main filesystem now.
4746  */
ext4_clear_journal_err(struct super_block * sb,struct ext4_super_block * es)4747 static void ext4_clear_journal_err(struct super_block *sb,
4748 				   struct ext4_super_block *es)
4749 {
4750 	journal_t *journal;
4751 	int j_errno;
4752 	const char *errstr;
4753 
4754 	BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
4755 
4756 	journal = EXT4_SB(sb)->s_journal;
4757 
4758 	/*
4759 	 * Now check for any error status which may have been recorded in the
4760 	 * journal by a prior ext4_error() or ext4_abort()
4761 	 */
4762 
4763 	j_errno = jbd2_journal_errno(journal);
4764 	if (j_errno) {
4765 		char nbuf[16];
4766 
4767 		errstr = ext4_decode_error(sb, j_errno, nbuf);
4768 		ext4_warning(sb, "Filesystem error recorded "
4769 			     "from previous mount: %s", errstr);
4770 		ext4_warning(sb, "Marking fs in need of filesystem check.");
4771 
4772 		EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
4773 		es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
4774 		ext4_commit_super(sb, 1);
4775 
4776 		jbd2_journal_clear_err(journal);
4777 		jbd2_journal_update_sb_errno(journal);
4778 	}
4779 }
4780 
4781 /*
4782  * Force the running and committing transactions to commit,
4783  * and wait on the commit.
4784  */
ext4_force_commit(struct super_block * sb)4785 int ext4_force_commit(struct super_block *sb)
4786 {
4787 	journal_t *journal;
4788 
4789 	if (sb->s_flags & MS_RDONLY)
4790 		return 0;
4791 
4792 	journal = EXT4_SB(sb)->s_journal;
4793 	return ext4_journal_force_commit(journal);
4794 }
4795 
ext4_sync_fs(struct super_block * sb,int wait)4796 static int ext4_sync_fs(struct super_block *sb, int wait)
4797 {
4798 	int ret = 0;
4799 	tid_t target;
4800 	bool needs_barrier = false;
4801 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4802 
4803 	trace_ext4_sync_fs(sb, wait);
4804 	flush_workqueue(sbi->rsv_conversion_wq);
4805 	/*
4806 	 * Writeback quota in non-journalled quota case - journalled quota has
4807 	 * no dirty dquots
4808 	 */
4809 	dquot_writeback_dquots(sb, -1);
4810 	/*
4811 	 * Data writeback is possible w/o journal transaction, so barrier must
4812 	 * being sent at the end of the function. But we can skip it if
4813 	 * transaction_commit will do it for us.
4814 	 */
4815 	if (sbi->s_journal) {
4816 		target = jbd2_get_latest_transaction(sbi->s_journal);
4817 		if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
4818 		    !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
4819 			needs_barrier = true;
4820 
4821 		if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
4822 			if (wait)
4823 				ret = jbd2_log_wait_commit(sbi->s_journal,
4824 							   target);
4825 		}
4826 	} else if (wait && test_opt(sb, BARRIER))
4827 		needs_barrier = true;
4828 	if (needs_barrier) {
4829 		int err;
4830 		err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
4831 		if (!ret)
4832 			ret = err;
4833 	}
4834 
4835 	return ret;
4836 }
4837 
4838 /*
4839  * LVM calls this function before a (read-only) snapshot is created.  This
4840  * gives us a chance to flush the journal completely and mark the fs clean.
4841  *
4842  * Note that only this function cannot bring a filesystem to be in a clean
4843  * state independently. It relies on upper layer to stop all data & metadata
4844  * modifications.
4845  */
ext4_freeze(struct super_block * sb)4846 static int ext4_freeze(struct super_block *sb)
4847 {
4848 	int error = 0;
4849 	journal_t *journal;
4850 
4851 	if (sb->s_flags & MS_RDONLY)
4852 		return 0;
4853 
4854 	journal = EXT4_SB(sb)->s_journal;
4855 
4856 	if (journal) {
4857 		/* Now we set up the journal barrier. */
4858 		jbd2_journal_lock_updates(journal);
4859 
4860 		/*
4861 		 * Don't clear the needs_recovery flag if we failed to
4862 		 * flush the journal.
4863 		 */
4864 		error = jbd2_journal_flush(journal);
4865 		if (error < 0)
4866 			goto out;
4867 
4868 		/* Journal blocked and flushed, clear needs_recovery flag. */
4869 		EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4870 	}
4871 
4872 	error = ext4_commit_super(sb, 1);
4873 out:
4874 	if (journal)
4875 		/* we rely on upper layer to stop further updates */
4876 		jbd2_journal_unlock_updates(journal);
4877 	return error;
4878 }
4879 
4880 /*
4881  * Called by LVM after the snapshot is done.  We need to reset the RECOVER
4882  * flag here, even though the filesystem is not technically dirty yet.
4883  */
ext4_unfreeze(struct super_block * sb)4884 static int ext4_unfreeze(struct super_block *sb)
4885 {
4886 	if (sb->s_flags & MS_RDONLY)
4887 		return 0;
4888 
4889 	if (EXT4_SB(sb)->s_journal) {
4890 		/* Reset the needs_recovery flag before the fs is unlocked. */
4891 		EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
4892 	}
4893 
4894 	ext4_commit_super(sb, 1);
4895 	return 0;
4896 }
4897 
4898 /*
4899  * Structure to save mount options for ext4_remount's benefit
4900  */
4901 struct ext4_mount_options {
4902 	unsigned long s_mount_opt;
4903 	unsigned long s_mount_opt2;
4904 	kuid_t s_resuid;
4905 	kgid_t s_resgid;
4906 	unsigned long s_commit_interval;
4907 	u32 s_min_batch_time, s_max_batch_time;
4908 #ifdef CONFIG_QUOTA
4909 	int s_jquota_fmt;
4910 	char *s_qf_names[EXT4_MAXQUOTAS];
4911 #endif
4912 };
4913 
ext4_remount(struct super_block * sb,int * flags,char * data)4914 static int ext4_remount(struct super_block *sb, int *flags, char *data)
4915 {
4916 	struct ext4_super_block *es;
4917 	struct ext4_sb_info *sbi = EXT4_SB(sb);
4918 	unsigned long old_sb_flags;
4919 	struct ext4_mount_options old_opts;
4920 	int enable_quota = 0;
4921 	ext4_group_t g;
4922 	unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
4923 	int err = 0;
4924 #ifdef CONFIG_QUOTA
4925 	int i, j;
4926 #endif
4927 	char *orig_data = kstrdup(data, GFP_KERNEL);
4928 
4929 	/* Store the original options */
4930 	old_sb_flags = sb->s_flags;
4931 	old_opts.s_mount_opt = sbi->s_mount_opt;
4932 	old_opts.s_mount_opt2 = sbi->s_mount_opt2;
4933 	old_opts.s_resuid = sbi->s_resuid;
4934 	old_opts.s_resgid = sbi->s_resgid;
4935 	old_opts.s_commit_interval = sbi->s_commit_interval;
4936 	old_opts.s_min_batch_time = sbi->s_min_batch_time;
4937 	old_opts.s_max_batch_time = sbi->s_max_batch_time;
4938 #ifdef CONFIG_QUOTA
4939 	old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
4940 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
4941 		if (sbi->s_qf_names[i]) {
4942 			old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
4943 							 GFP_KERNEL);
4944 			if (!old_opts.s_qf_names[i]) {
4945 				for (j = 0; j < i; j++)
4946 					kfree(old_opts.s_qf_names[j]);
4947 				kfree(orig_data);
4948 				return -ENOMEM;
4949 			}
4950 		} else
4951 			old_opts.s_qf_names[i] = NULL;
4952 #endif
4953 	if (sbi->s_journal && sbi->s_journal->j_task->io_context)
4954 		journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
4955 
4956 	/*
4957 	 * Allow the "check" option to be passed as a remount option.
4958 	 */
4959 	if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
4960 		err = -EINVAL;
4961 		goto restore_opts;
4962 	}
4963 
4964 	if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
4965 	    test_opt(sb, JOURNAL_CHECKSUM)) {
4966 		ext4_msg(sb, KERN_ERR, "changing journal_checksum "
4967 			 "during remount not supported; ignoring");
4968 		sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
4969 	}
4970 
4971 	if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
4972 		if (test_opt2(sb, EXPLICIT_DELALLOC)) {
4973 			ext4_msg(sb, KERN_ERR, "can't mount with "
4974 				 "both data=journal and delalloc");
4975 			err = -EINVAL;
4976 			goto restore_opts;
4977 		}
4978 		if (test_opt(sb, DIOREAD_NOLOCK)) {
4979 			ext4_msg(sb, KERN_ERR, "can't mount with "
4980 				 "both data=journal and dioread_nolock");
4981 			err = -EINVAL;
4982 			goto restore_opts;
4983 		}
4984 	}
4985 
4986 	if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
4987 		ext4_abort(sb, "Abort forced by user");
4988 
4989 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
4990 		(test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
4991 
4992 	es = sbi->s_es;
4993 
4994 	if (sbi->s_journal) {
4995 		ext4_init_journal_params(sb, sbi->s_journal);
4996 		set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
4997 	}
4998 
4999 	if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
5000 		if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
5001 			err = -EROFS;
5002 			goto restore_opts;
5003 		}
5004 
5005 		if (*flags & MS_RDONLY) {
5006 			err = sync_filesystem(sb);
5007 			if (err < 0)
5008 				goto restore_opts;
5009 			err = dquot_suspend(sb, -1);
5010 			if (err < 0)
5011 				goto restore_opts;
5012 
5013 			/*
5014 			 * First of all, the unconditional stuff we have to do
5015 			 * to disable replay of the journal when we next remount
5016 			 */
5017 			sb->s_flags |= MS_RDONLY;
5018 
5019 			/*
5020 			 * OK, test if we are remounting a valid rw partition
5021 			 * readonly, and if so set the rdonly flag and then
5022 			 * mark the partition as valid again.
5023 			 */
5024 			if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
5025 			    (sbi->s_mount_state & EXT4_VALID_FS))
5026 				es->s_state = cpu_to_le16(sbi->s_mount_state);
5027 
5028 			if (sbi->s_journal)
5029 				ext4_mark_recovery_complete(sb, es);
5030 		} else {
5031 			/* Make sure we can mount this feature set readwrite */
5032 			if (!ext4_feature_set_ok(sb, 0)) {
5033 				err = -EROFS;
5034 				goto restore_opts;
5035 			}
5036 			/*
5037 			 * Make sure the group descriptor checksums
5038 			 * are sane.  If they aren't, refuse to remount r/w.
5039 			 */
5040 			for (g = 0; g < sbi->s_groups_count; g++) {
5041 				struct ext4_group_desc *gdp =
5042 					ext4_get_group_desc(sb, g, NULL);
5043 
5044 				if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
5045 					ext4_msg(sb, KERN_ERR,
5046 	       "ext4_remount: Checksum for group %u failed (%u!=%u)",
5047 		g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
5048 					       le16_to_cpu(gdp->bg_checksum));
5049 					err = -EINVAL;
5050 					goto restore_opts;
5051 				}
5052 			}
5053 
5054 			/*
5055 			 * If we have an unprocessed orphan list hanging
5056 			 * around from a previously readonly bdev mount,
5057 			 * require a full umount/remount for now.
5058 			 */
5059 			if (es->s_last_orphan) {
5060 				ext4_msg(sb, KERN_WARNING, "Couldn't "
5061 				       "remount RDWR because of unprocessed "
5062 				       "orphan inode list.  Please "
5063 				       "umount/remount instead");
5064 				err = -EINVAL;
5065 				goto restore_opts;
5066 			}
5067 
5068 			/*
5069 			 * Mounting a RDONLY partition read-write, so reread
5070 			 * and store the current valid flag.  (It may have
5071 			 * been changed by e2fsck since we originally mounted
5072 			 * the partition.)
5073 			 */
5074 			if (sbi->s_journal)
5075 				ext4_clear_journal_err(sb, es);
5076 			sbi->s_mount_state = le16_to_cpu(es->s_state);
5077 			if (!ext4_setup_super(sb, es, 0))
5078 				sb->s_flags &= ~MS_RDONLY;
5079 			if (EXT4_HAS_INCOMPAT_FEATURE(sb,
5080 						     EXT4_FEATURE_INCOMPAT_MMP))
5081 				if (ext4_multi_mount_protect(sb,
5082 						le64_to_cpu(es->s_mmp_block))) {
5083 					err = -EROFS;
5084 					goto restore_opts;
5085 				}
5086 			enable_quota = 1;
5087 		}
5088 	}
5089 
5090 	/*
5091 	 * Reinitialize lazy itable initialization thread based on
5092 	 * current settings
5093 	 */
5094 	if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
5095 		ext4_unregister_li_request(sb);
5096 	else {
5097 		ext4_group_t first_not_zeroed;
5098 		first_not_zeroed = ext4_has_uninit_itable(sb);
5099 		ext4_register_li_request(sb, first_not_zeroed);
5100 	}
5101 
5102 	ext4_setup_system_zone(sb);
5103 	if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
5104 		ext4_commit_super(sb, 1);
5105 
5106 #ifdef CONFIG_QUOTA
5107 	/* Release old quota file names */
5108 	for (i = 0; i < EXT4_MAXQUOTAS; i++)
5109 		kfree(old_opts.s_qf_names[i]);
5110 	if (enable_quota) {
5111 		if (sb_any_quota_suspended(sb))
5112 			dquot_resume(sb, -1);
5113 		else if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
5114 					EXT4_FEATURE_RO_COMPAT_QUOTA)) {
5115 			err = ext4_enable_quotas(sb);
5116 			if (err)
5117 				goto restore_opts;
5118 		}
5119 	}
5120 #endif
5121 
5122 	ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
5123 	kfree(orig_data);
5124 	return 0;
5125 
5126 restore_opts:
5127 	sb->s_flags = old_sb_flags;
5128 	sbi->s_mount_opt = old_opts.s_mount_opt;
5129 	sbi->s_mount_opt2 = old_opts.s_mount_opt2;
5130 	sbi->s_resuid = old_opts.s_resuid;
5131 	sbi->s_resgid = old_opts.s_resgid;
5132 	sbi->s_commit_interval = old_opts.s_commit_interval;
5133 	sbi->s_min_batch_time = old_opts.s_min_batch_time;
5134 	sbi->s_max_batch_time = old_opts.s_max_batch_time;
5135 #ifdef CONFIG_QUOTA
5136 	sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
5137 	for (i = 0; i < EXT4_MAXQUOTAS; i++) {
5138 		kfree(sbi->s_qf_names[i]);
5139 		sbi->s_qf_names[i] = old_opts.s_qf_names[i];
5140 	}
5141 #endif
5142 	kfree(orig_data);
5143 	return err;
5144 }
5145 
ext4_statfs(struct dentry * dentry,struct kstatfs * buf)5146 static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
5147 {
5148 	struct super_block *sb = dentry->d_sb;
5149 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5150 	struct ext4_super_block *es = sbi->s_es;
5151 	ext4_fsblk_t overhead = 0, resv_blocks;
5152 	u64 fsid;
5153 	s64 bfree;
5154 	resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
5155 
5156 	if (!test_opt(sb, MINIX_DF))
5157 		overhead = sbi->s_overhead;
5158 
5159 	buf->f_type = EXT4_SUPER_MAGIC;
5160 	buf->f_bsize = sb->s_blocksize;
5161 	buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
5162 	bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
5163 		percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
5164 	/* prevent underflow in case that few free space is available */
5165 	buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
5166 	buf->f_bavail = buf->f_bfree -
5167 			(ext4_r_blocks_count(es) + resv_blocks);
5168 	if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
5169 		buf->f_bavail = 0;
5170 	buf->f_files = le32_to_cpu(es->s_inodes_count);
5171 	buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
5172 	buf->f_namelen = EXT4_NAME_LEN;
5173 	fsid = le64_to_cpup((void *)es->s_uuid) ^
5174 	       le64_to_cpup((void *)es->s_uuid + sizeof(u64));
5175 	buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
5176 	buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
5177 
5178 	return 0;
5179 }
5180 
5181 /* Helper function for writing quotas on sync - we need to start transaction
5182  * before quota file is locked for write. Otherwise the are possible deadlocks:
5183  * Process 1                         Process 2
5184  * ext4_create()                     quota_sync()
5185  *   jbd2_journal_start()                  write_dquot()
5186  *   dquot_initialize()                         down(dqio_mutex)
5187  *     down(dqio_mutex)                    jbd2_journal_start()
5188  *
5189  */
5190 
5191 #ifdef CONFIG_QUOTA
5192 
dquot_to_inode(struct dquot * dquot)5193 static inline struct inode *dquot_to_inode(struct dquot *dquot)
5194 {
5195 	return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
5196 }
5197 
ext4_write_dquot(struct dquot * dquot)5198 static int ext4_write_dquot(struct dquot *dquot)
5199 {
5200 	int ret, err;
5201 	handle_t *handle;
5202 	struct inode *inode;
5203 
5204 	inode = dquot_to_inode(dquot);
5205 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
5206 				    EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
5207 	if (IS_ERR(handle))
5208 		return PTR_ERR(handle);
5209 	ret = dquot_commit(dquot);
5210 	err = ext4_journal_stop(handle);
5211 	if (!ret)
5212 		ret = err;
5213 	return ret;
5214 }
5215 
ext4_acquire_dquot(struct dquot * dquot)5216 static int ext4_acquire_dquot(struct dquot *dquot)
5217 {
5218 	int ret, err;
5219 	handle_t *handle;
5220 
5221 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5222 				    EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
5223 	if (IS_ERR(handle))
5224 		return PTR_ERR(handle);
5225 	ret = dquot_acquire(dquot);
5226 	err = ext4_journal_stop(handle);
5227 	if (!ret)
5228 		ret = err;
5229 	return ret;
5230 }
5231 
ext4_release_dquot(struct dquot * dquot)5232 static int ext4_release_dquot(struct dquot *dquot)
5233 {
5234 	int ret, err;
5235 	handle_t *handle;
5236 
5237 	handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
5238 				    EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
5239 	if (IS_ERR(handle)) {
5240 		/* Release dquot anyway to avoid endless cycle in dqput() */
5241 		dquot_release(dquot);
5242 		return PTR_ERR(handle);
5243 	}
5244 	ret = dquot_release(dquot);
5245 	err = ext4_journal_stop(handle);
5246 	if (!ret)
5247 		ret = err;
5248 	return ret;
5249 }
5250 
ext4_mark_dquot_dirty(struct dquot * dquot)5251 static int ext4_mark_dquot_dirty(struct dquot *dquot)
5252 {
5253 	struct super_block *sb = dquot->dq_sb;
5254 	struct ext4_sb_info *sbi = EXT4_SB(sb);
5255 
5256 	/* Are we journaling quotas? */
5257 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA) ||
5258 	    sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
5259 		dquot_mark_dquot_dirty(dquot);
5260 		return ext4_write_dquot(dquot);
5261 	} else {
5262 		return dquot_mark_dquot_dirty(dquot);
5263 	}
5264 }
5265 
ext4_write_info(struct super_block * sb,int type)5266 static int ext4_write_info(struct super_block *sb, int type)
5267 {
5268 	int ret, err;
5269 	handle_t *handle;
5270 
5271 	/* Data block + inode block */
5272 	handle = ext4_journal_start(sb->s_root->d_inode, EXT4_HT_QUOTA, 2);
5273 	if (IS_ERR(handle))
5274 		return PTR_ERR(handle);
5275 	ret = dquot_commit_info(sb, type);
5276 	err = ext4_journal_stop(handle);
5277 	if (!ret)
5278 		ret = err;
5279 	return ret;
5280 }
5281 
5282 /*
5283  * Turn on quotas during mount time - we need to find
5284  * the quota file and such...
5285  */
ext4_quota_on_mount(struct super_block * sb,int type)5286 static int ext4_quota_on_mount(struct super_block *sb, int type)
5287 {
5288 	return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
5289 					EXT4_SB(sb)->s_jquota_fmt, type);
5290 }
5291 
lockdep_set_quota_inode(struct inode * inode,int subclass)5292 static void lockdep_set_quota_inode(struct inode *inode, int subclass)
5293 {
5294 	struct ext4_inode_info *ei = EXT4_I(inode);
5295 
5296 	/* The first argument of lockdep_set_subclass has to be
5297 	 * *exactly* the same as the argument to init_rwsem() --- in
5298 	 * this case, in init_once() --- or lockdep gets unhappy
5299 	 * because the name of the lock is set using the
5300 	 * stringification of the argument to init_rwsem().
5301 	 */
5302 	(void) ei;	/* shut up clang warning if !CONFIG_LOCKDEP */
5303 	lockdep_set_subclass(&ei->i_data_sem, subclass);
5304 }
5305 
5306 /*
5307  * Standard function to be called on quota_on
5308  */
ext4_quota_on(struct super_block * sb,int type,int format_id,struct path * path)5309 static int ext4_quota_on(struct super_block *sb, int type, int format_id,
5310 			 struct path *path)
5311 {
5312 	int err;
5313 
5314 	if (!test_opt(sb, QUOTA))
5315 		return -EINVAL;
5316 
5317 	/* Quotafile not on the same filesystem? */
5318 	if (path->dentry->d_sb != sb)
5319 		return -EXDEV;
5320 	/* Journaling quota? */
5321 	if (EXT4_SB(sb)->s_qf_names[type]) {
5322 		/* Quotafile not in fs root? */
5323 		if (path->dentry->d_parent != sb->s_root)
5324 			ext4_msg(sb, KERN_WARNING,
5325 				"Quota file not on filesystem root. "
5326 				"Journaled quota will not work");
5327 	}
5328 
5329 	/*
5330 	 * When we journal data on quota file, we have to flush journal to see
5331 	 * all updates to the file when we bypass pagecache...
5332 	 */
5333 	if (EXT4_SB(sb)->s_journal &&
5334 	    ext4_should_journal_data(path->dentry->d_inode)) {
5335 		/*
5336 		 * We don't need to lock updates but journal_flush() could
5337 		 * otherwise be livelocked...
5338 		 */
5339 		jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
5340 		err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
5341 		jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
5342 		if (err)
5343 			return err;
5344 	}
5345 	lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
5346 	err = dquot_quota_on(sb, type, format_id, path);
5347 	if (err)
5348 		lockdep_set_quota_inode(path->dentry->d_inode,
5349 					     I_DATA_SEM_NORMAL);
5350 	return err;
5351 }
5352 
ext4_quota_enable(struct super_block * sb,int type,int format_id,unsigned int flags)5353 static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
5354 			     unsigned int flags)
5355 {
5356 	int err;
5357 	struct inode *qf_inode;
5358 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
5359 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5360 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
5361 	};
5362 
5363 	BUG_ON(!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA));
5364 
5365 	if (!qf_inums[type])
5366 		return -EPERM;
5367 
5368 	qf_inode = ext4_iget(sb, qf_inums[type]);
5369 	if (IS_ERR(qf_inode)) {
5370 		ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
5371 		return PTR_ERR(qf_inode);
5372 	}
5373 
5374 	/* Don't account quota for quota files to avoid recursion */
5375 	qf_inode->i_flags |= S_NOQUOTA;
5376 	lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
5377 	err = dquot_enable(qf_inode, type, format_id, flags);
5378 	iput(qf_inode);
5379 	if (err)
5380 		lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
5381 
5382 	return err;
5383 }
5384 
5385 /* Enable usage tracking for all quota types. */
ext4_enable_quotas(struct super_block * sb)5386 static int ext4_enable_quotas(struct super_block *sb)
5387 {
5388 	int type, err = 0;
5389 	unsigned long qf_inums[EXT4_MAXQUOTAS] = {
5390 		le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
5391 		le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum)
5392 	};
5393 
5394 	sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
5395 	for (type = 0; type < EXT4_MAXQUOTAS; type++) {
5396 		if (qf_inums[type]) {
5397 			err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
5398 						DQUOT_USAGE_ENABLED);
5399 			if (err) {
5400 				ext4_warning(sb,
5401 					"Failed to enable quota tracking "
5402 					"(type=%d, err=%d). Please run "
5403 					"e2fsck to fix.", type, err);
5404 				return err;
5405 			}
5406 		}
5407 	}
5408 	return 0;
5409 }
5410 
5411 /*
5412  * quota_on function that is used when QUOTA feature is set.
5413  */
ext4_quota_on_sysfile(struct super_block * sb,int type,int format_id)5414 static int ext4_quota_on_sysfile(struct super_block *sb, int type,
5415 				 int format_id)
5416 {
5417 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5418 		return -EINVAL;
5419 
5420 	/*
5421 	 * USAGE was enabled at mount time. Only need to enable LIMITS now.
5422 	 */
5423 	return ext4_quota_enable(sb, type, format_id, DQUOT_LIMITS_ENABLED);
5424 }
5425 
ext4_quota_off(struct super_block * sb,int type)5426 static int ext4_quota_off(struct super_block *sb, int type)
5427 {
5428 	struct inode *inode = sb_dqopt(sb)->files[type];
5429 	handle_t *handle;
5430 
5431 	/* Force all delayed allocation blocks to be allocated.
5432 	 * Caller already holds s_umount sem */
5433 	if (test_opt(sb, DELALLOC))
5434 		sync_filesystem(sb);
5435 
5436 	if (!inode)
5437 		goto out;
5438 
5439 	/* Update modification times of quota files when userspace can
5440 	 * start looking at them */
5441 	handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
5442 	if (IS_ERR(handle))
5443 		goto out;
5444 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
5445 	ext4_mark_inode_dirty(handle, inode);
5446 	ext4_journal_stop(handle);
5447 
5448 out:
5449 	return dquot_quota_off(sb, type);
5450 }
5451 
5452 /*
5453  * quota_off function that is used when QUOTA feature is set.
5454  */
ext4_quota_off_sysfile(struct super_block * sb,int type)5455 static int ext4_quota_off_sysfile(struct super_block *sb, int type)
5456 {
5457 	if (!EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_QUOTA))
5458 		return -EINVAL;
5459 
5460 	/* Disable only the limits. */
5461 	return dquot_disable(sb, type, DQUOT_LIMITS_ENABLED);
5462 }
5463 
5464 /* Read data from quotafile - avoid pagecache and such because we cannot afford
5465  * acquiring the locks... As quota files are never truncated and quota code
5466  * itself serializes the operations (and no one else should touch the files)
5467  * we don't have to be afraid of races */
ext4_quota_read(struct super_block * sb,int type,char * data,size_t len,loff_t off)5468 static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
5469 			       size_t len, loff_t off)
5470 {
5471 	struct inode *inode = sb_dqopt(sb)->files[type];
5472 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5473 	int offset = off & (sb->s_blocksize - 1);
5474 	int tocopy;
5475 	size_t toread;
5476 	struct buffer_head *bh;
5477 	loff_t i_size = i_size_read(inode);
5478 
5479 	if (off > i_size)
5480 		return 0;
5481 	if (off+len > i_size)
5482 		len = i_size-off;
5483 	toread = len;
5484 	while (toread > 0) {
5485 		tocopy = sb->s_blocksize - offset < toread ?
5486 				sb->s_blocksize - offset : toread;
5487 		bh = ext4_bread(NULL, inode, blk, 0);
5488 		if (IS_ERR(bh))
5489 			return PTR_ERR(bh);
5490 		if (!bh)	/* A hole? */
5491 			memset(data, 0, tocopy);
5492 		else
5493 			memcpy(data, bh->b_data+offset, tocopy);
5494 		brelse(bh);
5495 		offset = 0;
5496 		toread -= tocopy;
5497 		data += tocopy;
5498 		blk++;
5499 	}
5500 	return len;
5501 }
5502 
5503 /* Write to quotafile (we know the transaction is already started and has
5504  * enough credits) */
ext4_quota_write(struct super_block * sb,int type,const char * data,size_t len,loff_t off)5505 static ssize_t ext4_quota_write(struct super_block *sb, int type,
5506 				const char *data, size_t len, loff_t off)
5507 {
5508 	struct inode *inode = sb_dqopt(sb)->files[type];
5509 	ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
5510 	int err, offset = off & (sb->s_blocksize - 1);
5511 	struct buffer_head *bh;
5512 	handle_t *handle = journal_current_handle();
5513 
5514 	if (EXT4_SB(sb)->s_journal && !handle) {
5515 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5516 			" cancelled because transaction is not started",
5517 			(unsigned long long)off, (unsigned long long)len);
5518 		return -EIO;
5519 	}
5520 	/*
5521 	 * Since we account only one data block in transaction credits,
5522 	 * then it is impossible to cross a block boundary.
5523 	 */
5524 	if (sb->s_blocksize - offset < len) {
5525 		ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
5526 			" cancelled because not block aligned",
5527 			(unsigned long long)off, (unsigned long long)len);
5528 		return -EIO;
5529 	}
5530 
5531 	bh = ext4_bread(handle, inode, blk, 1);
5532 	if (IS_ERR(bh))
5533 		return PTR_ERR(bh);
5534 	if (!bh)
5535 		goto out;
5536 	BUFFER_TRACE(bh, "get write access");
5537 	err = ext4_journal_get_write_access(handle, bh);
5538 	if (err) {
5539 		brelse(bh);
5540 		return err;
5541 	}
5542 	lock_buffer(bh);
5543 	memcpy(bh->b_data+offset, data, len);
5544 	flush_dcache_page(bh->b_page);
5545 	unlock_buffer(bh);
5546 	err = ext4_handle_dirty_metadata(handle, NULL, bh);
5547 	brelse(bh);
5548 out:
5549 	if (inode->i_size < off + len) {
5550 		i_size_write(inode, off + len);
5551 		EXT4_I(inode)->i_disksize = inode->i_size;
5552 		ext4_mark_inode_dirty(handle, inode);
5553 	}
5554 	return len;
5555 }
5556 
5557 #endif
5558 
ext4_mount(struct file_system_type * fs_type,int flags,const char * dev_name,void * data)5559 static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
5560 		       const char *dev_name, void *data)
5561 {
5562 	return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
5563 }
5564 
5565 #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
register_as_ext2(void)5566 static inline void register_as_ext2(void)
5567 {
5568 	int err = register_filesystem(&ext2_fs_type);
5569 	if (err)
5570 		printk(KERN_WARNING
5571 		       "EXT4-fs: Unable to register as ext2 (%d)\n", err);
5572 }
5573 
unregister_as_ext2(void)5574 static inline void unregister_as_ext2(void)
5575 {
5576 	unregister_filesystem(&ext2_fs_type);
5577 }
5578 
ext2_feature_set_ok(struct super_block * sb)5579 static inline int ext2_feature_set_ok(struct super_block *sb)
5580 {
5581 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
5582 		return 0;
5583 	if (sb->s_flags & MS_RDONLY)
5584 		return 1;
5585 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
5586 		return 0;
5587 	return 1;
5588 }
5589 #else
register_as_ext2(void)5590 static inline void register_as_ext2(void) { }
unregister_as_ext2(void)5591 static inline void unregister_as_ext2(void) { }
ext2_feature_set_ok(struct super_block * sb)5592 static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
5593 #endif
5594 
5595 #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
register_as_ext3(void)5596 static inline void register_as_ext3(void)
5597 {
5598 	int err = register_filesystem(&ext3_fs_type);
5599 	if (err)
5600 		printk(KERN_WARNING
5601 		       "EXT4-fs: Unable to register as ext3 (%d)\n", err);
5602 }
5603 
unregister_as_ext3(void)5604 static inline void unregister_as_ext3(void)
5605 {
5606 	unregister_filesystem(&ext3_fs_type);
5607 }
5608 
ext3_feature_set_ok(struct super_block * sb)5609 static inline int ext3_feature_set_ok(struct super_block *sb)
5610 {
5611 	if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
5612 		return 0;
5613 	if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
5614 		return 0;
5615 	if (sb->s_flags & MS_RDONLY)
5616 		return 1;
5617 	if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
5618 		return 0;
5619 	return 1;
5620 }
5621 #else
register_as_ext3(void)5622 static inline void register_as_ext3(void) { }
unregister_as_ext3(void)5623 static inline void unregister_as_ext3(void) { }
ext3_feature_set_ok(struct super_block * sb)5624 static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
5625 #endif
5626 
5627 static struct file_system_type ext4_fs_type = {
5628 	.owner		= THIS_MODULE,
5629 	.name		= "ext4",
5630 	.mount		= ext4_mount,
5631 	.kill_sb	= kill_block_super,
5632 	.fs_flags	= FS_REQUIRES_DEV,
5633 };
5634 MODULE_ALIAS_FS("ext4");
5635 
ext4_init_feat_adverts(void)5636 static int __init ext4_init_feat_adverts(void)
5637 {
5638 	struct ext4_features *ef;
5639 	int ret = -ENOMEM;
5640 
5641 	ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
5642 	if (!ef)
5643 		goto out;
5644 
5645 	ef->f_kobj.kset = ext4_kset;
5646 	init_completion(&ef->f_kobj_unregister);
5647 	ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
5648 				   "features");
5649 	if (ret) {
5650 		kfree(ef);
5651 		goto out;
5652 	}
5653 
5654 	ext4_feat = ef;
5655 	ret = 0;
5656 out:
5657 	return ret;
5658 }
5659 
ext4_exit_feat_adverts(void)5660 static void ext4_exit_feat_adverts(void)
5661 {
5662 	kobject_put(&ext4_feat->f_kobj);
5663 	wait_for_completion(&ext4_feat->f_kobj_unregister);
5664 	kfree(ext4_feat);
5665 }
5666 
5667 /* Shared across all ext4 file systems */
5668 wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
5669 struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
5670 
ext4_init_fs(void)5671 static int __init ext4_init_fs(void)
5672 {
5673 	int i, err;
5674 
5675 	ext4_li_info = NULL;
5676 	mutex_init(&ext4_li_mtx);
5677 
5678 	/* Build-time check for flags consistency */
5679 	ext4_check_flag_values();
5680 
5681 	for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
5682 		mutex_init(&ext4__aio_mutex[i]);
5683 		init_waitqueue_head(&ext4__ioend_wq[i]);
5684 	}
5685 
5686 	err = ext4_init_es();
5687 	if (err)
5688 		return err;
5689 
5690 	err = ext4_init_pageio();
5691 	if (err)
5692 		goto out7;
5693 
5694 	err = ext4_init_system_zone();
5695 	if (err)
5696 		goto out6;
5697 	ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
5698 	if (!ext4_kset) {
5699 		err = -ENOMEM;
5700 		goto out5;
5701 	}
5702 	ext4_proc_root = proc_mkdir("fs/ext4", NULL);
5703 
5704 	err = ext4_init_feat_adverts();
5705 	if (err)
5706 		goto out4;
5707 
5708 	err = ext4_init_mballoc();
5709 	if (err)
5710 		goto out2;
5711 	else
5712 		ext4_mballoc_ready = 1;
5713 	err = init_inodecache();
5714 	if (err)
5715 		goto out1;
5716 	register_as_ext3();
5717 	register_as_ext2();
5718 	err = register_filesystem(&ext4_fs_type);
5719 	if (err)
5720 		goto out;
5721 
5722 	return 0;
5723 out:
5724 	unregister_as_ext2();
5725 	unregister_as_ext3();
5726 	destroy_inodecache();
5727 out1:
5728 	ext4_mballoc_ready = 0;
5729 	ext4_exit_mballoc();
5730 out2:
5731 	ext4_exit_feat_adverts();
5732 out4:
5733 	if (ext4_proc_root)
5734 		remove_proc_entry("fs/ext4", NULL);
5735 	kset_unregister(ext4_kset);
5736 out5:
5737 	ext4_exit_system_zone();
5738 out6:
5739 	ext4_exit_pageio();
5740 out7:
5741 	ext4_exit_es();
5742 
5743 	return err;
5744 }
5745 
ext4_exit_fs(void)5746 static void __exit ext4_exit_fs(void)
5747 {
5748 	ext4_exit_crypto();
5749 	ext4_destroy_lazyinit_thread();
5750 	unregister_as_ext2();
5751 	unregister_as_ext3();
5752 	unregister_filesystem(&ext4_fs_type);
5753 	destroy_inodecache();
5754 	ext4_exit_mballoc();
5755 	ext4_exit_feat_adverts();
5756 	remove_proc_entry("fs/ext4", NULL);
5757 	kset_unregister(ext4_kset);
5758 	ext4_exit_system_zone();
5759 	ext4_exit_pageio();
5760 	ext4_exit_es();
5761 }
5762 
5763 MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
5764 MODULE_DESCRIPTION("Fourth Extended Filesystem");
5765 MODULE_LICENSE("GPL");
5766 module_init(ext4_init_fs)
5767 module_exit(ext4_exit_fs)
5768