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
51
fs/vfs/vfs.c
51
fs/vfs/vfs.c
|
|
@ -20,6 +20,8 @@ static char *path_trim(const char *name)
|
||||||
size_t len = strlen(name);
|
size_t len = strlen(name);
|
||||||
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
||||||
const char *zeroptr = name + len;
|
const char *zeroptr = name + len;
|
||||||
|
if (!ret)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/* trim trailing slashes */
|
/* trim trailing slashes */
|
||||||
for (size_t i = len - 1; i >= 1; i--) {
|
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);
|
spinlock_acquire(&dirent->lock);
|
||||||
if (dirent->inode) {
|
if (dirent->inode) {
|
||||||
*cur = dirent->inode;
|
*cur = dirent->inode;
|
||||||
|
inode_inc_ref(*cur);
|
||||||
} else {
|
} else {
|
||||||
fs_t *fs = (*cur)->fs;
|
fs_t *fs = (*cur)->fs;
|
||||||
res = fs->fs_ops.open_inode(fs, dirent->ino_num, cur);
|
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);
|
spinlock_release(&dirent->lock);
|
||||||
inode_inc_ref(*cur);
|
|
||||||
return i;
|
return i;
|
||||||
} else {
|
} else {
|
||||||
build[i] = c;
|
build[i] = c;
|
||||||
|
|
@ -97,36 +99,19 @@ int vfs_open(const char *abs_path, inode_t **out)
|
||||||
if (!cur_path)
|
if (!cur_path)
|
||||||
return -ENOENT;
|
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_t visited;
|
||||||
list_create(&visited);
|
list_create(&visited);
|
||||||
inode_inc_ref(c_ino);
|
inode_inc_ref(c_ino);
|
||||||
list_insert(&visited, c_ino);
|
list_insert(&visited, c_ino);
|
||||||
|
|
||||||
while (1) {
|
if (strlen(abs_path) == 1 && *abs_path == '/') {
|
||||||
while (*cur_path == '/')
|
inode_inc_ref(c_ino);
|
||||||
cur_path++;
|
|
||||||
|
|
||||||
if (*cur_path == '\0') {
|
|
||||||
*out = c_ino;
|
*out = c_ino;
|
||||||
ret = 0;
|
kfree(path_optr);
|
||||||
|
return 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
while (1) {
|
||||||
int res = next_level(cur_path, &c_ino);
|
int res = next_level(cur_path, &c_ino);
|
||||||
|
|
||||||
if (res < 0)
|
if (res < 0)
|
||||||
|
|
@ -134,38 +119,30 @@ int vfs_open(const char *abs_path, inode_t **out)
|
||||||
|
|
||||||
cur_path += res;
|
cur_path += res;
|
||||||
|
|
||||||
|
while (*cur_path == '/')
|
||||||
|
cur_path++;
|
||||||
|
|
||||||
if (!c_ino)
|
if (!c_ino)
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
list_insert(&visited, c_ino);
|
|
||||||
|
|
||||||
/* found */
|
/* found */
|
||||||
if (*cur_path == '\0') {
|
if (*cur_path == '\0') {
|
||||||
*out = c_ino;
|
*out = c_ino;
|
||||||
ret = 0;
|
ret = 0;
|
||||||
|
goto out;
|
||||||
|
}
|
||||||
|
|
||||||
/* Clean intermediate elements, keeping only target node reference alive */
|
list_insert(&visited, c_ino);
|
||||||
item = visited.head;
|
|
||||||
while (item && item->next) {
|
|
||||||
inode_dec_ref(item->ptr);
|
|
||||||
item = item->next;
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
out:
|
out:
|
||||||
/* If we hit an error, ret remains -ENOENT and we unwind everything */
|
|
||||||
if (ret != 0) {
|
|
||||||
item = visited.head;
|
item = visited.head;
|
||||||
while (item) {
|
while (item) {
|
||||||
inode_t *inode = item->ptr;
|
inode_t *inode = item->ptr;
|
||||||
inode_dec_ref(inode);
|
inode_dec_ref(inode);
|
||||||
item = item->next;
|
item = item->next;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
list_destroy(&visited);
|
list_destroy(&visited);
|
||||||
out_1:
|
|
||||||
kfree(path_optr);
|
kfree(path_optr);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue