1 | /* |
2 | * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) |
3 | * |
4 | * |
5 | * HelenOS: |
6 | * Copyright (c) 2012 Martin Sucha |
7 | * Copyright (c) 2012 Frantisek Princ |
8 | * All rights reserved. |
9 | * |
10 | * Redistribution and use in source and binary forms, with or without |
11 | * modification, are permitted provided that the following conditions |
12 | * are met: |
13 | * |
14 | * - Redistributions of source code must retain the above copyright |
15 | * notice, this list of conditions and the following disclaimer. |
16 | * - Redistributions in binary form must reproduce the above copyright |
17 | * notice, this list of conditions and the following disclaimer in the |
18 | * documentation and/or other materials provided with the distribution. |
19 | * - The name of the author may not be used to endorse or promote products |
20 | * derived from this software without specific prior written permission. |
21 | * |
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
23 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
24 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
25 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
26 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
27 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
28 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
29 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
31 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | */ |
33 | |
34 | /** @addtogroup lwext4 |
35 | * @{ |
36 | */ |
37 | /** |
38 | * @file ext4_dir.h |
39 | * @brief Directory handle procedures. |
40 | */ |
41 | |
42 | #ifndef EXT4_DIR_H_ |
43 | #define EXT4_DIR_H_ |
44 | |
45 | #ifdef __cplusplus |
46 | extern "C" { |
47 | #endif |
48 | |
49 | #include <ext4_config.h> |
50 | #include <ext4_types.h> |
51 | #include <ext4_misc.h> |
52 | #include <ext4_blockdev.h> |
53 | #include <ext4_super.h> |
54 | |
55 | #include <stdint.h> |
56 | |
57 | struct ext4_dir_iter { |
58 | struct ext4_inode_ref *inode_ref; |
59 | struct ext4_block curr_blk; |
60 | uint64_t curr_off; |
61 | struct ext4_dir_en *curr; |
62 | }; |
63 | |
64 | struct ext4_dir_search_result { |
65 | struct ext4_block block; |
66 | struct ext4_dir_en *dentry; |
67 | }; |
68 | |
69 | |
70 | /**@brief Get i-node number from directory entry. |
71 | * @param de Directory entry |
72 | * @return I-node number |
73 | */ |
74 | static inline uint32_t |
75 | ext4_dir_en_get_inode(struct ext4_dir_en *de) |
76 | { |
77 | return to_le32(de->inode); |
78 | } |
79 | |
80 | /**@brief Set i-node number to directory entry. |
81 | * @param de Directory entry |
82 | * @param inode I-node number |
83 | */ |
84 | static inline void |
85 | ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode) |
86 | { |
87 | de->inode = to_le32(inode); |
88 | } |
89 | |
90 | /**@brief Set i-node number to directory entry. (For HTree root) |
91 | * @param de Directory entry |
92 | * @param inode I-node number |
93 | */ |
94 | static inline void |
95 | ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode) |
96 | { |
97 | de->inode = to_le32(inode); |
98 | } |
99 | |
100 | /**@brief Get directory entry length. |
101 | * @param de Directory entry |
102 | * @return Entry length |
103 | */ |
104 | static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de) |
105 | { |
106 | return to_le16(de->entry_len); |
107 | } |
108 | |
109 | /**@brief Set directory entry length. |
110 | * @param de Directory entry |
111 | * @param l Entry length |
112 | */ |
113 | static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l) |
114 | { |
115 | de->entry_len = to_le16(l); |
116 | } |
117 | |
118 | /**@brief Get directory entry name length. |
119 | * @param sb Superblock |
120 | * @param de Directory entry |
121 | * @return Entry name length |
122 | */ |
123 | static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb, |
124 | struct ext4_dir_en *de) |
125 | { |
126 | uint16_t v = de->name_len; |
127 | |
128 | if ((ext4_get32(sb, rev_level) == 0) && |
129 | (ext4_get32(sb, minor_rev_level) < 5)) |
130 | v |= ((uint16_t)de->in.name_length_high) << 8; |
131 | |
132 | return v; |
133 | } |
134 | |
135 | /**@brief Set directory entry name length. |
136 | * @param sb Superblock |
137 | * @param de Directory entry |
138 | * @param len Entry name length |
139 | */ |
140 | static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb, |
141 | struct ext4_dir_en *de, |
142 | uint16_t len) |
143 | { |
144 | de->name_len = (len << 8) >> 8; |
145 | |
146 | if ((ext4_get32(sb, rev_level) == 0) && |
147 | (ext4_get32(sb, minor_rev_level) < 5)) |
148 | de->in.name_length_high = len >> 8; |
149 | } |
150 | |
151 | /**@brief Get i-node type of directory entry. |
152 | * @param sb Superblock |
153 | * @param de Directory entry |
154 | * @return I-node type (file, dir, etc.) |
155 | */ |
156 | static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb, |
157 | struct ext4_dir_en *de) |
158 | { |
159 | if ((ext4_get32(sb, rev_level) > 0) || |
160 | (ext4_get32(sb, minor_rev_level) >= 5)) |
161 | return de->in.inode_type; |
162 | |
163 | return EXT4_DE_UNKNOWN; |
164 | } |
165 | /**@brief Set i-node type of directory entry. |
166 | * @param sb Superblock |
167 | * @param de Directory entry |
168 | * @param t I-node type (file, dir, etc.) |
169 | */ |
170 | |
171 | static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb, |
172 | struct ext4_dir_en *de, uint8_t t) |
173 | { |
174 | if ((ext4_get32(sb, rev_level) > 0) || |
175 | (ext4_get32(sb, minor_rev_level) >= 5)) |
176 | de->in.inode_type = t; |
177 | } |
178 | |
179 | /**@brief Verify checksum of a linear directory leaf block |
180 | * @param inode_ref Directory i-node |
181 | * @param dirent Linear directory leaf block |
182 | * @return true means the block passed checksum verification |
183 | */ |
184 | bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref, |
185 | struct ext4_dir_en *dirent); |
186 | |
187 | /**@brief Initialize directory iterator. |
188 | * Set position to the first valid entry from the required position. |
189 | * @param it Pointer to iterator to be initialized |
190 | * @param inode_ref Directory i-node |
191 | * @param pos Position to start reading entries from |
192 | * @return Error code |
193 | */ |
194 | int ext4_dir_iterator_init(struct ext4_dir_iter *it, |
195 | struct ext4_inode_ref *inode_ref, uint64_t pos); |
196 | |
197 | /**@brief Jump to the next valid entry |
198 | * @param it Initialized iterator |
199 | * @return Error code |
200 | */ |
201 | int ext4_dir_iterator_next(struct ext4_dir_iter *it); |
202 | |
203 | /**@brief Uninitialize directory iterator. |
204 | * Release all allocated structures. |
205 | * @param it Iterator to be finished |
206 | * @return Error code |
207 | */ |
208 | int ext4_dir_iterator_fini(struct ext4_dir_iter *it); |
209 | |
210 | /**@brief Write directory entry to concrete data block. |
211 | * @param sb Superblock |
212 | * @param en Pointer to entry to be written |
213 | * @param entry_len Length of new entry |
214 | * @param child Child i-node to be written to new entry |
215 | * @param name Name of the new entry |
216 | * @param name_len Length of entry name |
217 | */ |
218 | void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en, |
219 | uint16_t entry_len, struct ext4_inode_ref *child, |
220 | const char *name, size_t name_len); |
221 | |
222 | /**@brief Add new entry to the directory. |
223 | * @param parent Directory i-node |
224 | * @param name Name of new entry |
225 | * @param child I-node to be referenced from new entry |
226 | * @return Error code |
227 | */ |
228 | int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name, |
229 | uint32_t name_len, struct ext4_inode_ref *child); |
230 | |
231 | /**@brief Find directory entry with passed name. |
232 | * @param result Result structure to be returned if entry found |
233 | * @param parent Directory i-node |
234 | * @param name Name of entry to be found |
235 | * @param name_len Name length |
236 | * @return Error code |
237 | */ |
238 | int ext4_dir_find_entry(struct ext4_dir_search_result *result, |
239 | struct ext4_inode_ref *parent, const char *name, |
240 | uint32_t name_len); |
241 | |
242 | /**@brief Remove directory entry. |
243 | * @param parent Directory i-node |
244 | * @param name Name of the entry to be removed |
245 | * @param name_len Name length |
246 | * @return Error code |
247 | */ |
248 | int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name, |
249 | uint32_t name_len); |
250 | |
251 | /**@brief Try to insert entry to concrete data block. |
252 | * @param sb Superblock |
253 | * @param inode_ref Directory i-node |
254 | * @param dst_blk Block to try to insert entry to |
255 | * @param child Child i-node to be inserted by new entry |
256 | * @param name Name of the new entry |
257 | * @param name_len Length of the new entry name |
258 | * @return Error code |
259 | */ |
260 | int ext4_dir_try_insert_entry(struct ext4_sblock *sb, |
261 | struct ext4_inode_ref *inode_ref, |
262 | struct ext4_block *dst_blk, |
263 | struct ext4_inode_ref *child, const char *name, |
264 | uint32_t name_len); |
265 | |
266 | /**@brief Try to find entry in block by name. |
267 | * @param block Block containing entries |
268 | * @param sb Superblock |
269 | * @param name_len Length of entry name |
270 | * @param name Name of entry to be found |
271 | * @param res_entry Output pointer to found entry, NULL if not found |
272 | * @return Error code |
273 | */ |
274 | int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb, |
275 | size_t name_len, const char *name, |
276 | struct ext4_dir_en **res_entry); |
277 | |
278 | /**@brief Simple function to release allocated data from result. |
279 | * @param parent Parent inode |
280 | * @param result Search result to destroy |
281 | * @return Error code |
282 | * |
283 | */ |
284 | int ext4_dir_destroy_result(struct ext4_inode_ref *parent, |
285 | struct ext4_dir_search_result *result); |
286 | |
287 | void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref, |
288 | struct ext4_dir_en *dirent); |
289 | |
290 | |
291 | void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t); |
292 | |
293 | #ifdef __cplusplus |
294 | } |
295 | #endif |
296 | |
297 | #endif /* EXT4_DIR_H_ */ |
298 | |
299 | /** |
300 | * @} |
301 | */ |
302 | |