!588 删除zpfs冗余代码

Merge pull request !588 from 马帅/master
This commit is contained in:
openharmony_ci 2021-08-31 07:40:38 +00:00 committed by Gitee
commit f3562e490c
3 changed files with 0 additions and 995 deletions

View File

@ -1,458 +0,0 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "vfs_zpfs.h"
#include <fcntl.h>
#include <stdlib.h>
#include <sys/statfs.h>
#include <unistd.h>
#include "fs/dirent_fs.h"
#include "inode/inode.h"
#include "internal.h"
#include "los_tables.h"
#include "los_vm_filemap.h"
#ifdef LOSCFG_FS_ZPFS
static int CheckEntryExist(const struct ZpfsEntry *zpfsEntry, char **fPath, struct stat *buf)
{
int ret;
struct inode *inode = zpfsEntry->mountedInode;
const char *path = zpfsEntry->mountedPath;
const char *relPath = zpfsEntry->mountedRelpath;
int len = strlen(path) + strlen(relPath) + 1;
int fullLen = len + 1;
char *fullPath = (char *)malloc(fullLen);
if (fullPath == NULL) {
return -1;
}
ret = snprintf_s(fullPath, fullLen, len, "%s/%s", path, relPath);
if (ret < 0) {
free(fullPath);
return -1;
}
if (inode->u.i_mops->stat != NULL) {
ret = inode->u.i_mops->stat(inode, fullPath, buf);
} else {
ret = -ENOSYS;
}
if (fPath == NULL) {
free(fullPath);
} else {
*fPath = fullPath;
}
return ret;
}
static struct inode *GetRelInodeFromVInode(const ZpfsConfig *zpfsCfg,
const char* relPath, struct stat *buf, char **finalPath)
{
int ret;
struct ZpfsEntry entry;
for (int i = 0; i < zpfsCfg->entryCount; i++) {
entry.mountedPath = zpfsCfg->orgEntry[i].mountedRelpath;
entry.mountedRelpath = (char*)relPath;
entry.mountedInode = zpfsCfg->orgEntry[i].mountedInode;
ret = CheckEntryExist(&entry, finalPath, buf);
if (ret == OK) {
return entry.mountedInode;
}
}
return NULL;
}
/****************************************************************************
* Name: VfsZpfsRealInode
*
* Description:
* Get the inode which is hidden.
*
* Input Parameters:
* The relative path.
* Returned Value:
* finalPath the relative path of the mount point.
* OK
* NULL memory is not enough.
****************************************************************************/
static inline struct inode *VfsZpfsRealInode(const ZpfsConfig *zpfsCfg, const char *relPath, char **finalPath)
{
struct stat buf;
struct inode *inode = GetRelInodeFromVInode(zpfsCfg, relPath, &buf, finalPath);
return inode;
}
static int VfsZpfsOpen(struct file *file, const char *relPath, int oflAgs, mode_t mode)
{
char *finalPath = NULL;
struct inode *swapInode = NULL;
int ret;
struct inode *inode = file->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, relPath, &finalPath);
if ((inode == NULL) || (!inode->u.i_mops->open)) {
if (finalPath) {
free(finalPath);
}
return -ENOENT;
}
swapInode = file->f_inode;
file->f_inode = inode;
ret = inode->u.i_mops->open(file, finalPath, oflAgs, mode);
free(finalPath);
finalPath = NULL;
file->f_inode = swapInode;
return ret;
}
static int VfsZpfsClose(struct file *file)
{
struct inode *swapInode = NULL;
int ret;
struct inode *inode = file->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, file->f_relpath, NULL);
if ((inode == NULL) || (!inode->u.i_mops->close)) {
return -ENOSYS;
}
swapInode = file->f_inode;
file->f_inode = inode;
ret = inode->u.i_mops->close(file);
file->f_inode = swapInode;
return ret;
}
static ssize_t VfsZpfsRead(struct file *file, char *buffer, size_t bufLen)
{
struct inode *swapInode = NULL;
ssize_t sret;
struct inode *inode = file->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, file->f_relpath, NULL);
if ((inode == NULL) || (!inode->u.i_mops->read)) {
return -ENOSYS;
}
swapInode = file->f_inode;
file->f_inode = inode;
sret = inode->u.i_mops->read(file, buffer, bufLen);
file->f_inode = swapInode;
return sret;
}
static off_t VfsZpfsLseek(struct file *file, off_t offset, int whence)
{
off_t off;
struct inode *swapInode = NULL;
struct inode *inode = file->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, file->f_relpath, NULL);
if ((inode == NULL) || (!inode->u.i_mops->seek)) {
return -ENOSYS;
}
swapInode = file->f_inode;
file->f_inode = inode;
off = inode->u.i_mops->seek(file, offset, whence);
file->f_inode = swapInode;
return off;
}
static loff_t VfsZpfsLseek64(struct file *file, loff_t offset, int whence)
{
loff_t off;
struct inode *swapInode = NULL;
struct inode *inode = file->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, file->f_relpath, NULL);
if ((inode == NULL) || (!inode->u.i_mops->seek64)) {
return -ENOSYS;
}
swapInode = file->f_inode;
file->f_inode = inode;
off = inode->u.i_mops->seek64(file, offset, whence);
file->f_inode = swapInode;
return off;
}
static int VfsZpfsDup(const struct file *oldFile, struct file *newFile)
{
int ret;
struct inode *inode = oldFile->f_inode;
ZpfsConfig *zpfsCfg = GetZpfsConfig(inode);
inode = VfsZpfsRealInode(zpfsCfg, oldFile->f_relpath, NULL);
if ((inode == NULL) || (!inode->u.i_mops->dup)) {
return -ENOSYS;
}
ret = inode->u.i_mops->dup(oldFile, newFile);
return ret;
}
static int VfsZpfsOpenDir(struct inode *mountpt, const char *relPath, struct fs_dirent_s *dir)
{
ZpfsConfig *zpfsCfg = GetZpfsConfig(mountpt);
if (!VfsZpfsRealInode(zpfsCfg, relPath, NULL)) {
return -ENOENT;
}
struct ZpfsDir *zpfsDir = malloc(sizeof(struct ZpfsDir));
if (zpfsDir == NULL) {
return -ENOMEM;
}
zpfsDir->relPath = strdup(relPath);
if (zpfsDir->relPath == NULL) {
return -ENOMEM;
}
zpfsDir->index = zpfsCfg->entryCount;
zpfsDir->openEntry = -1;
dir->u.zpfs = (void*)zpfsDir;
return OK;
}
static void CloseEntry(const struct ZpfsConfig *zpfsCfg, struct fs_dirent_s *dir, const int index)
{
struct inode *inode = zpfsCfg->orgEntry[index].mountedInode;
if (inode->u.i_mops->closedir != NULL) {
inode->u.i_mops->closedir(inode, dir);
}
}
static int VfsZpfsCloseDir(struct inode *mountpt, struct fs_dirent_s *dir)
{
struct ZpfsDir *zpfsDir = (struct ZpfsDir *)(dir->u.zpfs);
if (zpfsDir->relPath) {
free(zpfsDir->relPath);
zpfsDir->relPath = NULL;
}
free(zpfsDir);
zpfsDir = NULL;
return OK;
}
static int IsExistInEntries(const struct ZpfsConfig *zpfsCfg, const char* relPath, const int index)
{
struct ZpfsEntry entry;
struct stat buf;
for (int i = index; i >= 0; i--) {
entry.mountedPath = zpfsCfg->orgEntry[i].mountedRelpath;
entry.mountedRelpath = (char *)relPath;
entry.mountedInode = zpfsCfg->orgEntry[i].mountedInode;
if (CheckEntryExist(&entry, NULL, &buf) == OK) {
return OK;
}
}
return -1;
}
static int OpenEntry(const struct ZpfsConfig *zpfsCfg,
const struct ZpfsDir *zpfsDir, struct fs_dirent_s *dir, const int index)
{
int ret;
char *fullPath;
char *path = (char *)zpfsCfg->orgEntry[index].mountedRelpath;
char *relPath = zpfsDir->relPath;
struct inode *curInode = zpfsCfg->orgEntry[index].mountedInode;
int len = strlen(path) + strlen(relPath) + 1;
int fullLen = len + 1;
fullPath = (char *)malloc(fullLen);
if (fullPath == NULL) {
return -ENOMEM;
}
ret = snprintf_s(fullPath, fullLen, len, "%s/%s", path, relPath);
if (ret < 0) {
free(fullPath);
return -EINVAL;
}
ret = -ENOSYS;
if (curInode->u.i_mops->opendir != NULL) {
ret = curInode->u.i_mops->opendir(curInode, fullPath, dir);
}
free(fullPath);
return ret;
}
static int VfsZpfsReadDir(struct inode *mountpt, struct fs_dirent_s *dir)
{
int ret;
struct inode *oldInode = NULL;
struct inode *curInode = NULL;
struct ZpfsDir *zpfsDir = (struct ZpfsDir *)dir->u.zpfs;
ZpfsConfig *zpfsCfg = GetZpfsConfig(mountpt);
int index = zpfsDir->index;
do {
if (zpfsDir->openEntry == -1) {
zpfsDir->index--;
index = zpfsDir->index;
if (index < 0) {
ret = -1;
break;
}
ret = OpenEntry(zpfsCfg, zpfsDir, dir, index);
if (ret != OK) {
break;
}
zpfsDir->openEntry = 1;
}
curInode = zpfsCfg->orgEntry[index].mountedInode;
oldInode = dir->fd_root;
dir->fd_root = curInode;
ret = curInode->u.i_mops->readdir(curInode, dir);
dir->fd_root = oldInode;
if (ret != OK) {
if (index >= 0) {
CloseEntry(zpfsCfg, dir, index);
zpfsDir->openEntry = -1;
continue;
}
} else if (IsExistInEntries(zpfsCfg, dir->fd_dir[0].d_name, (index - 1)) == OK) {
continue;
}
ret = 1; // 1 means current op return one file
dir->fd_position++;
dir->fd_dir[0].d_off = dir->fd_position;
dir->fd_dir[0].d_reclen = (uint16_t)sizeof(struct dirent);
break;
} while (1);
return ret;
}
static int VfsZpfsRewindDir(struct inode *mountpt, struct fs_dirent_s *dir)
{
PRINT_DEBUG("%s NOT support!\n", __FUNCTION__);
return OK;
}
static int VfsZpfsBind(struct inode *blkDriver, const void *data, void **handle, const char *relPath)
{
if (data == NULL) {
return -1;
}
(*handle) = (void*)data;
return OK;
}
static int VfsZpfsUnbind(void *handle, struct inode **blkDriver)
{
struct inode *node = NULL;
ZpfsConfig *zpfsCfg = NULL;
if (handle != NULL) {
zpfsCfg = (ZpfsConfig *)handle;
node = inode_unlink(zpfsCfg->patchTarget);
INODE_SET_TYPE(node, FSNODEFLAG_DELETED);
if (node != zpfsCfg->patchInode) {
return -EINVAL;
}
}
return OK;
}
static int VfsZpfsStatFs(struct inode *mountpt, struct statfs *buf)
{
if (buf == NULL) {
return -EINVAL;
}
(void)memset_s(buf, sizeof(struct statfs), 0, sizeof(struct statfs));
buf->f_type = ZPFS_MAGIC;
return OK;
}
static int VfsZpfsStat(struct inode *mountpt, const char *relPath, struct stat *buf)
{
struct inode *inode = NULL;
ZpfsConfig *zpfsCfg = GetZpfsConfig(mountpt);
inode = GetRelInodeFromVInode(zpfsCfg, relPath, buf, NULL);
if (inode == NULL) {
return -ENOENT;
}
return OK;
}
const struct mountpt_operations zpfsOperations = {
VfsZpfsOpen, /* open */
VfsZpfsClose, /* close */
VfsZpfsRead, /* read */
NULL, /* write */
VfsZpfsLseek, /* seek */
NULL, /* ioctl */
OsVfsFileMmap, /* mmap */
NULL, /* sync */
VfsZpfsDup, /* dup */
NULL, /* fstat */
NULL, /* truncate */
VfsZpfsOpenDir, /* opendir */
VfsZpfsCloseDir, /* closedir */
VfsZpfsReadDir, /* readdir */
VfsZpfsRewindDir, /* rewinddir */
VfsZpfsBind, /* bind */
VfsZpfsUnbind, /* unbind */
VfsZpfsStatFs, /* statfs */
NULL, /* virstatfs */
NULL, /* unlink */
NULL, /* mkdir */
NULL, /* rmdir */
NULL, /* rename */
VfsZpfsStat, /* stat */
NULL, /* utime */
NULL, /* chattr */
VfsZpfsLseek64, /* seek64 */
NULL, /* getlabel */
NULL, /* fallocate */
NULL, /* fallocate64 */
NULL, /* truncate64 */
NULL, /* fscheck */
NULL, /* map_pages */
NULL, /* readpage */
NULL, /* writepage */
};
FSMAP_ENTRY(zpfs_fsmap, ZPFS_NAME, zpfsOperations, FALSE, FALSE);
#endif // LOSCFG_FS_ZPFS

