dfs_lfs.c 22 KB

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