Welcome to The Fiwix Project
A UNIX-like kernel for the i386 architecture
1 /* 2 * fiwix/include/fiwix/fs.h 3 * 4 * Copyright 2018-2021, Jordi Sanfeliu. All rights reserved. 5 * Distributed under the terms of the Fiwix License. 6 */ 7 8 #ifndef _FIWIX_FS_H 9 #define _FIWIX_FS_H 10 11 #include <fiwix/config.h> 12 #include <fiwix/types.h> 13 14 #define CHECK_UFD(ufd) \ 15 { \ 16 if((ufd) > OPEN_MAX || current->fd[(ufd)] == 0) { \ 17 return -EBADF; \ 18 } \ 19 } \ 20 21 struct fd { 22 struct inode *inode; /* file inode */ 23 unsigned short int flags; /* flags */ 24 unsigned short int count; /* number of opened instances */ 25 __off_t offset; /* r/w pointer position */ 26 }; 27 28 #include <fiwix/statfs.h> 29 #include <fiwix/limits.h> 30 #include <fiwix/process.h> 31 #include <fiwix/dirent.h> 32 #include <fiwix/fs_minix.h> 33 #include <fiwix/fs_ext2.h> 34 #include <fiwix/fs_pipe.h> 35 #include <fiwix/fs_iso9660.h> 36 #include <fiwix/fs_proc.h> 37 38 #define BPS 512 /* bytes per sector */ 39 #define BLKSIZE_1K 1024 /* 1KB block size */ 40 #define BLKSIZE_2K 2048 /* 2KB block size */ 41 #define SUPERBLOCK 1 /* block 1 is for superblock */ 42 43 #define MAJOR(dev) (((__dev_t) (dev)) >> 8) 44 #define MINOR(dev) (((__dev_t) (dev)) & 0xFF) 45 #define MKDEV(major, minor) (((major) << 8) | (minor)) 46 47 /* filesystem independent mount-flags */ 48 #define MS_RDONLY 1 /* mount read-only */ 49 #define MS_REMOUNT 32 /* alter flags of a mounted FS */ 50 51 /* old magic mount flag and mask */ 52 #define MS_MGC_VAL 0xC0ED0000 53 #define MS_MGC_MSK 0xFFFF0000 54 55 #define IS_RDONLY_FS(inode) (((inode)->sb) && ((inode)->sb->flags & MS_RDONLY)) 56 57 #define FOLLOW_LINKS 1 58 #define MAX_SYMLINKS 8 /* this prevents infinite loops in symlinks */ 59 60 #define SEEK_SET 0 61 #define SEEK_CUR 1 62 #define SEEK_END 2 63 64 #define FOR_READING 0 65 #define FOR_WRITING 1 66 67 #define VERIFY_READ 1 68 #define VERIFY_WRITE 2 69 70 #define SEL_R 1 71 #define SEL_W 2 72 #define SEL_E 4 73 74 struct inode { 75 __mode_t i_mode; /* file mode */ 76 __uid_t i_uid; /* owner uid */ 77 __size_t i_size; /* size in bytes */ 78 __u32 i_atime; /* access time */ 79 __u32 i_ctime; /* creation time */ 80 __u32 i_mtime; /* modification time */ 81 __gid_t i_gid; /* group id */ 82 __nlink_t i_nlink; /* links count */ 83 __blk_t i_blocks; /* blocks count */ 84 __u32 i_flags; /* file flags */ 85 unsigned char locked; 86 unsigned char dirty; /* 1 = delayed write */ 87 struct inode *mount_point; 88 __dev_t dev; 89 __ino_t inode; 90 __s16 count; 91 __dev_t rdev; 92 struct fs_operations *fsop; 93 struct superblock *sb; 94 struct inode *prev_hash; 95 struct inode *next_hash; 96 struct inode *prev_free; 97 struct inode *next_free; 98 union { 99 struct minix_i_info minix; 100 struct ext2_i_info ext2; 101 struct pipefs_inode pipefs; 102 struct iso9660_inode iso9660; 103 struct procfs_inode procfs; 104 } u; 105 }; 106 extern struct inode *inode_table; 107 extern struct inode **inode_hash_table; 108 109 /* values to be determined during system startup */ 110 extern unsigned int inode_table_size; /* size in bytes */ 111 extern unsigned int inode_hash_table_size; /* size in bytes */ 112 extern unsigned int fd_table_size; /* size in bytes */ 113 114 extern struct fd *fd_table; 115 116 struct superblock { 117 __dev_t dev; 118 unsigned char locked; 119 unsigned char wanted; 120 struct inode *root; /* root inode of mounted fs */ 121 struct inode *dir; /* inode on which the fs was mounted */ 122 unsigned int flags; 123 unsigned char dirty; /* 1 = delayed write */ 124 struct fs_operations *fsop; 125 __u32 s_blocksize; 126 union { 127 struct minix_sb_info minix; 128 struct ext2_sb_info ext2; 129 struct iso9660_sb_info iso9660; 130 } u; 131 }; 132 133 134 #define FSOP_REQUIRES_DEV 1 /* requires a block device */ 135 #define FSOP_KERN_MOUNT 2 /* mounted by kernel */ 136 137 struct fs_operations { 138 int flags; 139 int fsdev; /* internal filesystem (nodev) */ 140 141 /* file operations */ 142 int (*open)(struct inode *, struct fd *); 143 int (*close)(struct inode *, struct fd *); 144 int (*read)(struct inode *, struct fd *, char *, __size_t); 145 int (*write)(struct inode *, struct fd *, const char *, __size_t); 146 int (*ioctl)(struct inode *, int, unsigned long int); 147 int (*lseek)(struct inode *, __off_t); 148 int (*readdir)(struct inode *, struct fd *, struct dirent *, unsigned int); 149 int (*mmap)(struct inode *, struct vma *); 150 int (*select)(struct inode *, int); 151 152 /* inode operations */ 153 int (*readlink)(struct inode *, char *, __size_t); 154 int (*followlink)(struct inode *, struct inode *, struct inode **); 155 int (*bmap)(struct inode *, __off_t, int); 156 int (*lookup)(const char *, struct inode *, struct inode **); 157 int (*rmdir)(struct inode *, struct inode *); 158 int (*link)(struct inode *, struct inode *, char *); 159 int (*unlink)(struct inode *, struct inode *, char *); 160 int (*symlink)(struct inode *, char *, char *); 161 int (*mkdir)(struct inode *, char *, __mode_t); 162 int (*mknod)(struct inode *, char *, __mode_t, __dev_t); 163 int (*truncate)(struct inode *, __off_t); 164 int (*create)(struct inode *, char *, __mode_t, struct inode **); 165 int (*rename)(struct inode *, struct inode *, struct inode *, struct inode *, char *, char *); 166 167 /* block device I/O operations */ 168 int (*read_block)(__dev_t, __blk_t, char *, int); 169 int (*write_block)(__dev_t, __blk_t, char *, int); 170 171 /* superblock operations */ 172 int (*read_inode)(struct inode *); 173 int (*write_inode)(struct inode *); 174 int (*ialloc)(struct inode *, int); 175 void (*ifree)(struct inode *); 176 void (*statfs)(struct superblock *, struct statfs *); 177 int (*read_superblock)(__dev_t, struct superblock *); 178 int (*remount_fs)(struct superblock *, int); 179 int (*write_superblock)(struct superblock *); 180 void (*release_superblock)(struct superblock *); 181 }; 182 183 extern struct fs_operations def_chr_fsop; 184 extern struct fs_operations def_blk_fsop; 185 186 /* fs_minix.h prototypes */ 187 extern struct fs_operations minix_fsop; 188 extern struct fs_operations minix_file_fsop; 189 extern struct fs_operations minix_dir_fsop; 190 extern struct fs_operations minix_symlink_fsop; 191 extern int minix_count_free_inodes(struct superblock *); 192 extern int minix_count_free_blocks(struct superblock *); 193 extern int minix_find_first_zero(struct superblock *, __blk_t, int, int); 194 extern int minix_change_bit(int, struct superblock *, int, int); 195 extern int minix_balloc(struct superblock *); 196 extern void minix_bfree(struct superblock *, int); 197 198 /* fs_ext2.h prototypes */ 199 extern struct fs_operations ext2_fsop; 200 extern struct fs_operations ext2_file_fsop; 201 extern struct fs_operations ext2_dir_fsop; 202 extern struct fs_operations ext2_symlink_fsop; 203 extern int ext2_balloc(struct superblock *); 204 extern void ext2_bfree(struct superblock *, int); 205 206 /* fs_proc.h prototypes */ 207 extern struct fs_operations procfs_fsop; 208 extern struct fs_operations procfs_file_fsop; 209 extern struct fs_operations procfs_dir_fsop; 210 extern struct fs_operations procfs_symlink_fsop; 211 struct procfs_dir_entry * get_procfs_by_inode(struct inode *); 212 213 /* fs_iso9660.h prototypes */ 214 extern int isonum_711(char *); 215 extern int isonum_723(char *); 216 extern int isonum_731(char *); 217 extern int isonum_733(char *); 218 extern unsigned long int isodate(char *); 219 extern int iso9660_cleanfilename(char *, int); 220 extern struct fs_operations iso9660_fsop; 221 extern struct fs_operations iso9660_file_fsop; 222 extern struct fs_operations iso9660_dir_fsop; 223 extern struct fs_operations iso9660_symlink_fsop; 224 void check_rrip_inode(struct iso9660_directory_record *, struct inode *); 225 int get_rrip_filename(struct iso9660_directory_record *, struct inode *, char *); 226 int get_rrip_symlink(struct inode *, char *); 227 228 229 /* generic VFS function prototypes */ 230 void inode_lock(struct inode *); 231 void inode_unlock(struct inode *); 232 struct inode * ialloc(struct superblock *, int); 233 struct inode * iget(struct superblock *, __ino_t); 234 int bmap(struct inode *, __off_t, int); 235 int check_fs_busy(__dev_t, struct inode *); 236 void iput(struct inode *); 237 void sync_inodes(__dev_t); 238 void invalidate_inodes(__dev_t); 239 void inode_init(void); 240 241 int parse_namei(char *, struct inode *, struct inode **, struct inode **, int); 242 int namei(char *, struct inode **, struct inode **, int); 243 244 void superblock_lock(struct superblock *); 245 void superblock_unlock(struct superblock *); 246 struct mount * get_free_mount_point(__dev_t); 247 void release_mount_point(struct mount *); 248 struct mount * get_mount_point(struct inode *); 249 250 int get_new_fd(struct inode *); 251 void release_fd(unsigned int); 252 void fd_init(void); 253 254 void free_name(const char *); 255 int malloc_name(const char *, char **); 256 int check_user_permission(struct inode *); 257 int check_group(struct inode *); 258 int check_user_area(int, const void *, unsigned int); 259 int check_permission(int, struct inode *); 260 261 int do_select(int, fd_set *, fd_set *, fd_set *, fd_set *, fd_set *, fd_set *); 262 263 #endif /* _FIWIX_FS_H */