cynix

x86 UNIX-like OS
git clone git://git.2f30.org/cynix
Log | Files | Refs | README | LICENSE

ext2.h (7362B)


      1 #ifndef __EXT2_H__
      2 #define __EXT2_H__
      3 
      4 #include <stdint.h>
      5 #include <sys/types.h>
      6 #include <list.h>
      7 
      8 #define O_RDONLY 0
      9 #define NR_MAX_OPEN_FILES 32
     10 #define EXT2_NAME_LEN 255
     11 
     12 #define S_IFMT 0170000 /* type of file */
     13 #define S_IFREG 0100000 /* regular */
     14 #define S_IFDIR 0040000 /* directory */
     15 #define S_IFBLK 0060000 /* block special */
     16 #define S_IFCHR 0020000 /* character special */
     17 #define S_IFIFO 0010000 /* fifo */
     18 
     19 #define S_ISUID 0004000 /* set user id on execution */
     20 #define S_ISGID 0002000 /* set group id on execution */
     21 
     22 #define S_IRWXU 0000700 /* read,write,execute perm: owner */
     23 #define S_IRUSR 0000400 /* read permission: owner */
     24 #define S_IWUSR 0000200 /* write permission: owner */
     25 #define S_IXUSR 0000100 /* execute/search permission: owner */
     26 #define S_IRWXG 0000070 /* read,write,execute perm: group */
     27 #define S_IRGRP 0000040 /* read permission: group */
     28 #define S_IWGRP 0000020 /* write permission: group */
     29 #define S_IXGRP 0000010 /* execute/search permission: group */
     30 #define S_IRWXO 0000007 /* read,write,execute perm: other */
     31 #define S_IROTH 0000004 /* read permission: other */
     32 #define S_IWOTH 0000002 /* write permission: other */
     33 #define S_IXOTH 0000001 /* execute/search permission: other */
     34 
     35 #define S_ISFIFO(m) (((m)&(S_IFMT)) == (S_IFIFO))
     36 #define S_ISDIR(m) (((m)&(S_IFMT)) == (S_IFDIR))
     37 #define S_ISCHR(m) (((m)&(S_IFMT)) == (S_IFCHR))
     38 #define S_ISBLK(m) (((m)&(S_IFMT)) == (S_IFBLK))
     39 #define S_ISREG(m) (((m)&(S_IFMT)) == (S_IFREG))
     40 
     41 enum file_state {
     42 	FILE_ALLOC,
     43 	FILE_NONE
     44 };
     45 
     46 enum file_type {
     47 	FILE_REG,
     48 	FILE_PIPE
     49 };
     50 
     51 enum fs_state {
     52 	EXT2_CLEAN = 1 << 0,
     53 	EXT2_FAULTY = 1 << 1,
     54 	EXT2_ORPHAN_REC = 1 << 2
     55 };
     56 
     57 enum fs_err_handling {
     58 	EXT2_CONT,
     59 	EXT2_REMNT_RO,
     60 	EXT2_PANIC
     61 };
     62 
     63 enum fs_creator {
     64 	EXT2_LINUX,
     65 	EXT2_HURD,
     66 	EXT2_MASIX,
     67 	EXT2_FREEBSD,
     68 	EXT2_LITES
     69 };
     70 
     71 enum fs_feat_compat {
     72 	EXT2_PREALLOCATE_DIR_BLOCKS = 1 << 0,
     73 	EXT2_AFS_EXIST = 1 << 1,
     74 	EXT2_HAS_JOURNAL = 1 << 2,
     75 	EXT2_INODES_EXTENDED = 1 << 3,
     76 	EXT2_CAN_RESIZE = 1 << 4,
     77 	EXT2_USE_HASH = 1 << 5
     78 };
     79 
     80 enum fs_feat_incompat {
     81 	EXT2_COMPRESSION = 1 << 0,
     82 	EXT2_DIR_HAS_TYPE = 1 << 1,
     83 	EXT2_NEEDS_RECOVERY = 1 << 2,
     84 	EXT2_USES_JOURNAL_DEVICE = 1 << 3
     85 };
     86 
     87 enum fs_feat_ro_compat {
     88 	EXT2_SPARSE_SUPER_AND_GDT = 1 << 0,
     89 	EXT2_LARGE_FILES = 1 << 1,
     90 	EXT2_DIR_B_TREES = 1 << 2
     91 };
     92 
     93 enum fs_file_mode_flags {
     94 	/* bits [0, 8] */
     95 	OX = 01,
     96 	OW = 02,
     97 	OR = 03,
     98 	GX = 010,
     99 	GW = 020,
    100 	GR = 040,
    101 	UX = 0100,
    102 	UW = 0200,
    103 	UR = 0400,
    104 	/* bits [9, 11] */
    105 	STICKY = 0x200,
    106 	SGID = 0x400,
    107 	SUID = 0x800,
    108 	/* bits [12, 15] */
    109 	FIFO_T = 0x1000,
    110 	CHDEV_T = 0x2000,
    111 	DIRECTORY_T = 0x4000,
    112 	BLOCK_T = 0x6000,
    113 	REGULAR_T = 0x8000,
    114 	SYM_T = 0xa000,
    115 	UNIX_SOCKET_T = 0xc000
    116 };
    117 
    118 enum fs_inode_flags {
    119 	EXT2_SEC_DEL = 1 << 0,
    120 	EXT2_KEEP_COPY_ON_DEL = 1 << 1,
    121 	EXT2_FILE_COMPRESSION = 1 << 2,
    122 	EXT2_SYNC = 1 << 3,
    123 	EXT2_IMMUTABLE = 1 << 4,
    124 	EXT2_APPEND_ONLY = 1 << 5,
    125 	EXT2_DUMP_EXCLUDE = 1 << 6,
    126 	EXT2_IGNORE_ATIME = 1 << 7,
    127 	EXT2_HASH_DIR = 1 << 8,
    128 	EXT2_DATA_JOURNALED_EXT3 = 1 << 9
    129 };
    130 
    131 enum fs_file_t {
    132 	EXT2_FT_UNKNOWN,
    133 	EXT2_FT_REG_FILE,
    134 	EXT2_FT_DIR,
    135 	EXT2_FT_CHRDEV,
    136 	EXT2_FT_BLKDEV,
    137 	EXT2_FT_FIFO,
    138 	EXT2_FT_SOCK,
    139 	EXT2_FT_SYMLINK,
    140 	EXT2_FT_MAX
    141 };
    142 
    143 struct ext2_superblock_t {
    144 	uint32_t nr_inodes;
    145 	uint32_t nr_blocks;
    146 	uint32_t nr_reserved_inodes;
    147 	uint32_t nr_free_blocks;
    148 	uint32_t nr_free_inodes;
    149 	uint32_t first_data_block;
    150 	uint32_t block_size; /* saved as the number of places to shift 1024 to the left */
    151 	uint32_t fragment_size; /* same as above */
    152 	uint32_t nr_blocks_per_group;
    153 	uint32_t nr_fragments_per_group;
    154 	uint32_t nr_inodes_per_group;
    155 	uint32_t last_mtime;
    156 	uint32_t last_wtime;
    157 	uint16_t mnt_count;
    158 	uint16_t max_mnt_count;
    159 	uint16_t magic;
    160 	uint16_t state;
    161 	uint16_t err_handling_method;
    162 	uint16_t minor_version;
    163 	uint32_t lastcheck;
    164 	uint32_t check_interval;
    165 	uint32_t creator_os;
    166 	uint32_t major_version;
    167 	uint16_t uid_for_reserved_blocks;
    168 	uint16_t gid_for_reserved_blocks;
    169 	uint32_t first_ino;
    170 	uint16_t inode_size;
    171 	uint16_t super_block_group;
    172 	uint32_t feat_compat;
    173 	uint32_t feat_incompat;
    174 	uint32_t feat_ro_compat;
    175 	char fs_id[16];
    176 	char volume_name[16];
    177 	char path_last_mount[64];
    178 	uint32_t algo_usage_bitmap;
    179 	uint8_t nr_blocks_preallocate_file;
    180 	uint8_t nr_blocks_preallocate_dir;
    181 	uint16_t unused1;
    182 	char journal_id[16];
    183 	uint32_t journal_inode;
    184 	uint32_t journal_dev;
    185 	uint32_t head_orphan_inode_list;
    186 	uint32_t hash_seed[4];
    187 	uint8_t default_hash_version;
    188 	uint8_t unused2;
    189 	uint16_t unused3;
    190 	uint32_t mount_options;
    191 	uint32_t first_metablock;
    192 	uint32_t reserved[190];
    193 } __attribute__ ((packed));
    194 
    195 struct ext2_gdt_t {
    196 	uint32_t addr_block_bitmap;
    197 	uint32_t addr_inode_bitmap;
    198 	uint32_t addr_inode_table;
    199 	uint16_t nr_free_blocks;
    200 	uint16_t nr_free_inodes;
    201 	uint16_t nr_dir_entries;
    202 	uint16_t unused;
    203 	uint32_t reserved[3];
    204 } __attribute__ ((packed));
    205 
    206 struct ext2_inode_t {
    207 	uint16_t file_mode;
    208 	uint16_t uid_low16;
    209 	uint32_t size_low32;
    210 	uint32_t atime;
    211 	uint32_t ctime;
    212 	uint32_t mtime;
    213 	uint32_t dtime;
    214 	uint16_t gid_low16;
    215 	uint16_t link_count;
    216 	uint32_t sector_count;
    217 	uint32_t flags;
    218 	uint32_t unused1;
    219 	char direct_blocks[48];
    220 	uint32_t single_indir_block;
    221 	uint32_t double_indir_block;
    222 	uint32_t triple_indir_block;
    223 	uint32_t gen_number;
    224 	uint32_t ext_attribute_block;
    225 	uint32_t size_upper32;
    226 	uint32_t addr_fragment;
    227 	uint8_t fragment_index;
    228 	uint8_t fragment_size;
    229 	uint16_t unused2;
    230 	uint16_t uid_upper16;
    231 	uint16_t gid_upper16;
    232 	uint32_t reserved;
    233 } __attribute__ ((packed));
    234 
    235 struct ext2_dir_t {
    236 	uint32_t inode;
    237 	uint16_t rec_length;
    238 	uint8_t name_length;
    239 	uint8_t file_t;
    240 	char filename[EXT2_NAME_LEN];
    241 } __attribute__ ((packed));
    242 
    243 struct file_t {
    244 	struct ext2_inode_t *f_inode;
    245 	ino_t f_inode_nr;
    246 	off_t f_off;
    247 	enum file_state f_state;
    248 	enum file_type f_type;
    249 	int f_refcount;
    250 	struct list_head f_listopen;
    251 };
    252 
    253 typedef struct DIR {
    254 	int fd;
    255 	uint32_t off;
    256 } DIR;
    257 
    258 struct dirent_t {
    259 	ino_t d_inode;
    260 	off_t d_off;
    261 	uint16_t d_namelen;
    262 	char d_name[EXT2_NAME_LEN + 1];
    263 };
    264 
    265 struct stat {
    266 	dev_t     st_dev; /* ID of device containing file */
    267 	ino_t     st_ino; /* inode number */
    268 	mode_t    st_mode; /* protection */
    269 	nlink_t   st_nlink; /* number of hard links */
    270 	uid_t     st_uid; /* user ID of owner */
    271 	gid_t     st_gid; /* group ID of owner */
    272 	dev_t     st_rdev; /* device ID (if special file) */
    273 	off_t     st_size; /* total size, in bytes */
    274 	blksize_t st_blksize; /* blocksize for file system I/O */
    275 	blkcnt_t  st_blocks; /* number of 512B blocks allocated */
    276 	time_t    st_atime; /* time of last access */
    277 	time_t    st_mtime; /* time of last modification */
    278 	time_t    st_ctime; /* time of last status change */
    279 };
    280 
    281 extern struct ext2_inode_t *get_root_inode(void);
    282 extern int ext2_mount(char *addr);
    283 extern void ext2_dump(void);
    284 extern int open(const char *pathname, int flags, mode_t mode);
    285 extern int creat(const char *pathname, mode_t mode);
    286 extern int close(int fd);
    287 extern ssize_t write(int fd, const void *buf, size_t count);
    288 extern ssize_t read(int fd, void *buf, size_t count);
    289 extern void closefds(void);
    290 extern DIR *opendir(const char *name);
    291 extern int closedir(DIR *dirp);
    292 extern struct dirent_t *readdir(DIR *dirp);
    293 extern int dup(int oldfd);
    294 extern int dup2(int oldfd, int newfd);
    295 extern int stat(const char *path, struct stat *buf);
    296 extern int fstat(int fd, struct stat *buf);
    297 extern int execve(const char *filename, char *const argv[], char *const envp[]);
    298 extern int fileclose(int fd);
    299 
    300 #endif
    301