2 * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
6 * Copyright (c) 2012 Martin Sucha
7 * Copyright (c) 2012 Frantisek Princ
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
34 /** @addtogroup lwext4
39 * @brief Directory handle procedures.
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"
57 struct ext4_dir_iter {
58 struct ext4_inode_ref *inode_ref;
59 struct ext4_block curr_blk;
61 struct ext4_dir_en *curr;
64 struct ext4_dir_search_result {
65 struct ext4_block block;
66 struct ext4_dir_en *dentry;
70 /**@brief Get i-node number from directory entry.
71 * @param de Directory entry
72 * @return I-node number
74 static inline uint32_t
75 ext4_dir_en_get_inode(struct ext4_dir_en *de)
77 return to_le32(de->inode);
80 /**@brief Set i-node number to directory entry.
81 * @param de Directory entry
82 * @param inode I-node number
85 ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode)
87 de->inode = to_le32(inode);
90 /**@brief Set i-node number to directory entry. (For HTree root)
91 * @param de Directory entry
92 * @param inode I-node number
95 ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode)
97 de->inode = to_le32(inode);
100 /**@brief Get directory entry length.
101 * @param de Directory entry
102 * @return Entry length
104 static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de)
106 return to_le16(de->entry_len);
109 /**@brief Set directory entry length.
110 * @param de Directory entry
111 * @param length Entry length
113 static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l)
115 de->entry_len = to_le16(l);
118 /**@brief Get directory entry name length.
119 * @param sb Superblock
120 * @param de Directory entry
121 * @return Entry name length
123 static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb,
124 struct ext4_dir_en *de)
126 uint16_t v = de->name_len;
128 if ((ext4_get32(sb, rev_level) == 0) &&
129 (ext4_get32(sb, minor_rev_level) < 5))
130 v |= ((uint16_t)de->in.name_length_high) << 8;
135 /**@brief Set directory entry name length.
136 * @param sb Superblock
137 * @param de Directory entry
138 * @param length Entry name length
140 static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb,
141 struct ext4_dir_en *de,
144 de->name_len = (len << 8) >> 8;
146 if ((ext4_get32(sb, rev_level) == 0) &&
147 (ext4_get32(sb, minor_rev_level) < 5))
148 de->in.name_length_high = len >> 8;
151 /**@brief Get i-node type of directory entry.
152 * @param sb Superblock
153 * @param de Directory entry
154 * @return I-node type (file, dir, etc.)
156 static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb,
157 struct ext4_dir_en *de)
159 if ((ext4_get32(sb, rev_level) > 0) ||
160 (ext4_get32(sb, minor_rev_level) >= 5))
161 return de->in.inode_type;
163 return EXT4_DE_UNKNOWN;
165 /**@brief Set i-node type of directory entry.
166 * @param sb Superblock
167 * @param de Directory entry
168 * @param type I-node type (file, dir, etc.)
171 static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb,
172 struct ext4_dir_en *de, uint8_t t)
174 if ((ext4_get32(sb, rev_level) > 0) ||
175 (ext4_get32(sb, minor_rev_level) >= 5))
176 de->in.inode_type = t;
179 /**@brief Verify checksum of a linear directory leaf block
180 * @param inode_ref Directory i-node
181 * @param dirent Linear directory leaf block
182 * @return true means the block passed checksum verification
184 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
185 struct ext4_dir_en *dirent);
187 /**@brief Initialize directory iterator.
188 * Set position to the first valid entry from the required position.
189 * @param it Pointer to iterator to be initialized
190 * @param inode_ref Directory i-node
191 * @param pos Position to start reading entries from
194 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
195 struct ext4_inode_ref *inode_ref, uint64_t pos);
197 /**@brief Jump to the next valid entry
198 * @param it Initialized iterator
201 int ext4_dir_iterator_next(struct ext4_dir_iter *it);
203 /**@brief Uninitialize directory iterator.
204 * Release all allocated structures.
205 * @param it Iterator to be finished
208 int ext4_dir_iterator_fini(struct ext4_dir_iter *it);
210 /**@brief Write directory entry to concrete data block.
211 * @param sb Superblock
212 * @param en Pointer to entry to be written
213 * @param entry_len Length of new entry
214 * @param child Child i-node to be written to new entry
215 * @param name Name of the new entry
216 * @param name_len Length of entry name
218 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
219 uint16_t entry_len, struct ext4_inode_ref *child,
220 const char *name, size_t name_len);
222 /**@brief Add new entry to the directory.
223 * @param parent Directory i-node
224 * @param name Name of new entry
225 * @param child I-node to be referenced from new entry
228 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
229 uint32_t name_len, struct ext4_inode_ref *child);
231 /**@brief Find directory entry with passed name.
232 * @param result Result structure to be returned if entry found
233 * @param parent Directory i-node
234 * @param name Name of entry to be found
235 * @param name_len Name length
238 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
239 struct ext4_inode_ref *parent, const char *name,
242 /**@brief Remove directory entry.
243 * @param parent Directory i-node
244 * @param name Name of the entry to be removed
245 * @param name_len Name length
248 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
251 /**@brief Try to insert entry to concrete data block.
252 * @param sb Superblock
253 * @param inode_ref Directory i-node
254 * @param dst_blk Block to try to insert entry to
255 * @param child Child i-node to be inserted by new entry
256 * @param name Name of the new entry
257 * @param name_len Length of the new entry name
260 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
261 struct ext4_inode_ref *inode_ref,
262 struct ext4_block *dst_blk,
263 struct ext4_inode_ref *child, const char *name,
266 /**@brief Try to find entry in block by name.
267 * @param block Block containing entries
268 * @param sb Superblock
269 * @param name_len Length of entry name
270 * @param name Name of entry to be found
271 * @param res_entry Output pointer to found entry, NULL if not found
274 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
275 size_t name_len, const char *name,
276 struct ext4_dir_en **res_entry);
278 /**@brief Simple function to release allocated data from result.
279 * @param parent Parent inode
280 * @param result Search result to destroy
284 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
285 struct ext4_dir_search_result *result);
287 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
288 struct ext4_dir_en *dirent);
291 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
297 #endif /* EXT4_DIR_H_ */