dfs_mnt.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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-05-05 Bernard Implement mnt in dfs v2.0
  9. */
  10. #include <rtthread.h>
  11. #include "dfs.h"
  12. #include "dfs_mnt.h"
  13. #include "dfs_dentry.h"
  14. #include "dfs_private.h"
  15. #define DBG_TAG "DFS.mnt"
  16. #define DBG_LVL DBG_WARNING
  17. #include <rtdbg.h>
  18. static struct dfs_mnt *_root_mnt = RT_NULL;
  19. /*
  20. * mnt tree structure
  21. *
  22. * mnt_root <----------------------------------------+
  23. * | (child) +----------+ |
  24. * v (sibling) v | |
  25. * mnt_child0 -> mnt_child1 | |
  26. * | (child) | |
  27. * v / (parent) | (root)
  28. * mnt_child10 ---/
  29. *
  30. */
  31. struct dfs_mnt *dfs_mnt_create(const char *path)
  32. {
  33. struct dfs_mnt *mnt = rt_calloc(1, sizeof(struct dfs_mnt));
  34. if (mnt)
  35. {
  36. LOG_I("create mnt at %s", path);
  37. mnt->fullpath = rt_strdup(path);
  38. rt_list_init(&mnt->sibling);
  39. rt_list_init(&mnt->child);
  40. mnt->flags |= MNT_IS_ALLOCED;
  41. rt_atomic_store(&(mnt->ref_count), 1);
  42. }
  43. else
  44. {
  45. rt_set_errno(-ENOMEM);
  46. }
  47. return mnt;
  48. }
  49. int dfs_mnt_insert(struct dfs_mnt* mnt, struct dfs_mnt* child)
  50. {
  51. if (child)
  52. {
  53. if (mnt == RT_NULL)
  54. {
  55. /* insert into root */
  56. mnt = dfs_mnt_lookup(child->fullpath);
  57. if (mnt == RT_NULL || (strcmp(child->fullpath, "/") == 0))
  58. {
  59. /* it's root mnt */
  60. mnt = child;
  61. mnt->flags |= MNT_IS_LOCKED;
  62. /* ref to gobal root */
  63. if (_root_mnt)
  64. {
  65. child = _root_mnt;
  66. rt_atomic_sub(&(_root_mnt->parent->ref_count), 1);
  67. rt_atomic_sub(&(_root_mnt->ref_count), 1);
  68. _root_mnt = dfs_mnt_ref(mnt);
  69. mnt->parent = dfs_mnt_ref(mnt);
  70. mnt->flags |= MNT_IS_ADDLIST;
  71. mkdir("/dev", 0777);
  72. }
  73. else
  74. {
  75. _root_mnt = dfs_mnt_ref(mnt);
  76. }
  77. }
  78. }
  79. if (mnt)
  80. {
  81. child->flags |= MNT_IS_ADDLIST;
  82. if (child != mnt)
  83. {
  84. /* not the root, insert into the child list */
  85. rt_list_insert_before(&mnt->child, &child->sibling);
  86. /* child ref self */
  87. dfs_mnt_ref(child);
  88. }
  89. /* parent ref parent */
  90. child->parent = dfs_mnt_ref(mnt);
  91. }
  92. }
  93. return 0;
  94. }
  95. /* remove mnt from mnt_tree */
  96. int dfs_mnt_remove(struct dfs_mnt* mnt)
  97. {
  98. int ret = -RT_ERROR;
  99. if (rt_list_isempty(&mnt->child))
  100. {
  101. rt_list_remove(&mnt->sibling);
  102. if (mnt->parent)
  103. {
  104. /* parent unref parent */
  105. rt_atomic_sub(&(mnt->parent->ref_count), 1);
  106. }
  107. ret = RT_EOK;
  108. }
  109. else
  110. {
  111. LOG_W("remove a mnt point:%s with child.", mnt->fullpath);
  112. }
  113. return ret;
  114. }
  115. static struct dfs_mnt *_dfs_mnt_dev_lookup(struct dfs_mnt *mnt, rt_device_t dev_id)
  116. {
  117. struct dfs_mnt *ret = RT_NULL, *iter = RT_NULL;
  118. rt_list_for_each_entry(iter, &mnt->child, sibling)
  119. {
  120. if (iter->dev_id == dev_id)
  121. {
  122. ret = iter;
  123. break;
  124. }
  125. else
  126. {
  127. ret = _dfs_mnt_dev_lookup(iter, dev_id);
  128. if (ret)
  129. {
  130. break;
  131. }
  132. }
  133. }
  134. return ret;
  135. }
  136. struct dfs_mnt *dfs_mnt_dev_lookup(rt_device_t dev_id)
  137. {
  138. struct dfs_mnt *mnt = _root_mnt;
  139. struct dfs_mnt *ret = RT_NULL;
  140. if (mnt)
  141. {
  142. dfs_lock();
  143. if (mnt->dev_id == dev_id)
  144. {
  145. dfs_unlock();
  146. return mnt;
  147. }
  148. ret = _dfs_mnt_dev_lookup(mnt, dev_id);
  149. dfs_unlock();
  150. }
  151. return ret;
  152. }
  153. /**
  154. * this function will return the file system mounted on specified path.
  155. *
  156. * @param path the specified path string.
  157. *
  158. * @return the found file system or NULL if no file system mounted on
  159. * specified path
  160. */
  161. struct dfs_mnt *dfs_mnt_lookup(const char *fullpath)
  162. {
  163. struct dfs_mnt *mnt = _root_mnt;
  164. struct dfs_mnt *iter = RT_NULL;
  165. if (mnt)
  166. {
  167. int mnt_len = rt_strlen(mnt->fullpath);
  168. dfs_lock();
  169. if ((strncmp(mnt->fullpath, fullpath, mnt_len) == 0) &&
  170. (mnt_len == 1 || (fullpath[mnt_len] == '\0') || (fullpath[mnt_len] == '/')))
  171. {
  172. while (!rt_list_isempty(&mnt->child))
  173. {
  174. rt_list_for_each_entry(iter, &mnt->child, sibling)
  175. {
  176. mnt_len = rt_strlen(iter->fullpath);
  177. if ((strncmp(iter->fullpath, fullpath, mnt_len) == 0) &&
  178. ((fullpath[mnt_len] == '\0') || (fullpath[mnt_len] == '/')))
  179. {
  180. mnt = iter;
  181. break;
  182. }
  183. }
  184. if (mnt != iter) break;
  185. }
  186. }
  187. else
  188. {
  189. mnt = RT_NULL;
  190. }
  191. dfs_unlock();
  192. if (mnt)
  193. {
  194. LOG_D("mnt_lookup: %s path @ mount point %p", fullpath, mnt);
  195. DLOG(note, "mnt", "found mnt(%s)", mnt->fs_ops->name);
  196. }
  197. }
  198. return mnt;
  199. }
  200. struct dfs_mnt* dfs_mnt_ref(struct dfs_mnt* mnt)
  201. {
  202. if (mnt)
  203. {
  204. rt_atomic_add(&(mnt->ref_count), 1);
  205. DLOG(note, "mnt", "mnt(%s),ref_count=%d", mnt->fs_ops->name, rt_atomic_load(&(mnt->ref_count)));
  206. }
  207. return mnt;
  208. }
  209. int dfs_mnt_unref(struct dfs_mnt* mnt)
  210. {
  211. rt_err_t ret = RT_EOK;
  212. if (mnt)
  213. {
  214. rt_atomic_sub(&(mnt->ref_count), 1);
  215. if (rt_atomic_load(&(mnt->ref_count)) == 0)
  216. {
  217. dfs_lock();
  218. if (mnt->flags & MNT_IS_UMOUNT)
  219. {
  220. mnt->fs_ops->umount(mnt);
  221. }
  222. /* free full path */
  223. rt_free(mnt->fullpath);
  224. mnt->fullpath = RT_NULL;
  225. /* destroy self and the ref_count should be 0 */
  226. DLOG(msg, "mnt", "mnt", DLOG_MSG, "free mnt(%s)", mnt->fs_ops->name);
  227. rt_free(mnt);
  228. dfs_unlock();
  229. }
  230. else
  231. {
  232. DLOG(note, "mnt", "mnt(%s),ref_count=%d", mnt->fs_ops->name, rt_atomic_load(&(mnt->ref_count)));
  233. }
  234. }
  235. return ret;
  236. }
  237. int dfs_mnt_destroy(struct dfs_mnt* mnt)
  238. {
  239. rt_err_t ret = RT_EOK;
  240. if (mnt)
  241. {
  242. if (mnt->flags & MNT_IS_MOUNTED)
  243. {
  244. mnt->flags &= ~MNT_IS_MOUNTED;
  245. mnt->flags |= MNT_IS_UMOUNT;
  246. /* remote it from mnt list */
  247. if (mnt->flags & MNT_IS_ADDLIST)
  248. {
  249. dfs_mnt_remove(mnt);
  250. }
  251. }
  252. dfs_mnt_unref(mnt);
  253. }
  254. return ret;
  255. }
  256. static struct dfs_mnt* _dfs_mnt_foreach(struct dfs_mnt *mnt, struct dfs_mnt* (*func)(struct dfs_mnt *mnt, void *parameter), void *parameter)
  257. {
  258. struct dfs_mnt *iter, *ret = NULL;
  259. if (mnt)
  260. {
  261. ret = func(mnt, parameter);
  262. if (ret == RT_NULL)
  263. {
  264. if (!rt_list_isempty(&mnt->child))
  265. {
  266. /* for each in mount point list */
  267. rt_list_for_each_entry(iter, &mnt->child, sibling)
  268. {
  269. ret = _dfs_mnt_foreach(iter, func, parameter);
  270. if (ret != RT_NULL)
  271. {
  272. break;
  273. }
  274. }
  275. }
  276. }
  277. }
  278. else
  279. {
  280. ret = RT_NULL;
  281. }
  282. return ret;
  283. }
  284. static struct dfs_mnt* _mnt_cmp_devid(struct dfs_mnt *mnt, void *device)
  285. {
  286. struct dfs_mnt *ret = RT_NULL;
  287. struct rt_device *dev = (struct rt_device*)device;
  288. if (dev && mnt)
  289. {
  290. if (mnt->dev_id == dev)
  291. {
  292. ret = mnt;
  293. }
  294. }
  295. return ret;
  296. }
  297. /**
  298. * this function will return the mounted path for specified device.
  299. *
  300. * @param device the device object which is mounted.
  301. *
  302. * @return the mounted path or NULL if none device mounted.
  303. */
  304. const char *dfs_mnt_get_mounted_path(struct rt_device *device)
  305. {
  306. const char* path = RT_NULL;
  307. if (_root_mnt)
  308. {
  309. struct dfs_mnt* mnt;
  310. dfs_lock();
  311. mnt = _dfs_mnt_foreach(_root_mnt, _mnt_cmp_devid, device);
  312. dfs_unlock();
  313. if (mnt) path = mnt->fullpath;
  314. }
  315. return path;
  316. }
  317. static struct dfs_mnt* _mnt_dump(struct dfs_mnt *mnt, void *parameter)
  318. {
  319. if (mnt)
  320. {
  321. if (mnt->dev_id)
  322. {
  323. rt_kprintf("%-10s %-6s %-10s %d\n",
  324. mnt->fs_ops->name, mnt->dev_id->parent.name, mnt->fullpath, rt_atomic_load(&(mnt->ref_count)));
  325. }
  326. else
  327. {
  328. rt_kprintf("%-10s (NULL) %-10s %d\n",
  329. mnt->fs_ops->name, mnt->fullpath, rt_atomic_load(&(mnt->ref_count)));
  330. }
  331. }
  332. return RT_NULL;
  333. }
  334. static struct dfs_mnt* _mnt_cmp_path(struct dfs_mnt* mnt, void *parameter)
  335. {
  336. const char* fullpath = (const char*)parameter;
  337. struct dfs_mnt *ret = RT_NULL;
  338. if (strncmp(mnt->fullpath, fullpath, rt_strlen(fullpath)) == 0)
  339. {
  340. ret = mnt;
  341. }
  342. return ret;
  343. }
  344. rt_bool_t dfs_mnt_has_child_mnt(struct dfs_mnt *mnt, const char* fullpath)
  345. {
  346. int ret = RT_FALSE;
  347. if (mnt && fullpath)
  348. {
  349. struct dfs_mnt *m = RT_NULL;
  350. dfs_lock();
  351. m = _dfs_mnt_foreach(mnt, _mnt_cmp_path, (void*)fullpath);
  352. dfs_unlock();
  353. if (m)
  354. {
  355. ret = RT_TRUE;
  356. }
  357. }
  358. return ret;
  359. }
  360. int dfs_mnt_list(struct dfs_mnt *mnt)
  361. {
  362. if (!mnt) mnt = _root_mnt;
  363. /* lock file system */
  364. dfs_lock();
  365. _dfs_mnt_foreach(mnt, _mnt_dump, RT_NULL);
  366. /* unlock file system */
  367. dfs_unlock();
  368. return 0;
  369. }
  370. int dfs_mnt_foreach(struct dfs_mnt* (*func)(struct dfs_mnt *mnt, void *parameter), void *parameter)
  371. {
  372. /* lock file system */
  373. dfs_lock();
  374. _dfs_mnt_foreach(_root_mnt, func, parameter);
  375. /* unlock file system */
  376. dfs_unlock();
  377. return 0;
  378. }