dfs_tmpfs.c 15 KB

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