Add to mkfs configurable descriptor size & hash seed init
[lwext4.git] / lwext4 / 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 #include "ext4_config.h"
46 #include "ext4_types.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_super.h"
49
50 #include <stdint.h>
51
52 /**@brief Get i-node number from directory entry.
53  * @param de Directory entry
54  * @return I-node number
55  */
56 static inline uint32_t
57 ext4_dir_entry_ll_get_inode(struct ext4_dir_entry_ll *de)
58 {
59         return to_le32(de->inode);
60 }
61
62 /**@brief Set i-node number to directory entry.
63  * @param de Directory entry
64  * @param inode I-node number
65  */
66 static inline void
67 ext4_dir_entry_ll_set_inode(struct ext4_dir_entry_ll *de, uint32_t inode)
68 {
69         de->inode = to_le32(inode);
70 }
71
72 /**@brief Set i-node number to directory entry. (For HTree root)
73  * @param de Directory entry
74  * @param inode I-node number
75  */
76 static inline void
77 ext4_dx_dot_entry_set_inode(struct ext4_dir_idx_dot_entry *de, uint32_t inode)
78 {
79         de->inode = to_le32(inode);
80 }
81
82 /**@brief Get directory entry length.
83  * @param de Directory entry
84  * @return Entry length
85  */
86 static inline uint16_t
87 ext4_dir_entry_ll_get_entry_length(struct ext4_dir_entry_ll *de)
88 {
89         return to_le16(de->entry_length);
90 }
91
92 /**@brief Set directory entry length.
93  * @param de     Directory entry
94  * @param length Entry length
95  */
96 static inline void
97 ext4_dir_entry_ll_set_entry_length(struct ext4_dir_entry_ll *de,
98                                    uint16_t len)
99 {
100         de->entry_length = to_le16(len);
101 }
102
103 /**@brief Get directory entry name length.
104  * @param sb Superblock
105  * @param de Directory entry
106  * @return Entry name length
107  */
108 static inline uint16_t
109 ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
110                                   struct ext4_dir_entry_ll *de)
111 {
112         uint16_t v = de->name_length;
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_entry_ll_set_name_length(
127     struct ext4_sblock *sb, struct ext4_dir_entry_ll *de, uint16_t len)
128 {
129         de->name_length = (len << 8) >> 8;
130
131         if ((ext4_get32(sb, rev_level) == 0) &&
132             (ext4_get32(sb, minor_rev_level) < 5))
133                 de->in.name_length_high = len >> 8;
134 }
135
136 /**@brief Get i-node type of directory entry.
137  * @param sb Superblock
138  * @param de Directory entry
139  * @return I-node type (file, dir, etc.)
140  */
141 static inline uint8_t
142 ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
143                                  struct ext4_dir_entry_ll *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_DIRENTRY_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_entry_ll_set_inode_type(
158     struct ext4_sblock *sb, struct ext4_dir_entry_ll *de, uint8_t type)
159 {
160         if ((ext4_get32(sb, rev_level) > 0) ||
161             (ext4_get32(sb, minor_rev_level) >= 5))
162                 de->in.inode_type = type;
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
171 ext4_dir_checksum_verify(struct ext4_inode_ref *inode_ref,
172                          struct ext4_dir_entry_ll *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_iterator *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_iterator *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_iterator *it);
196
197 /**@brief Write directory entry to concrete data block.
198  * @param sb        Superblock
199  * @param entry     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,
206                           struct ext4_dir_entry_ll *entry,
207                           uint16_t entry_len, struct ext4_inode_ref *child,
208                           const char *name, size_t name_len);
209
210 /**@brief Add new entry to the directory.
211  * @param parent Directory i-node
212  * @param name   Name of new entry
213  * @param child  I-node to be referenced from new entry
214  * @return Error code
215  */
216 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
217                        uint32_t name_len, struct ext4_inode_ref *child);
218
219 /**@brief Find directory entry with passed name.
220  * @param result Result structure to be returned if entry found
221  * @param parent Directory i-node
222  * @param name   Name of entry to be found
223  * @param name_len  Name length
224  * @return Error code
225  */
226 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
227                         struct ext4_inode_ref *parent, const char *name,
228                         uint32_t name_len);
229
230 /**@brief Remove directory entry.
231  * @param parent Directory i-node
232  * @param name   Name of the entry to be removed
233  * @param name_len  Name length
234  * @return Error code
235  */
236 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
237                           uint32_t name_len);
238
239 /**@brief Try to insert entry to concrete data block.
240  * @param sb           Superblock
241  * @param inode_ref     Directory i-node
242  * @param target_block Block to try to insert entry to
243  * @param child        Child i-node to be inserted by new entry
244  * @param name         Name of the new entry
245  * @param name_len     Length of the new entry name
246  * @return Error code
247  */
248 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
249                               struct ext4_inode_ref *inode_ref,
250                               struct ext4_block *target_block,
251                               struct ext4_inode_ref *child, const char *name,
252                               uint32_t name_len);
253
254 /**@brief Try to find entry in block by name.
255  * @param block     Block containing entries
256  * @param sb        Superblock
257  * @param name_len  Length of entry name
258  * @param name      Name of entry to be found
259  * @param res_entry Output pointer to found entry, NULL if not found
260  * @return Error code
261  */
262 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
263                            size_t name_len, const char *name,
264                            struct ext4_dir_entry_ll **res_entry);
265
266 /**@brief Simple function to release allocated data from result.
267  * @param parent Parent inode
268  * @param result Search result to destroy
269  * @return Error code
270  *
271  */
272 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
273                             struct ext4_dir_search_result *result);
274
275 void ext4_dir_set_checksum(struct ext4_inode_ref *inode_ref,
276                            struct ext4_dir_entry_ll *dirent);
277
278 /* checksumming functions */
279 void initialize_dir_tail(struct ext4_dir_entry_tail *t);
280
281 #endif /* EXT4_DIR_H_ */
282
283 /**
284  * @}
285  */