ext4_dir_dx_reset_parent_inode proposed.
[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_directory_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_directory_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_directory_dx_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_directory_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_directory_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_directory_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_directory_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_directory_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_DIRECTORY_FILETYPE_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_directory_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 Initialize directory iterator.
166  * Set position to the first valid entry from the required position.
167  * @param it        Pointer to iterator to be initialized
168  * @param inode_ref Directory i-node
169  * @param pos       Position to start reading entries from
170  * @return Error code
171  */
172 int ext4_dir_iterator_init(struct ext4_directory_iterator *it,
173                            struct ext4_inode_ref *inode_ref, uint64_t pos);
174
175 /**@brief Jump to the next valid entry
176  * @param it Initialized iterator
177  * @return Error code
178  */
179 int ext4_dir_iterator_next(struct ext4_directory_iterator *it);
180
181 /**@brief Uninitialize directory iterator.
182  *        Release all allocated structures.
183  * @param it Iterator to be finished
184  * @return Error code
185  */
186 int ext4_dir_iterator_fini(struct ext4_directory_iterator *it);
187
188 /**@brief Write directory entry to concrete data block.
189  * @param sb        Superblock
190  * @param entry     Pointer to entry to be written
191  * @param entry_len Length of new entry
192  * @param child     Child i-node to be written to new entry
193  * @param name      Name of the new entry
194  * @param name_len  Length of entry name
195  */
196 void ext4_dir_write_entry(struct ext4_sblock *sb,
197                           struct ext4_directory_entry_ll *entry,
198                           uint16_t entry_len, struct ext4_inode_ref *child,
199                           const char *name, size_t name_len);
200
201 /**@brief Add new entry to the directory.
202  * @param parent Directory i-node
203  * @param name   Name of new entry
204  * @param child  I-node to be referenced from new entry
205  * @return Error code
206  */
207 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
208                        uint32_t name_len, struct ext4_inode_ref *child);
209
210 /**@brief Find directory entry with passed name.
211  * @param result Result structure to be returned if entry found
212  * @param parent Directory i-node
213  * @param name   Name of entry to be found
214  * @param name_len  Name length
215  * @return Error code
216  */
217 int ext4_dir_find_entry(struct ext4_directory_search_result *result,
218                         struct ext4_inode_ref *parent, const char *name,
219                         uint32_t name_len);
220
221 /**@brief Remove directory entry.
222  * @param parent Directory i-node
223  * @param name   Name of the entry to be removed
224  * @param name_len  Name length
225  * @return Error code
226  */
227 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
228                           uint32_t name_len);
229
230 /**@brief Try to insert entry to concrete data block.
231  * @param sb           Superblock
232  * @param target_block Block to try to insert entry to
233  * @param child        Child i-node to be inserted by new entry
234  * @param name         Name of the new entry
235  * @param name_len     Length of the new entry name
236  * @return Error code
237  */
238 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
239                               struct ext4_block *target_block,
240                               struct ext4_inode_ref *child, const char *name,
241                               uint32_t name_len);
242
243 /**@brief Try to find entry in block by name.
244  * @param block     Block containing entries
245  * @param sb        Superblock
246  * @param name_len  Length of entry name
247  * @param name      Name of entry to be found
248  * @param res_entry Output pointer to found entry, NULL if not found
249  * @return Error code
250  */
251 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
252                            size_t name_len, const char *name,
253                            struct ext4_directory_entry_ll **res_entry);
254
255 /**@brief Simple function to release allocated data from result.
256  * @param parent Parent inode
257  * @param result Search result to destroy
258  * @return Error code
259  *
260  */
261 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
262                             struct ext4_directory_search_result *result);
263
264 #endif /* EXT4_DIR_H_ */
265
266 /**
267  * @}
268  */