Pre Merge pull request !1050 from wangchen/0412_umount

This commit is contained in:
wangchen 2024-03-26 06:58:50 +00:00 committed by Gitee
commit a42ddace4a
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 70 additions and 9 deletions

View File

@ -87,6 +87,7 @@ struct Dir {
struct dirent dDent; struct dirent dDent;
off_t dOffset; off_t dOffset;
void *dData; void *dData;
struct Dir *next;
}; };
int FileToFd(const struct File *file); int FileToFd(const struct File *file);
@ -94,6 +95,7 @@ struct File *FdToFile(int fd);
struct File *VfsFileGet(void); struct File *VfsFileGet(void);
struct File *VfsFileGetSpec(int fd); struct File *VfsFileGetSpec(int fd);
void VfsFilePut(struct File *file); void VfsFilePut(struct File *file);
void CloseDirInMp(struct MountPoint *mp);
#ifdef __cplusplus #ifdef __cplusplus
#if __cplusplus #if __cplusplus

View File

@ -93,6 +93,57 @@ int PollQueryFd(int fd, struct PollTable *table)
UINT32 g_fsMutex; UINT32 g_fsMutex;
static UINT32 g_dirNum = 0; static UINT32 g_dirNum = 0;
static struct Dir *g_dirs = NULL;
static void AddToDirList(struct Dir *d)
{
d->next = g_dirs;
g_dirs = d;
}
static void DelFromDirList(struct Dir *d)
{
struct Dir *node = g_dirs;
if (d == g_dirs) {
g_dirs = d->next;
return;
}
while (node->next != d) {
node = node->next;
}
node->next = d->next;
}
void CloseDirInMp(struct MountPoint *mp)
{
struct Dir *node = g_dirs;
struct Dir *tmp = node;
while (node != NULL) {
if (node->dMp != mp) {
tmp = node;
node = node->next;
continue;
}
tmp->next = node->next;
if ((node->dMp->mFs != NULL) && (node->dMp->mFs->fsFops != NULL) &&
(node->dMp->mFs->fsFops->closedir != NULL)) {
(void)node->dMp->mFs->fsFops->closedir(node);
}
LOSCFG_FS_FREE_HOOK(node);
mp->mRefs--;
g_dirNum--;
node = tmp->next;
}
g_dirs = NULL;
}
int LOS_FsLock(void) int LOS_FsLock(void)
{ {
if (!OsCheckKernelRunning()) { if (!OsCheckKernelRunning()) {
@ -932,6 +983,7 @@ DIR *opendir(const char *path)
if (ret == 0) { if (ret == 0) {
mp->mRefs++; mp->mRefs++;
g_dirNum++; g_dirNum++;
AddToDirList(dir);
} else { } else {
LOSCFG_FS_FREE_HOOK(dir); LOSCFG_FS_FREE_HOOK(dir);
dir = NULL; dir = NULL;
@ -998,6 +1050,7 @@ int closedir(DIR *dir)
if (ret == 0) { if (ret == 0) {
mp->mRefs--; mp->mRefs--;
g_dirNum--; g_dirNum--;
DelFromDirList(d);
} else { } else {
VFS_ERRNO_SET(EBADF); VFS_ERRNO_SET(EBADF);
} }

View File

@ -346,18 +346,19 @@ errout:
return (int)LOS_NOK; return (int)LOS_NOK;
} }
static void CloseFdsInMp(const struct MountPoint *mp) static void CloseFdsInMp(struct MountPoint *mp)
{ {
for (int fd = 0; fd < NR_OPEN_DEFAULT; fd++) { for (int fd = 0; fd < NR_OPEN_DEFAULT; fd++) {
struct File *f = FdToFile(fd); struct File *f = FdToFile(fd);
if (f == NULL) { if ((f == NULL) || (f->fMp != mp)) {
continue; continue;
} }
if ((f->fMp == mp) && if ((f->fFops != NULL) &&
(f->fFops != NULL) &&
(f->fFops->close != NULL)) { (f->fFops->close != NULL)) {
(void)f->fFops->close(f); (void)f->fFops->close(f);
} }
mp->mRefs--;
VfsFilePut(f);
} }
} }
@ -372,15 +373,20 @@ int umount2(const char *target, int flag)
} }
mp = VfsMpFind(target, NULL); mp = VfsMpFind(target, NULL);
if ((mp == NULL) || (mp->mRefs != 0) || if ((mp == NULL) || (mp->mFs == NULL) ||
(mp->mFs == NULL) || (mp->mFs->fsMops == NULL) || (mp->mFs->fsMops == NULL) ||
(mp->mFs->fsMops->umount2 == NULL)) { (mp->mFs->fsMops->umount2 == NULL)) {
goto errout; goto errout;
} }
/* Close all files under the mount point */ if (mp->mRefs != 0) {
if ((UINT32)flag & MNT_FORCE) { if ((UINT32)flag & MNT_FORCE) {
CloseFdsInMp(mp); CloseFdsInMp(mp);
CloseDirInMp(mp);
LOS_ASSERT(mp->mRefs == 0);
} else {
goto errout;
}
} }
ret = mp->mFs->fsMops->umount2(mp, flag); ret = mp->mFs->fsMops->umount2(mp, flag);