dfs_fs.c 12 KB

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