dfs_tmpfs.c 15 KB

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