1
0

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