The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
ext2.h File Reference

Go to the source code of this file.

Data Structures

struct  ext2_volume_t
struct  ext2_file_t
struct  ext2_directory_entry_t

Macros

#define EXT2_SUPERBLOCK_OFFSET   1024u
#define EXT2_SUPERBLOCK_MAGIC   0xEF53u
#define EXT2_NDIR_BLOCKS   12u
#define EXT2_INODE_ROOT   2u
#define EXT2_FT_REG_FILE   1u
#define EXT2_FT_DIR   2u
#define EXT2_FT_CHRDEV   3u
#define EXT2_FT_BLKDEV   4u
#define EXT2_FT_FIFO   5u
#define EXT2_FT_SYMLINK   7u
#define EXT2_S_IFREG   0x8000u
#define EXT2_S_IFDIR   0x4000u
#define EXT2_S_IFLNK   0xA000u
#define EXT2_S_IFIFO   0x1000u
#define EXT2_S_IFCHR   0x2000u
#define EXT2_S_IFBLK   0x6000u
#define EXT2_LINK_MAX   32000u
#define EXT2_S_IFMT   0xF000u
#define EXT2_FEATURE_INCOMPAT_COMPRESSION   0x0001u
#define EXT2_FEATURE_INCOMPAT_FILETYPE   0x0002u
#define EXT2_FEATURE_INCOMPAT_RECOVER   0x0004u
#define EXT2_FEATURE_INCOMPAT_JOURNAL_DEV   0x0008u
#define EXT2_FEATURE_INCOMPAT_META_BG   0x0010u
#define EXT2_FEATURE_INCOMPAT_EXTENTS   0x0040u
#define EXT2_FEATURE_INCOMPAT_64BIT   0x0080u
#define EXT2_FEATURE_INCOMPAT_MMP   0x0100u
#define EXT2_FEATURE_INCOMPAT_FLEX_BG   0x0200u
#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE   0x0002u
#define EXT2_MAX_BLOCK_SIZE   4096u
#define EXT2_MAX_INODE_SIZE   256u
#define EXT2_NAME_MAX   256u

Functions

struct __attribute__ ((packed))
uint32_t ext2_now (void)
 the time for inode times: ext2_clock, or a fixed time without one.
bool ext2_write_inode (ext2_volume_t *volume, uint32_t inode_number, const ext2_inode_t *inode)
 write an inode back to the volume.
kerr_t ext2_initialize (ext2_volume_t *volume, block_device_t *disk)
 initialize an ext2 volume from a disk.
kerr_t ext2_read_inode (ext2_volume_t *volume, uint32_t inode_number, ext2_inode_t *inode_out)
 read one inode by inode number.
kerr_t ext2_list_directory (ext2_volume_t *volume, uint32_t inode_number)
 list entries in a directory inode.
uint64_t ext2_inode_size (const ext2_inode_t *inode)
 the size of an inode's file in bytes.
uint64_t ext2_max_file_size (const ext2_volume_t *volume)
 the largest size a regular file can have on a volume: what the direct, single, double and triple indirect blocks can map, and at most what i_blocks can count (2 TiB).
kerr_t ext2_read_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t offset, uint32_t length, void *buffer)
 read bytes from a file inode into buffer.
kerr_t ext2_create_file (ext2_volume_t *volume, const char *path)
 create a regular file at an absolute path.
kerr_t ext2_create_dir (ext2_volume_t *volume, const char *path)
 create a directory at an absolute path.
kerr_t ext2_remove_file (ext2_volume_t *volume, const char *path)
 remove a regular file at an absolute path.
kerr_t ext2_remove_dir (ext2_volume_t *volume, const char *path)
 remove a directory at an absolute path.
kerr_t ext2_rename (ext2_volume_t *volume, const char *old_path, const char *new_path)
 rename a file or directory (both paths absolute and normalized).
kerr_t ext2_lookup_path (ext2_volume_t *volume, const char *path, uint32_t *inode_out)
 resolve an absolute path to an inode.
kerr_t ext2_find_link (ext2_volume_t *volume, const char *path, bool follow_last, char *target, uint32_t size, uint32_t *end_out)
 find the first symbolic link along a path.
kerr_t ext2_readlink (ext2_volume_t *volume, const char *path, char *target, uint32_t size)
 read a symbolic link's target, with a NUL.
kerr_t ext2_symlink (ext2_volume_t *volume, const char *path, const char *target)
 make a symbolic link at path that points at target.
kerr_t ext2_link (ext2_volume_t *volume, const char *old_path, const char *new_path)
 give the file at old_path one more name, new_path.
