ext4_blockdev.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com)
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * - The name of the author may not be used to endorse or promote products
  15. * derived from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. /** @addtogroup lwext4
  29. * @{
  30. */
  31. /**
  32. * @file ext4_blockdev.h
  33. * @brief Block device module.
  34. */
  35. #ifndef EXT4_BLOCKDEV_H_
  36. #define EXT4_BLOCKDEV_H_
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. #include <ext4_config.h>
  41. #include <ext4_bcache.h>
  42. #include <stdbool.h>
  43. #include <stdint.h>
  44. struct ext4_blockdev_iface {
  45. /**@brief Open device function
  46. * @param bdev block device.*/
  47. int (*open)(struct ext4_blockdev *bdev);
  48. /**@brief Block read function.
  49. * @param bdev block device
  50. * @param buf output buffer
  51. * @param blk_id block id
  52. * @param blk_cnt block count*/
  53. int (*bread)(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  54. uint32_t blk_cnt);
  55. /**@brief Block write function.
  56. * @param buf input buffer
  57. * @param blk_id block id
  58. * @param blk_cnt block count*/
  59. int (*bwrite)(struct ext4_blockdev *bdev, const void *buf,
  60. uint64_t blk_id, uint32_t blk_cnt);
  61. /**@brief Close device function.
  62. * @param bdev block device.*/
  63. int (*close)(struct ext4_blockdev *bdev);
  64. /**@brief Lock block device. Required in multi partition mode
  65. * operations. Not mandatory field.
  66. * @param bdev block device.*/
  67. int (*lock)(struct ext4_blockdev *bdev);
  68. /**@brief Unlock block device. Required in multi partition mode
  69. * operations. Not mandatory field.
  70. * @param bdev block device.*/
  71. int (*unlock)(struct ext4_blockdev *bdev);
  72. /**@brief Block size (bytes): physical*/
  73. uint32_t ph_bsize;
  74. /**@brief Block count: physical*/
  75. uint64_t ph_bcnt;
  76. /**@brief Block size buffer: physical*/
  77. uint8_t *ph_bbuf;
  78. /**@brief Reference counter to block device interface*/
  79. uint32_t ph_refctr;
  80. /**@brief Physical read counter*/
  81. uint32_t bread_ctr;
  82. /**@brief Physical write counter*/
  83. uint32_t bwrite_ctr;
  84. /**@brief User data pointer*/
  85. void* p_user;
  86. };
  87. /**@brief Definition of the simple block device.*/
  88. struct ext4_blockdev {
  89. /**@brief Block device interface*/
  90. struct ext4_blockdev_iface *bdif;
  91. /**@brief Offset in bdif. For multi partition mode.*/
  92. uint64_t part_offset;
  93. /**@brief Part size in bdif. For multi partition mode.*/
  94. uint64_t part_size;
  95. /**@brief Block cache.*/
  96. struct ext4_bcache *bc;
  97. /**@brief Block size (bytes) logical*/
  98. uint32_t lg_bsize;
  99. /**@brief Block count: logical*/
  100. uint64_t lg_bcnt;
  101. /**@brief Cache write back mode reference counter*/
  102. uint32_t cache_write_back;
  103. /**@brief The filesystem this block device belongs to. */
  104. struct ext4_fs *fs;
  105. void *journal;
  106. };
  107. /**@brief Static initialization of the block device.*/
  108. #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
  109. __bwrite, __close, __lock, __unlock) \
  110. static uint8_t __name##_ph_bbuf[(__bsize)]; \
  111. static struct ext4_blockdev_iface __name##_iface = { \
  112. .open = __open, \
  113. .bread = __bread, \
  114. .bwrite = __bwrite, \
  115. .close = __close, \
  116. .lock = __lock, \
  117. .unlock = __unlock, \
  118. .ph_bsize = __bsize, \
  119. .ph_bcnt = __bcnt, \
  120. .ph_bbuf = __name##_ph_bbuf, \
  121. }; \
  122. static struct ext4_blockdev __name = { \
  123. .bdif = &__name##_iface, \
  124. .part_offset = 0, \
  125. .part_size = (__bcnt) * (__bsize), \
  126. }
  127. /**@brief Block device initialization.
  128. * @param bdev block device descriptor
  129. * @param bg_bsize logical block size
  130. * @param bdev block device descriptor
  131. * @return standard error code*/
  132. int ext4_block_init(struct ext4_blockdev *bdev);
  133. /**@brief Binds a bcache to block device.
  134. * @param bdev block device descriptor
  135. * @param bc block cache descriptor
  136. * @return standard error code*/
  137. int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
  138. /**@brief Close block device
  139. * @param bdev block device descriptor
  140. * @return standard error code*/
  141. int ext4_block_fini(struct ext4_blockdev *bdev);
  142. /**@brief Flush data in given buffer to disk.
  143. * @param bdev block device descriptor
  144. * @param buf buffer
  145. * @return standard error code*/
  146. int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
  147. /**@brief Flush data in buffer of given lba to disk,
  148. * if that buffer exists in block cache.
  149. * @param bdev block device descriptor
  150. * @param lba logical block address
  151. * @return standard error code*/
  152. int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba);
  153. /**@brief Set logical block size in block device.
  154. * @param bdev block device descriptor
  155. * @param lb_size logical block size (in bytes)
  156. * @return standard error code*/
  157. void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize);
  158. /**@brief Block get function (through cache, don't read).
  159. * @param bdev block device descriptor
  160. * @param b block descriptor
  161. * @param lba logical block address
  162. * @return standard error code*/
  163. int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
  164. uint64_t lba);
  165. /**@brief Block get function (through cache).
  166. * @param bdev block device descriptor
  167. * @param b block descriptor
  168. * @param lba logical block address
  169. * @return standard error code*/
  170. int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
  171. uint64_t lba);
  172. /**@brief Block set procedure (through cache).
  173. * @param bdev block device descriptor
  174. * @param b block descriptor
  175. * @return standard error code*/
  176. int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
  177. /**@brief Block read procedure (without cache)
  178. * @param bdev block device descriptor
  179. * @param buf output buffer
  180. * @param lba logical block address
  181. * @return standard error code*/
  182. int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
  183. uint32_t cnt);
  184. /**@brief Block write procedure (without cache)
  185. * @param bdev block device descriptor
  186. * @param buf output buffer
  187. * @param lba logical block address
  188. * @return standard error code*/
  189. int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
  190. uint64_t lba, uint32_t cnt);
  191. /**@brief Write to block device (by direct address).
  192. * @param bdev block device descriptor
  193. * @param off byte offset in block device
  194. * @param buf input buffer
  195. * @param len length of the write buffer
  196. * @return standard error code*/
  197. int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
  198. const void *buf, uint32_t len);
  199. /**@brief Read freom block device (by direct address).
  200. * @param bdev block device descriptor
  201. * @param off byte offset in block device
  202. * @param buf input buffer
  203. * @param len length of the write buffer
  204. * @return standard error code*/
  205. int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
  206. uint32_t len);
  207. /**@brief Flush all dirty buffers to disk
  208. * @param bdev block device descriptor
  209. * @return standard error code*/
  210. int ext4_block_cache_flush(struct ext4_blockdev *bdev);
  211. /**@brief Enable/disable write back cache mode
  212. * @param bdev block device descriptor
  213. * @param on_off
  214. * !0 - ENABLE
  215. * 0 - DISABLE (all delayed cache buffers will be flushed)
  216. * @return standard error code*/
  217. int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
  218. #ifdef __cplusplus
  219. }
  220. #endif
  221. #endif /* EXT4_BLOCKDEV_H_ */
  222. /**
  223. * @}
  224. */