1 /*
2  * initialize.c --- initialize a filesystem handle given superblock
3  * 	parameters.  Used by mke2fs when initializing a filesystem.
4  *
5  * Copyright (C) 1994, 1995, 1996 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Library
9  * General Public License, version 2.
10  * %End-Header%
11  */
12 
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #if HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 
27 #include "ext2_fs.h"
28 #include "ext2fs.h"
29 
30 #if defined(__linux__)    &&	defined(EXT2_OS_LINUX)
31 #define CREATOR_OS EXT2_OS_LINUX
32 #else
33 #if defined(__GNU__)     &&	defined(EXT2_OS_HURD)
34 #define CREATOR_OS EXT2_OS_HURD
35 #else
36 #if defined(__FreeBSD__) &&	defined(EXT2_OS_FREEBSD)
37 #define CREATOR_OS EXT2_OS_FREEBSD
38 #else
39 #if defined(LITES) 	   &&	defined(EXT2_OS_LITES)
40 #define CREATOR_OS EXT2_OS_LITES
41 #else
42 #define CREATOR_OS EXT2_OS_LINUX /* by default */
43 #endif /* defined(LITES) && defined(EXT2_OS_LITES) */
44 #endif /* defined(__FreeBSD__) && defined(EXT2_OS_FREEBSD) */
45 #endif /* defined(__GNU__)     && defined(EXT2_OS_HURD) */
46 #endif /* defined(__linux__)   && defined(EXT2_OS_LINUX) */
47 
48 /*
49  * Calculate the number of GDT blocks to reserve for online filesystem growth.
50  * The absolute maximum number of GDT blocks we can reserve is determined by
51  * the number of block pointers that can fit into a single block.
52  */
calc_reserved_gdt_blocks(ext2_filsys fs)53 static unsigned int calc_reserved_gdt_blocks(ext2_filsys fs)
54 {
55 	struct ext2_super_block *sb = fs->super;
56 	unsigned long bpg = sb->s_blocks_per_group;
57 	unsigned int gdpb = EXT2_DESC_PER_BLOCK(sb);
58 	unsigned long max_blocks = 0xffffffff;
59 	unsigned long rsv_groups;
60 	unsigned int rsv_gdb;
61 
62 	/* We set it at 1024x the current filesystem size, or
63 	 * the upper block count limit (2^32), whichever is lower.
64 	 */
65 	if (ext2fs_blocks_count(sb) < max_blocks / 1024)
66 		max_blocks = ext2fs_blocks_count(sb) * 1024;
67 	/*
68 	 * ext2fs_div64_ceil() is unnecessary because max_blocks is
69 	 * max _GDT_ blocks, which is limited to 32 bits.
70 	 */
71 	rsv_groups = ext2fs_div_ceil(max_blocks - sb->s_first_data_block, bpg);
72 	rsv_gdb = ext2fs_div_ceil(rsv_groups, gdpb) - fs->desc_blocks;
73 	if (rsv_gdb > EXT2_ADDR_PER_BLOCK(sb))
74 		rsv_gdb = EXT2_ADDR_PER_BLOCK(sb);
75 #ifdef RES_GDT_DEBUG
76 	printf("max_blocks %lu, rsv_groups = %lu, rsv_gdb = %u\n",
77 	       max_blocks, rsv_groups, rsv_gdb);
78 #endif
79 
80 	return rsv_gdb;
81 }
82 
ext2fs_initialize(const char * name,int flags,struct ext2_super_block * param,io_manager manager,ext2_filsys * ret_fs)83 errcode_t ext2fs_initialize(const char *name, int flags,
84 			    struct ext2_super_block *param,
85 			    io_manager manager, ext2_filsys *ret_fs)
86 {
87 	ext2_filsys	fs;
88 	errcode_t	retval;
89 	struct ext2_super_block *super;
90 	unsigned int	rem;
91 	unsigned int	overhead = 0;
92 	unsigned int	ipg;
93 	dgrp_t		i;
94 	blk64_t		free_blocks;
95 	blk_t		numblocks;
96 	int		rsv_gdt;
97 	int		csum_flag;
98 	int		bigalloc_flag;
99 	int		io_flags;
100 	unsigned	reserved_inos;
101 	char		*buf = 0;
102 	char		c;
103 	double		reserved_ratio;
104 
105 	if (!param || !ext2fs_blocks_count(param))
106 		return EXT2_ET_INVALID_ARGUMENT;
107 
108 	retval = ext2fs_get_mem(sizeof(struct struct_ext2_filsys), &fs);
109 	if (retval)
110 		return retval;
111 
112 	memset(fs, 0, sizeof(struct struct_ext2_filsys));
113 	fs->magic = EXT2_ET_MAGIC_EXT2FS_FILSYS;
114 	fs->flags = flags | EXT2_FLAG_RW;
115 	fs->umask = 022;
116 	fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
117 #ifdef WORDS_BIGENDIAN
118 	fs->flags |= EXT2_FLAG_SWAP_BYTES;
119 #endif
120 	io_flags = IO_FLAG_RW;
121 	if (flags & EXT2_FLAG_EXCLUSIVE)
122 		io_flags |= IO_FLAG_EXCLUSIVE;
123 	if (flags & EXT2_FLAG_DIRECT_IO)
124 		io_flags |= IO_FLAG_DIRECT_IO;
125 	retval = manager->open(name, io_flags, &fs->io);
126 	if (retval)
127 		goto cleanup;
128 	fs->image_io = fs->io;
129 	fs->io->app_data = fs;
130 	retval = ext2fs_get_mem(strlen(name)+1, &fs->device_name);
131 	if (retval)
132 		goto cleanup;
133 
134 	strcpy(fs->device_name, name);
135 	retval = ext2fs_get_mem(SUPERBLOCK_SIZE, &super);
136 	if (retval)
137 		goto cleanup;
138 	fs->super = super;
139 
140 	memset(super, 0, SUPERBLOCK_SIZE);
141 
142 #define set_field(field, default) (super->field = param->field ? \
143 				   param->field : (default))
144 #define assign_field(field)	(super->field = param->field)
145 
146 	super->s_magic = EXT2_SUPER_MAGIC;
147 	super->s_state = EXT2_VALID_FS;
148 
149 	bigalloc_flag = EXT2_HAS_RO_COMPAT_FEATURE(param,
150 				   EXT4_FEATURE_RO_COMPAT_BIGALLOC);
151 
152 	assign_field(s_log_block_size);
153 
154 	if (bigalloc_flag) {
155 		set_field(s_log_cluster_size, super->s_log_block_size+4);
156 		if (super->s_log_block_size > super->s_log_cluster_size) {
157 			retval = EXT2_ET_INVALID_ARGUMENT;
158 			goto cleanup;
159 		}
160 	} else
161 		super->s_log_cluster_size = super->s_log_block_size;
162 
163 	set_field(s_first_data_block, super->s_log_cluster_size ? 0 : 1);
164 	set_field(s_max_mnt_count, 0);
165 	set_field(s_errors, EXT2_ERRORS_DEFAULT);
166 	set_field(s_feature_compat, 0);
167 	set_field(s_feature_incompat, 0);
168 	set_field(s_feature_ro_compat, 0);
169 	set_field(s_default_mount_opts, 0);
170 	set_field(s_first_meta_bg, 0);
171 	set_field(s_raid_stride, 0);		/* default stride size: 0 */
172 	set_field(s_raid_stripe_width, 0);	/* default stripe width: 0 */
173 	set_field(s_log_groups_per_flex, 0);
174 	set_field(s_flags, 0);
175 	if (super->s_feature_incompat & ~EXT2_LIB_FEATURE_INCOMPAT_SUPP) {
176 		retval = EXT2_ET_UNSUPP_FEATURE;
177 		goto cleanup;
178 	}
179 	if (super->s_feature_ro_compat & ~EXT2_LIB_FEATURE_RO_COMPAT_SUPP) {
180 		retval = EXT2_ET_RO_UNSUPP_FEATURE;
181 		goto cleanup;
182 	}
183 
184 	set_field(s_rev_level, EXT2_GOOD_OLD_REV);
185 	if (super->s_rev_level >= EXT2_DYNAMIC_REV) {
186 		set_field(s_first_ino, EXT2_GOOD_OLD_FIRST_INO);
187 		set_field(s_inode_size, EXT2_GOOD_OLD_INODE_SIZE);
188 		if (super->s_inode_size >= sizeof(struct ext2_inode_large)) {
189 			int extra_isize = sizeof(struct ext2_inode_large) -
190 				EXT2_GOOD_OLD_INODE_SIZE;
191 			set_field(s_min_extra_isize, extra_isize);
192 			set_field(s_want_extra_isize, extra_isize);
193 		}
194 	} else {
195 		super->s_first_ino = EXT2_GOOD_OLD_FIRST_INO;
196 		super->s_inode_size = EXT2_GOOD_OLD_INODE_SIZE;
197 	}
198 
199 	set_field(s_checkinterval, 0);
200 	super->s_mkfs_time = super->s_lastcheck = fs->now ? fs->now : time(NULL);
201 
202 	super->s_creator_os = CREATOR_OS;
203 
204 	fs->fragsize = fs->blocksize = EXT2_BLOCK_SIZE(super);
205 	fs->cluster_ratio_bits = super->s_log_cluster_size -
206 		super->s_log_block_size;
207 
208 	if (bigalloc_flag) {
209 		unsigned long long bpg;
210 
211 		if (param->s_blocks_per_group &&
212 		    param->s_clusters_per_group &&
213 		    ((param->s_clusters_per_group * EXT2FS_CLUSTER_RATIO(fs)) !=
214 		     param->s_blocks_per_group)) {
215 			retval = EXT2_ET_INVALID_ARGUMENT;
216 			goto cleanup;
217 		}
218 		if (param->s_clusters_per_group)
219 			assign_field(s_clusters_per_group);
220 		else if (param->s_blocks_per_group)
221 			super->s_clusters_per_group =
222 				param->s_blocks_per_group /
223 				EXT2FS_CLUSTER_RATIO(fs);
224 		else if (super->s_log_cluster_size + 15 < 32)
225 			super->s_clusters_per_group = fs->blocksize * 8;
226 		else
227 			super->s_clusters_per_group = (fs->blocksize - 1) * 8;
228 		if (super->s_clusters_per_group > EXT2_MAX_CLUSTERS_PER_GROUP(super))
229 			super->s_clusters_per_group = EXT2_MAX_CLUSTERS_PER_GROUP(super);
230 		bpg = EXT2FS_C2B(fs,
231 			(unsigned long long) super->s_clusters_per_group);
232 		if (bpg >= (((unsigned long long) 1) << 32)) {
233 			retval = EXT2_ET_INVALID_ARGUMENT;
234 			goto cleanup;
235 		}
236 		super->s_blocks_per_group = bpg;
237 	} else {
238 		set_field(s_blocks_per_group, fs->blocksize * 8);
239 		if (super->s_blocks_per_group > EXT2_MAX_BLOCKS_PER_GROUP(super))
240 			super->s_blocks_per_group = EXT2_MAX_BLOCKS_PER_GROUP(super);
241 		super->s_clusters_per_group = super->s_blocks_per_group;
242 	}
243 
244 	ext2fs_blocks_count_set(super, ext2fs_blocks_count(param) &
245 				~((blk64_t) EXT2FS_CLUSTER_MASK(fs)));
246 	ext2fs_r_blocks_count_set(super, ext2fs_r_blocks_count(param));
247 	if (ext2fs_r_blocks_count(super) >= ext2fs_blocks_count(param)) {
248 		retval = EXT2_ET_INVALID_ARGUMENT;
249 		goto cleanup;
250 	}
251 
252 	set_field(s_mmp_update_interval, 0);
253 
254 	/*
255 	 * If we're creating an external journal device, we don't need
256 	 * to bother with the rest.
257 	 */
258 	if (super->s_feature_incompat & EXT3_FEATURE_INCOMPAT_JOURNAL_DEV) {
259 		fs->group_desc_count = 0;
260 		ext2fs_mark_super_dirty(fs);
261 		*ret_fs = fs;
262 		return 0;
263 	}
264 
265 retry:
266 	fs->group_desc_count = (dgrp_t) ext2fs_div64_ceil(
267 		ext2fs_blocks_count(super) - super->s_first_data_block,
268 		EXT2_BLOCKS_PER_GROUP(super));
269 	if (fs->group_desc_count == 0) {
270 		retval = EXT2_ET_TOOSMALL;
271 		goto cleanup;
272 	}
273 
274 	set_field(s_desc_size,
275 		  super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT ?
276 		  EXT2_MIN_DESC_SIZE_64BIT : 0);
277 
278 	fs->desc_blocks = ext2fs_div_ceil(fs->group_desc_count,
279 					  EXT2_DESC_PER_BLOCK(super));
280 
281 	i = fs->blocksize >= 4096 ? 1 : 4096 / fs->blocksize;
282 
283 	if (super->s_feature_incompat & EXT4_FEATURE_INCOMPAT_64BIT &&
284 	    (ext2fs_blocks_count(super) / i) > (1ULL << 32))
285 		set_field(s_inodes_count, ~0U);
286 	else
287 		set_field(s_inodes_count, ext2fs_blocks_count(super) / i);
288 
289 	/*
290 	 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
291 	 * that we have enough inodes for the filesystem(!)
292 	 */
293 	if (super->s_inodes_count < EXT2_FIRST_INODE(super)+1)
294 		super->s_inodes_count = EXT2_FIRST_INODE(super)+1;
295 
296 	/*
297 	 * There should be at least as many inodes as the user
298 	 * requested.  Figure out how many inodes per group that
299 	 * should be.  But make sure that we don't allocate more than
300 	 * one bitmap's worth of inodes each group.
301 	 */
302 	ipg = ext2fs_div_ceil(super->s_inodes_count, fs->group_desc_count);
303 	if (ipg > fs->blocksize * 8) {
304 		if (!bigalloc_flag && super->s_blocks_per_group >= 256) {
305 			/* Try again with slightly different parameters */
306 			super->s_blocks_per_group -= 8;
307 			ext2fs_blocks_count_set(super,
308 						ext2fs_blocks_count(param));
309 			super->s_clusters_per_group = super->s_blocks_per_group;
310 			goto retry;
311 		} else {
312 			retval = EXT2_ET_TOO_MANY_INODES;
313 			goto cleanup;
314 		}
315 	}
316 
317 	if (ipg > (unsigned) EXT2_MAX_INODES_PER_GROUP(super))
318 		ipg = EXT2_MAX_INODES_PER_GROUP(super);
319 
320 ipg_retry:
321 	super->s_inodes_per_group = ipg;
322 
323 	/*
324 	 * Make sure the number of inodes per group completely fills
325 	 * the inode table blocks in the descriptor.  If not, add some
326 	 * additional inodes/group.  Waste not, want not...
327 	 */
328 	fs->inode_blocks_per_group = (((super->s_inodes_per_group *
329 					EXT2_INODE_SIZE(super)) +
330 				       EXT2_BLOCK_SIZE(super) - 1) /
331 				      EXT2_BLOCK_SIZE(super));
332 	super->s_inodes_per_group = ((fs->inode_blocks_per_group *
333 				      EXT2_BLOCK_SIZE(super)) /
334 				     EXT2_INODE_SIZE(super));
335 	/*
336 	 * Finally, make sure the number of inodes per group is a
337 	 * multiple of 8.  This is needed to simplify the bitmap
338 	 * splicing code.
339 	 */
340 	if (super->s_inodes_per_group < 8)
341 		super->s_inodes_per_group = 8;
342 	super->s_inodes_per_group &= ~7;
343 	fs->inode_blocks_per_group = (((super->s_inodes_per_group *
344 					EXT2_INODE_SIZE(super)) +
345 				       EXT2_BLOCK_SIZE(super) - 1) /
346 				      EXT2_BLOCK_SIZE(super));
347 
348 	/*
349 	 * adjust inode count to reflect the adjusted inodes_per_group
350 	 */
351 	if ((__u64)super->s_inodes_per_group * fs->group_desc_count > ~0U) {
352 		ipg--;
353 		goto ipg_retry;
354 	}
355 	super->s_inodes_count = super->s_inodes_per_group *
356 		fs->group_desc_count;
357 	super->s_free_inodes_count = super->s_inodes_count;
358 
359 	/*
360 	 * check the number of reserved group descriptor table blocks
361 	 */
362 	if (super->s_feature_compat & EXT2_FEATURE_COMPAT_RESIZE_INODE)
363 		rsv_gdt = calc_reserved_gdt_blocks(fs);
364 	else
365 		rsv_gdt = 0;
366 	set_field(s_reserved_gdt_blocks, rsv_gdt);
367 	if (super->s_reserved_gdt_blocks > EXT2_ADDR_PER_BLOCK(super)) {
368 		retval = EXT2_ET_RES_GDT_BLOCKS;
369 		goto cleanup;
370 	}
371 
372 	/*
373 	 * Calculate the maximum number of bookkeeping blocks per
374 	 * group.  It includes the superblock, the block group
375 	 * descriptors, the block bitmap, the inode bitmap, the inode
376 	 * table, and the reserved gdt blocks.
377 	 */
378 	overhead = (int) (3 + fs->inode_blocks_per_group +
379 			  fs->desc_blocks + super->s_reserved_gdt_blocks);
380 
381 	/* This can only happen if the user requested too many inodes */
382 	if (overhead > super->s_blocks_per_group) {
383 		retval = EXT2_ET_TOO_MANY_INODES;
384 		goto cleanup;
385 	}
386 
387 	/*
388 	 * See if the last group is big enough to support the
389 	 * necessary data structures.  If not, we need to get rid of
390 	 * it.  We need to recalculate the overhead for the last block
391 	 * group, since it might or might not have a superblock
392 	 * backup.
393 	 */
394 	overhead = (int) (2 + fs->inode_blocks_per_group);
395 	if (ext2fs_bg_has_super(fs, fs->group_desc_count - 1))
396 		overhead += 1 + fs->desc_blocks + super->s_reserved_gdt_blocks;
397 	rem = ((ext2fs_blocks_count(super) - super->s_first_data_block) %
398 	       super->s_blocks_per_group);
399 	if ((fs->group_desc_count == 1) && rem && (rem < overhead)) {
400 		retval = EXT2_ET_TOOSMALL;
401 		goto cleanup;
402 	}
403 	if (rem && (rem < overhead+50)) {
404 		ext2fs_blocks_count_set(super, ext2fs_blocks_count(super) -
405 					rem);
406 		/*
407 		 * If blocks count is changed, we need to recalculate
408 		 * reserved blocks count not to exceed 50%.
409 		 */
410 		reserved_ratio = 100.0 * ext2fs_r_blocks_count(param) /
411 			ext2fs_blocks_count(param);
412 		ext2fs_r_blocks_count_set(super, reserved_ratio *
413 			ext2fs_blocks_count(super) / 100.0);
414 
415 		goto retry;
416 	}
417 
418 	/*
419 	 * At this point we know how big the filesystem will be.  So
420 	 * we can do any and all allocations that depend on the block
421 	 * count.
422 	 */
423 
424 	retval = ext2fs_get_mem(strlen(fs->device_name) + 80, &buf);
425 	if (retval)
426 		goto cleanup;
427 
428 	strcpy(buf, "block bitmap for ");
429 	strcat(buf, fs->device_name);
430 	retval = ext2fs_allocate_subcluster_bitmap(fs, buf, &fs->block_map);
431 	if (retval)
432 		goto cleanup;
433 
434 	strcpy(buf, "inode bitmap for ");
435 	strcat(buf, fs->device_name);
436 	retval = ext2fs_allocate_inode_bitmap(fs, buf, &fs->inode_map);
437 	if (retval)
438 		goto cleanup;
439 
440 	ext2fs_free_mem(&buf);
441 
442 	retval = ext2fs_get_array(fs->desc_blocks, fs->blocksize,
443 				&fs->group_desc);
444 	if (retval)
445 		goto cleanup;
446 
447 	memset(fs->group_desc, 0, (size_t) fs->desc_blocks * fs->blocksize);
448 
449 	/*
450 	 * Reserve the superblock and group descriptors for each
451 	 * group, and fill in the correct group statistics for group.
452 	 * Note that although the block bitmap, inode bitmap, and
453 	 * inode table have not been allocated (and in fact won't be
454 	 * by this routine), they are accounted for nevertheless.
455 	 *
456 	 * If FLEX_BG meta-data grouping is used, only account for the
457 	 * superblock and group descriptors (the inode tables and
458 	 * bitmaps will be accounted for when allocated).
459 	 */
460 	free_blocks = 0;
461 	csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
462 					       EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
463 	reserved_inos = super->s_first_ino;
464 	for (i = 0; i < fs->group_desc_count; i++) {
465 		/*
466 		 * Don't set the BLOCK_UNINIT group for the last group
467 		 * because the block bitmap needs to be padded.
468 		 */
469 		if (csum_flag) {
470 			if (i != fs->group_desc_count - 1)
471 				ext2fs_bg_flags_set(fs, i,
472 						    EXT2_BG_BLOCK_UNINIT);
473 			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
474 			numblocks = super->s_inodes_per_group;
475 			if (reserved_inos) {
476 				if (numblocks > reserved_inos) {
477 					numblocks -= reserved_inos;
478 					reserved_inos = 0;
479 				} else {
480 					reserved_inos -= numblocks;
481 					numblocks = 0;
482 				}
483 			}
484 			ext2fs_bg_itable_unused_set(fs, i, numblocks);
485 		}
486 		numblocks = ext2fs_reserve_super_and_bgd(fs, i, fs->block_map);
487 		if (fs->super->s_log_groups_per_flex)
488 			numblocks += 2 + fs->inode_blocks_per_group;
489 
490 		free_blocks += numblocks;
491 		ext2fs_bg_free_blocks_count_set(fs, i, numblocks);
492 		ext2fs_bg_free_inodes_count_set(fs, i, fs->super->s_inodes_per_group);
493 		ext2fs_bg_used_dirs_count_set(fs, i, 0);
494 		ext2fs_group_desc_csum_set(fs, i);
495 	}
496 	free_blocks &= ~EXT2FS_CLUSTER_MASK(fs);
497 	ext2fs_free_blocks_count_set(super, free_blocks);
498 
499 	c = (char) 255;
500 	if (((int) c) == -1) {
501 		super->s_flags |= EXT2_FLAGS_SIGNED_HASH;
502 	} else {
503 		super->s_flags |= EXT2_FLAGS_UNSIGNED_HASH;
504 	}
505 
506 	ext2fs_mark_super_dirty(fs);
507 	ext2fs_mark_bb_dirty(fs);
508 	ext2fs_mark_ib_dirty(fs);
509 
510 	io_channel_set_blksize(fs->io, fs->blocksize);
511 
512 	*ret_fs = fs;
513 	return 0;
514 cleanup:
515 	free(buf);
516 	ext2fs_free(fs);
517 	return retval;
518 }
519