dfs_tmpfs.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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. RT_UNUSED(superblock);
  172. switch (cmd)
  173. {
  174. #ifdef RT_USING_SMART
  175. case RT_FIOMMAP2:
  176. {
  177. struct dfs_mmap2_args *mmap2 = (struct dfs_mmap2_args *)args;
  178. if (mmap2)
  179. {
  180. if (mmap2->length > file->vnode->size)
  181. {
  182. return -RT_ENOMEM;
  183. }
  184. LOG_D("tmpfile mmap ptr:%x , size:%d\n", d_file->data, mmap2->length);
  185. mmap2->ret = lwp_map_user_phy(lwp_self(), RT_NULL, d_file->data, mmap2->length, 0);
  186. }
  187. return RT_EOK;
  188. break;
  189. }
  190. #endif
  191. default:
  192. break;
  193. }
  194. return -EIO;
  195. }
  196. struct tmpfs_file *dfs_tmpfs_lookup(struct tmpfs_sb *superblock,
  197. const char *path,
  198. rt_size_t *size)
  199. {
  200. const char *subpath, *curpath, *filename = RT_NULL;
  201. char subdir_name[TMPFS_NAME_MAX];
  202. struct tmpfs_file *file, *curfile;
  203. rt_list_t *list;
  204. subpath = path;
  205. while (*subpath == '/' && *subpath)
  206. subpath ++;
  207. if (! *subpath) /* is root directory */
  208. {
  209. *size = 0;
  210. return &(superblock->root);
  211. }
  212. curpath = subpath;
  213. curfile = &superblock->root;
  214. find_subpath:
  215. while (*subpath != '/' && *subpath)
  216. subpath ++;
  217. if (! *subpath) /* is last directory */
  218. filename = curpath;
  219. else
  220. subpath ++; /* skip '/' */
  221. memset(subdir_name, 0, TMPFS_NAME_MAX);
  222. _get_subdir(curpath, subdir_name);
  223. rt_spin_lock(&superblock->lock);
  224. rt_list_for_each(list, &curfile->subdirs)
  225. {
  226. file = rt_list_entry(list, struct tmpfs_file, sibling);
  227. if (filename) /* find file */
  228. {
  229. if (rt_strcmp(file->name, filename) == 0)
  230. {
  231. *size = file->size;
  232. rt_spin_unlock(&superblock->lock);
  233. return file;
  234. }
  235. }
  236. else if (rt_strcmp(file->name, subdir_name) == 0)
  237. {
  238. *size = file->size;
  239. curpath = subpath;
  240. curfile = file;
  241. rt_spin_unlock(&superblock->lock);
  242. goto find_subpath;
  243. }
  244. }
  245. rt_spin_unlock(&superblock->lock);
  246. /* not found */
  247. return NULL;
  248. }
  249. static ssize_t dfs_tmpfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  250. {
  251. rt_size_t length;
  252. struct tmpfs_file *d_file;
  253. d_file = (struct tmpfs_file *)file->vnode->data;
  254. RT_ASSERT(d_file != NULL);
  255. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  256. if (count < file->vnode->size - *pos)
  257. length = count;
  258. else
  259. length = file->vnode->size - *pos;
  260. if (length > 0)
  261. memcpy(buf, &(d_file->data[*pos]), length);
  262. /* update file current position */
  263. *pos += length;
  264. rt_mutex_release(&file->vnode->lock);
  265. return length;
  266. }
  267. static ssize_t _dfs_tmpfs_write(struct tmpfs_file *d_file, const void *buf, size_t count, off_t *pos)
  268. {
  269. struct tmpfs_sb *superblock;
  270. RT_ASSERT(d_file != NULL);
  271. superblock = d_file->sb;
  272. RT_ASSERT(superblock != NULL);
  273. if (count + *pos > d_file->size)
  274. {
  275. rt_uint8_t *ptr;
  276. ptr = rt_realloc(d_file->data, *pos + count);
  277. if (ptr == NULL)
  278. {
  279. rt_set_errno(-ENOMEM);
  280. return 0;
  281. }
  282. rt_spin_lock(&superblock->lock);
  283. superblock->df_size += (*pos - d_file->size + count);
  284. rt_spin_unlock(&superblock->lock);
  285. /* update d_file and file size */
  286. d_file->data = ptr;
  287. d_file->size = *pos + count;
  288. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  289. }
  290. if (count > 0)
  291. memcpy(d_file->data + *pos, buf, count);
  292. /* update file current position */
  293. *pos += count;
  294. return count;
  295. }
  296. static ssize_t dfs_tmpfs_write(struct dfs_file *file, const void *buf, size_t count, off_t *pos)
  297. {
  298. struct tmpfs_file *d_file;
  299. d_file = (struct tmpfs_file *)file->vnode->data;
  300. RT_ASSERT(d_file != NULL);
  301. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  302. count = _dfs_tmpfs_write(d_file, buf, count, pos);
  303. rt_mutex_release(&file->vnode->lock);
  304. return count;
  305. }
  306. static off_t dfs_tmpfs_lseek(struct dfs_file *file, off_t offset, int wherece)
  307. {
  308. switch (wherece)
  309. {
  310. case SEEK_SET:
  311. break;
  312. case SEEK_CUR:
  313. offset += file->fpos;
  314. break;
  315. case SEEK_END:
  316. offset += file->vnode->size;
  317. break;
  318. default:
  319. return -EINVAL;
  320. }
  321. if (offset <= (off_t)file->vnode->size)
  322. {
  323. return offset;
  324. }
  325. return -EIO;
  326. }
  327. static int dfs_tmpfs_close(struct dfs_file *file)
  328. {
  329. struct tmpfs_file *d_file;
  330. RT_ASSERT(file->vnode->ref_count > 0);
  331. if (file->vnode->ref_count != 1)
  332. return 0;
  333. d_file = (struct tmpfs_file *)file->vnode->data;
  334. if (d_file == NULL)
  335. return -ENOENT;
  336. if (d_file->fre_memory == RT_TRUE)
  337. {
  338. if (d_file->data != NULL)
  339. {
  340. rt_free(d_file->data);
  341. d_file->data = RT_NULL;
  342. }
  343. rt_free(d_file);
  344. }
  345. rt_mutex_detach(&file->vnode->lock);
  346. return RT_EOK;
  347. }
  348. static int dfs_tmpfs_open(struct dfs_file *file)
  349. {
  350. struct tmpfs_file *d_file;
  351. d_file = (struct tmpfs_file *)file->vnode->data;
  352. RT_ASSERT(d_file != RT_NULL);
  353. /* Creates a new file.
  354. * If the file is existing, it is truncated and overwritten.
  355. */
  356. if (file->flags & O_TRUNC)
  357. {
  358. d_file->size = 0;
  359. file->vnode->size = d_file->size;
  360. file->fpos = file->vnode->size;
  361. if (d_file->data != NULL)
  362. {
  363. /* ToDo: fix for rt-smart. */
  364. rt_free(d_file->data);
  365. d_file->data = NULL;
  366. }
  367. }
  368. if (file->flags & O_APPEND)
  369. {
  370. file->fpos = file->vnode->size;
  371. }
  372. else
  373. {
  374. file->fpos = 0;
  375. }
  376. RT_ASSERT(file->vnode->ref_count > 0);
  377. if(file->vnode->ref_count == 1)
  378. {
  379. rt_mutex_init(&file->vnode->lock, file->dentry->pathname, RT_IPC_FLAG_PRIO);
  380. }
  381. return 0;
  382. }
  383. static int dfs_tmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
  384. {
  385. rt_size_t size;
  386. struct tmpfs_file *d_file;
  387. struct tmpfs_sb *superblock;
  388. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  389. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  390. if (d_file == NULL)
  391. return -ENOENT;
  392. st->st_dev = (dev_t)(size_t)(dentry->mnt->dev_id);
  393. st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
  394. if (d_file->type == TMPFS_TYPE_DIR)
  395. {
  396. st->st_mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  397. }
  398. else
  399. {
  400. st->st_mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  401. }
  402. st->st_size = d_file->size;
  403. st->st_mtime = 0;
  404. return RT_EOK;
  405. }
  406. static int dfs_tmpfs_getdents(struct dfs_file *file,
  407. struct dirent *dirp,
  408. uint32_t count)
  409. {
  410. rt_size_t index, end;
  411. struct dirent *d;
  412. struct tmpfs_file *d_file, *n_file;
  413. rt_list_t *list;
  414. struct tmpfs_sb *superblock;
  415. d_file = (struct tmpfs_file *)file->vnode->data;
  416. rt_mutex_take(&file->vnode->lock, RT_WAITING_FOREVER);
  417. superblock = d_file->sb;
  418. RT_ASSERT(superblock != RT_NULL);
  419. RT_UNUSED(superblock);
  420. /* make integer count */
  421. count = (count / sizeof(struct dirent));
  422. if (count == 0)
  423. {
  424. rt_mutex_release(&file->vnode->lock);
  425. return -EINVAL;
  426. }
  427. end = file->fpos + count;
  428. index = 0;
  429. count = 0;
  430. rt_list_for_each(list, &d_file->subdirs)
  431. {
  432. n_file = rt_list_entry(list, struct tmpfs_file, sibling);
  433. if (index >= (rt_size_t)file->fpos)
  434. {
  435. d = dirp + count;
  436. if (d_file->type == TMPFS_TYPE_FILE)
  437. {
  438. d->d_type = DT_REG;
  439. }
  440. if (d_file->type == TMPFS_TYPE_DIR)
  441. {
  442. d->d_type = DT_DIR;
  443. }
  444. d->d_namlen = RT_NAME_MAX;
  445. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  446. rt_strncpy(d->d_name, n_file->name, TMPFS_NAME_MAX);
  447. count += 1;
  448. file->fpos += 1;
  449. }
  450. index += 1;
  451. if (index >= end)
  452. {
  453. break;
  454. }
  455. }
  456. rt_mutex_release(&file->vnode->lock);
  457. return count * sizeof(struct dirent);
  458. }
  459. static int dfs_tmpfs_unlink(struct dfs_dentry *dentry)
  460. {
  461. rt_size_t size;
  462. struct tmpfs_sb *superblock;
  463. struct tmpfs_file *d_file;
  464. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  465. RT_ASSERT(superblock != NULL);
  466. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  467. if (d_file == NULL)
  468. return -ENOENT;
  469. rt_spin_lock(&superblock->lock);
  470. rt_list_remove(&(d_file->sibling));
  471. rt_spin_unlock(&superblock->lock);
  472. if (rt_atomic_load(&(dentry->ref_count)) == 1)
  473. {
  474. if (d_file->data != NULL)
  475. {
  476. rt_free(d_file->data);
  477. d_file->data = RT_NULL;
  478. }
  479. rt_free(d_file);
  480. }
  481. else
  482. {
  483. d_file->fre_memory = RT_TRUE;
  484. }
  485. return RT_EOK;
  486. }
  487. static int dfs_tmpfs_rename(struct dfs_dentry *old_dentry, struct dfs_dentry *new_dentry)
  488. {
  489. struct tmpfs_file *d_file, *p_file;
  490. struct tmpfs_sb *superblock;
  491. rt_size_t size;
  492. char *parent_path;
  493. char file_name[TMPFS_NAME_MAX];
  494. superblock = (struct tmpfs_sb *)old_dentry->mnt->data;
  495. RT_ASSERT(superblock != NULL);
  496. d_file = dfs_tmpfs_lookup(superblock, new_dentry->pathname, &size);
  497. if (d_file != NULL)
  498. return -EEXIST;
  499. d_file = dfs_tmpfs_lookup(superblock, old_dentry->pathname, &size);
  500. if (d_file == NULL)
  501. return -ENOENT;
  502. parent_path = rt_malloc(DFS_PATH_MAX);
  503. if (!parent_path)
  504. {
  505. return -ENOMEM;
  506. }
  507. /* find parent file */
  508. _path_separate(new_dentry->pathname, parent_path, file_name);
  509. if (file_name[0] == '\0') /* it's root dir */
  510. {
  511. rt_free(parent_path);
  512. return -ENOENT;
  513. }
  514. /* open parent directory */
  515. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  516. RT_ASSERT(p_file != NULL);
  517. rt_spin_lock(&superblock->lock);
  518. rt_list_remove(&(d_file->sibling));
  519. rt_spin_unlock(&superblock->lock);
  520. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  521. rt_spin_lock(&superblock->lock);
  522. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  523. rt_spin_unlock(&superblock->lock);
  524. rt_free(parent_path);
  525. return RT_EOK;
  526. }
  527. static struct dfs_vnode *_dfs_tmpfs_lookup(struct dfs_dentry *dentry)
  528. {
  529. struct dfs_vnode *vnode = RT_NULL;
  530. rt_size_t size;
  531. struct tmpfs_sb *superblock;
  532. struct tmpfs_file *d_file;
  533. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  534. {
  535. return NULL;
  536. }
  537. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  538. d_file = dfs_tmpfs_lookup(superblock, dentry->pathname, &size);
  539. if (d_file)
  540. {
  541. vnode = dfs_vnode_create();
  542. if (vnode)
  543. {
  544. if (d_file->type == TMPFS_TYPE_DIR)
  545. {
  546. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  547. vnode->type = FT_DIRECTORY;
  548. }
  549. else
  550. {
  551. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  552. vnode->type = FT_REGULAR;
  553. #ifdef RT_USING_PAGECACHE
  554. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  555. #endif
  556. }
  557. vnode->mnt = dentry->mnt;
  558. vnode->data = d_file;
  559. vnode->size = d_file->size;
  560. }
  561. }
  562. return vnode;
  563. }
  564. static struct dfs_vnode *dfs_tmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  565. {
  566. struct dfs_vnode *vnode = RT_NULL;
  567. rt_size_t size;
  568. struct tmpfs_sb *superblock;
  569. struct tmpfs_file *d_file, *p_file;
  570. char *parent_path;
  571. char file_name[TMPFS_NAME_MAX];
  572. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  573. {
  574. return NULL;
  575. }
  576. superblock = (struct tmpfs_sb *)dentry->mnt->data;
  577. RT_ASSERT(superblock != NULL);
  578. parent_path = rt_malloc(DFS_PATH_MAX);
  579. if (!parent_path)
  580. {
  581. return NULL;
  582. }
  583. vnode = dfs_vnode_create();
  584. if (vnode)
  585. {
  586. /* find parent file */
  587. _path_separate(dentry->pathname, parent_path, file_name);
  588. if (file_name[0] == '\0') /* it's root dir */
  589. {
  590. rt_free(parent_path);
  591. dfs_vnode_destroy(vnode);
  592. return NULL;
  593. }
  594. /* open parent directory */
  595. p_file = dfs_tmpfs_lookup(superblock, parent_path, &size);
  596. if (p_file == NULL)
  597. {
  598. rt_free(parent_path);
  599. dfs_vnode_destroy(vnode);
  600. return NULL;
  601. }
  602. /* create a file entry */
  603. d_file = (struct tmpfs_file *)rt_calloc(1, sizeof(struct tmpfs_file));
  604. if (d_file == NULL)
  605. {
  606. rt_free(parent_path);
  607. dfs_vnode_destroy(vnode);
  608. return NULL;
  609. }
  610. superblock->df_size += sizeof(struct tmpfs_file);
  611. strncpy(d_file->name, file_name, TMPFS_NAME_MAX);
  612. rt_list_init(&(d_file->subdirs));
  613. rt_list_init(&(d_file->sibling));
  614. d_file->data = NULL;
  615. d_file->size = 0;
  616. d_file->sb = superblock;
  617. d_file->fre_memory = RT_FALSE;
  618. if (type == FT_DIRECTORY)
  619. {
  620. d_file->type = TMPFS_TYPE_DIR;
  621. vnode->mode = S_IFDIR | (S_IRUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
  622. vnode->type = FT_DIRECTORY;
  623. }
  624. else
  625. {
  626. d_file->type = TMPFS_TYPE_FILE;
  627. vnode->mode = S_IFREG | (S_IRWXU | S_IRWXG | S_IRWXO);
  628. vnode->type = FT_REGULAR;
  629. #ifdef RT_USING_PAGECACHE
  630. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_tmp_aspace_ops);
  631. #endif
  632. }
  633. rt_spin_lock(&superblock->lock);
  634. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  635. rt_spin_unlock(&superblock->lock);
  636. vnode->mnt = dentry->mnt;
  637. vnode->data = d_file;
  638. vnode->size = d_file->size;
  639. }
  640. rt_free(parent_path);
  641. return vnode;
  642. }
  643. static int dfs_tmpfs_free_vnode(struct dfs_vnode *vnode)
  644. {
  645. /* nothing to be freed */
  646. if (vnode && vnode->ref_count <= 1)
  647. {
  648. vnode->data = NULL;
  649. }
  650. return 0;
  651. }
  652. #ifdef RT_USING_PAGECACHE
  653. static ssize_t dfs_tmp_page_read(struct dfs_file *file, struct dfs_page *page)
  654. {
  655. int ret = -EINVAL;
  656. if (page->page)
  657. {
  658. off_t fpos = page->fpos;
  659. ret = dfs_tmpfs_read(file, page->page, page->size, &fpos);
  660. }
  661. return ret;
  662. }
  663. ssize_t dfs_tmp_page_write(struct dfs_page *page)
  664. {
  665. off_t pos;
  666. size_t count = 0;
  667. struct tmpfs_file *d_file;
  668. if (page->aspace->vnode->type == FT_DIRECTORY)
  669. {
  670. return -EISDIR;
  671. }
  672. d_file = (struct tmpfs_file *)(page->aspace->vnode->data);
  673. RT_ASSERT(d_file != RT_NULL);
  674. rt_mutex_take(&page->aspace->vnode->lock, RT_WAITING_FOREVER);
  675. if (page->len > 0)
  676. {
  677. pos = page->fpos;
  678. count = _dfs_tmpfs_write(d_file, page->page, page->len, &pos);
  679. }
  680. rt_mutex_release(&page->aspace->vnode->lock);
  681. return count;
  682. }
  683. #endif
  684. static int dfs_tmpfs_truncate(struct dfs_file *file, off_t offset)
  685. {
  686. struct tmpfs_file *d_file = RT_NULL;
  687. struct tmpfs_sb *superblock = RT_NULL;
  688. rt_uint8_t *ptr = RT_NULL;
  689. d_file = (struct tmpfs_file *)file->vnode->data;
  690. RT_ASSERT(d_file != RT_NULL);
  691. superblock = d_file->sb;
  692. RT_ASSERT(superblock != RT_NULL);
  693. ptr = rt_realloc(d_file->data, offset);
  694. if (ptr == RT_NULL)
  695. {
  696. rt_set_errno(-ENOMEM);
  697. return 0;
  698. }
  699. rt_spin_lock(&superblock->lock);
  700. superblock->df_size = offset;
  701. rt_spin_unlock(&superblock->lock);
  702. /* update d_file and file size */
  703. d_file->data = ptr;
  704. d_file->size = offset;
  705. file->vnode->size = d_file->size;
  706. LOG_D("tmpfile ptr:%x, size:%d", ptr, d_file->size);
  707. return 0;
  708. }
  709. static const struct dfs_file_ops _tmp_fops =
  710. {
  711. .open = dfs_tmpfs_open,
  712. .close = dfs_tmpfs_close,
  713. .ioctl = dfs_tmpfs_ioctl,
  714. .read = dfs_tmpfs_read,
  715. .write = dfs_tmpfs_write,
  716. .lseek = dfs_tmpfs_lseek,
  717. .getdents = dfs_tmpfs_getdents,
  718. .truncate = dfs_tmpfs_truncate,
  719. };
  720. static const struct dfs_filesystem_ops _tmpfs_ops =
  721. {
  722. .name = "tmp",
  723. .flags = DFS_FS_FLAG_DEFAULT,
  724. .default_fops = &_tmp_fops,
  725. .mount = dfs_tmpfs_mount,
  726. .umount = dfs_tmpfs_unmount,
  727. .statfs = dfs_tmpfs_statfs,
  728. .unlink = dfs_tmpfs_unlink,
  729. .stat = dfs_tmpfs_stat,
  730. .rename = dfs_tmpfs_rename,
  731. .lookup = _dfs_tmpfs_lookup,
  732. .create_vnode = dfs_tmpfs_create_vnode,
  733. .free_vnode = dfs_tmpfs_free_vnode
  734. };
  735. static struct dfs_filesystem_type _tmpfs =
  736. {
  737. .fs_ops = &_tmpfs_ops,
  738. };
  739. int dfs_tmpfs_init(void)
  740. {
  741. /* register tmp file system */
  742. dfs_register(&_tmpfs);
  743. return 0;
  744. }
  745. INIT_COMPONENT_EXPORT(dfs_tmpfs_init);