dfs_lfs.c 23 KB

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