dfs_lfs.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  1. #include <rtdevice.h>
  2. #include <rtthread.h>
  3. #include <dfs_file.h>
  4. #include <dfs_fs.h>
  5. #include "lfs.h"
  6. #include <stdio.h>
  7. #include <string.h>
  8. #ifndef RT_DEF_LFS_DRIVERS
  9. #define RT_DEF_LFS_DRIVERS 1
  10. #endif
  11. #if (RT_DEF_LFS_DRIVERS < 1)
  12. #error "#define RT_DEF_LFS_DRIVERS must > 0"
  13. #endif
  14. #ifndef LFS_READ_SIZE
  15. #define LFS_READ_SIZE 256
  16. #endif
  17. #ifndef LFS_PROG_SIZE
  18. #define LFS_PROG_SIZE 256
  19. #endif
  20. #ifndef LFS_BLOCK_SIZE
  21. #define LFS_BLOCK_SIZE 4096
  22. #endif
  23. #ifndef LFS_CACHE_SIZE
  24. #define LFS_CACHE_SIZE LFS_PROG_SIZE
  25. #endif
  26. #ifndef LFS_BLOCK_CYCLES
  27. #define LFS_BLOCK_CYCLES (-1)
  28. #endif
  29. #ifndef LFS_LOOKAHEAD_MAX
  30. #define LFS_LOOKAHEAD_MAX 128
  31. #endif
  32. typedef struct _dfs_lfs_s
  33. {
  34. struct lfs lfs;
  35. struct lfs_config cfg;
  36. } dfs_lfs_t;
  37. typedef struct _dfs_lfs_fd_s
  38. {
  39. struct lfs* lfs;
  40. union
  41. {
  42. struct lfs_file file;
  43. struct lfs_dir dir;
  44. } u;
  45. } dfs_lfs_fd_t;
  46. static struct _dfs_lfs_s* _lfs_mount_tbl[RT_DEF_LFS_DRIVERS] = {0};
  47. static struct rt_mutex _lfs_lock;
  48. #define lfs_dfs_lock() rt_mutex_take(&_lfs_lock, RT_WAITING_FOREVER);
  49. #define lfs_dfs_unlock() rt_mutex_release(&_lfs_lock);
  50. // Read a region in a block. Negative error codes are propogated
  51. // to the user.
  52. static int _lfs_flash_read(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, void* buffer, lfs_size_t size)
  53. {
  54. struct rt_mtd_nor_device* mtd_nor;
  55. RT_ASSERT(c != RT_NULL);
  56. RT_ASSERT(c->context != RT_NULL);
  57. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  58. if (rt_mtd_nor_read(mtd_nor, block * c->block_size + off, buffer, size) != size)
  59. {
  60. return LFS_ERR_IO;
  61. }
  62. return LFS_ERR_OK;
  63. }
  64. // Program a region in a block. The block must have previously
  65. // been erased. Negative error codes are propogated to the user.
  66. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  67. static int _lfs_flash_prog(const struct lfs_config* c, lfs_block_t block, lfs_off_t off, const void* buffer, lfs_size_t size)
  68. {
  69. struct rt_mtd_nor_device* mtd_nor;
  70. RT_ASSERT(c != RT_NULL);
  71. RT_ASSERT(c->context != RT_NULL);
  72. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  73. if (rt_mtd_nor_write(mtd_nor, block * c->block_size + off, buffer, size) != size)
  74. {
  75. return LFS_ERR_IO;
  76. }
  77. return LFS_ERR_OK;
  78. }
  79. // Erase a block. A block must be erased before being programmed.
  80. // The state of an erased block is undefined. Negative error codes
  81. // are propogated to the user.
  82. // May return LFS_ERR_CORRUPT if the block should be considered bad.
  83. static int _lfs_flash_erase(const struct lfs_config* c, lfs_block_t block)
  84. {
  85. struct rt_mtd_nor_device* mtd_nor;
  86. RT_ASSERT(c != RT_NULL);
  87. RT_ASSERT(c->context != RT_NULL);
  88. mtd_nor = (struct rt_mtd_nor_device*)c->context;
  89. if (rt_mtd_nor_erase_block(mtd_nor, block * c->block_size, c->block_size) != RT_EOK)
  90. {
  91. return LFS_ERR_IO;
  92. }
  93. return LFS_ERR_OK;
  94. }
  95. // Sync the state of the underlying block device. Negative error codes
  96. // are propogated to the user.
  97. static int _lfs_flash_sync(const struct lfs_config* c)
  98. {
  99. return LFS_ERR_OK;
  100. }
  101. /* results:
  102. * -1, no space to install fatfs driver
  103. * >= 0, there is an space to install littlefs driver
  104. */
  105. static int _get_disk(rt_device_t dev_id)
  106. {
  107. int index;
  108. if (dev_id == RT_NULL)
  109. {
  110. for (index = 0; index < RT_DEF_LFS_DRIVERS; index ++)
  111. {
  112. if(_lfs_mount_tbl[index] == RT_NULL)
  113. {
  114. return index;
  115. }
  116. }
  117. }
  118. else
  119. {
  120. for (index = 0; index < RT_DEF_LFS_DRIVERS; index ++)
  121. {
  122. if ((_lfs_mount_tbl[index] != RT_NULL) && (_lfs_mount_tbl[index]->cfg.context == (void *)dev_id))
  123. {
  124. return index;
  125. }
  126. }
  127. }
  128. return -1;
  129. }
  130. static int _lfs_result_to_dfs(int result)
  131. {
  132. int status = 0;
  133. switch (result)
  134. {
  135. case LFS_ERR_OK:
  136. break;
  137. case LFS_ERR_IO:
  138. status = -EIO;
  139. break; // Error during device operation
  140. case LFS_ERR_NOENT:
  141. status = -ENOENT;
  142. break; // No directory entry
  143. case LFS_ERR_EXIST:
  144. status = -EEXIST;
  145. break; // Entry already exists
  146. case LFS_ERR_NOTDIR:
  147. status = -ENOTDIR;
  148. break; // Entry is not a dir
  149. case LFS_ERR_ISDIR:
  150. status = -EISDIR;
  151. break; // Entry is a dir
  152. case LFS_ERR_NOTEMPTY:
  153. status = -ENOTEMPTY;
  154. break; // Dir is not empty
  155. case LFS_ERR_BADF:
  156. status = -EBADF;
  157. break; // Bad file number
  158. case LFS_ERR_INVAL:
  159. status = -EINVAL;
  160. break; // Invalid parameter
  161. case LFS_ERR_NOSPC:
  162. status = -ENOSPC;
  163. break; // No space left on device
  164. case LFS_ERR_NOMEM:
  165. status = -ENOMEM;
  166. break; // No more memory available
  167. case LFS_ERR_CORRUPT:
  168. status = -52;
  169. break; // Corrupted
  170. default:
  171. status = -EIO;
  172. break;
  173. }
  174. return status;
  175. }
  176. static void _lfs_load_config(struct lfs_config* lfs_cfg, struct rt_mtd_nor_device* mtd_nor)
  177. {
  178. lfs_cfg->context = (void*)mtd_nor;
  179. lfs_cfg->read_size = LFS_READ_SIZE;
  180. lfs_cfg->prog_size = LFS_PROG_SIZE;
  181. lfs_cfg->block_size = mtd_nor->block_size;
  182. if (lfs_cfg->block_size < LFS_BLOCK_SIZE)
  183. {
  184. lfs_cfg->block_size = LFS_BLOCK_SIZE;
  185. }
  186. lfs_cfg->cache_size = LFS_CACHE_SIZE;
  187. lfs_cfg->block_cycles = LFS_BLOCK_CYCLES;
  188. lfs_cfg->block_count = mtd_nor->block_end - mtd_nor->block_start;
  189. lfs_cfg->lookahead_size = 32 * ((lfs_cfg->block_count + 31) / 32);
  190. if (lfs_cfg->lookahead_size > LFS_LOOKAHEAD_MAX)
  191. {
  192. lfs_cfg->lookahead_size = LFS_LOOKAHEAD_MAX;
  193. }
  194. lfs_cfg->read = &_lfs_flash_read;
  195. lfs_cfg->prog = &_lfs_flash_prog;
  196. lfs_cfg->erase = &_lfs_flash_erase;
  197. lfs_cfg->sync = &_lfs_flash_sync;
  198. }
  199. static int _dfs_lfs_mount(struct dfs_filesystem* dfs, unsigned long rwflag, const void* data)
  200. {
  201. int result;
  202. int index;
  203. dfs_lfs_t* dfs_lfs;
  204. /* Check Device Type */
  205. if (dfs->dev_id->type != RT_Device_Class_MTD)
  206. {
  207. rt_kprintf("The flash device type must be MTD!\n");
  208. return -EINVAL;
  209. }
  210. lfs_dfs_lock();
  211. /* get an empty position */
  212. index = _get_disk(RT_NULL);
  213. if (index == -1)
  214. {
  215. lfs_dfs_unlock();
  216. return -EIO;
  217. }
  218. /*create lfs handle */
  219. dfs_lfs = (dfs_lfs_t*)rt_malloc(sizeof(dfs_lfs_t));
  220. if (dfs_lfs == RT_NULL)
  221. {
  222. lfs_dfs_unlock();
  223. rt_kprintf("ERROR:no memory!\n");
  224. return -ENOMEM;
  225. }
  226. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  227. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dfs->dev_id);
  228. /* mount lfs*/
  229. result = lfs_mount(&dfs_lfs->lfs, &dfs_lfs->cfg);
  230. if (result != LFS_ERR_OK)
  231. {
  232. lfs_dfs_unlock();
  233. /* release memory */
  234. rt_free(dfs_lfs);
  235. return -EIO;
  236. }
  237. /* mount succeed! */
  238. dfs->data = (void*)dfs_lfs;
  239. _lfs_mount_tbl[index] = dfs_lfs;
  240. lfs_dfs_unlock();
  241. return RT_EOK;
  242. }
  243. static int _dfs_lfs_unmount(struct dfs_filesystem* dfs)
  244. {
  245. int result;
  246. int index;
  247. dfs_lfs_t* dfs_lfs;
  248. RT_ASSERT(dfs != RT_NULL);
  249. RT_ASSERT(dfs->data != RT_NULL);
  250. lfs_dfs_lock();
  251. /* find the device index and then umount it */
  252. index = _get_disk(dfs->dev_id);
  253. if (index == -1)
  254. {
  255. lfs_dfs_unlock();
  256. return -ENOENT;
  257. }
  258. _lfs_mount_tbl[index] = RT_NULL;
  259. dfs_lfs = (dfs_lfs_t*)dfs->data;
  260. dfs->data = RT_NULL;
  261. result = lfs_unmount(&dfs_lfs->lfs);
  262. lfs_dfs_unlock();
  263. rt_free(dfs_lfs);
  264. return _lfs_result_to_dfs(result);
  265. }
  266. static int _dfs_lfs_mkfs(rt_device_t dev_id)
  267. {
  268. int result;
  269. int index;
  270. dfs_lfs_t* dfs_lfs;
  271. if (dev_id == RT_NULL)
  272. {
  273. return -EINVAL;
  274. }
  275. lfs_dfs_lock();
  276. /* Check Device Type */
  277. if (dev_id->type != RT_Device_Class_MTD)
  278. {
  279. lfs_dfs_unlock();
  280. rt_kprintf("The flash device type must be MTD!\n");
  281. return -EINVAL;
  282. }
  283. index = _get_disk(dev_id);
  284. if (index == -1)
  285. {
  286. /* not found the device id */
  287. index = _get_disk(RT_NULL);
  288. if (index == -1)
  289. {
  290. lfs_dfs_unlock();
  291. /* no space to store an temp driver */
  292. rt_kprintf("sorry, there is no space to do mkfs!\n");
  293. return -ENOSPC;
  294. }
  295. /* create lfs handle */
  296. dfs_lfs = rt_malloc(sizeof(dfs_lfs_t));
  297. if (dfs_lfs == RT_NULL)
  298. {
  299. _lfs_mount_tbl[index] = RT_NULL;
  300. lfs_dfs_unlock();
  301. rt_kprintf("ERROR:no memory!\n");
  302. return -ENOMEM;
  303. }
  304. rt_memset(dfs_lfs, 0, sizeof(dfs_lfs_t));
  305. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dev_id);
  306. /* format flash device */
  307. result = lfs_format(&dfs_lfs->lfs, &dfs_lfs->cfg);
  308. lfs_dfs_unlock();
  309. return _lfs_result_to_dfs(result);
  310. }
  311. dfs_lfs = _lfs_mount_tbl[index];
  312. /* unmount it */
  313. result = lfs_unmount(&dfs_lfs->lfs);
  314. if (result != LFS_ERR_OK)
  315. {
  316. lfs_dfs_unlock();
  317. return _lfs_result_to_dfs(result);
  318. }
  319. _lfs_mount_tbl[index] = RT_NULL;
  320. /* format flash device */
  321. result = lfs_format(&dfs_lfs->lfs, &dfs_lfs->cfg);
  322. if (result != LFS_ERR_OK)
  323. {
  324. lfs_dfs_unlock();
  325. return _lfs_result_to_dfs(result);
  326. }
  327. _lfs_load_config(&dfs_lfs->cfg, (struct rt_mtd_nor_device*)dev_id);
  328. /* mount lfs*/
  329. result = lfs_mount(&dfs_lfs->lfs, &dfs_lfs->cfg);
  330. if (result == LFS_ERR_OK)
  331. {
  332. _lfs_mount_tbl[index] = dfs_lfs;
  333. }
  334. lfs_dfs_unlock();
  335. return _lfs_result_to_dfs(result);
  336. }
  337. static int _dfs_lfs_statfs_count(void* p, lfs_block_t b)
  338. {
  339. *(lfs_size_t*)p += 1;
  340. return 0;
  341. }
  342. static int _dfs_lfs_statfs(struct dfs_filesystem* dfs, struct statfs* buf)
  343. {
  344. dfs_lfs_t* dfs_lfs;
  345. int result;
  346. lfs_size_t in_use = 0;
  347. RT_ASSERT(buf != RT_NULL);
  348. RT_ASSERT(dfs != RT_NULL);
  349. RT_ASSERT(dfs->data != RT_NULL);
  350. lfs_dfs_lock();
  351. dfs_lfs = (dfs_lfs_t*)dfs->data;
  352. /* Get total sectors and free sectors */
  353. result = lfs_fs_traverse(&dfs_lfs->lfs, _dfs_lfs_statfs_count, &in_use);
  354. if (result != LFS_ERR_OK)
  355. {
  356. lfs_dfs_unlock();
  357. return _lfs_result_to_dfs(result);
  358. }
  359. buf->f_bsize = dfs_lfs->cfg.block_size;
  360. buf->f_blocks = dfs_lfs->cfg.block_count;
  361. buf->f_bfree = dfs_lfs->cfg.block_count - in_use;
  362. lfs_dfs_unlock();
  363. return RT_EOK;
  364. }
  365. static int _dfs_lfs_unlink(struct dfs_filesystem* dfs, const char* path)
  366. {
  367. dfs_lfs_t* dfs_lfs;
  368. int result;
  369. RT_ASSERT(dfs != RT_NULL);
  370. RT_ASSERT(dfs->data != RT_NULL);
  371. lfs_dfs_lock();
  372. dfs_lfs = (dfs_lfs_t*)dfs->data;
  373. result = lfs_remove(&dfs_lfs->lfs, path);
  374. lfs_dfs_unlock();
  375. return _lfs_result_to_dfs(result);
  376. }
  377. static void _dfs_lfs_tostat(struct stat* st, struct lfs_info* info)
  378. {
  379. memset(st, 0, sizeof(struct stat));
  380. /* convert to dfs stat structure */
  381. st->st_dev = 0;
  382. st->st_size = info->size;
  383. st->st_mode = S_IRWXU | S_IRWXG | S_IRWXO;
  384. switch (info->type)
  385. {
  386. case LFS_TYPE_DIR:
  387. st->st_mode |= S_IFDIR;
  388. break;
  389. case LFS_TYPE_REG:
  390. st->st_mode |= S_IFREG;
  391. break;
  392. }
  393. }
  394. static int _dfs_lfs_stat(struct dfs_filesystem* dfs, const char* path, struct stat* st)
  395. {
  396. dfs_lfs_t* dfs_lfs;
  397. int result;
  398. struct lfs_info info;
  399. RT_ASSERT(dfs != RT_NULL);
  400. RT_ASSERT(dfs->data != RT_NULL);
  401. lfs_dfs_lock();
  402. dfs_lfs = (dfs_lfs_t*)dfs->data;
  403. result = lfs_stat(&dfs_lfs->lfs, path, &info);
  404. lfs_dfs_unlock();
  405. if (result != LFS_ERR_OK)
  406. {
  407. return _lfs_result_to_dfs(result);
  408. }
  409. _dfs_lfs_tostat(st, &info);
  410. return 0;
  411. }
  412. static int _dfs_lfs_rename(struct dfs_filesystem* dfs, const char* from, const char* to)
  413. {
  414. dfs_lfs_t* dfs_lfs;
  415. int result;
  416. RT_ASSERT(dfs != RT_NULL);
  417. RT_ASSERT(dfs->data != RT_NULL);
  418. lfs_dfs_lock();
  419. dfs_lfs = (dfs_lfs_t*)dfs->data;
  420. result = lfs_rename(&dfs_lfs->lfs, from, to);
  421. lfs_dfs_unlock();
  422. return _lfs_result_to_dfs(result);
  423. }
  424. /******************************************************************************
  425. * file operations
  426. ******************************************************************************/
  427. static int _dfs_lfs_open(struct dfs_fd* file)
  428. {
  429. struct dfs_filesystem* dfs;
  430. dfs_lfs_t* dfs_lfs;
  431. int result;
  432. int flags = 0;
  433. RT_ASSERT(file != RT_NULL);
  434. RT_ASSERT(file->data != RT_NULL);
  435. lfs_dfs_lock();
  436. dfs = (struct dfs_filesystem*)file->data;
  437. dfs_lfs = (dfs_lfs_t*)dfs->data;
  438. if (file->flags & O_DIRECTORY)
  439. {
  440. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  441. if (dfs_lfs_fd == RT_NULL)
  442. {
  443. rt_kprintf("ERROR:no memory!\n");
  444. result = -ENOMEM;
  445. goto _error_dir;
  446. }
  447. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  448. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  449. if (file->flags & O_CREAT)
  450. {
  451. result = lfs_mkdir(dfs_lfs_fd->lfs, file->path);
  452. if (result != LFS_ERR_OK)
  453. {
  454. goto _error_dir;
  455. }
  456. }
  457. result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->path);
  458. if (result != LFS_ERR_OK)
  459. {
  460. goto _error_dir;
  461. }
  462. else
  463. {
  464. file->data = (void*)dfs_lfs_fd;
  465. lfs_dfs_unlock();
  466. return RT_EOK;
  467. }
  468. _error_dir:
  469. if (dfs_lfs_fd != RT_NULL)
  470. {
  471. rt_free(dfs_lfs_fd);
  472. }
  473. lfs_dfs_unlock();
  474. return _lfs_result_to_dfs(result);
  475. }
  476. else
  477. {
  478. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  479. if (dfs_lfs_fd == RT_NULL)
  480. {
  481. rt_kprintf("ERROR:no memory!\n");
  482. result = -ENOMEM;
  483. goto _error_file;
  484. }
  485. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  486. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  487. if ((file->flags & 3) == O_RDONLY)
  488. flags |= LFS_O_RDONLY;
  489. if ((file->flags & 3) == O_WRONLY)
  490. flags |= LFS_O_WRONLY;
  491. if ((file->flags & 3) == O_RDWR)
  492. flags |= LFS_O_RDWR;
  493. if (file->flags & O_CREAT)
  494. flags |= LFS_O_CREAT;
  495. if (file->flags & O_EXCL)
  496. flags |= LFS_O_EXCL;
  497. if (file->flags & O_TRUNC)
  498. flags |= LFS_O_TRUNC;
  499. if (file->flags & O_APPEND)
  500. flags |= LFS_O_APPEND;
  501. result = lfs_file_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->path, flags);
  502. if (result != LFS_ERR_OK)
  503. {
  504. goto _error_file;
  505. }
  506. else
  507. {
  508. file->data = (void*)dfs_lfs_fd;
  509. file->pos = dfs_lfs_fd->u.file.pos;
  510. file->size = dfs_lfs_fd->u.file.ctz.size;
  511. lfs_dfs_unlock();
  512. return RT_EOK;
  513. }
  514. _error_file:
  515. if (dfs_lfs_fd != RT_NULL)
  516. {
  517. rt_free(dfs_lfs_fd);
  518. }
  519. lfs_dfs_unlock();
  520. return _lfs_result_to_dfs(result);
  521. }
  522. }
  523. static int _dfs_lfs_close(struct dfs_fd* file)
  524. {
  525. int result;
  526. dfs_lfs_fd_t* dfs_lfs_fd;
  527. RT_ASSERT(file != RT_NULL);
  528. RT_ASSERT(file->data != RT_NULL);
  529. lfs_dfs_lock();
  530. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  531. if (file->type == FT_DIRECTORY)
  532. {
  533. result = lfs_dir_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir);
  534. }
  535. else
  536. {
  537. result = lfs_file_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  538. }
  539. rt_free(dfs_lfs_fd);
  540. lfs_dfs_unlock();
  541. return _lfs_result_to_dfs(result);
  542. }
  543. static int _dfs_lfs_ioctl(struct dfs_fd* file, int cmd, void* args)
  544. {
  545. return -ENOSYS;
  546. }
  547. static int _dfs_lfs_read(struct dfs_fd* file, void* buf, size_t len)
  548. {
  549. lfs_ssize_t ssize;
  550. dfs_lfs_fd_t* dfs_lfs_fd;
  551. RT_ASSERT(file != RT_NULL);
  552. RT_ASSERT(file->data != RT_NULL);
  553. lfs_dfs_lock();
  554. if (file->type == FT_DIRECTORY)
  555. {
  556. lfs_dfs_unlock();
  557. return -EISDIR;
  558. }
  559. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  560. #if 0
  561. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  562. {
  563. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  564. if (soff < 0)
  565. {
  566. lfs_dfs_unlock();
  567. return _lfs_result_to_dfs(soff);
  568. }
  569. }
  570. #endif
  571. ssize = lfs_file_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  572. if (ssize < 0)
  573. {
  574. lfs_dfs_unlock();
  575. return _lfs_result_to_dfs(ssize);
  576. }
  577. /* update position */
  578. file->pos = dfs_lfs_fd->u.file.pos;
  579. lfs_dfs_unlock();
  580. return ssize;
  581. }
  582. static int _dfs_lfs_write(struct dfs_fd* file, const void* buf, size_t len)
  583. {
  584. lfs_ssize_t ssize;
  585. dfs_lfs_fd_t* dfs_lfs_fd;
  586. RT_ASSERT(file != RT_NULL);
  587. RT_ASSERT(file->data != RT_NULL);
  588. lfs_dfs_lock();
  589. if (file->type == FT_DIRECTORY)
  590. {
  591. lfs_dfs_unlock();
  592. return -EISDIR;
  593. }
  594. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  595. #if 0
  596. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  597. {
  598. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  599. if (soff < 0)
  600. {
  601. lfs_dfs_unlock();
  602. return _lfs_result_to_dfs(soff);
  603. }
  604. }
  605. #endif
  606. ssize = lfs_file_write(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  607. if (ssize < 0)
  608. {
  609. lfs_dfs_unlock();
  610. return _lfs_result_to_dfs(ssize);
  611. }
  612. /* update position and file size */
  613. file->pos = dfs_lfs_fd->u.file.pos;
  614. file->size = dfs_lfs_fd->u.file.ctz.size;
  615. lfs_dfs_unlock();
  616. return ssize;
  617. }
  618. static int _dfs_lfs_flush(struct dfs_fd* file)
  619. {
  620. int result;
  621. dfs_lfs_fd_t* dfs_lfs_fd;
  622. RT_ASSERT(file != RT_NULL);
  623. RT_ASSERT(file->data != RT_NULL);
  624. lfs_dfs_lock();
  625. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  626. result = lfs_file_sync(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  627. lfs_dfs_unlock();
  628. return _lfs_result_to_dfs(result);
  629. }
  630. static int _dfs_lfs_lseek(struct dfs_fd* file, rt_off_t offset)
  631. {
  632. dfs_lfs_fd_t* dfs_lfs_fd;
  633. RT_ASSERT(file != RT_NULL);
  634. RT_ASSERT(file->data != RT_NULL);
  635. lfs_dfs_lock();
  636. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  637. if (file->type == FT_REGULAR)
  638. {
  639. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, offset, LFS_SEEK_SET);
  640. if (soff < 0)
  641. {
  642. lfs_dfs_unlock();
  643. return _lfs_result_to_dfs(soff);
  644. }
  645. file->pos = dfs_lfs_fd->u.file.pos;
  646. }
  647. else if (file->type == FT_DIRECTORY)
  648. {
  649. lfs_soff_t soff = lfs_dir_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, offset);
  650. if (soff < 0)
  651. {
  652. lfs_dfs_unlock();
  653. return _lfs_result_to_dfs(soff);
  654. }
  655. file->pos = dfs_lfs_fd->u.dir.pos;
  656. }
  657. lfs_dfs_unlock();
  658. return (file->pos);
  659. }
  660. static int _dfs_lfs_getdents(struct dfs_fd* file, struct dirent* dirp, uint32_t count)
  661. {
  662. dfs_lfs_fd_t* dfs_lfs_fd;
  663. int result;
  664. int index;
  665. struct dirent* d;
  666. struct lfs_info info;
  667. RT_ASSERT(file->data != RT_NULL);
  668. lfs_dfs_lock();
  669. dfs_lfs_fd = (dfs_lfs_fd_t*)(file->data);
  670. /* make integer count */
  671. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  672. if (count == 0)
  673. {
  674. lfs_dfs_unlock();
  675. return -EINVAL;
  676. }
  677. index = 0;
  678. while (1)
  679. {
  680. d = dirp + index;
  681. result = lfs_dir_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, &info);
  682. if ((result != 1) || (info.name[0] == 0))
  683. {
  684. break;
  685. }
  686. if (rt_strcmp(info.name, ".") == 0)
  687. {
  688. continue;
  689. }
  690. else if (rt_strcmp(info.name, "..") == 0)
  691. {
  692. continue;
  693. }
  694. d->d_type = DT_UNKNOWN;
  695. switch (info.type)
  696. {
  697. case LFS_TYPE_DIR:
  698. d->d_type |= DT_DIR;
  699. break;
  700. case LFS_TYPE_REG:
  701. d->d_type |= DT_REG;
  702. break;
  703. }
  704. d->d_namlen = (rt_uint8_t)rt_strlen(info.name);
  705. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  706. rt_strncpy(d->d_name, info.name, rt_strlen(info.name) + 1);
  707. index++;
  708. if (index * sizeof(struct dirent) >= count)
  709. {
  710. break;
  711. }
  712. }
  713. if (index == 0)
  714. {
  715. lfs_dfs_unlock();
  716. return _lfs_result_to_dfs(result);
  717. }
  718. file->pos += index * sizeof(struct dirent);
  719. lfs_dfs_unlock();
  720. return index * sizeof(struct dirent);
  721. }
  722. static const struct dfs_file_ops _dfs_lfs_fops = {
  723. _dfs_lfs_open,
  724. _dfs_lfs_close,
  725. _dfs_lfs_ioctl,
  726. _dfs_lfs_read,
  727. _dfs_lfs_write,
  728. _dfs_lfs_flush,
  729. _dfs_lfs_lseek,
  730. _dfs_lfs_getdents,
  731. // RT_NULL, /* poll interface */
  732. };
  733. static const struct dfs_filesystem_ops _dfs_lfs_ops = {
  734. "lfs",
  735. DFS_FS_FLAG_DEFAULT,
  736. &_dfs_lfs_fops,
  737. _dfs_lfs_mount,
  738. _dfs_lfs_unmount,
  739. _dfs_lfs_mkfs,
  740. _dfs_lfs_statfs,
  741. _dfs_lfs_unlink,
  742. _dfs_lfs_stat,
  743. _dfs_lfs_rename,
  744. };
  745. int dfs_lfs_init(void)
  746. {
  747. /* init file system lock */
  748. rt_mutex_init(&_lfs_lock, "lfsmtx", RT_IPC_FLAG_FIFO);
  749. /* register ram file system */
  750. return dfs_register(&_dfs_lfs_ops);
  751. }
  752. INIT_COMPONENT_EXPORT(dfs_lfs_init);