vfs: switch from struct file to struct vnode

the vnode will provide a much simpler and more elegant API for
programming virtual filesystems
This commit is contained in:
Michael Shaw 2023-10-30 19:40:00 -04:00
commit 2e7b1acda8
3 changed files with 111 additions and 114 deletions

126
fs/ext2.c
View file

@ -18,17 +18,10 @@
static struct fs_ops *ext2_ops;
static uint8_t inode_to_ftype[] = {
ITYPE_DECL(EXT2_INO_FIFO, VFS_FILE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, VFS_FILE_CHARDEV),
ITYPE_DECL(EXT2_INO_DIR, VFS_FILE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, VFS_FILE_BLKDEV),
ITYPE_DECL(EXT2_INO_REG, VFS_FILE_FILE), ITYPE_DECL(EXT2_INO_SYMLINK, VFS_FILE_SYMLINK),
ITYPE_DECL(EXT2_INO_SOCK, VFS_FILE_SOCK),
};
static uint16_t ftype_to_ino[] = {
DTYPE_DECL(VFS_FILE_FIFO, EXT2_INO_FIFO), DTYPE_DECL(VFS_FILE_CHARDEV, EXT2_INO_CHARDEV),
DTYPE_DECL(VFS_FILE_DIR, EXT2_INO_DIR), DTYPE_DECL(VFS_FILE_BLKDEV, EXT2_INO_BLKDEV),
DTYPE_DECL(VFS_FILE_FILE, EXT2_INO_REG), DTYPE_DECL(VFS_FILE_SYMLINK, EXT2_INO_SYMLINK),
DTYPE_DECL(VFS_FILE_SOCK, EXT2_INO_SOCK),
ITYPE_DECL(EXT2_INO_FIFO, EXT2_DE_FIFO), ITYPE_DECL(EXT2_INO_CHARDEV, EXT2_DE_CHRDEV),
ITYPE_DECL(EXT2_INO_DIR, EXT2_DE_DIR), ITYPE_DECL(EXT2_INO_BLKDEV, EXT2_DE_BLKDEV),
ITYPE_DECL(EXT2_INO_REG, EXT2_DE_FILE), ITYPE_DECL(EXT2_INO_SYMLINK, EXT2_DE_SYMLINK),
ITYPE_DECL(EXT2_INO_SOCK, EXT2_DE_SOCK),
};
#undef ITYPE_DECL
@ -625,7 +618,7 @@ static long ext2_inode_find_by_name(struct ext2fs *fs, const char *path)
}
}
if (!entry || !entry->inode) {
if (!entry || !entry->inode || ino_num_blocks == 0) {
kfree(inode);
kfree(path_copy);
kfree(oentry);
@ -644,7 +637,7 @@ found:
return inode_no;
}
static int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
static int ext2_open_file(struct fs *vfs, struct vnode *vnode, const char *path)
{
/* find the inode of the file */
struct ext2fs *fs = (struct ext2fs *)vfs->fs;
@ -665,22 +658,19 @@ static int ext2_open_file(struct fs *vfs, struct file *file, const char *path)
return ret;
}
memcpy(&file->inode, inode, sizeof(struct ext2_inode));
file->size = inode->size;
file->type = inode_to_ftype[inode->mode >> 12];
file->inode_num = inode_no;
vnode->fs = vfs;
vnode->size = inode->size;
vnode->uid = inode->uid;
vnode->gid = inode->gid;
vnode->flags = inode->mode;
vnode->ino_num = inode_no;
file->path = kzalloc(strlen(path) + 1, ALLOC_KERN);
if (!file->path) {
kfree(inode);
return -ENOMEM;
}
strcpy((char *)file->path, path);
file->name = basename((char *)path);
char *bn = basename((char *)path);
memcpy(vnode->name, bn, MIN(strlen(bn), 255));
/* check for long filesize */
if (fs->sb.feature_ro_compat & EXT2_FLAG_LONG_FILESIZE)
file->size |= ((uint64_t)inode->dir_acl << 32);
vnode->size |= ((uint64_t)inode->dir_acl << 32);
kfree(inode);
@ -758,27 +748,32 @@ static int ext2_readdir(struct fs *fs, uint32_t ino_num, struct dirent **out)
return num_entries;
}
static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
static int ext2_write_file(struct fs *vfs, struct vnode *vnode, void *buf, size_t offset, size_t count)
{
struct ext2fs *fs = vfs->fs;
struct ext2_inode inode;
if ((file->inode.mode & 0xF000) != EXT2_INO_REG)
int res = ext2_read_inode(fs, &inode, vnode->ino_num);
if (res < 0)
return res;
if ((inode.flags & 0xF000) != EXT2_INO_REG)
return -EISDIR;
size_t cur_num_blocks = (file->inode.blocks * fs->block_size) / fs->bdev->block_size;
size_t cur_num_blocks = (inode.blocks * fs->block_size) / fs->bdev->block_size;
size_t block = offset / fs->block_size;
size_t ooffset = offset;
if (offset > file->size)
if (offset > vnode->size)
return -EINVAL;
if (offset + count > file->size) {
if (offset + count > vnode->size) {
/* allocate more blocks */
size_t new_num_blocks = npow2(offset + count) / fs->block_size;
new_num_blocks -= MIN(cur_num_blocks, new_num_blocks);
for (size_t i = 0; i < new_num_blocks; i++) {
long block_no = ext2_inode_alloc_block(fs, (struct ext2_inode *)&file->inode, file->inode_num);
long block_no = ext2_inode_alloc_block(fs, (struct ext2_inode *)&inode, vnode->ino_num);
if (block_no < 0)
return block_no;
}
@ -794,7 +789,7 @@ static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t
size_t num = 0;
while (count > 0) {
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&inode, block_buf, block);
if (retval < 0) {
kfree(block_buf);
return retval;
@ -804,7 +799,7 @@ static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t
bufoffset += fs->block_size;
offset = 0;
retval = ext2_inode_write_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
retval = ext2_inode_write_block(fs, (struct ext2_inode *)&inode, block_buf, block);
if (retval < 0) {
kfree(block_buf);
return retval;
@ -819,23 +814,45 @@ static int ext2_write_file(struct fs *vfs, struct file *file, void *buf, size_t
kfree(block_buf);
file->inode.size = MAX(file->inode.size, num + ooffset);
ext2_write_inode(fs, (struct ext2_inode *)&file->inode, file->inode_num);
inode.size = MAX(vnode->size, num + ooffset);
ext2_write_inode(fs, (struct ext2_inode *)&inode, vnode->ino_num);
return num;
}
static long ext2_creat(struct fs *vfs, const char *path, ftype_t type)
static int ino_type_to_dent(uint32_t ino_flags)
{
/* convert from ext2 inode type to dirent type */
switch (ino_flags & 0xF000) {
case EXT2_INO_REG:
return EXT2_DE_FILE;
case EXT2_INO_DIR:
return EXT2_DE_DIR;
case EXT2_INO_CHARDEV:
return EXT2_DE_CHRDEV;
case EXT2_INO_BLKDEV:
return EXT2_DE_BLKDEV;
case EXT2_INO_FIFO:
return EXT2_DE_FIFO;
case EXT2_INO_SOCK:
return EXT2_DE_SOCK;
case EXT2_INO_SYMLINK:
return EXT2_DE_SYMLINK;
}
return -1;
}
static long ext2_creat(struct fs *vfs, const char *path, uint32_t type)
{
struct ext2fs *fs = (struct ext2fs *)vfs->fs;
/* make sure the file doesn't already exist */
struct file file;
int ret = ext2_open_file(vfs, &file, path);
if (ret == 0) {
kfree(file.path);
struct vnode vnode;
int ret = ext2_open_file(vfs, &vnode, path);
if (ret == 0)
return -EEXIST;
}
/* find the parent directory */
char *path_copy = kzalloc(strlen(path) + 1, ALLOC_KERN);
@ -881,7 +898,7 @@ static long ext2_creat(struct fs *vfs, const char *path, ftype_t type)
}
memset(inode, 0, sizeof(struct ext2_inode));
inode->mode = ftype_to_ino[type];
inode->mode = type;
inode->links_count = 1;
inode->size = 0;
inode->blocks = sizeof(struct ext2_inode) / fs->bdev->block_size;
@ -926,7 +943,7 @@ static long ext2_creat(struct fs *vfs, const char *path, ftype_t type)
d_ent->inode = inode_no;
d_ent->rec_len = sizeof(struct ext2_dir_entry) + strlen(base);
d_ent->name_len = strlen(base);
d_ent->file_type = type;
d_ent->file_type = ino_type_to_dent(type);
memcpy(d_ent->name, base, strlen(base));
/* write the block back to disk */
@ -961,7 +978,7 @@ static long ext2_creat(struct fs *vfs, const char *path, ftype_t type)
entry->inode = inode_no;
entry->rec_len = fs->block_size;
entry->name_len = strlen(base);
entry->file_type = type;
entry->file_type = ino_type_to_dent(type);
memcpy(entry->name, base, strlen(base));
/* write the block back to disk */
@ -979,7 +996,7 @@ static long ext2_creat(struct fs *vfs, const char *path, ftype_t type)
static int ext2_mkdir(struct fs *vfs, const char *path)
{
long ret = ext2_creat(vfs, path, VFS_FILE_DIR);
long ret = ext2_creat(vfs, path, EXT2_INO_DIR);
if (ret < 0)
return ret;
@ -1080,20 +1097,25 @@ static int ext2_unlink(struct fs *vfs, const char *path)
return -ENOENT;
}
static int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t offset, size_t count)
static int ext2_read_file(struct fs *vfs, struct vnode *vnode, void *buf, size_t offset, size_t count)
{
struct ext2fs *fs = vfs->fs;
struct ext2_inode inode;
if ((file->inode.mode & 0xF000) != EXT2_INO_REG)
int res = ext2_read_inode(fs, &inode, vnode->ino_num);
if (res < 0)
return res;
if ((inode.mode & 0xF000) != EXT2_INO_REG)
return -EISDIR;
size_t block = offset / fs->block_size;
if (offset > file->size)
if (offset > vnode->size)
return -EINVAL;
if (offset + count > file->size)
count = (file->size - offset);
if (offset + count > vnode->size)
count = (vnode->size - offset);
offset %= fs->block_size;
@ -1107,7 +1129,7 @@ static int ext2_read_file(struct fs *vfs, struct file *file, void *buf, size_t o
size_t bufoffset = 0;
while (count > 0) {
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&file->inode, block_buf, block);
int retval = ext2_inode_read_block(fs, (struct ext2_inode *)&inode, block_buf, block);
if (retval < 0) {
kfree(block_buf);
return retval;
@ -1166,7 +1188,7 @@ struct fs *ext2_init_fs(struct block_device *bdev)
ext2_ops = kmalloc(sizeof(struct fs_ops), ALLOC_KERN);
if(!ext2_ops) {
if (!ext2_ops) {
kfree(extfs->bgdt);
kfree(extfs);
return NULL;

View file

@ -23,18 +23,18 @@ int write(int fd, void *buf, size_t count)
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
if (fdesc->file.type == VFS_FILE_DIR)
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR;
if (fdesc->file.type != VFS_FILE_FILE)
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EBADF;
int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->file, buf, fdesc->pos, count);
int ret = fdesc->fs->ops->write(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
if (ret < 0)
return ret;
fdesc->pos = MIN(fdesc->pos + ret, fdesc->file.size);
fdesc->pos = MIN(fdesc->pos + ret, fdesc->vnode.size);
return ret;
}
@ -48,18 +48,18 @@ int read(int fd, void *buf, size_t count)
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
if (fdesc->file.type == VFS_FILE_DIR)
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) == VFS_VNO_DIR)
return -EISDIR;
if (fdesc->file.type != VFS_FILE_FILE)
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_REG)
return -EBADF;
int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->file, buf, fdesc->pos, count);
int ret = fdesc->fs->ops->read(fdesc->fs, &fdesc->vnode, buf, fdesc->pos, count);
if (ret < 0)
return ret;
fdesc->pos = MIN(fdesc->pos + ret, fdesc->file.size);
fdesc->pos = MIN(fdesc->pos + ret, fdesc->vnode.size);
return ret;
}
@ -73,7 +73,7 @@ int statfd(int fd, struct statbuf *statbuf)
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
statbuf->size = fdesc->file.size;
statbuf->size = fdesc->vnode.size;
return 0;
}
@ -90,7 +90,7 @@ int open(const char *pathname, int flags)
memset(fd, 0, sizeof(struct file_descriptor));
/* find the file */
int result = rootfs->ops->open(rootfs, &fd->file, pathname);
int result = rootfs->ops->open(rootfs, &fd->vnode, pathname);
if (result < 0) {
kprintf(LOG_ERROR "Failed to open %s: %s\n", pathname, strerror(-result));
@ -118,7 +118,6 @@ int close(int fd)
return -EBADF;
struct file_descriptor *fdesc = (struct file_descriptor *)fdnode->value;
kfree(fdesc->file.path);
slab_free(fd_slab, fdesc);
rbt_delete(kfd, fdnode);
@ -143,7 +142,7 @@ int seek(int fd, size_t offset, int whence)
fdesc->pos += offset;
break;
case SEEK_END:
fdesc->pos = fdesc->file.size + offset;
fdesc->pos = fdesc->vnode.size + offset;
break;
default:
return -EINVAL;
@ -186,9 +185,8 @@ DIR *opendir(const char *name)
return NULL;
struct fs *fs = fdesc->fs;
struct file *file = &fdesc->file;
if (file->type != VFS_FILE_DIR) {
if ((fdesc->vnode.flags & VFS_VTYPE_MASK) != VFS_VNO_DIR) {
close(fd);
return NULL;
}
@ -196,7 +194,7 @@ DIR *opendir(const char *name)
DIR *dir = kzalloc(sizeof(DIR), ALLOC_KERN);
dir->fd = fd;
dir->fs = fs;
dir->file = file;
dir->vnode = &fdesc->vnode;
dir->pos = 0;
dir->dirents = NULL;
@ -209,13 +207,12 @@ struct dirent *readdir(DIR *dir)
return NULL;
struct fs *fs = dir->fs;
struct file *file = dir->file;
if (file->type != VFS_FILE_DIR)
if ((dir->vnode->flags & VFS_VTYPE_MASK) != VFS_VNO_DIR)
return NULL;
if (dir->dirents == NULL)
dir->num_dirents = fs->ops->readdir(fs, file->inode_num, &dir->dirents);
dir->num_dirents = fs->ops->readdir(fs, dir->vnode->ino_num, &dir->dirents);
if (dir->dirents == NULL)
return NULL;

View file

@ -8,13 +8,14 @@
#define VFS_TYPE_EXT2 0x0001
#define VFS_FILE_FILE 0x0001
#define VFS_FILE_DIR 0x0002
#define VFS_FILE_CHARDEV 0x0003
#define VFS_FILE_BLKDEV 0x0004
#define VFS_FILE_FIFO 0x0005
#define VFS_FILE_SOCK 0x0006
#define VFS_FILE_SYMLINK 0x0007
#define VFS_VNO_FIFO 0x1000
#define VFS_VNO_CHARDEV 0x2000
#define VFS_VNO_DIR 0x4000
#define VFS_VNO_BLKDEV 0x6000
#define VFS_VNO_REG 0x8000
#define VFS_VNO_SYMLINK 0xA000
#define VFS_VNO_SOCK 0xC000
#define VFS_VTYPE_MASK 0xF000
#define SEEK_SET 0
#define SEEK_CUR 1
@ -23,36 +24,16 @@
typedef uint32_t ino_t;
typedef uint8_t ftype_t;
struct inode {
uint16_t mode;
uint16_t uid;
uint32_t size;
uint32_t atime;
uint32_t ctime;
uint32_t mtime;
uint32_t dtime;
uint16_t gid;
uint16_t links_count;
uint32_t blocks;
struct vnode {
char name[256];
uint32_t flags;
uint32_t osd1;
uint32_t block[12];
uint32_t gid;
uint32_t uid;
uint32_t size;
uint32_t ino_num;
uint32_t block_indirect;
uint32_t block_dindirect;
uint32_t block_tindirect;
uint32_t generation;
uint32_t file_acl;
uint32_t dir_acl;
uint32_t faddr;
uint64_t af_size;
uint16_t frag_number;
uint16_t frag_size;
} PACKED; /* PACKED to allow for memcpy */
struct fs *fs;
};
struct dirent {
uint64_t inode;
@ -65,26 +46,23 @@ typedef struct _DIR {
struct fs *fs;
struct dirent *dirents;
size_t num_dirents;
struct file *file;
struct vnode *vnode;
int fd;
uint64_t pos;
} DIR;
struct file {
struct inode inode;
struct vnode vnode;
ino_t inode_num; /* inode number */
uint32_t type; /* type of the file */
uint64_t size; /* size of the file */
char *path; /* path to the file */
char *name; /* name of the file */
void *fs_file; /* filesystem specific object */
};
struct file_descriptor {
struct file file;
struct vnode vnode;
uint64_t pos;
int fd;
@ -95,9 +73,9 @@ struct file_descriptor {
};
struct fs_ops {
int (*read)(struct fs *fs, struct file *file, void *buf, size_t offset, size_t size);
int (*write)(struct fs *fs, struct file *file, void *buf, size_t offset, size_t size);
int (*open)(struct fs *fs, struct file *file, const char *path);
int (*read)(struct fs *fs, struct vnode *vnode, void *buf, size_t offset, size_t size);
int (*write)(struct fs *fs, struct vnode *vnode, void *buf, size_t offset, size_t size);
int (*open)(struct fs *fs, struct vnode *vnode, const char *path);
int (*readdir)(struct fs *fs, uint32_t ino, struct dirent **dir);
int (*mkdir)(struct fs *fs, const char *path);
int (*unlink)(struct fs *fs, const char *path);