vfs: further fix logic errors in vfs_open
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
dd00e79b18
commit
5c55192c76
1 changed files with 22 additions and 45 deletions
67
fs/vfs/vfs.c
67
fs/vfs/vfs.c
|
|
@ -20,6 +20,8 @@ static char *path_trim(const char *name)
|
|||
size_t len = strlen(name);
|
||||
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
||||
const char *zeroptr = name + len;
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
/* trim trailing slashes */
|
||||
for (size_t i = len - 1; i >= 1; i--) {
|
||||
|
|
@ -64,6 +66,7 @@ static int next_level(const char *path, inode_t **cur)
|
|||
spinlock_acquire(&dirent->lock);
|
||||
if (dirent->inode) {
|
||||
*cur = dirent->inode;
|
||||
inode_inc_ref(*cur);
|
||||
} else {
|
||||
fs_t *fs = (*cur)->fs;
|
||||
res = fs->fs_ops.open_inode(fs, dirent->ino_num, cur);
|
||||
|
|
@ -73,7 +76,6 @@ static int next_level(const char *path, inode_t **cur)
|
|||
}
|
||||
}
|
||||
spinlock_release(&dirent->lock);
|
||||
inode_inc_ref(*cur);
|
||||
return i;
|
||||
} else {
|
||||
build[i] = c;
|
||||
|
|
@ -97,75 +99,50 @@ int vfs_open(const char *abs_path, inode_t **out)
|
|||
if (!cur_path)
|
||||
return -ENOENT;
|
||||
|
||||
/* root case */
|
||||
if (*cur_path == '\0') {
|
||||
inode_inc_ref(c_ino);
|
||||
*out = c_ino;
|
||||
ret = 0;
|
||||
goto out_1;
|
||||
}
|
||||
|
||||
list_t visited;
|
||||
list_create(&visited);
|
||||
inode_inc_ref(c_ino);
|
||||
list_insert(&visited, c_ino);
|
||||
|
||||
if (strlen(abs_path) == 1 && *abs_path == '/') {
|
||||
inode_inc_ref(c_ino);
|
||||
*out = c_ino;
|
||||
kfree(path_optr);
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
if (*cur_path == '\0') {
|
||||
*out = c_ino;
|
||||
ret = 0;
|
||||
|
||||
/* Clean intermediate elements, keeping only target node reference alive */
|
||||
item = visited.head;
|
||||
while (item && item->next) {
|
||||
inode_dec_ref(item->ptr);
|
||||
item = item->next;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
int res = next_level(cur_path, &c_ino);
|
||||
|
||||
|
||||
if (res < 0)
|
||||
goto out;
|
||||
|
||||
cur_path += res;
|
||||
|
||||
while (*cur_path == '/')
|
||||
cur_path++;
|
||||
|
||||
if (!c_ino)
|
||||
goto out;
|
||||
|
||||
list_insert(&visited, c_ino);
|
||||
|
||||
/* found */
|
||||
if (*cur_path == '\0') {
|
||||
*out = c_ino;
|
||||
ret = 0;
|
||||
|
||||
/* Clean intermediate elements, keeping only target node reference alive */
|
||||
item = visited.head;
|
||||
while (item && item->next) {
|
||||
inode_dec_ref(item->ptr);
|
||||
item = item->next;
|
||||
}
|
||||
break;
|
||||
goto out;
|
||||
}
|
||||
|
||||
list_insert(&visited, c_ino);
|
||||
}
|
||||
|
||||
out:
|
||||
/* If we hit an error, ret remains -ENOENT and we unwind everything */
|
||||
if (ret != 0) {
|
||||
item = visited.head;
|
||||
while (item) {
|
||||
inode_t *inode = item->ptr;
|
||||
inode_dec_ref(inode);
|
||||
item = item->next;
|
||||
}
|
||||
item = visited.head;
|
||||
while (item) {
|
||||
inode_t *inode = item->ptr;
|
||||
inode_dec_ref(inode);
|
||||
item = item->next;
|
||||
}
|
||||
list_destroy(&visited);
|
||||
out_1:
|
||||
kfree(path_optr);
|
||||
|
||||
return ret;
|
||||
|
|
|
|||
Loading…
Reference in a new issue