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
184
fs/vfs/vfs.c
184
fs/vfs/vfs.c
|
|
@ -11,43 +11,29 @@ static list_t fs_type_list = {0};
|
|||
|
||||
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);
|
||||
|
||||
if (len == 0)
|
||||
return kzalloc(1, ALLOC_KERN);
|
||||
|
||||
const char *end = name + len;
|
||||
while (end > name && *(end - 1) == '/')
|
||||
end--;
|
||||
|
||||
char *ret = kzalloc((end - name) + 1, ALLOC_KERN);
|
||||
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] == '/')
|
||||
zeroptr = name + i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
/* copy, collapsing doubled slashes */
|
||||
char c;
|
||||
char last = 0;
|
||||
size_t i = 0;
|
||||
const char *cursor = name;
|
||||
while (cursor < zeroptr && (c = *cursor++)) {
|
||||
if (last == c && c == '/')
|
||||
size_t j = 0;
|
||||
for (const char *c = name; c < end; c++) {
|
||||
if (last == '/' && *c == '/')
|
||||
continue;
|
||||
|
||||
last = c;
|
||||
ret[i++] = last;
|
||||
ret[j++] = *c;
|
||||
last = *c;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
|
@ -59,14 +45,17 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
return -EINVAL;
|
||||
|
||||
int res;
|
||||
char build[256] = {0};
|
||||
char *build = kzalloc(256, ALLOC_KERN);
|
||||
if (!build)
|
||||
return -ENOMEM;
|
||||
|
||||
for (int i = 0; i < sizeof(build) - 1; i++) {
|
||||
for (int i = 0; i < 255; i++) {
|
||||
char c = path[i];
|
||||
|
||||
if (c == '/' || c == '\0') {
|
||||
dirent_t *dirent;
|
||||
res = inode_find_dirent(dent->inode, build, &dirent);
|
||||
kfree(build);
|
||||
if (res < 0)
|
||||
return -ENOENT;
|
||||
|
||||
|
|
@ -78,7 +67,7 @@ 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);
|
||||
dirent_dec_ref(dirent);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
|
@ -92,49 +81,45 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent)
|
|||
}
|
||||
}
|
||||
|
||||
kfree(build);
|
||||
return -ENOENT;
|
||||
}
|
||||
|
||||
/* opens a dirent by absolute path only
|
||||
*
|
||||
* IMPORTANT: ownership of the out dirent is passed to the caller, and managing
|
||||
* its refcount is up to the caller (e.g. dirent_dec_ref)
|
||||
*/
|
||||
int vfs_open(const char *abs_path, dirent_t **out)
|
||||
{
|
||||
if (!abs_path || !out)
|
||||
return -EINVAL;
|
||||
|
||||
if (!root_fs->root_dirent)
|
||||
return -ENOENT;
|
||||
|
||||
char *path_optr = path_trim(abs_path);
|
||||
if (!path_optr)
|
||||
return -ENOMEM;
|
||||
|
||||
char *cur_path = path_optr;
|
||||
dirent_t *current_dir = root_fs->root_dirent;
|
||||
int ret = -ENOENT;
|
||||
dirent_inc_ref(current_dir);
|
||||
|
||||
dirent_inc_ref_lock(current_dir);
|
||||
|
||||
/* Handle root path "/" */
|
||||
if (strlen(abs_path) == 1 && *abs_path == '/') {
|
||||
if (*path_optr == '\0') {
|
||||
*out = current_dir;
|
||||
kfree(path_optr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char *cur_path = path_optr;
|
||||
int ret = -ENOENT;
|
||||
|
||||
while (1) {
|
||||
dirent_t *next_dir;
|
||||
|
||||
int res = next_level(cur_path, current_dir, &next_dir);
|
||||
|
||||
if (res < 0) {
|
||||
dirent_dec_ref_lock(current_dir);
|
||||
dirent_dec_ref(current_dir);
|
||||
ret = res;
|
||||
goto out;
|
||||
}
|
||||
|
||||
dirent_dec_ref_lock(current_dir);
|
||||
|
||||
dirent_dec_ref(current_dir);
|
||||
current_dir = next_dir;
|
||||
cur_path += res;
|
||||
|
||||
|
|
@ -155,18 +140,16 @@ out:
|
|||
|
||||
void vfs_close(dirent_t *dent)
|
||||
{
|
||||
dirent_dec_ref_lock(dent);
|
||||
dirent_dec_ref(dent);
|
||||
}
|
||||
|
||||
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);
|
||||
ssize_t ret = ino->i_ops.write(ino, buf, size, offset);
|
||||
write_unlock(&ino->rwlock);
|
||||
|
||||
return ret;
|
||||
|
|
@ -174,13 +157,11 @@ ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset)
|
|||
|
||||
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);
|
||||
ssize_t ret = ino->i_ops.read(ino, buf, size, offset);
|
||||
read_unlock(&ino->rwlock);
|
||||
|
||||
return ret;
|
||||
|
|
@ -193,74 +174,94 @@ int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t
|
|||
|
||||
inode_t *ino = parent->inode;
|
||||
size_t len = strlen(name);
|
||||
int res;
|
||||
int ret = 0;
|
||||
if (len > 255)
|
||||
return -ENAMETOOLONG;
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY)
|
||||
return -ENOTDIR;
|
||||
|
||||
dirent_t *test = NULL;
|
||||
res = inode_find_dirent(ino, name, &test);
|
||||
|
||||
/* best-effort first check */
|
||||
if (res == 0) {
|
||||
dirent_dec_ref_lock(test);
|
||||
return -EEXIST;
|
||||
}
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
ret = -ENOTDIR;
|
||||
goto out_err;
|
||||
write_unlock(&ino->rwlock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
||||
/* re-check under lock */
|
||||
test = NULL;
|
||||
res = inode_find_dirent_locked(ino, name, &test);
|
||||
dirent_t *test = NULL;
|
||||
int res = inode_find_dirent_locked(ino, name, &test);
|
||||
if (res == 0) {
|
||||
/* exists */
|
||||
ret = -EEXIST;
|
||||
goto out_err;
|
||||
dirent_dec_ref(test);
|
||||
write_unlock(&ino->rwlock);
|
||||
return -EEXIST;
|
||||
} else if (res != -ENOENT) {
|
||||
/* various other error codes */
|
||||
ret = res;
|
||||
goto out_err;
|
||||
write_unlock(&ino->rwlock);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* create inode */
|
||||
fs_t *fs = parent->fs;
|
||||
inode_t *new_ino;
|
||||
dirent_t *new_dirent;
|
||||
|
||||
res = fs->fs_ops.alloc_inode(fs, mode, &new_ino);
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
goto out_err;
|
||||
write_unlock(&ino->rwlock);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = ino->i_ops.link(parent, new_ino, name, &new_dirent);
|
||||
|
||||
if (res < 0) {
|
||||
ret = res;
|
||||
|
||||
res = fs->fs_ops.free_inode(fs, new_ino);
|
||||
if (res < 0)
|
||||
kerr("failed to link inode, then failed to free it!\n");
|
||||
|
||||
goto out_err;
|
||||
fs->fs_ops.free_inode(fs, new_ino);
|
||||
write_unlock(&ino->rwlock);
|
||||
return res;
|
||||
}
|
||||
|
||||
*out = new_dirent;
|
||||
|
||||
out_err:
|
||||
if (test)
|
||||
dirent_dec_ref_lock(test);
|
||||
write_unlock(&ino->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
int vfs_unlink(dirent_t *parent, const char *name)
|
||||
{
|
||||
if (!parent || !name)
|
||||
return -EINVAL;
|
||||
|
||||
inode_t *ino = parent->inode;
|
||||
|
||||
write_lock(&ino->rwlock);
|
||||
|
||||
if (ino->mode != INODE_DIRECTORY) {
|
||||
write_unlock(&ino->rwlock);
|
||||
return -ENOTDIR;
|
||||
}
|
||||
|
||||
dirent_t *dent = NULL;
|
||||
int res = inode_find_dirent_locked(ino, name, &dent);
|
||||
if (res < 0) {
|
||||
write_unlock(&ino->rwlock);
|
||||
return res;
|
||||
}
|
||||
|
||||
res = ino->i_ops.unlink(dent);
|
||||
if (res < 0) {
|
||||
dirent_dec_ref(dent);
|
||||
write_unlock(&ino->rwlock);
|
||||
return res;
|
||||
}
|
||||
|
||||
list_item_t *item = ino->dirents.head;
|
||||
while (item) {
|
||||
if (item->ptr == dent) {
|
||||
list_remove(item);
|
||||
break;
|
||||
}
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
dirent_dec_ref(dent);
|
||||
|
||||
write_unlock(&ino->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount)
|
||||
|
|
@ -284,9 +285,8 @@ void vfs_register(struct fs_type *fs)
|
|||
kfree(ins);
|
||||
}
|
||||
|
||||
void vfs_init()
|
||||
void vfs_init(void)
|
||||
{
|
||||
void inode_init();
|
||||
inode_init();
|
||||
root_fs = kalloc(sizeof(fs_t), ALLOC_KERN);
|
||||
memset(root_fs, 0, sizeof(*root_fs));
|
||||
|
|
|
|||
Loading…
Reference in a new issue