From fa142daffb200dd8aa72ed916a109e64fdbbce8e Mon Sep 17 00:00:00 2001 From: openharmony_ci <120357966@qq.com> Date: Mon, 19 Dec 2022 08:57:11 +0000 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E9=80=80=20'Pull=20Request=20!938=20:?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D=E6=96=87=E4=BB=B6=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E4=B8=8D=E5=90=88=E7=90=86=E7=9A=84=E9=94=81=E6=93=8D=E4=BD=9C?= =?UTF-8?q?'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/fs/fatfs/fatfs.c | 459 ++++++++++++++++++++++----- components/fs/littlefs/lfs_adapter.c | 6 + components/fs/vfs/Kconfig | 8 - components/fs/vfs/los_fs.h | 11 - components/fs/vfs/vfs_fs.c | 108 +++---- components/fs/vfs/vfs_maps.c | 10 +- components/fs/vfs/vfs_mount.c | 24 +- components/fs/vfs/vfs_operations.h | 2 + 8 files changed, 451 insertions(+), 177 deletions(-) diff --git a/components/fs/fatfs/fatfs.c b/components/fs/fatfs/fatfs.c index 0423761e..89b59924 100644 --- a/components/fs/fatfs/fatfs.c +++ b/components/fs/fatfs/fatfs.c @@ -60,6 +60,7 @@ #endif /* FS_LOCK_TIMEOUT_SEC */ static UINT8 g_workBuffer[FF_MAX_SS]; +static pthread_mutex_t g_fatfsMutex = PTHREAD_MUTEX_INITIALIZER; static char *g_volPath[FF_VOLUMES] = {FF_VOLUME_STRS}; PARTITION VolToPart[] = { @@ -69,6 +70,31 @@ PARTITION VolToPart[] = { { 0, 0, 4, 0, 0 } }; +static int FsLock(void) +{ + int ret = 0; + struct timespec absTimeout = {0}; + if (!OsCheckKernelRunning()) { + return ret; + } + ret = clock_gettime(CLOCK_REALTIME, &absTimeout); + if (ret != 0) { + PRINT_ERR("clock gettime err 0x%x!\r\n", errno); + return errno; + } + absTimeout.tv_sec += FS_LOCK_TIMEOUT_SEC; + ret = pthread_mutex_timedlock(&g_fatfsMutex, &absTimeout); + return ret; +} + +static void FsUnlock(void) +{ + if (!OsCheckKernelRunning()) { + return; + } + (void)pthread_mutex_unlock(&g_fatfsMutex); +} + static int FsChangeDrive(const char *path) { INT32 res; @@ -249,69 +275,95 @@ int FatfsMount(struct MountPoint *mp, unsigned long mountflags, { FRESULT res; FATFS *fs = NULL; + int ret; + + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } if (mountflags & MS_REMOUNT) { - return Remount(mp, mountflags); + ret = Remount(mp, mountflags); + FsUnlock(); + return ret; } char *ldPath = GetLdPath(mp->mDev); if (ldPath == NULL) { errno = EFAULT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto ERROUT; } fs = (FATFS *)LOSCFG_FS_MALLOC_HOOK(sizeof(FATFS)); if (fs == NULL) { errno = ENOMEM; - PutLdPath(ldPath); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto ERROUT; } (void)memset_s(fs, sizeof(FATFS), 0, sizeof(FATFS)); - - res = f_mount(fs, ldPath, 1); - if (res != FR_OK) { - LOSCFG_FS_FREE_HOOK(fs); - PutLdPath(ldPath); - errno = FatfsErrno(res); - return (int)LOS_NOK; - } mp->mData = (void *)fs; + res = f_mount((FATFS *)mp->mData, ldPath, 1); + if (res != FR_OK) { + errno = FatfsErrno(res); + ret = (int)LOS_NOK; + goto ERROUT; + } + PutLdPath(ldPath); + FsUnlock(); return (int)LOS_OK; + +ERROUT: + LOSCFG_FS_FREE_HOOK(fs); + mp->mData = NULL; + PutLdPath(ldPath); + FsUnlock(); + return ret; } int FatfsUmount(struct MountPoint *mp) { + int ret; int volId; FRESULT res; - char *ldPath; FATFS *fatfs = (FATFS *)mp->mData; + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + + char *ldPath = GetLdPath(mp->mDev); + if (ldPath == NULL) { + errno = EFAULT; + ret = (int)LOS_NOK; + goto OUT; + } + /* The volume is not mounted */ if (fatfs->fs_type == 0) { errno = EINVAL; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } volId = GetPartIdByPartName(mp->mDev); /* umount is not allowed when a file or directory is opened. */ if (f_checkopenlock(volId) != FR_OK) { errno = EBUSY; - return (int)LOS_NOK; - } - - ldPath = GetLdPath(mp->mDev); - if (ldPath == NULL) { - errno = EFAULT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_mount((FATFS *)NULL, ldPath, 0); if (res != FR_OK) { errno = FatfsErrno(res); - PutLdPath(ldPath); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } if (fatfs->win != NULL) { @@ -321,15 +373,19 @@ int FatfsUmount(struct MountPoint *mp) LOSCFG_FS_FREE_HOOK(mp->mData); mp->mData = NULL; + ret = (int)LOS_OK; + +OUT: PutLdPath(ldPath); - return (int)LOS_OK; + FsUnlock(); + return ret; } int FatfsUmount2(struct MountPoint *mp, int flag) { + int ret; UINT32 flags; FRESULT res; - char *ldPath; FATFS *fatfs = (FATFS *)mp->mData; flags = MNT_FORCE | MNT_DETACH | MNT_EXPIRE | UMOUNT_NOFOLLOW; @@ -338,23 +394,31 @@ int FatfsUmount2(struct MountPoint *mp, int flag) return (int)LOS_NOK; } - /* The volume is not mounted */ - if (fatfs->fs_type == 0) { - errno = EINVAL; + ret = FsLock(); + if (ret != 0) { + errno = ret; return (int)LOS_NOK; } - ldPath = GetLdPath(mp->mDev); + char *ldPath = GetLdPath(mp->mDev); if (ldPath == NULL) { errno = EFAULT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; + } + + /* The volume is not mounted */ + if (fatfs->fs_type == 0) { + errno = EINVAL; + ret = (int)LOS_NOK; + goto OUT; } res = f_mount((FATFS *)NULL, ldPath, 0); if (res != FR_OK) { - PutLdPath(ldPath); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } if (fatfs->win != NULL) { @@ -364,8 +428,12 @@ int FatfsUmount2(struct MountPoint *mp, int flag) LOSCFG_FS_FREE_HOOK(mp->mData); mp->mData = NULL; + ret = (int)LOS_OK; + +OUT: PutLdPath(ldPath); - return (int)LOS_OK; + FsUnlock(); + return ret; } int FatfsOpen(struct File *file, const char *path, int oflag) @@ -388,12 +456,20 @@ int FatfsOpen(struct File *file, const char *path, int oflag) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + LOSCFG_FS_FREE_HOOK(fp); + return (int)LOS_NOK; + } + ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT open ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; + ret = (int)LOS_NOK; LOSCFG_FS_FREE_HOOK(fp); - return (int)LOS_NOK; + goto OUT; } res = f_open(fp, path, fmode); @@ -401,20 +477,31 @@ int FatfsOpen(struct File *file, const char *path, int oflag) PRINT_ERR("FAT open err 0x%x!\r\n", res); LOSCFG_FS_FREE_HOOK(fp); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } file->fData = (void *)fp; - return (int)LOS_OK; +OUT: + FsUnlock(); + return ret; } int FatfsClose(struct File *file) { FRESULT res; FIL *fp = (FIL *)file->fData; + int ret; + + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } if ((fp == NULL) || (fp->obj.fs == NULL)) { + FsUnlock(); errno = ENOENT; return (int)LOS_NOK; } @@ -422,6 +509,7 @@ int FatfsClose(struct File *file) res = f_close(fp); if (res != FR_OK) { PRINT_ERR("FAT close err 0x%x!\r\n", res); + FsUnlock(); errno = FatfsErrno(res); return (int)LOS_NOK; } @@ -433,6 +521,7 @@ int FatfsClose(struct File *file) #endif LOSCFG_FS_FREE_HOOK(file->fData); file->fData = NULL; + FsUnlock(); return (int)LOS_OK; } @@ -442,22 +531,31 @@ ssize_t FatfsRead(struct File *file, char *buf, size_t nbyte) FRESULT res; UINT32 lenRead; FIL *fp = (FIL *)file->fData; + int ret; if (buf == NULL) { errno = EFAULT; return (ssize_t)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (ssize_t)LOS_NOK; + } if (fp == NULL) { + FsUnlock(); errno = ENOENT; return (ssize_t)LOS_NOK; } res = f_read(fp, buf, nbyte, &lenRead); if (res != FR_OK) { + FsUnlock(); errno = FatfsErrno(res); return (ssize_t)LOS_NOK; } + FsUnlock(); return (ssize_t)lenRead; } @@ -468,15 +566,22 @@ ssize_t FatfsWrite(struct File *file, const char *buf, size_t nbyte) UINT32 lenWrite; static BOOL overFlow = FALSE; FIL *fp = (FIL *)file->fData; + int ret; if (buf == NULL) { errno = EFAULT; return (ssize_t)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (ssize_t)LOS_NOK; + } + if ((fp ==NULL) || (fp->obj.fs == NULL)) { errno = ENOENT; - return (ssize_t)LOS_NOK; + goto ERROUT; } res = f_write(fp, buf, nbyte, &lenWrite); @@ -487,10 +592,15 @@ ssize_t FatfsWrite(struct File *file, const char *buf, size_t nbyte) if ((res != FR_OK) || (nbyte != lenWrite)) { errno = FatfsErrno(res); - return (ssize_t)LOS_NOK; + goto ERROUT; } + FsUnlock(); return (ssize_t)lenWrite; + +ERROUT: + FsUnlock(); + return (ssize_t)LOS_NOK; } off_t FatfsLseek(struct File *file, off_t offset, int whence) @@ -498,10 +608,17 @@ off_t FatfsLseek(struct File *file, off_t offset, int whence) FRESULT res; off_t pos; FIL *fp = (FIL *)file->fData; + int ret; + + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (off_t)LOS_NOK; + } if ((fp == NULL) || (fp->obj.fs == NULL)) { errno = ENOENT; - return (off_t)LOS_NOK; + goto ERROUT; } if (whence == SEEK_SET) { @@ -512,17 +629,22 @@ off_t FatfsLseek(struct File *file, off_t offset, int whence) pos = f_size(fp); } else { errno = EINVAL; - return (off_t)LOS_NOK; + goto ERROUT; } res = f_lseek(fp, offset + pos); if (res != FR_OK) { errno = FatfsErrno(res); - return (off_t)LOS_NOK; + goto ERROUT; } pos = f_tell(fp); + FsUnlock(); return pos; + +ERROUT: + FsUnlock(); + return (off_t)LOS_NOK; } /* Remove the specified FILE */ @@ -536,26 +658,39 @@ int FatfsUnlink(struct MountPoint *mp, const char *path) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if (!mp->mWriteEnable) { errno = EACCES; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT unlink ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_unlink(path); if (res != FR_OK) { PRINT_ERR("FAT unlink err 0x%x!\r\n", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } - return (int)LOS_OK; + ret = (int)LOS_OK; + +OUT: + FsUnlock(); + return ret; } int FatfsStat(struct MountPoint *mp, const char *path, struct stat *buf) @@ -569,18 +704,26 @@ int FatfsStat(struct MountPoint *mp, const char *path, struct stat *buf) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT stat ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_stat(path, &fileInfo); if (res != FR_OK) { PRINT_ERR("FAT stat err 0x%x!\r\n", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } buf->st_size = fileInfo.fsize; @@ -597,27 +740,43 @@ int FatfsStat(struct MountPoint *mp, const char *path, struct stat *buf) buf->st_mode |= S_IFDIR; } - return (int)LOS_OK; + ret = (int)LOS_OK; + +OUT: + FsUnlock(); + return ret; } /* Synchronize all changes to Flash */ int FatfsSync(struct File *file) { + int ret; FRESULT res; FIL *fp = (FIL *)file->fData; + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if ((fp == NULL) || (fp->obj.fs == NULL)) { errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_sync(fp); if (res != FR_OK) { errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } + ret = (int)LOS_OK; - return (int)LOS_OK; +OUT: + FsUnlock(); + return ret; } int FatfsMkdir(struct MountPoint *mp, const char *path) @@ -630,26 +789,38 @@ int FatfsMkdir(struct MountPoint *mp, const char *path) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if (!mp->mWriteEnable) { errno = EACCES; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT mkdir ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_mkdir(path); if (res != FR_OK) { PRINT_ERR("FAT mkdir err 0x%x!\r\n", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } + ret = (int)LOS_OK; - return (int)LOS_OK; +OUT: + FsUnlock(); + return ret; } int FatfsOpendir(struct Dir *dir, const char *dirName) @@ -660,34 +831,47 @@ int FatfsOpendir(struct Dir *dir, const char *dirName) if (dirName == NULL) { errno = EFAULT; - return (int)LOS_NOK; + goto ERROUT; + } + + dp = (DIR *)LOSCFG_FS_MALLOC_HOOK(sizeof(DIR)); + if (dp == NULL) { + errno = ENOENT; + goto ERROUT; + } + + ret = FsLock(); + if (ret != 0) { + errno = ret; + goto ERROUT; } ret = FsChangeDrive(dirName); if (ret != (int)LOS_OK) { PRINT_ERR("FAT opendir ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; - } - - dp = (DIR *)LOSCFG_FS_MALLOC_HOOK(sizeof(DIR)); - if (dp == NULL) { - errno = ENOENT; - return (int)LOS_NOK; + goto ERROUT; } res = f_opendir(dp, dirName); if (res != FR_OK) { PRINT_ERR("FAT opendir err 0x%x!\r\n", res); - LOSCFG_FS_FREE_HOOK(dp); errno = FatfsErrno(res); - return (int)LOS_NOK; + goto ERROUT; } dir->dData = dp; dir->dOffset = 0; + FsUnlock(); return (int)LOS_OK; + +ERROUT: + if (dp != NULL) { + LOSCFG_FS_FREE_HOOK(dp); + } + FsUnlock(); + return (int)LOS_NOK; } int FatfsReaddir(struct Dir *dir, struct dirent *dent) @@ -695,6 +879,7 @@ int FatfsReaddir(struct Dir *dir, struct dirent *dent) FRESULT res; FILINFO fileInfo = {0}; DIR *dp = NULL; + int ret; if ((dir == NULL) || (dir->dData == NULL)) { errno = EBADF; @@ -702,11 +887,18 @@ int FatfsReaddir(struct Dir *dir, struct dirent *dent) } dp = (DIR *)dir->dData; + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + res = f_readdir(dp, &fileInfo); /* if res not ok or fname is NULL , return NULL */ if ((res != FR_OK) || (fileInfo.fname[0] == 0x0)) { PRINT_ERR("FAT readdir err 0x%x!\r\n", res); errno = FatfsErrno(res); + FsUnlock(); return (int)LOS_NOK; } @@ -717,6 +909,7 @@ int FatfsReaddir(struct Dir *dir, struct dirent *dent) } else { dent->d_type = DT_REG; } + FsUnlock(); return (int)LOS_OK; } @@ -725,6 +918,7 @@ int FatfsClosedir(struct Dir *dir) { FRESULT res; DIR *dp = NULL; + int ret; if ((dir == NULL) || (dir->dData == NULL)) { errno = EBADF; @@ -732,15 +926,23 @@ int FatfsClosedir(struct Dir *dir) } dp = dir->dData; + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + res = f_closedir(dp); if (res != FR_OK) { PRINT_ERR("FAT closedir err 0x%x!\r\n", res); + FsUnlock(); errno = FatfsErrno(res); return (int)LOS_NOK; } LOSCFG_FS_FREE_HOOK(dir->dData); dir->dData = NULL; + FsUnlock(); return (int)LOS_OK; } @@ -755,26 +957,38 @@ int FatfsRmdir(struct MountPoint *mp, const char *path) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if (!mp->mWriteEnable) { errno = EACCES; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } ret = FsChangeDrive(path); if (ret != (int)LOS_OK) { PRINT_ERR("FAT rmdir ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_rmdir(path); if (res != FR_OK) { PRINT_ERR("FAT rmdir err 0x%x!\r\n", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } + ret = (int)LOS_OK; - return (int)LOS_OK; +OUT: + FsUnlock(); + return ret; } int FatfsRename(struct MountPoint *mp, const char *oldName, const char *newName) @@ -787,26 +1001,38 @@ int FatfsRename(struct MountPoint *mp, const char *oldName, const char *newName) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if (!mp->mWriteEnable) { errno = EACCES; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } ret = FsChangeDrive(oldName); if (ret != (int)LOS_OK) { PRINT_ERR("FAT f_getfree ChangeDrive err 0x%x!\r\n", ret); errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_rename(oldName, newName); if (res != FR_OK) { PRINT_ERR("FAT frename err 0x%x!\r\n", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } + ret = (int)LOS_OK; - return (int)LOS_OK; +OUT: + FsUnlock(); + return ret; } int FatfsStatfs(const char *path, struct statfs *buf) @@ -821,18 +1047,26 @@ int FatfsStatfs(const char *path, struct statfs *buf) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + ret = FsChangeDrive(path); if (ret != FR_OK) { PRINT_ERR("FAT f_getfree ChangeDrive err %d.", ret); errno = FatfsErrno(FR_INVALID_PARAMETER); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_getfree(path, &freeClust, &fs); if (res != FR_OK) { PRINT_ERR("FAT f_getfree err 0x%x.", res); errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } buf->f_bfree = freeClust; buf->f_bavail = freeClust; @@ -844,7 +1078,11 @@ int FatfsStatfs(const char *path, struct statfs *buf) buf->f_bsize = FF_MIN_SS * fs->csize; #endif - return (int)LOS_OK; + ret = (int)LOS_OK; + +OUT: + FsUnlock(); + return ret; } static int DoTruncate(struct File *file, off_t length, UINT32 count) @@ -852,6 +1090,7 @@ static int DoTruncate(struct File *file, off_t length, UINT32 count) FRESULT res = FR_OK; DWORD csz; FIL *fp = (FIL *)file->fData; + int ret = (int)LOS_OK; csz = (DWORD)(fp->obj.fs)->csize * SS(fp->obj.fs); /* Cluster size */ if (length > csz * count) { @@ -859,7 +1098,8 @@ static int DoTruncate(struct File *file, off_t length, UINT32 count) res = f_expand(fp, 0, (FSIZE_t)(length), FALLOC_FL_KEEP_SIZE); #else errno = ENOSYS; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + return ret; #endif } else if (length < csz * count) { res = f_truncate(fp, (FSIZE_t)length); @@ -867,13 +1107,14 @@ static int DoTruncate(struct File *file, off_t length, UINT32 count) if (res != FR_OK) { errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + return ret; } fp->obj.objsize = length; /* Set file size to length */ fp->flag |= 0x40; /* Set modified flag */ - return (int)LOS_OK; + return ret; } int FatfsTruncate(struct File *file, off_t length) @@ -882,30 +1123,49 @@ int FatfsTruncate(struct File *file, off_t length) UINT count; DWORD fclust; FIL *fp = (FIL *)file->fData; + int ret; if ((length < 0) || (length > UINT_MAX)) { errno = EINVAL; return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + return (int)LOS_NOK; + } + if ((fp == NULL) || (fp->obj.fs == NULL)) { errno = ENOENT; - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } res = f_getclustinfo(fp, &fclust, &count); if (res != FR_OK) { errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } - return DoTruncate(file, length, count); + ret = DoTruncate(file, length, count); + if (ret != FR_OK) { + goto OUT; + } + + ret = (int)LOS_OK; + +OUT: + FsUnlock(); + return ret; } int FatfsFdisk(const char *dev, int *partTbl, int arrayNum) { int pdrv; FRESULT res; + int ret; if ((dev == NULL) || (partTbl == NULL)) { errno = EFAULT; @@ -918,13 +1178,25 @@ int FatfsFdisk(const char *dev, int *partTbl, int arrayNum) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + ret = (int)LOS_NOK; + goto OUT; + } + res = f_fdisk(pdrv, (DWORD const *)partTbl, g_workBuffer); if (res != FR_OK) { errno = FatfsErrno(res); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } - return (int)LOS_OK; + ret = (int)LOS_OK; + +OUT: + FsUnlock(); + return ret; } int FatfsFormat(const char *partName, void *privData) @@ -933,6 +1205,7 @@ int FatfsFormat(const char *partName, void *privData) MKFS_PARM opt = {0}; int option = *(int *)privData; char *dev = NULL; /* logical driver */ + int ret; if (partName == NULL) { errno = EFAULT; @@ -945,17 +1218,29 @@ int FatfsFormat(const char *partName, void *privData) return (int)LOS_NOK; } + ret = FsLock(); + if (ret != 0) { + errno = ret; + PutLdPath(dev); + return (int)LOS_NOK; + } + opt.fmt = option; opt.n_sect = 0; /* use default allocation unit size depends on the volume size. */ res = f_mkfs(dev, &opt, g_workBuffer, FF_MAX_SS); if (res != FR_OK) { errno = FatfsErrno(res); - PutLdPath(dev); - return (int)LOS_NOK; + ret = (int)LOS_NOK; + goto OUT; } - return (int)LOS_OK; + ret = (int)LOS_OK; + +OUT: + PutLdPath(dev); + FsUnlock(); + return ret; } static struct MountOps g_fatfsMnt = { diff --git a/components/fs/littlefs/lfs_adapter.c b/components/fs/littlefs/lfs_adapter.c index f0d3a706..5af2fb5f 100644 --- a/components/fs/littlefs/lfs_adapter.c +++ b/components/fs/littlefs/lfs_adapter.c @@ -41,6 +41,8 @@ #include "securec.h" #include "los_fs.h" +static pthread_mutex_t g_fsLocalMutex = PTHREAD_MUTEX_INITIALIZER; + static struct PartitionCfg g_partitionCfg; static struct DeviceDesc *g_lfsDevice = NULL; @@ -376,6 +378,7 @@ int LfsReaddir(struct Dir *dir, struct dirent *dent) ret = lfs_dir_read(lfs, dirInfo, &lfsInfo); if (ret == TRUE) { + pthread_mutex_lock(&g_fsLocalMutex); (void)strncpy_s(dent->d_name, sizeof(dent->d_name), lfsInfo.name, strlen(lfsInfo.name) + 1); if (lfsInfo.type == LFS_TYPE_DIR) { dent->d_type = DT_DIR; @@ -384,6 +387,7 @@ int LfsReaddir(struct Dir *dir, struct dirent *dent) } dent->d_reclen = lfsInfo.size; + pthread_mutex_unlock(&g_fsLocalMutex); return LOS_OK; } @@ -563,7 +567,9 @@ int LfsClose(struct File *file) return (int)LOS_NOK; } + pthread_mutex_lock(&g_fsLocalMutex); ret = lfs_file_close((lfs_t *)mp->mData, lfsHandle); + pthread_mutex_unlock(&g_fsLocalMutex); if (ret != 0) { errno = LittlefsErrno(ret); diff --git a/components/fs/vfs/Kconfig b/components/fs/vfs/Kconfig index 226455b6..df878dd2 100644 --- a/components/fs/vfs/Kconfig +++ b/components/fs/vfs/Kconfig @@ -34,11 +34,3 @@ 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 diff --git a/components/fs/vfs/los_fs.h b/components/fs/vfs/los_fs.h index d0ce2143..f1efc4a4 100644 --- a/components/fs/vfs/los_fs.h +++ b/components/fs/vfs/los_fs.h @@ -139,17 +139,6 @@ 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 diff --git a/components/fs/vfs/vfs_fs.c b/components/fs/vfs/vfs_fs.c index 7fcd849c..6afbe44a 100644 --- a/components/fs/vfs/vfs_fs.c +++ b/components/fs/vfs/vfs_fs.c @@ -100,20 +100,20 @@ int PollQueryFd(int fd, struct PollTable *table) UINT32 g_fsMutex; static UINT32 g_dirNum = 0; -int LOS_FsLock(void) +int VfsLock(void) { if (!OsCheckKernelRunning()) { return LOS_OK; } - if (LOS_MuxPend(g_fsMutex, (UINT32)LOSCFG_FS_LOCK_TIMEOUT) != LOS_OK) { - PRINT_ERR("LOS_FsLock failed!"); + if (LOS_MuxPend(g_fsMutex, LOS_WAIT_FOREVER) != LOS_OK) { + PRINT_ERR("VfsLock failed!"); return (int)LOS_NOK; } return LOS_OK; } -void LOS_FsUnlock(void) +void VfsUnlock(void) { if (!OsCheckKernelRunning()) { return; @@ -224,7 +224,7 @@ static int VfsOpen(const char *path, int flags) return fd; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != 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); - LOS_FsUnlock(); + VfsUnlock(); 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); - LOS_FsUnlock(); + VfsUnlock(); return fd; } file = VfsFileGet(); if (file == NULL) { VFS_ERRNO_SET(ENFILE); - LOS_FsUnlock(); + VfsUnlock(); return fd; } @@ -258,7 +258,7 @@ static int VfsOpen(const char *path, int flags) if (file->fullPath == NULL) { VFS_ERRNO_SET(ENOMEM); VfsFilePut(file); - LOS_FsUnlock(); + VfsUnlock(); return (int)LOS_NOK; } (void)strcpy_s((char *)file->fullPath, len, path); @@ -279,7 +279,7 @@ static int VfsOpen(const char *path, int flags) VfsFilePut(file); } - LOS_FsUnlock(); + VfsUnlock(); return fd; } @@ -294,7 +294,7 @@ static struct File *VfsAttachFile(int fd, UINT32 status) return NULL; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EFAULT); return NULL; } @@ -302,13 +302,13 @@ static struct File *VfsAttachFile(int fd, UINT32 status) file = FdToFile(fd); if ((file == NULL) || (file->fMp == NULL)) { VFS_ERRNO_SET(EBADF); - LOS_FsUnlock(); + VfsUnlock(); return NULL; } if (file->fStatus != FILE_STATUS_READY) { VFS_ERRNO_SET(EBADF); - LOS_FsUnlock(); + VfsUnlock(); return NULL; } @@ -329,7 +329,7 @@ static struct File *VfsAttachFileWithStatus(int fd, int status) static void VfsDetachFile(const struct File *file) { (void)file; - LOS_FsUnlock(); + VfsUnlock(); } static int VfsClose(int fd) @@ -674,7 +674,7 @@ int stat(const char *path, struct stat *stat) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -682,7 +682,7 @@ int stat(const char *path, struct stat *stat) mp = VfsMpFind(path, &pathInMp); if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -692,7 +692,7 @@ int stat(const char *path, struct stat *stat) VFS_ERRNO_SET(ENOTSUP); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } FUNC_ALIAS(stat, _stat, (const char *path, struct stat *stat), int); @@ -708,7 +708,7 @@ int statfs(const char *path, struct statfs *buf) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -716,7 +716,7 @@ int statfs(const char *path, struct statfs *buf) mp = VfsMpFind(path, &pathInMp); if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -726,7 +726,7 @@ int statfs(const char *path, struct statfs *buf) VFS_ERRNO_SET(ENOTSUP); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -741,7 +741,7 @@ int unlink(const char *path) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -750,13 +750,13 @@ int unlink(const char *path) if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->mFs->fsFops->unlink == NULL)) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } ret = mp->mFs->fsFops->unlink(mp, pathInMp); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } FUNC_ALIAS(unlink, _unlink, (const char *path), int); @@ -774,7 +774,7 @@ int rename(const char *oldpath, const char *newpath) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -783,27 +783,27 @@ int rename(const char *oldpath, const char *newpath) if (pathInMpOld == NULL) { VFS_ERRNO_SET(EINVAL); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } if ((mpOld == NULL) || (*pathInMpOld == '\0') || (mpOld->mFs->fsFops->unlink == NULL)) { VFS_ERRNO_SET(EINVAL); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } mpNew = VfsMpFind(newpath, &pathInMpNew); if ((mpNew == NULL) || (pathInMpNew == NULL) || (*pathInMpNew == '\0') || (mpNew->mFs->fsFops->unlink == NULL)) { VFS_ERRNO_SET(EINVAL); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } if (mpOld != mpNew) { VFS_ERRNO_SET(EXDEV); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -813,7 +813,7 @@ int rename(const char *oldpath, const char *newpath) VFS_ERRNO_SET(ENOTSUP); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -861,7 +861,7 @@ DIR *opendir(const char *path) return NULL; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); LOSCFG_FS_FREE_HOOK(dir); return NULL; @@ -869,7 +869,7 @@ DIR *opendir(const char *path) if (g_dirNum >= LOSCFG_MAX_OPEN_DIRS) { VFS_ERRNO_SET(ENFILE); - LOS_FsUnlock(); + VfsUnlock(); LOSCFG_FS_FREE_HOOK(dir); return NULL; } @@ -877,14 +877,14 @@ DIR *opendir(const char *path) mp = VfsMpFind(path, &pathInMp); if ((mp == NULL) || (pathInMp == NULL)) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); LOSCFG_FS_FREE_HOOK(dir); return NULL; } if (mp->mFs->fsFops->opendir == NULL) { VFS_ERRNO_SET(ENOTSUP); - LOS_FsUnlock(); + VfsUnlock(); LOSCFG_FS_FREE_HOOK(dir); return NULL; } @@ -901,7 +901,7 @@ DIR *opendir(const char *path) dir = NULL; } - LOS_FsUnlock(); + VfsUnlock(); return (DIR *)dir; } @@ -915,7 +915,7 @@ struct dirent *readdir(DIR *dir) return NULL; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return NULL; } @@ -929,7 +929,7 @@ struct dirent *readdir(DIR *dir) VFS_ERRNO_SET(ENOTSUP); } - LOS_FsUnlock(); + VfsUnlock(); return ret; } @@ -947,7 +947,7 @@ int closedir(DIR *dir) mp = d->dMp; - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -966,7 +966,7 @@ int closedir(DIR *dir) VFS_ERRNO_SET(EBADF); } - LOS_FsUnlock(); + VfsUnlock(); LOSCFG_FS_FREE_HOOK(d); d = NULL; return MapToPosixRet(ret); @@ -984,7 +984,7 @@ int mkdir(const char *path, mode_t mode) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -992,7 +992,7 @@ int mkdir(const char *path, mode_t mode) mp = VfsMpFind(path, &pathInMp); if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0')) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -1002,7 +1002,7 @@ int mkdir(const char *path, mode_t mode) VFS_ERRNO_SET(ENOTSUP); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -1017,7 +1017,7 @@ int rmdir(const char *path) return MapToPosixRet(ret); } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet(ret); } @@ -1026,13 +1026,13 @@ int rmdir(const char *path) if ((mp == NULL) || (pathInMp == NULL) || (*pathInMp == '\0') || (mp->mFs->fsFops->rmdir == NULL)) { VFS_ERRNO_SET(ENOENT); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } ret = mp->mFs->fsFops->rmdir(mp, pathInMp); - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet(ret); } @@ -1312,31 +1312,31 @@ ssize_t pread(int fd, void *buff, size_t bytes, off_t off) return 0; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet((int)ret); } savepos = lseek(fd, 0, SEEK_CUR); if (savepos == (off_t)-1) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } pos = lseek(fd, off, SEEK_SET); if (pos == (off_t)-1) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } ret = read(fd, buff, bytes); pos = lseek(fd, savepos, SEEK_SET); if ((pos == (off_t)-1) && (ret >= 0)) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)LOS_NOK); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } @@ -1359,31 +1359,31 @@ ssize_t pwrite(int fd, const void *buff, size_t bytes, off_t off) return 0; } - if (LOS_FsLock() != LOS_OK) { + if (VfsLock() != LOS_OK) { VFS_ERRNO_SET(EAGAIN); return MapToPosixRet((int)ret); } savepos = lseek(fd, 0, SEEK_CUR); if (savepos == (off_t)-1) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } pos = lseek(fd, off, SEEK_SET); if (pos == (off_t)-1) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } ret = write(fd, buff, bytes); pos = lseek(fd, savepos, SEEK_SET); if ((pos == (off_t)-1) && (ret >= 0)) { - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)LOS_NOK); } - LOS_FsUnlock(); + VfsUnlock(); return MapToPosixRet((int)ret); } diff --git a/components/fs/vfs/vfs_maps.c b/components/fs/vfs/vfs_maps.c index 6b9dd167..bec4c1a1 100644 --- a/components/fs/vfs/vfs_maps.c +++ b/components/fs/vfs/vfs_maps.c @@ -41,17 +41,17 @@ struct FsMap *VfsFsMapGet(const char *fsType) { struct FsMap *curr = g_fsMap; - (void)LOS_FsLock(); + (void)VfsLock(); while (curr != NULL) { if ((curr->fsType != NULL) && (fsType != NULL) && (strcmp(curr->fsType, fsType) == 0)) { - LOS_FsUnlock(); + (void)VfsUnlock(); return curr; } curr = curr->next; } - LOS_FsUnlock(); + VfsUnlock(); return NULL; } @@ -86,11 +86,11 @@ int OsFsRegister(const char *fsType, const struct MountOps *fsMops, newfs->fsMgt = fsMgt; newfs->fsRefs = 0; - (void)LOS_FsLock(); + (void)VfsLock(); newfs->next = g_fsMap; g_fsMap = newfs; - LOS_FsUnlock(); + VfsUnlock(); return LOS_OK; } diff --git a/components/fs/vfs/vfs_mount.c b/components/fs/vfs/vfs_mount.c index 30144f44..3b939643 100644 --- a/components/fs/vfs/vfs_mount.c +++ b/components/fs/vfs/vfs_mount.c @@ -191,17 +191,17 @@ int mount(const char *source, const char *target, return (int)LOS_NOK; } - (void)LOS_FsLock(); + (void)VfsLock(); if (mountflags & MS_REMOUNT) { ret = VfsRemount(source, target, fsType, mountflags, data); - LOS_FsUnlock(); + VfsUnlock(); return ret; } mp = VfsMountPointInit(target, fsType, mountflags); if (mp == NULL) { - LOS_FsUnlock(); + VfsUnlock(); 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); - LOS_FsUnlock(); + VfsUnlock(); 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; - LOS_FsUnlock(); + VfsUnlock(); return LOS_OK; errout: LOSCFG_FS_FREE_HOOK((void *)mp->mPath); LOSCFG_FS_FREE_HOOK((void *)mp->mDev); LOSCFG_FS_FREE_HOOK(mp); - LOS_FsUnlock(); + VfsUnlock(); return (int)LOS_NOK; } @@ -248,7 +248,7 @@ int umount(const char *target) const char *pathInMp = NULL; int ret = (int)LOS_NOK; - (void)LOS_FsLock(); + (void)VfsLock(); 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); - LOS_FsUnlock(); + VfsUnlock(); return LOS_OK; errout: PRINT_ERR("umount2 failed, target %s.\n", target); - LOS_FsUnlock(); + VfsUnlock(); 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)LOS_FsLock(); + (void)VfsLock(); 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); - LOS_FsUnlock(); + VfsUnlock(); return LOS_OK; errout: PRINT_ERR("umount2 failed, target %s.\n", target); - LOS_FsUnlock(); + VfsUnlock(); return (int)LOS_NOK; } diff --git a/components/fs/vfs/vfs_operations.h b/components/fs/vfs/vfs_operations.h index 0e33206c..4ee430f9 100644 --- a/components/fs/vfs/vfs_operations.h +++ b/components/fs/vfs/vfs_operations.h @@ -52,6 +52,8 @@ extern "C" { extern UINT32 g_fsMutex; int OsVfsInit(void); +int VfsLock(void); +void VfsUnlock(void); #ifdef __cplusplus #if __cplusplus