[EXT2LIB]
[reactos.git] / reactos / lib / fslib / ext2lib / Mke2fs.h
1 /*
2 * PROJECT: Mke2fs
3 * FILE: Mke2fs.h
4 * PROGRAMMER: Matt Wu <mattwu@163.com>
5 * HOMEPAGE: http://ext2.yeah.net
6 */
7
8 /* INCLUDES **************************************************************/
9
10 #define NTOS_MODE_USER
11 #include <ndk/kefuncs.h>
12 #include <ndk/rtlfuncs.h>
13
14 #include "ext2_fs.h"
15
16 /* DEFINITIONS ***********************************************************/
17
18 #define SECTOR_SIZE (Ext2Sys->DiskGeometry.BytesPerSector)
19
20 #ifndef GUID_DEFINED
21 #define GUID_DEFINED
22 typedef struct _GUID
23 {
24 unsigned long Data1;
25 unsigned short Data2;
26 unsigned short Data3;
27 unsigned char Data4[8];
28 } GUID;
29 #endif /* GUID_DEFINED */
30
31 #ifndef UUID_DEFINED
32 #define UUID_DEFINED
33 typedef GUID UUID;
34 #ifndef uuid_t
35 #define uuid_t UUID
36 #endif
37 #endif
38
39 #ifndef bool
40 #define bool BOOLEAN
41 #endif
42
43 #ifndef true
44 #define true TRUE
45 #endif
46
47 #ifndef false
48 #define false FALSE
49 #endif
50
51
52 #define EXT2_CHECK_MAGIC(struct, code) \
53 if ((struct)->magic != (code)) return (code)
54
55 /*
56 * ext2fs_scan flags
57 */
58 #define EXT2_SF_CHK_BADBLOCKS 0x0001
59 #define EXT2_SF_BAD_INODE_BLK 0x0002
60 #define EXT2_SF_BAD_EXTRA_BYTES 0x0004
61 #define EXT2_SF_SKIP_MISSING_ITABLE 0x0008
62
63 /*
64 * ext2fs_check_if_mounted flags
65 */
66 #define EXT2_MF_MOUNTED 1
67 #define EXT2_MF_ISROOT 2
68 #define EXT2_MF_READONLY 4
69 #define EXT2_MF_SWAP 8
70
71 /*
72 * Ext2/linux mode flags. We define them here so that we don't need
73 * to depend on the OS's sys/stat.h, since we may be compiling on a
74 * non-Linux system.
75 */
76
77 #define LINUX_S_IFMT 00170000
78 #define LINUX_S_IFSOCK 0140000
79 #define LINUX_S_IFLNK 0120000
80 #define LINUX_S_IFREG 0100000
81 #define LINUX_S_IFBLK 0060000
82 #define LINUX_S_IFDIR 0040000
83 #define LINUX_S_IFCHR 0020000
84 #define LINUX_S_IFIFO 0010000
85 #define LINUX_S_ISUID 0004000
86 #define LINUX_S_ISGID 0002000
87 #define LINUX_S_ISVTX 0001000
88
89 #define LINUX_S_IRWXU 00700
90 #define LINUX_S_IRUSR 00400
91 #define LINUX_S_IWUSR 00200
92 #define LINUX_S_IXUSR 00100
93
94 #define LINUX_S_IRWXG 00070
95 #define LINUX_S_IRGRP 00040
96 #define LINUX_S_IWGRP 00020
97 #define LINUX_S_IXGRP 00010
98
99 #define LINUX_S_IRWXO 00007
100 #define LINUX_S_IROTH 00004
101 #define LINUX_S_IWOTH 00002
102 #define LINUX_S_IXOTH 00001
103
104 #define LINUX_S_ISLNK(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFLNK)
105 #define LINUX_S_ISREG(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFREG)
106 #define LINUX_S_ISDIR(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFDIR)
107 #define LINUX_S_ISCHR(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFCHR)
108 #define LINUX_S_ISBLK(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFBLK)
109 #define LINUX_S_ISFIFO(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFIFO)
110 #define LINUX_S_ISSOCK(m) (((m) & LINUX_S_IFMT) == LINUX_S_IFSOCK)
111
112
113 #define EXT2_FIRST_INODE(s) EXT2_FIRST_INO(s)
114
115 typedef struct _ext2fs_bitmap {
116 __u32 start, end;
117 __u32 real_end;
118 char* bitmap;
119 } EXT2_BITMAP, *PEXT2_BITMAP;
120
121 typedef EXT2_BITMAP EXT2_GENERIC_BITMAP, *PEXT2_GENERIC_BITMAP;
122 typedef EXT2_BITMAP EXT2_INODE_BITMAP, *PEXT2_INODE_BITMAP;
123 typedef EXT2_BITMAP EXT2_BLOCK_BITMAP, *PEXT2_BLOCK_BITMAP;
124
125 typedef struct ext2_acl_entry EXT2_ACL_ENTRY, *PEXT2_ACL_ENTRY;
126 typedef struct ext2_acl_header EXT2_ACL_HEADER, *PEXT2_ACL_HEADER;
127 typedef struct ext2_dir_entry EXT2_DIR_ENTRY, *PEXT2_DIR_ENTRY;
128 typedef struct ext2_dir_entry_2 EXT2_DIR_ENTRY2, *PEXT2_DIR_ENTRY2;
129 typedef struct ext2_dx_countlimit EXT2_DX_CL, *PEXT2_DX_CL;
130 typedef struct ext2_dx_entry EXT2_DX_ENTRY, *PEXT2_DX_ENTRY;
131 typedef struct ext2_dx_root_info EXT2_DX_RI, *PEXT2_DX_RI;
132 typedef struct ext2_inode EXT2_INODE, *PEXT2_INODE;
133 typedef struct ext2_group_desc EXT2_GROUP_DESC, *PEXT2_GROUP_DESC;
134 typedef struct ext2_super_block EXT2_SUPER_BLOCK, *PEXT2_SUPER_BLOCK;
135
136 /*
137 * Badblocks list
138 */
139 struct ext2_struct_badblocks_list {
140 int num;
141 int size;
142 ULONG *list;
143 int badblocks_flags;
144 };
145
146 typedef struct ext2_struct_badblocks_list EXT2_BADBLK_LIST, *PEXT2_BADBLK_LIST;
147
148 typedef struct _ext2_filesys
149 {
150 int flags;
151 int blocksize;
152 int fragsize;
153 ULONG group_desc_count;
154 unsigned long desc_blocks;
155 PEXT2_GROUP_DESC group_desc;
156 PEXT2_SUPER_BLOCK ext2_sb;
157 unsigned long inode_blocks_per_group;
158 PEXT2_INODE_BITMAP inode_map;
159 PEXT2_BLOCK_BITMAP block_map;
160
161 EXT2_BADBLK_LIST badblocks;
162 /*
163 ext2_dblist dblist;
164 */
165 __u32 stride; /* for mke2fs */
166
167 __u32 umask;
168
169 /*
170 * Reserved for future expansion
171 */
172 __u32 reserved[8];
173
174 /*
175 * Reserved for the use of the calling application.
176 */
177 void * priv_data;
178
179 HANDLE MediaHandle;
180
181 DISK_GEOMETRY DiskGeometry;
182
183 PARTITION_INFORMATION PartInfo;
184
185 } EXT2_FILESYS, *PEXT2_FILESYS;
186
187 // Block Description List
188 typedef struct _EXT2_BDL {
189 LONGLONG Lba;
190 ULONG Offset;
191 ULONG Length;
192 } EXT2_BDL, *PEXT2_BDL;
193
194 /*
195 * Where the master copy of the superblock is located, and how big
196 * superblocks are supposed to be. We define SUPERBLOCK_SIZE because
197 * the size of the superblock structure is not necessarily trustworthy
198 * (some versions have the padding set up so that the superblock is
199 * 1032 bytes long).
200 */
201 #define SUPERBLOCK_OFFSET 1024
202 #define SUPERBLOCK_SIZE 1024
203
204
205 bool create_bad_block_inode(PEXT2_FILESYS fs, PEXT2_BADBLK_LIST bb_list);
206
207
208 /*
209 * Bitmap.c
210 */
211
212 #define ext2_mark_block_bitmap ext2_mark_bitmap
213 #define ext2_mark_inode_bitmap ext2_mark_bitmap
214 #define ext2_unmark_block_bitmap ext2_unmark_bitmap
215 #define ext2_unmark_inode_bitmap ext2_unmark_bitmap
216
217 bool ext2_set_bit(int nr, void * addr);
218 bool ext2_clear_bit(int nr, void * addr);
219 bool ext2_test_bit(int nr, void * addr);
220
221 bool ext2_mark_bitmap(PEXT2_BITMAP bitmap, ULONG bitno);
222 bool ext2_unmark_bitmap(PEXT2_BITMAP bitmap, ULONG bitno);
223
224 bool ext2_test_block_bitmap(PEXT2_BLOCK_BITMAP bitmap,
225 ULONG block);
226
227 bool ext2_test_block_bitmap_range(PEXT2_BLOCK_BITMAP bitmap,
228 ULONG block, int num);
229
230 bool ext2_test_inode_bitmap(PEXT2_BLOCK_BITMAP bitmap,
231 ULONG inode);
232
233
234 bool ext2_allocate_block_bitmap(PEXT2_FILESYS pExt2Sys);
235 bool ext2_allocate_inode_bitmap(PEXT2_FILESYS pExt2Sys);
236 void ext2_free_inode_bitmap(PEXT2_FILESYS pExt2Sys);
237 void ext2_free_block_bitmap(PEXT2_FILESYS pExt2Sys);
238
239 bool ext2_write_block_bitmap (PEXT2_FILESYS fs);
240 bool ext2_write_inode_bitmap (PEXT2_FILESYS fs);
241
242 bool ext2_write_bitmaps(PEXT2_FILESYS fs);
243
244 //bool read_bitmaps(PEXT2_FILESYS fs, int do_inode, int do_block);
245 bool ext2_read_inode_bitmap (PEXT2_FILESYS fs);
246 bool ext2_read_block_bitmap(PEXT2_FILESYS fs);
247 bool ext2_read_bitmaps(PEXT2_FILESYS fs);
248
249
250 /*
251 * Disk.c
252 */
253
254 NTSTATUS
255 Ext2OpenDevice( PEXT2_FILESYS Ext2Sys,
256 PUNICODE_STRING DeviceName );
257
258 NTSTATUS
259 Ext2CloseDevice( PEXT2_FILESYS Ext2Sys);
260
261 NTSTATUS
262 Ext2ReadDisk( PEXT2_FILESYS Ext2Sys,
263 ULONGLONG Offset,
264 ULONG Length,
265 PVOID Buffer );
266
267 NTSTATUS
268 Ext2WriteDisk( PEXT2_FILESYS Ext2Sys,
269 ULONGLONG Offset,
270 ULONG Length,
271 PVOID Buffer );
272
273 NTSTATUS
274 Ext2GetMediaInfo( PEXT2_FILESYS Ext2Sys );
275
276
277 NTSTATUS
278 Ext2LockVolume( PEXT2_FILESYS Ext2Sys );
279
280 NTSTATUS
281 Ext2UnLockVolume( PEXT2_FILESYS Ext2Sys );
282
283 NTSTATUS
284 Ext2DisMountVolume( PEXT2_FILESYS Ext2Sys );
285
286
287 /*
288 * Group.c
289 */
290
291 bool ext2_allocate_group_desc(PEXT2_FILESYS pExt2Sys);
292 void ext2_free_group_desc(PEXT2_FILESYS pExt2Sys);
293 bool ext2_bg_has_super(PEXT2_SUPER_BLOCK pExt2Sb, int group_block);
294
295 /*
296 * Inode.c
297 */
298
299 bool ext2_get_inode_lba(PEXT2_FILESYS pExt2Sys, ULONG no, LONGLONG *offset);
300 bool ext2_load_inode(PEXT2_FILESYS pExt2Sys, ULONG no, PEXT2_INODE pInode);
301 bool ext2_save_inode(PEXT2_FILESYS pExt2Sys, ULONG no, PEXT2_INODE pInode);
302 bool ext2_new_inode(PEXT2_FILESYS fs, ULONG dir, int mode,
303 PEXT2_INODE_BITMAP map, ULONG *ret);
304 bool ext2_expand_inode(PEXT2_FILESYS pExt2Sys, PEXT2_INODE, ULONG newBlk);
305
306 bool ext2_read_inode (PEXT2_FILESYS pExt2Sys,
307 ULONG ino,
308 ULONG offset,
309 PVOID Buffer,
310 ULONG size,
311 PULONG dwReturn );
312 bool ext2_write_inode (PEXT2_FILESYS pExt2Sys,
313 ULONG ino,
314 ULONG offset,
315 PVOID Buffer,
316 ULONG size,
317 PULONG dwReturn );
318
319 bool ext2_add_entry(PEXT2_FILESYS pExt2Sys, ULONG parent, ULONG inode, int filetype, char *name);
320 bool ext2_reserve_inodes(PEXT2_FILESYS fs);
321 /*
322 * Memory.c
323 */
324
325 //
326 // Return the group # of an inode number
327 //
328 int ext2_group_of_ino(PEXT2_FILESYS fs, ULONG ino);
329
330 //
331 // Return the group # of a block
332 //
333 int ext2_group_of_blk(PEXT2_FILESYS fs, ULONG blk);
334
335 /*
336 * Badblock.c
337 */
338
339
340 void ext2_inode_alloc_stats2(PEXT2_FILESYS fs, ULONG ino, int inuse, int isdir);
341 void ext2_inode_alloc_stats(PEXT2_FILESYS fs, ULONG ino, int inuse);
342 void ext2_block_alloc_stats(PEXT2_FILESYS fs, ULONG blk, int inuse);
343
344 bool ext2_allocate_tables(PEXT2_FILESYS pExt2Sys);
345 bool ext2_allocate_group_table(PEXT2_FILESYS fs, ULONG group,
346 PEXT2_BLOCK_BITMAP bmap);
347 bool ext2_get_free_blocks(PEXT2_FILESYS fs, ULONG start, ULONG finish,
348 int num, PEXT2_BLOCK_BITMAP map, ULONG *ret);
349 bool write_inode_tables(PEXT2_FILESYS fs);
350
351 bool ext2_new_block(PEXT2_FILESYS fs, ULONG goal,
352 PEXT2_BLOCK_BITMAP map, ULONG *ret);
353 bool ext2_alloc_block(PEXT2_FILESYS fs, ULONG goal, ULONG *ret);
354 bool ext2_new_dir_block(PEXT2_FILESYS fs, ULONG dir_ino,
355 ULONG parent_ino, char **block);
356 bool ext2_write_block(PEXT2_FILESYS fs, ULONG block, void *inbuf);
357 bool ext2_read_block(PEXT2_FILESYS fs, ULONG block, void *inbuf);
358
359 /*
360 * Mke2fs.c
361 */
362
363 bool parase_cmd(int argc, char *argv[], PEXT2_FILESYS pExt2Sys);
364
365 bool zero_blocks(PEXT2_FILESYS fs, ULONG blk, ULONG num,
366 ULONG *ret_blk, ULONG *ret_count);
367
368 ULONG
369 Ext2DataBlocks(PEXT2_FILESYS Ext2Sys, ULONG TotalBlocks);
370
371 ULONG
372 Ext2TotalBlocks(PEXT2_FILESYS Ext2Sys, ULONG DataBlocks);
373
374
375
376 /*
377 * Super.c
378 */
379
380 void ext2_print_super(PEXT2_SUPER_BLOCK pExt2Sb);
381 bool ext2_initialize_sb(PEXT2_FILESYS pExt2Sys);
382
383
384 /*
385 * Super.c
386 */
387
388 LONGLONG ext2_nt_time (ULONG i_time);
389 ULONG ext2_unix_time (LONGLONG n_time);
390
391 /*
392 * Uuid.c
393 */
394
395 void uuid_generate(__u8 * uuid);