1/* 2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without modification, 5 * are permitted provided that the following conditions are met: 6 * 7 * 1. Redistributions of source code must retain the above copyright notice, this list of 8 * conditions and the following disclaimer. 9 * 10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list 11 * of conditions and the following disclaimer in the documentation and/or other materials 12 * provided with the distribution. 13 * 14 * 3. Neither the name of the copyright holder nor the names of its contributors may be used 15 * to endorse or promote products derived from this software without specific prior written 16 * permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 22 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 23 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 24 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 25 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 27 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#ifndef _VNODE_H_ 32#define _VNODE_H_ 33 34#include <sys/stat.h> 35#include "fs/fs_operation.h" 36#include "fs/file.h" 37#include "los_list.h" 38 39typedef LOS_DL_LIST LIST_HEAD; 40typedef LOS_DL_LIST LIST_ENTRY; 41 42#define VNODE_FLAG_MOUNT_NEW (1 << 0) /* new mount vnode */ 43#define VNODE_FLAG_MOUNT_ORIGIN (1 << 1) /* origin vnode */ 44 45#define V_CREATE (1 << 0) 46#define V_DUMMY (1 << 2) 47 48#ifndef VFS_ERROR 49#define VFS_ERROR -1 50#endif 51 52#ifndef OK 53#define OK 0 54#endif 55 56#define AT_REMOVEDIR 0x200 57 58#define DEV_PATH_LEN 5 59 60/* Permission flags */ 61#define READ_OP 4 62#define WRITE_OP 2 63#define EXEC_OP 1 64#define UGO_NUMS 3 65#define MODE_IXUGO 0111 66#define USER_MODE_SHIFT 6 67#define GROUP_MODE_SHIFT 3 68#define UMASK_FULL 0777 69 70/* Attribute flags. */ 71#define CHG_MODE 1 72#define CHG_UID 2 73#define CHG_GID 4 74#define CHG_SIZE 8 75#define CHG_ATIME 16 76#define CHG_MTIME 32 77#define CHG_CTIME 64 78 79struct IATTR { 80 /* This structure is used for record vnode attr. */ 81 unsigned int attr_chg_valid; 82 unsigned int attr_chg_flags; 83 unsigned attr_chg_mode; 84 unsigned attr_chg_uid; 85 unsigned attr_chg_gid; 86 unsigned attr_chg_size; 87 unsigned attr_chg_atime; 88 unsigned attr_chg_mtime; 89 unsigned attr_chg_ctime; 90}; 91 92 /* 93 * Vnode types. VNODE_TYPE_UNKNOWN means no type. 94 */ 95enum VnodeType { 96 VNODE_TYPE_UNKNOWN, /* unknown type */ 97 VNODE_TYPE_REG, /* regular fle */ 98 VNODE_TYPE_DIR, /* directory */ 99 VNODE_TYPE_BLK, /* block device */ 100 VNODE_TYPE_CHR, /* char device */ 101 VNODE_TYPE_BCHR, /* block char mix device */ 102 VNODE_TYPE_FIFO, /* pipe */ 103 VNODE_TYPE_LNK, /* link */ 104#ifdef LOSCFG_PROC_PROCESS_DIR 105 VNODE_TYPE_VIR_LNK, /* virtual link */ 106#endif 107}; 108 109struct fs_dirent_s; 110struct VnodeOps; 111struct IATTR; 112 113struct Vnode { 114 enum VnodeType type; /* vnode type */ 115 int useCount; /* ref count of users */ 116 uint32_t hash; /* vnode hash */ 117 uint uid; /* uid for dac */ 118 uint gid; /* gid for dac */ 119 mode_t mode; /* mode for dac */ 120 LIST_HEAD parentPathCaches; /* pathCaches point to parents */ 121 LIST_HEAD childPathCaches; /* pathCaches point to children */ 122 struct Vnode *parent; /* parent vnode */ 123 struct VnodeOps *vop; /* vnode operations */ 124 struct file_operations_vfs *fop; /* file operations */ 125 void *data; /* private data */ 126 uint32_t flag; /* vnode flag */ 127 LIST_ENTRY hashEntry; /* list entry for bucket in hash table */ 128 LIST_ENTRY actFreeEntry; /* vnode active/free list entry */ 129 struct Mount *originMount; /* fs info about this vnode */ 130 struct Mount *newMount; /* fs info about who mount on this vnode */ 131 char *filePath; /* file path of the vnode */ 132 struct page_mapping mapping; /* page mapping of the vnode */ 133#ifdef LOSCFG_MNT_CONTAINER 134 int mntCount; /* ref count of mounts */ 135#endif 136}; 137 138struct VnodeOps { 139 int (*Create)(struct Vnode *parent, const char *name, int mode, struct Vnode **vnode); 140 int (*Lookup)(struct Vnode *parent, const char *name, int len, struct Vnode **vnode); 141 int (*Open)(struct Vnode *vnode, int fd, int mode, int flags); 142 ssize_t (*ReadPage)(struct Vnode *vnode, char *buffer, off_t pos); 143 ssize_t (*WritePage)(struct Vnode *vnode, char *buffer, off_t pos, size_t buflen); 144 int (*Close)(struct Vnode *vnode); 145 int (*Reclaim)(struct Vnode *vnode); 146 int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, const char *fileName); 147 int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, const char *dirName); 148 int (*Mkdir)(struct Vnode *parent, const char *dirName, mode_t mode, struct Vnode **vnode); 149 int (*Readdir)(struct Vnode *vnode, struct fs_dirent_s *dir); 150 int (*Opendir)(struct Vnode *vnode, struct fs_dirent_s *dir); 151 int (*Rewinddir)(struct Vnode *vnode, struct fs_dirent_s *dir); 152 int (*Closedir)(struct Vnode *vnode, struct fs_dirent_s *dir); 153 int (*Getattr)(struct Vnode *vnode, struct stat *st); 154 int (*Setattr)(struct Vnode *vnode, struct stat *st); 155 int (*Chattr)(struct Vnode *vnode, struct IATTR *attr); 156 int (*Rename)(struct Vnode *src, struct Vnode *dstParent, const char *srcName, const char *dstName); 157 int (*Truncate)(struct Vnode *vnode, off_t len); 158 int (*Truncate64)(struct Vnode *vnode, off64_t len); 159 int (*Fscheck)(struct Vnode *vnode, struct fs_dirent_s *dir); 160 int (*Link)(struct Vnode *src, struct Vnode *dstParent, struct Vnode **dst, const char *dstName); 161 int (*Symlink)(struct Vnode *parentVnode, struct Vnode **newVnode, const char *path, const char *target); 162 ssize_t (*Readlink)(struct Vnode *vnode, char *buffer, size_t bufLen); 163}; 164 165typedef int VfsHashCmp(struct Vnode *vnode, void *arg); 166 167int VnodesInit(void); 168int VnodeDevInit(void); 169int VnodeAlloc(struct VnodeOps *vop, struct Vnode **newVnode); 170int VnodeFree(struct Vnode *vnode); 171int VnodeLookup(const char *path, struct Vnode **vnode, uint32_t flags); 172int VnodeLookupFullpath(const char *fullpath, struct Vnode **vnode, uint32_t flags); 173int VnodeLookupAt(const char *path, struct Vnode **vnode, uint32_t flags, struct Vnode *orgVnode); 174int VnodeHold(void); 175int VnodeDrop(void); 176void VnodeRefDec(struct Vnode *vnode); 177int VnodeFreeAll(const struct Mount *mnt); 178int VnodeHashInit(void); 179uint32_t VfsHashIndex(struct Vnode *vnode); 180int VfsHashGet(const struct Mount *mount, uint32_t hash, struct Vnode **vnode, VfsHashCmp *fun, void *arg); 181void VfsHashRemove(struct Vnode *vnode); 182int VfsHashInsert(struct Vnode *vnode, uint32_t hash); 183void ChangeRoot(struct Vnode *newRoot); 184BOOL VnodeInUseIter(const struct Mount *mount); 185struct Vnode *VnodeGetRoot(void); 186void VnodeMemoryDump(void); 187mode_t GetUmask(void); 188int VfsPermissionCheck(uint fuid, uint fgid, mode_t fileMode, int accMode); 189int VfsVnodePermissionCheck(const struct Vnode *node, int accMode); 190LIST_HEAD* GetVnodeFreeList(void); 191LIST_HEAD* GetVnodeActiveList(void); 192LIST_HEAD* GetVnodeVirtualList(void); 193int VnodeClearCache(void); 194struct Vnode *GetCurrRootVnode(void); 195#ifdef LOSCFG_PROC_PROCESS_DIR 196struct Vnode *VnodeFind(int fd); 197#endif 198#endif /* !_VNODE_H_ */ 199