205 lines
5.9 KiB
C++
205 lines
5.9 KiB
C++
// ext2.h
|
|
// ext2 filesystem driver for HOS
|
|
// Author: Josh Holtrop
|
|
// Date: 05/10/05
|
|
// Modified: 05/23/05
|
|
|
|
#ifndef __HOS_EXT2_H__
|
|
#define __HOS_EXT2_H__ __HOS_EXT2_H__
|
|
|
|
#define EXT2_MAGIC 0xEF53
|
|
#define EXT2_NAME_LEN 255
|
|
|
|
#define EXT2_I_MODE_ATTR_MASK 0x0FFF
|
|
#define EXT2_I_MODE_OX 0x0001
|
|
#define EXT2_I_MODE_OW 0x0002
|
|
#define EXT2_I_MODE_OR 0x0004
|
|
#define EXT2_I_MODE_GX 0x0008
|
|
#define EXT2_I_MODE_GW 0x0010
|
|
#define EXT2_I_MODE_GR 0x0020
|
|
#define EXT2_I_MODE_UX 0x0040
|
|
#define EXT2_I_MODE_UW 0x0080
|
|
#define EXT2_I_MODE_UR 0x0100
|
|
#define EXT2_I_MODE_STICKY 0x0200
|
|
#define EXT2_I_MODE_SGID 0x0400
|
|
#define EXT2_I_MODE_SUID 0x0800
|
|
|
|
#define EXT2_I_MODE_TYPE_MASK 0xF000
|
|
#define EXT2_I_MODE_FIFO 0x1000
|
|
#define EXT2_I_MODE_CHAR 0x2000
|
|
#define EXT2_I_MODE_DIR 0x4000
|
|
#define EXT2_I_MODE_BLOCK 0x6000
|
|
#define EXT2_I_MODE_FILE 0x8000
|
|
#define EXT2_I_MODE_SYM 0xA000
|
|
#define EXT2_I_MODE_SOCK 0xC000
|
|
|
|
#define EXT2_I_FLAGS_SEC_DEL 0x01
|
|
#define EXT2_I_FLAGS_UNDELETE 0x02
|
|
#define EXT2_I_FLAGS_COMPRESS 0x04
|
|
#define EXT2_I_FLAGS_SYNC 0x08
|
|
#define EXT2_I_FLAGS_IMMUTABLE 0x10
|
|
#define EXT2_I_FLAGS_APPEND 0x20
|
|
#define EXT2_I_FLAGS_NODUMP 0x40
|
|
|
|
/* ext2 standard inode numbers */
|
|
#define EXT2_INODE_BAD_BLOCKS 1
|
|
#define EXT2_INODE_ROOT 2
|
|
#define EXT2_INODE_ACL_INDEX 3
|
|
#define EXT2_INODE_ACL_DATA 4
|
|
#define EXT2_INODE_BOOT_LOADER 5
|
|
#define EXT2_INODE_UNDELETE_DIR 6
|
|
#define EXT2_INODE_AVAIL 11
|
|
|
|
/* The directory entry file type field values */
|
|
#define EXT2_FT_UNKNOWN 0
|
|
#define EXT2_FT_FILE 1
|
|
#define EXT2_FT_DIR 2
|
|
#define EXT2_FT_CHAR 3
|
|
#define EXT2_FT_BLOCK 4
|
|
#define EXT2_FT_FIFO 5
|
|
#define EXT2_FT_SOCK 6
|
|
#define EXT2_FT_SYMLINK 7
|
|
#define EXT2_FT_MAX 8
|
|
|
|
#include "fs/vfs.h"
|
|
|
|
typedef struct
|
|
{
|
|
u32_t s_inodes_count; /* Inodes count */
|
|
u32_t s_blocks_count; /* Blocks count */
|
|
u32_t s_r_blocks_count; /* Reserved blocks count */
|
|
u32_t s_free_blocks_count; /* Free blocks count */
|
|
u32_t s_free_inodes_count; /* Free inodes count */
|
|
u32_t s_first_data_block; /* First Data Block */
|
|
u32_t s_log_block_size; /* Block size: 0->1024, 1->2048, 2->4096 */
|
|
int s_log_frag_size; /* Fragment size */
|
|
u32_t s_blocks_per_group; /* # Blocks per group */
|
|
u32_t s_frags_per_group; /* # Fragments per group */
|
|
u32_t s_inodes_per_group; /* # Inodes per group */
|
|
u32_t s_mtime; /* Mount time */
|
|
u32_t s_wtime; /* Write time */
|
|
u16_t s_mnt_count; /* Mount count */
|
|
short s_max_mnt_count; /* Maximal mount count */
|
|
u16_t s_magic; /* Magic signature */
|
|
u16_t s_state; /* File system state */
|
|
u16_t s_errors; /* Behaviour when detecting errors */
|
|
u16_t s_minor_rev_level; /* minor revision level */
|
|
u32_t s_lastcheck; /* time of last check */
|
|
u32_t s_checkinterval; /* max. time between checks */
|
|
u32_t s_creator_os; /* OS */
|
|
u32_t s_rev_level; /* Revision level */
|
|
u16_t s_def_resuid; /* Default uid for reserved blocks */
|
|
u16_t s_def_resgid; /* Default gid for reserved blocks */
|
|
|
|
u32_t s_reserved[235];
|
|
} ext2_super_block_t;
|
|
|
|
typedef struct
|
|
{
|
|
u32_t bg_block_bitmap; // Blocks bitmap block
|
|
u32_t bg_inode_bitmap; // Inode bitmap block
|
|
u32_t bg_inode_table; // Inode table block
|
|
u16_t bg_free_blocks_count; // Free blocks count
|
|
u16_t bg_free_inodes_count; // Free Inodes count
|
|
u16_t bg_used_dirs_count; // Directories count
|
|
u16_t bg_pad1;
|
|
u32_t bg_reserved[3];
|
|
} ext2_group_desc_t;
|
|
|
|
typedef struct
|
|
{
|
|
u16_t i_mode; // File mode
|
|
u16_t i_uid; // Owner UID
|
|
u32_t i_size; // Size in bytes
|
|
u32_t i_atime; // Access time
|
|
u32_t i_ctime; // Creation time
|
|
u32_t i_mtime; // Modification time
|
|
u32_t i_dtime; // Deletion time
|
|
u16_t i_gid; // Group ID
|
|
u16_t i_links_count; // Links count
|
|
u32_t i_blocks; // Disk Blocks count (512)
|
|
u32_t i_flags; // File flags
|
|
u32_t i_reserved1;
|
|
u32_t i_block[15]; // Pointers to blocks (12 direct, single, double, triple indirect)
|
|
u32_t i_version; // File version (for NFS)
|
|
u32_t i_file_acl; // File ACL
|
|
u32_t i_dir_acl; // Directory ACL
|
|
u32_t i_faddr; // Fragment address
|
|
u8_t i_frag; // Fragment number
|
|
u8_t i_fsize; // Fragment size
|
|
u16_t i_pad1;
|
|
u32_t i_reserved2[2];
|
|
} ext2_inode_t;
|
|
|
|
typedef struct
|
|
{
|
|
u32_t inode; // inode number
|
|
u16_t length; // directory entry length
|
|
u8_t name_length; // name length
|
|
u8_t file_type; // File type (used???)
|
|
char name[EXT2_NAME_LEN];
|
|
} ext2_dir_entry_t;
|
|
|
|
#ifdef _HOS_CPP_
|
|
|
|
int ext2_init();
|
|
FileSystem *ext2__mount_func(device_t dev);
|
|
|
|
class Ext2fs : public FileSystem
|
|
{
|
|
protected:
|
|
/* my data members */
|
|
device_t myDevice;
|
|
int mySuperDirty;
|
|
ext2_super_block_t mySuper;
|
|
u32_t myNumGroups;
|
|
u32_t myGroupDescriptorBlocks;
|
|
u32_t myInodeTableBlocks;
|
|
u32_t myGroupDescriptorsPerBlock;
|
|
u32_t myInodesPerBlock;
|
|
|
|
/* my functions */
|
|
int readGroupDescriptor(u32_t group, ext2_group_desc_t *buf);
|
|
int writeGroupDescriptor(u32_t group, ext2_group_desc_t *buf);
|
|
int inodeStatus(u32_t inum);
|
|
int blockStatus(u32_t bnum);
|
|
u32_t allocFromBitmap(u32_t blockNum, u32_t max);
|
|
void freeFromBitmap(u32_t blockNum, u32_t node);
|
|
|
|
public:
|
|
Ext2fs(ext2_super_block_t *super, device_t dev);
|
|
~Ext2fs();
|
|
u32_t totalBlocks();
|
|
u32_t freeBlocks();
|
|
u32_t totalInodes();
|
|
u32_t freeInodes();
|
|
u32_t getRootInodeNumber();
|
|
OpenDirectory *openDirectory(u32_t inum, int mode);
|
|
OpenFile *openFile(u32_t inum, int mode);
|
|
int stat(u32_t inum, vfs_stat_t *buf);
|
|
int readBlock(u32_t blockNum, void *buf);
|
|
int writeBlock(u32_t blockNum, void *buf);
|
|
int link_deref(u32_t inum, char *buf);
|
|
int readInode(u32_t inum, ext2_inode_t *buf);
|
|
int writeInode(u32_t inum, ext2_inode_t *buf);
|
|
u32_t allocInode();
|
|
u32_t allocBlock();
|
|
int freeInode(u32_t inum);
|
|
int freeBlock(u32_t bnum);
|
|
void unlink(u32_t inum);
|
|
int attemptRemoveDir(u32_t inum);
|
|
void truncateInode(u32_t inum);
|
|
int initializeDirectory(u32_t inum, u32_t parent);
|
|
|
|
/* inlined functions */
|
|
inline u32_t numGroups() { return myNumGroups; }
|
|
inline u32_t groupDescriptorBlocks() { return myGroupDescriptorBlocks; }
|
|
inline u32_t inodeTableBlocks() { return myInodeTableBlocks; }
|
|
inline ext2_super_block_t *getSuper() { return &mySuper; }
|
|
};
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|