dfs_fs.c 14 KB

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