dfs_tmpfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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[DFS_PATH_MAX], 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. /* find parent file */
  496. _path_separate(new_dentry->pathname, parent_path, file_name);
  497. if (file_name[0] == '\0') /* it's root dir */
  498. return -ENOENT;
  499. /* open parent directory */
  500. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  501. RT_ASSERT(p_file != NULL);
  502. rt_spin_lock(&superblock->lock);
  503. rt_list_remove(&(d_file->sibling));
  504. rt_spin_unlock(&superblock->lock);
  505. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  506. rt_spin_lock(&superblock->lock);
  507. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  508. rt_spin_unlock(&superblock->lock);
  509. return RT_EOK;
  510. }
  511. static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
  512. {
  513. struct dfs_vnode *vnode = RT_NULL;
  514. rt_size_t size;
  515. struct tmpfs_sb *superblock;
  516. struct tmpfs_file *d_file;
  517. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  518. {
  519. return NULL;
  520. }
  521. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  522. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  523. if (d_file)
  524. {
  525. vnode = dfs_vnode_create();
  526. if (vnode)
  527. {
  528. if (d_file->type == TMPFS_TYPE_DIR)
  529. {
  530. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  531. vnode->type = FT_DIRECTORY;
  532. }
  533. else
  534. {
  535. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  536. vnode->type = FT_REGULAR;
  537. #ifdef RT_USING_PAGECACHE
  538. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  539. #endif
  540. }
  541. vnode->mnt = dentry->mnt;
  542. vnode->data = d_file;
  543. vnode->size = d_file->size;
  544. }
  545. }
  546. return vnode;
  547. }
  548. static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  549. {
  550. struct dfs_vnode *vnode = RT_NULL;
  551. rt_size_t size;
  552. struct tmpfs_sb *superblock;
  553. struct tmpfs_file *d_file, *p_file;
  554. char parent_path[DFS_PATH_MAX], file_name[TMPFS_NAME_MAX];
  555. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  556. {
  557. return NULL;
  558. }
  559. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  560. RT_ASSERT(superblock != NULL);
  561. vnode = dfs_vnode_create();
  562. if (vnode)
  563. {
  564. /* find parent file */
  565. _path_separate(dentry->pathname, parent_path, file_name);
  566. if (file_name[0] == '\0') /* it's root dir */
  567. {
  568. dfs_vnode_destroy(vnode);
  569. return NULL;
  570. }
  571. /* open parent directory */
  572. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  573. if (p_file == NULL)
  574. {
  575. dfs_vnode_destroy(vnode);
  576. return NULL;
  577. }
  578. /* create a file entry */
  579. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  580. if (d_file == NULL)
  581. {
  582. dfs_vnode_destroy(vnode);
  583. return NULL;
  584. }
  585. superblock->df_size += sizeof(struct tmpfs_file);
  586. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  587. rt_list_init(&(d_file->subdirs));
  588. rt_list_init(&(d_file->sibling));
  589. d_file->data = NULL;
  590. d_file->size = 0;
  591. d_file->sb = superblock;
  592. d_file->fre_memory = RT_FALSE;
  593. if (type == FT_DIRECTORY)
  594. {
  595. d_file->type = TMPFS_TYPE_DIR;
  596. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  597. vnode->type = FT_DIRECTORY;
  598. }
  599. else
  600. {
  601. d_file->type = TMPFS_TYPE_FILE;
  602. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  603. vnode->type = FT_REGULAR;
  604. #ifdef RT_USING_PAGECACHE
  605. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  606. #endif
  607. }
  608. rt_spin_lock(&superblock->lock);
  609. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  610. rt_spin_unlock(&superblock->lock);
  611. vnode->mnt = dentry->mnt;
  612. vnode->data = d_file;
  613. vnode->size = d_file->size;
  614. }
  615. return vnode;
  616. }
  617. static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
  618. {
  619. /* nothing to be freed */
  620. if (vnode && vnode->ref_count <= 1)
  621. {
  622. vnode->data = NULL;
  623. }
  624. return 0;
  625. }
  626. #ifdef RT_USING_PAGECACHE
  627. static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page)
  628. {
  629. int ret = -EINVAL;
  630. if (page->page)
  631. {
  632. off_t fpos = page->fpos;
  633. ret = dfs_tmpfs_read(file, page->page, page->size, &fpos);
  634. }
  635. return ret;
  636. }
  637. ssize_t dfs_tmp_page_write(struct dfs_page *page)
  638. {
  639. struct tmpfs_file *d_file;
  640. if (page->aspace->vnode->type == FT_DIRECTORY)
  641. {
  642. return -EISDIR;
  643. }
  644. d_file = (struct tmpfs_file *)(page->aspace->vnode->data);
  645. RT_ASSERT(d_file != RT_NULL);
  646. rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER);
  647. if (page->len > 0)
  648. memcpy(d_file->data + page->fpos, page->page, page->len);
  649. rt_mutex_release(&page->aspace->vnode->lock);
  650. return F_OK;
  651. }
  652. #endif
  653. static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
  654. {
  655. struct tmpfs_file *d_file = RT_NULL;
  656. struct tmpfs_sb *superblock = RT_NULL;
  657. rt_uint8_t *ptr = RT_NULL;
  658. d_file = (struct tmpfs_file *)file->vnode->data;
  659. RT_ASSERT(d_file != RT_NULL);
  660. superblock = d_file->sb;
  661. RT_ASSERT(superblock != RT_NULL);
  662. ptr = rt_realloc(d_file->data, offset);
  663. if (ptr == RT_NULL)
  664. {
  665. rt_set_errno(-ENOMEM);
  666. return 0;
  667. }
  668. rt_spin_lock(&superblock->lock);
  669. superblock->df_size = offset;
  670. rt_spin_unlock(&superblock->lock);
  671. /* update d_file and file size */
  672. d_file->data = ptr;
  673. d_file->size = offset;
  674. file->vnode->size = d_file->size;
  675. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  676. return 0;
  677. }
  678. static const struct dfs_file_ops _tmp_fops =
  679. {
  680. .open = dfs_tmpfs_open,
  681. .close = dfs_tmpfs_close,
  682. .ioctl = dfs_tmpfs_ioctl,
  683. .read = dfs_tmpfs_read,
  684. .write = dfs_tmpfs_write,
  685. .lseek = dfs_tmpfs_lseek,
  686. .getdents = dfs_tmpfs_getdents,
  687. .truncate = dfs_tmpfs_truncate,
  688. };
  689. static const struct dfs_filesystem_ops _tmpfs_ops =
  690. {
  691. .name = "tmp",
  692. .flags = DFS_FS_FLAG_DEFAULT,
  693. .default_fops = &_tmp_fops,
  694. .mount = dfs_tmpfs_mount,
  695. .umount = dfs_tmpfs_unmount,
  696. .statfs = dfs_tmpfs_statfs,
  697. .unlink = dfs_tmpfs_unlink,
  698. .stat = dfs_tmpfs_stat,
  699. .rename = dfs_tmpfs_rename,
  700. .lookup = _dfs_tmpfs_lookup,
  701. .create_vnode = dfs_tmpfs_create_vnode,
  702. .free_vnode = dfs_tmpfs_free_vnode
  703. };
  704. static struct dfs_filesystem_type _tmpfs =
  705. {
  706. .fs_ops = &_tmpfs_ops,
  707. };
  708. int dfs_tmpfs_init(void)
  709. {
  710. /* register tmp file system */
  711. dfs_register(&_tmpfs);
  712. return 0;
  713. }
  714. INIT_COMPONENT_EXPORT(dfs_tmpfs_init);