dfs_fs.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /*
  2. * File : dfs_fs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2005-02-22 Bernard The first version.
  23. * 2010-06-30 Bernard Optimize for RT-Thread RTOS
  24. * 2011-03-12 Bernard fix the filesystem lookup issue.
  25. * 2017-11-30 Bernard fix the filesystem_operation_table issue.
  26. * 2017-12-05 Bernard fix the fs type search issue in mkfs.
  27. */
  28. #include <dfs_fs.h>
  29. #include <dfs_file.h>
  30. #include "dfs_private.h"
  31. /**
  32. * @addtogroup FsApi
  33. */
  34. /*@{*/
  35. /**
  36. * this function will register a file system instance to device file system.
  37. *
  38. * @param ops the file system instance to be registered.
  39. *
  40. * @return 0 on successful, -1 on failed.
  41. */
  42. int dfs_register(const struct dfs_filesystem_ops *ops)
  43. {
  44. int ret = RT_EOK;
  45. const struct dfs_filesystem_ops **empty = NULL;
  46. const struct dfs_filesystem_ops **iter;
  47. /* lock filesystem */
  48. dfs_lock();
  49. /* check if this filesystem was already registered */
  50. for (iter = &filesystem_operation_table[0];
  51. iter < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; iter ++)
  52. {
  53. /* find out an empty filesystem type entry */
  54. if (*iter == NULL)
  55. (empty == NULL) ? (empty = iter) : 0;
  56. else if (strcmp((*iter)->name, ops->name) == 0)
  57. {
  58. rt_set_errno(-EEXIST);
  59. ret = -1;
  60. break;
  61. }
  62. }
  63. /* save the filesystem's operations */
  64. if (empty == NULL)
  65. {
  66. rt_set_errno(-ENOSPC);
  67. ret = -1;
  68. }
  69. else if (ret == RT_EOK)
  70. {
  71. *empty = ops;
  72. }
  73. dfs_unlock();
  74. return ret;
  75. }
  76. /**
  77. * this function will return the file system mounted on specified path.
  78. *
  79. * @param path the specified path string.
  80. *
  81. * @return the found file system or NULL if no file system mounted on
  82. * specified path
  83. */
  84. struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
  85. {
  86. struct dfs_filesystem *iter;
  87. struct dfs_filesystem *fs = NULL;
  88. uint32_t fspath, prefixlen;
  89. prefixlen = 0;
  90. RT_ASSERT(path);
  91. /* lock filesystem */
  92. dfs_lock();
  93. /* lookup it in the filesystem table */
  94. for (iter = &filesystem_table[0];
  95. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  96. {
  97. if ((iter->path == NULL) || (iter->ops == NULL))
  98. continue;
  99. fspath = strlen(iter->path);
  100. if ((fspath < prefixlen)
  101. || (strncmp(iter->path, path, fspath) != 0))
  102. continue;
  103. /* check next path separator */
  104. if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
  105. continue;
  106. fs = iter;
  107. prefixlen = fspath;
  108. }
  109. dfs_unlock();
  110. return fs;
  111. }
  112. /**
  113. * this function will return the mounted path for specified device.
  114. *
  115. * @param device the device object which is mounted.
  116. *
  117. * @return the mounted path or NULL if none device mounted.
  118. */
  119. const char* dfs_filesystem_get_mounted_path(struct rt_device* device)
  120. {
  121. const char* path = NULL;
  122. struct dfs_filesystem *iter;
  123. dfs_lock();
  124. for (iter = &filesystem_table[0];
  125. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  126. {
  127. /* fint the mounted device */
  128. if (iter->ops == NULL) continue;
  129. else if (iter->dev_id == device)
  130. {
  131. path = iter->path;
  132. break;
  133. }
  134. }
  135. /* release filesystem_table lock */
  136. dfs_unlock();
  137. return path;
  138. }
  139. /**
  140. * this function will fetch the partition table on specified buffer.
  141. *
  142. * @param part the returned partition structure.
  143. * @param buf the buffer contains partition table.
  144. * @param pindex the index of partition table to fetch.
  145. *
  146. * @return RT_EOK on successful or -RT_ERROR on failed.
  147. */
  148. int dfs_filesystem_get_partition(struct dfs_partition *part,
  149. uint8_t *buf,
  150. uint32_t pindex)
  151. {
  152. #define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
  153. #define DPT_ITEM_SIZE 16 /* partition item size */
  154. uint8_t *dpt;
  155. uint8_t type;
  156. RT_ASSERT(part != NULL);
  157. RT_ASSERT(buf != NULL);
  158. dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
  159. /* check if it is a valid partition table */
  160. if ((*dpt != 0x80) && (*dpt != 0x00))
  161. return -EIO;
  162. /* get partition type */
  163. type = *(dpt+4);
  164. if (type == 0)
  165. return -EIO;
  166. /* set partition information
  167. * size is the number of 512-Byte */
  168. part->type = type;
  169. part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24;
  170. part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;
  171. rt_kprintf("found part[%d], begin: %d, size: ",
  172. pindex, part->offset*512);
  173. if ((part->size>>11) == 0)
  174. rt_kprintf("%d%s",part->size>>1,"KB\n"); /* KB */
  175. else
  176. {
  177. unsigned int part_size;
  178. part_size = part->size >> 11; /* MB */
  179. if ((part_size>>10) == 0)
  180. rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\n");
  181. else
  182. rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\n");
  183. }
  184. return RT_EOK;
  185. }
  186. /**
  187. * this function will mount a file system on a specified path.
  188. *
  189. * @param device_name the name of device which includes a file system.
  190. * @param path the path to mount a file system
  191. * @param filesystemtype the file system type
  192. * @param rwflag the read/write etc. flag.
  193. * @param data the private data(parameter) for this file system.
  194. *
  195. * @return 0 on successful or -1 on failed.
  196. */
  197. int dfs_mount(const char *device_name,
  198. const char *path,
  199. const char *filesystemtype,
  200. unsigned long rwflag,
  201. const void *data)
  202. {
  203. const struct dfs_filesystem_ops **ops;
  204. struct dfs_filesystem *iter;
  205. struct dfs_filesystem *fs = NULL;
  206. char *fullpath = NULL;
  207. rt_device_t dev_id;
  208. /* open specific device */
  209. if (device_name == NULL)
  210. {
  211. /* which is a non-device filesystem mount */
  212. dev_id = NULL;
  213. }
  214. else if ((dev_id = rt_device_find(device_name)) == NULL)
  215. {
  216. /* no this device */
  217. rt_set_errno(-ENODEV);
  218. return -1;
  219. }
  220. /* find out the specific filesystem */
  221. dfs_lock();
  222. for (ops = &filesystem_operation_table[0];
  223. ops < &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX]; ops++)
  224. if ((*ops != NULL) && (strcmp((*ops)->name, filesystemtype) == 0))
  225. break;
  226. dfs_unlock();
  227. if (ops == &filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX])
  228. {
  229. /* can't find filesystem */
  230. rt_set_errno(-ENODEV);
  231. return -1;
  232. }
  233. /* check if there is mount implementation */
  234. if ((*ops == NULL) || ((*ops)->mount == NULL))
  235. {
  236. rt_set_errno(-ENOSYS);
  237. return -1;
  238. }
  239. /* make full path for special file */
  240. fullpath = dfs_normalize_path(NULL, path);
  241. if (fullpath == NULL) /* not an abstract path */
  242. {
  243. rt_set_errno(-ENOTDIR);
  244. return -1;
  245. }
  246. /* Check if the path exists or not, raw APIs call, fixme */
  247. if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
  248. {
  249. struct dfs_fd fd;
  250. if (dfs_file_open(&fd, fullpath, O_RDONLY | O_DIRECTORY) < 0)
  251. {
  252. rt_free(fullpath);
  253. rt_set_errno(-ENOTDIR);
  254. return -1;
  255. }
  256. dfs_file_close(&fd);
  257. }
  258. /* check whether the file system mounted or not in the filesystem table
  259. * if it is unmounted yet, find out an empty entry */
  260. dfs_lock();
  261. for (iter = &filesystem_table[0];
  262. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  263. {
  264. /* check if it is an empty filesystem table entry? if it is, save fs */
  265. if (iter->ops == NULL)
  266. (fs == NULL) ? (fs = iter) : 0;
  267. /* check if the PATH is mounted */
  268. else if (strcmp(iter->path, path) == 0)
  269. {
  270. rt_set_errno(-EINVAL);
  271. goto err1;
  272. }
  273. }
  274. if ((fs == NULL) && (iter == &filesystem_table[DFS_FILESYSTEMS_MAX]))
  275. {
  276. rt_set_errno(-ENOSPC);
  277. goto err1;
  278. }
  279. /* register file system */
  280. fs->path = fullpath;
  281. fs->ops = *ops;
  282. fs->dev_id = dev_id;
  283. /* release filesystem_table lock */
  284. dfs_unlock();
  285. /* open device, but do not check the status of device */
  286. if (dev_id != NULL)
  287. {
  288. if (rt_device_open(fs->dev_id,
  289. RT_DEVICE_OFLAG_RDWR) != RT_EOK)
  290. {
  291. /* The underlaying device has error, clear the entry. */
  292. dfs_lock();
  293. memset(fs, 0, sizeof(struct dfs_filesystem));
  294. goto err1;
  295. }
  296. }
  297. /* call mount of this filesystem */
  298. if ((*ops)->mount(fs, rwflag, data) < 0)
  299. {
  300. /* close device */
  301. if (dev_id != NULL)
  302. rt_device_close(fs->dev_id);
  303. /* mount failed */
  304. dfs_lock();
  305. /* clear filesystem table entry */
  306. memset(fs, 0, sizeof(struct dfs_filesystem));
  307. goto err1;
  308. }
  309. return 0;
  310. err1:
  311. dfs_unlock();
  312. rt_free(fullpath);
  313. return -1;
  314. }
  315. /**
  316. * this function will unmount a file system on specified path.
  317. *
  318. * @param specialfile the specified path which mounted a file system.
  319. *
  320. * @return 0 on successful or -1 on failed.
  321. */
  322. int dfs_unmount(const char *specialfile)
  323. {
  324. char *fullpath;
  325. struct dfs_filesystem *iter;
  326. struct dfs_filesystem *fs = NULL;
  327. fullpath = dfs_normalize_path(NULL, specialfile);
  328. if (fullpath == NULL)
  329. {
  330. rt_set_errno(-ENOTDIR);
  331. return -1;
  332. }
  333. /* lock filesystem */
  334. dfs_lock();
  335. for (iter = &filesystem_table[0];
  336. iter < &filesystem_table[DFS_FILESYSTEMS_MAX]; iter++)
  337. {
  338. /* check if the PATH is mounted */
  339. if ((iter->path != NULL) && (strcmp(iter->path, fullpath) == 0))
  340. {
  341. fs = iter;
  342. break;
  343. }
  344. }
  345. if (fs == NULL ||
  346. fs->ops->unmount == NULL ||
  347. fs->ops->unmount(fs) < 0)
  348. {
  349. goto err1;
  350. }
  351. /* close device, but do not check the status of device */
  352. if (fs->dev_id != NULL)
  353. rt_device_close(fs->dev_id);
  354. if (fs->path != NULL)
  355. rt_free(fs->path);
  356. /* clear this filesystem table entry */
  357. memset(fs, 0, sizeof(struct dfs_filesystem));
  358. dfs_unlock();
  359. rt_free(fullpath);
  360. return 0;
  361. err1:
  362. dfs_unlock();
  363. rt_free(fullpath);
  364. return -1;
  365. }
  366. /**
  367. * make a file system on the special device
  368. *
  369. * @param fs_name the file system name
  370. * @param device_name the special device name
  371. *
  372. * @return 0 on successful, otherwise failed.
  373. */
  374. int dfs_mkfs(const char *fs_name, const char *device_name)
  375. {
  376. int index;
  377. rt_device_t dev_id = NULL;
  378. /* check device name, and it should not be NULL */
  379. if (device_name != NULL)
  380. dev_id = rt_device_find(device_name);
  381. if (dev_id == NULL)
  382. {
  383. rt_set_errno(-ENODEV);
  384. return -1;
  385. }
  386. /* lock file system */
  387. dfs_lock();
  388. /* find the file system operations */
  389. for (index = 0; index <= DFS_FILESYSTEM_TYPES_MAX; index ++)
  390. {
  391. if (filesystem_operation_table[index] != NULL &&
  392. strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
  393. break;
  394. }
  395. dfs_unlock();
  396. if (index <= DFS_FILESYSTEM_TYPES_MAX)
  397. {
  398. /* find file system operation */
  399. const struct dfs_filesystem_ops *ops = filesystem_operation_table[index];
  400. if (ops->mkfs == NULL)
  401. {
  402. rt_set_errno(-ENOSYS);
  403. return -1;
  404. }
  405. return ops->mkfs(dev_id);
  406. }
  407. rt_kprintf("Can not find the file system which named as %s.\n", fs_name);
  408. return -1;
  409. }
  410. /**
  411. * this function will return the information about a mounted file system.
  412. *
  413. * @param path the path which mounted file system.
  414. * @param buffer the buffer to save the returned information.
  415. *
  416. * @return 0 on successful, others on failed.
  417. */
  418. int dfs_statfs(const char *path, struct statfs *buffer)
  419. {
  420. struct dfs_filesystem *fs;
  421. fs = dfs_filesystem_lookup(path);
  422. if (fs != NULL)
  423. {
  424. if (fs->ops->statfs != NULL)
  425. return fs->ops->statfs(fs, buffer);
  426. }
  427. return -1;
  428. }
  429. #ifdef RT_USING_DFS_MNTTABLE
  430. int dfs_mount_table(void)
  431. {
  432. int index = 0;
  433. while (1)
  434. {
  435. if (mount_table[index].path == NULL) break;
  436. if (dfs_mount(mount_table[index].device_name,
  437. mount_table[index].path,
  438. mount_table[index].filesystemtype,
  439. mount_table[index].rwflag,
  440. mount_table[index].data) != 0)
  441. {
  442. rt_kprintf("mount fs[%s] on %s failed.\n", mount_table[index].filesystemtype,
  443. mount_table[index].path);
  444. return -RT_ERROR;
  445. }
  446. index ++;
  447. }
  448. return 0;
  449. }
  450. INIT_ENV_EXPORT(dfs_mount_table);
  451. #endif
  452. #ifdef RT_USING_FINSH
  453. #include <finsh.h>
  454. void mkfs(const char *fs_name, const char *device_name)
  455. {
  456. dfs_mkfs(fs_name, device_name);
  457. }
  458. FINSH_FUNCTION_EXPORT(mkfs, make a file system);
  459. int df(const char *path)
  460. {
  461. int result;
  462. int minor = 0;
  463. long long cap;
  464. struct statfs buffer;
  465. int unit_index = 0;
  466. char *unit_str[] = {"KB", "MB", "GB"};
  467. result = dfs_statfs(path ? path : NULL, &buffer);
  468. if (result != 0)
  469. {
  470. rt_kprintf("dfs_statfs failed.\n");
  471. return -1;
  472. }
  473. cap = ((long long)buffer.f_bsize) * ((long long)buffer.f_bfree) / 1024LL;
  474. for (unit_index = 0; unit_index < 2; unit_index ++)
  475. {
  476. if (cap < 1024) break;
  477. minor = (cap % 1024) * 10 / 1024; /* only one decimal point */
  478. cap = cap / 1024;
  479. }
  480. rt_kprintf("disk free: %d.%d %s [ %d block, %d bytes per block ]\n",
  481. (unsigned long)cap, minor, unit_str[unit_index], buffer.f_bfree, buffer.f_bsize);
  482. return 0;
  483. }
  484. FINSH_FUNCTION_EXPORT(df, get disk free);
  485. #endif
  486. /* @} */