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 /**@brief Get i-node number from directory entry.
58 * @param de Directory entry
59 * @return I-node number
61 static inline uint32_t
62 ext4_dir_en_get_inode(struct ext4_dir_en *de)
64 return to_le32(de->inode);
67 /**@brief Set i-node number to directory entry.
68 * @param de Directory entry
69 * @param inode I-node number
72 ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode)
74 de->inode = to_le32(inode);
77 /**@brief Set i-node number to directory entry. (For HTree root)
78 * @param de Directory entry
79 * @param inode I-node number
82 ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode)
84 de->inode = to_le32(inode);
87 /**@brief Get directory entry length.
88 * @param de Directory entry
89 * @return Entry length
91 static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de)
93 return to_le16(de->entry_len);
96 /**@brief Set directory entry length.
97 * @param de Directory entry
98 * @param length Entry length
100 static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l)
102 de->entry_len = to_le16(l);
105 /**@brief Get directory entry name length.
106 * @param sb Superblock
107 * @param de Directory entry
108 * @return Entry name length
110 static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb,
111 struct ext4_dir_en *de)
113 uint16_t v = de->name_len;
115 if ((ext4_get32(sb, rev_level) == 0) &&
116 (ext4_get32(sb, minor_rev_level) < 5))
117 v |= ((uint16_t)de->in.name_length_high) << 8;
122 /**@brief Set directory entry name length.
123 * @param sb Superblock
124 * @param de Directory entry
125 * @param length Entry name length
127 static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb,
128 struct ext4_dir_en *de,
131 de->name_len = (len << 8) >> 8;
133 if ((ext4_get32(sb, rev_level) == 0) &&
134 (ext4_get32(sb, minor_rev_level) < 5))
135 de->in.name_length_high = len >> 8;
138 /**@brief Get i-node type of directory entry.
139 * @param sb Superblock
140 * @param de Directory entry
141 * @return I-node type (file, dir, etc.)
143 static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb,
144 struct ext4_dir_en *de)
146 if ((ext4_get32(sb, rev_level) > 0) ||
147 (ext4_get32(sb, minor_rev_level) >= 5))
148 return de->in.inode_type;
150 return EXT4_DE_UNKNOWN;
152 /**@brief Set i-node type of directory entry.
153 * @param sb Superblock
154 * @param de Directory entry
155 * @param type I-node type (file, dir, etc.)
158 static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb,
159 struct ext4_dir_en *de, uint8_t t)
161 if ((ext4_get32(sb, rev_level) > 0) ||
162 (ext4_get32(sb, minor_rev_level) >= 5))
163 de->in.inode_type = t;
166 /**@brief Verify checksum of a linear directory leaf block
167 * @param inode_ref Directory i-node
168 * @param dirent Linear directory leaf block
169 * @return true means the block passed checksum verification
171 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
172 struct ext4_dir_en *dirent);
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
181 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
182 struct ext4_inode_ref *inode_ref, uint64_t pos);
184 /**@brief Jump to the next valid entry
185 * @param it Initialized iterator
188 int ext4_dir_iterator_next(struct ext4_dir_iter *it);
190 /**@brief Uninitialize directory iterator.
191 * Release all allocated structures.
192 * @param it Iterator to be finished
195 int ext4_dir_iterator_fini(struct ext4_dir_iter *it);
197 /**@brief Write directory entry to concrete data block.
198 * @param sb Superblock
199 * @param en 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
205 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
206 uint16_t entry_len, struct ext4_inode_ref *child,
207 const char *name, size_t name_len);
209 /**@brief Add new entry to the directory.
210 * @param parent Directory i-node
211 * @param name Name of new entry
212 * @param child I-node to be referenced from new entry
215 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
216 uint32_t name_len, struct ext4_inode_ref *child);
218 /**@brief Find directory entry with passed name.
219 * @param result Result structure to be returned if entry found
220 * @param parent Directory i-node
221 * @param name Name of entry to be found
222 * @param name_len Name length
225 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
226 struct ext4_inode_ref *parent, const char *name,
229 /**@brief Remove directory entry.
230 * @param parent Directory i-node
231 * @param name Name of the entry to be removed
232 * @param name_len Name length
235 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
238 /**@brief Try to insert entry to concrete data block.
239 * @param sb Superblock
240 * @param inode_ref Directory i-node
241 * @param dst_blk Block to try to insert entry to
242 * @param child Child i-node to be inserted by new entry
243 * @param name Name of the new entry
244 * @param name_len Length of the new entry name
247 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
248 struct ext4_inode_ref *inode_ref,
249 struct ext4_block *dst_blk,
250 struct ext4_inode_ref *child, const char *name,
253 /**@brief Try to find entry in block by name.
254 * @param block Block containing entries
255 * @param sb Superblock
256 * @param name_len Length of entry name
257 * @param name Name of entry to be found
258 * @param res_entry Output pointer to found entry, NULL if not found
261 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
262 size_t name_len, const char *name,
263 struct ext4_dir_en **res_entry);
265 /**@brief Simple function to release allocated data from result.
266 * @param parent Parent inode
267 * @param result Search result to destroy
271 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
272 struct ext4_dir_search_result *result);
274 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
275 struct ext4_dir_en *dirent);
278 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
284 #endif /* EXT4_DIR_H_ */