vfs: fix logic errors and leaks in vfs_open
Signed-off-by: Michael Shaw <m@michaelsshaw.com>
This commit is contained in:
parent
a8540494a9
commit
dd00e79b18
3 changed files with 69 additions and 22 deletions
79
fs/vfs/vfs.c
79
fs/vfs/vfs.c
|
|
@ -14,14 +14,17 @@ static list_t fs_type_list = {0};
|
|||
|
||||
static char *path_trim(const char *name)
|
||||
{
|
||||
if (!*name)
|
||||
return NULL;
|
||||
|
||||
size_t len = strlen(name);
|
||||
char *ret = kzalloc(len, ALLOC_KERN);
|
||||
const char *zeroptr = NULL;
|
||||
char *ret = kzalloc(len + 1, ALLOC_KERN);
|
||||
const char *zeroptr = name + len;
|
||||
|
||||
/* trim trailing slashes */
|
||||
for (size_t i = len - 1; i >= 1; i++) {
|
||||
for (size_t i = len - 1; i >= 1; i--) {
|
||||
if (name[i] == '/')
|
||||
zeroptr = &name[i];
|
||||
zeroptr = name + i;
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
|
@ -30,7 +33,8 @@ static char *path_trim(const char *name)
|
|||
char c;
|
||||
char last = 0;
|
||||
size_t i = 0;
|
||||
while (name < zeroptr && (c = *name++)) {
|
||||
const char *cursor = name;
|
||||
while (cursor < zeroptr && (c = *cursor++)) {
|
||||
if (last == c && c == '/')
|
||||
continue;
|
||||
|
||||
|
|
@ -88,21 +92,45 @@ int vfs_open(const char *abs_path, inode_t **out)
|
|||
char *path_optr = path_trim(abs_path);
|
||||
char *cur_path = path_optr;
|
||||
list_item_t *item;
|
||||
size_t ret = -ENOENT;
|
||||
|
||||
list_t visited;
|
||||
list_create(&visited);
|
||||
if (!cur_path)
|
||||
return -ENOENT;
|
||||
|
||||
/* root case */
|
||||
if (*cur_path == '\0') {
|
||||
inode_inc_ref(c_ino);
|
||||
*out = c_ino;
|
||||
return 0;
|
||||
ret = 0;
|
||||
goto out_1;
|
||||
}
|
||||
|
||||
list_t visited;
|
||||
list_create(&visited);
|
||||
inode_inc_ref(c_ino);
|
||||
list_insert(&visited, c_ino);
|
||||
|
||||
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;
|
||||
|
||||
|
|
@ -111,29 +139,36 @@ int vfs_open(const char *abs_path, inode_t **out)
|
|||
|
||||
list_insert(&visited, c_ino);
|
||||
|
||||
if (res < 0)
|
||||
goto out;
|
||||
|
||||
/* found */
|
||||
if (res == 0) {
|
||||
if (*cur_path == '\0') {
|
||||
*out = c_ino;
|
||||
return 0;
|
||||
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;
|
||||
}
|
||||
|
||||
cur_path += res;
|
||||
}
|
||||
|
||||
out:
|
||||
item = visited.head;
|
||||
while (item) {
|
||||
inode_t *inode = item->ptr;
|
||||
inode_dec_ref(inode);
|
||||
item = item->next;
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
list_destroy(&visited);
|
||||
out_1:
|
||||
kfree(path_optr);
|
||||
|
||||
return -ENOENT;
|
||||
return ret;
|
||||
}
|
||||
|
||||
void vfs_close(inode_t *inode)
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ typedef struct _list_head {
|
|||
} list_t;
|
||||
|
||||
void list_create(list_t *list);
|
||||
void list_destroy(list_t * list);
|
||||
list_item_t *list_insert(list_t *list, void *item);
|
||||
void list_remove(list_item_t *item);
|
||||
void list_init();
|
||||
|
|
|
|||
11
lib/list.c
11
lib/list.c
|
|
@ -12,6 +12,17 @@ void list_create(list_t *list)
|
|||
list->tail = NULL;
|
||||
}
|
||||
|
||||
void list_destroy(list_t *list)
|
||||
{
|
||||
list_item_t *item = list->head;
|
||||
|
||||
while (item) {
|
||||
list_item_t *next = item->next;
|
||||
slab_free(list_cache, item);
|
||||
item = next;
|
||||
}
|
||||
}
|
||||
|
||||
list_item_t *list_insert(list_t *list, void *item)
|
||||
{
|
||||
spinlock_acquire(&list->lock);
|
||||
|
|
|
|||
Loading…
Reference in a new issue