kerr_t ext2_mknod (ext2_volume_t *volume, const char *path, uint32_t mode, uint32_t major, uint32_t minor)
 make a named pipe or a device node.
void ext2_device_numbers (const ext2_inode_t *inode, uint32_t *major, uint32_t *minor)
 the numbers of a device node, as ext2_mknod stored them.
kerr_t ext2_open (ext2_volume_t *volume, const char *path, ext2_file_t *file)
 open a file or directory by absolute path.
uint32_t ext2_read (ext2_file_t *file, uint32_t byteCount, void *dataOut)
 read bytes from an open ext2 file handle.
uint32_t ext2_write (ext2_file_t *file, uint32_t byteCount, const void *dataIn)
 write bytes to an open ext2 file at the current cursor position.
kerr_t ext2_truncate (ext2_file_t *file, uint64_t length)
 cut or grow an open file to length bytes (growth reads as zeros).
kerr_t ext2_read_entry (ext2_file_t *file, ext2_directory_entry_t *entryOut)
 read one directory entry from an open directory handle.
void ext2_close (ext2_file_t *file)
 close an open ext2 file handle.
kerr_t ext2_write_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t offset, uint32_t length, const void *buffer)
 write bytes to a file inode.
kerr_t ext2_truncate_file (ext2_volume_t *volume, uint32_t inode_number, uint64_t length)
 cut or grow a file to length bytes.

Variables

 ext2_superblock_t
 ext2_block_group_desc_t
 ext2_inode_t
 ext2_dir_entry_t
uint32_t(*) ext2_clock (void)
 where the driver gets the time for inode times, in seconds since 1970.

Macro Definition Documentation

◆ EXT2_SUPERBLOCK_OFFSET

#define EXT2_SUPERBLOCK_OFFSET   1024u

◆ EXT2_SUPERBLOCK_MAGIC

#define EXT2_SUPERBLOCK_MAGIC   0xEF53u

◆ EXT2_NDIR_BLOCKS

#define EXT2_NDIR_BLOCKS   12u

◆ EXT2_INODE_ROOT

#define EXT2_INODE_ROOT   2u

◆ EXT2_FT_REG_FILE

#define EXT2_FT_REG_FILE   1u

◆ EXT2_FT_DIR

#define EXT2_FT_DIR   2u

◆ EXT2_FT_CHRDEV

#define EXT2_FT_CHRDEV   3u

◆ EXT2_FT_BLKDEV

#define EXT2_FT_BLKDEV   4u

◆ EXT2_FT_FIFO

#define EXT2_FT_FIFO   5u

◆ EXT2_FT_SYMLINK

#define EXT2_FT_SYMLINK   7u

◆ EXT2_S_IFREG

#define EXT2_S_IFREG   0x8000u

◆ EXT2_S_IFDIR

#define EXT2_S_IFDIR   0x4000u

◆ EXT2_S_IFLNK

#define EXT2_S_IFLNK   0xA000u

◆ EXT2_S_IFIFO

#define EXT2_S_IFIFO   0x1000u

◆ EXT2_S_IFCHR

#define EXT2_S_IFCHR   0x2000u

◆ EXT2_S_IFBLK

#define EXT2_S_IFBLK   0x6000u

◆ EXT2_LINK_MAX

#define EXT2_LINK_MAX   32000u

◆ EXT2_S_IFMT

#define EXT2_S_IFMT   0xF000u

◆ EXT2_FEATURE_INCOMPAT_COMPRESSION

#define EXT2_FEATURE_INCOMPAT_COMPRESSION   0x0001u

◆ EXT2_FEATURE_INCOMPAT_FILETYPE

#define EXT2_FEATURE_INCOMPAT_FILETYPE   0x0002u

◆ EXT2_FEATURE_INCOMPAT_RECOVER

#define EXT2_FEATURE_INCOMPAT_RECOVER   0x0004u

◆ EXT2_FEATURE_INCOMPAT_JOURNAL_DEV

#define EXT2_FEATURE_INCOMPAT_JOURNAL_DEV   0x0008u

◆ EXT2_FEATURE_INCOMPAT_META_BG

#define EXT2_FEATURE_INCOMPAT_META_BG   0x0010u

◆ EXT2_FEATURE_INCOMPAT_EXTENTS

#define EXT2_FEATURE_INCOMPAT_EXTENTS   0x0040u

◆ EXT2_FEATURE_INCOMPAT_64BIT

#define EXT2_FEATURE_INCOMPAT_64BIT   0x0080u

◆ EXT2_FEATURE_INCOMPAT_MMP

#define EXT2_FEATURE_INCOMPAT_MMP   0x0100u

