dfs_tmpfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  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 int 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 - file->fpos)
  239. length = count;
  240. else
  241. length = file->vnode->size - file->fpos;
  242. if (length > 0)
  243. memcpy(buf, &(d_file->data[file->fpos]), length);
  244. /* update file current position */
  245. file->fpos += length;
  246. *pos = file->fpos;
  247. return length;
  248. }
  249. static int dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
  250. {
  251. struct tmpfs_file *d_file;
  252. struct tmpfs_sb *superblock;
  253. d_file = (struct tmpfs_file *)file->vnode->data;
  254. RT_ASSERT(d_file != NULL);
  255. superblock = d_file->sb;
  256. RT_ASSERT(superblock != NULL);
  257. if (count + file->fpos > file->vnode->size)
  258. {
  259. rt_uint8_t *ptr;
  260. ptr = rt_realloc(d_file->data, file->fpos + count);
  261. if (ptr == NULL)
  262. {
  263. rt_set_errno(-ENOMEM);
  264. return 0;
  265. }
  266. superblock->df_size += (file->fpos - d_file->size + count);
  267. /* update d_file and file size */
  268. d_file->data = ptr;
  269. d_file->size = file->fpos + count;
  270. file->vnode->size = d_file->size;
  271. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  272. }
  273. if (count > 0)
  274. memcpy(d_file->data + file->fpos, buf, count);
  275. /* update file current position */
  276. file->fpos += count;
  277. *pos = file->fpos;
  278. return count;
  279. }
  280. static int dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
  281. {
  282. if (offset <= (off_t)file->vnode->size)
  283. {
  284. file->fpos = offset;
  285. return file->fpos;
  286. }
  287. return -EIO;
  288. }
  289. int dfs_tmpfs_close(struct dfs_file *file)
  290. {
  291. RT_ASSERT(file->vnode->ref_count > 0);
  292. return RT_EOK;
  293. }
  294. int dfs_tmpfs_open(struct dfs_file *file)
  295. {
  296. struct tmpfs_file *d_file;
  297. d_file = (struct tmpfs_file *)file->vnode->data;
  298. RT_ASSERT(d_file != RT_NULL);
  299. /* Creates a new file.
  300. * If the file is existing, it is truncated and overwritten.
  301. */
  302. if (file->flags & O_TRUNC)
  303. {
  304. d_file->size = 0;
  305. file->vnode->size = d_file->size;
  306. file->fpos = file->vnode->size;
  307. if (d_file->data != NULL)
  308. {
  309. /* ToDo: fix for rt-smart. */
  310. rt_free(d_file->data);
  311. d_file->data = NULL;
  312. }
  313. }
  314. if (file->flags & O_APPEND)
  315. {
  316. file->fpos = file->vnode->size;
  317. }
  318. else
  319. {
  320. file->fpos = 0;
  321. }
  322. return 0;
  323. }
  324. static int dfs_tmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
  325. {
  326. rt_size_t size;
  327. struct tmpfs_file *d_file;
  328. struct tmpfs_sb *superblock;
  329. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  330. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  331. if (d_file == NULL)
  332. return -ENOENT;
  333. st->st_dev = (dev_t)(size_t)(dentry->mnt->dev_id);
  334. st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
  335. st->st_mode = S_IFREG | S_IRUSR | S_IRGRP | S_IROTH |
  336. S_IWUSR | S_IWGRP | S_IWOTH;
  337. if (d_file->type == TMPFS_TYPE_DIR)
  338. {
  339. st->st_mode &= ~S_IFREG;
  340. st->st_mode |= S_IFDIR | S_IXUSR | S_IXGRP | S_IXOTH;
  341. }
  342. st->st_size = d_file->size;
  343. st->st_mtime = 0;
  344. return RT_EOK;
  345. }
  346. int dfs_tmpfs_getdents(struct dfs_file *file,
  347. struct dirent *dirp,
  348. uint32_t count)
  349. {
  350. rt_size_t index, end;
  351. struct dirent *d;
  352. struct tmpfs_file *d_file, *n_file;
  353. rt_list_t *list;
  354. struct tmpfs_sb *superblock;
  355. d_file = (struct tmpfs_file *)file->vnode->data;
  356. superblock = d_file->sb;
  357. RT_ASSERT(superblock != RT_NULL);
  358. /* make integer count */
  359. count = (count / sizeof(struct dirent));
  360. if (count == 0)
  361. return -EINVAL;
  362. end = file->fpos + count;
  363. index = 0;
  364. count = 0;
  365. rt_list_for_each(list, &d_file->subdirs)
  366. {
  367. n_file = rt_list_entry(list, struct tmpfs_file, sibling);
  368. if (index >= (rt_size_t)file->fpos)
  369. {
  370. d = dirp + count;
  371. if (d_file->type == TMPFS_TYPE_FILE)
  372. {
  373. d->d_type = DT_REG;
  374. }
  375. if (d_file->type == TMPFS_TYPE_DIR)
  376. {
  377. d->d_type = DT_DIR;
  378. }
  379. d->d_namlen = RT_NAME_MAX;
  380. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  381. rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
  382. count += 1;
  383. file->fpos += 1;
  384. }
  385. index += 1;
  386. if (index >= end)
  387. {
  388. break;
  389. }
  390. }
  391. return count * sizeof(struct dirent);
  392. }
  393. int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
  394. {
  395. rt_size_t size;
  396. struct tmpfs_sb *superblock;
  397. struct tmpfs_file *d_file;
  398. RT_DEFINE_SPINLOCK(lock);
  399. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  400. RT_ASSERT(superblock != NULL);
  401. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  402. if (d_file == NULL)
  403. return -ENOENT;
  404. rt_hw_spin_lock(&lock);
  405. rt_list_remove(&(d_file->sibling));
  406. rt_hw_spin_unlock(&lock);
  407. if (d_file->data != NULL)
  408. rt_free(d_file->data);
  409. rt_free(d_file);
  410. return RT_EOK;
  411. }
  412. int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
  413. {
  414. struct tmpfs_file *d_file, *p_file;
  415. struct tmpfs_sb *superblock;
  416. rt_size_t size;
  417. char parent_path[DFS_PATH_MAX], file_name[TMPFS_NAME_MAX];
  418. RT_DEFINE_SPINLOCK(lock);
  419. superblock = (struct tmpfs_sb *)old_dentry->mnt->data;
  420. RT_ASSERT(superblock != NULL);
  421. d_file = dfs_tmpfs_lookup(superblock, new_dentry->pathname, &size);
  422. if (d_file != NULL)
  423. return -EEXIST;
  424. d_file = dfs_tmpfs_lookup(superblock, old_dentry->pathname, &size);
  425. if (d_file == NULL)
  426. return -ENOENT;
  427. /* find parent file */
  428. _path_separate(new_dentry->pathname, parent_path, file_name);
  429. if (file_name[0] == '\0') /* it's root dir */
  430. return -ENOENT;
  431. /* open parent directory */
  432. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  433. RT_ASSERT(p_file != NULL);
  434. rt_hw_spin_lock(&lock);
  435. rt_list_remove(&(d_file->sibling));
  436. rt_hw_spin_unlock(&lock);
  437. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  438. rt_hw_spin_lock(&lock);
  439. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  440. rt_hw_spin_unlock(&lock);
  441. return RT_EOK;
  442. }
  443. static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
  444. {
  445. struct dfs_vnode *vnode = RT_NULL;
  446. rt_size_t size;
  447. struct tmpfs_sb *superblock;
  448. struct tmpfs_file *d_file;
  449. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  450. {
  451. return NULL;
  452. }
  453. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  454. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  455. if (d_file)
  456. {
  457. vnode = dfs_vnode_create();
  458. if (vnode)
  459. {
  460. if (d_file->type == TMPFS_TYPE_DIR)
  461. {
  462. vnode->mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
  463. vnode->type = FT_DIRECTORY;
  464. }
  465. else
  466. {
  467. vnode->mode = S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR;
  468. vnode->type = FT_REGULAR;
  469. }
  470. vnode->mnt = dentry->mnt;
  471. vnode->data = d_file;
  472. vnode->size = d_file->size;
  473. }
  474. }
  475. return vnode;
  476. }
  477. static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  478. {
  479. struct dfs_vnode *vnode = RT_NULL;
  480. rt_size_t size;
  481. struct tmpfs_sb *superblock;
  482. struct tmpfs_file *d_file, *p_file;
  483. char parent_path[DFS_PATH_MAX], file_name[TMPFS_NAME_MAX];
  484. RT_DEFINE_SPINLOCK(lock);
  485. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  486. {
  487. return NULL;
  488. }
  489. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  490. vnode = dfs_vnode_create();
  491. if (vnode)
  492. {
  493. /* find parent file */
  494. _path_separate(dentry->pathname, parent_path, file_name);
  495. if (file_name[0] == '\0') /* it's root dir */
  496. {
  497. dfs_vnode_destroy(vnode);
  498. return NULL;
  499. }
  500. /* open parent directory */
  501. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  502. if (p_file == NULL)
  503. {
  504. dfs_vnode_destroy(vnode);
  505. return NULL;
  506. }
  507. /* create a file entry */
  508. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  509. if (d_file == NULL)
  510. {
  511. dfs_vnode_destroy(vnode);
  512. return NULL;
  513. }
  514. superblock->df_size += sizeof(struct tmpfs_file);
  515. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  516. rt_list_init(&(d_file->subdirs));
  517. rt_list_init(&(d_file->sibling));
  518. d_file->data = NULL;
  519. d_file->size = 0;
  520. d_file->sb = superblock;
  521. if (type == FT_DIRECTORY)
  522. {
  523. d_file->type = TMPFS_TYPE_DIR;
  524. vnode->mode = S_IFDIR | mode;
  525. vnode->type = FT_DIRECTORY;
  526. }
  527. else
  528. {
  529. d_file->type = TMPFS_TYPE_FILE;
  530. vnode->mode = S_IFREG | mode;
  531. vnode->type = FT_REGULAR;
  532. }
  533. rt_hw_spin_lock(&lock);
  534. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  535. rt_hw_spin_unlock(&lock);
  536. vnode->mnt = dentry->mnt;
  537. vnode->data = d_file;
  538. vnode->size = d_file->size;
  539. }
  540. return vnode;
  541. }
  542. static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
  543. {
  544. /* nothing to be freed */
  545. if (vnode && vnode->ref_count <= 1)
  546. {
  547. vnode->data = NULL;
  548. }
  549. return 0;
  550. }
  551. static const struct dfs_file_ops _tmp_fops =
  552. {
  553. .open = dfs_tmpfs_open,
  554. .close = dfs_tmpfs_close,
  555. .ioctl = dfs_tmpfs_ioctl,
  556. .read = dfs_tmpfs_read,
  557. .write = dfs_tmpfs_write,
  558. .lseek = dfs_tmpfs_lseek,
  559. .getdents = dfs_tmpfs_getdents,
  560. };
  561. static const struct dfs_filesystem_ops _tmpfs_ops =
  562. {
  563. .name = "tmp",
  564. .flags = DFS_FS_FLAG_DEFAULT,
  565. .default_fops = &_tmp_fops,
  566. .mount = dfs_tmpfs_mount,
  567. .umount = dfs_tmpfs_unmount,
  568. .statfs = dfs_tmpfs_statfs,
  569. .unlink = dfs_tmpfs_unlink,
  570. .stat = dfs_tmpfs_stat,
  571. .rename = dfs_tmpfs_rename,
  572. .lookup = _dfs_tmpfs_lookup,
  573. .create_vnode = dfs_tmpfs_create_vnode,
  574. .free_vnode = dfs_tmpfs_free_vnode
  575. };
  576. static struct dfs_filesystem_type _tmpfs =
  577. {
  578. .fs_ops = &_tmpfs_ops,
  579. };
  580. int dfs_tmpfs_init(void)
  581. {
  582. /* register tmp file system */
  583. dfs_register(&_tmpfs);
  584. return 0;
  585. }
  586. INIT_COMPONENT_EXPORT(dfs_tmpfs_init);