fix: 修复文件系统不合理的锁操作

1. 移除了fatfs和littlefs中冗余的加锁操作。
2. 增加了配置选项FS_LOCK_TIMEOUT(宏LOSCFG_FS_LOCK_TIMEOUT),提供配置
   文件系统获取锁超时能力。
3. 新增对外接口LOS_FsLock()和LOS_FsUnlock()接口,提供HAL层与文件系统的
   互斥操作。

BREAKING CHANGE:
修复文件系统不合理的锁操作
新增API:
los_fs.h:
int LOS_FsLock(void)
int LOS_FsUnlock(void)
新增配置项:
LOSCFG_FS_LOCK_TIMEOUT(menuconfig FS_LOCK_TIMEOUT)

fix #I66S2J

Signed-off-by: Far <yesiyuan2@huawei.com>
Change-Id: I2ae9b6b5e2c788b2f4a00d9678a0240d00c335eb
This commit is contained in:
Far
2022-12-20 11:11:15 +08:00
parent abb83fd23b
commit b7f906ef8a
8 changed files with 180 additions and 454 deletions

View File

@@ -34,3 +34,11 @@ config FS_VFS
help
Answer Y to enable LiteOS support VFS.
if FS_VFS
config FS_LOCK_TIMEOUT
int "Filesystem global lock timeout value in tick. -1 for waiting forever"
default -1
help
The timeout value of getting filesystem lock in tick. -1 for waiting forever
endif

View File

@@ -139,6 +139,17 @@ int LOS_PartitionFormat(const char *partName, char *fsType, void *data);
int LOS_FsRegister(const char *fsType, const struct MountOps *fsMops,
const struct FileOps *fsFops, const struct FsManagement *fsMgt);
/*
* @brief Lock the whole filesystem to forbid filesystem access.
*
* @return Return LOS_NOK if error. Return LOS_OK if seccess.
*/
int LOS_FsLock(void);
/*
* @brief Unlock the whole filesystem to allow filesystem access.
*/
void LOS_FsUnlock(void);
#ifdef __cplusplus
#if __cplusplus

View File

