Protect busy VfsObjs during opening and deleting
This commit is contained in:
@ -19,6 +19,7 @@ size_t hal_strspn(const char *s, const char *accept);
|
|||||||
char *hal_strcpy(char *dest, const char *src);
|
char *hal_strcpy(char *dest, const char *src);
|
||||||
char *hal_strchr(const char *s, int c);
|
char *hal_strchr(const char *s, int c);
|
||||||
char *hal_strstr(const char *str, const char *substring);
|
char *hal_strstr(const char *str, const char *substring);
|
||||||
|
char *hal_string_combine(char *dest, const char *src);
|
||||||
void hal_wait(uint32_t ms);
|
void hal_wait(uint32_t ms);
|
||||||
int32_t hal_randnum(void);
|
int32_t hal_randnum(void);
|
||||||
|
|
||||||
|
|||||||
@ -140,3 +140,13 @@ char *hal_strstr(const char *str, const char *substring)
|
|||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char *hal_string_combine(char *dest, const char *src) {
|
||||||
|
size_t i, j;
|
||||||
|
for(i = 0; dest[i] != '\0'; i++);
|
||||||
|
for(j = 0; src[j] != '\0'; j++) {
|
||||||
|
dest[i+j] = src[j];
|
||||||
|
}
|
||||||
|
dest[i+j] = '\0';
|
||||||
|
return dest;
|
||||||
|
}
|
||||||
|
|||||||
@ -14,6 +14,28 @@
|
|||||||
#include "dlmalloc/malloc.h"
|
#include "dlmalloc/malloc.h"
|
||||||
|
|
||||||
VfsTable VFS_TABLE;
|
VfsTable VFS_TABLE;
|
||||||
|
VfsBusyObjs VFS_BUSY_VOBJS;
|
||||||
|
|
||||||
|
#define CHECK_BUSY_VFSOBJ() \
|
||||||
|
char *tmpbuf = dlmalloc(VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
|
||||||
|
\
|
||||||
|
hal_memset(tmpbuf, 0, VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX); \
|
||||||
|
hal_string_combine(tmpbuf, mountpoint); \
|
||||||
|
hal_string_combine(tmpbuf, ":"); \
|
||||||
|
hal_string_combine(tmpbuf, path); \
|
||||||
|
\
|
||||||
|
bool busy = false; \
|
||||||
|
VfsObj *vobj, *vobjtmp; \
|
||||||
|
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock); \
|
||||||
|
LL_FOREACH_SAFE(VFS_BUSY_VOBJS.vobjs, vobj, vobjtmp) { \
|
||||||
|
if (hal_strcmp(vobj->path, tmpbuf) == 0) { \
|
||||||
|
busy = true; \
|
||||||
|
break; \
|
||||||
|
} \
|
||||||
|
} \
|
||||||
|
spinlock_release(&VFS_BUSY_VOBJS.spinlock); \
|
||||||
|
\
|
||||||
|
dlfree(tmpbuf);
|
||||||
|
|
||||||
void vfs_init_littlefs(VfsMountPoint *mp, bool format) {
|
void vfs_init_littlefs(VfsMountPoint *mp, bool format) {
|
||||||
struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
|
struct lfs_config *cfg = dlmalloc(sizeof(*cfg));
|
||||||
@ -95,6 +117,12 @@ int32_t vfs_fetchdirent(char *mountpoint, const char *path, FsDirent *direntbuf,
|
|||||||
}
|
}
|
||||||
|
|
||||||
int32_t vfs_delete(char *mountpoint, const char *path) {
|
int32_t vfs_delete(char *mountpoint, const char *path) {
|
||||||
|
CHECK_BUSY_VFSOBJ()
|
||||||
|
|
||||||
|
if (busy) {
|
||||||
|
return E_NOTYET;
|
||||||
|
}
|
||||||
|
|
||||||
VfsMountPoint *mp = NULL;
|
VfsMountPoint *mp = NULL;
|
||||||
|
|
||||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||||
@ -156,6 +184,12 @@ int32_t vfs_unmount(char *mountpoint) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) {
|
VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) {
|
||||||
|
CHECK_BUSY_VFSOBJ()
|
||||||
|
|
||||||
|
if (busy) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
VfsMountPoint *mp = NULL;
|
VfsMountPoint *mp = NULL;
|
||||||
|
|
||||||
spinlock_acquire(&VFS_TABLE.spinlock);
|
spinlock_acquire(&VFS_TABLE.spinlock);
|
||||||
@ -166,16 +200,34 @@ VfsObj *vfs_open(char *mountpoint, const char *path, uint32_t flags) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
return mp->open(mp, path, flags);
|
VfsObj *vobj1 = mp->open(mp, path, flags);
|
||||||
|
if (vobj1 == NULL) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
hal_string_combine(vobj1->path, mountpoint);
|
||||||
|
hal_string_combine(vobj1->path, ":");
|
||||||
|
hal_string_combine(vobj1->path, path);
|
||||||
|
|
||||||
|
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock);
|
||||||
|
LL_APPEND(VFS_BUSY_VOBJS.vobjs, vobj1);
|
||||||
|
spinlock_release(&VFS_BUSY_VOBJS.spinlock);
|
||||||
|
|
||||||
|
return vobj1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vfs_close(VfsObj *vobj) {
|
void vfs_close(VfsObj *vobj) {
|
||||||
|
spinlock_acquire(&VFS_BUSY_VOBJS.spinlock);
|
||||||
|
LL_REMOVE(VFS_BUSY_VOBJS.vobjs, vobj);
|
||||||
|
spinlock_release(&VFS_BUSY_VOBJS.spinlock);
|
||||||
vobj->cleanup(vobj);
|
vobj->cleanup(vobj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void vfs_init(void) {
|
void vfs_init(void) {
|
||||||
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
|
hal_memset(&VFS_TABLE, 0, sizeof(VFS_TABLE));
|
||||||
spinlock_init(&VFS_TABLE.spinlock);
|
spinlock_init(&VFS_TABLE.spinlock);
|
||||||
|
hal_memset(&VFS_BUSY_VOBJS, 0, sizeof(VFS_BUSY_VOBJS));
|
||||||
|
spinlock_init(&VFS_BUSY_VOBJS.spinlock);
|
||||||
|
|
||||||
{
|
{
|
||||||
RamSdInitExtra extra = { .capacity = baseimg_getsize(), .preallocbuffer = (uint8_t *)baseimg_getaddr() };
|
RamSdInitExtra extra = { .capacity = baseimg_getsize(), .preallocbuffer = (uint8_t *)baseimg_getaddr() };
|
||||||
|
|||||||
@ -30,7 +30,9 @@ enum {
|
|||||||
#define VFS_PATH_MAX 1024
|
#define VFS_PATH_MAX 1024
|
||||||
|
|
||||||
typedef struct VfsObj {
|
typedef struct VfsObj {
|
||||||
|
struct VfsObj *next;
|
||||||
SpinLock spinlock;
|
SpinLock spinlock;
|
||||||
|
char path[VFS_MOUNTPOINT_LABEL_MAX+1+VFS_PATH_MAX];
|
||||||
void *extra;
|
void *extra;
|
||||||
size_t extrasize;
|
size_t extrasize;
|
||||||
struct VfsMountPoint *vmp;
|
struct VfsMountPoint *vmp;
|
||||||
@ -40,6 +42,13 @@ typedef struct VfsObj {
|
|||||||
void (*cleanup)(struct VfsObj *vobj);
|
void (*cleanup)(struct VfsObj *vobj);
|
||||||
} VfsObj;
|
} VfsObj;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SpinLock spinlock;
|
||||||
|
VfsObj *vobjs;
|
||||||
|
} VfsBusyObjs;
|
||||||
|
|
||||||
|
extern VfsBusyObjs VFS_BUSY_VOBJS;
|
||||||
|
|
||||||
typedef struct VfsMountPoint {
|
typedef struct VfsMountPoint {
|
||||||
int _hshtbstate;
|
int _hshtbstate;
|
||||||
char label[VFS_MOUNTPOINT_LABEL_MAX];
|
char label[VFS_MOUNTPOINT_LABEL_MAX];
|
||||||
|
|||||||
Reference in New Issue
Block a user