dfs_fs.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * File : dfs_fs.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2005-02-22 Bernard The first version.
  13. * 2010-06-30 Bernard Optimize for RT-Thread RTOS
  14. * 2011-03-12 Bernard fix the filesystem lookup issue.
  15. */
  16. #include <dfs_fs.h>
  17. #include <dfs_file.h>
  18. /**
  19. * @addtogroup FsApi
  20. */
  21. /*@{*/
  22. /**
  23. * this function will register a file system instance to device file system.
  24. *
  25. * @param ops the file system instance to be registered.
  26. *
  27. * @return 0 on sucessful, -1 on failed.
  28. */
  29. int dfs_register(const struct dfs_filesystem_operation *ops)
  30. {
  31. int index, result;
  32. result = 0;
  33. /* lock filesystem */
  34. dfs_lock();
  35. /* check if this filesystem was already registered */
  36. for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
  37. {
  38. if (filesystem_operation_table[index] != RT_NULL &&
  39. strcmp(filesystem_operation_table[index]->name, ops->name) == 0)
  40. {
  41. result = -1;
  42. goto err;
  43. }
  44. }
  45. /* find out an empty filesystem type entry */
  46. for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX && filesystem_operation_table[index] != RT_NULL;
  47. index++) ;
  48. /* filesystem type table full */
  49. if (index == DFS_FILESYSTEM_TYPES_MAX)
  50. {
  51. result = -1;
  52. goto err;
  53. }
  54. /* save the filesystem's operations */
  55. filesystem_operation_table[index] = ops;
  56. err:
  57. dfs_unlock();
  58. return result;
  59. }
  60. /**
  61. * this function will return the file system mounted on specified path.
  62. *
  63. * @param path the specified path string.
  64. *
  65. * @return the found file system or NULL if no file system mounted on
  66. * specified path
  67. */
  68. struct dfs_filesystem *dfs_filesystem_lookup(const char *path)
  69. {
  70. struct dfs_filesystem *fs;
  71. rt_uint32_t index, fspath, prefixlen;
  72. fs = RT_NULL;
  73. prefixlen = 0;
  74. /* lock filesystem */
  75. dfs_lock();
  76. /* lookup it in the filesystem table */
  77. for (index = 0; index < DFS_FILESYSTEMS_MAX; index++)
  78. {
  79. if (filesystem_table[index].path == RT_NULL)
  80. continue;
  81. else
  82. {
  83. fspath = strlen(filesystem_table[index].path);
  84. if (fspath < prefixlen)
  85. continue;
  86. }
  87. if ((filesystem_table[index].ops != RT_NULL) &&
  88. (strncmp(filesystem_table[index].path, path, fspath) == 0))
  89. {
  90. /* check next path separator */
  91. if (fspath > 1 && (strlen(path) > fspath) && (path[fspath] != '/'))
  92. continue;
  93. fs = &filesystem_table[index];
  94. prefixlen = fspath;
  95. }
  96. }
  97. dfs_unlock();
  98. return fs;
  99. }
  100. /**
  101. * this function will fetch the partition table on specified buffer.
  102. *
  103. * @param part the returned partition structure.
  104. * @param buf the buffer contains partition table.
  105. * @param pindex the index of partition table to fetch.
  106. *
  107. * @return RT_EOK on successful or -RT_ERROR on failed.
  108. */
  109. rt_err_t dfs_filesystem_get_partition(struct dfs_partition *part, rt_uint8_t *buf, rt_uint32_t pindex)
  110. {
  111. #define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
  112. #define DPT_ITEM_SIZE 16 /* partition item size */
  113. rt_uint8_t *dpt;
  114. rt_uint8_t type;
  115. rt_err_t result;
  116. RT_ASSERT(part != RT_NULL);
  117. RT_ASSERT(buf != RT_NULL);
  118. result = RT_EOK;
  119. dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
  120. if ((*dpt != 0x80) && (*dpt != 0x00))
  121. {
  122. /* which is not a partition table */
  123. result = -RT_ERROR;
  124. return result;
  125. }
  126. /* get partition type */
  127. type = *(dpt+4);
  128. if (type != 0)
  129. {
  130. /* set partition type */
  131. part->type = type;
  132. /* get partition offset and size */
  133. part->offset = *(dpt+8) | *(dpt+9)<<8 | *(dpt+10)<<16 | *(dpt+11)<<24;
  134. part->size = *(dpt+12) | *(dpt+13)<<8 | *(dpt+14)<<16 | *(dpt+15)<<24;
  135. rt_kprintf("found part[%d], begin: %d, size: ", pindex, part->offset*512);
  136. if ((part->size>>11) > 0) /* MB */
  137. {
  138. unsigned int part_size;
  139. part_size = part->size >> 11;/* MB */
  140. if ((part_size>>10) > 0) /* GB */
  141. {
  142. rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
  143. }
  144. else
  145. {
  146. rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */
  147. }
  148. }
  149. else
  150. {
  151. rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
  152. }
  153. }
  154. else
  155. {
  156. result = -RT_ERROR;
  157. }
  158. return result;
  159. }
  160. /**
  161. * this function will mount a file system on a specified path.
  162. *
  163. * @param device_name the name of device which includes a file system.
  164. * @param path the path to mount a file system
  165. * @param filesystemtype the file system type
  166. * @param rwflag the read/write etc. flag.
  167. * @param data the private data(parameter) for this file system.
  168. *
  169. * @return 0 on successful or -1 on failed.
  170. */
  171. int dfs_mount(const char *device_name, const char *path,
  172. const char *filesystemtype, unsigned long rwflag, const
  173. void *data)
  174. {
  175. const struct dfs_filesystem_operation *ops;
  176. struct dfs_filesystem *fs;
  177. char *fullpath=RT_NULL;
  178. rt_device_t dev_id;
  179. int index;
  180. /* open specific device */
  181. if (device_name != RT_NULL)
  182. {
  183. dev_id = rt_device_find(device_name);
  184. if (dev_id == RT_NULL)
  185. {
  186. /* no this device */
  187. rt_set_errno(-DFS_STATUS_ENODEV);
  188. return -1;
  189. }
  190. }
  191. else
  192. {
  193. /* which is a non-device filesystem mount */
  194. dev_id = RT_NULL;
  195. }
  196. /* find out specific filesystem */
  197. dfs_lock();
  198. for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
  199. {
  200. if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)
  201. break;
  202. }
  203. /* can't find filesystem */
  204. if (index == DFS_FILESYSTEM_TYPES_MAX)
  205. {
  206. rt_set_errno(-DFS_STATUS_ENODEV);
  207. dfs_unlock();
  208. return -1;
  209. }
  210. ops = filesystem_operation_table[index];
  211. dfs_unlock();
  212. /* make full path for special file */
  213. fullpath = dfs_normalize_path(RT_NULL, path);
  214. if (fullpath == RT_NULL) /* not an abstract path */
  215. {
  216. rt_set_errno(-DFS_STATUS_ENOTDIR);
  217. return -1;
  218. }
  219. /* Check if the path exists or not, raw APIs call, fixme */
  220. if ((strcmp(fullpath, "/") != 0) && (strcmp(fullpath, "/dev") != 0))
  221. {
  222. struct dfs_fd fd;
  223. if (dfs_file_open(&fd, fullpath, DFS_O_RDONLY | DFS_O_DIRECTORY) < 0)
  224. {
  225. rt_free(fullpath);
  226. rt_set_errno(-DFS_STATUS_ENOTDIR);
  227. return -1;
  228. }
  229. dfs_file_close(&fd);
  230. }
  231. /* check whether the file system mounted or not */
  232. dfs_lock();
  233. for (index =0; index < DFS_FILESYSTEMS_MAX; index++)
  234. {
  235. if (filesystem_table[index].ops != RT_NULL &&
  236. strcmp(filesystem_table[index].path, path) == 0)
  237. {
  238. rt_set_errno(-DFS_STATUS_EINVAL);
  239. goto err1;
  240. }
  241. }
  242. /* find out an empty filesystem table entry */
  243. for (index = 0; index < DFS_FILESYSTEMS_MAX && filesystem_table[index].ops != RT_NULL;
  244. index++) ;
  245. if (index == DFS_FILESYSTEMS_MAX) /* can't find en empty filesystem table entry */
  246. {
  247. rt_set_errno(-DFS_STATUS_ENOSPC);
  248. goto err1;
  249. }
  250. /* register file system */
  251. fs = &(filesystem_table[index]);
  252. fs->path = fullpath;
  253. fs->ops = ops;
  254. fs->dev_id = dev_id;
  255. /* release filesystem_table lock */
  256. dfs_unlock();
  257. /* open device, but do not check the status of device */
  258. if (dev_id != RT_NULL)
  259. rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
  260. if (ops->mount == RT_NULL) /* there is no mount implementation */
  261. {
  262. if (dev_id != RT_NULL)
  263. rt_device_close(dev_id);
  264. dfs_lock();
  265. /* clear filesystem table entry */
  266. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  267. dfs_unlock();
  268. rt_free(fullpath);
  269. rt_set_errno(-DFS_STATUS_ENOSYS);
  270. return -1;
  271. }
  272. /* call mount of this filesystem */
  273. else if (ops->mount(fs, rwflag, data) < 0)
  274. {
  275. /* close device */
  276. if (dev_id != RT_NULL)
  277. rt_device_close(fs->dev_id);
  278. /* mount failed */
  279. dfs_lock();
  280. /* clear filesystem table entry */
  281. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  282. dfs_unlock();
  283. rt_free(fullpath);
  284. return -1;
  285. }
  286. return 0;
  287. err1:
  288. dfs_unlock();
  289. if (fullpath != RT_NULL)
  290. rt_free(fullpath);
  291. return -1;
  292. }
  293. /**
  294. * this function will umount a file system on specified path.
  295. *
  296. * @param specialfile the specified path which mounted a file system.
  297. *
  298. * @return 0 on successful or -1 on failed.
  299. */
  300. int dfs_unmount(const char *specialfile)
  301. {
  302. char *fullpath;
  303. struct dfs_filesystem *fs = RT_NULL;
  304. fullpath = dfs_normalize_path(RT_NULL, specialfile);
  305. if (fullpath == RT_NULL)
  306. {
  307. rt_set_errno(-DFS_STATUS_ENOTDIR);
  308. return -1;
  309. }
  310. /* lock filesystem */
  311. dfs_lock();
  312. fs = dfs_filesystem_lookup(fullpath);
  313. if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
  314. {
  315. goto err1;
  316. }
  317. /* close device, but do not check the status of device */
  318. if (fs->dev_id != RT_NULL)
  319. rt_device_close(fs->dev_id);
  320. /* clear this filesystem table entry */
  321. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  322. dfs_unlock();
  323. rt_free(fullpath);
  324. return 0;
  325. err1:
  326. dfs_unlock();
  327. rt_free(fullpath);
  328. return -1;
  329. }
  330. /**
  331. * make a file system on the special device
  332. *
  333. * @param fs_name the file system name
  334. * @param device_name the special device name
  335. *
  336. * @return 0 on successful, otherwise failed.
  337. */
  338. int dfs_mkfs(const char *fs_name, const char *device_name)
  339. {
  340. int index;
  341. /* lock filesystem */
  342. dfs_lock();
  343. /* find the file system operations */
  344. for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
  345. {
  346. if (filesystem_operation_table[index] != RT_NULL &&
  347. strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
  348. {
  349. /* find file system operation */
  350. const struct dfs_filesystem_operation* ops = filesystem_operation_table[index];
  351. dfs_unlock();
  352. if (ops->mkfs != RT_NULL)
  353. return ops->mkfs(device_name);
  354. break;
  355. }
  356. }
  357. dfs_unlock();
  358. return -1;
  359. }
  360. /**
  361. * this function will return the information about a mounted file system.
  362. *
  363. * @param path the path which mounted file system.
  364. * @param buffer the buffer to save the returned information.
  365. *
  366. * @return 0 on successful, others on failed.
  367. */
  368. int dfs_statfs(const char *path, struct statfs *buffer)
  369. {
  370. struct dfs_filesystem *fs;
  371. fs = dfs_filesystem_lookup(path);
  372. if (fs != NULL)
  373. {
  374. if (fs->ops->statfs!= RT_NULL)
  375. return fs->ops->statfs(fs, buffer);
  376. }
  377. return -1;
  378. }
  379. #ifdef RT_USING_FINSH
  380. #include <finsh.h>
  381. void mkfs(const char *fs_name, const char *device_name)
  382. {
  383. dfs_mkfs(fs_name, device_name);
  384. }
  385. FINSH_FUNCTION_EXPORT(mkfs, make a file system);
  386. void df(const char *path)
  387. {
  388. struct statfs buffer;
  389. if (dfs_statfs(path, &buffer) == 0)
  390. {
  391. rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize);
  392. }
  393. }
  394. FINSH_FUNCTION_EXPORT(df, get disk free);
  395. #endif
  396. /* @} */