AquilaOS documentation
How it works, and its code
Loading...
Searching...
No Matches
ext2.h
Go to the documentation of this file.
1#ifndef _EXT2_H
2#define _EXT2_H
3
4/*
5 * The second extended filesystem, ext2. The disk is cut into block
6 * groups; each has a block bitmap, an inode bitmap and part of the inode
7 * table, and the block group descriptor table (after the superblock)
8 * says where they are. An inode has 12 direct blocks, then a singly, a
9 * doubly and a triply indirect one. A directory's blocks hold entries of
10 * different sizes, which make a list in each block.
11 */
12
13#include <core/system.h>
14#include <fs/vfs.h>
15
16#define EXT2_SIGNATURE 0xEF53
17
18/* revision 1's features: compatible ones may be ignored; an incompatible
19 * one that the code doesn't know means it can't use the filesystem, and a
20 * read-only compatible one means it can't write to it */
21#define EXT2_INCOMPAT_FILETYPE 0x0002 /* the entries' types */
22#define EXT2_RO_COMPAT_SPARSE_SUPER 0x0001 /* backup superblocks in fewer groups */
23#define EXT2_RO_COMPAT_LARGE_FILE 0x0002 /* sizes past 2 GiB (size_high) */
24
25/* the features AquilaOS's ext2 handles: those mke2fs -t ext2 uses */
26#define EXT2_INCOMPAT_SUPPORTED EXT2_INCOMPAT_FILETYPE
27#define EXT2_RO_COMPAT_SUPPORTED (EXT2_RO_COMPAT_SPARSE_SUPER | EXT2_RO_COMPAT_LARGE_FILE)
28
29/* revision 0's superblock has no inode size or first inode: they are
30 * fixed */
31#define EXT2_GOOD_OLD_INODE_SIZE 128
32
96
112
139
154
155/* the entry types, with EXT2_INCOMPAT_FILETYPE */
156#define EXT2_DENTRY_TYPE_BAD 0
157#define EXT2_DENTRY_TYPE_RGL 1
158#define EXT2_DENTRY_TYPE_DIR 2
159#define EXT2_DENTRY_TYPE_CHR 3
160#define EXT2_DENTRY_TYPE_BLK 4
161#define EXT2_DENTRY_TYPE_FIFO 5
162#define EXT2_DENTRY_TYPE_SCKT 6
163#define EXT2_DENTRY_TYPE_SLINK 7
164
165#define EXT2_DIRECT_POINTERS 12
166
168struct ext2 {
169 struct vnode *supernode; /* the device it's on */
171 struct ext2_group *groups; /* the block group descriptor table */
172
174 size_t ngroups;
175
177 size_t bs;
178
181
185
187 struct vcache *vcache;
188};
189
190extern struct fs ext2fs;
191
192/* the functions that read or write the disk return 0, or a negative
193 * errno: -EIO if not all of it was read or written */
194
195/* super.c */
196int ext2_superblock_rewrite(struct ext2 *desc);
197
198/* block.c */
199int ext2_io(struct ext2 *desc, int write, off_t off, size_t size, void *buf);
200int ext2_bgd_table_rewrite(struct ext2 *desc);
201int ext2_block_read(struct ext2 *desc, uint32_t number, void *buf);
202int ext2_block_write(struct ext2 *desc, uint32_t number, void *buf);
203int ext2_block_alloc(struct ext2 *desc, uint32_t *block);
204int ext2_block_free(struct ext2 *desc, uint32_t block);
205
206/* inode.c */
207int ext2_inode_read(struct ext2 *desc, ino_t inode, struct ext2_inode *ref);
208int ext2_inode_write(struct ext2 *desc, ino_t inode, struct ext2_inode *i);
209
210int ext2_inode_block_read(struct ext2 *desc, struct ext2_inode *inode, size_t idx, void *buf);
211int ext2_inode_block_write(struct ext2 *desc, struct ext2_inode *inode, uint32_t inode_nr, size_t idx, void *buf);
212int ext2_inode_alloc(struct ext2 *desc, ino_t *ino);
213
214/* dentry.c */
215int ext2_dentry_find(struct ext2 *desc, struct ext2_inode *inode, const char *name, ino_t *ino);
216int ext2_dentry_create(struct vnode *dir, const char *name, ino_t ino, mode_t mode);
217
218/* vops.c and remove.c: the vnode operations */
219ssize_t ext2_read(struct vnode *node, off_t offset, struct iobuf *io);
220ssize_t ext2_write(struct vnode *node, off_t offset, struct iobuf *io);
221ssize_t ext2_readdir(struct vnode *dir, off_t offset, struct dirent *dirent);
222int ext2_finddir(struct vnode *dir, const char *fn, struct dirent *dirent);
223
224int ext2_trunc(struct vnode *inode, off_t len);
225int ext2_vmknod(struct vnode *dir, const char *fn, mode_t mode, dev_t dev, struct uio *uio, struct vnode **ref);
226
227int ext2_vget(struct vnode *super, ino_t ino, struct vnode **ref);
228int ext2_setattr(struct vnode *vnode);
229int ext2_close(struct vnode *vnode);
230int ext2_vunlink(struct vnode *dir, const char *fn, struct uio *uio);
231int ext2_vlink(struct vnode *dir, const char *fn, struct vnode *vnode, struct uio *uio);
232int ext2_vrename(struct vnode *odir, const char *ofn, struct vnode *ndir, const char *nfn, struct uio *uio);
233void ext2_blocks_free(struct ext2 *desc, struct ext2_inode *inode, size_t from);
234void ext2_inode_discard(struct ext2 *desc, ino_t ino);
235
236/* ext2.c: an inode's vnode (the cached one, or a new one), and writing a
237 * vnode back to its inode */
238int ext2_inode_build(struct ext2 *desc, ino_t ino, struct vnode **ref_inode);
239int ext2_inode_sync(struct vnode *inode);
240
241#endif
int ext2_block_free(struct ext2 *desc, uint32_t block)
Frees a block: its bit is cleared in its group's block bitmap, and the free counts go up.
Definition block.c:145
int ext2_block_write(struct ext2 *desc, uint32_t number, void *buf)
Writes a block to the disk.
Definition block.c:62
int ext2_io(struct ext2 *desc, int write, off_t off, size_t size, void *buf)
Reads or writes bytes of the filesystem's disk, all of them.
Definition block.c:19
int ext2_bgd_table_rewrite(struct ext2 *desc)
Writes the block group descriptor table back to the disk, after a change to a group's counts.
Definition block.c:34
int ext2_block_read(struct ext2 *desc, uint32_t number, void *buf)
Reads a block from the disk.
Definition block.c:49
int ext2_block_alloc(struct ext2 *desc, uint32_t *block)
Allocates a free block: its bit is set in its group's block bitmap, the free counts go down,...
Definition block.c:77
int ext2_dentry_create(struct vnode *dir, const char *name, ino_t ino, mode_t mode)
Adds an entry to a directory.
Definition dentry.c:87
int ext2_dentry_find(struct ext2 *desc, struct ext2_inode *inode, const char *name, ino_t *ino)
Finds the inode number of a name in a directory.
Definition dentry.c:20
struct fs ext2fs
Definition ext2.c:282
int ext2_inode_build(struct ext2 *desc, ino_t ino, struct vnode **ref_inode)
Gets the vnode of an inode: the cached one if it's in use, or a new one made from the inode on the di...
Definition ext2.c:29
int ext2_inode_sync(struct vnode *inode)
Writes a vnode's attributes back to its inode on the disk.
Definition ext2.c:93
ssize_t ext2_write(struct vnode *node, off_t offset, struct iobuf *io)
Writes to a file, which grows if the write goes past its end.
Definition vops.c:83
void ext2_blocks_free(struct ext2 *desc, struct ext2_inode *inode, size_t from)
Frees a file's blocks, from a block on, when the file is truncated or removed.
Definition remove.c:69
ssize_t ext2_read(struct vnode *node, off_t offset, struct iobuf *io)
Reads from a file.
Definition vops.c:23
int ext2_inode_alloc(struct ext2 *desc, ino_t *ino)
Allocates a free inode: its bit is set in its group's inode bitmap, and the free counts go down.
Definition inode.c:281
int ext2_vget(struct vnode *super, ino_t ino, struct vnode **ref)
Gets the vnode of an inode, by its number.
Definition vops.c:443
int ext2_close(struct vnode *vnode)
Releases a vnode when its last reference goes.
Definition remove.c:158
int ext2_inode_read(struct ext2 *desc, ino_t inode, struct ext2_inode *ref)
Reads an inode from the disk.
Definition inode.c:33
int ext2_vlink(struct vnode *dir, const char *fn, struct vnode *vnode, struct uio *uio)
Adds an entry for an existing file (a hard link) to a directory.
Definition remove.c:298
int ext2_trunc(struct vnode *inode, off_t len)
Changes a file's length.
Definition vops.c:252
int ext2_vrename(struct vnode *odir, const char *ofn, struct vnode *ndir, const char *nfn, struct uio *uio)
Renames an entry, possibly to another directory, replacing an entry at the new name (the VFS checked ...
Definition remove.c:337
void ext2_inode_discard(struct ext2 *desc, ino_t ino)
Frees an inode that has no links left, and the blocks it uses.
Definition remove.c:131
int ext2_inode_block_read(struct ext2 *desc, struct ext2_inode *inode, size_t idx, void *buf)
Reads a block of a file.
Definition inode.c:144
int ext2_vmknod(struct vnode *dir, const char *fn, mode_t mode, dev_t dev, struct uio *uio, struct vnode **ref)
Makes a node in a directory: a new inode, and an entry for it.
Definition vops.c:312
int ext2_inode_write(struct ext2 *desc, ino_t inode, struct ext2_inode *i)
Writes an inode to the disk.
Definition inode.c:51
int ext2_superblock_rewrite(struct ext2 *desc)
Writes the in-memory copy of the superblock back to the disk, after a change to its counts of free bl...
Definition super.c:14
ssize_t ext2_readdir(struct vnode *dir, off_t offset, struct dirent *dirent)
Reads a directory's entry at an offset; unused entries don't count.
Definition vops.c:174
int ext2_setattr(struct vnode *vnode)
Stores a vnode's attributes (mode, owner, times) in its inode.
Definition vops.c:456
int ext2_vunlink(struct vnode *dir, const char *fn, struct uio *uio)
Removes an entry from a directory, and drops a link to its file.
Definition remove.c:249
int ext2_finddir(struct vnode *dir, const char *fn, struct dirent *dirent)
Finds the entry of a directory that has a name.
Definition vops.c:414
int ext2_inode_block_write(struct ext2 *desc, struct ext2_inode *inode, uint32_t inode_nr, size_t idx, void *buf)
Writes a block of a file, allocating it and the indirect blocks above it if they don't exist yet.
Definition inode.c:235
#define __packed
Definition system.h:30
unsigned int uint32_t
Definition stddef.h:22
unsigned char uint8_t
Definition stddef.h:20
unsigned short int uint16_t
Definition stddef.h:21
A device driver: its operations, any of which may be NULL.
Definition dev.h:28
A directory entry, as it is on the disk.
Definition ext2.h:142
uint16_t size
entry total size
Definition ext2.h:146
uint8_t name[0]
the name follows (not NUL-terminated)
Definition ext2.h:152
uint32_t ino
inode number
Definition ext2.h:144
uint8_t type
entry type
Definition ext2.h:150
uint8_t length
name length
Definition ext2.h:148
A block group descriptor, as it is on the disk.
Definition ext2.h:98
uint8_t __unused__[14]
Definition ext2.h:110
uint16_t free_inodes
number of unallocated inodes
Definition ext2.h:108
uint16_t directories_count
Definition ext2.h:109
uint32_t imap
inodes bitmap
Definition ext2.h:102
uint16_t free_blocks
number of unallocated blocks
Definition ext2.h:106
uint32_t bmap
blocks bitmap
Definition ext2.h:100
uint32_t inodes
inode table
Definition ext2.h:104
An inode, as it is on the disk.
Definition ext2.h:114
uint32_t ctime
Definition ext2.h:119
uint16_t nlink
Definition ext2.h:123
uint16_t gid
Definition ext2.h:122
uint32_t extended_attribute_block
Definition ext2.h:134
uint32_t fragment
Definition ext2.h:136
uint32_t flags
Definition ext2.h:125
uint32_t blocks
Definition ext2.h:124
uint32_t size
Definition ext2.h:117
uint32_t mtime
Definition ext2.h:120
uint32_t doubly_indirect_pointer
Definition ext2.h:130
uint32_t os_specific1
Definition ext2.h:127
uint32_t direct_pointer[12]
Definition ext2.h:128
uint32_t triply_indirect_pointer
Definition ext2.h:131
uint32_t size_high
Definition ext2.h:135
uint8_t os_specific2[12]
Definition ext2.h:137
uint16_t mode
Definition ext2.h:115
uint32_t generation_number
Definition ext2.h:133
uint32_t atime
Definition ext2.h:118
uint16_t uid
Definition ext2.h:116
uint32_t dtime
Definition ext2.h:121
uint32_t singly_indirect_pointer
Definition ext2.h:129
The superblock, as it is on the disk, 1024 bytes into it.
Definition ext2.h:34
uint16_t sb_block_group
Definition ext2.h:78
uint32_t mtime
last mount time
Definition ext2.h:58
uint16_t ext2_signature
Definition ext2.h:64
uint16_t on_error
Definition ext2.h:66
uint8_t fsid[16]
Definition ext2.h:84
uint32_t head_of_orphan_inode_list
Definition ext2.h:94
uint32_t ginodes
number of inodes per block group
Definition ext2.h:56
uint32_t major_version
Definition ext2.h:71
uint16_t inode_size
Definition ext2.h:77
uint16_t gid
Definition ext2.h:73
uint16_t uid
Definition ext2.h:72
uint32_t compression_algorithms
Definition ext2.h:87
uint32_t jinode
Definition ext2.h:92
uint32_t time_between_fsck
Definition ext2.h:69
uint32_t feature_compat
Definition ext2.h:80
uint32_t frag_size
log(fragment size)
Definition ext2.h:50
uint16_t times_mounted
Definition ext2.h:62
uint32_t nrblocks
number of reserved blocks
Definition ext2.h:40
uint32_t jdevice
Definition ext2.h:93
uint16_t allowed_times_mounted
Definition ext2.h:63
uint8_t blocks_to_preallocate_for_directories
Definition ext2.h:89
uint32_t nblocks
number of blocks
Definition ext2.h:38
uint32_t gfrags
number of fragments per block group
Definition ext2.h:54
uint32_t free_inodes
number of unallocated inodes
Definition ext2.h:44
uint32_t wtime
last write time
Definition ext2.h:60
uint32_t free_blocks
number of unallocated blocks
Definition ext2.h:42
uint32_t dblock
first data block
Definition ext2.h:46
uint8_t path_last_mounted_to[64]
Definition ext2.h:86
uint32_t first_non_reserved_inode
Definition ext2.h:76
uint32_t ninodes
number of inodes
Definition ext2.h:36
uint32_t feature_ro_compat
Definition ext2.h:82
uint32_t feature_incompat
Definition ext2.h:81
uint8_t name[16]
Definition ext2.h:85
uint8_t jid[16]
Definition ext2.h:91
uint16_t minor_version
Definition ext2.h:67
uint16_t filesystem_state
Definition ext2.h:65
uint32_t gblocks
number of blocks per block group
Definition ext2.h:52
uint32_t block_size
log(block size)
Definition ext2.h:48
uint32_t last_fsck
Definition ext2.h:68
uint32_t os_id
Definition ext2.h:70
uint8_t blocks_to_preallocate_for_files
Definition ext2.h:88
uint16_t __unused__
Definition ext2.h:90
A mounted ext2 filesystem.
Definition ext2.h:168
struct ext2_superblock * superblock
Definition ext2.h:170
struct ext2_group * groups
Definition ext2.h:171
struct vcache * vcache
its vnodes in use, by inode number
Definition ext2.h:187
size_t ngroups
number of block group descriptors
Definition ext2.h:174
struct vnode * supernode
Definition ext2.h:169
size_t bs
the block size
Definition ext2.h:177
size_t inode_size
an inode's size on the disk (revision 0's, or the superblock's)
Definition ext2.h:180
int filetype
directory entries have their file's type (EXT2_INCOMPAT_FILETYPE); without it, the byte is 0
Definition ext2.h:184
A filesystem type.
Definition vfs.h:98
char * name
Definition vfs.h:99
An iobuf.
Definition iobuf.h:35
Who does a filesystem operation: where its paths start, and its credentials and umask.
Definition vfs.h:60
A file in memory (a vnode): what its inode says, and the kernel's own state.
Definition vfs.h:113
uint16_t dev_t
Definition types.h:18
uintptr_t ino_t
Definition types.h:23
long int ssize_t
Definition types.h:12
long int off_t
Definition types.h:10
uint32_t mode_t
Definition types.h:15