devtmpfs.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  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. * 2023-12-02 Shell Support of dynamic device
  12. */
  13. #include <rthw.h>
  14. #include <rtthread.h>
  15. #include <dfs.h>
  16. #include <dfs_fs.h>
  17. #include <dfs_dentry.h>
  18. #include <dfs_file.h>
  19. #include <dfs_mnt.h>
  20. #include <devfs.h>
  21. #include <unistd.h>
  22. #define TMPFS_MAGIC 0x0B0B0B0B
  23. #define TMPFS_TYPE_FILE 0x00
  24. #define TMPFS_TYPE_DIR 0x01
  25. #define TMPFS_TYPE_DYN_DEV 0x02 /* dynamic device */
  26. struct devtmpfs_sb;
  27. struct devtmpfs_file
  28. {
  29. char name[DIRENT_NAME_MAX]; /* file name */
  30. rt_uint32_t type; /* file type */
  31. rt_list_t subdirs; /* file subdir list */
  32. rt_list_t sibling; /* file sibling list */
  33. struct devtmpfs_sb *sb; /* superblock ptr */
  34. rt_uint32_t mode;
  35. char *link;
  36. };
  37. struct devtmpfs_sb
  38. {
  39. rt_uint32_t magic; /* TMPFS_MAGIC */
  40. struct devtmpfs_file root; /* root dir */
  41. rt_size_t df_size; /* df size */
  42. rt_list_t sibling; /* sb sibling list */
  43. struct rt_spinlock lock; /* tmpfs lock */
  44. };
  45. static struct dfs_file_ops _default_fops = { 0 };
  46. static int _path_separate(const char *path, char *parent_path, char *file_name)
  47. {
  48. const char *path_p, *path_q;
  49. RT_ASSERT(path[0] == '/');
  50. file_name[0] = '\0';
  51. path_p = path_q = &path[1];
  52. __next_dir:
  53. while (*path_q != '/' && *path_q != '\0')
  54. {
  55. path_q++;
  56. }
  57. if (path_q != path_p) /*sub dir*/
  58. {
  59. if (*path_q != '\0')
  60. {
  61. path_q++;
  62. path_p = path_q;
  63. goto __next_dir;
  64. }
  65. else /* Last level dir */
  66. {
  67. rt_memcpy(parent_path, path, path_p - path - 1);
  68. parent_path[path_p - path - 1] = '\0';
  69. rt_memcpy(file_name, path_p, path_q - path_p);
  70. file_name[path_q - path_p] = '\0';
  71. }
  72. }
  73. if (parent_path[0] == 0)
  74. {
  75. parent_path[0] = '/';
  76. parent_path[1] = '\0';
  77. }
  78. //LOG_D("parent_path: %s", parent_path);
  79. //LOG_D("file_name: %s", file_name);
  80. return 0;
  81. }
  82. static int _get_subdir(const char *path, char *name)
  83. {
  84. const char *subpath = path;
  85. while (*subpath == '/' && *subpath)
  86. subpath ++;
  87. while (*subpath != '/' && *subpath)
  88. {
  89. *name = *subpath;
  90. name ++;
  91. subpath ++;
  92. }
  93. return 0;
  94. }
  95. #if 0
  96. static int _free_subdir(struct devtmpfs_file *dfile)
  97. {
  98. struct devtmpfs_file *file;
  99. rt_list_t *list, *temp_list;
  100. struct devtmpfs_sb *superblock;
  101. RT_ASSERT(dfile->type == TMPFS_TYPE_DIR);
  102. rt_list_for_each_safe(list, temp_list, &dfile->subdirs)
  103. {
  104. file = rt_list_entry(list, struct devtmpfs_file, sibling);
  105. if (file->type == TMPFS_TYPE_DIR)
  106. {
  107. _free_subdir(file);
  108. }
  109. if (file->link)
  110. {
  111. rt_free(file->link);
  112. }
  113. superblock = file->sb;
  114. RT_ASSERT(superblock);
  115. rt_spin_lock(&superblock->lock);
  116. rt_list_remove(&(file->sibling));
  117. rt_spin_unlock(&superblock->lock);
  118. rt_free(file);
  119. }
  120. return 0;
  121. }
  122. #endif
  123. static int devtmpfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
  124. {
  125. struct devtmpfs_sb *superblock;
  126. superblock = rt_calloc(1, sizeof(struct devtmpfs_sb));
  127. if (superblock)
  128. {
  129. superblock->df_size = sizeof(struct devtmpfs_sb);
  130. superblock->magic = TMPFS_MAGIC;
  131. rt_list_init(&superblock->sibling);
  132. superblock->root.name[0] = '/';
  133. superblock->root.sb = superblock;
  134. superblock->root.type = TMPFS_TYPE_DIR;
  135. superblock->root.mode = S_IFDIR | (S_IRUSR | S_IRGRP | S_IROTH) | (S_IXUSR | S_IXGRP | S_IXOTH);
  136. rt_list_init(&superblock->root.sibling);
  137. rt_list_init(&superblock->root.subdirs);
  138. rt_spin_lock_init(&superblock->lock);
  139. mnt->data = superblock;
  140. }
  141. else
  142. {
  143. return -RT_ERROR;
  144. }
  145. return RT_EOK;
  146. }
  147. static int devtmpfs_unmount(struct dfs_mnt *mnt)
  148. {
  149. #if 0
  150. struct devtmpfs_sb *superblock;
  151. /* FIXME: don't unmount on busy. */
  152. superblock = (struct devtmpfs_sb *)mnt->data;
  153. RT_ASSERT(superblock != NULL);
  154. mnt->data = NULL;
  155. _free_subdir(&(superblock->root));
  156. rt_free(superblock);
  157. #endif
  158. return -RT_ERROR;
  159. }
  160. static struct devtmpfs_file *devtmpfs_file_lookup(struct devtmpfs_sb *superblock, const char *path)
  161. {
  162. const char *subpath, *curpath, *filename = RT_NULL;
  163. char subdir_name[DIRENT_NAME_MAX];
  164. struct devtmpfs_file *file, *curfile;
  165. rt_list_t *list;
  166. subpath = path;
  167. while (*subpath == '/' && *subpath)
  168. subpath ++;
  169. if (! *subpath) /* is root directory */
  170. {
  171. return &(superblock->root);
  172. }
  173. curpath = subpath;
  174. curfile = &superblock->root;
  175. find_subpath:
  176. while (*subpath != '/' && *subpath)
  177. subpath ++;
  178. if (! *subpath) /* is last directory */
  179. filename = curpath;
  180. else
  181. subpath ++; /* skip '/' */
  182. memset(subdir_name, 0, DIRENT_NAME_MAX);
  183. _get_subdir(curpath, subdir_name);
  184. rt_spin_lock(&superblock->lock);
  185. rt_list_for_each(list, &curfile->subdirs)
  186. {
  187. file = rt_list_entry(list, struct devtmpfs_file, sibling);
  188. if (filename) /* find file */
  189. {
  190. if (rt_strcmp(file->name, filename) == 0)
  191. {
  192. rt_spin_unlock(&superblock->lock);
  193. return file;
  194. }
  195. }
  196. else if (rt_strcmp(file->name, subdir_name) == 0)
  197. {
  198. curpath = subpath;
  199. curfile = file;
  200. rt_spin_unlock(&superblock->lock);
  201. goto find_subpath;
  202. }
  203. }
  204. rt_spin_unlock(&superblock->lock);
  205. /* not found */
  206. return NULL;
  207. }
  208. static int devtmpfs_statfs(struct dfs_mnt *mnt, struct statfs *buf)
  209. {
  210. struct devtmpfs_sb *superblock;
  211. RT_ASSERT(mnt != NULL);
  212. RT_ASSERT(buf != NULL);
  213. superblock = (struct devtmpfs_sb *)mnt->data;
  214. RT_ASSERT(superblock != NULL);
  215. buf->f_bsize = 512;
  216. buf->f_blocks = (superblock->df_size + 511) / 512;
  217. buf->f_bfree = 1;
  218. buf->f_bavail = buf->f_bfree;
  219. return RT_EOK;
  220. }
  221. static int devtmpfs_stat(struct dfs_dentry *dentry, struct stat *st)
  222. {
  223. struct dfs_vnode *vnode;
  224. if (dentry && dentry->vnode)
  225. {
  226. vnode = dentry->vnode;
  227. st->st_dev = (dev_t)(long)(dentry->mnt->dev_id);
  228. st->st_ino = (ino_t)dfs_dentry_full_path_crc32(dentry);
  229. st->st_gid = vnode->gid;
  230. st->st_uid = vnode->uid;
  231. st->st_mode = vnode->mode;
  232. st->st_nlink = vnode->nlink;
  233. st->st_size = vnode->size;
  234. st->st_mtim.tv_nsec = vnode->mtime.tv_nsec;
  235. st->st_mtim.tv_sec = vnode->mtime.tv_sec;
  236. st->st_ctim.tv_nsec = vnode->ctime.tv_nsec;
  237. st->st_ctim.tv_sec = vnode->ctime.tv_sec;
  238. st->st_atim.tv_nsec = vnode->atime.tv_nsec;
  239. st->st_atim.tv_sec = vnode->atime.tv_sec;
  240. }
  241. return RT_EOK;
  242. }
  243. static int devtmpfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
  244. {
  245. struct devtmpfs_file *d_file;
  246. struct devtmpfs_sb *superblock;
  247. RT_ASSERT(file);
  248. RT_ASSERT(file->dentry);
  249. RT_ASSERT(file->dentry->mnt);
  250. superblock = (struct devtmpfs_sb *)file->dentry->mnt->data;
  251. RT_ASSERT(superblock);
  252. d_file = devtmpfs_file_lookup(superblock, file->dentry->pathname);
  253. if (d_file)
  254. {
  255. rt_size_t index, end;
  256. struct dirent *d;
  257. struct devtmpfs_file *n_file;
  258. rt_list_t *list;
  259. /* make integer count */
  260. count = (count / sizeof(struct dirent));
  261. if (count == 0)
  262. {
  263. return -EINVAL;
  264. }
  265. end = file->fpos + count;
  266. index = 0;
  267. count = 0;
  268. rt_list_for_each(list, &d_file->subdirs)
  269. {
  270. if (index >= (rt_size_t)file->fpos)
  271. {
  272. n_file = rt_list_entry(list, struct devtmpfs_file, sibling);
  273. d = dirp + count;
  274. if (n_file->type == TMPFS_TYPE_FILE)
  275. {
  276. d->d_type = DT_REG;
  277. }
  278. if (n_file->type == TMPFS_TYPE_DIR)
  279. {
  280. d->d_type = DT_DIR;
  281. }
  282. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  283. rt_strncpy(d->d_name, n_file->name, DIRENT_NAME_MAX);
  284. d->d_namlen = rt_strlen(d->d_name);
  285. count += 1;
  286. file->fpos += 1;
  287. }
  288. index += 1;
  289. if (index >= end)
  290. {
  291. break;
  292. }
  293. }
  294. }
  295. return count * sizeof(struct dirent);
  296. }
  297. static int devtmpfs_symlink(struct dfs_dentry *parent_dentry, const char *target, const char *linkpath)
  298. {
  299. int ret = RT_EOK;
  300. struct devtmpfs_file *p_file, *l_file;
  301. struct devtmpfs_sb *superblock;
  302. RT_ASSERT(parent_dentry);
  303. RT_ASSERT(parent_dentry->mnt);
  304. superblock = (struct devtmpfs_sb *)parent_dentry->mnt->data;
  305. RT_ASSERT(superblock);
  306. p_file = devtmpfs_file_lookup(superblock, parent_dentry->pathname);
  307. if (p_file)
  308. {
  309. l_file = (struct devtmpfs_file *)rt_calloc(1, sizeof(struct devtmpfs_file));
  310. if (l_file)
  311. {
  312. superblock->df_size += sizeof(struct devtmpfs_file);
  313. strncpy(l_file->name, linkpath, DIRENT_NAME_MAX - 1);
  314. rt_list_init(&(l_file->subdirs));
  315. rt_list_init(&(l_file->sibling));
  316. l_file->sb = superblock;
  317. l_file->type = TMPFS_TYPE_FILE;
  318. l_file->mode = p_file->mode;
  319. l_file->mode &= ~S_IFMT;
  320. l_file->mode |= S_IFLNK;
  321. l_file->link = rt_strdup(target);
  322. rt_spin_lock(&superblock->lock);
  323. rt_list_insert_after(&(p_file->subdirs), &(l_file->sibling));
  324. rt_spin_unlock(&superblock->lock);
  325. }
  326. }
  327. return ret;
  328. }
  329. static int devtmpfs_readlink(struct dfs_dentry *dentry, char *buf, int len)
  330. {
  331. int ret = 0;
  332. struct devtmpfs_file *d_file;
  333. struct devtmpfs_sb *superblock;
  334. RT_ASSERT(dentry);
  335. RT_ASSERT(dentry->mnt);
  336. superblock = (struct devtmpfs_sb *)dentry->mnt->data;
  337. RT_ASSERT(superblock);
  338. d_file = devtmpfs_file_lookup(superblock, dentry->pathname);
  339. if (d_file)
  340. {
  341. if (d_file->link)
  342. {
  343. if (d_file->type == TMPFS_TYPE_DYN_DEV)
  344. {
  345. rt_device_t device = (void *)d_file->link;
  346. buf[0] = '\0';
  347. ret = device->readlink(device, buf, len);
  348. if (ret == 0)
  349. {
  350. buf[len - 1] = '\0';
  351. ret = rt_strlen(buf);
  352. }
  353. else
  354. {
  355. ret = 0;
  356. }
  357. }
  358. else
  359. {
  360. rt_strncpy(buf, (const char *)d_file->link, len);
  361. buf[len - 1] = '\0';
  362. ret = rt_strlen(buf);
  363. }
  364. }
  365. }
  366. return ret;
  367. }
  368. static int devtmpfs_unlink(struct dfs_dentry *dentry)
  369. {
  370. struct devtmpfs_file *d_file;
  371. struct devtmpfs_sb *superblock;
  372. RT_ASSERT(dentry);
  373. RT_ASSERT(dentry->mnt);
  374. superblock = (struct devtmpfs_sb *)dentry->mnt->data;
  375. RT_ASSERT(superblock);
  376. d_file = devtmpfs_file_lookup(superblock, dentry->pathname);
  377. if (d_file)
  378. {
  379. if (d_file->link && d_file->type != TMPFS_TYPE_DYN_DEV)
  380. {
  381. rt_free(d_file->link);
  382. }
  383. rt_spin_lock(&superblock->lock);
  384. rt_list_remove(&(d_file->sibling));
  385. rt_spin_unlock(&superblock->lock);
  386. rt_free(d_file);
  387. }
  388. return RT_EOK;
  389. }
  390. static int devtmpfs_setattr(struct dfs_dentry *dentry, struct dfs_attr *attr)
  391. {
  392. struct devtmpfs_file *d_file;
  393. struct devtmpfs_sb *superblock;
  394. RT_ASSERT(dentry);
  395. RT_ASSERT(dentry->mnt);
  396. superblock = (struct devtmpfs_sb *)dentry->mnt->data;
  397. RT_ASSERT(superblock);
  398. d_file = devtmpfs_file_lookup(superblock, dentry->pathname);
  399. if (d_file)
  400. {
  401. d_file->mode &= ~0xFFF;
  402. d_file->mode |= attr->st_mode & 0xFFF;
  403. return RT_EOK;
  404. }
  405. return -RT_ERROR;
  406. }
  407. static struct dfs_vnode *devtmpfs_create_vnode(struct dfs_dentry *dentry, int type, mode_t mode)
  408. {
  409. struct dfs_vnode *vnode = RT_NULL;
  410. struct devtmpfs_sb *superblock;
  411. struct devtmpfs_file *d_file, *p_file;
  412. char parent_path[DFS_PATH_MAX], file_name[DIRENT_NAME_MAX];
  413. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  414. {
  415. return NULL;
  416. }
  417. superblock = (struct devtmpfs_sb *)dentry->mnt->data;
  418. RT_ASSERT(superblock != NULL);
  419. vnode = dfs_vnode_create();
  420. if (vnode)
  421. {
  422. /* find parent file */
  423. _path_separate(dentry->pathname, parent_path, file_name);
  424. if (file_name[0] == '\0') /* it's root dir */
  425. {
  426. dfs_vnode_destroy(vnode);
  427. return NULL;
  428. }
  429. /* open parent directory */
  430. p_file = devtmpfs_file_lookup(superblock, parent_path);
  431. if (p_file == NULL)
  432. {
  433. dfs_vnode_destroy(vnode);
  434. return NULL;
  435. }
  436. /* create a file entry */
  437. d_file = (struct devtmpfs_file *)rt_calloc(1, sizeof(struct devtmpfs_file));
  438. if (d_file == NULL)
  439. {
  440. dfs_vnode_destroy(vnode);
  441. return NULL;
  442. }
  443. superblock->df_size += sizeof(struct devtmpfs_file);
  444. strncpy(d_file->name, file_name, DIRENT_NAME_MAX);
  445. rt_list_init(&(d_file->subdirs));
  446. rt_list_init(&(d_file->sibling));
  447. d_file->sb = superblock;
  448. vnode->nlink = 1;
  449. vnode->size = 0;
  450. vnode->mode = mode;
  451. vnode->mnt = dentry->mnt;
  452. vnode->fops = &_default_fops;
  453. if (type == FT_DIRECTORY)
  454. {
  455. d_file->type = TMPFS_TYPE_DIR;
  456. vnode->type = FT_DIRECTORY;
  457. vnode->mode &= ~S_IFMT;
  458. vnode->mode |= S_IFDIR;
  459. }
  460. else
  461. {
  462. d_file->type = TMPFS_TYPE_FILE;
  463. vnode->type = FT_DEVICE;
  464. }
  465. d_file->mode = vnode->mode;
  466. rt_spin_lock(&superblock->lock);
  467. rt_list_insert_after(&(p_file->subdirs), &(d_file->sibling));
  468. rt_spin_unlock(&superblock->lock);
  469. }
  470. return vnode;
  471. }
  472. static struct dfs_vnode *devtmpfs_lookup(struct dfs_dentry *dentry)
  473. {
  474. struct dfs_vnode *vnode = RT_NULL;
  475. struct devtmpfs_sb *superblock;
  476. struct devtmpfs_file *d_file;
  477. if (dentry == NULL || dentry->mnt == NULL || dentry->mnt->data == NULL)
  478. {
  479. return NULL;
  480. }
  481. superblock = (struct devtmpfs_sb *)dentry->mnt->data;
  482. d_file = devtmpfs_file_lookup(superblock, dentry->pathname);
  483. if (d_file)
  484. {
  485. vnode = dfs_vnode_create();
  486. if (vnode)
  487. {
  488. vnode->nlink = 1;
  489. vnode->size = 0;
  490. vnode->mnt = dentry->mnt;
  491. vnode->fops = &_default_fops;
  492. vnode->mode = d_file->mode;
  493. if (d_file->type == TMPFS_TYPE_DIR)
  494. {
  495. vnode->type = FT_DIRECTORY;
  496. }
  497. else if (d_file->link)
  498. {
  499. vnode->type = FT_SYMLINK;
  500. }
  501. else
  502. {
  503. vnode->type = FT_DEVICE;
  504. }
  505. }
  506. }
  507. else
  508. {
  509. rt_device_t device = RT_NULL;
  510. device = rt_device_find(&dentry->pathname[1]);
  511. if (device)
  512. {
  513. vnode = devtmpfs_create_vnode(dentry, FT_REGULAR, dfs_devfs_device_to_mode(device));
  514. if (device->flag & RT_DEVICE_FLAG_DYNAMIC)
  515. {
  516. d_file = devtmpfs_file_lookup(superblock, dentry->pathname);
  517. d_file->type = TMPFS_TYPE_DYN_DEV;
  518. d_file->link = (char *)device;
  519. }
  520. }
  521. }
  522. return vnode;
  523. }
  524. static int devtmpfs_free_vnode(struct dfs_vnode *vnode)
  525. {
  526. return RT_EOK;
  527. }
  528. static const struct dfs_filesystem_ops _devtmpfs_ops =
  529. {
  530. .name = "devtmpfs",
  531. .flags = DFS_FS_FLAG_DEFAULT,
  532. .default_fops = &_default_fops,
  533. .mount = devtmpfs_mount,
  534. .umount = devtmpfs_unmount,
  535. .symlink = devtmpfs_symlink,
  536. .readlink = devtmpfs_readlink,
  537. .unlink = devtmpfs_unlink,
  538. .setattr = devtmpfs_setattr,
  539. .statfs = devtmpfs_statfs,
  540. .stat = devtmpfs_stat,
  541. .lookup = devtmpfs_lookup,
  542. .create_vnode = devtmpfs_create_vnode,
  543. .free_vnode = devtmpfs_free_vnode
  544. };
  545. static struct dfs_filesystem_type _devtmpfs =
  546. {
  547. .fs_ops = &_devtmpfs_ops,
  548. };
  549. int dfs_devtmpfs_init(void)
  550. {
  551. _default_fops = *dfs_devfs_fops();
  552. _default_fops.getdents = devtmpfs_getdents;
  553. /* register file system */
  554. dfs_register(&_devtmpfs);
  555. dfs_mount(RT_NULL, "/dev", "devtmpfs", 0, RT_NULL);
  556. dfs_devfs_update();
  557. return 0;
  558. }
  559. INIT_COMPONENT_EXPORT(dfs_devtmpfs_init);