dfs_fs.c 15 KB

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