dfs_fs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. * 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) continue;
  80. else
  81. {
  82. fspath = strlen(filesystem_table[index].path);
  83. if (fspath < prefixlen) continue;
  84. }
  85. if ((filesystem_table[index].ops != RT_NULL) &&
  86. (strncmp(filesystem_table[index].path, path, fspath) == 0))
  87. {
  88. /* check next path separator */
  89. if ( fspath > 1 && (strlen(path) > fspath) &&
  90. (path[fspath] != '/')) continue;
  91. fs = &filesystem_table[index];
  92. prefixlen = fspath;
  93. }
  94. }
  95. dfs_unlock();
  96. return fs;
  97. }
  98. /**
  99. * this function will fetch the partition table on specified buffer.
  100. *
  101. * @param part the returned partition structure.
  102. * @param buf the buffer contains partition table.
  103. * @param pindex the index of partition table to fetch.
  104. *
  105. * @return RT_EOK on successful or -RT_ERROR on failed.
  106. */
  107. rt_err_t dfs_filesystem_get_partition(struct dfs_partition* part, rt_uint8_t* buf, rt_uint32_t pindex)
  108. {
  109. #define DPT_ADDRESS 0x1be /* device partition offset in Boot Sector */
  110. #define DPT_ITEM_SIZE 16 /* partition item size */
  111. rt_uint8_t* dpt;
  112. rt_uint8_t type;
  113. rt_err_t result;
  114. RT_ASSERT(part != RT_NULL);
  115. RT_ASSERT(buf != RT_NULL);
  116. result = RT_EOK;
  117. dpt = buf + DPT_ADDRESS + pindex * DPT_ITEM_SIZE;
  118. if ((*dpt != 0x80) && (*dpt != 0x00))
  119. {
  120. /* which is not a partition table */
  121. result = -RT_ERROR;
  122. return result;
  123. }
  124. /* get partition type */
  125. type = *(dpt+4);
  126. if (type != 0)
  127. {
  128. /* set partition type */
  129. part->type = type;
  130. /* get partition offset and size */
  131. part->offset = *(dpt+ 8) | *(dpt+ 9) << 8 |
  132. *(dpt+10) << 16 | *(dpt+11) << 24;
  133. part->size = *(dpt+12) | *(dpt+13) << 8 |
  134. *(dpt+14) << 16 | *(dpt+15) << 24;
  135. rt_kprintf("found part[%d], begin: %d, size: ",
  136. pindex, part->offset * 512);
  137. if ( (part->size>>11) > 0 ) /* MB */
  138. {
  139. unsigned int part_size;
  140. part_size = part->size>>11;/* MB */
  141. if ( (part_size>>10) > 0) /* GB */
  142. {
  143. rt_kprintf("%d.%d%s",part_size>>10,part_size&0x3FF,"GB\r\n");/* GB */
  144. }
  145. else
  146. {
  147. rt_kprintf("%d.%d%s",part_size,(part->size>>1)&0x3FF,"MB\r\n");/* MB */
  148. }
  149. }
  150. else
  151. {
  152. rt_kprintf("%d%s",part->size>>1,"KB\r\n");/* KB */
  153. }
  154. }
  155. else
  156. {
  157. result = -RT_ERROR;
  158. }
  159. return result;
  160. }
  161. /**
  162. * this function will mount a file system on a specified path.
  163. *
  164. * @param device_name the name of device which includes a file system.
  165. * @param path the path to mount a file system
  166. * @param filesystemtype the file system type
  167. * @param rwflag the read/write etc. flag.
  168. * @param data the private data(parameter) for this file system.
  169. *
  170. * @return 0 on successful or -1 on failed.
  171. */
  172. int dfs_mount(const char* device_name, const char* path,
  173. const char* filesystemtype, unsigned long rwflag, const
  174. void* data)
  175. {
  176. const struct dfs_filesystem_operation* ops;
  177. struct dfs_filesystem* fs;
  178. char *fullpath=RT_NULL;
  179. rt_device_t dev_id;
  180. int index;
  181. /* open specific device */
  182. if (device_name != RT_NULL)
  183. {
  184. dev_id = rt_device_find(device_name);
  185. if (dev_id == RT_NULL)
  186. {
  187. /* no this device */
  188. rt_set_errno(-DFS_STATUS_ENODEV);
  189. return -1;
  190. }
  191. }
  192. else
  193. {
  194. /* which is a non-device filesystem mount */
  195. dev_id = RT_NULL;
  196. }
  197. /* find out specific filesystem */
  198. dfs_lock();
  199. for ( index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++ )
  200. {
  201. if (strcmp(filesystem_operation_table[index]->name, filesystemtype) == 0)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 en 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) rt_device_open(fs->dev_id, RT_DEVICE_OFLAG_RDWR);
  259. if (ops->mount == RT_NULL) /* there is no mount implementation */
  260. {
  261. if (dev_id != RT_NULL) rt_device_close(dev_id);
  262. dfs_lock();
  263. /* clear filesystem table entry */
  264. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  265. dfs_unlock();
  266. rt_free(fullpath);
  267. rt_set_errno(-DFS_STATUS_ENOSYS);
  268. return -1;
  269. }
  270. /* call mount of this filesystem */
  271. else if (ops->mount(fs, rwflag, data) < 0)
  272. {
  273. /* close device */
  274. if (dev_id != RT_NULL) rt_device_close(fs->dev_id);
  275. /* mount failed */
  276. dfs_lock();
  277. /* clear filesystem table entry */
  278. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  279. dfs_unlock();
  280. rt_free(fullpath);
  281. return -1;
  282. }
  283. return 0;
  284. err1:
  285. dfs_unlock();
  286. if (fullpath != RT_NULL) rt_free(fullpath);
  287. return -1;
  288. }
  289. /**
  290. * this function will umount a file system on specified path.
  291. *
  292. * @param specialfile the specified path which mounted a file system.
  293. *
  294. * @return 0 on successful or -1 on failed.
  295. */
  296. int dfs_unmount(const char *specialfile)
  297. {
  298. char *fullpath;
  299. struct dfs_filesystem* fs = RT_NULL;
  300. fullpath = dfs_normalize_path(RT_NULL, specialfile);
  301. if (fullpath == RT_NULL)
  302. {
  303. rt_set_errno(-DFS_STATUS_ENOTDIR);
  304. return -1;
  305. }
  306. /* lock filesystem */
  307. dfs_lock();
  308. fs = dfs_filesystem_lookup(fullpath);
  309. if (fs != RT_NULL && fs->ops->unmount != RT_NULL && fs->ops->unmount(fs) < 0)
  310. {
  311. goto err1;
  312. }
  313. /* close device, but do not check the status of device */
  314. if (fs->dev_id != RT_NULL)
  315. rt_device_close(fs->dev_id);
  316. /* clear this filesystem table entry */
  317. rt_memset(fs, 0, sizeof(struct dfs_filesystem));
  318. dfs_unlock();
  319. rt_free(fullpath);
  320. return 0;
  321. err1:
  322. dfs_unlock();
  323. rt_free(fullpath);
  324. return -1;
  325. }
  326. /**
  327. * make a file system on the special device
  328. *
  329. * @param fs_name the file system name
  330. * @param device_name the special device name
  331. *
  332. * @return 0 on successful, otherwise failed.
  333. */
  334. int dfs_mkfs(const char* fs_name, const char* device_name)
  335. {
  336. int index;
  337. /* lock filesystem */
  338. dfs_lock();
  339. /* find the file system operations */
  340. for (index = 0; index < DFS_FILESYSTEM_TYPES_MAX; index++)
  341. {
  342. if (filesystem_operation_table[index] != RT_NULL &&
  343. strcmp(filesystem_operation_table[index]->name, fs_name) == 0)
  344. {
  345. /* find file system operation */
  346. const struct dfs_filesystem_operation* ops = filesystem_operation_table[index];
  347. dfs_unlock();
  348. if (ops->mkfs != RT_NULL)
  349. return ops->mkfs(device_name);
  350. break;
  351. }
  352. }
  353. dfs_unlock();
  354. return -1;
  355. }
  356. /**
  357. * this function will return the information about a mounted file system.
  358. *
  359. * @param path the path which mounted file system.
  360. * @param buffer the buffer to save the returned information.
  361. *
  362. * @return 0 on successful, others on failed.
  363. */
  364. int dfs_statfs(const char* path, struct statfs* buffer)
  365. {
  366. struct dfs_filesystem* fs;
  367. fs = dfs_filesystem_lookup(path);
  368. if (fs != NULL)
  369. {
  370. if (fs->ops->statfs!= RT_NULL)
  371. return fs->ops->statfs(fs, buffer);
  372. }
  373. return -1;
  374. }
  375. #ifdef RT_USING_FINSH
  376. #include <finsh.h>
  377. void mkfs(const char* fs_name, const char* device_name)
  378. {
  379. dfs_mkfs(fs_name, device_name);
  380. }
  381. FINSH_FUNCTION_EXPORT(mkfs, make a file system);
  382. void df(const char* path)
  383. {
  384. struct statfs buffer;
  385. if (dfs_statfs(path, &buffer) == 0)
  386. {
  387. rt_kprintf("disk free: %d block[%d bytes per block]\n", buffer.f_bfree, buffer.f_bsize);
  388. }
  389. }
  390. FINSH_FUNCTION_EXPORT(df, get disk free);
  391. #endif
  392. /* @} */