summaryrefslogtreecommitdiff
path: root/kernel/fs/ext2.h
blob: 55fcbe146492b43262fd874c9bcf352521e7f239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef EXT2_H
#define EXT2_H
#include <drivers/ata.h>
#include <fs/vfs.h>
#include <kmalloc.h>
#include <typedefs.h>

typedef struct Superblock {
  u32 num_inodes;
  u32 num_blocks;
  u32 num_blocks_reserved;
  u32 num_blocks_unallocated;
  u32 num_inodes_unallocated;
  u32 superblock_block_num;
  u32 block_size;
  u32 fragment_size;
  u32 num_blocks_in_group;
  u32 num_fragments_in_group;
  u32 num_inodes_in_group;
  u32 last_mount;
  u32 last_write;
  u16 num_mounts_since_fsck;
  u16 num_mounts_allowed;
  u16 ext2_signature; // 0xEF53
  u16 fs_state;
  u16 when_error;
  u16 minor_version;
  u32 last_fsck;
  u32 interval_fsck;
  u32 os_id;
  u32 major_version;
  u16 userid_reserved_blocks;
  u16 groupid_reserved_blocks;
} __attribute__((packed)) superblock_t;

typedef struct ExtendedSuperblock {
  u32 num_inodes;
  u32 num_blocks;
  u32 num_blocks_reserved;
  u32 num_blocks_unallocated;
  u32 num_inodes_unallocated;
  u32 superblock_block_num;
  u32 block_size;
  u32 fragment_size;
  u32 num_blocks_group;
  u32 num_fragments_group;
  u32 num_inodes_group;
  u32 last_mount;
  u32 last_write;
  u16 num_mounts_since_fsck;
  u16 num_mounts_allowed;
  u16 ext2_signature; // 0xEF53
  u16 fs_state;
  u16 when_error;
  u16 minor_version;
  u32 last_fsck;
  u32 interval_fsck;
  u32 os_id;
  u32 major_version;
  u16 userid_reserved_blocks;
  u16 groupid_reserved_blocks;
  u32 pad;
  u16 inode_size;
} __attribute__((packed)) ext_superblock_t;

typedef struct BlockGroupDescriptorTable {
  u32 block_usage_bitmap;
  u32 inode_usage_bitmap;
  u32 starting_inode_table;
  u16 num_unallocated_blocks_in_group;
  u16 num_unallocated_inodes_in_group;
  u16 num_directories_group;
  u16 padding;
  u8 reserved[12];
} __attribute__((packed)) bgdt_t;

typedef struct INode {
  u16 types_permissions;
  u16 user_id;
  u32 low_32size;
  u32 last_access_time;
  u32 creation_time;
  u32 last_modification_time;
  u32 deletion_time;
  u16 group_id;
  u16 num_hard_links;
  u32 num_disk_sectors;
  u32 flags;
  u32 os_specific;
  u32 block_pointers[12];
  u32 single_indirect_block_pointer;
  u32 double_indirect_block_pointer;
  u32 triple_indirect_block_pointer;
  u32 gen_number;
  u32 _extended_attribute_block;
  u32 _upper_32size;
  u32 address_fragment;
  u32 os_specific2;
} __attribute__((packed)) inode_t;

// 0    Unknown type
// 1    Regular file
// 2    Directory
// 3    Character device
// 4    Block device
// 5    FIFO
// 6    Socket
// 7    Symbolic link (soft link)
#define TYPE_INDICATOR_UNKOWN 0
#define TYPE_INDICATOR_REGULAR 1
#define TYPE_INDICATOR_DIRECTORY 2
#define TYPE_INDICATOR_CHARACTER_DEVICE 3
#define TYPE_INDICATOR_BLOCK_DEVICE 4
#define TYPE_INDICATOR_FIFO 5
#define TYPE_INDICATOR_SOCKET 6
#define TYPE_INDICATOR_SOFT_LINK 7

#define FIFO 0x1000
#define CHARACTER_DEVICE 0x2000
#define DIRECTORY 0x4000
#define BLOCK_DEVICE 0x6000
#define REGULAR_FILE 0x8000
#define SYMBOLIC_LINK 0xA000
#define UNIX_SOCKET 0xC000

typedef struct DirectoryEntryHeader {
  u32 inode;
  u16 size;
  u8 name_length;
  u8 type_indicator;
} __attribute__((packed)) direntry_header_t;

int ext2_create_file(const char *path, int mode);
vfs_inode_t *ext2_mount(void);
void parse_superblock(void);
size_t ext2_read_file_offset(const char *file, u8 *data, u64 size, u64 offset,
                             u64 *file_size);
size_t ext2_read_file(const char *file, u8 *data, size_t size, u64 *file_size);
int ext2_create_directory(const char *path, int mode);
#endif