dfs_tmpfs.c 17 KB

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