@@ -100,20 +100,20 @@ int PollQueryFd(int fd, struct PollTable *table)
UINT32 g_fsMutex;
static UINT32 g_dirNum = 0;
int VfsLock(void)
int LOS_FsLock(void)
{
if (!OsCheckKernelRunning()) {
return LOS_OK;
}
if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) {
PRINT_ERR("VfsLock failed!");
if (LOS_MuxPend(g_fsMutex, (UINT32)LOSCFG_FS_LOCK_TIMEOUT) != LOS_OK) {
PRINT_ERR("LOS_FsLock failed!");
return (int)LOS_NOK;
}
return LOS_OK;
}
void VfsUnlock(void)
void LOS_FsUnlock(void)
{
if (!OsCheckKernelRunning()) {
return;
@@ -224,7 +224,7 @@ static int VfsOpen(const char *path, int flags)
return fd;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return fd;
}
@@ -234,7 +234,7 @@ static int VfsOpen(const char *path, int flags)
(mp->mFs->fsFops == NULL) || (mp->mFs->fsFops->open == NULL)) {
/* path is not in any mountpoint */
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return fd;
}
@@ -242,14 +242,14 @@ static int VfsOpen(const char *path, int flags)
(flags & (O_CREAT | O_WRONLY | O_RDWR))) {
/* can't create file in read only mp */
VFS_ERRNO_SET(EACCES);
VfsUnlock();
LOS_FsUnlock();
return fd;
}
file = VfsFileGet();
if (file == NULL) {
VFS_ERRNO_SET(ENFILE);
VfsUnlock();
LOS_FsUnlock();
return fd;
}
@@ -258,7 +258,7 @@ static int VfsOpen(const char *path, int flags)
if (file->fullPath == NULL) {
VFS_ERRNO_SET(ENOMEM);
VfsFilePut(file);
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}
(void)strcpy_s((char *)file->fullPath, len, path);
@@ -278,7 +278,7 @@ static int VfsOpen(const char *path, int flags)
VfsFilePut(file);
}
VfsUnlock();
LOS_FsUnlock();
return fd;
}
@@ -293,7 +293,7 @@ static struct File *VfsAttachFile(int fd, UINT32 status)
return NULL;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EFAULT);
return NULL;
}
@@ -301,13 +301,13 @@ static struct File *VfsAttachFile(int fd, UINT32 status)
file = FdToFile(fd);
if ((file == NULL) || (file->fMp == NULL)) {
VFS_ERRNO_SET(EBADF);
VfsUnlock();
LOS_FsUnlock();
return NULL;
}
if (file->fStatus != FILE_STATUS_READY) {
VFS_ERRNO_SET(EBADF);
VfsUnlock();
LOS_FsUnlock();
return NULL;
}
@@ -328,7 +328,7 @@ static struct File *VfsAttachFileWithStatus(int fd, int status)
static void VfsDetachFile(const struct File *file)
{
(void)file;
VfsUnlock();
LOS_FsUnlock();
}
static int VfsClose(int fd)
@@ -673,7 +673,7 @@ int stat(const char *path, struct stat *stat)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -681,7 +681,7 @@ int stat(const char *path, struct stat *stat)
mp = VfsMpFind(path, &pathInMp);
if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -691,7 +691,7 @@ int stat(const char *path, struct stat *stat)
VFS_ERRNO_SET(ENOTSUP);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
FUNC_ALIAS(stat, _stat, (const char *path, struct stat *stat), int);
@@ -707,7 +707,7 @@ int statfs(const char *path, struct statfs *buf)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -715,7 +715,7 @@ int statfs(const char *path, struct statfs *buf)
mp = VfsMpFind(path, &pathInMp);
if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -725,7 +725,7 @@ int statfs(const char *path, struct statfs *buf)
VFS_ERRNO_SET(ENOTSUP);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -740,7 +740,7 @@ int unlink(const char *path)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -749,13 +749,13 @@ int unlink(const char *path)
if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') ||
(mp->mFs->fsFops->unlink == NULL)) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
ret = mp->mFs->fsFops->unlink(mp, pathInMp);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
FUNC_ALIAS(unlink, _unlink, (const char *path), int);
@@ -773,7 +773,7 @@ int rename(const char *oldpath, const char *newpath)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -782,27 +782,27 @@ int rename(const char *oldpath, const char *newpath)
if (pathInMpOld == NULL) {
VFS_ERRNO_SET(EINVAL);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
if ((mpOld == NULL) || (*pathInMpOld == '\0') ||
(mpOld->mFs->fsFops->unlink == NULL)) {
VFS_ERRNO_SET(EINVAL);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
mpNew = VfsMpFind(newpath, &pathInMpNew);
if ((mpNew == NULL) || (pathInMpNew == NULL) || (*pathInMpNew == '\0') || (mpNew->mFs->fsFops->unlink == NULL)) {
VFS_ERRNO_SET(EINVAL);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
if (mpOld != mpNew) {
VFS_ERRNO_SET(EXDEV);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -812,7 +812,7 @@ int rename(const char *oldpath, const char *newpath)
VFS_ERRNO_SET(ENOTSUP);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -860,7 +860,7 @@ DIR *opendir(const char *path)
return NULL;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
LOSCFG_FS_FREE_HOOK(dir);
return NULL;
@@ -868,7 +868,7 @@ DIR *opendir(const char *path)
if (g_dirNum >= LOSCFG_MAX_OPEN_DIRS) {
VFS_ERRNO_SET(ENFILE);
VfsUnlock();
LOS_FsUnlock();
LOSCFG_FS_FREE_HOOK(dir);
return NULL;
}
@@ -876,14 +876,14 @@ DIR *opendir(const char *path)
mp = VfsMpFind(path, &pathInMp);
if ((mp == NULL) || (pathInMp == NULL)) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
LOSCFG_FS_FREE_HOOK(dir);
return NULL;
}
if (mp->mFs->fsFops->opendir == NULL) {
VFS_ERRNO_SET(ENOTSUP);
VfsUnlock();
LOS_FsUnlock();
LOSCFG_FS_FREE_HOOK(dir);
return NULL;
}
@@ -900,7 +900,7 @@ DIR *opendir(const char *path)
dir = NULL;
}
VfsUnlock();
LOS_FsUnlock();
return (DIR *)dir;
}
@@ -914,7 +914,7 @@ struct dirent *readdir(DIR *dir)
return NULL;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return NULL;
}
@@ -928,7 +928,7 @@ struct dirent *readdir(DIR *dir)
VFS_ERRNO_SET(ENOTSUP);
}
VfsUnlock();
LOS_FsUnlock();
return ret;
}
@@ -946,7 +946,7 @@ int closedir(DIR *dir)
mp = d->dMp;
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -965,7 +965,7 @@ int closedir(DIR *dir)
VFS_ERRNO_SET(EBADF);
}
VfsUnlock();
LOS_FsUnlock();
LOSCFG_FS_FREE_HOOK(d);
d = NULL;
return MapToPosixRet(ret);
@@ -983,7 +983,7 @@ int mkdir(const char *path, mode_t mode)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -991,7 +991,7 @@ int mkdir(const char *path, mode_t mode)
mp = VfsMpFind(path, &pathInMp);
if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -1001,7 +1001,7 @@ int mkdir(const char *path, mode_t mode)
VFS_ERRNO_SET(ENOTSUP);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -1016,7 +1016,7 @@ int rmdir(const char *path)
return MapToPosixRet(ret);
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet(ret);
}
@@ -1025,13 +1025,13 @@ int rmdir(const char *path)
if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') ||
(mp->mFs->fsFops->rmdir == NULL)) {
VFS_ERRNO_SET(ENOENT);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
ret = mp->mFs->fsFops->rmdir(mp, pathInMp);
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet(ret);
}
@@ -1311,31 +1311,31 @@ ssize_t pread(int fd, void *buff, size_t bytes, off_t off)
return 0;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet((int)ret);
}
savepos = lseek(fd, 0, SEEK_CUR);
if (savepos == (off_t)-1) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}
pos = lseek(fd, off, SEEK_SET);
if (pos == (off_t)-1) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}
ret = read(fd, buff, bytes);
pos = lseek(fd, savepos, SEEK_SET);
if ((pos == (off_t)-1) && (ret >= 0)) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)LOS_NOK);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}
@@ -1358,31 +1358,31 @@ ssize_t pwrite(int fd, const void *buff, size_t bytes, off_t off)
return 0;
}
if (VfsLock() != LOS_OK) {
if (LOS_FsLock() != LOS_OK) {
VFS_ERRNO_SET(EAGAIN);
return MapToPosixRet((int)ret);
}
savepos = lseek(fd, 0, SEEK_CUR);
if (savepos == (off_t)-1) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}
pos = lseek(fd, off, SEEK_SET);
if (pos == (off_t)-1) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}
ret = write(fd, buff, bytes);
pos = lseek(fd, savepos, SEEK_SET);
if ((pos == (off_t)-1) && (ret >= 0)) {
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)LOS_NOK);
}
VfsUnlock();
LOS_FsUnlock();
return MapToPosixRet((int)ret);
}

View File

@@ -41,17 +41,17 @@ struct FsMap *VfsFsMapGet(const char *fsType)
{
struct FsMap *curr = g_fsMap;
(void)VfsLock();
(void)LOS_FsLock();
while (curr != NULL) {
if ((curr->fsType != NULL) && (fsType != NULL) &&
(strcmp(curr->fsType, fsType) == 0)) {
(void)VfsUnlock();
LOS_FsUnlock();
return curr;
}
curr = curr->next;
}
VfsUnlock();
LOS_FsUnlock();
return NULL;
}
@@ -86,11 +86,11 @@ int OsFsRegister(const char *fsType, const struct MountOps *fsMops,
newfs->fsMgt = fsMgt;
newfs->fsRefs = 0;
(void)VfsLock();
(void)LOS_FsLock();
newfs->next = g_fsMap;
g_fsMap = newfs;
VfsUnlock();
LOS_FsUnlock();
return LOS_OK;
}

View File

@@ -191,17 +191,17 @@ int mount(const char *source, const char *target,
return (int)LOS_NOK;
}
(void)VfsLock();
(void)LOS_FsLock();
if (mountflags & MS_REMOUNT) {
ret = VfsRemount(source, target, fsType, mountflags, data);
VfsUnlock();
LOS_FsUnlock();
return ret;
}
mp = VfsMountPointInit(target, fsType, mountflags);
if (mp == NULL) {
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}
@@ -210,7 +210,7 @@ int mount(const char *source, const char *target,
mp->mDev = LOSCFG_FS_MALLOC_HOOK(len);
if (mp->mDev == NULL) {
LOSCFG_FS_FREE_HOOK(mp);
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}
(void)strcpy_s((char *)mp->mDev, len, source);
@@ -231,14 +231,14 @@ int mount(const char *source, const char *target,
}
g_mountPoints = mp;
VfsUnlock();
LOS_FsUnlock();
return LOS_OK;
errout:
LOSCFG_FS_FREE_HOOK((void *)mp->mPath);
LOSCFG_FS_FREE_HOOK((void *)mp->mDev);
LOSCFG_FS_FREE_HOOK(mp);
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}
@@ -248,7 +248,7 @@ int umount(const char *target)
const char *pathInMp = NULL;
int ret = (int)LOS_NOK;
(void)VfsLock();
(void)LOS_FsLock();
if (target == NULL) {
goto errout;
}
@@ -276,12 +276,12 @@ int umount(const char *target)
LOSCFG_FS_FREE_HOOK((void *)mp->mDev);
LOSCFG_FS_FREE_HOOK(mp);
VfsUnlock();
LOS_FsUnlock();
return LOS_OK;
errout:
PRINT_ERR("umount2 failed, target %s.\n", target);
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}
@@ -306,7 +306,7 @@ int umount2(const char *target, int flag)
const char *pathInMp = NULL;
int ret = (int)LOS_NOK;
(void)VfsLock();
(void)LOS_FsLock();
if (target == NULL) {
goto errout;
}
@@ -336,11 +336,11 @@ int umount2(const char *target, int flag)
LOSCFG_FS_FREE_HOOK((void *)mp->mDev);
LOSCFG_FS_FREE_HOOK(mp);
VfsUnlock();
LOS_FsUnlock();
return LOS_OK;
errout:
PRINT_ERR("umount2 failed, target %s.\n", target);
VfsUnlock();
LOS_FsUnlock();
return (int)LOS_NOK;
}

View File

@@ -52,8 +52,6 @@ extern "C" {
extern UINT32 g_fsMutex;
int OsVfsInit(void);
int VfsLock(void);
void VfsUnlock(void);
#ifdef __cplusplus
#if __cplusplus