◆ EXT2_FEATURE_INCOMPAT_FLEX_BG

#define EXT2_FEATURE_INCOMPAT_FLEX_BG   0x0200u

◆ EXT2_FEATURE_RO_COMPAT_LARGE_FILE

#define EXT2_FEATURE_RO_COMPAT_LARGE_FILE   0x0002u

◆ EXT2_MAX_BLOCK_SIZE

#define EXT2_MAX_BLOCK_SIZE   4096u

◆ EXT2_MAX_INODE_SIZE

#define EXT2_MAX_INODE_SIZE   256u

◆ EXT2_NAME_MAX

#define EXT2_NAME_MAX   256u

Function Documentation

◆ __attribute__()

struct __attribute__ ( (packed) )

◆ ext2_now()

uint32_t ext2_now ( void )

the time for inode times: ext2_clock, or a fixed time without one.

◆ ext2_write_inode()

bool ext2_write_inode ( ext2_volume_t * volume,
uint32_t inode_number,
const ext2_inode_t * inode )

write an inode back to the volume.

Returns
true on success.

◆ ext2_initialize()

kerr_t ext2_initialize ( ext2_volume_t * volume,
block_device_t * disk )

initialize an ext2 volume from a disk.

Parameters
volumevolume object to initialize.
diskinitialized block device backend.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_read_inode()

kerr_t ext2_read_inode ( ext2_volume_t * volume,
uint32_t inode_number,
ext2_inode_t * inode_out )

read one inode by inode number.

Parameters
volumeinitialized ext2 volume.
inode_numberinode to read.
inode_outoutput inode.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_list_directory()

kerr_t ext2_list_directory ( ext2_volume_t * volume,
uint32_t inode_number )

list entries in a directory inode.

Parameters
volumeinitialized ext2 volume.
inode_numberdirectory inode number.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_inode_size()

uint64_t ext2_inode_size ( const ext2_inode_t * inode)

the size of an inode's file in bytes.

a regular file keeps the high 32 bits in i_dir_acl.

Parameters
inodethe inode.
Returns
the size.

◆ ext2_max_file_size()

uint64_t ext2_max_file_size ( const ext2_volume_t * volume)

the largest size a regular file can have on a volume: what the direct, single, double and triple indirect blocks can map, and at most what i_blocks can count (2 TiB).

on a revision 0 volume (no feature flags), 2 GiB - 1.

Parameters
volumethe volume.
Returns
the size in bytes.

◆ ext2_read_file()

kerr_t ext2_read_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t offset,
uint32_t length,
void * buffer )

read bytes from a file inode into buffer.

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
offsetbyte offset in file.
lengthbytes to read.
bufferdestination buffer.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_create_file()

kerr_t ext2_create_file ( ext2_volume_t * volume,
const char * path )

create a regular file at an absolute path.

◆ ext2_create_dir()

kerr_t ext2_create_dir ( ext2_volume_t * volume,
const char * path )

create a directory at an absolute path.

◆ ext2_remove_file()

kerr_t ext2_remove_file ( ext2_volume_t * volume,
const char * path )

remove a regular file at an absolute path.

◆ ext2_remove_dir()

kerr_t ext2_remove_dir ( ext2_volume_t * volume,
const char * path )

remove a directory at an absolute path.

◆ ext2_rename()

kerr_t ext2_rename ( ext2_volume_t * volume,
const char * old_path,
const char * new_path )

rename a file or directory (both paths absolute and normalized).

an existing new_path is replaced first, as POSIX rename does: a file by a file, an empty directory by a directory. this is not atomic: if the rename then fails, the replaced entry is gone.

Returns
KERR_OK, KERR_INVAL (a directory into itself), KERR_ISDIR, KERR_NOTDIR, KERR_NOTEMPTY, KERR_NOTFOUND, KERR_NOSPC or KERR_IO.

◆ ext2_lookup_path()

kerr_t ext2_lookup_path ( ext2_volume_t * volume,
const char * path,
uint32_t * inode_out )

resolve an absolute path to an inode.

Parameters
volumeinitialized ext2 volume.
pathabsolute path.
inode_outresolved inode.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_find_link()

kerr_t ext2_find_link ( ext2_volume_t * volume,
const char * path,
bool follow_last,
char * target,
uint32_t size,
uint32_t * end_out )

find the first symbolic link along a path.

Parameters
follow_lastalso the last name of the path, when it is a link.
targetgets the link's target, with a NUL.
end_outgets the length of the path up to and with the link's name, or 0 when there is no link (or a name is missing).
Returns
KERR_OK, KERR_RANGE (the target is longer than size), KERR_IO.

◆ ext2_readlink()

