ext4_dir.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. *
  4. *
  5. * HelenOS:
  6. * Copyright (c) 2012 Martin Sucha
  7. * Copyright (c) 2012 Frantisek Princ
  8. * All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  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.
  21. *
  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.
  32. */
  33. /** @addtogroup lwext4
  34. * @{
  35. */
  36. /**
  37. * @file ext4_dir.h
  38. * @brief Directory handle procedures.
  39. */
  40. #ifndef EXT4_DIR_H_
  41. #define EXT4_DIR_H_
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include <ext4_config.h>
  46. #include <ext4_types.h>
  47. #include <ext4_misc.h>
  48. #include <ext4_blockdev.h>
  49. #include <ext4_super.h>
  50. #include <stdint.h>
  51. struct ext4_dir_iter {
  52. struct ext4_inode_ref *inode_ref;
  53. struct ext4_block curr_blk;
  54. uint64_t curr_off;
  55. struct ext4_dir_en *curr;
  56. };
  57. struct ext4_dir_search_result {
  58. struct ext4_block block;
  59. struct ext4_dir_en *dentry;
  60. };
  61. /**@brief Get i-node number from directory entry.
  62. * @param de Directory entry
  63. * @return I-node number
  64. */
  65. static inline uint32_t
  66. ext4_dir_en_get_inode(struct ext4_dir_en *de)
  67. {
  68. return to_le32(de->inode);
  69. }
  70. /**@brief Set i-node number to directory entry.
  71. * @param de Directory entry
  72. * @param inode I-node number
  73. */
  74. static inline void
  75. ext4_dir_en_set_inode(struct ext4_dir_en *de, uint32_t inode)
  76. {
  77. de->inode = to_le32(inode);
  78. }
  79. /**@brief Set i-node number to directory entry. (For HTree root)
  80. * @param de Directory entry
  81. * @param inode I-node number
  82. */
  83. static inline void
  84. ext4_dx_dot_en_set_inode(struct ext4_dir_idx_dot_en *de, uint32_t inode)
  85. {
  86. de->inode = to_le32(inode);
  87. }
  88. /**@brief Get directory entry length.
  89. * @param de Directory entry
  90. * @return Entry length
  91. */
  92. static inline uint16_t ext4_dir_en_get_entry_len(struct ext4_dir_en *de)
  93. {
  94. return to_le16(de->entry_len);
  95. }
  96. /**@brief Set directory entry length.
  97. * @param de Directory entry
  98. * @param l Entry length
  99. */
  100. static inline void ext4_dir_en_set_entry_len(struct ext4_dir_en *de, uint16_t l)
  101. {
  102. de->entry_len = to_le16(l);
  103. }
  104. /**@brief Get directory entry name length.
  105. * @param sb Superblock
  106. * @param de Directory entry
  107. * @return Entry name length
  108. */
  109. static inline uint16_t ext4_dir_en_get_name_len(struct ext4_sblock *sb,
  110. struct ext4_dir_en *de)
  111. {
  112. uint16_t v = de->name_len;
  113. if ((ext4_get32(sb, rev_level) == 0) &&
  114. (ext4_get32(sb, minor_rev_level) < 5))
  115. v |= ((uint16_t)de->in.name_length_high) << 8;
  116. return v;
  117. }
  118. /**@brief Set directory entry name length.
  119. * @param sb Superblock
  120. * @param de Directory entry
  121. * @param len Entry name length
  122. */
  123. static inline void ext4_dir_en_set_name_len(struct ext4_sblock *sb,
  124. struct ext4_dir_en *de,
  125. uint16_t len)
  126. {
  127. de->name_len = (len << 8) >> 8;
  128. if ((ext4_get32(sb, rev_level) == 0) &&
  129. (ext4_get32(sb, minor_rev_level) < 5))
  130. de->in.name_length_high = len >> 8;
  131. }
  132. /**@brief Get i-node type of directory entry.
  133. * @param sb Superblock
  134. * @param de Directory entry
  135. * @return I-node type (file, dir, etc.)
  136. */
  137. static inline uint8_t ext4_dir_en_get_inode_type(struct ext4_sblock *sb,
  138. struct ext4_dir_en *de)
  139. {
  140. if ((ext4_get32(sb, rev_level) > 0) ||
  141. (ext4_get32(sb, minor_rev_level) >= 5))
  142. return de->in.inode_type;
  143. return EXT4_DE_UNKNOWN;
  144. }
  145. /**@brief Set i-node type of directory entry.
  146. * @param sb Superblock
  147. * @param de Directory entry
  148. * @param t I-node type (file, dir, etc.)
  149. */
  150. static inline void ext4_dir_en_set_inode_type(struct ext4_sblock *sb,
  151. struct ext4_dir_en *de, uint8_t t)
  152. {
  153. if ((ext4_get32(sb, rev_level) > 0) ||
  154. (ext4_get32(sb, minor_rev_level) >= 5))
  155. de->in.inode_type = t;
  156. }
  157. /**@brief Verify checksum of a linear directory leaf block
  158. * @param inode_ref Directory i-node
  159. * @param dirent Linear directory leaf block
  160. * @return true means the block passed checksum verification
  161. */
  162. bool ext4_dir_csum_verify(struct ext4_inode_ref *inode_ref,
  163. struct ext4_dir_en *dirent);
  164. /**@brief Initialize directory iterator.
  165. * Set position to the first valid entry from the required position.
  166. * @param it Pointer to iterator to be initialized
  167. * @param inode_ref Directory i-node
  168. * @param pos Position to start reading entries from
  169. * @return Error code
  170. */
  171. int ext4_dir_iterator_init(struct ext4_dir_iter *it,
  172. struct ext4_inode_ref *inode_ref, uint64_t pos);
  173. /**@brief Jump to the next valid entry
  174. * @param it Initialized iterator
  175. * @return Error code
  176. */
  177. int ext4_dir_iterator_next(struct ext4_dir_iter *it);
  178. /**@brief Uninitialize directory iterator.
  179. * Release all allocated structures.
  180. * @param it Iterator to be finished
  181. * @return Error code
  182. */
  183. int ext4_dir_iterator_fini(struct ext4_dir_iter *it);
  184. /**@brief Write directory entry to concrete data block.
  185. * @param sb Superblock
  186. * @param en Pointer to entry to be written
  187. * @param entry_len Length of new entry
  188. * @param child Child i-node to be written to new entry
  189. * @param name Name of the new entry
  190. * @param name_len Length of entry name
  191. */
  192. void ext4_dir_write_entry(struct ext4_sblock *sb, struct ext4_dir_en *en,
  193. uint16_t entry_len, struct ext4_inode_ref *child,
  194. const char *name, size_t name_len);
  195. /**@brief Add new entry to the directory.
  196. * @param parent Directory i-node
  197. * @param name Name of new entry
  198. * @param child I-node to be referenced from new entry
  199. * @return Error code
  200. */
  201. int ext4_dir_add_entry(struct ext4_inode_ref *parent, const char *name,
  202. uint32_t name_len, struct ext4_inode_ref *child);
  203. /**@brief Find directory entry with passed name.
  204. * @param result Result structure to be returned if entry found
  205. * @param parent Directory i-node
  206. * @param name Name of entry to be found
  207. * @param name_len Name length
  208. * @return Error code
  209. */
  210. int ext4_dir_find_entry(struct ext4_dir_search_result *result,
  211. struct ext4_inode_ref *parent, const char *name,
  212. uint32_t name_len);
  213. /**@brief Remove directory entry.
  214. * @param parent Directory i-node
  215. * @param name Name of the entry to be removed
  216. * @param name_len Name length
  217. * @return Error code
  218. */
  219. int ext4_dir_remove_entry(struct ext4_inode_ref *parent, const char *name,
  220. uint32_t name_len);
  221. /**@brief Try to insert entry to concrete data block.
  222. * @param sb Superblock
  223. * @param inode_ref Directory i-node
  224. * @param dst_blk Block to try to insert entry to
  225. * @param child Child i-node to be inserted by new entry
  226. * @param name Name of the new entry
  227. * @param name_len Length of the new entry name
  228. * @return Error code
  229. */
  230. int ext4_dir_try_insert_entry(struct ext4_sblock *sb,
  231. struct ext4_inode_ref *inode_ref,
  232. struct ext4_block *dst_blk,
  233. struct ext4_inode_ref *child, const char *name,
  234. uint32_t name_len);
  235. /**@brief Try to find entry in block by name.
  236. * @param block Block containing entries
  237. * @param sb Superblock
  238. * @param name_len Length of entry name
  239. * @param name Name of entry to be found
  240. * @param res_entry Output pointer to found entry, NULL if not found
  241. * @return Error code
  242. */
  243. int ext4_dir_find_in_block(struct ext4_block *block, struct ext4_sblock *sb,
  244. size_t name_len, const char *name,
  245. struct ext4_dir_en **res_entry);
  246. /**@brief Simple function to release allocated data from result.
  247. * @param parent Parent inode
  248. * @param result Search result to destroy
  249. * @return Error code
  250. *
  251. */
  252. int ext4_dir_destroy_result(struct ext4_inode_ref *parent,
  253. struct ext4_dir_search_result *result);
  254. void ext4_dir_set_csum(struct ext4_inode_ref *inode_ref,
  255. struct ext4_dir_en *dirent);
  256. void ext4_dir_init_entry_tail(struct ext4_dir_entry_tail *t);
  257. #ifdef __cplusplus
  258. }
  259. #endif
  260. #endif /* EXT4_DIR_H_ */
  261. /**
  262. * @}
  263. */