vfs: restructure vfs to reduce various errors
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
1fc17e6d6e
commit
0c29f0e517
7 changed files with 220 additions and 253 deletions
|
|
@ -50,7 +50,7 @@ int ramfs_create(u64 flags, fs_t **out)
|
|||
return 0;
|
||||
|
||||
out_root_inode:
|
||||
inode_dec_ref_lock(root_inode);
|
||||
inode_dec_ref(root_inode);
|
||||
dirent_free(root);
|
||||
out_root:
|
||||
kfree(fs);
|
||||
|
|
@ -77,8 +77,7 @@ static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
|
|||
static int free_inode(fs_t *fs, inode_t *inode)
|
||||
{
|
||||
kfree(inode->data);
|
||||
inode_dec_ref_lock(inode);
|
||||
|
||||
inode_dec_ref(inode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -89,13 +88,9 @@ static int open_inode(fs_t *fs, u64 id, dirent_t *dirent, inode_t **out)
|
|||
|
||||
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
if (!inode->data)
|
||||
if (!inode->data || inode->file_size == 0)
|
||||
return 0;
|
||||
|
||||
if (inode->file_size == 0)
|
||||
goto out;
|
||||
|
||||
if (offset >= inode->file_size)
|
||||
return -EINVAL;
|
||||
|
||||
|
|
@ -103,33 +98,22 @@ static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
|
|||
size = inode->file_size - offset;
|
||||
|
||||
memcpy(buf, inode->data + offset, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
return ret;
|
||||
return size;
|
||||
}
|
||||
|
||||
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
size_t new_size = size + offset;
|
||||
|
||||
if (!inode->data) {
|
||||
inode->data = kalloc(size + offset, ALLOC_KERN);
|
||||
|
||||
if (!inode->data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
inode->file_size = size + offset;
|
||||
}
|
||||
|
||||
if (inode->file_size < size + offset) {
|
||||
size_t new_size = size + offset;
|
||||
inode->data = kzalloc(new_size, ALLOC_KERN);
|
||||
if (!inode->data)
|
||||
return -ENOMEM;
|
||||
inode->file_size = new_size;
|
||||
} else if (inode->file_size < new_size) {
|
||||
u8 *new_data = krealloc(inode->data, new_size, ALLOC_KERN);
|
||||
if (!new_data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
if (!new_data)
|
||||
return -ENOMEM;
|
||||
if (offset > inode->file_size)
|
||||
memset(new_data + inode->file_size, 0, offset - inode->file_size);
|
||||
inode->data = new_data;
|
||||
|
|
@ -137,10 +121,7 @@ static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
|||
}
|
||||
|
||||
memcpy(inode->data + offset, buf, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
return ret;
|
||||
return size;
|
||||
}
|
||||
|
||||
static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
||||
|
|
@ -149,9 +130,8 @@ static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
|||
return -EINVAL;
|
||||
|
||||
dirent_t *new = dirent_create(name);
|
||||
if (!new) {
|
||||
if (!new)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
new->inode = to;
|
||||
new->fs = in->fs;
|
||||
|
|
@ -162,7 +142,6 @@ static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
|||
}
|
||||
|
||||
*out = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -173,15 +152,19 @@ static int unlink(dirent_t *dent)
|
|||
|
||||
static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
||||
{
|
||||
/* count first */
|
||||
list_item_t *item = parent->dirents.head;
|
||||
ssize_t n = 0;
|
||||
|
||||
while (item) {
|
||||
n++;
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
/* now copy */
|
||||
if (n == 0) {
|
||||
*dents = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN);
|
||||
if (!arr)
|
||||
return -ENOMEM;
|
||||
|
|
@ -189,11 +172,24 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
|||
item = parent->dirents.head;
|
||||
n = 0;
|
||||
while (item) {
|
||||
memcpy(arr + n, item->ptr, sizeof(dirent_t));
|
||||
dirent_t *copy = dirent_dup(item->ptr);
|
||||
if (!copy) {
|
||||
/* free already-copied entries before returning */
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
if (arr[i].inode)
|
||||
inode_dec_ref(arr[i].inode);
|
||||
kfree(arr[i].name);
|
||||
}
|
||||
kfree(arr);
|
||||
return -ENOMEM;
|
||||
}
|
||||
arr[n] = *copy;
|
||||
kfree(copy);
|
||||
n++;
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
*dents = arr;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +209,7 @@ static int umount(fs_t *fs)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
void ramfs_init()
|
||||
void ramfs_init(void)
|
||||
{
|
||||
void *ops = kalloc(sizeof(struct fs_ops) + sizeof(struct inode_ops), ALLOC_KERN);
|
||||
if (!ops) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue