dfs_tmpfs.c 16 KB

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