kerr_t ext2_readlink ( ext2_volume_t * volume,
const char * path,
char * target,
uint32_t size )

read a symbolic link's target, with a NUL.

Returns
KERR_OK, KERR_INVAL (not a link), KERR_RANGE (too long for size), KERR_NOTFOUND.

◆ ext2_symlink()

kerr_t ext2_symlink ( ext2_volume_t * volume,
const char * path,
const char * target )

make a symbolic link at path that points at target.

Returns
KERR_OK, KERR_EXIST, KERR_RANGE (the target is not shorter than a block), KERR_NOTFOUND (no parent), KERR_NOSPC.

◆ ext2_link()

kerr_t ext2_link ( ext2_volume_t * volume,
const char * old_path,
const char * new_path )

give the file at old_path one more name, new_path.

Returns
KERR_OK, KERR_ISDIR (a directory), KERR_EXIST, KERR_RANGE (too many links), KERR_NOTFOUND, KERR_NOSPC.

◆ ext2_mknod()

kerr_t ext2_mknod ( ext2_volume_t * volume,
const char * path,
uint32_t mode,
uint32_t major,
uint32_t minor )

make a named pipe or a device node.

Parameters
modethe type (EXT2_S_IFIFO, EXT2_S_IFCHR, EXT2_S_IFBLK) and the permission bits.
majorthe device's numbers (a device node only).
Returns
KERR_OK, KERR_EXIST, KERR_NOTFOUND (no parent), KERR_NOSPC, KERR_INVAL (another type).

◆ ext2_device_numbers()

void ext2_device_numbers ( const ext2_inode_t * inode,
uint32_t * major,
uint32_t * minor )

the numbers of a device node, as ext2_mknod stored them.

◆ ext2_open()

kerr_t ext2_open ( ext2_volume_t * volume,
const char * path,
ext2_file_t * file )

open a file or directory by absolute path.

Parameters
volumeinitialized ext2 volume.
pathabsolute path.
fileoutput file handle.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_read()

uint32_t ext2_read ( ext2_file_t * file,
uint32_t byteCount,
void * dataOut )

read bytes from an open ext2 file handle.

Parameters
fileopen file handle.
byteCountmax bytes to read.
dataOutdestination buffer.
Returns
number of bytes read.

◆ ext2_write()

uint32_t ext2_write ( ext2_file_t * file,
uint32_t byteCount,
const void * dataIn )

write bytes to an open ext2 file at the current cursor position.

allocates blocks as needed. advances the cursor.

Parameters
fileopen file handle.
byteCountbytes to write.
dataInsource buffer.
Returns
number of bytes actually written, or 0 on failure.

◆ ext2_truncate()

kerr_t ext2_truncate ( ext2_file_t * file,
uint64_t length )

cut or grow an open file to length bytes (growth reads as zeros).

the cursor stays.

Returns
KERR_OK, or KERR_IO.

◆ ext2_read_entry()

kerr_t ext2_read_entry ( ext2_file_t * file,
ext2_directory_entry_t * entryOut )

read one directory entry from an open directory handle.

Parameters
fileopen directory handle.
entryOutoutput directory entry.
Returns
KERR_OK when an entry is read. KERR_NOENT when done or on failure.

◆ ext2_close()

void ext2_close ( ext2_file_t * file)

close an open ext2 file handle.

Parameters
filefile handle to close.

◆ ext2_write_file()

kerr_t ext2_write_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t offset,
uint32_t length,
const void * buffer )

write bytes to a file inode.

allocates blocks as needed (direct blocks only, up to 48KB with 4K blocks).

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
offsetbyte offset in file to start writing.
lengthbytes to write.
buffersource data.
Returns
KERR_OK on success, or an error code on failure.

◆ ext2_truncate_file()

kerr_t ext2_truncate_file ( ext2_volume_t * volume,
uint32_t inode_number,
uint64_t length )

cut or grow a file to length bytes.

the blocks past a shorter end are freed; a longer file gets a hole, which reads as zeros.

Parameters
volumeinitialized ext2 volume.
inode_numberfile inode number.
lengththe new size.
Returns
KERR_OK on success, or an error code on failure.

Variable Documentation

◆ ext2_superblock_t

ext2_superblock_t

◆ ext2_block_group_desc_t

ext2_block_group_desc_t

◆ ext2_inode_t

ext2_inode_t

◆ ext2_dir_entry_t

ext2_dir_entry_t

◆ ext2_clock

uint32_t(*) ext2_clock(void) ( void )
extern

where the driver gets the time for inode times, in seconds since 1970.

the kernel sets it at boot; while it is NULL (the bootloader) the driver uses a fixed time.