dfs_tmpfs.c 21 KB

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