Add hexdump lib function
[thunix.git] / include / fs_ext2.h
blob3cd45b2094ca059f1b4a0463ffb9b33b65026e1f
1 #ifndef __FS_EXT2_H
2 #define __FS_EXT2_H
5 #include <types.h>
6 #include <rd.h>
8 /*
9 * here is the DISK DATA STRUCTURE of EXT2:
11 * |<-------------------Block Group 0------------------------>|
12 +-------+-------+-------------+------------+---------------+--------+---...---+-----------------+
13 | Boot | super | Group | Data block | inode | inode | Data | | block |
14 | Block | Block | Descriptors | bitmap | bitmap| Table | blocks | | group n |
15 +-------+-------+-------------+------------+-------+-------+--------+---...---+-----------------+
16 1 block 1 block n blocks 1 block 1 block n blcoks n blocks
18 Ok,that's a nice structure.here are some more detials:
19 1. I'm goin to use a 1.44M floppy.
20 2. So, the block size should be 1024B, that's two sector's size,means 1block = 2sector
21 3. As the EXT2 does, we leave the boot block along, doing nothing to her.It just reversed and takes
22 two sectors size.
23 4. We have ONE super block, ONE group descriptor which means it only takes ONE block size.
24 5. We have set the block size to be 1024B,so one block group can hold 1024 * 8 = 8096 blocks, and
25 that's quite enough to our 1.44M floppy which totally has 1440 blocks.
26 6. We use 23 blocks to store the inode disk structure, so there are 23*8=184 inodes per block group.
33 * super block structure:
34 * include/linux/ext2_fs.h
36 struct ext2_super_block
38 __u32 s_inodes_count; /* Inodes count */
39 __u32 s_blocks_count; /* Blocks count */
40 __u32 s_r_blocks_count; /* Reserved blocks count */
41 __u32 s_free_blocks_count; /* Free blocks count */
42 __u32 s_free_inodes_count; /* Free inodes count */
43 __u32 s_first_data_block; /* First Data Block */
44 __u32 s_log_block_size; /* Block size */
45 __s32 s_log_frag_size; /* Fragment size */
46 __u32 s_blocks_per_group; /* # Blocks per group */
47 __u32 s_frags_per_group; /* # Fragments per group */
48 __u32 s_inodes_per_group; /* # Inodes per group */
49 __u32 s_mtime; /* Mount time */
50 __u32 s_wtime; /* Write time */
51 __u16 s_mnt_count; /* Mount count */
52 __s16 s_max_mnt_count; /* Maximal mount count */
53 __u16 s_magic; /* Magic signature */
54 __u16 s_state; /* File system state */
55 __u16 s_errors; /* Behaviour when detecting errors */
56 __u16 s_pad;
57 __u32 s_lastcheck; /* time of last check */
58 __u32 s_checkinterval; /* max. time between checks */
59 __u32 s_creator_os; /* OS */
60 __u32 s_rev_level; /* Revision level */
61 __u16 s_def_resuid; /* Default uid for reserved blocks */
62 __u16 s_def_resgid; /* Default gid for reserved blocks */
63 __u32 s_reserved[235]; /* Padding to the end of the block */
67 * and typically, there are only serveral fileds of interest.
68 * s_inodes_count records the total inode number the filesystem have, here is 184
69 * s_blocks_count records the total block number the filesystem have, here is 1440.
70 * s_r_blocks count records the reserved blocks, usually be 5% of the filestem size.
71 * here is 72.
72 * s_free_blocks_count records the free number of free blocks, here is 1393
73 * s_free_inodes_count records the free number of free inodes, here is 173.
74 * s_first_data_block records the start block number of block group one, here is 1.
75 * s_log_block_size block size = 1 << (10 + s_log_blocks_size), so here is ZERO.
76 * s_log_frag_size similar to s_log_block_size, we don's use it.
77 * s_blocks_per_group records the block number per group, here is 8092.
78 * S_inodes_per_group records the block number per group, here is 184.
79 * s_magic a macro that indicate it IS an EXT2 filesystem, it's 0XEE53
80 */
83 /*
84 * ext2 group desc structure:
86 struct ext2_group_desc
88 __u32 bg_block_bitmap; /* Blocks bitmap block */
89 __u32 bg_inode_bitmap; /* Inodes bitmap block */
90 __u32 bg_inode_table; /* Inodes table block */
91 __u16 bg_free_blocks_count; /* Free blocks count */
92 __u16 bg_free_inodes_count; /* Free inodes count */
93 __u16 bg_used_dirs_count; /* Directories count */
94 __u16 bg_pad;
95 __u32 bg_reserved[3];
99 * As you can see, it's simply, just recode the state of the group, such as in which block the
100 * blocks/inodes bitmap stores, in which block the indoes table stores, and how many free
101 * blocks/inodes the group have now. And the bg_used_dirs_count keep the record of directories
102 * count in this group to keep the blance of each group.
105 #define EXT2_N_BLOCKS 15
108 * ext2 inode structure:
110 struct ext2_inode
112 __u16 i_mode; /* File mode */
113 __u16 i_uid; /* Owner Uid */
114 __u32 i_size; /* 4: Size in bytes */
115 __u32 i_atime; /* Access time */
116 __u32 i_ctime; /* 12: Creation time */
117 __u32 i_mtime; /* Modification time */
118 __u32 i_dtime; /* 20: Deletion Time */
119 __u16 i_gid; /* Group Id */
120 __u16 i_links_count; /* 24: Links count */
121 __u32 i_blocks; /* Blocks count */
122 __u32 i_flags; /* 32: File flags */
123 union
125 struct
127 __u32 l_i_reserved1;
129 linux1;
130 struct
132 __u32 h_i_translator;
134 hurd1;
135 struct
137 __u32 m_i_reserved1;
139 masix1;
141 osd1; /* OS dependent 1 */
142 __u32 i_block[EXT2_N_BLOCKS]; /* 40: Pointers to blocks */
143 __u32 i_version; /* File version (for NFS) */
144 __u32 i_file_acl; /* File ACL */
145 __u32 i_dir_acl; /* Directory ACL */
146 __u32 i_faddr; /* Fragment address */
147 union
149 struct
151 __u8 l_i_frag; /* Fragment number */
152 __u8 l_i_fsize; /* Fragment size */
153 __u16 i_pad1;
154 __u32 l_i_reserved2[2];
156 linux2;
157 struct
159 __u8 h_i_frag; /* Fragment number */
160 __u8 h_i_fsize; /* Fragment size */
161 __u16 h_i_mode_high;
162 __u16 h_i_uid_high;
163 __u16 h_i_gid_high;
164 __u32 h_i_author;
166 hurd2;
167 struct
169 __u8 m_i_frag; /* Fragment number */
170 __u8 m_i_fsize; /* Fragment size */
171 __u16 m_pad1;
172 __u32 m_i_reserved2[2];
174 masix2;
176 osd2; /* OS dependent 2 */
179 * Yeah, that looks really quite large, but just only several fileds we should note it.
180 * i_size recods the file size in bytes.
181 * i_block recods the data block not the filesyste block the file has taken up.
182 * i_blocks[] that's really IMPORTANT.it stors the block address where the file stores.
188 struct ext2_sb_info {
189 unsigned long s_frag_size; /* Size of a fragment in bytes */
190 unsigned long s_frags_per_block;/* Number of fragments per block */
191 unsigned long s_inodes_per_block;/* Number of inodes per block */
192 unsigned long s_frags_per_group;/* Number of fragments in a group */
193 unsigned long s_blocks_per_group;/* Number of blocks in a group */
194 unsigned long s_inodes_per_group;/* Number of inodes in a group */
195 unsigned long s_itb_per_group; /* Number of inode table blocks per group */
196 unsigned long s_gdb_count; /* Number of group descriptor blocks */
197 unsigned long s_desc_per_block; /* Number of group descriptors per block */
198 unsigned long s_groups_count; /* Number of groups in the fs */
199 //struct buffer_head * s_sbh; /* Buffer containing the super block */
200 //struct ext2_super_block * s_es; /* Pointer to the super block in the buffer */
201 //struct buffer_head ** s_group_desc;
204 unsigned long s_mount_opt;
205 uid_t s_resuid;
206 gid_t s_resgid;
207 unsigned short s_mount_state;
208 unsigned short s_pad;
209 int s_addr_per_block_bits;
210 int s_desc_per_block_bits;
211 int s_inode_size;
212 int s_first_ino;
213 spinlock_t s_next_gen_lock;
214 u32 s_next_generation;
215 unsigned long s_dir_count;
216 u8 *s_debts;
217 struct percpu_counter s_freeblocks_counter;
218 struct percpu_counter s_freeinodes_counter;
219 struct percpu_counter s_dirs_counter;
220 struct blockgroup_lock s_blockgroup_lock;
222 unsigned int s_free_blocks_count;
223 unsigned int s_free_inodes_count;
224 unsigned int s_dirs_count;
230 * second extended file system inode data in memory
232 struct m_inode {
233 struct ext2_inode inode;
234 /*addiation part in memory */
235 unsigned int i_mode;
236 unsigned int i_num;
237 unsigned int i_count;
238 unsigned int i_nlinks;
239 unsigned long i_atime;
240 unsigned long i_ctime;
241 unsigned char i_lock;
242 unsigned char i_dirt;
243 unsigned char i_mount;
244 unsigned char i_update;
248 #define S_IFREG 1
249 #define S_IFDIR 2
251 #define S_ISDIR(m) ( (m & S_IFDIR) == S_IFDIR )
252 #define S_ISREG(m) ( (m & S_IFREG) == S_IFREG )
256 #define EXT2_BUFFER (0x800000)
257 #define EXT2_GROUP_DESC_BUFFER (0x800400)
258 #define EXT2_BITMAP_BUFFER (0x802000)
260 #define EXT2_SBI() ((struct ext2_sb_info *)EXT2_BUFFER)
262 #define EXT2_BLOCK_SIZE (1024)
263 #define EXT2_INODE_SIZE (sizeof(struct ext2_inode))
266 #define EXT2_BLOCKS_PER_GROUP (8096)
267 #define EXT2_INODES_PER_GROUP (184)
268 #define EXT2_INODES_PER_BLOCK (8)
270 #define EXT2_BLOCKS_PER_GROUP_BITS (13)
271 #define EXT2_INODES_PER_GROUP_BITS (0)
273 #define SUPER_BLOCK 1
274 #define GROUP_DESC 2
275 #define BLOCK_BITMAP 8
276 #define INODE_BITMAP 9
278 #define ROOT_INODE 2
280 #define BLOCK 1
281 #define INODE 2
284 #define EXT2_NAME_LEN 255
285 struct ext2_dir_entry {
286 unsigned int inode; /* Inode number */
287 unsigned short rec_len; /* Directory entry length */
288 unsigned char name_len; /* Name length */
289 unsigned char file_type;
290 char name[EXT2_NAME_LEN]; /* File name */
293 #define EXT2_DIR_PAD 4
294 #define EXT2_DIR_ROUND (EXT2_DIR_PAD - 1)
295 #define EXT2_DIR_REC_LEN(name_len) (((name_len) + 8 + EXT2_DIR_ROUND) & \
296 ~EXT2_DIR_ROUND)
298 //extern struct ext2_dir_entry * ext2_next_entry(struct ext2_dir_entry *);
303 /* namei.c */
304 extern void ext2_add_entry(struct m_inode *, struct ext2_dir_entry *);
307 /* inode.c */
308 extern void ext2_iput(struct m_inode *);
310 /* dir.c */
311 extern void mkdir(char *);
313 #endif /* fs.ext2.h */