diff --git a/.gitignore b/.gitignore index 73462d0..5d11830 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,3 @@ edk2-ovmf/ .config *.hdd *.cpio -initramfs/ diff --git a/fs/vfs/inode.c b/fs/vfs/inode.c index 4d847b0..c237698 100644 --- a/fs/vfs/inode.c +++ b/fs/vfs/inode.c @@ -11,36 +11,45 @@ static mem_cache_t *inode_slab = NULL; static mem_cache_t *dirent_slab = NULL; -static int inode_populate_dirents(inode_t *inode) +int inode_populate_dirents(inode_t *inode) { - write_lock(&inode->rwlock); + /* write_lock(&inode->rwlock) is held by caller */ - if (inode->dirents.head != NULL) { - write_unlock(&inode->rwlock); + if (inode->dirents.head != NULL) return 0; - } dirent_t *dents; ssize_t n = inode->i_ops.get_dents(inode, &dents); - if (n < 0) { - write_unlock(&inode->rwlock); + if (n < 0) return n; - } for (size_t i = 0; i < n; i++) { dirent_t *dent = dents + i; - dirent_t *insert = dirent_dup(dent); + dirent_t *insert = dirent_create(dent->name); 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; @@ -52,12 +61,11 @@ cleanup: list_item_t *next = item->next; list_remove(item); if (dent->inode) - inode_dec_ref(dent->inode); + inode_dec_ref_lock(dent->inode); dirent_free(dent); item = next; } } - write_unlock(&inode->rwlock); kfree(dents); return -ENOMEM; } @@ -68,7 +76,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(dent); + dirent_inc_ref_lock(dent); *out = dent; return 0; } @@ -86,9 +94,15 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) if (inode->dirents.head == NULL) { read_unlock(&inode->rwlock); - int res = inode_populate_dirents(inode); - if (res < 0) - return res; + 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); + } read_lock(&inode->rwlock); } @@ -97,7 +111,7 @@ int inode_find_dirent(inode_t *inode, const char *name, dirent_t **out) return ret; } -inode_t *inode_create(void) +inode_t *inode_create() { inode_t *ret = slab_alloc(inode_slab); @@ -114,18 +128,18 @@ inode_t *inode_create(void) void inode_free(inode_t *inode) { - while (inode->dirents.head) { + /* caller must hold write_lock(&inode->rwlock) */ + + if (inode->dirents.head) { list_item_t *item = inode->dirents.head; - dirent_t *dent = item->ptr; - list_remove(item); - if (dent) { - if (dent->inode) - inode_dec_ref(dent->inode); - dirent_free(dent); + while (item) { + dirent_t *dent = item->ptr; + if (dent) + dirent_dec_ref_lock(dent); + item = item->next; } } - - kfree(inode->data); + list_destroy(&inode->dirents); slab_free(inode_slab, inode); } @@ -141,18 +155,36 @@ 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) { - spinlock_acquire(&dent->lock); dent->refcount += 1; - spinlock_release(&dent->lock); } 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); + spinlock_release(&dent->lock); +} + +void dirent_dec_ref_lock(dirent_t *dent) { spinlock_acquire(&dent->lock); if (dent->refcount == 0) { @@ -164,11 +196,25 @@ void dirent_dec_ref(dirent_t *dent) spinlock_release(&dent->lock); if (freed) { - inode_dec_ref(dent->inode); + inode_dec_ref_lock(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); @@ -191,34 +237,13 @@ 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) +void inode_init() { 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 cd62dfa..42d9b3a 100644 --- a/fs/vfs/ramfs.c +++ b/fs/vfs/ramfs.c @@ -40,18 +40,17 @@ 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); @@ -71,8 +70,6 @@ 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; } @@ -80,7 +77,8 @@ 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(inode); + inode_dec_ref_lock(inode); + return 0; } @@ -91,9 +89,13 @@ 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) { - if (!inode->data || inode->file_size == 0) + ssize_t ret = 0; + if (!inode->data) return 0; + if (inode->file_size == 0) + goto out; + if (offset >= inode->file_size) return -EINVAL; @@ -101,22 +103,33 @@ 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); - return size; + ret = size; + +out: + return ret; } static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset) { - size_t new_size = size + offset; - + ssize_t ret = 0; if (!inode->data) { - inode->data = kzalloc(new_size, ALLOC_KERN); - if (!inode->data) - return -ENOMEM; - inode->file_size = new_size; - } else if (inode->file_size < new_size) { + 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; u8 *new_data = krealloc(inode->data, new_size, ALLOC_KERN); - if (!new_data) - return -ENOMEM; + if (!new_data) { + ret = -ENOMEM; + goto out; + } if (offset > inode->file_size) memset(new_data + inode->file_size, 0, offset - inode->file_size); inode->data = new_data; @@ -124,63 +137,53 @@ static ssize_t write(inode_t *inode, char *buf, size_t size, size_t offset) } memcpy(inode->data + offset, buf, size); - return size; + ret = size; + +out: + return ret; } -static int link(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out) +static int link(dirent_t *in, inode_t *to, const char *name, dirent_t **out) { - if (!parent_dir || !child || !name || !out) + if (!in || !to || !name || !out) return -EINVAL; dirent_t *new = dirent_create(name); - if (!new) + if (!new) { return -ENOMEM; + } - new->inode = child; + inode_inc_ref(to); + new->inode = to; + new->fs = in->fs; - if (!list_insert(&parent_dir->dirents, new)) { + if (!list_insert(&in->inode->dirents, new)) { + inode_dec_ref_lock(to); dirent_free(new); return -ENOMEM; } *out = new; + return 0; } -static int unlink(inode_t *parent_dir, const char *name) +static int unlink(dirent_t *dent) { - 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; + return 0; } 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; } - if (n == 0) { - *dents = NULL; - return 0; - } - + /* now copy */ dirent_t *arr = kalloc(n * sizeof(dirent_t), ALLOC_KERN); if (!arr) return -ENOMEM; @@ -188,38 +191,31 @@ static ssize_t get_dents(inode_t *parent, dirent_t **dents) item = parent->dirents.head; n = 0; while (item) { - 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); + memcpy(arr + n, item->ptr, sizeof(dirent_t)); n++; item = item->next; } - *dents = arr; return n; } -static int init(int flags, inode_t *inode, fs_t **out) +static dirent_t *mount(int flags, struct block_device *bdev) { - return ramfs_create(flags, out); + fs_t *new_fs; + ssize_t res = ramfs_create(flags, &new_fs); + + if (res < 0) + return NULL; + + return new_fs->root_dirent; } -static int deinit(fs_t *fs) +static int umount(fs_t *fs) { return -EINVAL; } -void ramfs_init(void) +void ramfs_init() { void *ops = kalloc(sizeof(struct fs_ops) + sizeof(struct inode_ops), ALLOC_KERN); if (!ops) { @@ -239,7 +235,7 @@ void ramfs_init(void) ramfs_inode_ops->unlink = unlink; ramfs_inode_ops->get_dents = get_dents; - struct fs_driver ramfs_type = {.name = "ramfs", .init = init, .deinit = deinit}; + struct fs_type ramfs_type = {.name = "ramfs", .fs_mount = mount, .fs_umount = umount}; vfs_register(&ramfs_type); } diff --git a/fs/vfs/vfs.c b/fs/vfs/vfs.c index 7926d16..50f02ee 100644 --- a/fs/vfs/vfs.c +++ b/fs/vfs/vfs.c @@ -11,29 +11,43 @@ 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); - - 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); + char *ret = kzalloc(len + 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 j = 0; - for (const char *c = name; c < end; c++) { - if (last == '/' && *c == '/') + size_t i = 0; + const char *cursor = name; + while (cursor < zeroptr && (c = *cursor++)) { + if (last == c && c == '/') continue; - ret[j++] = *c; - last = *c; + + last = c; + ret[i++] = last; } return ret; @@ -45,17 +59,14 @@ static int next_level(const char *path, dirent_t *dent, dirent_t **out_dirent) return -EINVAL; int res; - char *build = kzalloc(256, ALLOC_KERN); - if (!build) - return -ENOMEM; + char build[256] = {0}; - for (int i = 0; i < 255; i++) { + for (int i = 0; i < sizeof(build) - 1; 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; @@ -67,7 +78,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(dirent); + dirent_dec_ref_lock(dirent); return res; } } @@ -81,45 +92,49 @@ 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; - dirent_inc_ref(current_dir); + int ret = -ENOENT; - if (*path_optr == '\0') { + dirent_inc_ref_lock(current_dir); + + /* Handle root path "/" */ + if (strlen(abs_path) == 1 && *abs_path == '/') { *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(current_dir); + dirent_dec_ref_lock(current_dir); ret = res; goto out; } - dirent_dec_ref(current_dir); + dirent_dec_ref_lock(current_dir); + current_dir = next_dir; cur_path += res; @@ -140,16 +155,18 @@ out: void vfs_close(dirent_t *dent) { - dirent_dec_ref(dent); + dirent_dec_ref_lock(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); - ssize_t ret = ino->i_ops.write(ino, buf, size, offset); + ret = ino->i_ops.write(ino, buf, size, offset); write_unlock(&ino->rwlock); return ret; @@ -157,11 +174,13 @@ 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); - ssize_t ret = ino->i_ops.read(ino, buf, size, offset); + ret = ino->i_ops.read(ino, buf, size, offset); read_unlock(&ino->rwlock); return ret; @@ -174,72 +193,74 @@ 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) { - write_unlock(&ino->rwlock); - return -ENOTDIR; + ret = -ENOTDIR; + goto out_err; } - dirent_t *test = NULL; - int res = inode_find_dirent_locked(ino, name, &test); + /* re-check under lock */ + test = NULL; + res = inode_find_dirent_locked(ino, name, &test); if (res == 0) { - dirent_dec_ref(test); - write_unlock(&ino->rwlock); - return -EEXIST; + /* exists */ + ret = -EEXIST; + goto out_err; } else if (res != -ENOENT) { - write_unlock(&ino->rwlock); - return res; + /* various other error codes */ + ret = res; + goto out_err; } + /* 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) { - write_unlock(&ino->rwlock); - return res; + ret = res; + goto out_err; } - res = ino->i_ops.link(ino, new_ino, name, &new_dirent); + res = ino->i_ops.link(parent, new_ino, name, &new_dirent); if (res < 0) { - fs->fs_ops.free_inode(fs, new_ino); - write_unlock(&ino->rwlock); - return res; + 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; } - new_dirent->fs = fs; *out = new_dirent; + +out_err: + if (test) + dirent_dec_ref_lock(test); write_unlock(&ino->rwlock); - return 0; -} -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; + return ret; } int vfs_mount(const char *mount_point, fs_t *fs_to_mount) @@ -252,9 +273,9 @@ int vfs_mount(const char *mount_point, fs_t *fs_to_mount) return -ENOSYS; } -void vfs_register(struct fs_driver *fs) +void vfs_register(struct fs_type *fs) { - struct fs_driver *ins = kalloc(sizeof(*fs), ALLOC_KERN); + struct fs_type *ins = kalloc(sizeof(*fs), ALLOC_KERN); if (!ins) return; @@ -263,8 +284,9 @@ void vfs_register(struct fs_driver *fs) kfree(ins); } -void vfs_init(void) +void vfs_init() { + 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 a78ade7..8ae27dd 100644 --- a/include/fs/inode.h +++ b/include/fs/inode.h @@ -34,11 +34,14 @@ 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); - int (*link)(inode_t *parent_dir, inode_t *child, const char *name, dirent_t **out); - int (*unlink)(inode_t *parent_dir, const char *name); + /* FOR DIRECTORY FILES */ + + int (*link)(dirent_t *in, inode_t *to, const char *name, dirent_t **out); + int (*unlink)(dirent_t *dent); ssize_t (*get_dents)(inode_t *parent, dirent_t **dents); }; @@ -62,8 +65,10 @@ struct _inode { struct _dirent { fs_t *fs; inode_t *inode; - - u64 ino_num; + union { + u64 ino_num; + fs_t *fs_ptr; + }; size_t rec_len; size_t name_len; @@ -77,18 +82,22 @@ 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(void); +inode_t *inode_create(); void inode_free(inode_t *inode); void inode_inc_ref(inode_t *inode); void inode_dec_ref(inode_t *inode); -void inode_init(void); +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); #endif /* _INODE_H_ */ diff --git a/include/fs/vfs.h b/include/fs/vfs.h index 0880061..698e8dc 100644 --- a/include/fs/vfs.h +++ b/include/fs/vfs.h @@ -5,6 +5,8 @@ #include #include +#include + #include #include @@ -29,10 +31,10 @@ #define FS_RDONLY 0x00000001 -struct fs_driver { +struct fs_type { char name[256]; - int (*init)(int flags, inode_t *inode, fs_t **out); - int (*deinit)(fs_t *fs); + dirent_t *(*fs_mount)(int flags, struct block_device *bdev); + int (*fs_umount)(fs_t *fs); }; struct fs_ops { @@ -43,7 +45,7 @@ struct fs_ops { struct _fs { u64 type; - u64 flags; + u64 flags; size_t block_size; size_t n_blocks; @@ -56,13 +58,13 @@ struct _fs { struct fs_ops fs_ops; }; -void vfs_init(void); -void vfs_register(struct fs_driver *fs); +/* VFS ops */ +void vfs_init(); +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); diff --git a/include/lib/rwlock.h b/include/lib/rwlock.h index 50a1fc1..87ab3a9 100644 --- a/include/lib/rwlock.h +++ b/include/lib/rwlock.h @@ -2,13 +2,12 @@ #ifndef _RWLOCK_H_ #define _RWLOCK_H_ -#include -#include +#include -typedef struct { - spinlock_t lock; - u64 readers; - bool writer; +typedef struct _rwlock { + sem_t sem; + spinlock_t reader_lock; + u64 reader_count; } rwlock_t; void read_lock(rwlock_t *lock); diff --git a/lib/rwlock.c b/lib/rwlock.c index 48e1944..92c4417 100644 --- a/lib/rwlock.c +++ b/lib/rwlock.c @@ -3,47 +3,43 @@ void read_lock(rwlock_t *lock) { - while (1) { - spinlock_acquire(&lock->lock); - if (!lock->writer) { - lock->readers++; - spinlock_release(&lock->lock); - return; - } - spinlock_release(&lock->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); } + + lock->reader_count += 1; + spinlock_release(&lock->reader_lock); } void read_unlock(rwlock_t *lock) { - spinlock_acquire(&lock->lock); - lock->readers--; - spinlock_release(&lock->lock); + spinlock_acquire(&lock->reader_lock); + + lock->reader_count -= 1; + + if (lock->reader_count == 0) + sem_post(&lock->sem); + + spinlock_release(&lock->reader_lock); } void write_lock(rwlock_t *lock) { - while (1) { - spinlock_acquire(&lock->lock); - if (!lock->writer && lock->readers == 0) { - lock->writer = true; - spinlock_release(&lock->lock); - return; - } - spinlock_release(&lock->lock); - } + sem_wait(&lock->sem); } void write_unlock(rwlock_t *lock) { - spinlock_acquire(&lock->lock); - lock->writer = false; - spinlock_release(&lock->lock); + sem_post(&lock->sem); } void rwlock_init(rwlock_t *lock) { - lock->lock = 0; - lock->readers = 0; - lock->writer = false; + lock->reader_lock = 0; + lock->reader_count = 0; + sem_init(&lock->sem, 1); } diff --git a/lib/string.c b/lib/string.c index 1f33ef9..bc8229a 100644 --- a/lib/string.c +++ b/lib/string.c @@ -28,12 +28,8 @@ int strncmp(const char *a, const char *b, size_t n) return 0; for (size_t i = 0; i < n; 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; - } + if (a[i] != b[i] || !a[i] || !b[i]) + return a[i] - b[i]; } return 0; diff --git a/mem/alloc.c b/mem/alloc.c index 69d72d2..44da227 100644 --- a/mem/alloc.c +++ b/mem/alloc.c @@ -47,11 +47,8 @@ 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 1140582..d4b9254 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; i++) { + for (size_t i = 0; i < cache->num_members - 1; i++) { vaddr_t cur_ptr = (vaddr_t)cache + pos + (i * item_size); prev_ptr = cur_ptr - item_size;