4 * PROGRAMMER: Matt Wu <mattwu@163.com>
5 * HOMEPAGE: http://ext2.yeah.net
8 /* INCLUDES **************************************************************/
13 /* DEFINITIONS ***********************************************************/
15 extern int inode_ratio
;
18 /* FUNCTIONS *************************************************************/
20 void ext2_print_super(PEXT2_SUPER_BLOCK pExt2Sb
)
24 DPRINT("\nExt2 Super Block Details ...\n\n");
25 DPRINT(" Inode Count: %lu\n", pExt2Sb
->s_inodes_count
);
26 DPRINT(" Block Count: %lu\n", pExt2Sb
->s_blocks_count
);
27 DPRINT(" Reserved Block Count: %lu\n", pExt2Sb
->s_r_blocks_count
);
28 DPRINT(" Free Blocks: %lu\n", pExt2Sb
->s_free_blocks_count
);
29 DPRINT(" Free Inodes: %lu\n", pExt2Sb
->s_free_inodes_count
);
30 DPRINT(" First Data Block: %lu\n", pExt2Sb
->s_first_data_block
);
31 DPRINT(" Log Block Size: %lu\n", pExt2Sb
->s_log_block_size
);
32 DPRINT(" Log Frag Size: %ld\n", pExt2Sb
->s_log_frag_size
);
33 DPRINT(" Blocks per Group: %lu\n", pExt2Sb
->s_blocks_per_group
);
34 DPRINT(" Fragments per Group: %lu\n", pExt2Sb
->s_frags_per_group
);
35 DPRINT(" Inodes per Group: %lu\n", pExt2Sb
->s_inodes_per_group
);
36 // DPRINT(" Mount Time: %s", ctime((time_t *) & (pExt2Sb->s_mtime)));
37 // DPRINT(" Write Time: %s", ctime((time_t *) & (pExt2Sb->s_wtime)));
38 DPRINT(" Mount Count: %u\n", pExt2Sb
->s_mnt_count
);
39 DPRINT(" Max Mount Count: %d\n", pExt2Sb
->s_max_mnt_count
);
40 DPRINT(" Magic Number: %X (%s)\n", pExt2Sb
->s_magic
,
41 pExt2Sb
->s_magic
== EXT2_SUPER_MAGIC
? "OK" : "BAD");
42 DPRINT(" File System State: %X\n", pExt2Sb
->s_state
);
43 DPRINT(" Error Behaviour: %X\n", pExt2Sb
->s_errors
);
44 DPRINT(" Minor rev: %u\n", pExt2Sb
->s_minor_rev_level
);
45 // DPRINT(" Last Check: %s", ctime((time_t *) & (pExt2Sb->s_lastcheck)));
46 DPRINT(" Check Interval: %lu\n", pExt2Sb
->s_checkinterval
);
47 DPRINT(" Creator OS: %lu\n", pExt2Sb
->s_creator_os
);
48 DPRINT(" Revision Level: %lu\n", pExt2Sb
->s_rev_level
);
49 DPRINT(" Reserved Block Default UID: %u\n", pExt2Sb
->s_def_resuid
);
50 DPRINT(" Reserved Block Default GID: %u\n", pExt2Sb
->s_def_resgid
);
52 for (i
=0; i
< 16; i
++)
53 DbgPrint("%x ", pExt2Sb
->s_uuid
[i
]);
56 DPRINT(" volume label name: ");
57 for (i
=0; i
< 16; i
++)
59 if (pExt2Sb
->s_volume_name
[i
] == 0)
61 DbgPrint("%c", pExt2Sb
->s_volume_name
[i
]);
68 #define set_field(field, default) if (!pExt2Sb->field) pExt2Sb->field = (default);
71 * Initialize super block ...
74 bool ext2_initialize_sb(PEXT2_FILESYS Ext2Sys
)
76 int frags_per_block
= 0;
80 ULONG group_block
= 0;
82 PEXT2_SUPER_BLOCK pExt2Sb
= Ext2Sys
->ext2_sb
;
83 LARGE_INTEGER SysTime
;
85 NtQuerySystemTime(&SysTime
);
87 Ext2Sys
->blocksize
= EXT2_BLOCK_SIZE(pExt2Sb
);
88 Ext2Sys
->fragsize
= EXT2_FRAG_SIZE(pExt2Sb
);
89 frags_per_block
= Ext2Sys
->blocksize
/ Ext2Sys
->fragsize
;
91 pExt2Sb
->s_magic
= EXT2_SUPER_MAGIC
;
92 pExt2Sb
->s_state
= EXT2_VALID_FS
;
94 pExt2Sb
->s_first_data_block
= (pExt2Sb
->s_log_block_size
) ? 0 : 1;
95 pExt2Sb
->s_max_mnt_count
= EXT2_DFL_MAX_MNT_COUNT
;
97 pExt2Sb
->s_errors
= EXT2_ERRORS_DEFAULT
;
99 pExt2Sb
->s_checkinterval
= EXT2_DFL_CHECKINTERVAL
;
101 if (!pExt2Sb
->s_rev_level
)
102 pExt2Sb
->s_rev_level
= EXT2_GOOD_OLD_REV
;
104 if (pExt2Sb
->s_rev_level
>= EXT2_DYNAMIC_REV
)
106 set_field(s_first_ino
, EXT2_GOOD_OLD_FIRST_INO
);
107 set_field(s_inode_size
, EXT2_GOOD_OLD_INODE_SIZE
);
110 RtlTimeToSecondsSince1970(&SysTime
, &pExt2Sb
->s_wtime
);
111 pExt2Sb
->s_lastcheck
= pExt2Sb
->s_mtime
= pExt2Sb
->s_wtime
;
113 if (!pExt2Sb
->s_blocks_per_group
)
114 pExt2Sb
->s_blocks_per_group
= Ext2Sys
->blocksize
* 8;
116 pExt2Sb
->s_frags_per_group
= pExt2Sb
->s_blocks_per_group
* frags_per_block
;
117 pExt2Sb
->s_creator_os
= EXT2_OS_WINNT
;
119 if (pExt2Sb
->s_r_blocks_count
>= pExt2Sb
->s_blocks_count
)
125 * If we're creating an external journal device, we don't need
126 * to bother with the rest.
128 if (pExt2Sb
->s_feature_incompat
&
129 EXT3_FEATURE_INCOMPAT_JOURNAL_DEV
)
131 Ext2Sys
->group_desc_count
= 0;
132 // ext2fs_mark_super_dirty(fs);
138 Ext2Sys
->group_desc_count
= (pExt2Sb
->s_blocks_count
- pExt2Sb
->s_first_data_block
139 + EXT2_BLOCKS_PER_GROUP(pExt2Sb
) - 1) / EXT2_BLOCKS_PER_GROUP(pExt2Sb
);
141 if (Ext2Sys
->group_desc_count
== 0)
144 Ext2Sys
->desc_blocks
= (Ext2Sys
->group_desc_count
+ EXT2_DESC_PER_BLOCK(pExt2Sb
)
145 - 1) / EXT2_DESC_PER_BLOCK(pExt2Sb
);
147 if (!pExt2Sb
->s_inodes_count
)
148 pExt2Sb
->s_inodes_count
= pExt2Sb
->s_blocks_count
/ ( inode_ratio
/Ext2Sys
->blocksize
);
151 * Make sure we have at least EXT2_FIRST_INO + 1 inodes, so
152 * that we have enough inodes for the filesystem(!)
154 if (pExt2Sb
->s_inodes_count
< EXT2_FIRST_INODE(pExt2Sb
)+1)
155 pExt2Sb
->s_inodes_count
= EXT2_FIRST_INODE(pExt2Sb
)+1;
158 * There should be at least as many inodes as the user
159 * requested. Figure out how many inodes per group that
160 * should be. But make sure that we don't allocate more than
161 * one bitmap's worth of inodes
163 pExt2Sb
->s_inodes_per_group
= (pExt2Sb
->s_inodes_count
+ Ext2Sys
->group_desc_count
- 1)
164 /Ext2Sys
->group_desc_count
;
166 if (pExt2Sb
->s_inodes_per_group
> (ULONG
)(Ext2Sys
->blocksize
*8))
167 pExt2Sb
->s_inodes_per_group
= Ext2Sys
->blocksize
*8;
170 * Make sure the number of inodes per group completely fills
171 * the inode table blocks in the descriptor. If not, add some
172 * additional inodes/group. Waste not, want not...
174 Ext2Sys
->inode_blocks_per_group
= (((pExt2Sb
->s_inodes_per_group
* EXT2_INODE_SIZE(pExt2Sb
))
175 + EXT2_BLOCK_SIZE(pExt2Sb
) - 1) / EXT2_BLOCK_SIZE(pExt2Sb
));
177 pExt2Sb
->s_inodes_per_group
= ((Ext2Sys
->inode_blocks_per_group
* EXT2_BLOCK_SIZE(pExt2Sb
))
178 / EXT2_INODE_SIZE(pExt2Sb
));
181 * Finally, make sure the number of inodes per group is a
182 * multiple of 8. This is needed to simplify the bitmap
185 pExt2Sb
->s_inodes_per_group
&= ~7;
186 Ext2Sys
->inode_blocks_per_group
= (((pExt2Sb
->s_inodes_per_group
* EXT2_INODE_SIZE(pExt2Sb
))
187 + EXT2_BLOCK_SIZE(pExt2Sb
) - 1) / EXT2_BLOCK_SIZE(pExt2Sb
));
190 * adjust inode count to reflect the adjusted inodes_per_group
192 pExt2Sb
->s_inodes_count
= pExt2Sb
->s_inodes_per_group
* Ext2Sys
->group_desc_count
;
193 pExt2Sb
->s_free_inodes_count
= pExt2Sb
->s_inodes_count
;
196 * Overhead is the number of bookkeeping blocks per group. It
197 * includes the superblock backup, the group descriptor
198 * backups, the inode bitmap, the block bitmap, and the inode
201 * XXX Not all block groups need the descriptor blocks, but
202 * being clever is tricky...
204 overhead
= (3 + Ext2Sys
->desc_blocks
+ Ext2Sys
->inode_blocks_per_group
);
207 * See if the last group is big enough to support the
208 * necessary data structures. If not, we need to get rid of
211 rem
= ((pExt2Sb
->s_blocks_count
- pExt2Sb
->s_first_data_block
) %
212 pExt2Sb
->s_blocks_per_group
);
214 if ((Ext2Sys
->group_desc_count
== 1) && rem
&& (rem
< overhead
))
217 if (rem
&& (rem
< overhead
+50))
219 pExt2Sb
->s_blocks_count
-= rem
;
224 * At this point we know how big the filesystem will be. So we can do
225 * any and all allocations that depend on the block count.
228 // Allocate block bitmap
229 if(!ext2_allocate_block_bitmap(Ext2Sys
))
234 // Allocate inode bitmap
235 if(!ext2_allocate_inode_bitmap(Ext2Sys
))
240 // Allocate gourp desc
241 if(!ext2_allocate_group_desc(Ext2Sys
))
247 * Reserve the superblock and group descriptors for each
248 * group, and fill in the correct group statistics for group.
249 * Note that although the block bitmap, inode bitmap, and
250 * inode table have not been allocated (and in fact won't be
251 * by this routine), they are accounted for nevertheless.
253 group_block
= pExt2Sb
->s_first_data_block
;
256 pExt2Sb
->s_free_blocks_count
= 0;
258 for (i
= 0; i
< Ext2Sys
->group_desc_count
; i
++)
260 if (i
== Ext2Sys
->group_desc_count
-1)
262 numblocks
= (pExt2Sb
->s_blocks_count
- pExt2Sb
->s_first_data_block
)
263 % pExt2Sb
->s_blocks_per_group
;
266 numblocks
= pExt2Sb
->s_blocks_per_group
;
270 numblocks
= pExt2Sb
->s_blocks_per_group
;
273 if (ext2_bg_has_super(pExt2Sb
, i
))
277 for (j
=0; j
< Ext2Sys
->desc_blocks
+1; j
++)
278 ext2_mark_bitmap(Ext2Sys
->block_map
, group_block
+ j
);
280 numblocks
-= 1 + Ext2Sys
->desc_blocks
;
283 numblocks
-= 2 + Ext2Sys
->inode_blocks_per_group
;
285 pExt2Sb
->s_free_blocks_count
+= numblocks
;
286 Ext2Sys
->group_desc
[i
].bg_free_blocks_count
= (__u16
)numblocks
;
287 Ext2Sys
->group_desc
[i
].bg_free_inodes_count
= (__u16
)pExt2Sb
->s_inodes_per_group
;
288 Ext2Sys
->group_desc
[i
].bg_used_dirs_count
= 0;
290 group_block
+= pExt2Sb
->s_blocks_per_group
;
297 ext2_free_group_desc(Ext2Sys
);
298 ext2_free_block_bitmap(Ext2Sys
);
299 ext2_free_inode_bitmap(Ext2Sys
);