xref: /linux/fs/ext2/super.c (revision 20d0021394c1b070bf04b22c5bc8fdb437edd4c5)
1 /*
2  *  linux/fs/ext2/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/config.h>
20 #include <linux/module.h>
21 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/blkdev.h>
25 #include <linux/parser.h>
26 #include <linux/random.h>
27 #include <linux/buffer_head.h>
28 #include <linux/smp_lock.h>
29 #include <linux/vfs.h>
30 #include <asm/uaccess.h>
31 #include "ext2.h"
32 #include "xattr.h"
33 #include "acl.h"
34 #include "xip.h"
35 
36 static void ext2_sync_super(struct super_block *sb,
37 			    struct ext2_super_block *es);
38 static int ext2_remount (struct super_block * sb, int * flags, char * data);
39 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf);
40 
41 void ext2_error (struct super_block * sb, const char * function,
42 		 const char * fmt, ...)
43 {
44 	va_list args;
45 	struct ext2_sb_info *sbi = EXT2_SB(sb);
46 	struct ext2_super_block *es = sbi->s_es;
47 
48 	if (!(sb->s_flags & MS_RDONLY)) {
49 		sbi->s_mount_state |= EXT2_ERROR_FS;
50 		es->s_state =
51 			cpu_to_le16(le16_to_cpu(es->s_state) | EXT2_ERROR_FS);
52 		ext2_sync_super(sb, es);
53 	}
54 
55 	va_start(args, fmt);
56 	printk(KERN_CRIT "EXT2-fs error (device %s): %s: ",sb->s_id, function);
57 	vprintk(fmt, args);
58 	printk("\n");
59 	va_end(args);
60 
61 	if (test_opt(sb, ERRORS_PANIC))
62 		panic("EXT2-fs panic from previous error\n");
63 	if (test_opt(sb, ERRORS_RO)) {
64 		printk("Remounting filesystem read-only\n");
65 		sb->s_flags |= MS_RDONLY;
66 	}
67 }
68 
69 void ext2_warning (struct super_block * sb, const char * function,
70 		   const char * fmt, ...)
71 {
72 	va_list args;
73 
74 	va_start(args, fmt);
75 	printk(KERN_WARNING "EXT2-fs warning (device %s): %s: ",
76 	       sb->s_id, function);
77 	vprintk(fmt, args);
78 	printk("\n");
79 	va_end(args);
80 }
81 
82 void ext2_update_dynamic_rev(struct super_block *sb)
83 {
84 	struct ext2_super_block *es = EXT2_SB(sb)->s_es;
85 
86 	if (le32_to_cpu(es->s_rev_level) > EXT2_GOOD_OLD_REV)
87 		return;
88 
89 	ext2_warning(sb, __FUNCTION__,
90 		     "updating to rev %d because of new feature flag, "
91 		     "running e2fsck is recommended",
92 		     EXT2_DYNAMIC_REV);
93 
94 	es->s_first_ino = cpu_to_le32(EXT2_GOOD_OLD_FIRST_INO);
95 	es->s_inode_size = cpu_to_le16(EXT2_GOOD_OLD_INODE_SIZE);
96 	es->s_rev_level = cpu_to_le32(EXT2_DYNAMIC_REV);
97 	/* leave es->s_feature_*compat flags alone */
98 	/* es->s_uuid will be set by e2fsck if empty */
99 
100 	/*
101 	 * The rest of the superblock fields should be zero, and if not it
102 	 * means they are likely already in use, so leave them alone.  We
103 	 * can leave it up to e2fsck to clean up any inconsistencies there.
104 	 */
105 }
106 
107 static void ext2_put_super (struct super_block * sb)
108 {
109 	int db_count;
110 	int i;
111 	struct ext2_sb_info *sbi = EXT2_SB(sb);
112 
113 	ext2_xattr_put_super(sb);
114 	if (!(sb->s_flags & MS_RDONLY)) {
115 		struct ext2_super_block *es = sbi->s_es;
116 
117 		es->s_state = cpu_to_le16(sbi->s_mount_state);
118 		ext2_sync_super(sb, es);
119 	}
120 	db_count = sbi->s_gdb_count;
121 	for (i = 0; i < db_count; i++)
122 		if (sbi->s_group_desc[i])
123 			brelse (sbi->s_group_desc[i]);
124 	kfree(sbi->s_group_desc);
125 	kfree(sbi->s_debts);
126 	percpu_counter_destroy(&sbi->s_freeblocks_counter);
127 	percpu_counter_destroy(&sbi->s_freeinodes_counter);
128 	percpu_counter_destroy(&sbi->s_dirs_counter);
129 	brelse (sbi->s_sbh);
130 	sb->s_fs_info = NULL;
131 	kfree(sbi);
132 
133 	return;
134 }
135 
136 static kmem_cache_t * ext2_inode_cachep;
137 
138 static struct inode *ext2_alloc_inode(struct super_block *sb)
139 {
140 	struct ext2_inode_info *ei;
141 	ei = (struct ext2_inode_info *)kmem_cache_alloc(ext2_inode_cachep, SLAB_KERNEL);
142 	if (!ei)
143 		return NULL;
144 #ifdef CONFIG_EXT2_FS_POSIX_ACL
145 	ei->i_acl = EXT2_ACL_NOT_CACHED;
146 	ei->i_default_acl = EXT2_ACL_NOT_CACHED;
147 #endif
148 	ei->vfs_inode.i_version = 1;
149 	return &ei->vfs_inode;
150 }
151 
152 static void ext2_destroy_inode(struct inode *inode)
153 {
154 	kmem_cache_free(ext2_inode_cachep, EXT2_I(inode));
155 }
156 
157 static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
158 {
159 	struct ext2_inode_info *ei = (struct ext2_inode_info *) foo;
160 
161 	if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
162 	    SLAB_CTOR_CONSTRUCTOR) {
163 		rwlock_init(&ei->i_meta_lock);
164 #ifdef CONFIG_EXT2_FS_XATTR
165 		init_rwsem(&ei->xattr_sem);
166 #endif
167 		inode_init_once(&ei->vfs_inode);
168 	}
169 }
170 
171 static int init_inodecache(void)
172 {
173 	ext2_inode_cachep = kmem_cache_create("ext2_inode_cache",
174 					     sizeof(struct ext2_inode_info),
175 					     0, SLAB_RECLAIM_ACCOUNT,
176 					     init_once, NULL);
177 	if (ext2_inode_cachep == NULL)
178 		return -ENOMEM;
179 	return 0;
180 }
181 
182 static void destroy_inodecache(void)
183 {
184 	if (kmem_cache_destroy(ext2_inode_cachep))
185 		printk(KERN_INFO "ext2_inode_cache: not all structures were freed\n");
186 }
187 
188 static void ext2_clear_inode(struct inode *inode)
189 {
190 #ifdef CONFIG_EXT2_FS_POSIX_ACL
191 	struct ext2_inode_info *ei = EXT2_I(inode);
192 
193 	if (ei->i_acl && ei->i_acl != EXT2_ACL_NOT_CACHED) {
194 		posix_acl_release(ei->i_acl);
195 		ei->i_acl = EXT2_ACL_NOT_CACHED;
196 	}
197 	if (ei->i_default_acl && ei->i_default_acl != EXT2_ACL_NOT_CACHED) {
198 		posix_acl_release(ei->i_default_acl);
199 		ei->i_default_acl = EXT2_ACL_NOT_CACHED;
200 	}
201 #endif
202 }
203 
204 #ifdef CONFIG_QUOTA
205 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data, size_t len, loff_t off);
206 static ssize_t ext2_quota_write(struct super_block *sb, int type, const char *data, size_t len, loff_t off);
207 #endif
208 
209 static struct super_operations ext2_sops = {
210 	.alloc_inode	= ext2_alloc_inode,
211 	.destroy_inode	= ext2_destroy_inode,
212 	.read_inode	= ext2_read_inode,
213 	.write_inode	= ext2_write_inode,
214 	.put_inode	= ext2_put_inode,
215 	.delete_inode	= ext2_delete_inode,
216 	.put_super	= ext2_put_super,
217 	.write_super	= ext2_write_super,
218 	.statfs		= ext2_statfs,
219 	.remount_fs	= ext2_remount,
220 	.clear_inode	= ext2_clear_inode,
221 #ifdef CONFIG_QUOTA
222 	.quota_read	= ext2_quota_read,
223 	.quota_write	= ext2_quota_write,
224 #endif
225 };
226 
227 /* Yes, most of these are left as NULL!!
228  * A NULL value implies the default, which works with ext2-like file
229  * systems, but can be improved upon.
230  * Currently only get_parent is required.
231  */
232 struct dentry *ext2_get_parent(struct dentry *child);
233 static struct export_operations ext2_export_ops = {
234 	.get_parent = ext2_get_parent,
235 };
236 
237 static unsigned long get_sb_block(void **data)
238 {
239 	unsigned long 	sb_block;
240 	char 		*options = (char *) *data;
241 
242 	if (!options || strncmp(options, "sb=", 3) != 0)
243 		return 1;	/* Default location */
244 	options += 3;
245 	sb_block = simple_strtoul(options, &options, 0);
246 	if (*options && *options != ',') {
247 		printk("EXT2-fs: Invalid sb specification: %s\n",
248 		       (char *) *data);
249 		return 1;
250 	}
251 	if (*options == ',')
252 		options++;
253 	*data = (void *) options;
254 	return sb_block;
255 }
256 
257 enum {
258 	Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
259 	Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
260 	Opt_nouid32, Opt_check, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov, Opt_nobh,
261 	Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl, Opt_xip,
262 	Opt_ignore, Opt_err,
263 };
264 
265 static match_table_t tokens = {
266 	{Opt_bsd_df, "bsddf"},
267 	{Opt_minix_df, "minixdf"},
268 	{Opt_grpid, "grpid"},
269 	{Opt_grpid, "bsdgroups"},
270 	{Opt_nogrpid, "nogrpid"},
271 	{Opt_nogrpid, "sysvgroups"},
272 	{Opt_resgid, "resgid=%u"},
273 	{Opt_resuid, "resuid=%u"},
274 	{Opt_sb, "sb=%u"},
275 	{Opt_err_cont, "errors=continue"},
276 	{Opt_err_panic, "errors=panic"},
277 	{Opt_err_ro, "errors=remount-ro"},
278 	{Opt_nouid32, "nouid32"},
279 	{Opt_nocheck, "check=none"},
280 	{Opt_nocheck, "nocheck"},
281 	{Opt_check, "check"},
282 	{Opt_debug, "debug"},
283 	{Opt_oldalloc, "oldalloc"},
284 	{Opt_orlov, "orlov"},
285 	{Opt_nobh, "nobh"},
286 	{Opt_user_xattr, "user_xattr"},
287 	{Opt_nouser_xattr, "nouser_xattr"},
288 	{Opt_acl, "acl"},
289 	{Opt_noacl, "noacl"},
290 	{Opt_xip, "xip"},
291 	{Opt_ignore, "grpquota"},
292 	{Opt_ignore, "noquota"},
293 	{Opt_ignore, "quota"},
294 	{Opt_ignore, "usrquota"},
295 	{Opt_err, NULL}
296 };
297 
298 static int parse_options (char * options,
299 			  struct ext2_sb_info *sbi)
300 {
301 	char * p;
302 	substring_t args[MAX_OPT_ARGS];
303 	unsigned long kind = EXT2_MOUNT_ERRORS_CONT;
304 	int option;
305 
306 	if (!options)
307 		return 1;
308 
309 	while ((p = strsep (&options, ",")) != NULL) {
310 		int token;
311 		if (!*p)
312 			continue;
313 
314 		token = match_token(p, tokens, args);
315 		switch (token) {
316 		case Opt_bsd_df:
317 			clear_opt (sbi->s_mount_opt, MINIX_DF);
318 			break;
319 		case Opt_minix_df:
320 			set_opt (sbi->s_mount_opt, MINIX_DF);
321 			break;
322 		case Opt_grpid:
323 			set_opt (sbi->s_mount_opt, GRPID);
324 			break;
325 		case Opt_nogrpid:
326 			clear_opt (sbi->s_mount_opt, GRPID);
327 			break;
328 		case Opt_resuid:
329 			if (match_int(&args[0], &option))
330 				return 0;
331 			sbi->s_resuid = option;
332 			break;
333 		case Opt_resgid:
334 			if (match_int(&args[0], &option))
335 				return 0;
336 			sbi->s_resgid = option;
337 			break;
338 		case Opt_sb:
339 			/* handled by get_sb_block() instead of here */
340 			/* *sb_block = match_int(&args[0]); */
341 			break;
342 		case Opt_err_panic:
343 			kind = EXT2_MOUNT_ERRORS_PANIC;
344 			break;
345 		case Opt_err_ro:
346 			kind = EXT2_MOUNT_ERRORS_RO;
347 			break;
348 		case Opt_err_cont:
349 			kind = EXT2_MOUNT_ERRORS_CONT;
350 			break;
351 		case Opt_nouid32:
352 			set_opt (sbi->s_mount_opt, NO_UID32);
353 			break;
354 		case Opt_check:
355 #ifdef CONFIG_EXT2_CHECK
356 			set_opt (sbi->s_mount_opt, CHECK);
357 #else
358 			printk("EXT2 Check option not supported\n");
359 #endif
360 			break;
361 		case Opt_nocheck:
362 			clear_opt (sbi->s_mount_opt, CHECK);
363 			break;
364 		case Opt_debug:
365 			set_opt (sbi->s_mount_opt, DEBUG);
366 			break;
367 		case Opt_oldalloc:
368 			set_opt (sbi->s_mount_opt, OLDALLOC);
369 			break;
370 		case Opt_orlov:
371 			clear_opt (sbi->s_mount_opt, OLDALLOC);
372 			break;
373 		case Opt_nobh:
374 			set_opt (sbi->s_mount_opt, NOBH);
375 			break;
376 #ifdef CONFIG_EXT2_FS_XATTR
377 		case Opt_user_xattr:
378 			set_opt (sbi->s_mount_opt, XATTR_USER);
379 			break;
380 		case Opt_nouser_xattr:
381 			clear_opt (sbi->s_mount_opt, XATTR_USER);
382 			break;
383 #else
384 		case Opt_user_xattr:
385 		case Opt_nouser_xattr:
386 			printk("EXT2 (no)user_xattr options not supported\n");
387 			break;
388 #endif
389 #ifdef CONFIG_EXT2_FS_POSIX_ACL
390 		case Opt_acl:
391 			set_opt(sbi->s_mount_opt, POSIX_ACL);
392 			break;
393 		case Opt_noacl:
394 			clear_opt(sbi->s_mount_opt, POSIX_ACL);
395 			break;
396 #else
397 		case Opt_acl:
398 		case Opt_noacl:
399 			printk("EXT2 (no)acl options not supported\n");
400 			break;
401 #endif
402 		case Opt_xip:
403 #ifdef CONFIG_EXT2_FS_XIP
404 			set_opt (sbi->s_mount_opt, XIP);
405 #else
406 			printk("EXT2 xip option not supported\n");
407 #endif
408 			break;
409 		case Opt_ignore:
410 			break;
411 		default:
412 			return 0;
413 		}
414 	}
415 	sbi->s_mount_opt |= kind;
416 	return 1;
417 }
418 
419 static int ext2_setup_super (struct super_block * sb,
420 			      struct ext2_super_block * es,
421 			      int read_only)
422 {
423 	int res = 0;
424 	struct ext2_sb_info *sbi = EXT2_SB(sb);
425 
426 	if (le32_to_cpu(es->s_rev_level) > EXT2_MAX_SUPP_REV) {
427 		printk ("EXT2-fs warning: revision level too high, "
428 			"forcing read-only mode\n");
429 		res = MS_RDONLY;
430 	}
431 	if (read_only)
432 		return res;
433 	if (!(sbi->s_mount_state & EXT2_VALID_FS))
434 		printk ("EXT2-fs warning: mounting unchecked fs, "
435 			"running e2fsck is recommended\n");
436 	else if ((sbi->s_mount_state & EXT2_ERROR_FS))
437 		printk ("EXT2-fs warning: mounting fs with errors, "
438 			"running e2fsck is recommended\n");
439 	else if ((__s16) le16_to_cpu(es->s_max_mnt_count) >= 0 &&
440 		 le16_to_cpu(es->s_mnt_count) >=
441 		 (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
442 		printk ("EXT2-fs warning: maximal mount count reached, "
443 			"running e2fsck is recommended\n");
444 	else if (le32_to_cpu(es->s_checkinterval) &&
445 		(le32_to_cpu(es->s_lastcheck) + le32_to_cpu(es->s_checkinterval) <= get_seconds()))
446 		printk ("EXT2-fs warning: checktime reached, "
447 			"running e2fsck is recommended\n");
448 	if (!le16_to_cpu(es->s_max_mnt_count))
449 		es->s_max_mnt_count = cpu_to_le16(EXT2_DFL_MAX_MNT_COUNT);
450 	es->s_mnt_count=cpu_to_le16(le16_to_cpu(es->s_mnt_count) + 1);
451 	ext2_write_super(sb);
452 	if (test_opt (sb, DEBUG))
453 		printk ("[EXT II FS %s, %s, bs=%lu, fs=%lu, gc=%lu, "
454 			"bpg=%lu, ipg=%lu, mo=%04lx]\n",
455 			EXT2FS_VERSION, EXT2FS_DATE, sb->s_blocksize,
456 			sbi->s_frag_size,
457 			sbi->s_groups_count,
458 			EXT2_BLOCKS_PER_GROUP(sb),
459 			EXT2_INODES_PER_GROUP(sb),
460 			sbi->s_mount_opt);
461 #ifdef CONFIG_EXT2_CHECK
462 	if (test_opt (sb, CHECK)) {
463 		ext2_check_blocks_bitmap (sb);
464 		ext2_check_inodes_bitmap (sb);
465 	}
466 #endif
467 	return res;
468 }
469 
470 static int ext2_check_descriptors (struct super_block * sb)
471 {
472 	int i;
473 	int desc_block = 0;
474 	struct ext2_sb_info *sbi = EXT2_SB(sb);
475 	unsigned long block = le32_to_cpu(sbi->s_es->s_first_data_block);
476 	struct ext2_group_desc * gdp = NULL;
477 
478 	ext2_debug ("Checking group descriptors");
479 
480 	for (i = 0; i < sbi->s_groups_count; i++)
481 	{
482 		if ((i % EXT2_DESC_PER_BLOCK(sb)) == 0)
483 			gdp = (struct ext2_group_desc *) sbi->s_group_desc[desc_block++]->b_data;
484 		if (le32_to_cpu(gdp->bg_block_bitmap) < block ||
485 		    le32_to_cpu(gdp->bg_block_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
486 		{
487 			ext2_error (sb, "ext2_check_descriptors",
488 				    "Block bitmap for group %d"
489 				    " not in group (block %lu)!",
490 				    i, (unsigned long) le32_to_cpu(gdp->bg_block_bitmap));
491 			return 0;
492 		}
493 		if (le32_to_cpu(gdp->bg_inode_bitmap) < block ||
494 		    le32_to_cpu(gdp->bg_inode_bitmap) >= block + EXT2_BLOCKS_PER_GROUP(sb))
495 		{
496 			ext2_error (sb, "ext2_check_descriptors",
497 				    "Inode bitmap for group %d"
498 				    " not in group (block %lu)!",
499 				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_bitmap));
500 			return 0;
501 		}
502 		if (le32_to_cpu(gdp->bg_inode_table) < block ||
503 		    le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group >=
504 		    block + EXT2_BLOCKS_PER_GROUP(sb))
505 		{
506 			ext2_error (sb, "ext2_check_descriptors",
507 				    "Inode table for group %d"
508 				    " not in group (block %lu)!",
509 				    i, (unsigned long) le32_to_cpu(gdp->bg_inode_table));
510 			return 0;
511 		}
512 		block += EXT2_BLOCKS_PER_GROUP(sb);
513 		gdp++;
514 	}
515 	return 1;
516 }
517 
518 #define log2(n) ffz(~(n))
519 
520 /*
521  * Maximal file size.  There is a direct, and {,double-,triple-}indirect
522  * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
523  * We need to be 1 filesystem block less than the 2^32 sector limit.
524  */
525 static loff_t ext2_max_size(int bits)
526 {
527 	loff_t res = EXT2_NDIR_BLOCKS;
528 	/* This constant is calculated to be the largest file size for a
529 	 * dense, 4k-blocksize file such that the total number of
530 	 * sectors in the file, including data and all indirect blocks,
531 	 * does not exceed 2^32. */
532 	const loff_t upper_limit = 0x1ff7fffd000LL;
533 
534 	res += 1LL << (bits-2);
535 	res += 1LL << (2*(bits-2));
536 	res += 1LL << (3*(bits-2));
537 	res <<= bits;
538 	if (res > upper_limit)
539 		res = upper_limit;
540 	return res;
541 }
542 
543 static unsigned long descriptor_loc(struct super_block *sb,
544 				    unsigned long logic_sb_block,
545 				    int nr)
546 {
547 	struct ext2_sb_info *sbi = EXT2_SB(sb);
548 	unsigned long bg, first_data_block, first_meta_bg;
549 	int has_super = 0;
550 
551 	first_data_block = le32_to_cpu(sbi->s_es->s_first_data_block);
552 	first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
553 
554 	if (!EXT2_HAS_INCOMPAT_FEATURE(sb, EXT2_FEATURE_INCOMPAT_META_BG) ||
555 	    nr < first_meta_bg)
556 		return (logic_sb_block + nr + 1);
557 	bg = sbi->s_desc_per_block * nr;
558 	if (ext2_bg_has_super(sb, bg))
559 		has_super = 1;
560 	return (first_data_block + has_super + (bg * sbi->s_blocks_per_group));
561 }
562 
563 static int ext2_fill_super(struct super_block *sb, void *data, int silent)
564 {
565 	struct buffer_head * bh;
566 	struct ext2_sb_info * sbi;
567 	struct ext2_super_block * es;
568 	struct inode *root;
569 	unsigned long block;
570 	unsigned long sb_block = get_sb_block(&data);
571 	unsigned long logic_sb_block;
572 	unsigned long offset = 0;
573 	unsigned long def_mount_opts;
574 	int blocksize = BLOCK_SIZE;
575 	int db_count;
576 	int i, j;
577 	__le32 features;
578 
579 	sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
580 	if (!sbi)
581 		return -ENOMEM;
582 	sb->s_fs_info = sbi;
583 	memset(sbi, 0, sizeof(*sbi));
584 
585 	/*
586 	 * See what the current blocksize for the device is, and
587 	 * use that as the blocksize.  Otherwise (or if the blocksize
588 	 * is smaller than the default) use the default.
589 	 * This is important for devices that have a hardware
590 	 * sectorsize that is larger than the default.
591 	 */
592 	blocksize = sb_min_blocksize(sb, BLOCK_SIZE);
593 	if (!blocksize) {
594 		printk ("EXT2-fs: unable to set blocksize\n");
595 		goto failed_sbi;
596 	}
597 
598 	/*
599 	 * If the superblock doesn't start on a hardware sector boundary,
600 	 * calculate the offset.
601 	 */
602 	if (blocksize != BLOCK_SIZE) {
603 		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
604 		offset = (sb_block*BLOCK_SIZE) % blocksize;
605 	} else {
606 		logic_sb_block = sb_block;
607 	}
608 
609 	if (!(bh = sb_bread(sb, logic_sb_block))) {
610 		printk ("EXT2-fs: unable to read superblock\n");
611 		goto failed_sbi;
612 	}
613 	/*
614 	 * Note: s_es must be initialized as soon as possible because
615 	 *       some ext2 macro-instructions depend on its value
616 	 */
617 	es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
618 	sbi->s_es = es;
619 	sb->s_magic = le16_to_cpu(es->s_magic);
620 
621 	if (sb->s_magic != EXT2_SUPER_MAGIC)
622 		goto cantfind_ext2;
623 
624 	/* Set defaults before we parse the mount options */
625 	def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
626 	if (def_mount_opts & EXT2_DEFM_DEBUG)
627 		set_opt(sbi->s_mount_opt, DEBUG);
628 	if (def_mount_opts & EXT2_DEFM_BSDGROUPS)
629 		set_opt(sbi->s_mount_opt, GRPID);
630 	if (def_mount_opts & EXT2_DEFM_UID16)
631 		set_opt(sbi->s_mount_opt, NO_UID32);
632 	if (def_mount_opts & EXT2_DEFM_XATTR_USER)
633 		set_opt(sbi->s_mount_opt, XATTR_USER);
634 	if (def_mount_opts & EXT2_DEFM_ACL)
635 		set_opt(sbi->s_mount_opt, POSIX_ACL);
636 
637 	if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_PANIC)
638 		set_opt(sbi->s_mount_opt, ERRORS_PANIC);
639 	else if (le16_to_cpu(sbi->s_es->s_errors) == EXT2_ERRORS_RO)
640 		set_opt(sbi->s_mount_opt, ERRORS_RO);
641 
642 	sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
643 	sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
644 
645 	if (!parse_options ((char *) data, sbi))
646 		goto failed_mount;
647 
648 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
649 		((EXT2_SB(sb)->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ?
650 		 MS_POSIXACL : 0);
651 
652 	ext2_xip_verify_sb(sb); /* see if bdev supports xip, unset
653 				    EXT2_MOUNT_XIP if not */
654 
655 	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV &&
656 	    (EXT2_HAS_COMPAT_FEATURE(sb, ~0U) ||
657 	     EXT2_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
658 	     EXT2_HAS_INCOMPAT_FEATURE(sb, ~0U)))
659 		printk("EXT2-fs warning: feature flags set on rev 0 fs, "
660 		       "running e2fsck is recommended\n");
661 	/*
662 	 * Check feature flags regardless of the revision level, since we
663 	 * previously didn't change the revision level when setting the flags,
664 	 * so there is a chance incompat flags are set on a rev 0 filesystem.
665 	 */
666 	features = EXT2_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP);
667 	if (features) {
668 		printk("EXT2-fs: %s: couldn't mount because of "
669 		       "unsupported optional features (%x).\n",
670 		       sb->s_id, le32_to_cpu(features));
671 		goto failed_mount;
672 	}
673 	if (!(sb->s_flags & MS_RDONLY) &&
674 	    (features = EXT2_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))){
675 		printk("EXT2-fs: %s: couldn't mount RDWR because of "
676 		       "unsupported optional features (%x).\n",
677 		       sb->s_id, le32_to_cpu(features));
678 		goto failed_mount;
679 	}
680 
681 	blocksize = BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
682 
683 	if ((ext2_use_xip(sb)) && ((blocksize != PAGE_SIZE) ||
684 				  (sb->s_blocksize != blocksize))) {
685 		if (!silent)
686 			printk("XIP: Unsupported blocksize\n");
687 		goto failed_mount;
688 	}
689 
690 	/* If the blocksize doesn't match, re-read the thing.. */
691 	if (sb->s_blocksize != blocksize) {
692 		brelse(bh);
693 
694 		if (!sb_set_blocksize(sb, blocksize)) {
695 			printk(KERN_ERR "EXT2-fs: blocksize too small for device.\n");
696 			goto failed_sbi;
697 		}
698 
699 		logic_sb_block = (sb_block*BLOCK_SIZE) / blocksize;
700 		offset = (sb_block*BLOCK_SIZE) % blocksize;
701 		bh = sb_bread(sb, logic_sb_block);
702 		if(!bh) {
703 			printk("EXT2-fs: Couldn't read superblock on "
704 			       "2nd try.\n");
705 			goto failed_sbi;
706 		}
707 		es = (struct ext2_super_block *) (((char *)bh->b_data) + offset);
708 		sbi->s_es = es;
709 		if (es->s_magic != cpu_to_le16(EXT2_SUPER_MAGIC)) {
710 			printk ("EXT2-fs: Magic mismatch, very weird !\n");
711 			goto failed_mount;
712 		}
713 	}
714 
715 	sb->s_maxbytes = ext2_max_size(sb->s_blocksize_bits);
716 
717 	if (le32_to_cpu(es->s_rev_level) == EXT2_GOOD_OLD_REV) {
718 		sbi->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
719 		sbi->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
720 	} else {
721 		sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
722 		sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
723 		if ((sbi->s_inode_size < EXT2_GOOD_OLD_INODE_SIZE) ||
724 		    (sbi->s_inode_size & (sbi->s_inode_size - 1)) ||
725 		    (sbi->s_inode_size > blocksize)) {
726 			printk ("EXT2-fs: unsupported inode size: %d\n",
727 				sbi->s_inode_size);
728 			goto failed_mount;
729 		}
730 	}
731 
732 	sbi->s_frag_size = EXT2_MIN_FRAG_SIZE <<
733 				   le32_to_cpu(es->s_log_frag_size);
734 	if (sbi->s_frag_size == 0)
735 		goto cantfind_ext2;
736 	sbi->s_frags_per_block = sb->s_blocksize / sbi->s_frag_size;
737 
738 	sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
739 	sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
740 	sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
741 
742 	if (EXT2_INODE_SIZE(sb) == 0)
743 		goto cantfind_ext2;
744 	sbi->s_inodes_per_block = sb->s_blocksize / EXT2_INODE_SIZE(sb);
745 	if (sbi->s_inodes_per_block == 0)
746 		goto cantfind_ext2;
747 	sbi->s_itb_per_group = sbi->s_inodes_per_group /
748 					sbi->s_inodes_per_block;
749 	sbi->s_desc_per_block = sb->s_blocksize /
750 					sizeof (struct ext2_group_desc);
751 	sbi->s_sbh = bh;
752 	sbi->s_mount_state = le16_to_cpu(es->s_state);
753 	sbi->s_addr_per_block_bits =
754 		log2 (EXT2_ADDR_PER_BLOCK(sb));
755 	sbi->s_desc_per_block_bits =
756 		log2 (EXT2_DESC_PER_BLOCK(sb));
757 
758 	if (sb->s_magic != EXT2_SUPER_MAGIC)
759 		goto cantfind_ext2;
760 
761 	if (sb->s_blocksize != bh->b_size) {
762 		if (!silent)
763 			printk ("VFS: Unsupported blocksize on dev "
764 				"%s.\n", sb->s_id);
765 		goto failed_mount;
766 	}
767 
768 	if (sb->s_blocksize != sbi->s_frag_size) {
769 		printk ("EXT2-fs: fragsize %lu != blocksize %lu (not supported yet)\n",
770 			sbi->s_frag_size, sb->s_blocksize);
771 		goto failed_mount;
772 	}
773 
774 	if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
775 		printk ("EXT2-fs: #blocks per group too big: %lu\n",
776 			sbi->s_blocks_per_group);
777 		goto failed_mount;
778 	}
779 	if (sbi->s_frags_per_group > sb->s_blocksize * 8) {
780 		printk ("EXT2-fs: #fragments per group too big: %lu\n",
781 			sbi->s_frags_per_group);
782 		goto failed_mount;
783 	}
784 	if (sbi->s_inodes_per_group > sb->s_blocksize * 8) {
785 		printk ("EXT2-fs: #inodes per group too big: %lu\n",
786 			sbi->s_inodes_per_group);
787 		goto failed_mount;
788 	}
789 
790 	if (EXT2_BLOCKS_PER_GROUP(sb) == 0)
791 		goto cantfind_ext2;
792 	sbi->s_groups_count = (le32_to_cpu(es->s_blocks_count) -
793 				        le32_to_cpu(es->s_first_data_block) +
794 				       EXT2_BLOCKS_PER_GROUP(sb) - 1) /
795 				       EXT2_BLOCKS_PER_GROUP(sb);
796 	db_count = (sbi->s_groups_count + EXT2_DESC_PER_BLOCK(sb) - 1) /
797 		   EXT2_DESC_PER_BLOCK(sb);
798 	sbi->s_group_desc = kmalloc (db_count * sizeof (struct buffer_head *), GFP_KERNEL);
799 	if (sbi->s_group_desc == NULL) {
800 		printk ("EXT2-fs: not enough memory\n");
801 		goto failed_mount;
802 	}
803 	percpu_counter_init(&sbi->s_freeblocks_counter);
804 	percpu_counter_init(&sbi->s_freeinodes_counter);
805 	percpu_counter_init(&sbi->s_dirs_counter);
806 	bgl_lock_init(&sbi->s_blockgroup_lock);
807 	sbi->s_debts = kmalloc(sbi->s_groups_count * sizeof(*sbi->s_debts),
808 			       GFP_KERNEL);
809 	if (!sbi->s_debts) {
810 		printk ("EXT2-fs: not enough memory\n");
811 		goto failed_mount_group_desc;
812 	}
813 	memset(sbi->s_debts, 0, sbi->s_groups_count * sizeof(*sbi->s_debts));
814 	for (i = 0; i < db_count; i++) {
815 		block = descriptor_loc(sb, logic_sb_block, i);
816 		sbi->s_group_desc[i] = sb_bread(sb, block);
817 		if (!sbi->s_group_desc[i]) {
818 			for (j = 0; j < i; j++)
819 				brelse (sbi->s_group_desc[j]);
820 			printk ("EXT2-fs: unable to read group descriptors\n");
821 			goto failed_mount_group_desc;
822 		}
823 	}
824 	if (!ext2_check_descriptors (sb)) {
825 		printk ("EXT2-fs: group descriptors corrupted!\n");
826 		db_count = i;
827 		goto failed_mount2;
828 	}
829 	sbi->s_gdb_count = db_count;
830 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
831 	spin_lock_init(&sbi->s_next_gen_lock);
832 	/*
833 	 * set up enough so that it can read an inode
834 	 */
835 	sb->s_op = &ext2_sops;
836 	sb->s_export_op = &ext2_export_ops;
837 	sb->s_xattr = ext2_xattr_handlers;
838 	root = iget(sb, EXT2_ROOT_INO);
839 	sb->s_root = d_alloc_root(root);
840 	if (!sb->s_root) {
841 		iput(root);
842 		printk(KERN_ERR "EXT2-fs: get root inode failed\n");
843 		goto failed_mount2;
844 	}
845 	if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
846 		dput(sb->s_root);
847 		sb->s_root = NULL;
848 		printk(KERN_ERR "EXT2-fs: corrupt root inode, run e2fsck\n");
849 		goto failed_mount2;
850 	}
851 	if (EXT2_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL))
852 		ext2_warning(sb, __FUNCTION__,
853 			"mounting ext3 filesystem as ext2\n");
854 	ext2_setup_super (sb, es, sb->s_flags & MS_RDONLY);
855 	percpu_counter_mod(&sbi->s_freeblocks_counter,
856 				ext2_count_free_blocks(sb));
857 	percpu_counter_mod(&sbi->s_freeinodes_counter,
858 				ext2_count_free_inodes(sb));
859 	percpu_counter_mod(&sbi->s_dirs_counter,
860 				ext2_count_dirs(sb));
861 	return 0;
862 
863 cantfind_ext2:
864 	if (!silent)
865 		printk("VFS: Can't find an ext2 filesystem on dev %s.\n",
866 		       sb->s_id);
867 	goto failed_mount;
868 
869 failed_mount2:
870 	for (i = 0; i < db_count; i++)
871 		brelse(sbi->s_group_desc[i]);
872 failed_mount_group_desc:
873 	kfree(sbi->s_group_desc);
874 	kfree(sbi->s_debts);
875 failed_mount:
876 	brelse(bh);
877 failed_sbi:
878 	sb->s_fs_info = NULL;
879 	kfree(sbi);
880 	return -EINVAL;
881 }
882 
883 static void ext2_commit_super (struct super_block * sb,
884 			       struct ext2_super_block * es)
885 {
886 	es->s_wtime = cpu_to_le32(get_seconds());
887 	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
888 	sb->s_dirt = 0;
889 }
890 
891 static void ext2_sync_super(struct super_block *sb, struct ext2_super_block *es)
892 {
893 	es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
894 	es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
895 	es->s_wtime = cpu_to_le32(get_seconds());
896 	mark_buffer_dirty(EXT2_SB(sb)->s_sbh);
897 	sync_dirty_buffer(EXT2_SB(sb)->s_sbh);
898 	sb->s_dirt = 0;
899 }
900 
901 /*
902  * In the second extended file system, it is not necessary to
903  * write the super block since we use a mapping of the
904  * disk super block in a buffer.
905  *
906  * However, this function is still used to set the fs valid
907  * flags to 0.  We need to set this flag to 0 since the fs
908  * may have been checked while mounted and e2fsck may have
909  * set s_state to EXT2_VALID_FS after some corrections.
910  */
911 
912 void ext2_write_super (struct super_block * sb)
913 {
914 	struct ext2_super_block * es;
915 	lock_kernel();
916 	if (!(sb->s_flags & MS_RDONLY)) {
917 		es = EXT2_SB(sb)->s_es;
918 
919 		if (le16_to_cpu(es->s_state) & EXT2_VALID_FS) {
920 			ext2_debug ("setting valid to 0\n");
921 			es->s_state = cpu_to_le16(le16_to_cpu(es->s_state) &
922 						  ~EXT2_VALID_FS);
923 			es->s_free_blocks_count = cpu_to_le32(ext2_count_free_blocks(sb));
924 			es->s_free_inodes_count = cpu_to_le32(ext2_count_free_inodes(sb));
925 			es->s_mtime = cpu_to_le32(get_seconds());
926 			ext2_sync_super(sb, es);
927 		} else
928 			ext2_commit_super (sb, es);
929 	}
930 	sb->s_dirt = 0;
931 	unlock_kernel();
932 }
933 
934 static int ext2_remount (struct super_block * sb, int * flags, char * data)
935 {
936 	struct ext2_sb_info * sbi = EXT2_SB(sb);
937 	struct ext2_super_block * es;
938 	unsigned long old_mount_opt = sbi->s_mount_opt;
939 	struct ext2_mount_options old_opts;
940 	unsigned long old_sb_flags;
941 	int err;
942 
943 	/* Store the old options */
944 	old_sb_flags = sb->s_flags;
945 	old_opts.s_mount_opt = sbi->s_mount_opt;
946 	old_opts.s_resuid = sbi->s_resuid;
947 	old_opts.s_resgid = sbi->s_resgid;
948 
949 	/*
950 	 * Allow the "check" option to be passed as a remount option.
951 	 */
952 	if (!parse_options (data, sbi)) {
953 		err = -EINVAL;
954 		goto restore_opts;
955 	}
956 
957 	sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
958 		((sbi->s_mount_opt & EXT2_MOUNT_POSIX_ACL) ? MS_POSIXACL : 0);
959 
960 	es = sbi->s_es;
961 	if (((sbi->s_mount_opt & EXT2_MOUNT_XIP) !=
962 	    (old_mount_opt & EXT2_MOUNT_XIP)) &&
963 	    invalidate_inodes(sb))
964 		ext2_warning(sb, __FUNCTION__, "busy inodes while remounting "\
965 			     "xip remain in cache (no functional problem)");
966 	if ((*flags & MS_RDONLY) == (sb->s_flags & MS_RDONLY))
967 		return 0;
968 	if (*flags & MS_RDONLY) {
969 		if (le16_to_cpu(es->s_state) & EXT2_VALID_FS ||
970 		    !(sbi->s_mount_state & EXT2_VALID_FS))
971 			return 0;
972 		/*
973 		 * OK, we are remounting a valid rw partition rdonly, so set
974 		 * the rdonly flag and then mark the partition as valid again.
975 		 */
976 		es->s_state = cpu_to_le16(sbi->s_mount_state);
977 		es->s_mtime = cpu_to_le32(get_seconds());
978 	} else {
979 		__le32 ret = EXT2_HAS_RO_COMPAT_FEATURE(sb,
980 					       ~EXT2_FEATURE_RO_COMPAT_SUPP);
981 		if (ret) {
982 			printk("EXT2-fs: %s: couldn't remount RDWR because of "
983 			       "unsupported optional features (%x).\n",
984 			       sb->s_id, le32_to_cpu(ret));
985 			err = -EROFS;
986 			goto restore_opts;
987 		}
988 		/*
989 		 * Mounting a RDONLY partition read-write, so reread and
990 		 * store the current valid flag.  (It may have been changed
991 		 * by e2fsck since we originally mounted the partition.)
992 		 */
993 		sbi->s_mount_state = le16_to_cpu(es->s_state);
994 		if (!ext2_setup_super (sb, es, 0))
995 			sb->s_flags &= ~MS_RDONLY;
996 	}
997 	ext2_sync_super(sb, es);
998 	return 0;
999 restore_opts:
1000 	sbi->s_mount_opt = old_opts.s_mount_opt;
1001 	sbi->s_resuid = old_opts.s_resuid;
1002 	sbi->s_resgid = old_opts.s_resgid;
1003 	sb->s_flags = old_sb_flags;
1004 	return err;
1005 }
1006 
1007 static int ext2_statfs (struct super_block * sb, struct kstatfs * buf)
1008 {
1009 	struct ext2_sb_info *sbi = EXT2_SB(sb);
1010 	unsigned long overhead;
1011 	int i;
1012 
1013 	if (test_opt (sb, MINIX_DF))
1014 		overhead = 0;
1015 	else {
1016 		/*
1017 		 * Compute the overhead (FS structures)
1018 		 */
1019 
1020 		/*
1021 		 * All of the blocks before first_data_block are
1022 		 * overhead
1023 		 */
1024 		overhead = le32_to_cpu(sbi->s_es->s_first_data_block);
1025 
1026 		/*
1027 		 * Add the overhead attributed to the superblock and
1028 		 * block group descriptors.  If the sparse superblocks
1029 		 * feature is turned on, then not all groups have this.
1030 		 */
1031 		for (i = 0; i < sbi->s_groups_count; i++)
1032 			overhead += ext2_bg_has_super(sb, i) +
1033 				ext2_bg_num_gdb(sb, i);
1034 
1035 		/*
1036 		 * Every block group has an inode bitmap, a block
1037 		 * bitmap, and an inode table.
1038 		 */
1039 		overhead += (sbi->s_groups_count *
1040 			     (2 + sbi->s_itb_per_group));
1041 	}
1042 
1043 	buf->f_type = EXT2_SUPER_MAGIC;
1044 	buf->f_bsize = sb->s_blocksize;
1045 	buf->f_blocks = le32_to_cpu(sbi->s_es->s_blocks_count) - overhead;
1046 	buf->f_bfree = ext2_count_free_blocks(sb);
1047 	buf->f_bavail = buf->f_bfree - le32_to_cpu(sbi->s_es->s_r_blocks_count);
1048 	if (buf->f_bfree < le32_to_cpu(sbi->s_es->s_r_blocks_count))
1049 		buf->f_bavail = 0;
1050 	buf->f_files = le32_to_cpu(sbi->s_es->s_inodes_count);
1051 	buf->f_ffree = ext2_count_free_inodes (sb);
1052 	buf->f_namelen = EXT2_NAME_LEN;
1053 	return 0;
1054 }
1055 
1056 static struct super_block *ext2_get_sb(struct file_system_type *fs_type,
1057 	int flags, const char *dev_name, void *data)
1058 {
1059 	return get_sb_bdev(fs_type, flags, dev_name, data, ext2_fill_super);
1060 }
1061 
1062 #ifdef CONFIG_QUOTA
1063 
1064 /* Read data from quotafile - avoid pagecache and such because we cannot afford
1065  * acquiring the locks... As quota files are never truncated and quota code
1066  * itself serializes the operations (and noone else should touch the files)
1067  * we don't have to be afraid of races */
1068 static ssize_t ext2_quota_read(struct super_block *sb, int type, char *data,
1069 			       size_t len, loff_t off)
1070 {
1071 	struct inode *inode = sb_dqopt(sb)->files[type];
1072 	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1073 	int err = 0;
1074 	int offset = off & (sb->s_blocksize - 1);
1075 	int tocopy;
1076 	size_t toread;
1077 	struct buffer_head tmp_bh;
1078 	struct buffer_head *bh;
1079 	loff_t i_size = i_size_read(inode);
1080 
1081 	if (off > i_size)
1082 		return 0;
1083 	if (off+len > i_size)
1084 		len = i_size-off;
1085 	toread = len;
1086 	while (toread > 0) {
1087 		tocopy = sb->s_blocksize - offset < toread ?
1088 				sb->s_blocksize - offset : toread;
1089 
1090 		tmp_bh.b_state = 0;
1091 		err = ext2_get_block(inode, blk, &tmp_bh, 0);
1092 		if (err)
1093 			return err;
1094 		if (!buffer_mapped(&tmp_bh))	/* A hole? */
1095 			memset(data, 0, tocopy);
1096 		else {
1097 			bh = sb_bread(sb, tmp_bh.b_blocknr);
1098 			if (!bh)
1099 				return -EIO;
1100 			memcpy(data, bh->b_data+offset, tocopy);
1101 			brelse(bh);
1102 		}
1103 		offset = 0;
1104 		toread -= tocopy;
1105 		data += tocopy;
1106 		blk++;
1107 	}
1108 	return len;
1109 }
1110 
1111 /* Write to quotafile */
1112 static ssize_t ext2_quota_write(struct super_block *sb, int type,
1113 				const char *data, size_t len, loff_t off)
1114 {
1115 	struct inode *inode = sb_dqopt(sb)->files[type];
1116 	sector_t blk = off >> EXT2_BLOCK_SIZE_BITS(sb);
1117 	int err = 0;
1118 	int offset = off & (sb->s_blocksize - 1);
1119 	int tocopy;
1120 	size_t towrite = len;
1121 	struct buffer_head tmp_bh;
1122 	struct buffer_head *bh;
1123 
1124 	down(&inode->i_sem);
1125 	while (towrite > 0) {
1126 		tocopy = sb->s_blocksize - offset < towrite ?
1127 				sb->s_blocksize - offset : towrite;
1128 
1129 		tmp_bh.b_state = 0;
1130 		err = ext2_get_block(inode, blk, &tmp_bh, 1);
1131 		if (err)
1132 			goto out;
1133 		if (offset || tocopy != EXT2_BLOCK_SIZE(sb))
1134 			bh = sb_bread(sb, tmp_bh.b_blocknr);
1135 		else
1136 			bh = sb_getblk(sb, tmp_bh.b_blocknr);
1137 		if (!bh) {
1138 			err = -EIO;
1139 			goto out;
1140 		}
1141 		lock_buffer(bh);
1142 		memcpy(bh->b_data+offset, data, tocopy);
1143 		flush_dcache_page(bh->b_page);
1144 		set_buffer_uptodate(bh);
1145 		mark_buffer_dirty(bh);
1146 		unlock_buffer(bh);
1147 		brelse(bh);
1148 		offset = 0;
1149 		towrite -= tocopy;
1150 		data += tocopy;
1151 		blk++;
1152 	}
1153 out:
1154 	if (len == towrite)
1155 		return err;
1156 	if (inode->i_size < off+len-towrite)
1157 		i_size_write(inode, off+len-towrite);
1158 	inode->i_version++;
1159 	inode->i_mtime = inode->i_ctime = CURRENT_TIME;
1160 	mark_inode_dirty(inode);
1161 	up(&inode->i_sem);
1162 	return len - towrite;
1163 }
1164 
1165 #endif
1166 
1167 static struct file_system_type ext2_fs_type = {
1168 	.owner		= THIS_MODULE,
1169 	.name		= "ext2",
1170 	.get_sb		= ext2_get_sb,
1171 	.kill_sb	= kill_block_super,
1172 	.fs_flags	= FS_REQUIRES_DEV,
1173 };
1174 
1175 static int __init init_ext2_fs(void)
1176 {
1177 	int err = init_ext2_xattr();
1178 	if (err)
1179 		return err;
1180 	err = init_inodecache();
1181 	if (err)
1182 		goto out1;
1183         err = register_filesystem(&ext2_fs_type);
1184 	if (err)
1185 		goto out;
1186 	return 0;
1187 out:
1188 	destroy_inodecache();
1189 out1:
1190 	exit_ext2_xattr();
1191 	return err;
1192 }
1193 
1194 static void __exit exit_ext2_fs(void)
1195 {
1196 	unregister_filesystem(&ext2_fs_type);
1197 	destroy_inodecache();
1198 	exit_ext2_xattr();
1199 }
1200 
1201 module_init(init_ext2_fs)
1202 module_exit(exit_ext2_fs)
1203