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