dfs_tmpfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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. * 2023-09-20 zmq810150896 adds truncate functionality and standardized unlink adaptations
  11. */
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. #include <dfs.h>
  15. #include <dfs_fs.h>
  16. #include <dfs_dentry.h>
  17. #include <dfs_file.h>
  18. #include <dfs_mnt.h>
  19. #ifdef RT_USING_SMART
  20. #include <lwp.h>
  21. #include <lwp_user_mm.h>
  22. #endif
  23. #include "dfs_tmpfs.h"
  24. #define DBG_TAG "tmpfs"
  25. #define DBG_LVL DBG_INFO
  26. #include <rtdbg.h>
  27. #ifdef RT_USING_PAGECACHE
  28. #include "dfs_pcache.h"
  29. #endif
  30. #ifdef RT_USING_PAGECACHE
  31. static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page);
  32. static ssize_t dfs_tmp_page_write(struct dfs_page *page);
  33. static struct dfs_aspace_ops dfs_tmp_aspace_ops =
  34. {
  35. .read = dfs_tmp_page_read,
  36. .write = dfs_tmp_page_write,
  37. };
  38. #endif
  39. static int _path_separate(const char *path, char *parent_path, char *file_name)
  40. {
  41. const char *path_p, *path_q;
  42. RT_ASSERT(path[0] == '/');
  43. file_name[0] = '\0';
  44. path_p = path_q = &path[1];
  45. __next_dir:
  46. while (*path_q != '/' && *path_q != '\0')
  47. {
  48. path_q++;
  49. }
  50. if (path_q != path_p) /*sub dir*/
  51. {
  52. if (*path_q != '\0')
  53. {
  54. path_q++;
  55. path_p = path_q;
  56. goto __next_dir;
  57. }
  58. else /* Last level dir */
  59. {
  60. rt_memcpy(parent_path, path, path_p - path - 1);
  61. parent_path[path_p - path - 1] = '\0';
  62. rt_memcpy(file_name, path_p, path_q - path_p);
  63. file_name[path_q - path_p] = '\0';
  64. }
  65. }
  66. if (parent_path[0] == 0)
  67. {
  68. parent_path[0] = '/';
  69. parent_path[1] = '\0';
  70. }
  71. LOG_D("parent_path: %s", parent_path);
  72. LOG_D("file_name: %s", file_name);
  73. return 0;
  74. }
  75. static int _get_subdir(const char *path, char *name)
  76. {
  77. const char *subpath = path;
  78. while (*subpath == '/' && *subpath)
  79. subpath ++;
  80. while (*subpath != '/' && *subpath)
  81. {
  82. *name = *subpath;
  83. name ++;
  84. subpath ++;
  85. }
  86. return 0;
  87. }
  88. static int _free_subdir(struct tmpfs_file *dfile)
  89. {
  90. struct tmpfs_file *file = RT_NULL, *tmp;
  91. struct tmpfs_sb *superblock;
  92. RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
  93. dfs_vfs_for_each_subnode(file, tmp, dfile, node)
  94. {
  95. if (file->type == TMPFS_TYPE_DIR)
  96. {
  97. _free_subdir(file);
  98. }
  99. if (file->data != NULL)
  100. {
  101. /* TODO: fix for rt-smart */
  102. rt_free(file->data);
  103. }
  104. superblock = file->sb;
  105. RT_ASSERT(superblock != NULL);
  106. rt_spin_lock(&superblock->lock);
  107. dfs_vfs_remove_node(&file->node);
  108. rt_spin_unlock(&superblock->lock);
  109. rt_free(file);
  110. }
  111. return 0;
  112. }
  113. static int dfs_tmpfs_mount(struct dfs_mnt *mnt,
  114. unsigned long rwflag,
  115. const void *data)
  116. {
  117. struct tmpfs_sb *superblock;
  118. superblock = rt_calloc(1, sizeof(struct tmpfs_sb));
  119. if (superblock)
  120. {
  121. superblock->df_size = sizeof(struct tmpfs_sb);
  122. superblock->magic = TMPFS_MAGIC;
  123. superblock->root.name[0] = '/';
  124. superblock->root.sb = superblock;
  125. superblock->root.type = TMPFS_TYPE_DIR;
  126. dfs_vfs_init_node(&superblock->root.node);
  127. rt_spin_lock_init(&superblock->lock);
  128. mnt->data = superblock;
  129. }
  130. else
  131. {
  132. return -1;
  133. }
  134. return RT_EOK;
  135. }
  136. static int dfs_tmpfs_unmount(struct dfs_mnt *mnt)
  137. {
  138. struct tmpfs_sb *superblock;
  139. /* FIXME: don't unmount on busy. */
  140. superblock = (struct tmpfs_sb *)mnt->data;
  141. RT_ASSERT(superblock != NULL);
  142. mnt->data = NULL;
  143. _free_subdir(&(superblock->root));
  144. rt_free(superblock);
  145. return RT_EOK;
  146. }
  147. int dfs_tmpfs_statfs(struct dfs_mnt *mnt, struct statfs *buf)
  148. {
  149. struct tmpfs_sb *superblock;
  150. superblock = (struct tmpfs_sb *)mnt->data;
  151. RT_ASSERT(superblock != NULL);
  152. RT_ASSERT(buf != NULL);
  153. buf->f_bsize = 512;
  154. buf->f_blocks = (superblock->df_size + 511) / 512;
  155. buf->f_bfree = 1;
  156. buf->f_bavail = buf->f_bfree;
  157. return RT_EOK;
  158. }
  159. int dfs_tmpfs_ioctl(struct dfs_file *file, int cmd, void *args)
  160. {
  161. struct tmpfs_file *d_file;
  162. struct tmpfs_sb *superblock;
  163. d_file = (struct tmpfs_file *)file->vnode->data;
  164. RT_ASSERT(d_file != NULL);
  165. superblock = d_file->sb;
  166. RT_ASSERT(superblock != NULL);
  167. RT_UNUSED(superblock);
  168. switch (cmd)
  169. {
  170. #ifdef RT_USING_SMART
  171. case RT_FIOMMAP2:
  172. {
  173. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
  174. if (mmap2)
  175. {
  176. if (mmap2->length > file->vnode->size)
  177. {
  178. return -RT_ENOMEM;
  179. }
  180. LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
  181. mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, d_file->data, mmap2->length, 0);
  182. }
  183. return RT_EOK;
  184. break;
  185. }
  186. #endif
  187. default:
  188. break;
  189. }
  190. return -EIO;
  191. }
  192. struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
  193. const char *path,
  194. rt_size_t *size)
  195. {
  196. const char *subpath, *curpath, *filename = RT_NULL;
  197. char subdir_name[TMPFS_NAME_MAX];
  198. struct tmpfs_file *file, *curfile, *tmp;
  199. subpath = path;
  200. while (*subpath == '/' && *subpath)
  201. subpath ++;
  202. if (! *subpath) /* is root directory */
  203. {
  204. *size = 0;
  205. return &(superblock->root);
  206. }
  207. curpath = subpath;
  208. curfile = &superblock->root;
  209. find_subpath:
  210. while (*subpath != '/' && *subpath)
  211. subpath ++;
  212. if (! *subpath) /* is last directory */
  213. filename = curpath;
  214. else
  215. subpath ++; /* skip '/' */
  216. memset(subdir_name, 0, TMPFS_NAME_MAX);
  217. _get_subdir(curpath, subdir_name);
  218. rt_spin_lock(&superblock->lock);
  219. dfs_vfs_for_each_subnode(file, tmp, curfile, node)
  220. {
  221. if (filename) /* find file */
  222. {
  223. if (rt_strcmp(file->name, filename) == 0)
  224. {
  225. *size = file->size;
  226. rt_spin_unlock(&superblock->lock);
  227. return file;
  228. }
  229. }
  230. else if (rt_strcmp(file->name, subdir_name) == 0)
  231. {
  232. *size = file->size;
  233. curpath = subpath;
  234. curfile = file;
  235. rt_spin_unlock(&superblock->lock);
  236. goto find_subpath;
  237. }
  238. }
  239. rt_spin_unlock(&superblock->lock);
  240. /* not found */
  241. return NULL;
  242. }
  243. static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  244. {
  245. ssize_t length;
  246. struct tmpfs_file *d_file;
  247. d_file = (struct tmpfs_file *)file->vnode->data;
  248. RT_ASSERT(d_file != NULL);
  249. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  250. ssize_t size = (ssize_t)file->vnode->size;
  251. if ((ssize_t)count < size - *pos)
  252. length = count;
  253. else
  254. length = size - *pos;
  255. if (length > 0)
  256. memcpy(buf, &(d_file->data[*pos]), length);
  257. /* update file current position */
  258. *pos += length;
  259. rt_mutex_release(&file->vnode->lock);
  260. return length;
  261. }
  262. static ssize_t _dfs_tmpfs_write(struct tmpfs_file *d_file, const void *buf, size_t count, off_t *pos)
  263. {
  264. struct tmpfs_sb *superblock;
  265. RT_ASSERT(d_file != NULL);
  266. superblock = d_file->sb;
  267. RT_ASSERT(superblock != NULL);
  268. if (count + *pos > d_file->size)
  269. {
  270. rt_uint8_t *ptr;
  271. ptr = rt_realloc(d_file->data, *pos + count);
  272. if (ptr == NULL)
  273. {
  274. rt_set_errno(-ENOMEM);
  275. return 0;
  276. }
  277. rt_spin_lock(&superblock->lock);
  278. superblock->df_size += (*pos - d_file->size + count);
  279. rt_spin_unlock(&superblock->lock);
  280. /* update d_file and file size */
  281. d_file->data = ptr;
  282. d_file->size = *pos + count;
  283. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  284. }
  285. if (count > 0)
  286. memcpy(d_file->data + *pos, buf, count);
  287. /* update file current position */
  288. *pos += count;
  289. return count;
  290. }
  291. static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
  292. {
  293. struct tmpfs_file *d_file;
  294. d_file = (struct tmpfs_file *)file->vnode->data;
  295. RT_ASSERT(d_file != NULL);
  296. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  297. count = _dfs_tmpfs_write(d_file, buf, count, pos);
  298. rt_mutex_release(&file->vnode->lock);
  299. return count;
  300. }
  301. static off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
  302. {
  303. switch (wherece)
  304. {
  305. case SEEK_SET:
  306. break;
  307. case SEEK_CUR:
  308. offset += file->fpos;
  309. break;
  310. case SEEK_END:
  311. offset += file->vnode->size;
  312. break;
  313. default:
  314. return -EINVAL;
  315. }
  316. if (offset <= (off_t)file->vnode->size)
  317. {
  318. return offset;
  319. }
  320. return -EIO;
  321. }
  322. static int dfs_tmpfs_close(struct dfs_file *file)
  323. {
  324. struct tmpfs_file *d_file;
  325. RT_ASSERT(file->vnode->ref_count > 0);
  326. if (file->vnode->ref_count != 1)
  327. return 0;
  328. d_file = (struct tmpfs_file *)file->vnode->data;
  329. if (d_file == NULL)
  330. return -ENOENT;
  331. if (d_file->fre_memory == RT_TRUE)
  332. {
  333. if (d_file->data != NULL)
  334. {
  335. rt_free(d_file->data);
  336. d_file->data = RT_NULL;
  337. }
  338. rt_free(d_file);
  339. }
  340. rt_mutex_detach(&file->vnode->lock);
  341. return RT_EOK;
  342. }
  343. static int dfs_tmpfs_open(struct dfs_file *file)
  344. {
  345. struct tmpfs_file *d_file;
  346. d_file = (struct tmpfs_file *)file->vnode->data;
  347. RT_ASSERT(d_file != RT_NULL);
  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. file->vnode->size = d_file->size;
  355. file->fpos = file->vnode->size;
  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. if (file->flags & O_APPEND)
  364. {
  365. file->fpos = file->vnode->size;
  366. }
  367. else
  368. {
  369. file->fpos = 0;
  370. }
  371. RT_ASSERT(file->vnode->ref_count > 0);
  372. if(file->vnode->ref_count == 1)
  373. {
  374. rt_mutex_init(&file->vnode->lock, file->dentry->pathname, RT_IPC_FLAG_PRIO);
  375. }
  376. return 0;
  377. }
  378. static int dfs_tmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
  379. {
  380. rt_size_t size;
  381. struct tmpfs_file *d_file;
  382. struct tmpfs_sb *superblock;
  383. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  384. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  385. if (d_file == NULL)
  386. return -ENOENT;
  387. st->st_dev = (dev_t)(size_t)(dentry->mnt->dev_id);
  388. st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
  389. if (d_file->type == TMPFS_TYPE_DIR)
  390. {
  391. st->st_mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  392. }
  393. else
  394. {
  395. st->st_mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  396. }
  397. st->st_size = d_file->size;
  398. st->st_mtime = 0;
  399. return RT_EOK;
  400. }
  401. static int dfs_tmpfs_getdents(struct dfs_file *file,
  402. struct dirent *dirp,
  403. uint32_t count)
  404. {
  405. rt_size_t index, end;
  406. struct dirent *d;
  407. struct tmpfs_file *d_file, *n_file, *tmp;
  408. struct tmpfs_sb *superblock;
  409. d_file = (struct tmpfs_file *)file->vnode->data;
  410. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  411. superblock = d_file->sb;
  412. RT_ASSERT(superblock != RT_NULL);
  413. RT_UNUSED(superblock);
  414. /* make integer count */
  415. count = (count / sizeof(struct dirent));
  416. if (count == 0)
  417. {
  418. rt_mutex_release(&file->vnode->lock);
  419. return -EINVAL;
  420. }
  421. end = file->fpos + count;
  422. index = 0;
  423. count = 0;
  424. dfs_vfs_for_each_subnode(n_file, tmp, d_file, node)
  425. {
  426. if (index >= (rt_size_t)file->fpos)
  427. {
  428. d = dirp + count;
  429. if (d_file->type == TMPFS_TYPE_FILE)
  430. {
  431. d->d_type = DT_REG;
  432. }
  433. if (d_file->type == TMPFS_TYPE_DIR)
  434. {
  435. d->d_type = DT_DIR;
  436. }
  437. d->d_namlen = RT_NAME_MAX;
  438. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  439. rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
  440. count += 1;
  441. file->fpos += 1;
  442. }
  443. index += 1;
  444. if (index >= end)
  445. {
  446. break;
  447. }
  448. }
  449. rt_mutex_release(&file->vnode->lock);
  450. return count * sizeof(struct dirent);
  451. }
  452. static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
  453. {
  454. rt_size_t size;
  455. struct tmpfs_sb *superblock;
  456. struct tmpfs_file *d_file;
  457. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  458. RT_ASSERT(superblock != NULL);
  459. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  460. if (d_file == NULL)
  461. return -ENOENT;
  462. rt_spin_lock(&superblock->lock);
  463. dfs_vfs_remove_node(&d_file->node);
  464. rt_spin_unlock(&superblock->lock);
  465. if (rt_atomic_load(&(dentry->ref_count)) == 1)
  466. {
  467. if (d_file->data != NULL)
  468. {
  469. rt_free(d_file->data);
  470. d_file->data = RT_NULL;
  471. }
  472. rt_free(d_file);
  473. }
  474. else
  475. {
  476. d_file->fre_memory = RT_TRUE;
  477. }
  478. return RT_EOK;
  479. }
  480. static int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
  481. {
  482. struct tmpfs_file *d_file, *p_file;
  483. struct tmpfs_sb *superblock;
  484. rt_size_t size;
  485. char *parent_path;
  486. char file_name[TMPFS_NAME_MAX];
  487. superblock = (struct tmpfs_sb *)old_dentry->mnt->data;
  488. RT_ASSERT(superblock != NULL);
  489. d_file = dfs_tmpfs_lookup(superblock, new_dentry->pathname, &size);
  490. if (d_file != NULL)
  491. return -EEXIST;
  492. d_file = dfs_tmpfs_lookup(superblock, old_dentry->pathname, &size);
  493. if (d_file == NULL)
  494. return -ENOENT;
  495. parent_path = rt_malloc(DFS_PATH_MAX);
  496. if (!parent_path)
  497. {
  498. return -ENOMEM;
  499. }
  500. /* find parent file */
  501. _path_separate(new_dentry->pathname, parent_path, file_name);
  502. if (file_name[0] == '\0') /* it's root dir */
  503. {
  504. rt_free(parent_path);
  505. return -ENOENT;
  506. }
  507. /* open parent directory */
  508. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  509. RT_ASSERT(p_file != NULL);
  510. rt_spin_lock(&superblock->lock);
  511. dfs_vfs_remove_node(&d_file->node);
  512. rt_spin_unlock(&superblock->lock);
  513. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  514. rt_spin_lock(&superblock->lock);
  515. dfs_vfs_append_node(&p_file->node, &d_file->node);
  516. rt_spin_unlock(&superblock->lock);
  517. rt_free(parent_path);
  518. return RT_EOK;
  519. }
  520. static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
  521. {
  522. struct dfs_vnode *vnode = RT_NULL;
  523. rt_size_t size;
  524. struct tmpfs_sb *superblock;
  525. struct tmpfs_file *d_file;
  526. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  527. {
  528. return NULL;
  529. }
  530. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  531. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  532. if (d_file)
  533. {
  534. vnode = dfs_vnode_create();
  535. if (vnode)
  536. {
  537. if (d_file->type == TMPFS_TYPE_DIR)
  538. {
  539. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  540. vnode->type = FT_DIRECTORY;
  541. }
  542. else
  543. {
  544. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  545. vnode->type = FT_REGULAR;
  546. #ifdef RT_USING_PAGECACHE
  547. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  548. #endif
  549. }
  550. vnode->mnt = dentry->mnt;
  551. vnode->data = d_file;
  552. vnode->size = d_file->size;
  553. }
  554. }
  555. return vnode;
  556. }
  557. static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  558. {
  559. struct dfs_vnode *vnode = RT_NULL;
  560. rt_size_t size;
  561. struct tmpfs_sb *superblock;
  562. struct tmpfs_file *d_file, *p_file;
  563. char *parent_path;
  564. char file_name[TMPFS_NAME_MAX];
  565. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  566. {
  567. return NULL;
  568. }
  569. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  570. RT_ASSERT(superblock != NULL);
  571. parent_path = rt_malloc(DFS_PATH_MAX);
  572. if (!parent_path)
  573. {
  574. return NULL;
  575. }
  576. vnode = dfs_vnode_create();
  577. if (vnode)
  578. {
  579. /* find parent file */
  580. _path_separate(dentry->pathname, parent_path, file_name);
  581. if (file_name[0] == '\0') /* it's root dir */
  582. {
  583. rt_free(parent_path);
  584. dfs_vnode_destroy(vnode);
  585. return NULL;
  586. }
  587. /* open parent directory */
  588. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  589. if (p_file == NULL)
  590. {
  591. rt_free(parent_path);
  592. dfs_vnode_destroy(vnode);
  593. return NULL;
  594. }
  595. /* create a file entry */
  596. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  597. if (d_file == NULL)
  598. {
  599. rt_free(parent_path);
  600. dfs_vnode_destroy(vnode);
  601. return NULL;
  602. }
  603. superblock->df_size += sizeof(struct tmpfs_file);
  604. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  605. dfs_vfs_init_node(&d_file->node);
  606. d_file->data = NULL;
  607. d_file->size = 0;
  608. d_file->sb = superblock;
  609. d_file->fre_memory = RT_FALSE;
  610. if (type == FT_DIRECTORY)
  611. {
  612. d_file->type = TMPFS_TYPE_DIR;
  613. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  614. vnode->type = FT_DIRECTORY;
  615. }
  616. else
  617. {
  618. d_file->type = TMPFS_TYPE_FILE;
  619. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  620. vnode->type = FT_REGULAR;
  621. #ifdef RT_USING_PAGECACHE
  622. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  623. #endif
  624. }
  625. rt_spin_lock(&superblock->lock);
  626. dfs_vfs_append_node(&p_file->node, &d_file->node);
  627. rt_spin_unlock(&superblock->lock);
  628. vnode->mnt = dentry->mnt;
  629. vnode->data = d_file;
  630. vnode->size = d_file->size;
  631. }
  632. rt_free(parent_path);
  633. return vnode;
  634. }
  635. static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
  636. {
  637. /* nothing to be freed */
  638. if (vnode && vnode->ref_count <= 1)
  639. {
  640. vnode->data = NULL;
  641. }
  642. return 0;
  643. }
  644. #ifdef RT_USING_PAGECACHE
  645. static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page)
  646. {
  647. int ret = -EINVAL;
  648. if (page->page)
  649. {
  650. off_t fpos = page->fpos;
  651. ret = dfs_tmpfs_read(file, page->page, page->size, &fpos);
  652. }
  653. return ret;
  654. }
  655. ssize_t dfs_tmp_page_write(struct dfs_page *page)
  656. {
  657. off_t pos;
  658. size_t count = 0;
  659. struct tmpfs_file *d_file;
  660. if (page->aspace->vnode->type == FT_DIRECTORY)
  661. {
  662. return -EISDIR;
  663. }
  664. d_file = (struct tmpfs_file *)(page->aspace->vnode->data);
  665. RT_ASSERT(d_file != RT_NULL);
  666. rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER);
  667. if (page->len > 0)
  668. {
  669. pos = page->fpos;
  670. count = _dfs_tmpfs_write(d_file, page->page, page->len, &pos);
  671. }
  672. rt_mutex_release(&page->aspace->vnode->lock);
  673. return count;
  674. }
  675. #endif
  676. static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
  677. {
  678. struct tmpfs_file *d_file = RT_NULL;
  679. struct tmpfs_sb *superblock = RT_NULL;
  680. rt_uint8_t *ptr = RT_NULL;
  681. d_file = (struct tmpfs_file *)file->vnode->data;
  682. RT_ASSERT(d_file != RT_NULL);
  683. superblock = d_file->sb;
  684. RT_ASSERT(superblock != RT_NULL);
  685. ptr = rt_realloc(d_file->data, offset);
  686. if (ptr == RT_NULL)
  687. {
  688. rt_set_errno(-ENOMEM);
  689. return 0;
  690. }
  691. rt_spin_lock(&superblock->lock);
  692. superblock->df_size = offset;
  693. rt_spin_unlock(&superblock->lock);
  694. /* update d_file and file size */
  695. d_file->data = ptr;
  696. d_file->size = offset;
  697. file->vnode->size = d_file->size;
  698. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  699. return 0;
  700. }
  701. static const struct dfs_file_ops _tmp_fops =
  702. {
  703. .open = dfs_tmpfs_open,
  704. .close = dfs_tmpfs_close,
  705. .ioctl = dfs_tmpfs_ioctl,
  706. .read = dfs_tmpfs_read,
  707. .write = dfs_tmpfs_write,
  708. .lseek = dfs_tmpfs_lseek,
  709. .getdents = dfs_tmpfs_getdents,
  710. .truncate = dfs_tmpfs_truncate,
  711. };
  712. static const struct dfs_filesystem_ops _tmpfs_ops =
  713. {
  714. .name = "tmp",
  715. .flags = DFS_FS_FLAG_DEFAULT,
  716. .default_fops = &_tmp_fops,
  717. .mount = dfs_tmpfs_mount,
  718. .umount = dfs_tmpfs_unmount,
  719. .statfs = dfs_tmpfs_statfs,
  720. .unlink = dfs_tmpfs_unlink,
  721. .stat = dfs_tmpfs_stat,
  722. .rename = dfs_tmpfs_rename,
  723. .lookup = _dfs_tmpfs_lookup,
  724. .create_vnode = dfs_tmpfs_create_vnode,
  725. .free_vnode = dfs_tmpfs_free_vnode
  726. };
  727. static struct dfs_filesystem_type _tmpfs =
  728. {
  729. .fs_ops = &_tmpfs_ops,
  730. };
  731. int dfs_tmpfs_init(void)
  732. {
  733. /* register tmp file system */
  734. dfs_register(&_tmpfs);
  735. return 0;
  736. }
  737. INIT_COMPONENT_EXPORT(dfs_tmpfs_init);