dfs_tmpfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893
  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;
  91. rt_list_t *list, *temp_list;
  92. struct tmpfs_sb *superblock;
  93. RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
  94. rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
  95. {
  96. file = rt_list_entry(list, struct tmpfs_file, sibling);
  97. if (file->type == TMPFS_TYPE_DIR)
  98. {
  99. _free_subdir(file);
  100. }
  101. if (file->data != NULL)
  102. {
  103. /* TODO: fix for rt-smart */
  104. rt_free(file->data);
  105. }
  106. superblock = file->sb;
  107. RT_ASSERT(superblock != NULL);
  108. rt_spin_lock(&superblock->lock);
  109. rt_list_remove(&(file->sibling));
  110. rt_spin_unlock(&superblock->lock);
  111. rt_free(file);
  112. }
  113. return 0;
  114. }
  115. static int dfs_tmpfs_mount(struct dfs_mnt *mnt,
  116. unsigned long rwflag,
  117. const void *data)
  118. {
  119. struct tmpfs_sb *superblock;
  120. superblock = rt_calloc(1, sizeof(struct tmpfs_sb));
  121. if (superblock)
  122. {
  123. superblock->df_size = sizeof(struct tmpfs_sb);
  124. superblock->magic = TMPFS_MAGIC;
  125. rt_list_init(&superblock->sibling);
  126. superblock->root.name[0] = '/';
  127. superblock->root.sb = superblock;
  128. superblock->root.type = TMPFS_TYPE_DIR;
  129. rt_list_init(&superblock->root.sibling);
  130. rt_list_init(&superblock->root.subdirs);
  131. rt_spin_lock_init(&superblock->lock);
  132. mnt->data = superblock;
  133. }
  134. else
  135. {
  136. return -1;
  137. }
  138. return RT_EOK;
  139. }
  140. static int dfs_tmpfs_unmount(struct dfs_mnt *mnt)
  141. {
  142. struct tmpfs_sb *superblock;
  143. /* FIXME: don't unmount on busy. */
  144. superblock = (struct tmpfs_sb *)mnt->data;
  145. RT_ASSERT(superblock != NULL);
  146. mnt->data = NULL;
  147. _free_subdir(&(superblock->root));
  148. rt_free(superblock);
  149. return RT_EOK;
  150. }
  151. int dfs_tmpfs_statfs(struct dfs_mnt *mnt, struct statfs *buf)
  152. {
  153. struct tmpfs_sb *superblock;
  154. superblock = (struct tmpfs_sb *)mnt->data;
  155. RT_ASSERT(superblock != NULL);
  156. RT_ASSERT(buf != NULL);
  157. buf->f_bsize = 512;
  158. buf->f_blocks = (superblock->df_size + 511) / 512;
  159. buf->f_bfree = 1;
  160. buf->f_bavail = buf->f_bfree;
  161. return RT_EOK;
  162. }
  163. int dfs_tmpfs_ioctl(struct dfs_file *file, int cmd, void *args)
  164. {
  165. struct tmpfs_file *d_file;
  166. struct tmpfs_sb *superblock;
  167. d_file = (struct tmpfs_file *)file->vnode->data;
  168. RT_ASSERT(d_file != NULL);
  169. superblock = d_file->sb;
  170. RT_ASSERT(superblock != NULL);
  171. switch (cmd)
  172. {
  173. #ifdef RT_USING_SMART
  174. case RT_FIOMMAP2:
  175. {
  176. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
  177. if (mmap2)
  178. {
  179. if (mmap2->length > file->vnode->size)
  180. {
  181. return -RT_ENOMEM;
  182. }
  183. LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
  184. mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, d_file->data, mmap2->length, 0);
  185. }
  186. return RT_EOK;
  187. break;
  188. }
  189. #endif
  190. default:
  191. break;
  192. }
  193. return -EIO;
  194. }
  195. struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
  196. const char *path,
  197. rt_size_t *size)
  198. {
  199. const char *subpath, *curpath, *filename = RT_NULL;
  200. char subdir_name[TMPFS_NAME_MAX];
  201. struct tmpfs_file *file, *curfile;
  202. rt_list_t *list;
  203. subpath = path;
  204. while (*subpath == '/' && *subpath)
  205. subpath ++;
  206. if (! *subpath) /* is root directory */
  207. {
  208. *size = 0;
  209. return &(superblock->root);
  210. }
  211. curpath = subpath;
  212. curfile = &superblock->root;
  213. find_subpath:
  214. while (*subpath != '/' && *subpath)
  215. subpath ++;
  216. if (! *subpath) /* is last directory */
  217. filename = curpath;
  218. else
  219. subpath ++; /* skip '/' */
  220. memset(subdir_name, 0, TMPFS_NAME_MAX);
  221. _get_subdir(curpath, subdir_name);
  222. rt_spin_lock(&superblock->lock);
  223. rt_list_for_each(list, &curfile->subdirs)
  224. {
  225. file = rt_list_entry(list, struct tmpfs_file, sibling);
  226. if (filename) /* find file */
  227. {
  228. if (rt_strcmp(file->name, filename) == 0)
  229. {
  230. *size = file->size;
  231. rt_spin_unlock(&superblock->lock);
  232. return file;
  233. }
  234. }
  235. else if (rt_strcmp(file->name, subdir_name) == 0)
  236. {
  237. *size = file->size;
  238. curpath = subpath;
  239. curfile = file;
  240. rt_spin_unlock(&superblock->lock);
  241. goto find_subpath;
  242. }
  243. }
  244. rt_spin_unlock(&superblock->lock);
  245. /* not found */
  246. return NULL;
  247. }
  248. static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  249. {
  250. rt_size_t length;
  251. struct tmpfs_file *d_file;
  252. d_file = (struct tmpfs_file *)file->vnode->data;
  253. RT_ASSERT(d_file != NULL);
  254. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  255. if (count < file->vnode->size - *pos)
  256. length = count;
  257. else
  258. length = file->vnode->size - *pos;
  259. if (length > 0)
  260. memcpy(buf, &(d_file->data[*pos]), length);
  261. /* update file current position */
  262. *pos += length;
  263. rt_mutex_release(&file->vnode->lock);
  264. return length;
  265. }
  266. static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
  267. {
  268. struct tmpfs_file *d_file;
  269. struct tmpfs_sb *superblock;
  270. d_file = (struct tmpfs_file *)file->vnode->data;
  271. RT_ASSERT(d_file != NULL);
  272. superblock = d_file->sb;
  273. RT_ASSERT(superblock != NULL);
  274. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  275. if (count + *pos > file->vnode->size)
  276. {
  277. rt_uint8_t *ptr;
  278. ptr = rt_realloc(d_file->data, *pos + count);
  279. if (ptr == NULL)
  280. {
  281. rt_mutex_release(&file->vnode->lock);
  282. rt_set_errno(-ENOMEM);
  283. return 0;
  284. }
  285. rt_spin_lock(&superblock->lock);
  286. superblock->df_size += (*pos - d_file->size + count);
  287. rt_spin_unlock(&superblock->lock);
  288. /* update d_file and file size */
  289. d_file->data = ptr;
  290. d_file->size = *pos + count;
  291. file->vnode->size = d_file->size;
  292. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  293. }
  294. if (count > 0)
  295. memcpy(d_file->data + *pos, buf, count);
  296. /* update file current position */
  297. *pos += count;
  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;
  408. rt_list_t *list;
  409. struct tmpfs_sb *superblock;
  410. d_file = (struct tmpfs_file *)file->vnode->data;
  411. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  412. superblock = d_file->sb;
  413. RT_ASSERT(superblock != RT_NULL);
  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. rt_list_for_each(list, &d_file->subdirs)
  425. {
  426. n_file = rt_list_entry(list, struct tmpfs_file, sibling);
  427. if (index >= (rt_size_t)file->fpos)
  428. {
  429. d = dirp + count;
  430. if (d_file->type == TMPFS_TYPE_FILE)
  431. {
  432. d->d_type = DT_REG;
  433. }
  434. if (d_file->type == TMPFS_TYPE_DIR)
  435. {
  436. d->d_type = DT_DIR;
  437. }
  438. d->d_namlen = RT_NAME_MAX;
  439. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  440. rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
  441. count += 1;
  442. file->fpos += 1;
  443. }
  444. index += 1;
  445. if (index >= end)
  446. {
  447. break;
  448. }
  449. }
  450. rt_mutex_release(&file->vnode->lock);
  451. return count * sizeof(struct dirent);
  452. }
  453. static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
  454. {
  455. rt_size_t size;
  456. struct tmpfs_sb *superblock;
  457. struct tmpfs_file *d_file;
  458. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  459. RT_ASSERT(superblock != NULL);
  460. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  461. if (d_file == NULL)
  462. return -ENOENT;
  463. rt_spin_lock(&superblock->lock);
  464. rt_list_remove(&(d_file->sibling));
  465. rt_spin_unlock(&superblock->lock);
  466. if (rt_atomic_load(&(dentry->ref_count)) == 1)
  467. {
  468. if (d_file->data != NULL)
  469. {
  470. rt_free(d_file->data);
  471. d_file->data = RT_NULL;
  472. }
  473. rt_free(d_file);
  474. }
  475. else
  476. {
  477. d_file->fre_memory = RT_TRUE;
  478. }
  479. return RT_EOK;
  480. }
  481. static int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
  482. {
  483. struct tmpfs_file *d_file, *p_file;
  484. struct tmpfs_sb *superblock;
  485. rt_size_t size;
  486. char *parent_path;
  487. char file_name[TMPFS_NAME_MAX];
  488. superblock = (struct tmpfs_sb *)old_dentry->mnt->data;
  489. RT_ASSERT(superblock != NULL);
  490. d_file = dfs_tmpfs_lookup(superblock, new_dentry->pathname, &size);
  491. if (d_file != NULL)
  492. return -EEXIST;
  493. d_file = dfs_tmpfs_lookup(superblock, old_dentry->pathname, &size);
  494. if (d_file == NULL)
  495. return -ENOENT;
  496. parent_path = rt_malloc(DFS_PATH_MAX);
  497. if (!parent_path)
  498. {
  499. return -ENOMEM;
  500. }
  501. /* find parent file */
  502. _path_separate(new_dentry->pathname, parent_path, file_name);
  503. if (file_name[0] == '\0') /* it's root dir */
  504. {
  505. rt_free(parent_path);
  506. return -ENOENT;
  507. }
  508. /* open parent directory */
  509. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  510. RT_ASSERT(p_file != NULL);
  511. rt_spin_lock(&superblock->lock);
  512. rt_list_remove(&(d_file->sibling));
  513. rt_spin_unlock(&superblock->lock);
  514. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  515. rt_spin_lock(&superblock->lock);
  516. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  517. rt_spin_unlock(&superblock->lock);
  518. rt_free(parent_path);
  519. return RT_EOK;
  520. }
  521. static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
  522. {
  523. struct dfs_vnode *vnode = RT_NULL;
  524. rt_size_t size;
  525. struct tmpfs_sb *superblock;
  526. struct tmpfs_file *d_file;
  527. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  528. {
  529. return NULL;
  530. }
  531. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  532. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  533. if (d_file)
  534. {
  535. vnode = dfs_vnode_create();
  536. if (vnode)
  537. {
  538. if (d_file->type == TMPFS_TYPE_DIR)
  539. {
  540. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  541. vnode->type = FT_DIRECTORY;
  542. }
  543. else
  544. {
  545. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  546. vnode->type = FT_REGULAR;
  547. #ifdef RT_USING_PAGECACHE
  548. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  549. #endif
  550. }
  551. vnode->mnt = dentry->mnt;
  552. vnode->data = d_file;
  553. vnode->size = d_file->size;
  554. }
  555. }
  556. return vnode;
  557. }
  558. static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  559. {
  560. struct dfs_vnode *vnode = RT_NULL;
  561. rt_size_t size;
  562. struct tmpfs_sb *superblock;
  563. struct tmpfs_file *d_file, *p_file;
  564. char *parent_path;
  565. char file_name[TMPFS_NAME_MAX];
  566. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  567. {
  568. return NULL;
  569. }
  570. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  571. RT_ASSERT(superblock != NULL);
  572. parent_path = rt_malloc(DFS_PATH_MAX);
  573. if (!parent_path)
  574. {
  575. return NULL;
  576. }
  577. vnode = dfs_vnode_create();
  578. if (vnode)
  579. {
  580. /* find parent file */
  581. _path_separate(dentry->pathname, parent_path, file_name);
  582. if (file_name[0] == '\0') /* it's root dir */
  583. {
  584. rt_free(parent_path);
  585. dfs_vnode_destroy(vnode);
  586. return NULL;
  587. }
  588. /* open parent directory */
  589. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  590. if (p_file == NULL)
  591. {
  592. rt_free(parent_path);
  593. dfs_vnode_destroy(vnode);
  594. return NULL;
  595. }
  596. /* create a file entry */
  597. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  598. if (d_file == NULL)
  599. {
  600. rt_free(parent_path);
  601. dfs_vnode_destroy(vnode);
  602. return NULL;
  603. }
  604. superblock->df_size += sizeof(struct tmpfs_file);
  605. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  606. rt_list_init(&(d_file->subdirs));
  607. rt_list_init(&(d_file->sibling));
  608. d_file->data = NULL;
  609. d_file->size = 0;
  610. d_file->sb = superblock;
  611. d_file->fre_memory = RT_FALSE;
  612. if (type == FT_DIRECTORY)
  613. {
  614. d_file->type = TMPFS_TYPE_DIR;
  615. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  616. vnode->type = FT_DIRECTORY;
  617. }
  618. else
  619. {
  620. d_file->type = TMPFS_TYPE_FILE;
  621. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  622. vnode->type = FT_REGULAR;
  623. #ifdef RT_USING_PAGECACHE
  624. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  625. #endif
  626. }
  627. rt_spin_lock(&superblock->lock);
  628. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  629. rt_spin_unlock(&superblock->lock);
  630. vnode->mnt = dentry->mnt;
  631. vnode->data = d_file;
  632. vnode->size = d_file->size;
  633. }
  634. rt_free(parent_path);
  635. return vnode;
  636. }
  637. static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
  638. {
  639. /* nothing to be freed */
  640. if (vnode && vnode->ref_count <= 1)
  641. {
  642. vnode->data = NULL;
  643. }
  644. return 0;
  645. }
  646. #ifdef RT_USING_PAGECACHE
  647. static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page)
  648. {
  649. int ret = -EINVAL;
  650. if (page->page)
  651. {
  652. off_t fpos = page->fpos;
  653. ret = dfs_tmpfs_read(file, page->page, page->size, &fpos);
  654. }
  655. return ret;
  656. }
  657. ssize_t dfs_tmp_page_write(struct dfs_page *page)
  658. {
  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. memcpy(d_file->data + page->fpos, page->page, page->len);
  669. rt_mutex_release(&page->aspace->vnode->lock);
  670. return F_OK;
  671. }
  672. #endif
  673. static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
  674. {
  675. struct tmpfs_file *d_file = RT_NULL;
  676. struct tmpfs_sb *superblock = RT_NULL;
  677. rt_uint8_t *ptr = RT_NULL;
  678. d_file = (struct tmpfs_file *)file->vnode->data;
  679. RT_ASSERT(d_file != RT_NULL);
  680. superblock = d_file->sb;
  681. RT_ASSERT(superblock != RT_NULL);
  682. ptr = rt_realloc(d_file->data, offset);
  683. if (ptr == RT_NULL)
  684. {
  685. rt_set_errno(-ENOMEM);
  686. return 0;
  687. }
  688. rt_spin_lock(&superblock->lock);
  689. superblock->df_size = offset;
  690. rt_spin_unlock(&superblock->lock);
  691. /* update d_file and file size */
  692. d_file->data = ptr;
  693. d_file->size = offset;
  694. file->vnode->size = d_file->size;
  695. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  696. return 0;
  697. }
  698. static const struct dfs_file_ops _tmp_fops =
  699. {
  700. .open = dfs_tmpfs_open,
  701. .close = dfs_tmpfs_close,
  702. .ioctl = dfs_tmpfs_ioctl,
  703. .read = dfs_tmpfs_read,
  704. .write = dfs_tmpfs_write,
  705. .lseek = dfs_tmpfs_lseek,
  706. .getdents = dfs_tmpfs_getdents,
  707. .truncate = dfs_tmpfs_truncate,
  708. };
  709. static const struct dfs_filesystem_ops _tmpfs_ops =
  710. {
  711. .name = "tmp",
  712. .flags = DFS_FS_FLAG_DEFAULT,
  713. .default_fops = &_tmp_fops,
  714. .mount = dfs_tmpfs_mount,
  715. .umount = dfs_tmpfs_unmount,
  716. .statfs = dfs_tmpfs_statfs,
  717. .unlink = dfs_tmpfs_unlink,
  718. .stat = dfs_tmpfs_stat,
  719. .rename = dfs_tmpfs_rename,
  720. .lookup = _dfs_tmpfs_lookup,
  721. .create_vnode = dfs_tmpfs_create_vnode,
  722. .free_vnode = dfs_tmpfs_free_vnode
  723. };
  724. static struct dfs_filesystem_type _tmpfs =
  725. {
  726. .fs_ops = &_tmpfs_ops,
  727. };
  728. int dfs_tmpfs_init(void)
  729. {
  730. /* register tmp file system */
  731. dfs_register(&_tmpfs);
  732. return 0;
  733. }
  734. INIT_COMPONENT_EXPORT(dfs_tmpfs_init);