dfs_tmpfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. * 2022-10-24 flybreak the first version
  9. * 2023-02-01 xqyjlj fix cannot open the same file repeatedly in 'w' mode
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include <dfs.h>
  14. #include <dfs_fs.h>
  15. #include <dfs_file.h>
  16. #ifdef RT_USING_SMART
  17. #include <lwp.h>
  18. #include <lwp_user_mm.h>
  19. #endif
  20. #include "dfs_tmpfs.h"
  21. #define DBG_TAG "tmpfs"
  22. #define DBG_LVL DBG_INFO
  23. #include <rtdbg.h>
  24. static int _tmpfs_path_validate(const char *path)
  25. {
  26. if (path == RT_NULL)
  27. {
  28. return -EINVAL;
  29. }
  30. if (path[0] != '/')
  31. {
  32. return -EINVAL;
  33. }
  34. return RT_EOK;
  35. }
  36. static int _path_separate(const char *path, char *parent_path, rt_size_t parent_size,
  37. char *file_name, rt_size_t file_size)
  38. {
  39. const char *path_p, *path_q;
  40. rt_size_t parent_len, file_len;
  41. int ret;
  42. ret = _tmpfs_path_validate(path);
  43. if (ret != RT_EOK)
  44. {
  45. return ret;
  46. }
  47. file_name[0] = '\0';
  48. parent_path[0] = '\0';
  49. path_p = path_q = &path[1];
  50. __next_dir:
  51. while (*path_q != '/' && *path_q != '\0')
  52. {
  53. path_q++;
  54. }
  55. if (path_q != path_p) /*sub dir*/
  56. {
  57. if (*path_q != '\0')
  58. {
  59. path_q++;
  60. path_p = path_q;
  61. goto __next_dir;
  62. }
  63. else /* Last level dir */
  64. {
  65. parent_len = path_p - path - 1;
  66. file_len = path_q - path_p;
  67. if ((parent_len + 1 > parent_size) || (file_len + 1 > file_size))
  68. {
  69. return -ENAMETOOLONG;
  70. }
  71. rt_memcpy(parent_path, path, parent_len);
  72. parent_path[parent_len] = '\0';
  73. rt_memcpy(file_name, path_p, file_len);
  74. file_name[file_len] = '\0';
  75. }
  76. }
  77. if (parent_path[0] == 0)
  78. {
  79. parent_path[0] = '/';
  80. parent_path[1] = '\0';
  81. }
  82. LOG_D("parent_path: %s", parent_path);
  83. LOG_D("file_name: %s", file_name);
  84. return 0;
  85. }
  86. static int _get_subdir(const char *path, char *name, rt_size_t name_size)
  87. {
  88. const char *subpath = path;
  89. rt_size_t name_len = 0;
  90. if (name_size == 0)
  91. {
  92. return -EINVAL;
  93. }
  94. while (*subpath == '/' && *subpath)
  95. subpath ++;
  96. while (*subpath != '/' && *subpath)
  97. {
  98. if (name_len + 1 >= name_size)
  99. {
  100. name[0] = '\0';
  101. return -ENAMETOOLONG;
  102. }
  103. *name = *subpath;
  104. name ++;
  105. subpath ++;
  106. name_len ++;
  107. }
  108. *name = '\0';
  109. return 0;
  110. }
  111. static int _free_subdir(struct tmpfs_file *dfile)
  112. {
  113. struct tmpfs_file *file;
  114. rt_list_t *list, *temp_list;
  115. struct tmpfs_sb *superblock;
  116. RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
  117. rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
  118. {
  119. file = rt_list_entry(list, struct tmpfs_file, sibling);
  120. if (file->type == TMPFS_TYPE_DIR)
  121. {
  122. _free_subdir(file);
  123. }
  124. if (file->data != NULL)
  125. {
  126. /* TODO: fix for rt-smart */
  127. rt_free(file->data);
  128. }
  129. superblock = file->sb;
  130. RT_ASSERT(superblock != NULL);
  131. rt_spin_lock(&superblock->lock);
  132. rt_list_remove(&(file->sibling));
  133. rt_spin_unlock(&superblock->lock);
  134. rt_free(file);
  135. }
  136. return 0;
  137. }
  138. int dfs_tmpfs_mount(struct dfs_filesystem *fs,
  139. unsigned long rwflag,
  140. const void *data)
  141. {
  142. struct tmpfs_sb *superblock;
  143. superblock = rt_calloc(1, sizeof(struct tmpfs_sb));
  144. if (superblock)
  145. {
  146. superblock->df_size = sizeof(struct tmpfs_sb);
  147. superblock->magic = TMPFS_MAGIC;
  148. rt_list_init(&superblock->sibling);
  149. superblock->root.name[0] = '/';
  150. superblock->root.sb = superblock;
  151. superblock->root.type = TMPFS_TYPE_DIR;
  152. rt_list_init(&superblock->root.sibling);
  153. rt_list_init(&superblock->root.subdirs);
  154. rt_spin_lock_init(&superblock->lock);
  155. fs->data = superblock;
  156. }
  157. else
  158. {
  159. return -1;
  160. }
  161. return RT_EOK;
  162. }
  163. int dfs_tmpfs_unmount(struct dfs_filesystem *fs)
  164. {
  165. struct tmpfs_sb *superblock;
  166. superblock = (struct tmpfs_sb *)fs->data;
  167. RT_ASSERT(superblock != NULL);
  168. _free_subdir(&(superblock->root));
  169. rt_free(superblock);
  170. fs->data = NULL;
  171. return RT_EOK;
  172. }
  173. int dfs_tmpfs_statfs(struct dfs_filesystem *fs, struct statfs *buf)
  174. {
  175. struct tmpfs_sb *superblock;
  176. superblock = (struct tmpfs_sb *)fs->data;
  177. RT_ASSERT(superblock != NULL);
  178. RT_ASSERT(buf != NULL);
  179. buf->f_bsize = 512;
  180. buf->f_blocks = (superblock->df_size + 511) / 512;
  181. buf->f_bfree = 1;
  182. buf->f_bavail = buf->f_bfree;
  183. return RT_EOK;
  184. }
  185. int dfs_tmpfs_ioctl(struct dfs_file *file, int cmd, void *args)
  186. {
  187. struct tmpfs_file *d_file;
  188. struct tmpfs_sb *superblock;
  189. d_file = (struct tmpfs_file *)file->vnode->data;
  190. RT_ASSERT(d_file != NULL);
  191. superblock = d_file->sb;
  192. RT_ASSERT(superblock != NULL);
  193. switch (cmd)
  194. {
  195. #ifdef RT_USING_SMART
  196. case RT_FIOMMAP2:
  197. {
  198. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
  199. if (mmap2)
  200. {
  201. if (mmap2->length > file->vnode->size)
  202. {
  203. return -RT_ENOMEM;
  204. }
  205. else if (mmap2->lwp == RT_NULL)
  206. return -RT_EINVAL;
  207. LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
  208. mmap2->ret = lwp_map_user_phy(mmap2->lwp, mmap2->addr, d_file->data, mmap2->length, 0);
  209. }
  210. return RT_EOK;
  211. break;
  212. }
  213. #endif
  214. default:
  215. break;
  216. }
  217. return -EIO;
  218. }
  219. struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
  220. const char *path,
  221. rt_size_t *size)
  222. {
  223. const char *subpath, *curpath, *filename = RT_NULL;
  224. char subdir_name[TMPFS_NAME_MAX];
  225. struct tmpfs_file *file, *curfile;
  226. rt_list_t *list;
  227. int ret;
  228. ret = _tmpfs_path_validate(path);
  229. if (ret != RT_EOK)
  230. {
  231. return RT_NULL;
  232. }
  233. subpath = path;
  234. while (*subpath == '/' && *subpath)
  235. subpath ++;
  236. if (! *subpath) /* is root directory */
  237. {
  238. *size = 0;
  239. return &(superblock->root);
  240. }
  241. curpath = subpath;
  242. curfile = &superblock->root;
  243. find_subpath:
  244. while (*subpath != '/' && *subpath)
  245. subpath ++;
  246. if (! *subpath) /* is last directory */
  247. filename = curpath;
  248. else
  249. subpath ++; /* skip '/' */
  250. memset(subdir_name, 0, TMPFS_NAME_MAX);
  251. if (_get_subdir(curpath, subdir_name, sizeof(subdir_name)) != 0)
  252. {
  253. return RT_NULL;
  254. }
  255. rt_spin_lock(&superblock->lock);
  256. rt_list_for_each(list, &curfile->subdirs)
  257. {
  258. file = rt_list_entry(list, struct tmpfs_file, sibling);
  259. if (filename) /* find file */
  260. {
  261. if (rt_strcmp(file->name, filename) == 0)
  262. {
  263. *size = file->size;
  264. rt_spin_unlock(&superblock->lock);
  265. return file;
  266. }
  267. }
  268. else if (rt_strcmp(file->name, subdir_name) == 0)
  269. {
  270. *size = file->size;
  271. curpath = subpath;
  272. curfile = file;
  273. rt_spin_unlock(&superblock->lock);
  274. goto find_subpath;
  275. }
  276. }
  277. rt_spin_unlock(&superblock->lock);
  278. /* not found */
  279. return NULL;
  280. }
  281. ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count)
  282. {
  283. rt_size_t length;
  284. struct tmpfs_file *d_file;
  285. d_file = (struct tmpfs_file *)file->vnode->data;
  286. RT_ASSERT(d_file != NULL);
  287. if (count < file->vnode->size - file->pos)
  288. length = count;
  289. else
  290. length = file->vnode->size - file->pos;
  291. if (length > 0)
  292. memcpy(buf, &(d_file->data[file->pos]), length);
  293. /* update file current position */
  294. file->pos += length;
  295. return length;
  296. }
  297. ssize_t dfs_tmpfs_write(struct dfs_file *fd, const void *buf, size_t count)
  298. {
  299. struct tmpfs_file *d_file;
  300. struct tmpfs_sb *superblock;
  301. d_file = (struct tmpfs_file *)fd->vnode->data;
  302. RT_ASSERT(d_file != NULL);
  303. superblock = d_file->sb;
  304. RT_ASSERT(superblock != NULL);
  305. if (count + fd->pos > fd->vnode->size)
  306. {
  307. rt_uint8_t *ptr;
  308. ptr = rt_realloc(d_file->data, fd->pos + count);
  309. if (ptr == NULL)
  310. {
  311. rt_set_errno(-ENOMEM);
  312. return 0;
  313. }
  314. superblock->df_size += (fd->pos - d_file->size + count);
  315. /* update d_file and file size */
  316. d_file->data = ptr;
  317. d_file->size = fd->pos + count;
  318. fd->vnode->size = d_file->size;
  319. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  320. }
  321. if (count > 0)
  322. memcpy(d_file->data + fd->pos, buf, count);
  323. /* update file current position */
  324. fd->pos += count;
  325. return count;
  326. }
  327. off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset)
  328. {
  329. if (offset <= (off_t)file->vnode->size)
  330. {
  331. file->pos = offset;
  332. return file->pos;
  333. }
  334. return -EIO;
  335. }
  336. int dfs_tmpfs_close(struct dfs_file *file)
  337. {
  338. RT_ASSERT(file->vnode->ref_count > 0);
  339. if (file->vnode->ref_count > 1)
  340. {
  341. return 0;
  342. }
  343. file->vnode->data = NULL;
  344. return RT_EOK;
  345. }
  346. int dfs_tmpfs_open(struct dfs_file *file)
  347. {
  348. int ret;
  349. rt_size_t size;
  350. struct tmpfs_sb *superblock;
  351. struct tmpfs_file *d_file, *p_file;
  352. struct dfs_filesystem *fs;
  353. char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX];
  354. RT_ASSERT(file->vnode->ref_count > 0);
  355. if (file->vnode->ref_count > 1)
  356. {
  357. if (file->vnode->type == FT_DIRECTORY
  358. && !(file->flags & O_DIRECTORY))
  359. {
  360. return -ENOENT;
  361. }
  362. file->pos = 0;
  363. return 0;
  364. }
  365. fs = file->vnode->fs;
  366. superblock = (struct tmpfs_sb *)fs->data;
  367. RT_ASSERT(superblock != NULL);
  368. ret = _tmpfs_path_validate(file->vnode->path);
  369. if (ret != RT_EOK)
  370. {
  371. return ret;
  372. }
  373. /* find file */
  374. d_file = dfs_tmpfs_lookup(superblock, file->vnode->path, &size);
  375. if (d_file == NULL && !(file->flags & O_CREAT))
  376. return -ENOENT;
  377. /* Creates a new file. */
  378. if (file->flags & O_CREAT)
  379. {
  380. if (d_file == NULL)
  381. {
  382. /* find parent file */
  383. ret = _path_separate(file->vnode->path, parent_path, sizeof(parent_path),
  384. file_name, sizeof(file_name));
  385. if (ret != RT_EOK)
  386. {
  387. return ret;
  388. }
  389. if (file_name[0] == '\0') /* it's root dir */
  390. return -ENOENT;
  391. /* open parent directory */
  392. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  393. if (p_file == NULL)
  394. return -ENOENT;
  395. /* create a file entry */
  396. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  397. if (d_file == NULL)
  398. {
  399. return -ENOMEM;
  400. }
  401. superblock->df_size += sizeof(struct tmpfs_file);
  402. rt_strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  403. d_file->name[TMPFS_NAME_MAX - 1] = '\0';
  404. rt_list_init(&(d_file->subdirs));
  405. rt_list_init(&(d_file->sibling));
  406. d_file->data = NULL;
  407. d_file->size = 0;
  408. d_file->sb = superblock;
  409. if (file->flags & O_DIRECTORY)
  410. {
  411. d_file->type = TMPFS_TYPE_DIR;
  412. }
  413. else
  414. {
  415. d_file->type = TMPFS_TYPE_FILE;
  416. }
  417. rt_spin_lock(&superblock->lock);
  418. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  419. rt_spin_unlock(&superblock->lock);
  420. }
  421. }
  422. /* Creates a new file.
  423. * If the file is existing, it is truncated and overwritten.
  424. */
  425. if (file->flags & O_TRUNC)
  426. {
  427. d_file->size = 0;
  428. if (d_file->data != NULL)
  429. {
  430. /* ToDo: fix for rt-smart. */
  431. rt_free(d_file->data);
  432. d_file->data = NULL;
  433. }
  434. }
  435. /* fill file */
  436. if (d_file->type == TMPFS_TYPE_DIR)
  437. {
  438. if (file->flags & O_DIRECTORY)
  439. file->vnode->type = FT_DIRECTORY;
  440. else
  441. return -ENOMEM;
  442. }
  443. else
  444. {
  445. if (file->flags & O_DIRECTORY)
  446. {
  447. return -ENOMEM;
  448. }
  449. file->vnode->type = FT_DEVICE;
  450. }
  451. file->vnode->data = d_file;
  452. file->vnode->size = d_file->size;
  453. if (file->flags & O_APPEND)
  454. {
  455. file->pos = file->vnode->size;
  456. }
  457. else
  458. {
  459. file->pos = 0;
  460. }
  461. return 0;
  462. }
  463. int dfs_tmpfs_stat(struct dfs_filesystem *fs,
  464. const char *path,
  465. struct stat *st)
  466. {
  467. rt_size_t size;
  468. struct tmpfs_file *d_file;
  469. struct tmpfs_sb *superblock;
  470. superblock = (struct tmpfs_sb *)fs->data;
  471. d_file = dfs_tmpfs_lookup(superblock, path, &size);
  472. if (d_file == NULL)
  473. return -ENOENT;
  474. st->st_dev = (dev_t)dfs_filesystem_lookup(fs->path);
  475. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  476. S_IWUSR | S_IWGRP | S_IWOTH;
  477. if (d_file->type == TMPFS_TYPE_DIR)
  478. {
  479. st->st_mode &= ~S_IFREG;
  480. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  481. }
  482. st->st_size = d_file->size;
  483. st->st_mtime = 0;
  484. return RT_EOK;
  485. }
  486. int dfs_tmpfs_getdents(struct dfs_file *file,
  487. struct dirent *dirp,
  488. uint32_t count)
  489. {
  490. rt_size_t index, end;
  491. struct dirent *d;
  492. struct tmpfs_file *d_file, *n_file;
  493. rt_list_t *list;
  494. struct tmpfs_sb *superblock;
  495. d_file = (struct tmpfs_file *)file->vnode->data;
  496. superblock = d_file->sb;
  497. RT_ASSERT(superblock != RT_NULL);
  498. /* make integer count */
  499. count = (count / sizeof(struct dirent));
  500. if (count == 0)
  501. return -EINVAL;
  502. end = file->pos + count;
  503. index = 0;
  504. count = 0;
  505. rt_list_for_each(list, &d_file->subdirs)
  506. {
  507. n_file = rt_list_entry(list, struct tmpfs_file, sibling);
  508. if (index >= (rt_size_t)file->pos)
  509. {
  510. d = dirp + count;
  511. if (d_file->type == TMPFS_TYPE_FILE)
  512. {
  513. d->d_type = DT_REG;
  514. }
  515. if (d_file->type == TMPFS_TYPE_DIR)
  516. {
  517. d->d_type = DT_DIR;
  518. }
  519. d->d_namlen = RT_NAME_MAX;
  520. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  521. rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
  522. count += 1;
  523. file->pos += 1;
  524. }
  525. index += 1;
  526. if (index >= end)
  527. {
  528. break;
  529. }
  530. }
  531. return count * sizeof(struct dirent);
  532. }
  533. int dfs_tmpfs_unlink(struct dfs_filesystem *fs, const char *path)
  534. {
  535. rt_size_t size;
  536. struct tmpfs_sb *superblock;
  537. struct tmpfs_file *d_file;
  538. superblock = (struct tmpfs_sb *)fs->data;
  539. RT_ASSERT(superblock != NULL);
  540. d_file = dfs_tmpfs_lookup(superblock, path, &size);
  541. if (d_file == NULL)
  542. return -ENOENT;
  543. rt_spin_lock(&superblock->lock);
  544. rt_list_remove(&(d_file->sibling));
  545. rt_spin_unlock(&superblock->lock);
  546. if (d_file->data != NULL)
  547. rt_free(d_file->data);
  548. rt_free(d_file);
  549. return RT_EOK;
  550. }
  551. int dfs_tmpfs_rename(struct dfs_filesystem *fs,
  552. const char *oldpath,
  553. const char *newpath)
  554. {
  555. int ret;
  556. struct tmpfs_file *d_file, *p_file;
  557. struct tmpfs_sb *superblock;
  558. rt_size_t size;
  559. char parent_path[DFS_PATH_MAX],file_name[TMPFS_NAME_MAX];
  560. superblock = (struct tmpfs_sb *)fs->data;
  561. RT_ASSERT(superblock != NULL);
  562. ret = _tmpfs_path_validate(newpath);
  563. if (ret != RT_EOK)
  564. {
  565. return ret;
  566. }
  567. ret = _tmpfs_path_validate(oldpath);
  568. if (ret != RT_EOK)
  569. {
  570. return ret;
  571. }
  572. d_file = dfs_tmpfs_lookup(superblock, newpath, &size);
  573. if (d_file != NULL)
  574. return -EEXIST;
  575. d_file = dfs_tmpfs_lookup(superblock, oldpath, &size);
  576. if (d_file == NULL)
  577. return -ENOENT;
  578. /* find parent file */
  579. ret = _path_separate(newpath, parent_path, sizeof(parent_path),
  580. file_name, sizeof(file_name));
  581. if (ret != RT_EOK)
  582. {
  583. return ret;
  584. }
  585. if (file_name[0] == '\0') /* it's root dir */
  586. return -ENOENT;
  587. /* open parent directory */
  588. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  589. RT_ASSERT(p_file != NULL);
  590. rt_spin_lock(&superblock->lock);
  591. rt_list_remove(&(d_file->sibling));
  592. rt_spin_unlock(&superblock->lock);
  593. rt_strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  594. d_file->name[TMPFS_NAME_MAX - 1] = '\0';
  595. rt_spin_lock(&superblock->lock);
  596. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  597. rt_spin_unlock(&superblock->lock);
  598. return RT_EOK;
  599. }
  600. static const struct dfs_file_ops _tmp_fops =
  601. {
  602. dfs_tmpfs_open,
  603. dfs_tmpfs_close,
  604. dfs_tmpfs_ioctl,
  605. dfs_tmpfs_read,
  606. dfs_tmpfs_write,
  607. NULL, /* flush */
  608. dfs_tmpfs_lseek,
  609. dfs_tmpfs_getdents,
  610. };
  611. static const struct dfs_filesystem_ops _tmpfs =
  612. {
  613. "tmp",
  614. DFS_FS_FLAG_DEFAULT,
  615. &_tmp_fops,
  616. dfs_tmpfs_mount,
  617. dfs_tmpfs_unmount,
  618. NULL, /* mkfs */
  619. dfs_tmpfs_statfs,
  620. dfs_tmpfs_unlink,
  621. dfs_tmpfs_stat,
  622. dfs_tmpfs_rename,
  623. };
  624. int dfs_tmpfs_init(void)
  625. {
  626. /* register tmp file system */
  627. dfs_register(&_tmpfs);
  628. return 0;
  629. }