string: add lock to strtok
This commit is contained in:
parent
5ab34eb11b
commit
3806d53237
4 changed files with 40 additions and 1 deletions
23
fs/ext2.c
23
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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@
|
|||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
#include <kernel/lock.h>
|
||||
|
||||
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_ */
|
||||
|
|
|
|||
|
|
@ -1,6 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <string.h>
|
||||
#include <kernel/slab.h>
|
||||
#include <kernel/lock.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue