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_blockdev.h"
52 #include "ext4_super.h"
56 /**@brief Get i-node number from directory entry.
57 * @param de Directory entry
58 * @return I-node number
60 static inline uint32_t
61 ext4_dir_entry_ll_get_inode(struct ext4_dir_entry_ll *de)
63 return to_le32(de->inode);
66 /**@brief Set i-node number to directory entry.
67 * @param de Directory entry
68 * @param inode I-node number
71 ext4_dir_entry_ll_set_inode(struct ext4_dir_entry_ll *de, uint32_t inode)
73 de->inode = to_le32(inode);
76 /**@brief Set i-node number to directory entry. (For HTree root)
77 * @param de Directory entry
78 * @param inode I-node number
81 ext4_dx_dot_entry_set_inode(struct ext4_dir_idx_dot_entry *de, uint32_t inode)
83 de->inode = to_le32(inode);
86 /**@brief Get directory entry length.
87 * @param de Directory entry
88 * @return Entry length
90 static inline uint16_t
91 ext4_dir_entry_ll_get_entry_length(struct ext4_dir_entry_ll *de)
93 return to_le16(de->entry_length);
96 /**@brief Set directory entry length.
97 * @param de Directory entry
98 * @param length Entry length
101 ext4_dir_entry_ll_set_entry_length(struct ext4_dir_entry_ll *de,
104 de->entry_length = to_le16(len);
107 /**@brief Get directory entry name length.
108 * @param sb Superblock
109 * @param de Directory entry
110 * @return Entry name length
112 static inline uint16_t
113 ext4_dir_entry_ll_get_name_length(struct ext4_sblock *sb,
114 struct ext4_dir_entry_ll *de)
116 uint16_t v = de->name_length;
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;
125 /**@brief Set directory entry name length.
126 * @param sb Superblock
127 * @param de Directory entry
128 * @param length Entry name length
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)
133 de->name_length = (len << 8) >> 8;
135 if ((ext4_get32(sb, rev_level) == 0) &&
136 (ext4_get32(sb, minor_rev_level) < 5))
137 de->in.name_length_high = len >> 8;
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.)
145 static inline uint8_t
146 ext4_dir_entry_ll_get_inode_type(struct ext4_sblock *sb,
147 struct ext4_dir_entry_ll *de)
149 if ((ext4_get32(sb, rev_level) > 0) ||
150 (ext4_get32(sb, minor_rev_level) >= 5))
151 return de->in.inode_type;
153 return EXT4_DIRENTRY_UNKNOWN;
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.)
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)
164 if ((ext4_get32(sb, rev_level) > 0) ||
165 (ext4_get32(sb, minor_rev_level) >= 5))
166 de->in.inode_type = type;
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
174 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
175 struct ext4_dir_entry_ll *dirent);
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
184 int ext4_dir_iterator_init(struct ext4_dir_iterator *it,
185 struct ext4_inode_ref *inode_ref, uint64_t pos);
187 /**@brief Jump to the next valid entry
188 * @param it Initialized iterator
191 int ext4_dir_iterator_next(struct ext4_dir_iterator *it);
193 /**@brief Uninitialize directory iterator.
194 * Release all allocated structures.
195 * @param it Iterator to be finished
198 int ext4_dir_iterator_fini(struct ext4_dir_iterator *it);
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
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);
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
218 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
219 uint32_t name_len, struct ext4_inode_ref *child);
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
228 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
229 struct ext4_inode_ref *parent, const char *name,
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
238 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
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
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,
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
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);
268 /**@brief Simple function to release allocated data from result.
269 * @param parent Parent inode
270 * @param result Search result to destroy
274 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
275 struct ext4_dir_search_result *result);
277 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
278 struct ext4_dir_entry_ll *dirent);
281 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
287 #endif /* EXT4_DIR_H_ */