Refactor header files dependencies.
[lwext4.git] / include / ext4_dir.h
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 /**@brief Get i-node number from directory entry.
58  * @param de Directory entry
59  * @return I-node number
60  */
61 static inline uint32_t
62 ext4_dir_en_get_inode(struct ext4_dir_en *de)
63 {
64         return to_le32(de->inode);
65 }
66
67 /**@brief Set i-node number to directory entry.
68  * @param de Directory entry
69  * @param inode I-node number
70  */
71 static inline void
72 ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode)
73 {
74         de->inode = to_le32(inode);
75 }
76
77 /**@brief Set i-node number to directory entry. (For HTree root)
78  * @param de Directory entry
79  * @param inode I-node number
80  */
81 static inline void
82 ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode)
83 {
84         de->inode = to_le32(inode);
85 }
86
87 /**@brief Get directory entry length.
88  * @param de Directory entry
89  * @return Entry length
90  */
91 static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de)
92 {
93         return to_le16(de->entry_len);
94 }
95
96 /**@brief Set directory entry length.
97  * @param de     Directory entry
98  * @param length Entry length
99  */
100 static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l)
101 {
102         de->entry_len = to_le16(l);
103 }
104
105 /**@brief Get directory entry name length.
106  * @param sb Superblock
107  * @param de Directory entry
108  * @return Entry name length
109  */
110 static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb,
111                                                 struct ext4_dir_en *de)
112 {
113         uint16_t v = de->name_len;
114
115         if ((ext4_get32(sb, rev_level) == 0) &&
116             (ext4_get32(sb, minor_rev_level) < 5))
117                 v |= ((uint16_t)de->in.name_length_high) << 8;
118
119         return v;
120 }
121
122 /**@brief Set directory entry name length.
123  * @param sb     Superblock
124  * @param de     Directory entry
125  * @param length Entry name length
126  */
127 static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb,
128                                             struct ext4_dir_en *de,
129                                             uint16_t len)
130 {
131         de->name_len = (len << 8) >> 8;
132
133         if ((ext4_get32(sb, rev_level) == 0) &&
134             (ext4_get32(sb, minor_rev_level) < 5))
135                 de->in.name_length_high = len >> 8;
136 }
137
138 /**@brief Get i-node type of directory entry.
139  * @param sb Superblock
140  * @param de Directory entry
141  * @return I-node type (file, dir, etc.)
142  */
143 static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb,
144                                                  struct ext4_dir_en *de)
145 {
146         if ((ext4_get32(sb, rev_level) > 0) ||
147             (ext4_get32(sb, minor_rev_level) >= 5))
148                 return de->in.inode_type;
149
150         return EXT4_DE_UNKNOWN;
151 }
152 /**@brief Set i-node type of directory entry.
153  * @param sb   Superblock
154  * @param de   Directory entry
155  * @param type I-node type (file, dir, etc.)
156  */
157
158 static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb,
159                                               struct ext4_dir_en *de, uint8_t t)
160 {
161         if ((ext4_get32(sb, rev_level) > 0) ||
162             (ext4_get32(sb, minor_rev_level) >= 5))
163                 de->in.inode_type = t;
164 }
165
166 /**@brief Verify checksum of a linear directory leaf block
167  * @param inode_ref Directory i-node
168  * @param dirent    Linear directory leaf block
169  * @return true means the block passed checksum verification
170  */
171 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
172                           struct ext4_dir_en *dirent);
173
174 /**@brief Initialize directory iterator.
175  * Set position to the first valid entry from the required position.
176  * @param it        Pointer to iterator to be initialized
177  * @param inode_ref Directory i-node
178  * @param pos       Position to start reading entries from
179  * @return Error code
180  */
181 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
182                            struct ext4_inode_ref *inode_ref, uint64_t pos);
183
184 /**@brief Jump to the next valid entry
185  * @param it Initialized iterator
186  * @return Error code
187  */
188 int ext4_dir_iterator_next(struct ext4_dir_iter *it);
189
190 /**@brief Uninitialize directory iterator.
191  *        Release all allocated structures.
192  * @param it Iterator to be finished
193  * @return Error code
194  */
195 int ext4_dir_iterator_fini(struct ext4_dir_iter *it);
196
197 /**@brief Write directory entry to concrete data block.
198  * @param sb        Superblock
199  * @param en     Pointer to entry to be written
200  * @param entry_len Length of new entry
201  * @param child     Child i-node to be written to new entry
202  * @param name      Name of the new entry
203  * @param name_len  Length of entry name
204  */
205 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
206                           uint16_t entry_len, struct ext4_inode_ref *child,
207                           const char *name, size_t name_len);
208
209 /**@brief Add new entry to the directory.
210  * @param parent Directory i-node
211  * @param name   Name of new entry
212  * @param child  I-node to be referenced from new entry
213  * @return Error code
214  */
215 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
216                        uint32_t name_len, struct ext4_inode_ref *child);
217
218 /**@brief Find directory entry with passed name.
219  * @param result Result structure to be returned if entry found
220  * @param parent Directory i-node
221  * @param name   Name of entry to be found
222  * @param name_len  Name length
223  * @return Error code
224  */
225 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
226                         struct ext4_inode_ref *parent, const char *name,
227                         uint32_t name_len);
228
229 /**@brief Remove directory entry.
230  * @param parent Directory i-node
231  * @param name   Name of the entry to be removed
232  * @param name_len  Name length
233  * @return Error code
234  */
235 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
236                           uint32_t name_len);
237
238 /**@brief Try to insert entry to concrete data block.
239  * @param sb           Superblock
240  * @param inode_ref    Directory i-node
241  * @param dst_blk      Block to try to insert entry to
242  * @param child        Child i-node to be inserted by new entry
243  * @param name         Name of the new entry
244  * @param name_len     Length of the new entry name
245  * @return Error code
246  */
247 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
248                               struct ext4_inode_ref *inode_ref,
249                               struct ext4_block *dst_blk,
250                               struct ext4_inode_ref *child, const char *name,
251                               uint32_t name_len);
252
253 /**@brief Try to find entry in block by name.
254  * @param block     Block containing entries
255  * @param sb        Superblock
256  * @param name_len  Length of entry name
257  * @param name      Name of entry to be found
258  * @param res_entry Output pointer to found entry, NULL if not found
259  * @return Error code
260  */
261 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
262                            size_t name_len, const char *name,
263                            struct ext4_dir_en **res_entry);
264
265 /**@brief Simple function to release allocated data from result.
266  * @param parent Parent inode
267  * @param result Search result to destroy
268  * @return Error code
269  *
270  */
271 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
272                             struct ext4_dir_search_result *result);
273
274 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
275                        struct ext4_dir_en *dirent);
276
277
278 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
279
280 #ifdef __cplusplus
281 }
282 #endif
283
284 #endif /* EXT4_DIR_H_ */
285
286 /**
287  * @}
288  */