dfs_lfs.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. }
  465. dfs_lfs = (dfs_lfs_t*)dfs->data;
  466. if (file->flags & O_DIRECTORY)
  467. {
  468. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  469. if (dfs_lfs_fd == RT_NULL)
  470. {
  471. rt_kprintf("ERROR:no memory!\n");
  472. result = -ENOMEM;
  473. goto _error_dir;
  474. }
  475. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  476. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  477. if (file->flags & O_CREAT)
  478. {
  479. #ifndef LFS_READONLY
  480. result = lfs_mkdir(dfs_lfs_fd->lfs, file->vnode->path);
  481. #else
  482. result = -EINVAL;
  483. #endif
  484. if (result != LFS_ERR_OK)
  485. {
  486. goto _error_dir;
  487. }
  488. else
  489. {
  490. time_t now = time(RT_NULL);
  491. lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
  492. }
  493. }
  494. result = lfs_dir_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, file->vnode->path);
  495. if (result != LFS_ERR_OK)
  496. {
  497. goto _error_dir;
  498. }
  499. else
  500. {
  501. file->data = (void*)dfs_lfs_fd;
  502. return RT_EOK;
  503. }
  504. _error_dir:
  505. if (dfs_lfs_fd != RT_NULL)
  506. {
  507. rt_free(dfs_lfs_fd);
  508. }
  509. return _lfs_result_to_dfs(result);
  510. }
  511. else
  512. {
  513. dfs_lfs_fd_t* dfs_lfs_fd = rt_malloc(sizeof(dfs_lfs_fd_t));
  514. if (dfs_lfs_fd == RT_NULL)
  515. {
  516. rt_kprintf("ERROR:no memory!\n");
  517. result = -ENOMEM;
  518. goto _error_file;
  519. }
  520. rt_memset(dfs_lfs_fd, 0, sizeof(dfs_lfs_fd_t));
  521. dfs_lfs_fd->lfs = &dfs_lfs->lfs;
  522. if ((file->flags & 3) == O_RDONLY)
  523. flags |= LFS_O_RDONLY;
  524. if ((file->flags & 3) == O_WRONLY)
  525. flags |= LFS_O_WRONLY;
  526. if ((file->flags & 3) == O_RDWR)
  527. flags |= LFS_O_RDWR;
  528. if (file->flags & O_CREAT)
  529. flags |= LFS_O_CREAT;
  530. if (file->flags & O_EXCL)
  531. flags |= LFS_O_EXCL;
  532. if (file->flags & O_TRUNC)
  533. flags |= LFS_O_TRUNC;
  534. if (file->flags & O_APPEND)
  535. flags |= LFS_O_APPEND;
  536. result = lfs_file_open(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->vnode->path, flags);
  537. if (result != LFS_ERR_OK)
  538. {
  539. goto _error_file;
  540. }
  541. else
  542. {
  543. file->data = (void*)dfs_lfs_fd;
  544. file->pos = dfs_lfs_fd->u.file.pos;
  545. file->vnode->size = dfs_lfs_fd->u.file.ctz.size;
  546. return RT_EOK;
  547. }
  548. _error_file:
  549. if (dfs_lfs_fd != RT_NULL)
  550. {
  551. rt_free(dfs_lfs_fd);
  552. }
  553. return _lfs_result_to_dfs(result);
  554. }
  555. }
  556. static int _dfs_lfs_close(struct dfs_file* file)
  557. {
  558. int result;
  559. dfs_lfs_fd_t* dfs_lfs_fd;
  560. uint8_t need_time_update;
  561. RT_ASSERT(file != RT_NULL);
  562. RT_ASSERT(file->data != RT_NULL);
  563. RT_ASSERT(file->vnode->ref_count > 0);
  564. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  565. if (file->vnode->type == FT_DIRECTORY)
  566. {
  567. result = lfs_dir_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir);
  568. }
  569. else
  570. {
  571. need_time_update = (dfs_lfs_fd->u.file.flags & LFS_F_DIRTY) || (dfs_lfs_fd->u.file.flags & LFS_F_WRITING);
  572. result = lfs_file_close(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  573. if (result == LFS_ERR_OK && need_time_update)
  574. {
  575. time_t now = time(RT_NULL);
  576. lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
  577. }
  578. }
  579. rt_free(dfs_lfs_fd);
  580. file->data = RT_NULL;
  581. return _lfs_result_to_dfs(result);
  582. }
  583. static int _dfs_lfs_ioctl(struct dfs_file* file, int cmd, void* args)
  584. {
  585. switch (cmd)
  586. {
  587. case RT_FIOFTRUNCATE:
  588. {
  589. #ifdef LFS_READONLY
  590. return -EROFS;
  591. #else
  592. dfs_lfs_fd_t *dfs_lfs_fd;
  593. off_t length;
  594. int result;
  595. lfs_soff_t pos;
  596. RT_ASSERT(file != RT_NULL);
  597. RT_ASSERT(file->data != RT_NULL);
  598. if (file->vnode->type == FT_DIRECTORY)
  599. {
  600. return -EISDIR;
  601. }
  602. if (args == RT_NULL)
  603. {
  604. return -EINVAL;
  605. }
  606. if ((file->flags & 3) == O_RDONLY)
  607. {
  608. return -EBADF;
  609. }
  610. length = *(off_t *)args;
  611. if (length < 0)
  612. {
  613. return -EINVAL;
  614. }
  615. dfs_lfs_fd = (dfs_lfs_fd_t *)file->data;
  616. result = lfs_file_truncate(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, (lfs_off_t)length);
  617. if (result < 0)
  618. {
  619. return _lfs_result_to_dfs(result);
  620. }
  621. pos = lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  622. if (pos < 0)
  623. {
  624. return _lfs_result_to_dfs((int)pos);
  625. }
  626. file->pos = (rt_off_t)pos;
  627. pos = lfs_file_size(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  628. if (pos < 0)
  629. {
  630. return _lfs_result_to_dfs((int)pos);
  631. }
  632. file->vnode->size = (rt_off_t)pos;
  633. return RT_EOK;
  634. #endif
  635. }
  636. default:
  637. break;
  638. }
  639. return -ENOSYS;
  640. }
  641. static DFS_LFS_RW_RETURN_TYPE _dfs_lfs_read(struct dfs_file* file, void* buf, size_t len)
  642. {
  643. lfs_ssize_t ssize;
  644. dfs_lfs_fd_t* dfs_lfs_fd;
  645. RT_ASSERT(file != RT_NULL);
  646. RT_ASSERT(file->data != RT_NULL);
  647. if (file->vnode->type == FT_DIRECTORY)
  648. {
  649. return -EISDIR;
  650. }
  651. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  652. #if 0
  653. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  654. {
  655. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  656. if (soff < 0)
  657. {
  658. return _lfs_result_to_dfs(soff);
  659. }
  660. }
  661. #endif
  662. ssize = lfs_file_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  663. if (ssize < 0)
  664. {
  665. return _lfs_result_to_dfs(ssize);
  666. }
  667. /* update position */
  668. file->pos = dfs_lfs_fd->u.file.pos;
  669. return ssize;
  670. }
  671. #ifndef LFS_READONLY
  672. static DFS_LFS_RW_RETURN_TYPE _dfs_lfs_write(struct dfs_file* file, const void* buf, size_t len)
  673. {
  674. lfs_ssize_t ssize;
  675. dfs_lfs_fd_t* dfs_lfs_fd;
  676. RT_ASSERT(file != RT_NULL);
  677. RT_ASSERT(file->data != RT_NULL);
  678. if (file->vnode->type == FT_DIRECTORY)
  679. {
  680. return -EISDIR;
  681. }
  682. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  683. #if 0
  684. if (lfs_file_tell(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file) != file->pos)
  685. {
  686. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, file->pos, LFS_SEEK_SET);
  687. if (soff < 0)
  688. {
  689. return _lfs_result_to_dfs(soff);
  690. }
  691. }
  692. #endif
  693. ssize = lfs_file_write(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, buf, len);
  694. if (ssize < 0)
  695. {
  696. return _lfs_result_to_dfs(ssize);
  697. }
  698. /* update position and file size */
  699. file->pos = dfs_lfs_fd->u.file.pos;
  700. file->vnode->size = dfs_lfs_fd->u.file.ctz.size;
  701. return ssize;
  702. }
  703. #endif
  704. static int _dfs_lfs_flush(struct dfs_file* file)
  705. {
  706. int result;
  707. dfs_lfs_fd_t* dfs_lfs_fd;
  708. uint8_t need_time_update;
  709. RT_ASSERT(file != RT_NULL);
  710. RT_ASSERT(file->data != RT_NULL);
  711. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  712. need_time_update = (dfs_lfs_fd->u.file.flags & LFS_F_DIRTY) || (dfs_lfs_fd->u.file.flags & LFS_F_WRITING);
  713. result = lfs_file_sync(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file);
  714. if (result == LFS_ERR_OK && need_time_update)
  715. {
  716. time_t now = time(RT_NULL);
  717. lfs_setattr(dfs_lfs_fd->lfs, file->vnode->path, ATTR_TIMESTAMP, &now, sizeof(time_t));
  718. }
  719. return _lfs_result_to_dfs(result);
  720. }
  721. static DFS_LFS_LSK_RETURN_TYPE _dfs_lfs_lseek(struct dfs_file* file, rt_off_t offset)
  722. {
  723. dfs_lfs_fd_t* dfs_lfs_fd;
  724. RT_ASSERT(file != RT_NULL);
  725. RT_ASSERT(file->data != RT_NULL);
  726. dfs_lfs_fd = (dfs_lfs_fd_t*)file->data;
  727. if (file->vnode->type == FT_REGULAR)
  728. {
  729. lfs_soff_t soff = lfs_file_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.file, offset, LFS_SEEK_SET);
  730. if (soff < 0)
  731. {
  732. return _lfs_result_to_dfs(soff);
  733. }
  734. file->pos = dfs_lfs_fd->u.file.pos;
  735. }
  736. else if (file->vnode->type == FT_DIRECTORY)
  737. {
  738. /* skip . and .. */
  739. lfs_off_t off = offset / sizeof(struct dirent) + 2;
  740. lfs_soff_t soff = lfs_dir_seek(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, off);
  741. if (soff < 0)
  742. {
  743. return _lfs_result_to_dfs(soff);
  744. }
  745. /* file->pos must stay in user-space: remove the two dot entries accounted for in dir.pos */
  746. file->pos = (dfs_lfs_fd->u.dir.pos > 2 ? dfs_lfs_fd->u.dir.pos - 2 : 0) * sizeof(struct dirent);
  747. }
  748. return (file->pos);
  749. }
  750. static int _dfs_lfs_getdents(struct dfs_file* file, struct dirent* dirp, uint32_t count)
  751. {
  752. dfs_lfs_fd_t* dfs_lfs_fd;
  753. int result;
  754. int index;
  755. struct dirent* d;
  756. struct lfs_info info;
  757. RT_ASSERT(file->data != RT_NULL);
  758. dfs_lfs_fd = (dfs_lfs_fd_t*)(file->data);
  759. /* make integer count */
  760. count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
  761. if (count == 0)
  762. {
  763. return -EINVAL;
  764. }
  765. index = 0;
  766. while (1)
  767. {
  768. d = dirp + index;
  769. result = lfs_dir_read(dfs_lfs_fd->lfs, &dfs_lfs_fd->u.dir, &info);
  770. if ((result != 1) || (info.name[0] == 0))
  771. {
  772. break;
  773. }
  774. if (rt_strcmp(info.name, ".") == 0)
  775. {
  776. continue;
  777. }
  778. else if (rt_strcmp(info.name, "..") == 0)
  779. {
  780. continue;
  781. }
  782. d->d_type = DT_UNKNOWN;
  783. switch (info.type)
  784. {
  785. case LFS_TYPE_DIR:
  786. d->d_type |= DT_DIR;
  787. break;
  788. case LFS_TYPE_REG:
  789. d->d_type |= DT_REG;
  790. break;
  791. }
  792. d->d_namlen = (rt_uint8_t)rt_strlen(info.name);
  793. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  794. rt_strncpy(d->d_name, info.name, DFS_PATH_MAX);
  795. index++;
  796. if (index * sizeof(struct dirent) >= count)
  797. {
  798. break;
  799. }
  800. }
  801. if (index == 0)
  802. {
  803. return _lfs_result_to_dfs(result);
  804. }
  805. file->pos += index * sizeof(struct dirent);
  806. return index * sizeof(struct dirent);
  807. }
  808. static const struct dfs_file_ops _dfs_lfs_fops = {
  809. _dfs_lfs_open,
  810. _dfs_lfs_close,
  811. _dfs_lfs_ioctl,
  812. _dfs_lfs_read,
  813. #ifndef LFS_READONLY
  814. _dfs_lfs_write,
  815. #else
  816. NULL,
  817. #endif
  818. _dfs_lfs_flush,
  819. _dfs_lfs_lseek,
  820. _dfs_lfs_getdents,
  821. // RT_NULL, /* poll interface */
  822. };
  823. static const struct dfs_filesystem_ops _dfs_lfs_ops = {
  824. "lfs",
  825. DFS_FS_FLAG_DEFAULT,
  826. &_dfs_lfs_fops,
  827. _dfs_lfs_mount,
  828. _dfs_lfs_unmount,
  829. #ifndef LFS_READONLY
  830. _dfs_lfs_mkfs,
  831. #else
  832. NULL,
  833. #endif
  834. _dfs_lfs_statfs,
  835. #ifndef LFS_READONLY
  836. _dfs_lfs_unlink,
  837. #else
  838. NULL,
  839. #endif
  840. _dfs_lfs_stat,
  841. #ifndef LFS_READONLY
  842. _dfs_lfs_rename,
  843. #else
  844. NULL,
  845. #endif
  846. };
  847. int dfs_lfs_init(void)
  848. {
  849. /* register ram file system */
  850. return dfs_register(&_dfs_lfs_ops);
  851. }
  852. INIT_COMPONENT_EXPORT(dfs_lfs_init);