fix: umount2函数无法强制关闭文件
方案描述: 1, 增加DIR链表管理 2,umount2时遍历关闭mp下的fd和dir fix #I6V3HQ Signed-off-by: wangchen <wangchen240@huawei.com>
This commit is contained in:
parent
556c10121b
commit
fd478cae47
|
@ -87,6 +87,7 @@ struct Dir {
|
|||
struct dirent dDent;
|
||||
off_t dOffset;
|
||||
void *dData;
|
||||
struct Dir *next;
|
||||
};
|
||||
|
||||
int FileToFd(const struct File *file);
|
||||
|
@ -94,6 +95,7 @@ struct File *FdToFile(int fd);
|
|||
struct File *VfsFileGet(void);
|
||||
struct File *VfsFileGetSpec(int fd);
|
||||
void VfsFilePut(struct File *file);
|
||||
void CloseDirInMp(struct MountPoint *mp);
|
||||
|
||||
#ifdef __cplusplus
|
||||
#if __cplusplus
|
||||
|
|
|
@ -93,6 +93,57 @@ int PollQueryFd(int fd, struct PollTable *table)
|
|||
UINT32 g_fsMutex;
|
||||
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)
|
||||
{
|
||||
if (!OsCheckKernelRunning()) {
|
||||
|
@ -932,6 +983,7 @@ DIR *opendir(const char *path)
|
|||
if (ret == 0) {
|
||||
mp->mRefs++;
|
||||
g_dirNum++;
|
||||
AddToDirList(dir);
|
||||
} else {
|
||||
LOSCFG_FS_FREE_HOOK(dir);
|
||||
dir = NULL;
|
||||
|
@ -998,6 +1050,7 @@ int closedir(DIR *dir)
|
|||
if (ret == 0) {
|
||||
mp->mRefs--;
|
||||
g_dirNum--;
|
||||
DelFromDirList(d);
|
||||
} else {
|
||||
VFS_ERRNO_SET(EBADF);
|
||||
}
|
||||
|
|
|
@ -346,18 +346,19 @@ errout:
|
|||
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++) {
|
||||
struct File *f = FdToFile(fd);
|
||||
if (f == NULL) {
|
||||
if ((f == NULL) || (f->fMp != mp)) {
|
||||
continue;
|
||||
}
|
||||
if ((f->fMp == mp) &&
|
||||
(f->fFops != NULL) &&
|
||||
if ((f->fFops != NULL) &&
|
||||
(f->fFops->close != NULL)) {
|
||||
(void)f->fFops->close(f);
|
||||
}
|
||||
mp->mRefs--;
|
||||
VfsFilePut(f);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -372,15 +373,20 @@ int umount2(const char *target, int flag)
|
|||
}
|
||||
|
||||
mp = VfsMpFind(target, NULL);
|
||||
if ((mp == NULL) || (mp->mRefs != 0) ||
|
||||
(mp->mFs == NULL) || (mp->mFs->fsMops == NULL) ||
|
||||
if ((mp == NULL) || (mp->mFs == NULL) ||
|
||||
(mp->mFs->fsMops == NULL) ||
|
||||
(mp->mFs->fsMops->umount2 == NULL)) {
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Close all files under the mount point */
|
||||
if ((UINT32)flag & MNT_FORCE) {
|
||||
CloseFdsInMp(mp);
|
||||
if (mp->mRefs != 0) {
|
||||
if ((UINT32)flag & MNT_FORCE) {
|
||||
CloseFdsInMp(mp);
|
||||
CloseDirInMp(mp);
|
||||
LOS_ASSERT(mp->mRefs == 0);
|
||||
} else {
|
||||
goto errout;
|
||||
}
|
||||
}
|
||||
|
||||
ret = mp->mFs->fsMops->umount2(mp, flag);
|
||||
|
|
Loading…
Reference in New Issue