dfs_fs.c 11 KB

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