ext4_fs.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_fs.c
  38. * @brief More complex filesystem functions.
  39. */
  40. #ifndef EXT4_FS_H_
  41. #define EXT4_FS_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 <stdint.h>
  49. #include <stdbool.h>
  50. struct ext4_fs {
  51. bool read_only;
  52. struct ext4_blockdev *bdev;
  53. struct ext4_sblock sb;
  54. uint64_t inode_block_limits[4];
  55. uint64_t inode_blocks_per_level[4];
  56. uint32_t last_inode_bg_id;
  57. struct jbd_fs *jbd_fs;
  58. struct jbd_journal *jbd_journal;
  59. struct jbd_trans *curr_trans;
  60. };
  61. struct ext4_block_group_ref {
  62. struct ext4_block block;
  63. struct ext4_bgroup *block_group;
  64. struct ext4_fs *fs;
  65. uint32_t index;
  66. bool dirty;
  67. };
  68. struct ext4_inode_ref {
  69. struct ext4_block block;
  70. struct ext4_inode *inode;
  71. struct ext4_fs *fs;
  72. uint32_t index;
  73. bool dirty;
  74. };
  75. /**@brief Convert block address to relative index in block group.
  76. * @param sb Superblock pointer
  77. * @param baddr Block number to convert
  78. * @return Relative number of block
  79. */
  80. static inline uint32_t ext4_fs_addr_to_idx_bg(struct ext4_sblock *s,
  81. ext4_fsblk_t baddr)
  82. {
  83. if (ext4_get32(s, first_data_block) && baddr)
  84. baddr--;
  85. return baddr % ext4_get32(s, blocks_per_group);
  86. }
  87. /**@brief Convert relative block address in group to absolute address.
  88. * @param s Superblock pointer
  89. * @param index Relative block address
  90. * @param bgid Block group
  91. * @return Absolute block address
  92. */
  93. static inline ext4_fsblk_t ext4_fs_bg_idx_to_addr(struct ext4_sblock *s,
  94. uint32_t index,
  95. uint32_t bgid)
  96. {
  97. if (ext4_get32(s, first_data_block))
  98. index++;
  99. return ext4_get32(s, blocks_per_group) * bgid + index;
  100. }
  101. /**@brief TODO: */
  102. static inline ext4_fsblk_t ext4_fs_first_bg_block_no(struct ext4_sblock *s,
  103. uint32_t bgid)
  104. {
  105. return (uint64_t)bgid * ext4_get32(s, blocks_per_group) +
  106. ext4_get32(s, first_data_block);
  107. }
  108. /**@brief Initialize filesystem and read all needed data.
  109. * @param fs Filesystem instance to be initialized
  110. * @param bdev Identifier if device with the filesystem
  111. * @param read_only Mark the filesystem as read-only.
  112. * @return Error code
  113. */
  114. int ext4_fs_init(struct ext4_fs *fs, struct ext4_blockdev *bdev,
  115. bool read_only);
  116. /**@brief Destroy filesystem instance (used by unmount operation).
  117. * @param fs Filesystem to be destroyed
  118. * @return Error code
  119. */
  120. int ext4_fs_fini(struct ext4_fs *fs);
  121. /**@brief Check filesystem's features, if supported by this driver
  122. * Function can return EOK and set read_only flag. It mean's that
  123. * there are some not-supported features, that can cause problems
  124. * during some write operations.
  125. * @param fs Filesystem to be checked
  126. * @param read_only Flag if filesystem should be mounted only for reading
  127. * @return Error code
  128. */
  129. int ext4_fs_check_features(struct ext4_fs *fs, bool *read_only);
  130. /**@brief Get reference to block group specified by index.
  131. * @param fs Filesystem to find block group on
  132. * @param bgid Index of block group to load
  133. * @param ref Output pointer for reference
  134. * @return Error code
  135. */
  136. int ext4_fs_get_block_group_ref(struct ext4_fs *fs, uint32_t bgid,
  137. struct ext4_block_group_ref *ref);
  138. /**@brief Put reference to block group.
  139. * @param ref Pointer for reference to be put back
  140. * @return Error code
  141. */
  142. int ext4_fs_put_block_group_ref(struct ext4_block_group_ref *ref);
  143. /**@brief Get reference to i-node specified by index.
  144. * @param fs Filesystem to find i-node on
  145. * @param index Index of i-node to load
  146. * @param ref Output pointer for reference
  147. * @return Error code
  148. */
  149. int ext4_fs_get_inode_ref(struct ext4_fs *fs, uint32_t index,
  150. struct ext4_inode_ref *ref);
  151. /**@brief Reset blocks field of i-node.
  152. * @param fs Filesystem to reset blocks field of i-inode on
  153. * @param inode_ref ref Pointer for inode to be operated on
  154. */
  155. void ext4_fs_inode_blocks_init(struct ext4_fs *fs,
  156. struct ext4_inode_ref *inode_ref);
  157. /**@brief Put reference to i-node.
  158. * @param ref Pointer for reference to be put back
  159. * @return Error code
  160. */
  161. int ext4_fs_put_inode_ref(struct ext4_inode_ref *ref);
  162. /**@brief Convert filetype to inode mode.
  163. * @param filetype
  164. * @return inode mode
  165. */
  166. uint32_t ext4_fs_correspond_inode_mode(int filetype);
  167. /**@brief Allocate new i-node in the filesystem.
  168. * @param fs Filesystem to allocated i-node on
  169. * @param inode_ref Output pointer to return reference to allocated i-node
  170. * @param filetype File type of newly created i-node
  171. * @return Error code
  172. */
  173. int ext4_fs_alloc_inode(struct ext4_fs *fs, struct ext4_inode_ref *inode_ref,
  174. int filetype);
  175. /**@brief Release i-node and mark it as free.
  176. * @param inode_ref I-node to be released
  177. * @return Error code
  178. */
  179. int ext4_fs_free_inode(struct ext4_inode_ref *inode_ref);
  180. /**@brief Truncate i-node data blocks.
  181. * @param inode_ref I-node to be truncated
  182. * @param new_size New size of inode (must be < current size)
  183. * @return Error code
  184. */
  185. int ext4_fs_truncate_inode(struct ext4_inode_ref *inode_ref, uint64_t new_size);
  186. /**@brief Compute 'goal' for inode index
  187. * @param inode_ref Reference to inode, to allocate block for
  188. * @return goal
  189. */
  190. ext4_fsblk_t ext4_fs_inode_to_goal_block(struct ext4_inode_ref *inode_ref);
  191. /**@brief Compute 'goal' for allocation algorithm (For blockmap).
  192. * @param inode_ref Reference to inode, to allocate block for
  193. * @param goal
  194. * @return error code
  195. */
  196. int ext4_fs_indirect_find_goal(struct ext4_inode_ref *inode_ref,
  197. ext4_fsblk_t *goal);
  198. /**@brief Get physical block address by logical index of the block.
  199. * @param inode_ref I-node to read block address from
  200. * @param iblock Logical index of block
  201. * @param fblock Output pointer for return physical
  202. * block address
  203. * @param support_unwritten Indicate whether unwritten block range
  204. * is supported under the current context
  205. * @return Error code
  206. */
  207. int ext4_fs_get_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
  208. ext4_lblk_t iblock, ext4_fsblk_t *fblock,
  209. bool support_unwritten);
  210. /**@brief Initialize a part of unwritten range of the inode.
  211. * @param inode_ref I-node to proceed on.
  212. * @param iblock Logical index of block
  213. * @param fblock Output pointer for return physical block address
  214. * @return Error code
  215. */
  216. int ext4_fs_init_inode_dblk_idx(struct ext4_inode_ref *inode_ref,
  217. ext4_lblk_t iblock, ext4_fsblk_t *fblock);
  218. /**@brief Append following logical block to the i-node.
  219. * @param inode_ref I-node to append block to
  220. * @param fblock Output physical block address of newly allocated block
  221. * @param iblock Output logical number of newly allocated block
  222. * @return Error code
  223. */
  224. int ext4_fs_append_inode_dblk(struct ext4_inode_ref *inode_ref,
  225. ext4_fsblk_t *fblock, ext4_lblk_t *iblock);
  226. /**@brief Increment inode link count.
  227. * @param inode none handle
  228. */
  229. void ext4_fs_inode_links_count_inc(struct ext4_inode_ref *inode_ref);
  230. /**@brief Decrement inode link count.
  231. * @param inode none handle
  232. */
  233. void ext4_fs_inode_links_count_dec(struct ext4_inode_ref *inode_ref);
  234. #ifdef __cplusplus
  235. }
  236. #endif
  237. #endif /* EXT4_FS_H_ */
  238. /**
  239. * @}
  240. */