View File

@ -1,73 +0,0 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef ZPFS_VFS_ZPFS_H
#define ZPFS_VFS_ZPFS_H
#include "compiler.h"
#include "los_base.h"
#include "los_typedef.h"
#ifdef LOSCFG_FS_ZPFS
#define ZPFS_NAME "zpfs"
#define ZPFS_LEVELS 3
typedef struct ZpfsDir {
char *relPath;
int index;
int openEntry;
} ZpfsDir;
typedef struct ZpfsEntry {
struct inode *mountedInode;
char *mountedPath;
char *mountedRelpath;
} ZpfsEntry;
typedef struct ZpfsConfig {
char *patchTarget;
struct inode *patchTargetInode;
struct inode *patchInode;
int entryCount;
ZpfsEntry orgEntry[ZPFS_LEVELS];
} ZpfsConfig;
ZpfsConfig *GetZpfsConfig(const struct inode *inode);
int ZpfsPrepare(const char *source, const char *target, struct inode **inodePtr, bool force);
void ZpfsFreeConfig(ZpfsConfig *zpfsCfg);
void ZpfsCleanUp(const void *node, const char *target);
bool IsZpfsFileSystem(struct inode *inode);
#endif /* LOSCFG_FS_ZPFS */
#endif /* ZPFS_VFS_ZPFS_H */

