• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * alloc_tables.c --- Allocate tables for a newly initialized
3  * filesystem.  Used by mke2fs when initializing a filesystem
4  *
5  * Copyright (C) 1996 Theodore Ts'o.
6  *
7  * %Begin-Header%
8  * This file may be redistributed under the terms of the GNU Public
9  * License.
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 
ext2fs_allocate_group_table(ext2_filsys fs,dgrp_t group,ext2fs_block_bitmap bmap)30 errcode_t ext2fs_allocate_group_table(ext2_filsys fs, dgrp_t group,
31 				      ext2fs_block_bitmap bmap)
32 {
33 	errcode_t	retval;
34 	blk_t		group_blk, start_blk, last_blk, new_blk, blk;
35 	int		j;
36 
37 	group_blk = ext2fs_group_first_block(fs, group);
38 	last_blk = ext2fs_group_last_block(fs, group);
39 
40 	if (!bmap)
41 		bmap = fs->block_map;
42 
43 	/*
44 	 * Allocate the block and inode bitmaps, if necessary
45 	 */
46 	if (fs->stride) {
47 		retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
48 						1, bmap, &start_blk);
49 		if (retval)
50 			return retval;
51 		start_blk += fs->inode_blocks_per_group;
52 		start_blk += ((fs->stride * group) %
53 			      (last_blk - start_blk + 1));
54 		if (start_blk >= last_blk)
55 			start_blk = group_blk;
56 	} else
57 		start_blk = group_blk;
58 
59 	if (!fs->group_desc[group].bg_block_bitmap) {
60 		retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
61 						1, bmap, &new_blk);
62 		if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
63 			retval = ext2fs_get_free_blocks(fs, group_blk,
64 					last_blk, 1, bmap, &new_blk);
65 		if (retval)
66 			return retval;
67 		ext2fs_mark_block_bitmap(bmap, new_blk);
68 		fs->group_desc[group].bg_block_bitmap = new_blk;
69 	}
70 
71 	if (!fs->group_desc[group].bg_inode_bitmap) {
72 		retval = ext2fs_get_free_blocks(fs, start_blk, last_blk,
73 						1, bmap, &new_blk);
74 		if (retval == EXT2_ET_BLOCK_ALLOC_FAIL)
75 			retval = ext2fs_get_free_blocks(fs, group_blk,
76 					last_blk, 1, bmap, &new_blk);
77 		if (retval)
78 			return retval;
79 		ext2fs_mark_block_bitmap(bmap, new_blk);
80 		fs->group_desc[group].bg_inode_bitmap = new_blk;
81 	}
82 
83 	/*
84 	 * Allocate the inode table
85 	 */
86 	if (!fs->group_desc[group].bg_inode_table) {
87 		retval = ext2fs_get_free_blocks(fs, group_blk, last_blk,
88 						fs->inode_blocks_per_group,
89 						bmap, &new_blk);
90 		if (retval)
91 			return retval;
92 		for (j=0, blk = new_blk;
93 		     j < fs->inode_blocks_per_group;
94 		     j++, blk++)
95 			ext2fs_mark_block_bitmap(bmap, blk);
96 		fs->group_desc[group].bg_inode_table = new_blk;
97 	}
98 
99 
100 	return 0;
101 }
102 
103 
104 
ext2fs_allocate_tables(ext2_filsys fs)105 errcode_t ext2fs_allocate_tables(ext2_filsys fs)
106 {
107 	errcode_t	retval;
108 	dgrp_t		i;
109 
110 	for (i = 0; i < fs->group_desc_count; i++) {
111 		retval = ext2fs_allocate_group_table(fs, i, fs->block_map);
112 		if (retval)
113 			return retval;
114 	}
115 	return 0;
116 }
117 
118