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