diff --git a/fs/ext2.c b/fs/ext2.c index 21cb57e..b236bdc 100644 --- a/fs/ext2.c +++ b/fs/ext2.c @@ -127,6 +127,29 @@ int ext2_write_inode(struct ext2fs *fs, struct ext2_inode *in, uint32_t inode) return ret; } +int ext2_read_dir(struct ext2fs *fs, struct ext2_inode *in, struct ext2_dir *out, uint32_t index) +{ + uint32_t block = index / (fs->block_size / sizeof(struct ext2_dir)); + uint32_t offset = index % (fs->block_size / sizeof(struct ext2_dir)); + + uint32_t block_num = in->block[block]; + + struct ext2_dir *buf = kmalloc(fs->block_size, ALLOC_DMA); + if (!buf) + return -1; + + int ret = ext2_read_block(fs, buf, block_num); + if (ret < 0) { + kfree(buf); + return ret; + } + + memcpy(out, (void *)((uint64_t)buf + offset), sizeof(*out)); + kfree(buf); + + return ret; +} + struct ext2fs *ext2_init_fs(struct block_device *bdev) { struct ext2fs *ret = kmalloc(sizeof(*ret), ALLOC_KERN); diff --git a/include/fs/ext2.h b/include/fs/ext2.h index 96ada32..2d404af 100644 --- a/include/fs/ext2.h +++ b/include/fs/ext2.h @@ -121,6 +121,11 @@ struct ext2_dir_entry { char name[]; } PACKED; +struct ext2_dir { + struct ext2_dir_entry *entry; + struct ext2_dir *next; +}; + struct ext2fs { struct ext2_superblock sb; struct block_device *bdev; diff --git a/include/string.h b/include/string.h index a0a0236..09c72a0 100644 --- a/include/string.h +++ b/include/string.h @@ -7,6 +7,8 @@ #include #include +#include + void *memcpy(void *dest, const void *src, size_t num); int memcmp(const void *aa, const void *bb, size_t num); void *memset(void *str, int c, size_t n); @@ -16,5 +18,7 @@ char **strsplit(const char *str, char delim, int *num); size_t strlen(const char *c); int atoi(const char *a); +extern spinlock_t strtok_lock; + #endif /* __ASM__ */ #endif /* _STRING_H_ */ diff --git a/lib/string.c b/lib/string.c index 2b5dcc8..0fa37ce 100644 --- a/lib/string.c +++ b/lib/string.c @@ -1,6 +1,10 @@ /* SPDX-License-Identifier: GPL-2.0-only */ -#include #include +#include + +#include + +spinlock_t strtok_lock = 0; void *memcpy(void *dest, const void *src, size_t num) { @@ -88,6 +92,7 @@ char *strchr(const char *s, int c) char *strtok(char *str, const char *delim) { + spinlock_acquire(&strtok_lock); static char *last = NULL; if (str) last = str; @@ -104,6 +109,8 @@ char *strtok(char *str, const char *delim) } last++; } + + spinlock_release(&strtok_lock); return ret; }