dfs_tmpfs.c 15 KB

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