dfs_fs.c 11 KB

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