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.
45 #include "ext4_config.h"
46 #include "ext4_types.h"
47 #include "ext4_blockdev.h"
48 #include "ext4_super.h"
52 /**@brief Get i-node number from directory entry.
53 * @param de Directory entry
54 * @return I-node number
56 static inline uint32_t
57 ext4_dir_entry_ll_get_inode(struct ext4_dir_entry_ll *de)
59 return to_le32(de->inode);
62 /**@brief Set i-node number to directory entry.
63 * @param de Directory entry
64 * @param inode I-node number
67 ext4_dir_entry_ll_set_inode(struct ext4_dir_entry_ll *de, uint32_t inode)
69 de->inode = to_le32(inode);
72 /**@brief Set i-node number to directory entry. (For HTree root)
73 * @param de Directory entry
74 * @param inode I-node number
77 ext4_dx_dot_entry_set_inode(struct ext4_dir_idx_dot_entry *de, uint32_t inode)
79 de->inode = to_le32(inode);
82 /**@brief Get directory entry length.
83 * @param de Directory entry
84 * @return Entry length
86 static inline uint16_t
87 ext4_dir_entry_ll_get_entry_length(struct ext4_dir_entry_ll *de)
89 return to_le16(de->entry_length);
92 /**@brief Set directory entry length.
93 * @param de Directory entry
94 * @param length Entry length
97 ext4_dir_entry_ll_set_entry_length(struct ext4_dir_entry_ll *de,
100 de->entry_length = to_le16(len);
103 /**@brief Get directory entry name length.
104 * @param sb Superblock
105 * @param de Directory entry
106 * @return Entry name length
108 static inline uint16_t
109 ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
110 struct ext4_dir_entry_ll *de)
112 uint16_t v = de->name_length;
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;
121 /**@brief Set directory entry name length.
122 * @param sb Superblock
123 * @param de Directory entry
124 * @param length Entry name length
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)
129 de->name_length = (len << 8) >> 8;
131 if ((ext4_get32(sb, rev_level) == 0) &&
132 (ext4_get32(sb, minor_rev_level) < 5))
133 de->in.name_length_high = len >> 8;
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.)
141 static inline uint8_t
142 ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
143 struct ext4_dir_entry_ll *de)
145 if ((ext4_get32(sb, rev_level) > 0) ||
146 (ext4_get32(sb, minor_rev_level) >= 5))
147 return de->in.inode_type;
149 return EXT4_DIRENTRY_UNKNOWN;
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.)
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)
160 if ((ext4_get32(sb, rev_level) > 0) ||
161 (ext4_get32(sb, minor_rev_level) >= 5))
162 de->in.inode_type = type;
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
171 ext4_dir_checksum_verify(struct ext4_inode_ref *inode_ref,
172 struct ext4_dir_entry_ll *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_iterator *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_iterator *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_iterator *it);
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
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);
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
216 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
217 uint32_t name_len, struct ext4_inode_ref *child);
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
226 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
227 struct ext4_inode_ref *parent, const char *name,
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
236 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
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
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,
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
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);
266 /**@brief Simple function to release allocated data from result.
267 * @param parent Parent inode
268 * @param result Search result to destroy
272 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
273 struct ext4_dir_search_result *result);
275 void ext4_dir_set_checksum(struct ext4_inode_ref *inode_ref,
276 struct ext4_dir_entry_ll *dirent);
278 /* checksumming functions */
279 void initialize_dir_tail(struct ext4_dir_entry_tail *t);
281 #endif /* EXT4_DIR_H_ */