dfs_jffs2.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. /*
  2. * File : rtthread.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006-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. * 2012-1-7 prife the first version
  13. */
  14. #include <rtthread.h>
  15. #include <rtdevice.h>
  16. #include "cyg/infra/cyg_type.h"
  17. #include "cyg/fileio/fileio.h"
  18. #include "codes.h"
  19. #undef mode_t
  20. #include <dfs_fs.h>
  21. #include <dfs_def.h>
  22. #include "dfs_jffs2.h"
  23. #include "jffs2_config.h"
  24. #include "porting.h"
  25. #include <string.h>
  26. #if DEVICE_PART_MAX > 1
  27. #error "support only one jffs2 partition on a flash device!"
  28. #endif
  29. /* make sure the following struct var had been initilased to 0! */
  30. struct device_part
  31. {
  32. struct cyg_mtab_entry * mte;
  33. struct rt_mtd_nor_device *dev;
  34. };
  35. static struct device_part device_partition[DEVICE_PART_MAX] = {0};
  36. static struct rt_mutex jffs2_lock;
  37. #define jffs2_mount jffs2_fste.mount
  38. #define jffs2_umount jffs2_fste.umount
  39. #define jffs2_open jffs2_fste.open
  40. #define jffs2_file_unlink jffs2_fste.unlink
  41. #define jffs2_mkdir jffs2_fste.mkdir
  42. #define jffs2_rmdir jffs2_fste.rmdir
  43. #define jffs2_rename jffs2_fste.rename
  44. #define jffs2_link jffs2_fste.link
  45. #define jffs2_opendir jffs2_fste.opendir
  46. #define jffs2_chdir jffs2_fste.chdir
  47. #define jffs2_ops_stat jffs2_fste.stat
  48. #define jffs2_getinfo jffs2_fste.getinfo
  49. #define jffs2_setinfo jffs2_fste.setinfo
  50. #define jffs2_file_read jffs2_fileops.fo_read
  51. #define jffs2_file_write jffs2_fileops.fo_write
  52. #define jffs2_file_lseek jffs2_fileops.fo_lseek
  53. #define jffs2_file_ioctl jffs2_fileops.fo_ioctl
  54. #define jffs2_file_select jffs2_fileops.fo_select
  55. #define jffs2_file_fsync jffs2_fileops.fo_fsync
  56. #define jffs2_file_colse jffs2_fileops.fo_close
  57. #define jffs2_file_fstat jffs2_fileops.fo_fstat
  58. #define jffs2_file_getinfo jffs2_fileops.fo_getinfo
  59. #define jffs2_file_setinfo jffs2_fileops.fo_setinfo
  60. #define jffs2_dir_read jffs2_dirops.fo_read
  61. //#define jffs2_dir_write jffs2_dirops.fo_write
  62. #define jffs2_dir_lseek jffs2_dirops.fo_lseek
  63. //#define jffs2_dir_ioctl jffs2_dirops.fo_ioctl
  64. #define jffs2_dir_select jffs2_dirops.fo_select
  65. //#define jffs2_dir_fsync jffs2_dirops.fo_fsync
  66. #define jffs2_dir_colse jffs2_dirops.fo_close
  67. //#define jffs2_dir_fstat jffs2_dirops.fo_fstat
  68. //#define jffs2_dir_getinfo jffs2_dirops.fo_getinfo
  69. //#define jffs2_dir_setinfo jffs2_dirops.fo_setinfo
  70. /*
  71. * RT-Thread Device Interface for jffs2
  72. */
  73. /* these code is in src/flashio.c */
  74. static int jffs2_result_to_dfs(int result)
  75. {
  76. int status = -1;
  77. if (result < 0)
  78. result = -result;
  79. switch (result)
  80. {
  81. case ENOERR:/** no error */
  82. break;
  83. case EACCES:/** Tried to open read-only file for writing, or files sharing mode
  84. does not allow specified operations, or given path is directory */
  85. status = -DFS_STATUS_EINVAL;
  86. break;/* no suitable */
  87. case EEXIST: /** _O_CREAT and _O_EXCL flags specified, but filename already exists */
  88. status = -DFS_STATUS_EEXIST;
  89. break;
  90. case EINVAL: /** Invalid oflag or pmode argument */
  91. status = -DFS_STATUS_EINVAL;
  92. break;
  93. case EMFILE: /** No more file handles available(too many open files) */
  94. rt_kprintf("dfs_jffs2.c error: no more file handles available\r\n");
  95. status = -1;
  96. break;//fixme
  97. case ENOENT: /** file or path not found */
  98. status = -DFS_STATUS_ENOENT;
  99. break;
  100. case EBADF: /** invalid file handle */
  101. status = -DFS_STATUS_EBADF;
  102. break;
  103. case ENOMEM:/** no enough memory */
  104. status = -DFS_STATUS_ENOMEM;
  105. break;
  106. case EIO: /** I/O error from lower level flash operation */
  107. status = -DFS_STATUS_EIO;
  108. break;
  109. case ENOTDIR: /** Not a directory */
  110. status = -DFS_STATUS_ENOTDIR;
  111. break;
  112. case EISDIR: /** Is a directory */
  113. status = -DFS_STATUS_EISDIR;
  114. break;
  115. case ENOSPC: /**//* No space left on device */
  116. status = -DFS_STATUS_ENOSPC;
  117. break;
  118. default:
  119. rt_kprintf("dfs_jffs2.c error: %d\r\n", result);
  120. status = -1;
  121. break; /* unknown error! */
  122. }
  123. return status;
  124. }
  125. /*
  126. * RT-Thread DFS Interface for jffs2
  127. */
  128. static int dfs_jffs2_mount(struct dfs_filesystem* fs,
  129. unsigned long rwflag,
  130. const void* data)
  131. {
  132. unsigned index;
  133. struct cyg_mtab_entry * mte;
  134. int result;
  135. /* find a empty entry in partition table */
  136. for (index = 0; index < DEVICE_PART_MAX; index ++)
  137. {
  138. if (device_partition[index].dev == RT_NULL)
  139. break;
  140. }
  141. if (index == DEVICE_PART_MAX)
  142. return -DFS_STATUS_ENOSPC;
  143. mte = rt_malloc(sizeof(struct cyg_mtab_entry));
  144. if (mte == RT_NULL)
  145. return -DFS_STATUS_ENOMEM;
  146. mte->name = fs->path;
  147. mte->fsname = "jffs2";
  148. mte->devname = NULL;
  149. /* note that, i use mte->data to store rtt's device
  150. * while, in jffs2_mount, mte->data will be copy into
  151. * s_dev in struct super_block, and mte->data will be
  152. * filled with jffs2_sb(see the source of jffs2_mount.
  153. */
  154. mte->data = (CYG_ADDRWORD)fs->dev_id;
  155. device_partition[index].dev = RT_MTD_NOR_DEVICE(fs->dev_id);
  156. /* after jffs2_mount, mte->data will not be dev_id any more */
  157. result = jffs2_mount(NULL, mte);
  158. if (result != 0)
  159. {
  160. return jffs2_result_to_dfs(result);
  161. }
  162. /* save this pointer */
  163. device_partition[index].mte = mte;
  164. return 0;
  165. }
  166. static int _find_fs(struct cyg_mtab_entry ** mte, rt_device_t dev_id)
  167. {
  168. unsigned index;
  169. /* find device index */
  170. for (index = 0; index < DEVICE_PART_MAX; index++)
  171. {
  172. if (device_partition[index].dev == RT_MTD_NOR_DEVICE(dev_id))
  173. {
  174. *mte = device_partition[index].mte;
  175. return 0;
  176. }
  177. }
  178. rt_kprintf("error, could not found the fs!");
  179. return -1;
  180. }
  181. static int dfs_jffs2_unmount(struct dfs_filesystem* fs)
  182. {
  183. int result;
  184. unsigned index;
  185. /* find device index, then umount it */
  186. for (index = 0; index < DEVICE_PART_MAX; index++)
  187. {
  188. if (device_partition[index].dev == RT_MTD_NOR_DEVICE(fs->dev_id))
  189. {
  190. result = jffs2_umount(device_partition[index].mte);
  191. if (result)
  192. return jffs2_result_to_dfs(result);
  193. rt_free(device_partition[index].mte);
  194. device_partition[index].dev = NULL;
  195. device_partition[index].mte = NULL;
  196. return DFS_STATUS_OK;
  197. }
  198. }
  199. return -DFS_STATUS_ENOENT;
  200. }
  201. static int dfs_jffs2_mkfs(const char* device_name)
  202. {
  203. /* just erase all blocks on this nand partition */
  204. return -DFS_STATUS_ENOSYS;
  205. }
  206. static int dfs_jffs2_statfs(struct dfs_filesystem* fs,
  207. struct statfs *buf)
  208. {
  209. /* since the limit of unsigned long, so the max size of flash device is 4G */
  210. struct cyg_mtab_entry * mte;
  211. struct jffs2_fs_info info;
  212. int result;
  213. result = _find_fs(&mte, fs->dev_id);
  214. if (result)
  215. return -DFS_STATUS_ENOENT;
  216. RT_ASSERT(mte->data != NULL);
  217. jffs2_get_info_from_sb((void *)mte->data, &info);
  218. buf->f_bsize = info.sector_size;
  219. buf->f_blocks = info.nr_blocks;
  220. buf->f_bfree = info.free_size / info.sector_size;
  221. return 0;
  222. }
  223. static const char jffs2_root_path[] = ".";
  224. static int dfs_jffs2_open(struct dfs_fd* file)
  225. {
  226. int oflag, mode;
  227. int result;
  228. cyg_file * jffs2_file;
  229. struct cyg_mtab_entry * mte;
  230. const char * name;
  231. oflag = file->flags;
  232. jffs2_file = rt_malloc(sizeof(cyg_file));
  233. if (jffs2_file == RT_NULL)
  234. return -DFS_STATUS_ENOMEM;
  235. /* just escape '/' provided by dfs code */
  236. name = file->path;
  237. if ((name[0] == '/') && (name[1] == 0))
  238. name = jffs2_root_path;
  239. else /* name[0] still will be '/' */
  240. name ++;
  241. result = _find_fs(&mte, file->fs->dev_id);
  242. if (result)
  243. {
  244. rt_free(jffs2_file);
  245. return -DFS_STATUS_ENOENT;
  246. }
  247. if (oflag & DFS_O_DIRECTORY) /* operations about dir */
  248. {
  249. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  250. if (oflag & DFS_O_CREAT) /* create a dir*/
  251. {
  252. /* fixme, should test file->path can end with '/' */
  253. result = jffs2_mkdir(mte, mte->root, name);
  254. if (result)
  255. {
  256. rt_mutex_release(&jffs2_lock);
  257. rt_free(jffs2_file);
  258. return jffs2_result_to_dfs(result);
  259. }
  260. }
  261. /* open dir */
  262. result = jffs2_opendir(mte, mte->root, name, jffs2_file);
  263. rt_mutex_release(&jffs2_lock);
  264. if (result)
  265. {
  266. rt_free(jffs2_file);
  267. return jffs2_result_to_dfs(result);
  268. }
  269. #ifdef CONFIG_JFFS2_NO_RELATIVEDIR
  270. jffs2_file->f_offset = 2;
  271. #endif
  272. /* save this pointer, it will be used by dfs_jffs2_getdents*/
  273. file->data = jffs2_file;
  274. return 0;
  275. }
  276. /* regular file operations */
  277. mode = 0;
  278. if (oflag & DFS_O_RDONLY) mode |= JFFS2_O_RDONLY;
  279. if (oflag & DFS_O_WRONLY) mode |= JFFS2_O_WRONLY;
  280. if (oflag & DFS_O_RDWR) mode |= JFFS2_O_RDWR;
  281. /* Opens the file, if it is existing. If not, a new file is created. */
  282. if (oflag & DFS_O_CREAT) mode |= JFFS2_O_CREAT;
  283. /* Creates a new file. If the file is existing, it is truncated and overwritten. */
  284. if (oflag & DFS_O_TRUNC) mode |= JFFS2_O_TRUNC;
  285. /* Creates a new file. The function fails if the file is already existing. */
  286. if (oflag & DFS_O_EXCL) mode |= JFFS2_O_EXCL;
  287. // if (oflag & DFS_O_APPEND) mode |= JFFS2_O_APPEND;
  288. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  289. result = jffs2_open(mte, 0, name, mode, jffs2_file);
  290. if (result != 0)
  291. {
  292. rt_mutex_release(&jffs2_lock);
  293. rt_free(jffs2_file);
  294. return jffs2_result_to_dfs(result);
  295. }
  296. /* save this pointer, it will be used when calling read()£¬write(),
  297. flush(), lessk(), and will be rt_free when calling close()*/
  298. file->data = jffs2_file;
  299. file->pos = jffs2_file->f_offset;
  300. file->size = 0;
  301. jffs2_file_lseek(jffs2_file, (off_t *)(&(file->size)), SEEK_END);
  302. jffs2_file->f_offset = (off_t)file->pos;
  303. rt_mutex_release(&jffs2_lock);
  304. if (oflag & DFS_O_APPEND)
  305. {
  306. file->pos = file->size;
  307. jffs2_file->f_offset = file->size;
  308. }
  309. return 0;
  310. }
  311. static int dfs_jffs2_close(struct dfs_fd* file)
  312. {
  313. int result;
  314. cyg_file * jffs2_file;
  315. RT_ASSERT(file->data != NULL);
  316. jffs2_file = (cyg_file *)(file->data);
  317. if (file->flags & DFS_O_DIRECTORY) /* operations about dir */
  318. {
  319. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  320. result = jffs2_dir_colse(jffs2_file);
  321. rt_mutex_release(&jffs2_lock);
  322. if (result)
  323. return jffs2_result_to_dfs(result);
  324. rt_free(jffs2_file);
  325. return 0;
  326. }
  327. /* regular file operations */
  328. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  329. result = jffs2_file_colse(jffs2_file);
  330. rt_mutex_release(&jffs2_lock);
  331. if (result)
  332. return jffs2_result_to_dfs(result);
  333. /* release memory */
  334. rt_free(jffs2_file);
  335. return 0;
  336. }
  337. static int dfs_jffs2_ioctl(struct dfs_fd* file, int cmd, void* args)
  338. {
  339. return -DFS_STATUS_ENOSYS;
  340. }
  341. static int dfs_jffs2_read(struct dfs_fd* file, void* buf, rt_size_t len)
  342. {
  343. cyg_file * jffs2_file;
  344. struct CYG_UIO_TAG uio_s;
  345. struct CYG_IOVEC_TAG iovec;
  346. int char_read;
  347. int result;
  348. RT_ASSERT(file->data != NULL);
  349. jffs2_file = (cyg_file *)(file->data);
  350. uio_s.uio_iov = &iovec;
  351. uio_s.uio_iov->iov_base = buf;
  352. uio_s.uio_iov->iov_len = len;
  353. uio_s.uio_iovcnt = 1; //must be 1
  354. //uio_s.uio_offset //not used...
  355. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  356. char_read = jffs2_file->f_offset; /* the current offset */
  357. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  358. result = jffs2_file_read(jffs2_file, &uio_s);
  359. rt_mutex_release(&jffs2_lock);
  360. if (result)
  361. return jffs2_result_to_dfs(result);
  362. /* update position */
  363. file->pos = jffs2_file->f_offset;
  364. char_read = jffs2_file->f_offset - char_read;
  365. return char_read;
  366. }
  367. static int dfs_jffs2_write(struct dfs_fd* file,
  368. const void* buf,
  369. rt_size_t len)
  370. {
  371. cyg_file * jffs2_file;
  372. struct CYG_UIO_TAG uio_s;
  373. struct CYG_IOVEC_TAG iovec;
  374. int char_write;
  375. int result;
  376. RT_ASSERT(file->data != NULL);
  377. jffs2_file = (cyg_file *)(file->data);
  378. uio_s.uio_iov = &iovec;
  379. uio_s.uio_iov->iov_base = (void *)buf;
  380. uio_s.uio_iov->iov_len = len;
  381. uio_s.uio_iovcnt = 1; //must be 1
  382. //uio_s.uio_offset //not used...
  383. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  384. char_write = jffs2_file->f_offset;
  385. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  386. result = jffs2_file_write(jffs2_file, &uio_s);
  387. rt_mutex_release(&jffs2_lock);
  388. if (result)
  389. return jffs2_result_to_dfs(result);
  390. /* update position */
  391. file->pos = jffs2_file->f_offset;
  392. char_write = jffs2_file->f_offset - char_write;
  393. return char_write;
  394. }
  395. static int dfs_jffs2_flush(struct dfs_fd* file)
  396. {
  397. /* infact, jffs2 not support, jffs2_fo_sync just return ok */
  398. return -DFS_STATUS_ENOSYS;
  399. }
  400. /* fixme warning: the offset is rt_off_t, so maybe the size of a file is must <= 2G*/
  401. static int dfs_jffs2_lseek(struct dfs_fd* file,
  402. rt_off_t offset)
  403. {
  404. cyg_file * jffs2_file;
  405. int result;
  406. RT_ASSERT(file->data != NULL);
  407. jffs2_file = (cyg_file *)(file->data);
  408. /* set offset as current offset */
  409. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  410. result = jffs2_file_lseek(jffs2_file, &offset, SEEK_SET);
  411. rt_mutex_release(&jffs2_lock);
  412. if (result)
  413. return jffs2_result_to_dfs(result);
  414. /* update file position */
  415. file->pos = offset;
  416. return offset;
  417. }
  418. /* return the size of struct dirent*/
  419. static int dfs_jffs2_getdents(struct dfs_fd* file,
  420. struct dirent* dirp,
  421. rt_uint32_t count)
  422. {
  423. cyg_file * jffs2_file;
  424. struct CYG_UIO_TAG uio_s;
  425. struct CYG_IOVEC_TAG iovec;
  426. struct jffs2_dirent jffs2_d;
  427. struct dirent * d;
  428. rt_uint32_t index;
  429. #if !defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  430. struct jffs2_stat s;
  431. cyg_mtab_entry * mte;
  432. char * fullname;
  433. #endif
  434. int result;
  435. RT_ASSERT(file->data != RT_NULL);
  436. jffs2_file = (cyg_file*)(file->data);
  437. //set jffs2_d
  438. memset(&jffs2_d, 0, sizeof(struct jffs2_dirent));
  439. //set CYG_UIO_TAG uio_s
  440. uio_s.uio_iov = &iovec;
  441. uio_s.uio_iov->iov_base = &jffs2_d;
  442. uio_s.uio_iov->iov_len = sizeof(struct jffs2_dirent);;
  443. uio_s.uio_iovcnt = 1; //must be 1
  444. uio_s.uio_offset = 0;//not used...
  445. uio_s.uio_resid = uio_s.uio_iov->iov_len; //seem no use in jffs2;
  446. #if !defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  447. result = _find_fs(&mte, file->fs->dev_id);
  448. if (result)
  449. return -DFS_STATUS_ENOENT;
  450. #endif
  451. /* make integer count, usually count is 1 */
  452. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  453. if (count == 0)
  454. return -DFS_STATUS_EINVAL;
  455. index = 0;
  456. /* usually, the while loop should only be looped only once! */
  457. while (1)
  458. {
  459. d = dirp + index;
  460. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  461. result = jffs2_dir_read(jffs2_file, &uio_s);
  462. rt_mutex_release(&jffs2_lock);
  463. /* if met a error or all entry are read over, break while*/
  464. if (result || jffs2_d.d_name[0] == 0)
  465. break;
  466. #if defined (CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE)
  467. switch(jffs2_d.d_type & JFFS2_S_IFMT)
  468. {
  469. case JFFS2_S_IFREG: d->d_type = DFS_DT_REG; break;
  470. case JFFS2_S_IFDIR: d->d_type = DFS_DT_DIR; break;
  471. default: d->d_type = DFS_DT_UNKNOWN; break;
  472. }
  473. #else
  474. fullname = rt_malloc(FILE_PATH_MAX);
  475. if(fullname == RT_NULL)
  476. return -DFS_STATUS_ENOMEM;
  477. /* make a right entry */
  478. if ((file->path[0] == '/') )
  479. {
  480. if (file->path[1] == 0)
  481. strcpy(fullname, jffs2_d.d_name);
  482. else
  483. rt_sprintf(fullname, "%s/%s", file->path+1, jffs2_d.d_name);
  484. }
  485. else
  486. rt_sprintf(fullname, "%s/%s", file->path, jffs2_d.d_name);
  487. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  488. result = jffs2_porting_stat(mte, mte->root, fullname, (void *)&s);
  489. rt_mutex_release(&jffs2_lock);
  490. if (result)
  491. return jffs2_result_to_dfs(result);
  492. rt_free(fullname);
  493. /* convert to dfs stat structure */
  494. switch(s.st_mode & JFFS2_S_IFMT)
  495. {
  496. case JFFS2_S_IFREG: d->d_type = DFS_DT_REG; break;
  497. case JFFS2_S_IFDIR: d->d_type = DFS_DT_DIR; break;
  498. default: d->d_type = DFS_DT_UNKNOWN; break;
  499. }
  500. #endif
  501. /* write the rest fields of struct dirent* dirp */
  502. d->d_namlen = rt_strlen(jffs2_d.d_name);
  503. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  504. rt_strncpy(d->d_name, jffs2_d.d_name, d->d_namlen + 1);
  505. index ++;
  506. if (index * sizeof(struct dirent) >= count)
  507. break;
  508. }
  509. if (result)
  510. return jffs2_result_to_dfs(result);
  511. return index * sizeof(struct dirent);
  512. }
  513. static int dfs_jffs2_unlink(struct dfs_filesystem* fs, const char* path)
  514. {
  515. int result;
  516. struct jffs2_stat s;
  517. cyg_mtab_entry * mte;
  518. result = _find_fs(&mte, fs->dev_id);
  519. if (result)
  520. return -DFS_STATUS_ENOENT;
  521. /* deal path */
  522. if (path[0] == '/')
  523. path++;
  524. /* judge file type, dir is to be delete by rmdir, others by unlink */
  525. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  526. result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
  527. if (result)
  528. {
  529. rt_mutex_release(&jffs2_lock);
  530. return jffs2_result_to_dfs(result);
  531. }
  532. switch(s.st_mode & JFFS2_S_IFMT)
  533. {
  534. case JFFS2_S_IFREG:
  535. result = jffs2_file_unlink(mte, mte->root, path);
  536. break;
  537. case JFFS2_S_IFDIR:
  538. result = jffs2_rmdir(mte, mte->root, path);
  539. break;
  540. default:
  541. /* unknown file type */
  542. rt_mutex_release(&jffs2_lock);
  543. return -1;
  544. }
  545. rt_mutex_release(&jffs2_lock);
  546. if (result)
  547. return jffs2_result_to_dfs(result);
  548. return 0;
  549. }
  550. static int dfs_jffs2_rename(struct dfs_filesystem* fs,
  551. const char* oldpath,
  552. const char* newpath)
  553. {
  554. int result;
  555. cyg_mtab_entry * mte;
  556. result = _find_fs(&mte, fs->dev_id);
  557. if (result)
  558. return -DFS_STATUS_ENOENT;
  559. if (*oldpath == '/')
  560. oldpath += 1;
  561. if (*newpath == '/')
  562. newpath += 1;
  563. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  564. result = jffs2_rename(mte, mte->root, oldpath, mte->root, newpath);
  565. rt_mutex_release(&jffs2_lock);
  566. if (result)
  567. return jffs2_result_to_dfs(result);
  568. return 0;
  569. }
  570. static int dfs_jffs2_stat(struct dfs_filesystem* fs, const char *path, struct stat *st)
  571. {
  572. int result;
  573. struct jffs2_stat s;
  574. cyg_mtab_entry * mte;
  575. /* deal the path for jffs2 */
  576. RT_ASSERT(!((path[0] == '/') && (path[1] == 0)));
  577. if (path[0] == '/')
  578. path++;
  579. result = _find_fs(&mte, fs->dev_id);
  580. if (result)
  581. return -DFS_STATUS_ENOENT;
  582. rt_mutex_take(&jffs2_lock, RT_WAITING_FOREVER);
  583. result = jffs2_porting_stat(mte, mte->root, path, (void *)&s);
  584. rt_mutex_release(&jffs2_lock);
  585. if (result)
  586. return jffs2_result_to_dfs(result);
  587. /* convert to dfs stat structure */
  588. switch(s.st_mode & JFFS2_S_IFMT)
  589. {
  590. case JFFS2_S_IFREG:
  591. st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  592. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  593. break;
  594. case JFFS2_S_IFDIR:
  595. st->st_mode = DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  596. break;
  597. default:
  598. st->st_mode = DFS_DT_UNKNOWN; //fixme
  599. break;
  600. }
  601. st->st_dev = 0;
  602. st->st_size = s.st_size;
  603. st->st_mtime = s.st_mtime;
  604. /*st->st_blksize = ;*/ //fixme: what's this field?
  605. return 0;
  606. }
  607. static const struct dfs_filesystem_operation dfs_jffs2_ops =
  608. {
  609. "jffs2", /* file system type: jffs2 */
  610. #if RTTHREAD_VERSION >= 10100
  611. DFS_FS_FLAG_DEFAULT, /* use Relative Path */
  612. #endif
  613. dfs_jffs2_mount,
  614. dfs_jffs2_unmount,
  615. dfs_jffs2_mkfs,
  616. dfs_jffs2_statfs,
  617. dfs_jffs2_open,
  618. dfs_jffs2_close,
  619. dfs_jffs2_ioctl,
  620. dfs_jffs2_read,
  621. dfs_jffs2_write,
  622. dfs_jffs2_flush,
  623. dfs_jffs2_lseek,
  624. dfs_jffs2_getdents,
  625. dfs_jffs2_unlink,
  626. dfs_jffs2_stat,
  627. dfs_jffs2_rename,
  628. };
  629. int dfs_jffs2_init(void)
  630. {
  631. /* register fatfs file system */
  632. dfs_register(&dfs_jffs2_ops);
  633. /* initialize mutex */
  634. if (rt_mutex_init(&jffs2_lock, "jffs2lock", RT_IPC_FLAG_FIFO) != RT_EOK)
  635. {
  636. rt_kprintf("init jffs2 lock mutex failed\n");
  637. }
  638. rt_kprintf("init jffs2 lock mutex okay\n");
  639. return 0;
  640. }