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
129
fs/vfs/inode.c
129
fs/vfs/inode.c
|
|
@ -11,45 +11,36 @@
|
|||
static mem_cache_t *inode_slab = NULL;
|
||||
static mem_cache_t *dirent_slab = NULL;
|
||||
|
||||
int inode_populate_dirents(inode_t *inode)
|
||||
static int inode_populate_dirents(inode_t *inode)
|
||||
{
|
||||
/* write_lock(&inode->rwlock) is held by caller */
|
||||
write_lock(&inode->rwlock);
|
||||
|
||||
if (inode->dirents.head != NULL)
|
||||
if (inode->dirents.head != NULL) {
|
||||
write_unlock(&inode->rwlock);
|
||||
return 0;
|
||||
}
|
||||
|
||||
dirent_t *dents;
|
||||
ssize_t n = inode->i_ops.get_dents(inode, &dents);
|
||||
|
||||
if (n < 0)
|
||||
if (n < 0) {
|
||||
write_unlock(&inode->rwlock);
|
||||
return n;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
dirent_t *dent = dents + i;
|
||||
dirent_t *insert = dirent_create(dent->name);
|
||||
dirent_t *insert = dirent_dup(dent);
|
||||
if (!insert)
|
||||
goto cleanup;
|
||||
|
||||
insert->ino_num = dent->ino_num;
|
||||
insert->rec_len = dent->rec_len;
|
||||
insert->name_len = dent->name_len;
|
||||
insert->flags = dent->flags;
|
||||
insert->type = dent->type;
|
||||
insert->fs = dent->fs;
|
||||
|
||||
if (dent->inode) {
|
||||
inode_inc_ref_lock(dent->inode);
|
||||
insert->inode = dent->inode;
|
||||
}
|
||||
|
||||
if (!list_insert(&inode->dirents, insert)) {
|
||||
if (insert->inode)
|
||||
inode_dec_ref_lock(insert->inode);
|
||||
dirent_free(insert);
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
|
||||
write_unlock(&inode->rwlock);
|
||||
kfree(dents);
|
||||
return 0;
|
||||
|
||||
|
|
@ -61,11 +52,12 @@ cleanup:
|
|||
list_item_t *next = item->next;
|
||||
list_remove(item);
|
||||
if (dent->inode)
|
||||
inode_dec_ref_lock(dent->inode);
|
||||
inode_dec_ref(dent->inode);
|
||||
dirent_free(dent);
|
||||
item = next;
|
||||
}
|
||||
}
|
||||
write_unlock(&inode->rwlock);
|
||||
kfree(dents);
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
|
@ -76,7 +68,7 @@ int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out)
|
|||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (!strcmp(dent->name, name)) {
|
||||
dirent_inc_ref_lock(dent);
|
||||
dirent_inc_ref(dent);
|
||||
*out = dent;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -94,15 +86,9 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
|||
|
||||
if (inode->dirents.head == NULL) {
|
||||
read_unlock(&inode->rwlock);
|
||||
write_lock(&inode->rwlock);
|
||||
if (inode->dirents.head == NULL) {
|
||||
ssize_t res = inode_populate_dirents(inode);
|
||||
write_unlock(&inode->rwlock);
|
||||
if (res < 0)
|
||||
return res;
|
||||
} else {
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
int res = inode_populate_dirents(inode);
|
||||
if (res < 0)
|
||||
return res;
|
||||
read_lock(&inode->rwlock);
|
||||
}
|
||||
|
||||
|
|
@ -111,7 +97,7 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out)
|
|||
return ret;
|
||||
}
|
||||
|
||||
inode_t *inode_create()
|
||||
inode_t *inode_create(void)
|
||||
{
|
||||
inode_t *ret = slab_alloc(inode_slab);
|
||||
|
||||
|
|
@ -128,18 +114,18 @@ inode_t *inode_create()
|
|||
|
||||
void inode_free(inode_t *inode)
|
||||
{
|
||||
/* caller must hold write_lock(&inode->rwlock) */
|
||||
|
||||
if (inode->dirents.head) {
|
||||
while (inode->dirents.head) {
|
||||
list_item_t *item = inode->dirents.head;
|
||||
while (item) {
|
||||
dirent_t *dent = item->ptr;
|
||||
if (dent)
|
||||
dirent_dec_ref_lock(dent);
|
||||
item = item->next;
|
||||
dirent_t *dent = item->ptr;
|
||||
list_remove(item);
|
||||
if (dent) {
|
||||
if (dent->inode)
|
||||
inode_dec_ref(dent->inode);
|
||||
dirent_free(dent);
|
||||
}
|
||||
}
|
||||
list_destroy(&inode->dirents);
|
||||
|
||||
kfree(inode->data);
|
||||
slab_free(inode_slab, inode);
|
||||
}
|
||||
|
||||
|
|
@ -155,36 +141,18 @@ void inode_dec_ref(inode_t *inode)
|
|||
if (!inode)
|
||||
return;
|
||||
|
||||
if (__sync_sub_and_fetch(&inode->refcount, 1) == 0) {
|
||||
if (__sync_sub_and_fetch(&inode->refcount, 1) == 0)
|
||||
inode_free(inode);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void dirent_inc_ref(dirent_t *dent)
|
||||
{
|
||||
dent->refcount += 1;
|
||||
}
|
||||
|
||||
void dirent_dec_ref(dirent_t *dent)
|
||||
{
|
||||
if (dent->refcount == 0)
|
||||
return;
|
||||
dent->refcount -= 1;
|
||||
if (dent->refcount == 0) {
|
||||
inode_dec_ref_lock(dent->inode);
|
||||
dirent_free(dent);
|
||||
}
|
||||
}
|
||||
|
||||
void dirent_inc_ref_lock(dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&dent->lock);
|
||||
dirent_inc_ref(dent);
|
||||
dent->refcount += 1;
|
||||
spinlock_release(&dent->lock);
|
||||
}
|
||||
|
||||
void dirent_dec_ref_lock(dirent_t *dent)
|
||||
void dirent_dec_ref(dirent_t *dent)
|
||||
{
|
||||
spinlock_acquire(&dent->lock);
|
||||
if (dent->refcount == 0) {
|
||||
|
|
@ -196,25 +164,11 @@ void dirent_dec_ref_lock(dirent_t *dent)
|
|||
spinlock_release(&dent->lock);
|
||||
|
||||
if (freed) {
|
||||
inode_dec_ref_lock(dent->inode);
|
||||
inode_dec_ref(dent->inode);
|
||||
dirent_free(dent);
|
||||
}
|
||||
}
|
||||
|
||||
void inode_inc_ref_lock(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode_inc_ref(inode);
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
void inode_dec_ref_lock(inode_t *inode)
|
||||
{
|
||||
write_lock(&inode->rwlock);
|
||||
inode_dec_ref(inode);
|
||||
write_unlock(&inode->rwlock);
|
||||
}
|
||||
|
||||
dirent_t *dirent_create(const char *name)
|
||||
{
|
||||
dirent_t *ret = slab_alloc(dirent_slab);
|
||||
|
|
@ -237,13 +191,34 @@ dirent_t *dirent_create(const char *name)
|
|||
return ret;
|
||||
}
|
||||
|
||||
dirent_t *dirent_dup(dirent_t *src)
|
||||
{
|
||||
dirent_t *ret = dirent_create(src->name);
|
||||
if (!ret)
|
||||
return NULL;
|
||||
|
||||
ret->ino_num = src->ino_num;
|
||||
ret->rec_len = src->rec_len;
|
||||
ret->name_len = src->name_len;
|
||||
ret->flags = src->flags;
|
||||
ret->type = src->type;
|
||||
ret->fs = src->fs;
|
||||
|
||||
if (src->inode) {
|
||||
inode_inc_ref(src->inode);
|
||||
ret->inode = src->inode;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void dirent_free(dirent_t *dirent)
|
||||
{
|
||||
kfree(dirent->name);
|
||||
slab_free(dirent_slab, dirent);
|
||||
}
|
||||
|
||||
void inode_init()
|
||||
void inode_init(void)
|
||||
{
|
||||
inode_slab = slab_create("inode_slab", sizeof(inode_t), 64 * KB, 0x8);
|
||||
if (!inode_slab)
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ int ramfs_create(u64 flags, fs_t **out)
|
|||
return 0;
|
||||
|
||||
out_root_inode:
|
||||
inode_dec_ref_lock(root_inode);
|
||||
inode_dec_ref(root_inode);
|
||||
dirent_free(root);
|
||||
out_root:
|
||||
kfree(fs);
|
||||
|
|
@ -77,8 +77,7 @@ static int alloc_inode(fs_t *fs, u64 mode, inode_t **out)
|
|||
static int free_inode(fs_t *fs, inode_t *inode)
|
||||
{
|
||||
kfree(inode->data);
|
||||
inode_dec_ref_lock(inode);
|
||||
|
||||
inode_dec_ref(inode);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -89,13 +88,9 @@ static int open_inode(fs_t *fs, u64 id, dirent_t *dirent, inode_t **out)
|
|||
|
||||
static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
if (!inode->data)
|
||||
if (!inode->data || inode->file_size == 0)
|
||||
return 0;
|
||||
|
||||
if (inode->file_size == 0)
|
||||
goto out;
|
||||
|
||||
if (offset >= inode->file_size)
|
||||
return -EINVAL;
|
||||
|
||||
|
|
@ -103,33 +98,22 @@ static ssize_t read(inode_t *inode, char *buf, size_t size, size_t offset)
|
|||
size = inode->file_size - offset;
|
||||
|
||||
memcpy(buf, inode->data + offset, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
return ret;
|
||||
return size;
|
||||
}
|
||||
|
||||
static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
||||
{
|
||||
ssize_t ret = 0;
|
||||
size_t new_size = size + offset;
|
||||
|
||||
if (!inode->data) {
|
||||
inode->data = kalloc(size + offset, ALLOC_KERN);
|
||||
|
||||
if (!inode->data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
|
||||
inode->file_size = size + offset;
|
||||
}
|
||||
|
||||
if (inode->file_size < size + offset) {
|
||||
size_t new_size = size + offset;
|
||||
inode->data = kzalloc(new_size, ALLOC_KERN);
|
||||
if (!inode->data)
|
||||
return -ENOMEM;
|
||||
inode->file_size = new_size;
|
||||
} else if (inode->file_size < new_size) {
|
||||
u8 *new_data = krealloc(inode->data, new_size, ALLOC_KERN);
|
||||
if (!new_data) {
|
||||
ret = -ENOMEM;
|
||||
goto out;
|
||||
}
|
||||
if (!new_data)
|
||||
return -ENOMEM;
|
||||
if (offset > inode->file_size)
|
||||
memset(new_data + inode->file_size, 0, offset - inode->file_size);
|
||||
inode->data = new_data;
|
||||
|
|
@ -137,10 +121,7 @@ static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset)
|
|||
}
|
||||
|
||||
memcpy(inode->data + offset, buf, size);
|
||||
ret = size;
|
||||
|
||||
out:
|
||||
return ret;
|
||||
return size;
|
||||
}
|
||||
|
||||
static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
||||
|
|
@ -149,9 +130,8 @@ static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
|||
return -EINVAL;
|
||||
|
||||
dirent_t *new = dirent_create(name);
|
||||
if (!new) {
|
||||
if (!new)
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
new->inode = to;
|
||||
new->fs = in->fs;
|
||||
|
|
@ -162,7 +142,6 @@ static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out)
|
|||
}
|
||||
|
||||
*out = new;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
@ -173,15 +152,19 @@ static int unlink(dirent_t *dent)
|
|||
|
||||
static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
||||
{
|
||||
/* count first */
|
||||
list_item_t *item = parent->dirents.head;
|
||||
ssize_t n = 0;
|
||||
|
||||
while (item) {
|
||||
n++;
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
/* now copy */
|
||||
if (n == 0) {
|
||||
*dents = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN);
|
||||
if (!arr)
|
||||
return -ENOMEM;
|
||||
|
|
@ -189,11 +172,24 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents)
|
|||
item = parent->dirents.head;
|
||||
n = 0;
|
||||
while (item) {
|
||||
memcpy(arr + n, item->ptr, sizeof(dirent_t));
|
||||
dirent_t *copy = dirent_dup(item->ptr);
|
||||
if (!copy) {
|
||||
/* free already-copied entries before returning */
|
||||
for (ssize_t i = 0; i < n; i++) {
|
||||
if (arr[i].inode)
|
||||
inode_dec_ref(arr[i].inode);
|
||||
kfree(arr[i].name);
|
||||
}
|
||||
kfree(arr);
|
||||
return -ENOMEM;
|
||||
}
|
||||
arr[n] = *copy;
|
||||
kfree(copy);
|
||||
n++;
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
*dents = arr;
|
||||
return n;
|
||||
}
|
||||
|
||||
|
|
@ -213,7 +209,7 @@ static int umount(fs_t *fs)
|
|||
return -EINVAL;
|
||||
}
|
||||
|
||||
void ramfs_init()
|
||||
void ramfs_init(void)
|
||||
{
|
||||
void *ops = kalloc(sizeof(struct fs_ops) + sizeof(struct inode_ops), ALLOC_KERN);
|
||||
if (!ops) {
|
||||
|
|
|
|||
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));
|
||||
|
|
|
|||
|
|
@ -34,13 +34,10 @@ typedef struct _dirent dirent_t;
|
|||
typedef struct _fs fs_t;
|
||||
|
||||
struct inode_ops {
|
||||
/* FOR ALL FILES */
|
||||
ssize_t (*read)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||
ssize_t (*write)(inode_t *inode, char *buf, size_t size, size_t offset);
|
||||
|
||||
/* FOR DIRECTORY FILES */
|
||||
|
||||
int (*link)(dirent_t *in, inode_t *to, const char *name, dirent_t **out);
|
||||
int (*link)(dirent_t *parent, inode_t *child, const char *name, dirent_t **out);
|
||||
int (*unlink)(dirent_t *dent);
|
||||
ssize_t (*get_dents)(inode_t *parent, dirent_t **dents);
|
||||
};
|
||||
|
|
@ -65,10 +62,8 @@ struct _inode {
|
|||
struct _dirent {
|
||||
fs_t *fs;
|
||||
inode_t *inode;
|
||||
union {
|
||||
u64 ino_num;
|
||||
fs_t *fs_ptr;
|
||||
};
|
||||
|
||||
u64 ino_num;
|
||||
|
||||
size_t rec_len;
|
||||
size_t name_len;
|
||||
|
|
@ -82,22 +77,18 @@ struct _dirent {
|
|||
char *name;
|
||||
};
|
||||
|
||||
/* dirent ops */
|
||||
dirent_t *dirent_create(const char *name);
|
||||
dirent_t *dirent_dup(dirent_t *src);
|
||||
void dirent_free(dirent_t *dirent);
|
||||
void dirent_inc_ref(dirent_t *dent);
|
||||
void dirent_dec_ref(dirent_t *dent);
|
||||
|
||||
/* inode ops */
|
||||
int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out);
|
||||
int inode_find_dirent_locked(inode_t *inode, const char *name, dirent_t **out);
|
||||
inode_t *inode_create();
|
||||
inode_t *inode_create(void);
|
||||
void inode_free(inode_t *inode);
|
||||
void inode_inc_ref(inode_t *inode);
|
||||
void inode_dec_ref(inode_t *inode);
|
||||
void dirent_inc_ref(dirent_t *dent);
|
||||
void dirent_dec_ref(dirent_t *dent);
|
||||
void dirent_inc_ref_lock(dirent_t *dent);
|
||||
void dirent_dec_ref_lock(dirent_t *dent);
|
||||
void inode_inc_ref_lock(inode_t *inode);
|
||||
void inode_dec_ref_lock(inode_t *inode);
|
||||
void inode_init(void);
|
||||
|
||||
#endif /* _INODE_H_ */
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ struct fs_ops {
|
|||
|
||||
struct _fs {
|
||||
u64 type;
|
||||
u64 flags;
|
||||
u64 flags;
|
||||
size_t block_size;
|
||||
size_t n_blocks;
|
||||
|
||||
|
|
@ -58,13 +58,13 @@ struct _fs {
|
|||
struct fs_ops fs_ops;
|
||||
};
|
||||
|
||||
/* VFS ops */
|
||||
void vfs_init();
|
||||
void vfs_init(void);
|
||||
void vfs_register(struct fs_type *fs);
|
||||
|
||||
int vfs_open(const char *abs_path, dirent_t **out);
|
||||
void vfs_close(dirent_t *dent);
|
||||
int vfs_creat(dirent_t *parent, const char *name, u64 mode, int flags, dirent_t **out);
|
||||
int vfs_unlink(dirent_t *parent, const char *name);
|
||||
int vfs_mount(const char *mount_point, fs_t *fs_to_mount);
|
||||
|
||||
ssize_t vfs_write(inode_t *ino, char *buf, size_t size, size_t offset);
|
||||
|
|
|
|||
|
|
@ -2,12 +2,13 @@
|
|||
#ifndef _RWLOCK_H_
|
||||
#define _RWLOCK_H_
|
||||
|
||||
#include <lib/sem.h>
|
||||
#include <kernel/spinlock.h>
|
||||
#include <kernel/types.h>
|
||||
|
||||
typedef struct _rwlock {
|
||||
sem_t sem;
|
||||
spinlock_t reader_lock;
|
||||
u64 reader_count;
|
||||
typedef struct {
|
||||
spinlock_t lock;
|
||||
u64 readers;
|
||||
bool writer;
|
||||
} rwlock_t;
|
||||
|
||||
void read_lock(rwlock_t *lock);
|
||||
|
|
|
|||
48
lib/rwlock.c
48
lib/rwlock.c
|
|
@ -3,43 +3,47 @@
|
|||
|
||||
void read_lock(rwlock_t *lock)
|
||||
{
|
||||
spinlock_acquire(&lock->reader_lock);
|
||||
|
||||
if (lock->reader_count == 0) {
|
||||
spinlock_release(&lock->reader_lock);
|
||||
sem_wait(&lock->sem);
|
||||
spinlock_acquire(&lock->reader_lock);
|
||||
while (1) {
|
||||
spinlock_acquire(&lock->lock);
|
||||
if (!lock->writer) {
|
||||
lock->readers++;
|
||||
spinlock_release(&lock->lock);
|
||||
return;
|
||||
}
|
||||
spinlock_release(&lock->lock);
|
||||
}
|
||||
|
||||
lock->reader_count += 1;
|
||||
spinlock_release(&lock->reader_lock);
|
||||
}
|
||||
|
||||
void read_unlock(rwlock_t *lock)
|
||||
{
|
||||
spinlock_acquire(&lock->reader_lock);
|
||||
|
||||
lock->reader_count -= 1;
|
||||
|
||||
if (lock->reader_count == 0)
|
||||
sem_post(&lock->sem);
|
||||
|
||||
spinlock_release(&lock->reader_lock);
|
||||
spinlock_acquire(&lock->lock);
|
||||
lock->readers--;
|
||||
spinlock_release(&lock->lock);
|
||||
}
|
||||
|
||||
void write_lock(rwlock_t *lock)
|
||||
{
|
||||
sem_wait(&lock->sem);
|
||||
while (1) {
|
||||
spinlock_acquire(&lock->lock);
|
||||
if (!lock->writer && lock->readers == 0) {
|
||||
lock->writer = true;
|
||||
spinlock_release(&lock->lock);
|
||||
return;
|
||||
}
|
||||
spinlock_release(&lock->lock);
|
||||
}
|
||||
}
|
||||
|
||||
void write_unlock(rwlock_t *lock)
|
||||
{
|
||||
sem_post(&lock->sem);
|
||||
spinlock_acquire(&lock->lock);
|
||||
lock->writer = false;
|
||||
spinlock_release(&lock->lock);
|
||||
}
|
||||
|
||||
void rwlock_init(rwlock_t *lock)
|
||||
{
|
||||
lock->reader_lock = 0;
|
||||
lock->reader_count = 0;
|
||||
sem_init(&lock->sem, 1);
|
||||
lock->lock = 0;
|
||||
lock->readers = 0;
|
||||
lock->writer = false;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue