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