dfs_cromfs.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  1. /*
  2. * Copyright (c) 2006-2020, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020/08/21 ShaoJinchun first version
  9. */
  10. #include <rtthread.h>
  11. #include <dfs.h>
  12. #include <dfs_fs.h>
  13. #include <dfs_file.h>
  14. #include <dfs_dentry.h>
  15. #include <dfs_mnt.h>
  16. #include "dfs_cromfs.h"
  17. #include <stdint.h>
  18. #include "zlib.h"
  19. #ifdef RT_USING_PAGECACHE
  20. #include "dfs_pcache.h"
  21. #endif
  22. /**********************************/
  23. #define CROMFS_PATITION_HEAD_SIZE 256
  24. #define CROMFS_DIRENT_CACHE_SIZE 8
  25. #define CROMFS_MAGIC "CROMFSMG"
  26. #define CROMFS_CT_ASSERT(name, x) \
  27. struct assert_##name {char ary[2 * (x) - 1];}
  28. #define CROMFS_POS_ROOT (0x0UL)
  29. #define CROMFS_POS_ERROR (0x1UL)
  30. typedef struct
  31. {
  32. uint8_t magic[8]; /* CROMFS_MAGIC */
  33. uint32_t version;
  34. uint32_t partition_attr; /* expand, now reserved 0 */
  35. uint32_t partition_size; /* with partition head */
  36. uint32_t root_dir_pos; /* root dir pos */
  37. uint32_t root_dir_size;
  38. } partition_head_data;
  39. typedef struct
  40. {
  41. partition_head_data head;
  42. uint8_t padding[CROMFS_PATITION_HEAD_SIZE - sizeof(partition_head_data)];
  43. } partition_head;
  44. enum
  45. {
  46. CROMFS_DIRENT_ATTR_FILE = 0x0UL,
  47. CROMFS_DIRENT_ATTR_DIR = 0x1UL,
  48. CROMFS_DIRENT_ATTR_SYMLINK = 0x2UL,
  49. };
  50. typedef struct
  51. {
  52. uint16_t attr; /* dir or file add other */
  53. uint16_t name_size; /* name real size */
  54. uint32_t file_size; /* file data size */
  55. uint32_t file_origin_size; /* file size before compress */
  56. uint32_t parition_pos; /* offset of data */
  57. uint8_t name[0]; /* name data */
  58. } cromfs_dirent;
  59. #define CROMFS_ALIGN_SIZE_BIT 4
  60. #define CROMFS_ALIGN_SIZE (1UL << CROMFS_ALIGN_SIZE_BIT) /* must be same as sizeof cromfs_dirent */
  61. #define CROMFS_ALIGN_SIZE_MASK (CROMFS_ALIGN_SIZE - 1)
  62. CROMFS_CT_ASSERT(align_size, CROMFS_ALIGN_SIZE == sizeof(cromfs_dirent));
  63. typedef union
  64. {
  65. cromfs_dirent dirent;
  66. uint8_t name[CROMFS_ALIGN_SIZE];
  67. } cromfs_dirent_item;
  68. /**********************************/
  69. typedef struct
  70. {
  71. rt_list_t list;
  72. uint32_t partition_pos;
  73. uint32_t size;
  74. uint8_t *buff;
  75. } cromfs_dirent_cache;
  76. typedef struct st_cromfs_info
  77. {
  78. rt_device_t device;
  79. uint32_t partition_size;
  80. uint32_t bytes_per_sector;
  81. uint32_t (*read_bytes)(struct st_cromfs_info *ci, uint32_t pos, void *buf, uint32_t size);
  82. partition_head_data part_info;
  83. struct rt_mutex lock;
  84. struct cromfs_avl_struct *cromfs_avl_root;
  85. rt_list_t cromfs_dirent_cache_head;
  86. int cromfs_dirent_cache_nr;
  87. const void *data;
  88. } cromfs_info;
  89. typedef struct
  90. {
  91. uint32_t ref;
  92. uint32_t partition_pos;
  93. cromfs_info *ci;
  94. uint32_t size;
  95. uint8_t *buff;
  96. uint32_t partition_size;
  97. int data_valid;
  98. } file_info;
  99. /**********************************/
  100. #define avl_key_t uint32_t
  101. #define AVL_EMPTY (struct cromfs_avl_struct *)0
  102. #define avl_maxheight 32
  103. #define heightof(tree) ((tree) == AVL_EMPTY ? 0 : (tree)->avl_height)
  104. struct cromfs_avl_struct
  105. {
  106. struct cromfs_avl_struct *avl_left;
  107. struct cromfs_avl_struct *avl_right;
  108. int avl_height;
  109. avl_key_t avl_key;
  110. file_info *fi;
  111. };
  112. static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree);
  113. static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree);
  114. static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct *ptree);
  115. static void cromfs_avl_rebalance(struct cromfs_avl_struct ***nodeplaces_ptr, int count)
  116. {
  117. for (;count > 0; count--)
  118. {
  119. struct cromfs_avl_struct **nodeplace = *--nodeplaces_ptr;
  120. struct cromfs_avl_struct *node = *nodeplace;
  121. struct cromfs_avl_struct *nodeleft = node->avl_left;
  122. struct cromfs_avl_struct *noderight = node->avl_right;
  123. int heightleft = heightof(nodeleft);
  124. int heightright = heightof(noderight);
  125. if (heightright + 1 < heightleft)
  126. {
  127. struct cromfs_avl_struct * nodeleftleft = nodeleft->avl_left;
  128. struct cromfs_avl_struct * nodeleftright = nodeleft->avl_right;
  129. int heightleftright = heightof(nodeleftright);
  130. if (heightof(nodeleftleft) >= heightleftright)
  131. {
  132. node->avl_left = nodeleftright;
  133. nodeleft->avl_right = node;
  134. nodeleft->avl_height = 1 + (node->avl_height = 1 + heightleftright);
  135. *nodeplace = nodeleft;
  136. }
  137. else
  138. {
  139. nodeleft->avl_right = nodeleftright->avl_left;
  140. node->avl_left = nodeleftright->avl_right;
  141. nodeleftright->avl_left = nodeleft;
  142. nodeleftright->avl_right = node;
  143. nodeleft->avl_height = node->avl_height = heightleftright;
  144. nodeleftright->avl_height = heightleft;
  145. *nodeplace = nodeleftright;
  146. }
  147. }
  148. else if (heightleft + 1 < heightright)
  149. {
  150. struct cromfs_avl_struct *noderightright = noderight->avl_right;
  151. struct cromfs_avl_struct *noderightleft = noderight->avl_left;
  152. int heightrightleft = heightof(noderightleft);
  153. if (heightof(noderightright) >= heightrightleft)
  154. {
  155. node->avl_right = noderightleft;
  156. noderight->avl_left = node;
  157. noderight->avl_height = 1 + (node->avl_height = 1 + heightrightleft);
  158. *nodeplace = noderight;
  159. }
  160. else
  161. {
  162. noderight->avl_left = noderightleft->avl_right;
  163. node->avl_right = noderightleft->avl_left;
  164. noderightleft->avl_right = noderight;
  165. noderightleft->avl_left = node;
  166. noderight->avl_height = node->avl_height = heightrightleft;
  167. noderightleft->avl_height = heightright;
  168. *nodeplace = noderightleft;
  169. }
  170. }
  171. else {
  172. int height = (heightleft<heightright ? heightright : heightleft) + 1;
  173. if (height == node->avl_height)
  174. {
  175. break;
  176. }
  177. node->avl_height = height;
  178. }
  179. }
  180. }
  181. static void cromfs_avl_remove(struct cromfs_avl_struct *node_to_delete, struct cromfs_avl_struct **ptree)
  182. {
  183. avl_key_t key = node_to_delete->avl_key;
  184. struct cromfs_avl_struct **nodeplace = ptree;
  185. struct cromfs_avl_struct **stack[avl_maxheight];
  186. uint32_t stack_count = 0;
  187. struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  188. struct cromfs_avl_struct **nodeplace_to_delete;
  189. for (;;)
  190. {
  191. struct cromfs_avl_struct *node = *nodeplace;
  192. if (node == AVL_EMPTY)
  193. {
  194. return;
  195. }
  196. *stack_ptr++ = nodeplace;
  197. stack_count++;
  198. if (key == node->avl_key)
  199. {
  200. break;
  201. }
  202. if (key < node->avl_key)
  203. {
  204. nodeplace = &node->avl_left;
  205. }
  206. else
  207. {
  208. nodeplace = &node->avl_right;
  209. }
  210. }
  211. nodeplace_to_delete = nodeplace;
  212. if (node_to_delete->avl_left == AVL_EMPTY)
  213. {
  214. *nodeplace_to_delete = node_to_delete->avl_right;
  215. stack_ptr--;
  216. stack_count--;
  217. }
  218. else
  219. {
  220. struct cromfs_avl_struct *** stack_ptr_to_delete = stack_ptr;
  221. struct cromfs_avl_struct ** nodeplace = &node_to_delete->avl_left;
  222. struct cromfs_avl_struct * node;
  223. for (;;)
  224. {
  225. node = *nodeplace;
  226. if (node->avl_right == AVL_EMPTY)
  227. {
  228. break;
  229. }
  230. *stack_ptr++ = nodeplace;
  231. stack_count++;
  232. nodeplace = &node->avl_right;
  233. }
  234. *nodeplace = node->avl_left;
  235. node->avl_left = node_to_delete->avl_left;
  236. node->avl_right = node_to_delete->avl_right;
  237. node->avl_height = node_to_delete->avl_height;
  238. *nodeplace_to_delete = node;
  239. *stack_ptr_to_delete = &node->avl_left;
  240. }
  241. cromfs_avl_rebalance(stack_ptr,stack_count);
  242. }
  243. static void cromfs_avl_insert(struct cromfs_avl_struct *new_node, struct cromfs_avl_struct **ptree)
  244. {
  245. avl_key_t key = new_node->avl_key;
  246. struct cromfs_avl_struct **nodeplace = ptree;
  247. struct cromfs_avl_struct **stack[avl_maxheight];
  248. int stack_count = 0;
  249. struct cromfs_avl_struct ***stack_ptr = &stack[0]; /* = &stack[stackcount] */
  250. for (;;)
  251. {
  252. struct cromfs_avl_struct * node = *nodeplace;
  253. if (node == AVL_EMPTY)
  254. {
  255. break;
  256. }
  257. *stack_ptr++ = nodeplace;
  258. stack_count++;
  259. if (key < node->avl_key)
  260. {
  261. nodeplace = &node->avl_left;
  262. }
  263. else
  264. {
  265. nodeplace = &node->avl_right;
  266. }
  267. }
  268. new_node->avl_left = AVL_EMPTY;
  269. new_node->avl_right = AVL_EMPTY;
  270. new_node->avl_height = 1;
  271. *nodeplace = new_node;
  272. cromfs_avl_rebalance(stack_ptr,stack_count);
  273. }
  274. static struct cromfs_avl_struct* cromfs_avl_find(avl_key_t key, struct cromfs_avl_struct* ptree)
  275. {
  276. for (;;)
  277. {
  278. if (ptree == AVL_EMPTY)
  279. {
  280. return (struct cromfs_avl_struct *)0;
  281. }
  282. if (key == ptree->avl_key)
  283. {
  284. break;
  285. }
  286. if (key < ptree->avl_key)
  287. {
  288. ptree = ptree->avl_left;
  289. }
  290. else
  291. {
  292. ptree = ptree->avl_right;
  293. }
  294. }
  295. return ptree;
  296. }
  297. /**********************************/
  298. static uint32_t cromfs_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  299. {
  300. if (pos >= ci->partition_size || pos + size > ci->partition_size)
  301. {
  302. return 0;
  303. }
  304. return ci->read_bytes(ci, pos, buf, size);
  305. }
  306. static uint32_t cromfs_noblk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  307. {
  308. uint32_t ret = 0;
  309. ret = rt_device_read(ci->device, pos, buf, size);
  310. if (ret != size)
  311. {
  312. return 0;
  313. }
  314. else
  315. {
  316. return ret;
  317. }
  318. }
  319. static uint32_t cromfs_data_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  320. {
  321. uint32_t ret = 0;
  322. uint8_t *data = (uint8_t *)ci->data;
  323. if (data)
  324. {
  325. memcpy(buf, data + pos, size);
  326. ret = size;
  327. }
  328. return ret;
  329. }
  330. static uint32_t cromfs_blk_read_bytes(cromfs_info *ci, uint32_t pos, void *buf, uint32_t size)
  331. {
  332. uint32_t ret = 0;
  333. uint32_t size_bak = size;
  334. uint32_t start_blk = 0;
  335. uint32_t end_blk = 0;
  336. uint32_t off_s = 0;
  337. uint32_t sector_nr = 0;
  338. uint8_t *block_buff = NULL;
  339. uint32_t ret_len = 0;
  340. if (!size || !buf)
  341. {
  342. return 0;
  343. }
  344. block_buff = (uint8_t *)malloc(2 * ci->bytes_per_sector);
  345. if (!block_buff)
  346. {
  347. return 0;
  348. }
  349. start_blk = pos / ci->bytes_per_sector;
  350. off_s = pos % ci->bytes_per_sector;
  351. end_blk = (pos + size - 1) / ci->bytes_per_sector;
  352. sector_nr = end_blk - start_blk;
  353. if (sector_nr < 2)
  354. {
  355. ret_len = rt_device_read(ci->device, start_blk, block_buff, sector_nr + 1);
  356. if (ret_len != sector_nr + 1)
  357. {
  358. goto end;
  359. }
  360. memcpy(buf, block_buff + off_s, size);
  361. }
  362. else
  363. {
  364. ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
  365. if (ret_len != 1)
  366. {
  367. goto end;
  368. }
  369. memcpy(buf, block_buff + off_s, ci->bytes_per_sector - off_s);
  370. off_s = (ci->bytes_per_sector - off_s);
  371. size -= off_s;
  372. sector_nr--;
  373. start_blk++;
  374. if (sector_nr)
  375. {
  376. ret_len = rt_device_read(ci->device, start_blk, (char*)buf + off_s, sector_nr);
  377. if (ret_len != sector_nr)
  378. {
  379. goto end;
  380. }
  381. start_blk += sector_nr;
  382. off_s += (sector_nr * ci->bytes_per_sector);
  383. size -= (sector_nr * ci->bytes_per_sector);
  384. }
  385. ret_len = rt_device_read(ci->device, start_blk, block_buff, 1);
  386. if (ret_len != 1)
  387. {
  388. goto end;
  389. }
  390. memcpy((char*)buf + off_s, block_buff, size);
  391. }
  392. ret = size_bak;
  393. end:
  394. free(block_buff);
  395. return ret;
  396. }
  397. /**********************************/
  398. static uint8_t *cromfs_dirent_cache_get(cromfs_info *ci, uint32_t pos, uint32_t size)
  399. {
  400. rt_list_t *l = NULL;
  401. cromfs_dirent_cache *dir = NULL;
  402. uint32_t len = 0;
  403. /* find */
  404. for (l = ci->cromfs_dirent_cache_head.next; l != &ci->cromfs_dirent_cache_head; l = l->next)
  405. {
  406. dir = (cromfs_dirent_cache *)l;
  407. if (dir->partition_pos == pos)
  408. {
  409. RT_ASSERT(dir->size == size);
  410. rt_list_remove(l);
  411. rt_list_insert_after(&ci->cromfs_dirent_cache_head, l);
  412. return dir->buff;
  413. }
  414. }
  415. /* not found */
  416. if (ci->cromfs_dirent_cache_nr >= CROMFS_DIRENT_CACHE_SIZE)
  417. {
  418. l = ci->cromfs_dirent_cache_head.prev;
  419. dir = (cromfs_dirent_cache *)l;
  420. rt_list_remove(l);
  421. free(dir->buff);
  422. free(dir);
  423. ci->cromfs_dirent_cache_nr--;
  424. }
  425. dir = (cromfs_dirent_cache *)malloc(sizeof *dir);
  426. if (!dir)
  427. {
  428. return NULL;
  429. }
  430. dir->buff = (uint8_t *)malloc(size);
  431. if (!dir->buff)
  432. {
  433. free(dir);
  434. return NULL;
  435. }
  436. len = cromfs_read_bytes(ci, pos, dir->buff, size);
  437. if (len != size)
  438. {
  439. free(dir->buff);
  440. free(dir);
  441. return NULL;
  442. }
  443. rt_list_insert_after(&ci->cromfs_dirent_cache_head, (rt_list_t *)dir);
  444. ci->cromfs_dirent_cache_nr++;
  445. dir->partition_pos = pos;
  446. dir->size = size;
  447. return dir->buff;
  448. }
  449. static void cromfs_dirent_cache_destroy(cromfs_info *ci)
  450. {
  451. rt_list_t *l = NULL;
  452. cromfs_dirent_cache *dir = NULL;
  453. while ((l = ci->cromfs_dirent_cache_head.next) != &ci->cromfs_dirent_cache_head)
  454. {
  455. rt_list_remove(l);
  456. dir = (cromfs_dirent_cache *)l;
  457. free(dir->buff);
  458. free(dir);
  459. ci->cromfs_dirent_cache_nr--;
  460. }
  461. }
  462. /**********************************/
  463. #ifdef RT_USING_PAGECACHE
  464. static ssize_t dfs_cromfs_page_read(struct dfs_file *file, struct dfs_page *page);
  465. static struct dfs_aspace_ops dfs_cromfs_aspace_ops =
  466. {
  467. .read = dfs_cromfs_page_read
  468. };
  469. #endif
  470. static int dfs_cromfs_mount(struct dfs_mnt *mnt, unsigned long rwflag, const void *data)
  471. {
  472. struct rt_device_blk_geometry geometry;
  473. uint32_t len = 0;
  474. cromfs_info *ci = NULL;
  475. ci = (cromfs_info *)malloc(sizeof *ci);
  476. if (!ci)
  477. {
  478. return -ENOMEM;
  479. }
  480. memset(ci, 0, sizeof *ci);
  481. ci->device = mnt->dev_id;
  482. ci->partition_size = UINT32_MAX;
  483. if (ci->device)
  484. {
  485. rt_err_t ret = rt_device_open(ci->device, RT_DEVICE_OFLAG_RDONLY);
  486. if (ret != RT_EOK)
  487. {
  488. free(ci);
  489. return ret;
  490. }
  491. if (ci->device->type == RT_Device_Class_Block)
  492. {
  493. rt_device_control(ci->device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);
  494. ci->bytes_per_sector = geometry.bytes_per_sector;
  495. ci->read_bytes = cromfs_blk_read_bytes;
  496. }
  497. else
  498. {
  499. ci->read_bytes = cromfs_noblk_read_bytes;
  500. }
  501. }
  502. else if (data)
  503. {
  504. ci->data = data;
  505. ci->read_bytes = cromfs_data_read_bytes;
  506. }
  507. else
  508. {
  509. free(ci);
  510. return -RT_EIO;
  511. }
  512. len = cromfs_read_bytes(ci, 0, &ci->part_info, sizeof ci->part_info);
  513. if (len != sizeof ci->part_info ||
  514. memcmp(ci->part_info.magic, CROMFS_MAGIC, sizeof ci->part_info.magic) != 0)
  515. {
  516. free(ci);
  517. return -RT_ERROR;
  518. }
  519. ci->partition_size = ci->part_info.partition_size;
  520. mnt->data = ci;
  521. rt_mutex_init(&ci->lock, "crom", RT_IPC_FLAG_FIFO);
  522. ci->cromfs_avl_root = NULL;
  523. rt_list_init(&ci->cromfs_dirent_cache_head);
  524. ci->cromfs_dirent_cache_nr = 0;
  525. return RT_EOK;
  526. }
  527. static int dfs_cromfs_unmount(struct dfs_mnt *mnt)
  528. {
  529. rt_err_t result = RT_EOK;
  530. cromfs_info *ci = NULL;
  531. ci = (cromfs_info *)mnt->data;
  532. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  533. if (result != RT_EOK)
  534. {
  535. return -RT_ERROR;
  536. }
  537. cromfs_dirent_cache_destroy(ci);
  538. while (ci->cromfs_avl_root)
  539. {
  540. struct cromfs_avl_struct *node;
  541. file_info *fi = NULL;
  542. node = ci->cromfs_avl_root;
  543. fi = node->fi;
  544. cromfs_avl_remove(node, &ci->cromfs_avl_root);
  545. free(node);
  546. if (fi->buff)
  547. {
  548. free(fi->buff);
  549. }
  550. free(fi);
  551. }
  552. if (ci->device)
  553. {
  554. rt_device_close(ci->device);
  555. }
  556. rt_mutex_detach(&ci->lock);
  557. free(ci);
  558. return RT_EOK;
  559. }
  560. static uint32_t cromfs_lookup(cromfs_info *ci, const char *path, int* file_type, uint32_t *size, uint32_t *osize)
  561. {
  562. uint32_t cur_size = 0, cur_pos = 0, cur_osize = 0;
  563. const char *subpath = NULL, *subpath_end = NULL;
  564. void *di_mem = NULL;
  565. int _file_type = 0;
  566. if (path[0] == '\0')
  567. {
  568. return CROMFS_POS_ERROR;
  569. }
  570. cur_size = ci->part_info.root_dir_size;
  571. cur_osize = 0;
  572. cur_pos = ci->part_info.root_dir_pos;
  573. _file_type = CROMFS_DIRENT_ATTR_DIR;
  574. subpath_end = path;
  575. while (1)
  576. {
  577. cromfs_dirent_item *di_iter = NULL;
  578. int found = 0;
  579. /* skip /// */
  580. while (*subpath_end && *subpath_end == '/')
  581. {
  582. subpath_end++;
  583. }
  584. subpath = subpath_end;
  585. while ((*subpath_end != '/') && *subpath_end)
  586. {
  587. subpath_end++;
  588. }
  589. if (*subpath == '\0')
  590. {
  591. break;
  592. }
  593. /* if not dir or empty dir, error */
  594. if (_file_type != CROMFS_DIRENT_ATTR_DIR || !cur_size)
  595. {
  596. return CROMFS_POS_ERROR;
  597. }
  598. /* find subpath */
  599. di_mem = cromfs_dirent_cache_get(ci, cur_pos, cur_size);
  600. if (!di_mem)
  601. {
  602. return CROMFS_POS_ERROR;
  603. }
  604. found = 0;
  605. di_iter = (cromfs_dirent_item *)di_mem;
  606. while (1)
  607. {
  608. uint32_t name_len = subpath_end - subpath;
  609. uint32_t name_block = 0;
  610. if (di_iter->dirent.name_size == name_len)
  611. {
  612. if (memcmp(di_iter->dirent.name, subpath, name_len) == 0)
  613. {
  614. found = 1;
  615. cur_size = di_iter->dirent.file_size;
  616. cur_osize = di_iter->dirent.file_origin_size;
  617. cur_pos = di_iter->dirent.parition_pos;
  618. if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_FILE)
  619. {
  620. _file_type = CROMFS_DIRENT_ATTR_FILE;
  621. }
  622. else if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
  623. {
  624. _file_type = CROMFS_DIRENT_ATTR_DIR;
  625. }
  626. else if (di_iter->dirent.attr == CROMFS_DIRENT_ATTR_SYMLINK)
  627. {
  628. _file_type = CROMFS_DIRENT_ATTR_SYMLINK;
  629. }
  630. else
  631. {
  632. RT_ASSERT(0);
  633. }
  634. break;
  635. }
  636. }
  637. name_block = (di_iter->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) >> CROMFS_ALIGN_SIZE_BIT;
  638. di_iter += (1 + name_block);
  639. if ((uint32_t)(intptr_t)di_iter - (uint32_t)(intptr_t)di_mem >= cur_size)
  640. {
  641. break;
  642. }
  643. }
  644. if (!found)
  645. {
  646. return CROMFS_POS_ERROR;
  647. }
  648. }
  649. *size = cur_size;
  650. *osize = cur_osize;
  651. *file_type = _file_type;
  652. return cur_pos;
  653. }
  654. static uint32_t __dfs_cromfs_lookup(cromfs_info *ci, const char *path, int* file_type, uint32_t *size, uint32_t *osize)
  655. {
  656. rt_err_t result = RT_EOK;
  657. uint32_t ret = 0;
  658. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  659. if (result != RT_EOK)
  660. {
  661. return CROMFS_POS_ERROR;
  662. }
  663. ret = cromfs_lookup(ci, path, file_type, size, osize);
  664. rt_mutex_release(&ci->lock);
  665. return ret;
  666. }
  667. static int fill_file_data(file_info *fi)
  668. {
  669. int ret = -1;
  670. cromfs_info *ci = NULL;
  671. void *compressed_file_buff = NULL;
  672. uLongf size = 0, osize = 0;
  673. if (!fi->data_valid)
  674. {
  675. RT_ASSERT(fi->buff != NULL);
  676. ci = fi->ci;
  677. osize = fi->size;
  678. size = fi->partition_size;
  679. compressed_file_buff = (void *)malloc(size);
  680. if (!compressed_file_buff)
  681. {
  682. goto end;
  683. }
  684. if (cromfs_read_bytes(ci, fi->partition_pos, compressed_file_buff, size) != size)
  685. {
  686. goto end;
  687. }
  688. if (uncompress((uint8_t *)fi->buff, (uLongf *)&osize, (uint8_t *)compressed_file_buff, size) != Z_OK)
  689. {
  690. goto end;
  691. }
  692. fi->data_valid = 1;
  693. }
  694. ret = 0;
  695. end:
  696. if (compressed_file_buff)
  697. {
  698. free(compressed_file_buff);
  699. }
  700. return ret;
  701. }
  702. static ssize_t dfs_cromfs_read(struct dfs_file *file, void *buf, size_t count, off_t *pos)
  703. {
  704. rt_err_t result = RT_EOK;
  705. file_info *fi = NULL;
  706. cromfs_info *ci = NULL;
  707. ssize_t length = 0;
  708. ci = (cromfs_info *)file->dentry->mnt->data;
  709. fi = (file_info *)file->vnode->data;
  710. if ((off_t)count < (off_t)file->vnode->size - *pos)
  711. {
  712. length = count;
  713. }
  714. else
  715. {
  716. length = (off_t)file->vnode->size - *pos;
  717. }
  718. if (length > 0)
  719. {
  720. RT_ASSERT(fi->size != 0);
  721. if (fi->buff)
  722. {
  723. int fill_ret = 0;
  724. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  725. if (result != RT_EOK)
  726. {
  727. return 0;
  728. }
  729. fill_ret = fill_file_data(fi);
  730. rt_mutex_release(&ci->lock);
  731. if (fill_ret < 0)
  732. {
  733. return 0;
  734. }
  735. memcpy(buf, fi->buff + *pos, length);
  736. }
  737. else
  738. {
  739. void *di_mem = NULL;
  740. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  741. if (result != RT_EOK)
  742. {
  743. return 0;
  744. }
  745. di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
  746. if (di_mem)
  747. {
  748. memcpy(buf, (char*)di_mem + *pos, length);
  749. }
  750. rt_mutex_release(&ci->lock);
  751. if (!di_mem)
  752. {
  753. return 0;
  754. }
  755. }
  756. /* update file current position */
  757. *pos += length;
  758. }
  759. return length;
  760. }
  761. static file_info *get_file_info(cromfs_info *ci, uint32_t partition_pos, int inc_ref)
  762. {
  763. struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
  764. if (node)
  765. {
  766. if (inc_ref)
  767. {
  768. node->fi->ref++;
  769. }
  770. return node->fi;
  771. }
  772. return NULL;
  773. }
  774. static file_info *inset_file_info(cromfs_info *ci, uint32_t partition_pos, int file_type, uint32_t size, uint32_t osize)
  775. {
  776. file_info *fi = NULL;
  777. void *file_buff = NULL;
  778. struct cromfs_avl_struct *node = NULL;
  779. fi = (file_info *)malloc(sizeof *fi);
  780. if (!fi)
  781. {
  782. goto err;
  783. }
  784. fi->partition_pos = partition_pos;
  785. fi->ci = ci;
  786. if (file_type == CROMFS_DIRENT_ATTR_DIR)
  787. {
  788. fi->size = size;
  789. }
  790. else
  791. {
  792. fi->size = osize;
  793. fi->partition_size = size;
  794. fi->data_valid = 0;
  795. if (osize)
  796. {
  797. file_buff = (void *)malloc(osize);
  798. if (!file_buff)
  799. {
  800. goto err;
  801. }
  802. }
  803. }
  804. fi->buff = file_buff;
  805. fi->ref = 1;
  806. node = (struct cromfs_avl_struct *)malloc(sizeof *node);
  807. if (!node)
  808. {
  809. goto err;
  810. }
  811. node->avl_key = partition_pos;
  812. node->fi = fi;
  813. cromfs_avl_insert(node, &ci->cromfs_avl_root);
  814. return fi;
  815. err:
  816. if (file_buff)
  817. {
  818. free(file_buff);
  819. }
  820. if (fi)
  821. {
  822. free(fi);
  823. }
  824. return NULL;
  825. }
  826. static void deref_file_info(cromfs_info *ci, uint32_t partition_pos)
  827. {
  828. struct cromfs_avl_struct* node = cromfs_avl_find(partition_pos, ci->cromfs_avl_root);
  829. file_info *fi = NULL;
  830. if (node)
  831. {
  832. node->fi->ref--;
  833. if (node->fi->ref == 0)
  834. {
  835. fi = node->fi;
  836. cromfs_avl_remove(node, &ci->cromfs_avl_root);
  837. free(node);
  838. if (fi->buff)
  839. {
  840. free(fi->buff);
  841. }
  842. free(fi);
  843. }
  844. }
  845. }
  846. static int dfs_cromfs_close(struct dfs_file *file)
  847. {
  848. file_info *fi = NULL;
  849. cromfs_info *ci = NULL;
  850. rt_err_t result = 0;
  851. RT_ASSERT(file->vnode->ref_count > 0);
  852. if (file->vnode->ref_count > 1)
  853. {
  854. return 0;
  855. }
  856. fi = (file_info *)file->vnode->data;
  857. ci = (cromfs_info *)file->dentry->mnt->data;
  858. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  859. if (result != RT_EOK)
  860. {
  861. return -RT_ERROR;
  862. }
  863. deref_file_info(ci, fi->partition_pos);
  864. rt_mutex_release(&ci->lock);
  865. file->vnode->data = NULL;
  866. return RT_EOK;
  867. }
  868. static int dfs_cromfs_open(struct dfs_file *file)
  869. {
  870. int ret = 0;
  871. file_info *fi = NULL;
  872. cromfs_info *ci = NULL;
  873. uint32_t file_pos = 0;
  874. uint32_t size = 0, osize = 0;
  875. int file_type = 0;
  876. rt_err_t result = RT_EOK;
  877. if (file->flags & (O_CREAT | O_WRONLY | O_APPEND | O_TRUNC | O_RDWR))
  878. {
  879. return -EINVAL;
  880. }
  881. RT_ASSERT(file->vnode->ref_count > 0);
  882. if (file->vnode->ref_count > 1)
  883. {
  884. if (file->vnode->type == FT_DIRECTORY
  885. && !(file->flags & O_DIRECTORY))
  886. {
  887. return -ENOENT;
  888. }
  889. file->fpos = 0;
  890. return 0;
  891. }
  892. ci = (cromfs_info *)file->dentry->mnt->data;
  893. file_pos = __dfs_cromfs_lookup(ci, file->dentry->pathname, &file_type, &size, &osize);
  894. if (file_pos == CROMFS_POS_ERROR)
  895. {
  896. ret = -ENOENT;
  897. goto end;
  898. }
  899. /* entry is a directory file type */
  900. if (file_type == CROMFS_DIRENT_ATTR_DIR)
  901. {
  902. if (!(file->flags & O_DIRECTORY))
  903. {
  904. ret = -ENOENT;
  905. goto end;
  906. }
  907. file->vnode->type = FT_DIRECTORY;
  908. }
  909. else if (file_type == CROMFS_DIRENT_ATTR_SYMLINK)
  910. {
  911. file->vnode->type = FT_SYMLINK;
  912. }
  913. else
  914. {
  915. /* entry is a file, but open it as a directory */
  916. if (file->flags & O_DIRECTORY)
  917. {
  918. ret = -ENOENT;
  919. goto end;
  920. }
  921. file->vnode->type = FT_REGULAR;
  922. }
  923. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  924. if (result != RT_EOK)
  925. {
  926. ret = -EINTR;
  927. goto end;
  928. }
  929. fi = get_file_info(ci, file_pos, 1);
  930. if (!fi)
  931. {
  932. fi = inset_file_info(ci, file_pos, file_type, size, osize);
  933. }
  934. rt_mutex_release(&ci->lock);
  935. if (!fi)
  936. {
  937. ret = -ENOENT;
  938. goto end;
  939. }
  940. file->vnode->data = fi;
  941. if (file_type)
  942. {
  943. file->vnode->size = size;
  944. }
  945. else
  946. {
  947. file->vnode->size = osize;
  948. }
  949. file->fpos = 0;
  950. ret = RT_EOK;
  951. end:
  952. return ret;
  953. }
  954. static int dfs_cromfs_stat(struct dfs_dentry *dentry, struct stat *st)
  955. {
  956. uint32_t size = 0, osize = 0;
  957. int file_type = 0;
  958. cromfs_info *ci = NULL;
  959. uint32_t file_pos = 0;
  960. ci = (cromfs_info *)dentry->mnt->data;
  961. file_pos = __dfs_cromfs_lookup(ci, dentry->pathname, &file_type, &size, &osize);
  962. if (file_pos == CROMFS_POS_ERROR)
  963. {
  964. return -ENOENT;
  965. }
  966. st->st_dev = 0;
  967. st->st_mode = S_IFREG | (0777);
  968. if (file_type == CROMFS_DIRENT_ATTR_DIR)
  969. {
  970. st->st_mode &= ~S_IFREG;
  971. st->st_mode |= S_IFDIR;
  972. st->st_size = size;
  973. }
  974. else if(file_type == CROMFS_DIRENT_ATTR_SYMLINK)
  975. {
  976. st->st_mode &= ~S_IFREG;
  977. st->st_mode |= S_IFLNK;
  978. st->st_size = osize;
  979. }
  980. else
  981. {
  982. #ifdef RT_USING_PAGECACHE
  983. st->st_size = (dentry->vnode && dentry->vnode->aspace) ? dentry->vnode->size : osize;
  984. #else
  985. st->st_size = osize;
  986. #endif
  987. }
  988. st->st_mtime = 0;
  989. return RT_EOK;
  990. }
  991. static int dfs_cromfs_getdents(struct dfs_file *file, struct dirent *dirp, uint32_t count)
  992. {
  993. uint32_t index = 0;
  994. uint8_t *name = NULL;
  995. struct dirent *d = NULL;
  996. file_info *fi = NULL;
  997. cromfs_info *ci = NULL;
  998. cromfs_dirent_item *dirent = NULL, *sub_dirent = NULL;
  999. void *di_mem = NULL;
  1000. rt_err_t result = RT_EOK;
  1001. fi = (file_info *)file->vnode->data;
  1002. ci = fi->ci;
  1003. RT_ASSERT(fi->buff == NULL);
  1004. if (!fi->size)
  1005. {
  1006. return -EINVAL;
  1007. }
  1008. dirent = (cromfs_dirent_item *)malloc(fi->size);
  1009. if (!dirent)
  1010. {
  1011. return -ENOMEM;
  1012. }
  1013. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  1014. if (result != RT_EOK)
  1015. {
  1016. free(dirent);
  1017. return -EINTR;
  1018. }
  1019. di_mem = cromfs_dirent_cache_get(ci, fi->partition_pos, fi->size);
  1020. if (di_mem)
  1021. {
  1022. memcpy(dirent, di_mem, fi->size);
  1023. }
  1024. rt_mutex_release(&ci->lock);
  1025. if (!di_mem)
  1026. {
  1027. free(dirent);
  1028. return -ENOMEM;
  1029. }
  1030. /* make integer count */
  1031. count = (count / sizeof(struct dirent));
  1032. if (count == 0)
  1033. {
  1034. free(dirent);
  1035. return -EINVAL;
  1036. }
  1037. for (index = 0; index < count && file->fpos < file->vnode->size; index++)
  1038. {
  1039. uint32_t name_size = 0;
  1040. d = dirp + index;
  1041. sub_dirent = &dirent[file->fpos >> CROMFS_ALIGN_SIZE_BIT];
  1042. name = sub_dirent->dirent.name;
  1043. /* fill dirent */
  1044. if (sub_dirent->dirent.attr == CROMFS_DIRENT_ATTR_DIR)
  1045. {
  1046. d->d_type = DT_DIR;
  1047. }
  1048. else
  1049. {
  1050. d->d_type = DT_REG;
  1051. }
  1052. d->d_namlen = sub_dirent->dirent.name_size;
  1053. d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
  1054. memcpy(d->d_name, (char *)name, sub_dirent->dirent.name_size);
  1055. d->d_name[sub_dirent->dirent.name_size] = '\0';
  1056. name_size = (sub_dirent->dirent.name_size + CROMFS_ALIGN_SIZE_MASK) & ~CROMFS_ALIGN_SIZE_MASK;
  1057. /* move to next position */
  1058. file->fpos += (name_size + sizeof *sub_dirent);
  1059. }
  1060. free(dirent);
  1061. return index * sizeof(struct dirent);
  1062. }
  1063. static struct dfs_vnode *dfs_cromfs_lookup (struct dfs_dentry *dentry)
  1064. {
  1065. struct dfs_vnode *vnode = RT_NULL;
  1066. cromfs_info *ci = NULL;
  1067. RT_ASSERT(dentry != RT_NULL);
  1068. RT_ASSERT(dentry->mnt != RT_NULL);
  1069. ci = (cromfs_info *)dentry->mnt->data;
  1070. if (ci)
  1071. {
  1072. uint32_t size = 0, osize = 0;
  1073. int file_type = 0;
  1074. uint32_t file_pos = __dfs_cromfs_lookup(ci, dentry->pathname, &file_type, &size, &osize);
  1075. if (file_pos != CROMFS_POS_ERROR)
  1076. {
  1077. vnode = dfs_vnode_create();
  1078. if (vnode)
  1079. {
  1080. vnode->nlink = 1;
  1081. if (file_type == CROMFS_DIRENT_ATTR_DIR)
  1082. {
  1083. vnode->mode = S_IFDIR | (0777);
  1084. vnode->type = FT_DIRECTORY;
  1085. vnode->size = size;
  1086. }
  1087. else if (file_type == CROMFS_DIRENT_ATTR_SYMLINK)
  1088. {
  1089. vnode->mode = S_IFLNK | (0777);
  1090. vnode->type = FT_SYMLINK;
  1091. vnode->size = osize;
  1092. }
  1093. else
  1094. {
  1095. vnode->mode = S_IFREG | (0777);
  1096. vnode->type = FT_REGULAR;
  1097. vnode->size = osize;
  1098. #ifdef RT_USING_PAGECACHE
  1099. vnode->aspace = dfs_aspace_create(dentry, vnode, &dfs_cromfs_aspace_ops);
  1100. #endif
  1101. }
  1102. vnode->mnt = dentry->mnt;
  1103. }
  1104. }
  1105. }
  1106. return vnode;
  1107. }
  1108. static int dfs_cromfs_free_vnode(struct dfs_vnode *vnode)
  1109. {
  1110. return 0;
  1111. }
  1112. static int cromfs_readlink(cromfs_info *ci, char *path, char *buf, int len)
  1113. {
  1114. int ret = 0;
  1115. file_info *fi = NULL;
  1116. uint32_t file_pos = 0;
  1117. int file_type = 0;
  1118. uint32_t size = 0, osize = 0;
  1119. rt_err_t result = RT_EOK;
  1120. file_pos = __dfs_cromfs_lookup(ci, path, &file_type, &size, &osize);
  1121. if (file_pos == CROMFS_POS_ERROR)
  1122. {
  1123. ret = -ENOENT;
  1124. goto end1;
  1125. }
  1126. result = rt_mutex_take(&ci->lock, RT_WAITING_FOREVER);
  1127. if (result != RT_EOK)
  1128. {
  1129. ret = -EINTR;
  1130. goto end;
  1131. }
  1132. fi = get_file_info(ci, file_pos, 1);
  1133. if (!fi)
  1134. {
  1135. fi = inset_file_info(ci, file_pos, file_type, size, osize);
  1136. }
  1137. rt_mutex_release(&ci->lock);
  1138. if (!fi)
  1139. {
  1140. ret = -ENOENT;
  1141. goto end;
  1142. }
  1143. if (len > 0)
  1144. {
  1145. RT_ASSERT(fi->size != 0);
  1146. RT_ASSERT(fi->buff);
  1147. int fill_ret = 0;
  1148. fill_ret = fill_file_data(fi);
  1149. if (fill_ret < 0)
  1150. {
  1151. ret = -ENOENT;
  1152. deref_file_info(ci, fi->partition_pos);
  1153. goto end;
  1154. }
  1155. len = len - 1;
  1156. osize = osize < len ? osize : len;
  1157. memcpy(buf, fi->buff, osize);
  1158. }
  1159. if (ret == 0)
  1160. {
  1161. buf[osize] = '\0';
  1162. ret = osize;
  1163. }
  1164. deref_file_info(ci, fi->partition_pos);
  1165. end:
  1166. rt_mutex_release(&ci->lock);
  1167. end1:
  1168. return ret;
  1169. }
  1170. #ifdef RT_USING_PAGECACHE
  1171. static ssize_t dfs_cromfs_page_read(struct dfs_file *file, struct dfs_page *page)
  1172. {
  1173. int ret = -EINVAL;
  1174. if (page->page)
  1175. {
  1176. off_t fpos = page->fpos;
  1177. ret = dfs_cromfs_read(file, page->page, page->size, &fpos);
  1178. }
  1179. return ret;
  1180. }
  1181. #endif
  1182. static int dfs_cromfs_readlink(struct dfs_dentry *dentry, char *buf, int len)
  1183. {
  1184. cromfs_info *ci = NULL;
  1185. if (dentry && buf)
  1186. {
  1187. ci = (cromfs_info *)dentry->mnt->data;
  1188. return cromfs_readlink(ci, dentry->pathname, buf, len);
  1189. }
  1190. return -EBADF;
  1191. }
  1192. static const struct dfs_file_ops _crom_fops =
  1193. {
  1194. .open = dfs_cromfs_open,
  1195. .close = dfs_cromfs_close,
  1196. .lseek = generic_dfs_lseek,
  1197. .read = dfs_cromfs_read,
  1198. .getdents = dfs_cromfs_getdents,
  1199. };
  1200. static const struct dfs_filesystem_ops _cromfs_ops =
  1201. {
  1202. .name = "crom",
  1203. .flags = 0,
  1204. .default_fops = &_crom_fops,
  1205. .mount = dfs_cromfs_mount,
  1206. .umount = dfs_cromfs_unmount,
  1207. .readlink = dfs_cromfs_readlink,
  1208. .stat = dfs_cromfs_stat,
  1209. .lookup = dfs_cromfs_lookup,
  1210. .free_vnode = dfs_cromfs_free_vnode
  1211. };
  1212. static struct dfs_filesystem_type _cromfs =
  1213. {
  1214. .fs_ops = &_cromfs_ops,
  1215. };
  1216. int dfs_cromfs_init(void)
  1217. {
  1218. /* register crom file system */
  1219. dfs_register(&_cromfs);
  1220. return 0;
  1221. }
  1222. INIT_COMPONENT_EXPORT(dfs_cromfs_init);