dfs_vnode.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-05-05 Bernard Implement vnode in dfs v2.0
  9. */
  10. #include <dfs_file.h>
  11. #include <dfs_mnt.h>
  12. #ifdef RT_USING_PAGECACHE
  13. #include "dfs_pcache.h"
  14. #endif
  15. #define DBG_TAG "DFS.vnode"
  16. #define DBG_LVL DBG_WARNING
  17. #include <rtdbg.h>
  18. /**
  19. * @brief Initialize a virtual node (vnode) structure
  20. *
  21. * @param[in,out] vnode Pointer to the vnode to be initialized
  22. * @param[in] type Type of the vnode
  23. * @param[in] fops Pointer to file operations structure
  24. *
  25. * @return int Always returns 0 indicating success
  26. */
  27. int dfs_vnode_init(struct dfs_vnode *vnode, int type, const struct dfs_file_ops *fops)
  28. {
  29. if (vnode)
  30. {
  31. rt_memset(vnode, 0, sizeof(struct dfs_vnode));
  32. vnode->type = type;
  33. rt_atomic_store(&(vnode->ref_count), 1);
  34. vnode->mnt = RT_NULL;
  35. vnode->fops = fops;
  36. }
  37. return 0;
  38. }
  39. /**
  40. * @brief Create and initialize a new virtual node (vnode)
  41. *
  42. * @return struct dfs_vnode* Pointer to the newly created vnode, or NULL if creation failed
  43. */
  44. struct dfs_vnode *dfs_vnode_create(void)
  45. {
  46. struct dfs_vnode *vnode = rt_calloc(1, sizeof(struct dfs_vnode));
  47. if (!vnode)
  48. {
  49. LOG_E("create a vnode failed.");
  50. return RT_NULL;
  51. }
  52. rt_atomic_store(&(vnode->ref_count), 1);
  53. LOG_I("create a vnode: %p", vnode);
  54. return vnode;
  55. }
  56. /**
  57. * @brief Destroy a virtual node (vnode) and free its resources
  58. *
  59. * @param[in] vnode Pointer to the vnode to be destroyed
  60. *
  61. * @return int Always returns 0. Note that this does not guarantee success, as errors may occur internally.
  62. */
  63. int dfs_vnode_destroy(struct dfs_vnode* vnode)
  64. {
  65. rt_err_t ret = RT_EOK;
  66. if (vnode)
  67. {
  68. ret = dfs_file_lock();
  69. if (ret == RT_EOK)
  70. {
  71. if (rt_atomic_load(&(vnode->ref_count)) == 1)
  72. {
  73. LOG_I("free a vnode: %p", vnode);
  74. #ifdef RT_USING_PAGECACHE
  75. if (vnode->aspace)
  76. {
  77. dfs_aspace_destroy(vnode->aspace);
  78. }
  79. #endif
  80. if (vnode->mnt)
  81. {
  82. DLOG(msg, "vnode", vnode->mnt->fs_ops->name, DLOG_MSG, "fs_ops->free_vnode");
  83. vnode->mnt->fs_ops->free_vnode(vnode);
  84. }
  85. else
  86. {
  87. DLOG(msg, "vnode", "vnode", DLOG_MSG, "destroy vnode(mnt=NULL)");
  88. }
  89. dfs_file_unlock();
  90. rt_free(vnode);
  91. }
  92. else
  93. {
  94. dfs_file_unlock();
  95. }
  96. }
  97. }
  98. return 0;
  99. }
  100. /**
  101. * @brief Increase reference count of a virtual node (vnode)
  102. *
  103. * @param[in,out] vnode Pointer to the vnode to be referenced
  104. *
  105. * @return struct dfs_vnode* The same vnode pointer that was passed in
  106. */
  107. struct dfs_vnode *dfs_vnode_ref(struct dfs_vnode *vnode)
  108. {
  109. if (vnode)
  110. {
  111. rt_atomic_add(&(vnode->ref_count), 1);
  112. DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
  113. }
  114. return vnode;
  115. }
  116. /**
  117. * @brief Decrease reference count of a virtual node (vnode) and potentially free it
  118. *
  119. * @param[in,out] vnode Pointer to the vnode to be unreferenced
  120. */
  121. void dfs_vnode_unref(struct dfs_vnode *vnode)
  122. {
  123. rt_err_t ret = RT_EOK;
  124. if (vnode)
  125. {
  126. ret = dfs_file_lock();
  127. if (ret == RT_EOK)
  128. {
  129. rt_atomic_sub(&(vnode->ref_count), 1);
  130. DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
  131. #ifdef RT_USING_PAGECACHE
  132. if (vnode->aspace)
  133. {
  134. dfs_aspace_destroy(vnode->aspace);
  135. }
  136. #endif
  137. if (rt_atomic_load(&(vnode->ref_count)) == 0)
  138. {
  139. LOG_I("free a vnode: %p", vnode);
  140. DLOG(msg, "vnode", "vnode", DLOG_MSG, "free vnode, ref_count=0");
  141. if (vnode->mnt)
  142. {
  143. DLOG(msg, "vnode", vnode->mnt->fs_ops->name, DLOG_MSG, "fs_ops->free_vnode");
  144. vnode->mnt->fs_ops->free_vnode(vnode);
  145. }
  146. dfs_file_unlock();
  147. rt_free(vnode);
  148. }
  149. else
  150. {
  151. dfs_file_unlock();
  152. DLOG(note, "vnode", "vnode ref_count=%d", rt_atomic_load(&(vnode->ref_count)));
  153. }
  154. }
  155. }
  156. return;
  157. }