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