The Pedigree Project 0.1
ext2.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef EXT2_H
21#define EXT2_H
22
23#include "pedigree/kernel/processor/types.h"
24
25#define EXT2_UNKNOWN 0x0
26#define EXT2_FILE 0x1
27#define EXT2_DIRECTORY 0x2
28#define EXT2_CHAR_DEV 0x3
29#define EXT2_BLOCK_DEV 0x4
30#define EXT2_FIFO 0x5
31#define EXT2_SOCKET 0x6
32#define EXT2_SYMLINK 0x7
33#define EXT2_MAX 0x8
34
35#define EXT2_STATE_CLEAN 1
36#define EXT2_STATE_UNCLEAN 2
37
38#define EXT2_S_IFSOCK 0xC000
39#define EXT2_S_IFLNK 0xA000
40#define EXT2_S_IFREG 0x8000
41#define EXT2_S_IFBLK 0x6000
42#define EXT2_S_IFDIR 0x4000
43#define EXT2_S_IFCHR 0x2000
44#define EXT2_S_IFIFO 0x1000
45
46#define EXT2_S_IRUSR 0x0100
47#define EXT2_S_IWUSR 0x0080
48#define EXT2_S_IXUSR 0x0040
49#define EXT2_S_IRGRP 0x0020
50#define EXT2_S_IWGRP 0x0010
51#define EXT2_S_IXGRP 0x0008
52#define EXT2_S_IROTH 0x0004
53#define EXT2_S_IWOTH 0x0002
54#define EXT2_S_IXOTH 0x0001
55
56#define EXT2_BAD_INO 0x01 // Bad blocks inode
57#define EXT2_ROOT_INO 0x02 // root directory inode
58#define EXT2_ACL_IDX_INO 0x03 // ACL index inode (deprecated?)
59#define EXT2_ACL_DATA_INO 0x04 // ACL data inode (deprecated?)
60#define EXT2_BOOT_LOADER_INO 0x05 // boot loader inode
61#define EXT2_UNDEL_DIR_INO 0x06
62
63#define EXT2_LZV1_ALG 0x01
64#define EXT2_LZRW3A_ALG 0x02
65#define EXT2_GZIP_ALG 0x04
66#define EXT2_BZIP2_ALG 0x08
67#define EXT2_LZO_ALG 0x10
68
69#define EXT2_SECRM_FL 0x00000001
70#define EXT2_UNRM_FL 0x00000002
71#define EXT2_COMPR_FL 0x00000004
72#define EXT2_SYNC_FL 0x00000008
73#define EXT2_IMMUTABLE_FL 0x00000010
74#define EXT2_APPEND_FL 0x00000020
75#define EXT2_NODUMP_FL 0x00000040
76#define EXT2_NOATIME_FL 0x00000080
77#define EXT2_DIRTY_FL 0x00000100
78#define EXT2_COMPRBLK_FL 0x00000200
79#define EXT2_NOCOMPR_FL 0x00000400
80#define EXT2_ECOMPR_FL 0x00000800
81#define EXT2_BTREE_FL 0x00001000
82#define EXT2_INDEX_FL 0x00001000
83#define EXT2_IMAGIC_FL 0x00002000
84#define EXT3_JOURNAL_DATA_FL 0x00004000
85#define EXT2_RESERVED_FL 0x80000000
86
88struct Superblock {
89 uint32_t s_inodes_count;
90 uint32_t s_blocks_count;
91 uint32_t s_r_blocks_count;
92 uint32_t s_free_blocks_count;
93 uint32_t s_free_inodes_count;
94 uint32_t s_first_data_block;
95 uint32_t s_log_block_size;
96 uint32_t s_log_frag_size;
97 uint32_t s_blocks_per_group;
98 uint32_t s_frags_per_group;
99 uint32_t s_inodes_per_group;
100 uint32_t s_mtime;
101 uint32_t s_wtime;
102 uint16_t s_mnt_count;
103 uint16_t s_max_mnt_count;
104 uint16_t s_magic;
105 uint16_t s_state;
106 uint16_t s_errors;
107 uint16_t s_minor_rev_level;
108 uint32_t s_lastcheck;
109 uint32_t s_checkinterval;
110 uint32_t s_creator_os;
111 uint32_t s_rev_level;
112 uint16_t s_def_resuid;
113 uint16_t s_def_resgid;
114 // -- EXT2_DYNAMIC_REV Specific --
115 uint32_t s_first_ino;
116 uint16_t s_inode_size;
117 uint16_t s_block_group_nr;
118 uint32_t s_feature_compat;
119 uint32_t s_feature_incompat;
120 uint32_t s_feature_ro_compat;
121 char s_uuid[16];
122
123 char s_volume_name[16];
124 char s_last_mounted[64];
125 uint32_t s_algo_bitmap;
126 // -- Performance Hints --
127 uint8_t s_prealloc_blocks;
128 uint8_t s_prealloc_dir_blocks;
129 uint16_t alignment;
130 // -- Journaling Support --
131 char s_journal_uuid[16];
132 uint32_t s_journal_inum;
133 uint32_t s_journal_dev;
134 uint32_t s_last_orphan;
135} __attribute__((packed));
136
138struct GroupDesc {
139 uint32_t bg_block_bitmap;
140 uint32_t bg_inode_bitmap;
141 uint32_t bg_inode_table;
142 uint16_t bg_free_blocks_count;
143 uint16_t bg_free_inodes_count;
144 uint16_t bg_used_dirs_count;
145 uint16_t bg_pad;
146 uint8_t bg_reserved[12];
147} __attribute__((packed));
148
149constexpr uint32_t EXT2_FEATURE_COMPAT_EXT_ATTR = 0x8;
150
152struct Inode {
153 uint16_t i_mode;
154 uint16_t i_uid;
155 uint32_t i_size;
156 uint32_t i_atime;
157 uint32_t i_ctime;
158 uint32_t i_mtime;
159 uint32_t i_dtime;
160 uint16_t i_gid;
161 uint16_t i_links_count;
162 uint32_t i_blocks;
163 uint32_t i_flags;
164 uint32_t i_osd1;
165 uint32_t i_block[15];
166 uint32_t i_generation;
167 uint32_t i_file_acl;
168 uint32_t i_dir_acl; // Top 32-bits of file size.
169 uint32_t i_faddr;
170 uint8_t i_osd2[12];
171} __attribute__((packed));
172
173// Linux/Hurd encode the high owner halves in these two OS-dependent fields.
174namespace Ext2Owner {
175inline uint16_t read16(const void* field) {
176 const auto* bytes = static_cast<const uint8_t*>(field);
177 return static_cast<uint16_t>(bytes[0]) | (static_cast<uint16_t>(bytes[1]) << 8);
178}
179inline void write16(void* field, uint16_t value) {
180 auto* bytes = static_cast<uint8_t*>(field);
181 bytes[0] = value;
182 bytes[1] = value >> 8;
183}
184inline uint32_t uid(const Inode& inode) {
185 return read16(&inode.i_uid) | (static_cast<uint32_t>(read16(inode.i_osd2 + 4)) << 16);
186}
187inline uint32_t gid(const Inode& inode) {
188 return read16(&inode.i_gid) | (static_cast<uint32_t>(read16(inode.i_osd2 + 6)) << 16);
189}
190inline void setUid(Inode& inode, uint32_t value) {
191 write16(&inode.i_uid, value);
192 write16(inode.i_osd2 + 4, value >> 16);
193}
194inline void setGid(Inode& inode, uint32_t value) {
195 write16(&inode.i_gid, value);
196 write16(inode.i_osd2 + 6, value >> 16);
197}
198} // namespace Ext2Owner
199
201struct Dir {
202 uint32_t d_inode;
203 uint16_t d_reclen;
204 uint8_t d_namelen;
205 uint8_t d_file_type;
206 char d_name[256];
207} __attribute__((packed));
208
209#endif
Definition ext2.h:201
Definition ext2.h:152