diff --git a/.gitignore b/.gitignore index 5d11830..73462d0 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ edk2-ovmf/ .config *.hdd *.cpio +initramfs/ diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index c237698..4d847b0 100644 --- a/fs/vfs/inode.c +++ b/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) diff --git a/fs/vfs/ramfs.c b/fs/vfs/ramfs.c index 42d9b3a..cd62dfa 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -40,17 +40,18 @@ int ramfs_create(u64 flags, fs_t **out) memcpy(&fs->fs_ops, ramfs_ops, sizeof(struct fs_ops)); root_inode->mode = S_IFDIR; + memcpy(&root_inode->i_ops, ramfs_inode_ops, sizeof(struct inode_ops)); fs->root_dirent = root; root->type = S_IFDIR; root->fs = fs; - inode_inc_ref(root_inode); root->inode = root_inode; *out = fs; return 0; out_root_inode: + inode_dec_ref(root_inode); dirent_free(root); out_root: kfree(fs); @@ -70,6 +71,8 @@ static int alloc_inode(fs_t *fs, u64 mode, inode_t **out) ret->mode = mode; ret->ino_num = atomic_xadd(&inode_ticker, 1); + memcpy(&ret->i_ops, ramfs_inode_ops, sizeof(struct inode_ops)); + *out = ret; return 0; } @@ -77,8 +80,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 +91,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 +101,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,53 +124,63 @@ 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) +static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out) { - if (!in || !to || !name || !out) + if (!parent_dir || !child || !name || !out) return -EINVAL; dirent_t *new = dirent_create(name); - if (!new) { + if (!new) return -ENOMEM; - } - inode_inc_ref(to); - new->inode = to; - new->fs = in->fs; + new->inode = child; - if (!list_insert(&in->inode->dirents, new)) { - inode_dec_ref_lock(to); + if (!list_insert(&parent_dir->dirents, new)) { dirent_free(new); return -ENOMEM; } *out = new; - return 0; } -static int unlink(dirent_t *dent) +static int unlink(inode_t *parent_dir, const char *name) { - return 0; + if (!parent_dir || !name) + return -EINVAL; + + list_item_t *item = parent_dir->dirents.head; + while (item) { + dirent_t *dent = item->ptr; + if (!strcmp(dent->name, name)) { + list_remove(item); + dirent_dec_ref(dent); + return 0; + } + item = item->next; + } + + return -ENOENT; } 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; @@ -191,31 +188,38 @@ 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; } -static dirent_t *mount(int flags, struct block_device *bdev) +static int init(int flags, inode_t *inode, fs_t **out) { - fs_t *new_fs; - ssize_t res = ramfs_create(flags, &new_fs); - - if (res < 0) - return NULL; - - return new_fs->root_dirent; + return ramfs_create(flags, out); } -static int umount(fs_t *fs) +static int deinit(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) { @@ -235,7 +239,7 @@ void ramfs_init() ramfs_inode_ops->unlink = unlink; ramfs_inode_ops->get_dents = get_dents; - struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = mount, .fs_umount = umount}; + struct fs_driver ramfs_type = {.name = "ramfs", .init = init, .deinit = deinit}; vfs_register(&ramfs_type); } diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 50f02ee..7926d16 100644 --- a/fs/vfs/vfs.c +++ b/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,72 @@ 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); + res = ino->i_ops.link(ino, 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; } + new_dirent->fs = fs; *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; + } + + int res = ino->i_ops.unlink(ino, name); + + write_unlock(&ino->rwlock); + return res; } int vfs_mount(const char *mount_point, fs_t *fs_to_mount) @@ -273,9 +252,9 @@ int vfs_mount(const char *mount_point, fs_t *fs_to_mount) return -ENOSYS; } -void vfs_register(struct fs_type *fs) +void vfs_register(struct fs_driver *fs) { - struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN); + struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN); if (!ins) return; @@ -284,9 +263,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)); diff --git a/include/fs/inode.h b/include/fs/inode.h index 8ae27dd..a78ade7 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -34,14 +34,11 @@ 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 (*unlink)(dirent_t *dent); + int (*link)(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out); + int (*unlink)(inode_t *parent_dir, const char *name); 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_ */ diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 698e8dc..0880061 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -5,8 +5,6 @@ #include #include -#include - #include #include @@ -31,10 +29,10 @@ #define FS_RDONLY 0x00000001 -struct fs_type { +struct fs_driver { char name[256]; - dirent_t *(*fs_mount)(int flags, struct block_device *bdev); - int (*fs_umount)(fs_t *fs); + int (*init)(int flags, inode_t *inode, fs_t **out); + int (*deinit)(fs_t *fs); }; struct fs_ops { @@ -45,7 +43,7 @@ struct fs_ops { struct _fs { u64 type; - u64 flags; + u64 flags; size_t block_size; size_t n_blocks; @@ -58,13 +56,13 @@ struct _fs { struct fs_ops fs_ops; }; -/* VFS ops */ -void vfs_init(); -void vfs_register(struct fs_type *fs); +void vfs_init(void); +void vfs_register(struct fs_driver *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); diff --git a/include/lib/rwlock.h b/include/lib/rwlock.h index 87ab3a9..50a1fc1 100644 --- a/include/lib/rwlock.h +++ b/include/lib/rwlock.h @@ -2,12 +2,13 @@ #ifndef _RWLOCK_H_ #define _RWLOCK_H_ -#include +#include +#include -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); diff --git a/lib/rwlock.c b/lib/rwlock.c index 92c4417..48e1944 100644 --- a/lib/rwlock.c +++ b/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; } diff --git a/lib/string.c b/lib/string.c index bc8229a..1f33ef9 100644 --- a/lib/string.c +++ b/lib/string.c @@ -28,8 +28,12 @@ int strncmp(const char *a, const char *b, size_t n) return 0; for (size_t i = 0; i < n; i++) { - if (a[i] != b[i] || !a[i] || !b[i]) - return a[i] - b[i]; + unsigned char char_a = (unsigned char)a[i]; + unsigned char char_b = (unsigned char)b[i]; + + if (char_a != char_b || char_a == '\0') { + return (int)char_a - (int)char_b; + } } return 0; diff --git a/mem/alloc.c b/mem/alloc.c index 44da227..69d72d2 100644 --- a/mem/alloc.c +++ b/mem/alloc.c @@ -47,8 +47,11 @@ void *kalloc(size_t size, u64 flags) for (size_t i = 0; i < n_slab_sizes; i++) { mem_cache_t *slab = kalloc_slab_array[i]; - if (slab->item_size <= size) { + if (slab->item_size >= size) { ret = slab_alloc(slab); + if (ret) + size = slab->item_size; + break; } } } diff --git a/mem/slab.c b/mem/slab.c index d4b9254..1140582 100644 --- a/mem/slab.c +++ b/mem/slab.c @@ -33,7 +33,7 @@ mem_cache_t *slab_create(const char *name, size_t item_size, size_t cache_size, cache->next_free = ((vaddr_t)cache) + pos; vaddr_t prev_ptr = 0; - for (size_t i = 0; i < cache->num_members - 1; i++) { + for (size_t i = 0; i < cache->num_members; i++) { vaddr_t cur_ptr = (vaddr_t)cache + pos + (i * item_size); prev_ptr = cur_ptr - item_size;