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