1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #ifndef VFS_INODE_H
6 #define VFS_INODE_H
7 
8 #ifdef __cplusplus
9 extern "C" {
10 #endif
11 
12 #include "vfs_types.h"
13 
14 enum {
15     VFS_TYPE_NOT_INIT,
16     VFS_TYPE_CHAR_DEV,
17     VFS_TYPE_BLOCK_DEV,
18     VFS_TYPE_FS_DEV
19 };
20 
21 #define INODE_IS_TYPE(node, t) ((node)->type == (t))
22 
23 #define INODE_IS_CHAR(node)  INODE_IS_TYPE(node, VFS_TYPE_CHAR_DEV)
24 #define INODE_IS_BLOCK(node) INODE_IS_TYPE(node, VFS_TYPE_BLOCK_DEV)
25 #define INODE_IS_FS(node)    INODE_IS_TYPE(node, VFS_TYPE_FS_DEV)
26 
27 #define INODE_GET_TYPE(node) ((node)->type)
28 #define INODE_SET_TYPE(node, t) do { (node)->type = (t); } while(0)
29 
30 #define INODE_SET_CHAR(node)  INODE_SET_TYPE(node, VFS_TYPE_CHAR_DEV)
31 #define INODE_SET_BLOCK(node) INODE_SET_TYPE(node, VFS_TYPE_BLOCK_DEV)
32 #define INODE_SET_FS(node)    INODE_SET_TYPE(node, VFS_TYPE_FS_DEV)
33 
34 /**
35  * @brief Initialize inode
36  *
37  * @return 0 on success, negative error on failure
38  *
39  */
40 int32_t vfs_inode_init(void);
41 
42 /**
43  * @brief Alloc a free inode
44  *
45  * @return the index of inode, VFS_ERR_NOMEM on failure
46  *
47  */
48 int32_t vfs_inode_alloc(void);
49 
50 /**
51  * @brief Delete a inode
52  *
53  * @param[in] node pointer to the inode to delete
54  *
55  * @return 0 on success, negative error on failure
56  *
57  */
58 int32_t vfs_inode_del(vfs_inode_t *node);
59 
60 /**
61  * @brief Open the inode by path
62  *
63  * @param[in] path the path of the inode reference
64  *
65  * @return the pointer of the inode, NULL on failure or not found
66  *
67  */
68 vfs_inode_t *vfs_inode_open(const char *path);
69 
70 /**
71  * @brief Get the inode pointer by fd
72  *
73  * @param[in]  fd     the file descriptor
74  * @param[out] p_node the pointer of the inode pointer
75  *
76  * @return 0 on success, negative error on failure
77  *
78  */
79 int32_t vfs_inode_ptr_get(int32_t fd, vfs_inode_t **p_node);
80 
81 /**
82  * @brief Get the available inode count
83  *
84  * @return the count of the available inodes
85  *
86  */
87 int32_t vfs_inode_avail_count(void);
88 
89 /**
90  * @brief Add the inode refence count
91  *
92  * @param[in] node the pointer of the inode
93  *
94  * @return none
95  *
96  */
97 void vfs_inode_ref(vfs_inode_t *node);
98 
99 /**
100  * @brief Dec the inode refence count
101  *
102  * @param[in] node the pointer of the inode
103  *
104  * @return none
105  *
106  */
107 void vfs_inode_unref(vfs_inode_t *node);
108 
109 /**
110  * @brief Check whether the inode is busy or not
111  *
112  * @param[in] node the pointer of the inode
113  *
114  * @return 1 on busy, 0 on free
115  *
116  */
117 int32_t vfs_inode_busy(vfs_inode_t *node);
118 
119 /**
120  * @brief Reserve an inode
121  *
122  * @param[in] path   the path of the inode reference
123  * @param[in] p_node pointer to the inode pointer
124  *
125  * @return 0 on success, negative error on failure
126  *
127  */
128 int32_t vfs_inode_reserve(const char *path, vfs_inode_t **p_node);
129 
130 /**
131  * @brief Release an inode
132  *
133  * @param[in] path the path of the inode reference
134  *
135  * @return 0 on success, negative error on failure
136  *
137  */
138 int32_t vfs_inode_release(const char *path);
139 
140 /**
141  * @brief list all inode with the type
142  *
143  * @param[in] type the type of inode
144  *
145  * @return 0 on success, negative error on failure
146  *
147  */
148 int32_t vfs_inode_list(vfs_list_type_t type);
149 
150 /**
151  * @brief get FS nodes name
152  *
153  * @param[in] path the parent path of inodes
154  *
155  * @param[out] names FS nodes name as request
156  *
157  * @param[out] size  names count
158  *
159  * @return 0 on success, negative error on failure
160  *
161  */
162 int vfs_inode_get_names(const char *path, char names[][64], uint32_t* size);
163 
164 /**
165  * @brief only used by FS node to mark deatched state, so it can
166  *        umount itself after it ceases to be busy.
167  */
168 int32_t vfs_inode_detach(vfs_inode_t *node);
169 
170 #define inode_init vfs_inode_init
171 #define inode_alloc vfs_inode_alloc
172 #define inode_del vfs_inode_del
173 #define inode_open vfs_inode_open
174 #define inode_ptr_get vfs_inode_ptr_get
175 #define inode_avail_count vfs_inode_avail_count
176 
177 #ifdef __cplusplus
178 }
179 #endif
180 
181 #endif /* VFS_INODE_H */
182