vfs: fix various logical errors in the vfs
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
ab15b88cd6
commit
eb41b17fea
6 changed files with 146 additions and 45 deletions
45
fs/vfs/vfs.c
45
fs/vfs/vfs.c
|
|
@ -14,12 +14,21 @@ static char *path_trim(const char *name)
|
|||
if (!*name)
|
||||
return NULL;
|
||||
|
||||
/* skip leading slashes so absolute paths resolve correctly */
|
||||
while (*name == '/')
|
||||
name++;
|
||||
|
||||
size_t len = strlen(name);
|
||||
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
||||
const char *zeroptr = name + len;
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
/* empty after stripping slashes (e.g. "/") */
|
||||
if (len == 0)
|
||||
return ret;
|
||||
|
||||
const char *zeroptr = name + len;
|
||||
|
||||
/* trim trailing slashes */
|
||||
for (size_t i = len - 1; i >= 1; i--) {
|
||||
if (name[i] == '/')
|
||||
|
|
@ -28,7 +37,7 @@ static char *path_trim(const char *name)
|
|||
break;
|
||||
}
|
||||
|
||||
/* trim double slashes */
|
||||
/* copy, collapsing doubled slashes */
|
||||
char c;
|
||||
char last = 0;
|
||||
size_t i = 0;
|
||||
|
|
@ -69,11 +78,11 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
res = fs->fs_ops.open_inode(fs, inum, dirent, &dirent->inode);
|
||||
if (res < 0) {
|
||||
spinlock_release(&dirent->lock);
|
||||
dirent_dec_ref_lock(dirent);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
dirent_inc_ref(dirent);
|
||||
spinlock_release(&dirent->lock);
|
||||
|
||||
*out_dirent = dirent;
|
||||
|
|
@ -98,7 +107,7 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
|
||||
char *path_optr = path_trim(abs_path);
|
||||
if (!path_optr)
|
||||
return -ENOENT;
|
||||
return -ENOMEM;
|
||||
|
||||
char *cur_path = path_optr;
|
||||
dirent_t *current_dir = root_fs->root_dirent;
|
||||
|
|
@ -124,7 +133,6 @@ int vfs_open(const char *abs_path, dirent_t **out)
|
|||
goto out;
|
||||
}
|
||||
|
||||
dirent_inc_ref_lock(next_dir);
|
||||
dirent_dec_ref_lock(current_dir);
|
||||
|
||||
current_dir = next_dir;
|
||||
|
|
@ -154,6 +162,9 @@ ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
{
|
||||
ssize_t ret;
|
||||
|
||||
if (!ino)
|
||||
return -EINVAL;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
ret = ino->i_ops.write(ino, buf, size, offset);
|
||||
write_unlock(&ino->rwlock);
|
||||
|
|
@ -165,6 +176,9 @@ ssize_t vfs_read(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
{
|
||||
ssize_t ret;
|
||||
|
||||
if (!ino)
|
||||
return -EINVAL;
|
||||
|
||||
read_lock(&ino->rwlock);
|
||||
ret = ino->i_ops.read(ino, buf, size, offset);
|
||||
read_unlock(&ino->rwlock);
|
||||
|
|
@ -184,6 +198,9 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
if (len > 255)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY)
|
||||
return -ENOTDIR;
|
||||
|
||||
dirent_t *test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
|
||||
|
|
@ -195,9 +212,14 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
/* re-check under lock */
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
ret = -ENOTDIR;
|
||||
goto out_err;
|
||||
}
|
||||
|
||||
/* re-check under lock */
|
||||
test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
res = inode_find_dirent_locked(ino, name, &test);
|
||||
if (res == 0) {
|
||||
/* exists */
|
||||
ret = -EEXIST;
|
||||
|
|
@ -245,18 +267,21 @@ int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
|||
{
|
||||
if (!mount_point || !strcmp(mount_point, "/")) {
|
||||
root_fs = fs_to_mount;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return -ENOSYS;
|
||||
}
|
||||
|
||||
void vfs_register(struct fs_type *fs)
|
||||
{
|
||||
struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN);
|
||||
if (!ins)
|
||||
return;
|
||||
|
||||
memcpy(ins, fs, sizeof(*fs));
|
||||
list_insert(&fs_type_list, ins);
|
||||
if (!list_insert(&fs_type_list, ins))
|
||||
kfree(ins);
|
||||
}
|
||||
|
||||
void vfs_init()
|
||||
|
|
|
|||
Loading…
Reference in a new issue