dfs_ext_blockdev.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-01-15 bernard add RT-Thread 5.0.x support
  9. */
  10. #include <rtthread.h>
  11. #include <dfs_fs.h>
  12. #include <ext4.h>
  13. #include <ext4_errno.h>
  14. #include <ext4_blockdev.h>
  15. #include "dfs_ext.h"
  16. #include "dfs_ext_blockdev.h"
  17. static int blockdev_lock(struct ext4_blockdev *bdev);
  18. static int blockdev_unlock(struct ext4_blockdev *bdev);
  19. static int blockdev_open(struct ext4_blockdev *bdev);
  20. static int blockdev_read(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id, uint32_t blk_cnt);
  21. static int blockdev_write(struct ext4_blockdev *bdev, const void *buf, uint64_t blk_id, uint32_t blk_cnt);
  22. static int blockdev_close(struct ext4_blockdev *bdev);
  23. static rt_mutex_t bdevice_mutex = RT_NULL;
  24. static int blockdev_lock(struct ext4_blockdev *bdev)
  25. {
  26. rt_err_t result = -RT_EBUSY;
  27. if (bdevice_mutex == RT_NULL)
  28. {
  29. /* create block device mutex */
  30. bdevice_mutex = rt_mutex_create("ext_bd", RT_IPC_FLAG_PRIO);
  31. RT_ASSERT(bdevice_mutex != RT_NULL);
  32. }
  33. while (result == -RT_EBUSY)
  34. {
  35. result = rt_mutex_take(bdevice_mutex, RT_WAITING_FOREVER);
  36. }
  37. if (result != RT_EOK)
  38. {
  39. RT_ASSERT(0);
  40. }
  41. return 0;
  42. }
  43. static int blockdev_unlock(struct ext4_blockdev *bdev)
  44. {
  45. rt_mutex_release(bdevice_mutex);
  46. return 0;
  47. }
  48. static int blockdev_open(struct ext4_blockdev *bdev)
  49. {
  50. int r;
  51. struct dfs_ext4_blockdev *dbd;
  52. rt_device_t device = NULL;
  53. struct rt_device_blk_geometry geometry;
  54. dbd = dfs_ext4_blockdev_from_bd(bdev);
  55. if (!dbd) return -RT_EINVAL;
  56. device = dbd->devid;
  57. RT_ASSERT(device != NULL);
  58. r = rt_device_open(device, RT_DEVICE_OFLAG_RDWR);
  59. if (r != RT_EOK)
  60. {
  61. return r;
  62. }
  63. r = rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  64. if (r == RT_EOK)
  65. {
  66. if (geometry.block_size != geometry.bytes_per_sector)
  67. {
  68. rt_kprintf("block device: block size != bytes_per_sector\n");
  69. rt_device_close(device);
  70. return -RT_EIO;
  71. }
  72. bdev->part_offset = 0;
  73. bdev->part_size = geometry.sector_count * geometry.bytes_per_sector;
  74. bdev->bdif->ph_bsize = geometry.block_size;
  75. bdev->bdif->ph_bcnt = bdev->part_size / bdev->bdif->ph_bsize;
  76. }
  77. else
  78. {
  79. rt_kprintf("block device: get geometry failed!\n");
  80. rt_device_close(device);
  81. return -RT_EIO;
  82. }
  83. return r;
  84. }
  85. static int blockdev_read(struct ext4_blockdev *bdev, void *buf, uint64_t blk_id,
  86. uint32_t blk_cnt)
  87. {
  88. int result;
  89. struct dfs_ext4_blockdev *dbd;
  90. rt_device_t device = NULL;
  91. dbd = dfs_ext4_blockdev_from_bd(bdev);
  92. if (!dbd) return -RT_EINVAL;
  93. device = dbd->devid;
  94. RT_ASSERT(device != NULL);
  95. result = rt_device_read(device, blk_id, buf, blk_cnt);
  96. if (result == blk_cnt)
  97. {
  98. result = 0;
  99. }
  100. else
  101. {
  102. result = -RT_EIO;
  103. }
  104. return result;
  105. }
  106. static int blockdev_write(struct ext4_blockdev *bdev, const void *buf,
  107. uint64_t blk_id, uint32_t blk_cnt)
  108. {
  109. int result;
  110. struct dfs_ext4_blockdev *dbd;
  111. rt_device_t device = NULL;
  112. dbd = dfs_ext4_blockdev_from_bd(bdev);
  113. if (!dbd) return -RT_EINVAL;
  114. device = dbd->devid;
  115. RT_ASSERT(device != NULL);
  116. result = rt_device_write(device, blk_id, buf, blk_cnt);
  117. if (result == blk_cnt)
  118. {
  119. result = 0;
  120. }
  121. else
  122. {
  123. result = -RT_EIO;
  124. }
  125. return result;
  126. }
  127. static int blockdev_close(struct ext4_blockdev *bdev)
  128. {
  129. int result;
  130. struct dfs_ext4_blockdev *dbd;
  131. rt_device_t device = NULL;
  132. dbd = dfs_ext4_blockdev_from_bd(bdev);
  133. if (!dbd) return -RT_EINVAL;
  134. device = dbd->devid;
  135. RT_ASSERT(device != NULL);
  136. result = rt_device_close(device);
  137. return result;
  138. }
  139. int dfs_ext4_blockdev_init(struct dfs_ext4_blockdev* dbd, rt_device_t devid)
  140. {
  141. uint8_t *ph_bbuf = NULL;
  142. struct ext4_blockdev *bd = NULL;
  143. struct ext4_blockdev_iface *iface = NULL;
  144. if (dbd && devid)
  145. {
  146. dbd->devid = devid;
  147. bd = &dbd->bd;
  148. iface = (struct ext4_blockdev_iface *)rt_calloc(1, sizeof(struct ext4_blockdev_iface));
  149. if (iface == NULL) return -RT_ENOMEM;
  150. bd->bdif = iface;
  151. ph_bbuf = &dbd->ph_bbuf[0];
  152. iface->open = blockdev_open;
  153. iface->bread = blockdev_read;
  154. iface->bwrite = blockdev_write;
  155. iface->close = blockdev_close;
  156. iface->lock = blockdev_lock,
  157. iface->unlock = blockdev_unlock;
  158. iface->ph_bsize = 4096;
  159. iface->ph_bcnt = 0;
  160. iface->ph_bbuf = ph_bbuf;
  161. bd->bdif = iface;
  162. bd->part_offset = 0;
  163. bd->part_size = 0;
  164. }
  165. return 0;
  166. }
  167. struct dfs_ext4_blockdev *dfs_ext4_blockdev_from_bd(struct ext4_blockdev *bd)
  168. {
  169. struct dfs_ext4_blockdev *dbd = NULL;
  170. if (bd)
  171. {
  172. dbd = rt_container_of(bd, struct dfs_ext4_blockdev, bd);
  173. }
  174. return dbd;
  175. }
  176. struct dfs_ext4_blockdev *dfs_ext4_blockdev_create(rt_device_t devid)
  177. {
  178. struct dfs_ext4_blockdev *dbd = NULL;
  179. dbd = (struct dfs_ext4_blockdev*) rt_calloc(1, sizeof(struct dfs_ext4_blockdev));
  180. if (dbd)
  181. {
  182. dfs_ext4_blockdev_init(dbd, devid);
  183. }
  184. return dbd;
  185. }
  186. int dfs_ext4_blockdev_destroy(struct dfs_ext4_blockdev *dbd)
  187. {
  188. int ret = -1;
  189. if (dbd)
  190. {
  191. rt_free(dbd->bd.bdif);
  192. rt_free(dbd);
  193. ret = 0;
  194. }
  195. return ret;
  196. }