• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
39 typedef LOS_DL_LIST LIST_HEAD;
40 typedef 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 
79 struct 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   */
95 enum 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 };
105 
106 struct fs_dirent_s;
107 struct VnodeOps;
108 struct IATTR;
109 
110 struct Vnode {
111     enum VnodeType type;                /* vnode type */
112     int useCount;                       /* ref count of users */
113     uint32_t hash;                      /* vnode hash */
114     uint uid;                           /* uid for dac */
115     uint gid;                           /* gid for dac */
116     mode_t mode;                        /* mode for dac */
117     LIST_HEAD parentPathCaches;         /* pathCaches point to parents */
118     LIST_HEAD childPathCaches;          /* pathCaches point to children */
119     struct Vnode *parent;               /* parent vnode */
120     struct VnodeOps *vop;               /* vnode operations */
121     struct file_operations_vfs *fop;    /* file operations */
122     void *data;                         /* private data */
123     uint32_t flag;                      /* vnode flag */
124     LIST_ENTRY hashEntry;               /* list entry for bucket in hash table */
125     LIST_ENTRY actFreeEntry;            /* vnode active/free list entry */
126     struct Mount *originMount;          /* fs info about this vnode */
127     struct Mount *newMount;             /* fs info about who mount on this vnode */
128     char *filePath;                     /* file path of the vnode */
129     struct page_mapping mapping;        /* page mapping of the vnode */
130 };
131 
132 struct VnodeOps {
133     int (*Create)(struct Vnode *parent, const char *name, int mode, struct Vnode **vnode);
134     int (*Lookup)(struct Vnode *parent, const char *name, int len, struct Vnode **vnode);
135     int (*Open)(struct Vnode *vnode, int fd, int mode, int flags);
136     ssize_t (*ReadPage)(struct Vnode *vnode, char *buffer, off_t pos);
137     ssize_t (*WritePage)(struct Vnode *vnode, char *buffer, off_t pos, size_t buflen);
138     int (*Close)(struct Vnode *vnode);
139     int (*Reclaim)(struct Vnode *vnode);
140     int (*Unlink)(struct Vnode *parent, struct Vnode *vnode, const char *fileName);
141     int (*Rmdir)(struct Vnode *parent, struct Vnode *vnode, const char *dirName);
142     int (*Mkdir)(struct Vnode *parent, const char *dirName, mode_t mode, struct Vnode **vnode);
143     int (*Readdir)(struct Vnode *vnode, struct fs_dirent_s *dir);
144     int (*Opendir)(struct Vnode *vnode, struct fs_dirent_s *dir);
145     int (*Rewinddir)(struct Vnode *vnode, struct fs_dirent_s *dir);
146     int (*Closedir)(struct Vnode *vnode, struct fs_dirent_s *dir);
147     int (*Getattr)(struct Vnode *vnode, struct stat *st);
148     int (*Setattr)(struct Vnode *vnode, struct stat *st);
149     int (*Chattr)(struct Vnode *vnode, struct IATTR *attr);
150     int (*Rename)(struct Vnode *src, struct Vnode *dstParent, const char *srcName, const char *dstName);
151     int (*Truncate)(struct Vnode *vnode, off_t len);
152     int (*Truncate64)(struct Vnode *vnode, off64_t len);
153     int (*Fscheck)(struct Vnode *vnode, struct fs_dirent_s *dir);
154     int (*Link)(struct Vnode *src, struct Vnode *dstParent, struct Vnode **dst, const char *dstName);
155     int (*Symlink)(struct Vnode *parentVnode, struct Vnode **newVnode, const char *path, const char *target);
156     ssize_t (*Readlink)(struct Vnode *vnode, char *buffer, size_t bufLen);
157 };
158 
159 typedef int VfsHashCmp(struct Vnode *vnode, void *arg);
160 
161 int VnodesInit(void);
162 int VnodeDevInit(void);
163 int VnodeAlloc(struct VnodeOps *vop, struct Vnode **newVnode);
164 int VnodeFree(struct Vnode *vnode);
165 int VnodeLookup(const char *path, struct Vnode **vnode, uint32_t flags);
166 int VnodeLookupFullpath(const char *fullpath, struct Vnode **vnode, uint32_t flags);
167 int VnodeLookupAt(const char *path, struct Vnode **vnode, uint32_t flags, struct Vnode *orgVnode);
168 int VnodeHold(void);
169 int VnodeDrop(void);
170 void VnodeRefDec(struct Vnode *vnode);
171 int VnodeFreeAll(const struct Mount *mnt);
172 int VnodeHashInit(void);
173 uint32_t VfsHashIndex(struct Vnode *vnode);
174 int VfsHashGet(const struct Mount *mount, uint32_t hash, struct Vnode **vnode, VfsHashCmp *fun, void *arg);
175 void VfsHashRemove(struct Vnode *vnode);
176 int VfsHashInsert(struct Vnode *vnode, uint32_t hash);
177 void ChangeRoot(struct Vnode *newRoot);
178 BOOL VnodeInUseIter(const struct Mount *mount);
179 struct Vnode *VnodeGetRoot(void);
180 void VnodeMemoryDump(void);
181 mode_t GetUmask(void);
182 int VfsPermissionCheck(uint fuid, uint fgid, mode_t fileMode, int accMode);
183 int VfsVnodePermissionCheck(const struct Vnode *node, int accMode);
184 LIST_HEAD* GetVnodeFreeList(void);
185 LIST_HEAD* GetVnodeActiveList(void);
186 LIST_HEAD* GetVnodeVirtualList(void);
187 int VnodeClearCache(void);
188 
189 #endif /* !_VNODE_H_ */
190