1 /*
2  * Copyright (c) 2023 Antmicro <www.antmicro.com>
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef __EXT2_H__
8 #define __EXT2_H__
9 
10 #define EXT2_SUPERBLOCK_OFFSET 1024
11 #define EXT2_MAGIC_NUMBER 0xEF53
12 #define EXT2_MAX_FILE_NAME 255
13 #define EXT2_ROOT_INODE 2
14 #define EXT2_RESERVED_INODES 10
15 #define EXT2_GOOD_OLD_INODE_SIZE 128
16 
17 #define EXT2_FEATURE_INCOMPAT_COMPRESSION 0x0001 /* Disk/File compression is used */
18 #define EXT2_FEATURE_INCOMPAT_FILETYPE    0x0002 /* Directory entries record the file type */
19 #define EXT3_FEATURE_INCOMPAT_RECOVER     0x0004 /* Filesystem needs recovery */
20 #define EXT3_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Filesystem has a separate journal device */
21 #define EXT2_FEATURE_INCOMPAT_META_BG     0x0010 /* Meta block groups */
22 
23 #define EXT2_FEATURE_INCOMPAT_SUPPORTED (EXT2_FEATURE_INCOMPAT_FILETYPE)
24 
25 #define EXT2_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001 /* Sparse Superblock */
26 #define EXT2_FEATURE_RO_COMPAT_LARGE_FILE   0x0002 /* Large file support, 64-bit file size */
27 #define EXT2_FEATURE_RO_COMPAT_BTREE_DIR    0x0004 /* Binary tree sorted directory files */
28 
29 #define EXT2_FEATURE_RO_COMPAT_SUPPORTED (0)
30 
31 #define EXT2_INODE_BLOCKS 15 /* number of blocks referenced by inode i_block field */
32 #define EXT2_INODE_BLOCK_DIRECT 12
33 #define EXT2_INODE_BLOCK_1LVL 12
34 #define EXT2_INODE_BLOCK_2LVL 13
35 #define EXT2_INODE_BLOCK_3LVL 14
36 
37 /* Inode mode flags */
38 #define EXT2_S_IFMT   0xF000 /* format mask */
39 #define EXT2_S_IFSOCK 0xC000 /* socket */
40 #define EXT2_S_IFLNK  0xA000 /* symbolic link */
41 #define EXT2_S_IFREG  0x8000 /* regular file */
42 #define EXT2_S_IFBLK  0x6000 /* block device */
43 #define EXT2_S_IFDIR  0x4000 /* directory */
44 #define EXT2_S_IFCHR  0x2000 /* character device */
45 #define EXT2_S_IFIFO  0x1000 /* fifo */
46 
47 #define EXT2_S_IRUSR  0x100 /* owner may read */
48 #define EXT2_S_IWUSR  0x080 /* owner may write */
49 #define EXT2_S_IXUSR  0x040 /* owner may execute */
50 #define EXT2_S_IRGRP  0x020 /* group members may read */
51 #define EXT2_S_IWGRP  0x010 /* group members may write */
52 #define EXT2_S_IXGRP  0x008 /* group members may execute */
53 #define EXT2_S_IROTH  0x004 /* others may read */
54 #define EXT2_S_IWOTH  0x002 /* others may write */
55 #define EXT2_S_IXOTH  0x001 /* others may execute */
56 
57 /* Default file mode: rw-r--r-- */
58 #define EXT2_DEF_FILE_MODE             \
59 	(EXT2_S_IFREG |                \
60 	 EXT2_S_IRUSR | EXT2_S_IWUSR | \
61 	 EXT2_S_IRGRP |                \
62 	 EXT2_S_IROTH)
63 
64 /* Default dir mode: rwxr-xr-x */
65 #define EXT2_DEF_DIR_MODE                             \
66 	(EXT2_S_IFDIR |                               \
67 	 EXT2_S_IRUSR | EXT2_S_IWUSR | EXT2_S_IXUSR | \
68 	 EXT2_S_IRGRP | EXT2_S_IXGRP |                \
69 	 EXT2_S_IROTH | EXT2_S_IXOTH)
70 
71 #define IS_REG_FILE(mode) (((mode) & EXT2_S_IFMT) == EXT2_S_IFREG)
72 #define IS_DIR(mode) (((mode) & EXT2_S_IFMT) == EXT2_S_IFDIR)
73 
74 /* Directory file type flags */
75 #define EXT2_FT_UNKNOWN  0
76 #define EXT2_FT_REG_FILE 1
77 #define EXT2_FT_DIR      2
78 #define EXT2_FT_CHRDEV   3
79 #define EXT2_FT_BLKDEV   4
80 #define EXT2_FT_FIFO     5
81 #define EXT2_FT_SOCK     6
82 #define EXT2_FT_SYMLINK  7
83 #define EXT2_FT_MAX      8
84 
85 /* Superblock status flags.
86  * When file system is mounted the status is set to EXT2_ERROR_FS.
87  * When file system is cleanly unmounted then flag is reset to EXT2_VALID_FS.
88  */
89 #define EXT2_VALID_FS 0x0001 /* Unmounted cleanly */
90 #define EXT2_ERROR_FS 0x0002 /* Errors detected */
91 
92 /* Revision flags. */
93 #define EXT2_GOOD_OLD_REV 0x0 /* Revision 0 */
94 #define EXT2_DYNAMIC_REV 0x1  /* Revision 1 */
95 
96 /* Strategy when error detected. */
97 #define EXT2_ERRORS_CONTINUE 1 /* Continue as if nothing happened. */
98 #define EXT2_ERRORS_RO       2 /* Mount read only. */
99 #define EXT2_ERRORS_PANIC    3 /* Cause kernel panic. */
100 
101 #endif /* __EXT2_H__ */
102