matos/core/fs.h

168 lines
4.6 KiB
C
Raw Normal View History

2024-05-23 00:21:41 +02:00
#pragma once
2024-08-03 11:19:28 +02:00
#include "ata.h"
2024-07-19 14:13:09 +02:00
#include "fsEntry.h"
#include "process.h"
#include "stdint.h"
#include "types.h"
#include <stddef.h>
#include <stdint.h>
2024-05-23 00:21:41 +02:00
2024-07-19 14:13:09 +02:00
#define FS_NAME_MAXLEN 32
// Forward declaration
struct file_system;
struct file_system_instance;
struct fs_stat;
struct file;
typedef enum {
FS_INODE_REGULAR_FILE = 0x01,
FS_INODE_DIRECTORY = 0x02,
FS_INODE_SYMLINK = 0x04,
FS_INODE_DEVICE_CHAR = 0x08,
FS_INODE_DEVICE_BLOCK = 0x10
} fs_inode_type_t;
struct inode_operation {
};
/**
* Represent a file on the disk
*/
struct inode {
struct file_system_instance *fs;
uint64_t location;
size_t onDiskRef;
size_t inMemRef;
fs_inode_type_t type;
#define FS_READABLE 00400
#define FS_WRITABLE 00200
#define FS_EXECUTABLE 00100
uint32_t mode;
bool_t dirty;
struct inode_operation *ops;
union {
struct inode_dir_ops *opsDir;
struct inode_dir_symlink *opsSymlink;
};
int (*destructor)(struct inode *this);
int (*newOpenedFile)(struct inode *this, const struct process *owner, uint32_t openFlags,
struct file **result);
int (*closeOpenedFile)(struct inode *this, struct file *of);
void *custom;
struct inode *prevDirty, *nextDirty;
};
struct file_system_instance_ops { // super_operations
// From the inode location, instantiace a struct inode
int (*getInodeFromDevice)(struct file_system_instance *this, uint64_t storageLocation,
struct inode **result);
int (*allocateNewInode)(struct file_system_instance *this, fs_inode_type_t type,
const struct process *creator, uint32_t accessRights,
uint32_t open_creat_flags, struct inode **result);
int (*statfs)(struct file_system_instance *this, struct fs_stat *result);
};
struct file_system_instance {
struct file_system *fsType; // this instance is of this type of file_system
struct inode *device; // device (like block one) could have a disk associated
#define FS_MOUNT_SYNC (1 << 0)
#define FS_MOUNT_READONLY (1 << 1)
#define FS_MOUNT_NOEXEC (1 << 2)
uint32_t flags;
struct fs_entry *root; // root fs_entry for this fs instance
struct inode *dirtyNodes; // list of inodes that are out of sync
void *custom; // custom
const struct file_system_instance_ops *ops;
struct file_system_instance *prev, *next;
};
2024-08-03 11:19:28 +02:00
typedef enum { SEEK_SET, SEEK_CUR, SEEK_END } seek_mode_t;
2024-07-19 14:13:09 +02:00
struct file_system {
char name[FS_NAME_MAXLEN];
struct file_system_instance *instances;
void *custom;
int (*mount)(struct file_system *this, struct inode *device, const char *args,
struct file_system_instance **mountedFs);
int (*umount)(struct file_system *this, struct file_system_instance *mountedFs);
2024-08-03 11:19:28 +02:00
//tmp function to implement fs in kernel only as a firts step
void *(*open)(struct ata_device *disk, struct ata_partition *path, uint32_t mode);
int (*read)(struct ata_device *disk, void *private, uint32_t size, uint32_t nmemb,
char *out);
int (*resolve)(struct ata_device *disk);
int (*close)(void *private);
int (*seek)(void *private, uint32_t offset, seek_mode_t seek_mode);
// end tmp
2024-07-19 14:13:09 +02:00
struct file_system *prev, *next;
};
struct file_operations {
loff_t (*llseek)(struct file *this, loff_t offset, seek_mode_t whence);
ssize_t (*read)(struct file *this, char *buf, size_t size, loff_t *offset);
ssize_t (*write)(struct file *this, const char *buf, size_t size, loff_t *offset);
int (*mmap)(struct file *this, uaddr_t *uaddr, size_t size,
int32_t access_rights, int32_t flags, loff_t offset);
int (*open)(struct inode *, struct file *);
int (*flush)(struct file *);
int (*release)(struct inode *, struct file *);
};
/**
* A opened file by a process
*/
struct file {
const struct process *owner;
struct fs_entry *direntry;
uint ref;
int64_t position;
#define O_ACCMODE 0003
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define O_CREAT 0100 /* Not fcntl. */
#define O_EXCL 0200 /* Not fcntl. */
#define O_NOCTTY 0400 /* Not fcntl. */
#define O_TRUNC 01000 /* Not fcntl. */
#define O_APPEND 02000
#define O_DIRECTORY 0200000 /* Must be a directory. */
#define O_NOFOLLOW 0400000 /* Do not follow links. */
#define O_SYNC 04010000
#define O_CLOEXEC 02000000 /* Set close_on_exec. */
uint32_t flags;
const struct file_operations *ops;
int (*duplicate)(struct file *this, const struct process *owner, struct file **result);
void *custom;
};
int fsRegister(struct file_system *fs);
2024-05-23 00:21:41 +02:00
int fsInodeUnref(struct inode *inode);
int fsInodeRef(struct inode *inode);