dfs_tmpfs.c 19 KB

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