dfs_lfs.c 23 KB

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