View File

@ -1,464 +0,0 @@
/*
* Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
* Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "vfs_zpfs.h"
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/statfs.h>
#include "fs/dirent_fs.h"
#include "inode/inode.h"
#include "internal.h"
#include "los_tables.h"
#ifdef LOSCFG_FS_ZPFS
/****************************************************************************
* Name: CheckEntryExist
*
* Description:
* check if entry is exist or not.
* Input Parameters:
* The path
* Returned Value:
* true or false, it is true for the exist of path.
****************************************************************************/
bool CheckEntryExist(const char *entry)
{
struct stat64 stat64Info;
struct stat statInfo;
if (stat64(entry, &stat64Info) == 0) {
if (S_ISDIR(stat64Info.st_mode) != 0) {
return true;
} else {
return false;
}
} else if (stat(entry, &statInfo) == 0) {
if (S_ISDIR(statInfo.st_mode) != 0) {
return true;
} else {
return false;
}
}
return false;
}
/****************************************************************************
* Name: GetZpfsConfig
*
* Description:
* get the zpfs configuration
* Input Parameters:
* void
* Returned Value:
* g_zpfsConfig Save the patch gobal data.
****************************************************************************/
ZpfsConfig *GetZpfsConfig(const struct inode *inode)
{
ZpfsConfig *zpfsCfg = (ZpfsConfig*)(inode->i_private);
return zpfsCfg;
}
/****************************************************************************
* Name: GetSourceList
*
* Description:
* Get the source path list.
* eg: /patch/etc:/patch/etc1:/patch/etc2
* Input Parameters:
* source: /patch/etc:/patch/etc1:/patch/etc2
* sourcelist: output path arry
* num: output list num
*
* Returned Value:
* OK The mounted folder parameter is right.
* EINVAL The mounted folder parameter is wrong.
* ENOMEM The memory is not enough
****************************************************************************/
static int GetSourceList(const char *source, char *sourceList[ZPFS_LEVELS], int *num)
{
char *subSource = NULL;
char *path = NULL;
char *subPath = NULL;
int index;
int ret;
subSource = strdup(source);
if (subSource == NULL) {
return -ENOMEM;
}
subPath = strtok_r(subSource, ":", &path);
for (*num = 0; *num < ZPFS_LEVELS && subPath != NULL; (*num)++) {
sourceList[*num] = strdup(subPath);
if (sourceList[*num] == NULL) {
ret = -ENOMEM;
goto EXIT;
}
subPath = strtok_r(NULL, ":", &path);
}
if (subPath != NULL || *num == 0) {
PRINTK("source path num %d error\n", *num);
ret = -EINVAL;
goto EXIT;
}
free(subSource);
return OK;
EXIT:
free(subSource);
for (index = 0; index < *num; index++) {
free(sourceList[index]);
sourceList[index] = NULL;
}
*num = 0;
return ret;
}
/****************************************************************************
* Name: CheckInputParamsFormat
*
* Description:
* check the mounted folder format and validity. MAX three segment.
* eg: /patch/etc:/patch/etc1:/patch/etc2
* Input Parameters:
* check the mounted folder. eg:
* source: /patch/etc:/patch/etc1:/patch/etc2
* target: /etc
*
* Returned Value:
* OK The mounted folder parameter is right.
* EINVAL The mounted folder parameter is wrong.
* ENOMEM The memory is not enough
****************************************************************************/
static int CheckInputParamsFormat(const char *source, const char *target)
{
char *sourceList[ZPFS_LEVELS] = {0};
int num = 0;
int ret;
int index;
bool isTargetValid = false;
if (source == NULL || target == NULL) {
return -EINVAL;
}
ret = GetSourceList(source, sourceList, &num);
if (ret != OK) {
goto EXIT;
}
for (index = 0; index < num; index++) {
if (!CheckEntryExist(sourceList[index])) {
ret = -ENOENT;
goto EXIT;
}
/* target must same with one source path */
if (strcmp(target, sourceList[index]) == 0) {
isTargetValid = true;
}
}
if (!isTargetValid) {
ret = -EINVAL;
goto EXIT;
}
ret = OK;
EXIT:
for (index = 0; index < num; index++) {
free(sourceList[index]);
}
return ret;
}
static void ZpfsFreeEntry(ZpfsEntry *zpfsEntry)
{
if (zpfsEntry->mountedPath != NULL) {
free(zpfsEntry->mountedPath);
zpfsEntry->mountedPath = NULL;
}
if (zpfsEntry->mountedRelpath != NULL) {
free(zpfsEntry->mountedRelpath);
zpfsEntry->mountedRelpath = NULL;
}
zpfsEntry->mountedInode = NULL;
}
/****************************************************************************
* Name: ZpfsFreeConfig
*
* Description:
* Umount the patch file system,
* Delete the global patch data and the mount pseudo point.
*
* Input Parameters:
* void
* Returned Value:
* void
****************************************************************************/
void ZpfsFreeConfig(ZpfsConfig *zpfsCfg)
{
struct inode *pInode = NULL;
if (zpfsCfg == NULL) {
return;
}
pInode = zpfsCfg->patchInode;
if (pInode != NULL) {
INODE_SET_TYPE(pInode, FSNODEFLAG_DELETED);
inode_release(pInode);
pInode = NULL;
}
for (int i = 0; i < zpfsCfg->entryCount; i++) {
ZpfsFreeEntry(&(zpfsCfg->orgEntry[i]));
}
if (zpfsCfg->patchTarget != NULL) {
free(zpfsCfg->patchTarget);
zpfsCfg->patchTarget = NULL;
}
free(zpfsCfg);
zpfsCfg = NULL;
}
/****************************************************************************
* Name: IsTargetMounted
* Description:
* The mount patch target path must be the mounted path.
* Input Parameters:
* target: The mount point
* Returned Value:
* OK
* EINVAL The mount patch target path is not the mounted path.
****************************************************************************/
static int IsTargetMounted(const char *target)
{
struct inode *inode = NULL;
char *path = NULL;
struct statfs buf;
if (!CheckEntryExist(target)) {
return -EINVAL;
}
inode = inode_search((const char **)&target, (struct inode**)NULL,
(struct inode**)NULL, (const char **)&path);
if (inode == NULL || !INODE_IS_MOUNTPT(inode)) {
PRINT_ERR("Can't to mount to this inode %s\n", target);
return -EINVAL;
}
if ((inode->u.i_mops != NULL) && (inode->u.i_mops->statfs != NULL)) {
if (inode->u.i_mops->statfs(inode, &buf) == OK) {
if (buf.f_type == ZPFS_MAGIC) {
return -EEXIST;
}
}
}
if (path == NULL || path[0] == '\0') {
return -EEXIST;
}
return OK;
}
/****************************************************************************
* Name: SaveZpfsParameter
*
* Description:
* When the patch file system is mounted, the patch gobal data is built.
*
* Input Parameters:
* source: The mounted patch folder
* target: The mount point
* Returned Value:
* OK Save the patch gobal data.
* ENOMEM The memory is not enough.
* EBADMSG There is no data.
****************************************************************************/
static int SaveZpfsParameter(const char *source, const char *target, struct ZpfsConfig *zpfsCfg)
{
int ret;
const char *path = NULL;
int num = 0;
int index;
char *sourceList[ZPFS_LEVELS] = {0};
/* save the mount point */
zpfsCfg->patchTarget = strdup(target);
if (zpfsCfg->patchTarget == NULL) {
return -ENOMEM;
}
/* save the mount folder inode */
zpfsCfg->patchTargetInode = inode_search((const char **)&target,
(struct inode**)NULL, (struct inode**)NULL, &path);
if (zpfsCfg->patchTargetInode == NULL) {
return -EINVAL;
}
ret = GetSourceList(source, sourceList, &num);
if (ret != OK) {
return ret;
}
zpfsCfg->entryCount = num;
for (index = 0; index < num; index++) {
zpfsCfg->orgEntry[index].mountedPath = sourceList[index];
/* save the mounted folder inode */
zpfsCfg->orgEntry[index].mountedInode = inode_search((const char **)&sourceList[index],
(struct inode**)NULL, (struct inode**)NULL, &path);
if (zpfsCfg->orgEntry[index].mountedInode == NULL) {
ret = -EINVAL;
goto ERROR_PROCESS;
}
/* save the mounted relative path */
zpfsCfg->orgEntry[index].mountedRelpath = (char*)path;
}
return OK;
ERROR_PROCESS:
for (index = 0; index < num; index++) {
free(sourceList[index]);
sourceList[index] = NULL;
}
return ret;
}
/****************************************************************************
* Name: ZpfsPrepare
* Description:
* The main function of mount zpfs.
* Input Parameters:
* source: The mounted parameter
* target: The mount path
* inodePtr: the new inode info of target
* force: It is true for zpfs
* Returned Value:
* OK
* EINVAL The parameter is wrong.
* ENOMEM It is not enough memory.
****************************************************************************/
int ZpfsPrepare(const char *source, const char *target, struct inode **inodePtr, bool force)
{
/* Check the mounted folder parameter */
int ret = CheckInputParamsFormat(source, target);
if (ret != OK) {
PRINT_ERR("Parameter is err source:%s target:%s.\n", source, target);
return ret;
}
/* The mount target path must be the mounted path */
ret = IsTargetMounted(target);
if (ret != OK) {
PRINT_ERR("Can't to mount to this inode %s\n", target);
return ret;
}
ZpfsConfig *zpfsCfg = (ZpfsConfig*) malloc(sizeof(struct ZpfsConfig));
if (zpfsCfg == NULL) {
PRINT_ERR("Memory is not enought.\n");
return -ENOMEM;
}
(void)memset_s(zpfsCfg, sizeof(struct ZpfsConfig), 0, sizeof(struct ZpfsConfig));
/* Save the patch global data */
ret = SaveZpfsParameter(source, target, zpfsCfg);
if (ret != OK) {
ZpfsFreeConfig(zpfsCfg);
PRINT_ERR("Memory is not enought.\n");
return ret;
}
/* Create the patch inode */
ret = inode_reserve_rootdir(target, inodePtr, force);
if (ret != OK) {
ZpfsFreeConfig(zpfsCfg);
PRINT_ERR("failed to create mounted inode.\n");
return ret;
}
zpfsCfg->patchInode = *inodePtr;
(*inodePtr)->i_private = zpfsCfg;
return ret;
}
/****************************************************************************
* Name: ZpfsCleanUp
* Description:
* Clean the configuration of zpfs.
* Input Parameters:
* target: The mount path
* Returned Value:
* void
****************************************************************************/
void ZpfsCleanUp(const void *node, const char *target)
{
struct statfs buf;
struct inode *inode = (struct inode *)node;
if ((target == NULL) || (inode == NULL) ||
(inode->u.i_mops == NULL) ||
(inode->u.i_mops->statfs == NULL)) {
return;
}
if (inode->u.i_mops->statfs(inode, &buf) == OK) {
if (buf.f_type == ZPFS_MAGIC) {
ZpfsConfig *zpfsCfg = inode->i_private;
if (strcmp(zpfsCfg->patchTarget, target) == 0) {
ZpfsFreeConfig(zpfsCfg);
}
}
}
}
bool IsZpfsFileSystem(struct inode *inode)
{
struct statfs buf;
if (inode == NULL || inode->u.i_mops == NULL ||
inode->u.i_mops->statfs == NULL) {
return false;
}
if (inode->u.i_mops->statfs(inode, &buf) == OK) {
if (buf.f_type == ZPFS_MAGIC) {
return true;
}
}
return false;
}
#endif // LOSCFG_FS_ZPFS