ext4_blockdev.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. };
  85. /**@brief Definition of the simple block device.*/
  86. struct ext4_blockdev {
  87. /**@brief Block device interface*/
  88. struct ext4_blockdev_iface *bdif;
  89. /**@brief Offset in bdif. For multi partition mode.*/
  90. uint64_t part_offset;
  91. /**@brief Part size in bdif. For multi partition mode.*/
  92. uint64_t part_size;
  93. /**@brief Block cache.*/
  94. struct ext4_bcache *bc;
  95. /**@brief Block size (bytes) logical*/
  96. uint32_t lg_bsize;
  97. /**@brief Block count: logical*/
  98. uint64_t lg_bcnt;
  99. /**@brief Cache write back mode reference counter*/
  100. uint32_t cache_write_back;
  101. /**@brief The filesystem this block device belongs to. */
  102. struct ext4_fs *fs;
  103. };
  104. /**@brief Static initialization of the block device.*/
  105. #define EXT4_BLOCKDEV_STATIC_INSTANCE(__name, __bsize, __bcnt, __open, __bread,\
  106. __bwrite, __close, __lock, __unlock) \
  107. static uint8_t __name##_ph_bbuf[(__bsize)]; \
  108. static struct ext4_blockdev_iface __name##_iface = { \
  109. .open = __open, \
  110. .bread = __bread, \
  111. .bwrite = __bwrite, \
  112. .close = __close, \
  113. .lock = __lock, \
  114. .unlock = __unlock, \
  115. .ph_bsize = __bsize, \
  116. .ph_bcnt = __bcnt, \
  117. .ph_bbuf = __name##_ph_bbuf, \
  118. }; \
  119. static struct ext4_blockdev __name = { \
  120. .bdif = &__name##_iface, \
  121. .part_offset = 0, \
  122. .part_size = (__bcnt) * (__bsize), \
  123. }
  124. /**@brief Block device initialization.
  125. * @param bdev block device descriptor
  126. * @param bg_bsize logical block size
  127. * @param bdev block device descriptor
  128. * @return standard error code*/
  129. int ext4_block_init(struct ext4_blockdev *bdev);
  130. /**@brief Binds a bcache to block device.
  131. * @param bdev block device descriptor
  132. * @param bc block cache descriptor
  133. * @return standard error code*/
  134. int ext4_block_bind_bcache(struct ext4_blockdev *bdev, struct ext4_bcache *bc);
  135. /**@brief Close block device
  136. * @param bdev block device descriptor
  137. * @return standard error code*/
  138. int ext4_block_fini(struct ext4_blockdev *bdev);
  139. /**@brief Flush data in given buffer to disk.
  140. * @param bdev block device descriptor
  141. * @param buf buffer
  142. * @return standard error code*/
  143. int ext4_block_flush_buf(struct ext4_blockdev *bdev, struct ext4_buf *buf);
  144. /**@brief Flush data in buffer of given lba to disk,
  145. * if that buffer exists in block cache.
  146. * @param bdev block device descriptor
  147. * @param lba logical block address
  148. * @return standard error code*/
  149. int ext4_block_flush_lba(struct ext4_blockdev *bdev, uint64_t lba);
  150. /**@brief Set logical block size in block device.
  151. * @param bdev block device descriptor
  152. * @param lb_size logical block size (in bytes)
  153. * @return standard error code*/
  154. void ext4_block_set_lb_size(struct ext4_blockdev *bdev, uint32_t lb_bsize);
  155. /**@brief Block get function (through cache, don't read).
  156. * @param bdev block device descriptor
  157. * @param b block descriptor
  158. * @param lba logical block address
  159. * @return standard error code*/
  160. int ext4_block_get_noread(struct ext4_blockdev *bdev, struct ext4_block *b,
  161. uint64_t lba);
  162. /**@brief Block get function (through cache).
  163. * @param bdev block device descriptor
  164. * @param b block descriptor
  165. * @param lba logical block address
  166. * @return standard error code*/
  167. int ext4_block_get(struct ext4_blockdev *bdev, struct ext4_block *b,
  168. uint64_t lba);
  169. /**@brief Block set procedure (through cache).
  170. * @param bdev block device descriptor
  171. * @param b block descriptor
  172. * @return standard error code*/
  173. int ext4_block_set(struct ext4_blockdev *bdev, struct ext4_block *b);
  174. /**@brief Block read procedure (without cache)
  175. * @param bdev block device descriptor
  176. * @param buf output buffer
  177. * @param lba logical block address
  178. * @return standard error code*/
  179. int ext4_blocks_get_direct(struct ext4_blockdev *bdev, void *buf, uint64_t lba,
  180. uint32_t cnt);
  181. /**@brief Block write procedure (without cache)
  182. * @param bdev block device descriptor
  183. * @param buf output buffer
  184. * @param lba logical block address
  185. * @return standard error code*/
  186. int ext4_blocks_set_direct(struct ext4_blockdev *bdev, const void *buf,
  187. uint64_t lba, uint32_t cnt);
  188. /**@brief Write to block device (by direct address).
  189. * @param bdev block device descriptor
  190. * @param off byte offset in block device
  191. * @param buf input buffer
  192. * @param len length of the write buffer
  193. * @return standard error code*/
  194. int ext4_block_writebytes(struct ext4_blockdev *bdev, uint64_t off,
  195. const void *buf, uint32_t len);
  196. /**@brief Read freom block device (by direct address).
  197. * @param bdev block device descriptor
  198. * @param off byte offset in block device
  199. * @param buf input buffer
  200. * @param len length of the write buffer
  201. * @return standard error code*/
  202. int ext4_block_readbytes(struct ext4_blockdev *bdev, uint64_t off, void *buf,
  203. uint32_t len);
  204. /**@brief Flush all dirty buffers to disk
  205. * @param bdev block device descriptor
  206. * @return standard error code*/
  207. int ext4_block_cache_flush(struct ext4_blockdev *bdev);
  208. /**@brief Enable/disable write back cache mode
  209. * @param bdev block device descriptor
  210. * @param on_off
  211. * !0 - ENABLE
  212. * 0 - DISABLE (all delayed cache buffers will be flushed)
  213. * @return standard error code*/
  214. int ext4_block_cache_write_back(struct ext4_blockdev *bdev, uint8_t on_off);
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #endif /* EXT4_BLOCKDEV_H_ */
  219. /**
  220. * @}
  221. */