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_en_get_inode(struct ext4_dir_en *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_en_set_inode(struct ext4_dir_en *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_en_set_inode(struct ext4_dir_idx_dot_en *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 ext4_dir_en_get_entry_len(struct ext4_dir_en *de)
92 return to_le16(de->entry_len);
95 /**@brief Set directory entry length.
96 * @param de Directory entry
97 * @param length Entry length
99 static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l)
101 de->entry_len = to_le16(l);
104 /**@brief Get directory entry name length.
105 * @param sb Superblock
106 * @param de Directory entry
107 * @return Entry name length
109 static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb,
110 struct ext4_dir_en *de)
112 uint16_t v = de->name_len;
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_en_set_name_len(struct ext4_sblock *sb,
127 struct ext4_dir_en *de,
130 de->name_len = (len << 8) >> 8;
132 if ((ext4_get32(sb, rev_level) == 0) &&
133 (ext4_get32(sb, minor_rev_level) < 5))
134 de->in.name_length_high = len >> 8;
137 /**@brief Get i-node type of directory entry.
138 * @param sb Superblock
139 * @param de Directory entry
140 * @return I-node type (file, dir, etc.)
142 static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb,
143 struct ext4_dir_en *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_DE_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_en_set_inode_type(struct ext4_sblock *sb,
158 struct ext4_dir_en *de, uint8_t t)
160 if ((ext4_get32(sb, rev_level) > 0) ||
161 (ext4_get32(sb, minor_rev_level) >= 5))
162 de->in.inode_type = t;
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
170 bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
171 struct ext4_dir_en *dirent);
173 /**@brief Initialize directory iterator.
174 * Set position to the first valid entry from the required position.
175 * @param it Pointer to iterator to be initialized
176 * @param inode_ref Directory i-node
177 * @param pos Position to start reading entries from
180 int ext4_dir_iterator_init(struct ext4_dir_iter *it,
181 struct ext4_inode_ref *inode_ref, uint64_t pos);
183 /**@brief Jump to the next valid entry
184 * @param it Initialized iterator
187 int ext4_dir_iterator_next(struct ext4_dir_iter *it);
189 /**@brief Uninitialize directory iterator.
190 * Release all allocated structures.
191 * @param it Iterator to be finished
194 int ext4_dir_iterator_fini(struct ext4_dir_iter *it);
196 /**@brief Write directory entry to concrete data block.
197 * @param sb Superblock
198 * @param en Pointer to entry to be written
199 * @param entry_len Length of new entry
200 * @param child Child i-node to be written to new entry
201 * @param name Name of the new entry
202 * @param name_len Length of entry name
204 void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
205 uint16_t entry_len, struct ext4_inode_ref *child,
206 const char *name, size_t name_len);
208 /**@brief Add new entry to the directory.
209 * @param parent Directory i-node
210 * @param name Name of new entry
211 * @param child I-node to be referenced from new entry
214 int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
215 uint32_t name_len, struct ext4_inode_ref *child);
217 /**@brief Find directory entry with passed name.
218 * @param result Result structure to be returned if entry found
219 * @param parent Directory i-node
220 * @param name Name of entry to be found
221 * @param name_len Name length
224 int ext4_dir_find_entry(struct ext4_dir_search_result *result,
225 struct ext4_inode_ref *parent, const char *name,
228 /**@brief Remove directory entry.
229 * @param parent Directory i-node
230 * @param name Name of the entry to be removed
231 * @param name_len Name length
234 int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
237 /**@brief Try to insert entry to concrete data block.
238 * @param sb Superblock
239 * @param inode_ref Directory i-node
240 * @param dst_blk Block to try to insert entry to
241 * @param child Child i-node to be inserted by new entry
242 * @param name Name of the new entry
243 * @param name_len Length of the new entry name
246 int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
247 struct ext4_inode_ref *inode_ref,
248 struct ext4_block *dst_blk,
249 struct ext4_inode_ref *child, const char *name,
252 /**@brief Try to find entry in block by name.
253 * @param block Block containing entries
254 * @param sb Superblock
255 * @param name_len Length of entry name
256 * @param name Name of entry to be found
257 * @param res_entry Output pointer to found entry, NULL if not found
260 int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
261 size_t name_len, const char *name,
262 struct ext4_dir_en **res_entry);
264 /**@brief Simple function to release allocated data from result.
265 * @param parent Parent inode
266 * @param result Search result to destroy
270 int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
271 struct ext4_dir_search_result *result);
273 void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
274 struct ext4_dir_en *dirent);
277 void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
283 #endif /* EXT4_DIR_H_ */