dfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2005-02-22 Bernard The first version.
  9. * 2017-12-11 Bernard Use rt_free to instead of free in fd_is_open().
  10. * 2018-03-20 Heyuanjie dynamic allocation FD
  11. */
  12. #include <dfs.h>
  13. #include <dfs_fs.h>
  14. #include <dfs_file.h>
  15. #include "dfs_private.h"
  16. #ifdef RT_USING_LWP
  17. #include <lwp.h>
  18. #endif
  19. /* Global variables */
  20. const struct dfs_filesystem_ops *filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
  21. struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];
  22. /* device filesystem lock */
  23. static struct rt_mutex fslock;
  24. #ifdef DFS_USING_WORKDIR
  25. char working_directory[DFS_PATH_MAX] = {"/"};
  26. #endif
  27. static struct dfs_fdtable _fdtab;
  28. static int fd_alloc(struct dfs_fdtable *fdt, int startfd);
  29. /**
  30. * @addtogroup DFS
  31. */
  32. /*@{*/
  33. /**
  34. * this function will initialize device file system.
  35. */
  36. int dfs_init(void)
  37. {
  38. static rt_bool_t init_ok = RT_FALSE;
  39. if (init_ok)
  40. {
  41. rt_kprintf("dfs already init.\n");
  42. return 0;
  43. }
  44. /* clear filesystem operations table */
  45. memset((void *)filesystem_operation_table, 0, sizeof(filesystem_operation_table));
  46. /* clear filesystem table */
  47. memset(filesystem_table, 0, sizeof(filesystem_table));
  48. /* clean fd table */
  49. memset(&_fdtab, 0, sizeof(_fdtab));
  50. /* create device filesystem lock */
  51. rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);
  52. #ifdef DFS_USING_WORKDIR
  53. /* set current working directory */
  54. memset(working_directory, 0, sizeof(working_directory));
  55. working_directory[0] = '/';
  56. #endif
  57. #ifdef RT_USING_DFS_DEVFS
  58. {
  59. extern int devfs_init(void);
  60. /* if enable devfs, initialize and mount it as soon as possible */
  61. devfs_init();
  62. dfs_mount(NULL, "/dev", "devfs", 0, 0);
  63. }
  64. #endif
  65. init_ok = RT_TRUE;
  66. return 0;
  67. }
  68. INIT_PREV_EXPORT(dfs_init);
  69. /**
  70. * this function will lock device file system.
  71. *
  72. * @note please don't invoke it on ISR.
  73. */
  74. void dfs_lock(void)
  75. {
  76. rt_err_t result = -RT_EBUSY;
  77. while (result == -RT_EBUSY)
  78. {
  79. result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
  80. }
  81. if (result != RT_EOK)
  82. {
  83. RT_ASSERT(0);
  84. }
  85. }
  86. /**
  87. * this function will lock device file system.
  88. *
  89. * @note please don't invoke it on ISR.
  90. */
  91. void dfs_unlock(void)
  92. {
  93. rt_mutex_release(&fslock);
  94. }
  95. static int fd_alloc(struct dfs_fdtable *fdt, int startfd)
  96. {
  97. int idx;
  98. /* find an empty fd entry */
  99. for (idx = startfd; idx < fdt->maxfd; idx++)
  100. {
  101. if (fdt->fds[idx] == RT_NULL)
  102. break;
  103. if (fdt->fds[idx]->ref_count == 0)
  104. break;
  105. }
  106. /* allocate a larger FD container */
  107. if (idx == fdt->maxfd && fdt->maxfd < DFS_FD_MAX)
  108. {
  109. int cnt, index;
  110. struct dfs_fd **fds;
  111. /* increase the number of FD with 4 step length */
  112. cnt = fdt->maxfd + 4;
  113. cnt = cnt > DFS_FD_MAX? DFS_FD_MAX : cnt;
  114. fds = rt_realloc(fdt->fds, cnt * sizeof(struct dfs_fd *));
  115. if (fds == NULL) goto __exit; /* return fdt->maxfd */
  116. /* clean the new allocated fds */
  117. for (index = fdt->maxfd; index < cnt; index ++)
  118. {
  119. fds[index] = NULL;
  120. }
  121. fdt->fds = fds;
  122. fdt->maxfd = cnt;
  123. }
  124. /* allocate 'struct dfs_fd' */
  125. if (idx < fdt->maxfd &&fdt->fds[idx] == RT_NULL)
  126. {
  127. fdt->fds[idx] = rt_calloc(1, sizeof(struct dfs_fd));
  128. if (fdt->fds[idx] == RT_NULL)
  129. idx = fdt->maxfd;
  130. }
  131. __exit:
  132. return idx;
  133. }
  134. /**
  135. * @ingroup Fd
  136. * This function will allocate a file descriptor.
  137. *
  138. * @return -1 on failed or the allocated file descriptor.
  139. */
  140. int fd_new(void)
  141. {
  142. struct dfs_fd *d;
  143. int idx;
  144. struct dfs_fdtable *fdt;
  145. fdt = dfs_fdtable_get();
  146. /* lock filesystem */
  147. dfs_lock();
  148. /* find an empty fd entry */
  149. idx = fd_alloc(fdt, 0);
  150. /* can't find an empty fd entry */
  151. if (idx == fdt->maxfd)
  152. {
  153. idx = -(1 + DFS_FD_OFFSET);
  154. dbg_log(DBG_ERROR, "DFS fd new is failed! Could not found an empty fd entry.");
  155. goto __result;
  156. }
  157. d = fdt->fds[idx];
  158. d->ref_count = 1;
  159. d->magic = DFS_FD_MAGIC;
  160. __result:
  161. dfs_unlock();
  162. return idx + DFS_FD_OFFSET;
  163. }
  164. /**
  165. * @ingroup Fd
  166. *
  167. * This function will return a file descriptor structure according to file
  168. * descriptor.
  169. *
  170. * @return NULL on on this file descriptor or the file descriptor structure
  171. * pointer.
  172. */
  173. struct dfs_fd *fd_get(int fd)
  174. {
  175. struct dfs_fd *d;
  176. struct dfs_fdtable *fdt;
  177. fdt = dfs_fdtable_get();
  178. fd = fd - DFS_FD_OFFSET;
  179. if (fd < 0 || fd >= fdt->maxfd)
  180. return NULL;
  181. dfs_lock();
  182. d = fdt->fds[fd];
  183. /* check dfs_fd valid or not */
  184. if (d->magic != DFS_FD_MAGIC)
  185. {
  186. dfs_unlock();
  187. return NULL;
  188. }
  189. /* increase the reference count */
  190. d->ref_count ++;
  191. dfs_unlock();
  192. return d;
  193. }
  194. /**
  195. * @ingroup Fd
  196. *
  197. * This function will put the file descriptor.
  198. */
  199. void fd_put(struct dfs_fd *fd)
  200. {
  201. RT_ASSERT(fd != NULL);
  202. dfs_lock();
  203. fd->ref_count --;
  204. /* clear this fd entry */
  205. if (fd->ref_count == 0)
  206. {
  207. int index;
  208. struct dfs_fdtable *fdt;
  209. fdt = dfs_fdtable_get();
  210. for (index = 0; index < fdt->maxfd; index ++)
  211. {
  212. if (fdt->fds[index] == fd)
  213. {
  214. rt_free(fd);
  215. fdt->fds[index] = 0;
  216. break;
  217. }
  218. }
  219. }
  220. dfs_unlock();
  221. }
  222. /**
  223. * @ingroup Fd
  224. *
  225. * This function will return whether this file has been opend.
  226. *
  227. * @param pathname the file path name.
  228. *
  229. * @return 0 on file has been open successfully, -1 on open failed.
  230. */
  231. int fd_is_open(const char *pathname)
  232. {
  233. char *fullpath;
  234. unsigned int index;
  235. struct dfs_filesystem *fs;
  236. struct dfs_fd *fd;
  237. struct dfs_fdtable *fdt;
  238. fdt = dfs_fdtable_get();
  239. fullpath = dfs_normalize_path(NULL, pathname);
  240. if (fullpath != NULL)
  241. {
  242. char *mountpath;
  243. fs = dfs_filesystem_lookup(fullpath);
  244. if (fs == NULL)
  245. {
  246. /* can't find mounted file system */
  247. rt_free(fullpath);
  248. return -1;
  249. }
  250. /* get file path name under mounted file system */
  251. if (fs->path[0] == '/' && fs->path[1] == '\0')
  252. mountpath = fullpath;
  253. else
  254. mountpath = fullpath + strlen(fs->path);
  255. dfs_lock();
  256. for (index = 0; index < fdt->maxfd; index++)
  257. {
  258. fd = fdt->fds[index];
  259. if (fd == NULL || fd->fops == NULL || fd->path == NULL) continue;
  260. if (fd->fops == fs->ops->fops && strcmp(fd->path, mountpath) == 0)
  261. {
  262. /* found file in file descriptor table */
  263. rt_free(fullpath);
  264. dfs_unlock();
  265. return 0;
  266. }
  267. }
  268. dfs_unlock();
  269. rt_free(fullpath);
  270. }
  271. return -1;
  272. }
  273. /**
  274. * this function will return a sub-path name under directory.
  275. *
  276. * @param directory the parent directory.
  277. * @param filename the filename.
  278. *
  279. * @return the subdir pointer in filename
  280. */
  281. const char *dfs_subdir(const char *directory, const char *filename)
  282. {
  283. const char *dir;
  284. if (strlen(directory) == strlen(filename)) /* it's a same path */
  285. return NULL;
  286. dir = filename + strlen(directory);
  287. if ((*dir != '/') && (dir != filename))
  288. {
  289. dir --;
  290. }
  291. return dir;
  292. }
  293. RTM_EXPORT(dfs_subdir);
  294. /**
  295. * this function will normalize a path according to specified parent directory
  296. * and file name.
  297. *
  298. * @param directory the parent path
  299. * @param filename the file name
  300. *
  301. * @return the built full file path (absolute path)
  302. */
  303. char *dfs_normalize_path(const char *directory, const char *filename)
  304. {
  305. char *fullpath;
  306. char *dst0, *dst, *src;
  307. /* check parameters */
  308. RT_ASSERT(filename != NULL);
  309. #ifdef DFS_USING_WORKDIR
  310. if (directory == NULL) /* shall use working directory */
  311. directory = &working_directory[0];
  312. #else
  313. if ((directory == NULL) && (filename[0] != '/'))
  314. {
  315. rt_kprintf(NO_WORKING_DIR);
  316. return NULL;
  317. }
  318. #endif
  319. if (filename[0] != '/') /* it's a absolute path, use it directly */
  320. {
  321. fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);
  322. if (fullpath == NULL)
  323. return NULL;
  324. /* join path and file name */
  325. rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2,
  326. "%s/%s", directory, filename);
  327. }
  328. else
  329. {
  330. fullpath = rt_strdup(filename); /* copy string */
  331. if (fullpath == NULL)
  332. return NULL;
  333. }
  334. src = fullpath;
  335. dst = fullpath;
  336. dst0 = dst;
  337. while (1)
  338. {
  339. char c = *src;
  340. if (c == '.')
  341. {
  342. if (!src[1]) src ++; /* '.' and ends */
  343. else if (src[1] == '/')
  344. {
  345. /* './' case */
  346. src += 2;
  347. while ((*src == '/') && (*src != '\0'))
  348. src ++;
  349. continue;
  350. }
  351. else if (src[1] == '.')
  352. {
  353. if (!src[2])
  354. {
  355. /* '..' and ends case */
  356. src += 2;
  357. goto up_one;
  358. }
  359. else if (src[2] == '/')
  360. {
  361. /* '../' case */
  362. src += 3;
  363. while ((*src == '/') && (*src != '\0'))
  364. src ++;
  365. goto up_one;
  366. }
  367. }
  368. }
  369. /* copy up the next '/' and erase all '/' */
  370. while ((c = *src++) != '\0' && c != '/')
  371. *dst ++ = c;
  372. if (c == '/')
  373. {
  374. *dst ++ = '/';
  375. while (c == '/')
  376. c = *src++;
  377. src --;
  378. }
  379. else if (!c)
  380. break;
  381. continue;
  382. up_one:
  383. dst --;
  384. if (dst < dst0)
  385. {
  386. rt_free(fullpath);
  387. return NULL;
  388. }
  389. while (dst0 < dst && dst[-1] != '/')
  390. dst --;
  391. }
  392. *dst = '\0';
  393. /* remove '/' in the end of path if exist */
  394. dst --;
  395. if ((dst != fullpath) && (*dst == '/'))
  396. *dst = '\0';
  397. /* final check fullpath is not empty, for the special path of lwext "/.." */
  398. if ('\0' == fullpath[0])
  399. {
  400. fullpath[0] = '/';
  401. fullpath[1] = '\0';
  402. }
  403. return fullpath;
  404. }
  405. RTM_EXPORT(dfs_normalize_path);
  406. /**
  407. * This function will get the file descriptor table of current process.
  408. */
  409. struct dfs_fdtable* dfs_fdtable_get(void)
  410. {
  411. struct dfs_fdtable *fdt;
  412. #ifdef RT_USING_LWP
  413. struct rt_lwp *lwp;
  414. lwp = (struct rt_lwp *)rt_thread_self()->lwp;
  415. if (lwp)
  416. fdt = &lwp->fdt;
  417. else
  418. fdt = &_fdtab;
  419. #else
  420. fdt = &_fdtab;
  421. #endif
  422. return fdt;
  423. }
  424. #ifdef RT_USING_FINSH
  425. #include <finsh.h>
  426. int list_fd(void)
  427. {
  428. int index;
  429. struct dfs_fdtable *fd_table;
  430. fd_table = dfs_fdtable_get();
  431. if (!fd_table) return -1;
  432. rt_enter_critical();
  433. rt_kprintf("fd type ref magic path\n");
  434. rt_kprintf("-- ------ --- ----- ------\n");
  435. for (index = 0; index < fd_table->maxfd; index ++)
  436. {
  437. struct dfs_fd *fd = fd_table->fds[index];
  438. if (fd && fd->fops)
  439. {
  440. rt_kprintf("%2d ", index);
  441. if (fd->type == FT_DIRECTORY) rt_kprintf("%-7.7s ", "dir");
  442. else if (fd->type == FT_REGULAR) rt_kprintf("%-7.7s ", "file");
  443. else if (fd->type == FT_SOCKET) rt_kprintf("%-7.7s ", "socket");
  444. else if (fd->type == FT_USER) rt_kprintf("%-7.7s ", "user");
  445. else rt_kprintf("%-8.8s ", "unknown");
  446. rt_kprintf("%3d ", fd->ref_count);
  447. rt_kprintf("%04x ", fd->magic);
  448. if (fd->path)
  449. {
  450. rt_kprintf("%s\n", fd->path);
  451. }
  452. else
  453. {
  454. rt_kprintf("\n");
  455. }
  456. }
  457. }
  458. rt_exit_critical();
  459. return 0;
  460. }
  461. MSH_CMD_EXPORT(list_fd, list file descriptor);
  462. #endif
  463. /*@}*/