dfs_tmpfs.c 15 KB

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