nxffs_pack.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_pack.c
  3. *
  4. * Copyright (C) 2011, 2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <string.h>
  42. #include <errno.h>
  43. #include <assert.h>
  44. #include <crc32.h>
  45. #include <debug.h>
  46. #include <nuttx/kmalloc.h>
  47. #include "nxffs.h"
  48. /****************************************************************************
  49. * Private Types
  50. ****************************************************************************/
  51. /* This structure supports access to one inode data stream */
  52. struct nxffs_packstream_s
  53. {
  54. struct nxffs_entry_s entry; /* Describes the inode header */
  55. off_t fpos; /* Current file position */
  56. off_t blkoffset; /* Offset to the current data block */
  57. uint16_t blklen; /* Size of this block */
  58. uint16_t blkpos; /* Position in block corresponding to fpos */
  59. };
  60. /* The structure supports the overall packing operation */
  61. struct nxffs_pack_s
  62. {
  63. /* These describe the source and destination streams */
  64. struct nxffs_packstream_s src;
  65. struct nxffs_packstream_s dest;
  66. /* These describe the state of the current contents of the (destination)
  67. * volume->pack buffer.
  68. */
  69. FAR uint8_t *iobuffer; /* I/O block start position */
  70. off_t ioblock; /* I/O block number */
  71. off_t block0; /* First I/O block number in the erase block */
  72. uint16_t iooffset; /* I/O block offset */
  73. };
  74. /****************************************************************************
  75. * Private Functions
  76. ****************************************************************************/
  77. /****************************************************************************
  78. * Name: nxffs_getblock
  79. *
  80. * Description:
  81. * Return the I/O block number that includes the provided offset.
  82. *
  83. * Input Parameters:
  84. * volume - Describes the NXFFS volume
  85. * offset - FLASH offset
  86. *
  87. * Returned Value:
  88. * The I/O block number.
  89. *
  90. ****************************************************************************/
  91. static off_t nxffs_getblock(FAR struct nxffs_volume_s *volume, off_t offset)
  92. {
  93. return offset / volume->geo.blocksize;
  94. }
  95. /****************************************************************************
  96. * Name: nxffs_getoffset
  97. *
  98. * Description:
  99. * Given an I/O block number return the block offset corresponding to the
  100. * FLASH offset;
  101. *
  102. * Input Parameters:
  103. * volume - Describes the NXFFS volume
  104. * offset - FLASH offset
  105. *
  106. * Returned Value:
  107. * The I/O block number.
  108. *
  109. ****************************************************************************/
  110. static off_t nxffs_getoffset(FAR struct nxffs_volume_s *volume,
  111. off_t offset, off_t block)
  112. {
  113. return offset - block * volume->geo.blocksize;
  114. }
  115. /****************************************************************************
  116. * Name: nxffs_packtell
  117. *
  118. * Description:
  119. * Report the current destination position in the pack buffer.
  120. *
  121. * Input Parameters:
  122. * volume - Describes the NXFFS volume
  123. * pack - The volume packing state structure.
  124. *
  125. * Returned Value:
  126. * The offset from the beginning of FLASH to the current seek position.
  127. *
  128. ****************************************************************************/
  129. static off_t nxffs_packtell(FAR struct nxffs_volume_s *volume,
  130. FAR struct nxffs_pack_s *pack)
  131. {
  132. return pack->ioblock * volume->geo.blocksize + pack->iooffset;
  133. }
  134. /****************************************************************************
  135. * Name: nxffs_packvalid
  136. *
  137. * Description:
  138. * Check if the current destination block is valid.
  139. *
  140. * Input Parameters:
  141. * pack - The volume packing state structure.
  142. *
  143. * Returned Value:
  144. * None
  145. *
  146. ****************************************************************************/
  147. static inline bool nxffs_packvalid(FAR struct nxffs_pack_s *pack)
  148. {
  149. FAR struct nxffs_block_s *blkhdr;
  150. blkhdr = (FAR struct nxffs_block_s *)pack->iobuffer;
  151. return (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) == 0 &&
  152. blkhdr->state == BLOCK_STATE_GOOD);
  153. }
  154. /****************************************************************************
  155. * Name: nxffs_mediacheck
  156. *
  157. * Description:
  158. * Verify that there is at least one valid block and at least one valid
  159. * inode header on the media. On successful return, the volume packing
  160. * structure is initialized and contains the offset to the first valid
  161. * inode header is returned.
  162. *
  163. * Input Parameters:
  164. * volume - The volume to be packed.
  165. * pack - The volume packing state structure.
  166. *
  167. * Returned Value:
  168. * The offset to the data area on the first valid block. Zero is return
  169. * if there are no valid blocks or if there are no valid inode headers
  170. * after the first valid block.
  171. *
  172. ****************************************************************************/
  173. static inline off_t nxffs_mediacheck(FAR struct nxffs_volume_s *volume,
  174. FAR struct nxffs_pack_s *pack)
  175. {
  176. off_t froffset;
  177. int ret;
  178. /* Initialize the packing structure to all zero */
  179. memset(pack, 0, sizeof(struct nxffs_pack_s));
  180. /* Find the FLASH offset to the first valid block */
  181. volume->ioblock = 0;
  182. ret = nxffs_validblock(volume, &volume->ioblock);
  183. if (ret < 0)
  184. {
  185. /* No valid blocks? Return offset zero. */
  186. return 0;
  187. }
  188. /* The offset to the free location to pack is then just after the block
  189. * header in this block.
  190. */
  191. volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  192. froffset = nxffs_iotell(volume);
  193. /* Get the offset to the first valid inode entry after this free offset */
  194. ret = nxffs_nextentry(volume, froffset, &pack->src.entry);
  195. if (ret < 0)
  196. {
  197. /* No valid entries on the media -- Return offset zero */
  198. return 0;
  199. }
  200. /* Okay.. the start block and first entry have been found */
  201. return froffset;
  202. }
  203. /****************************************************************************
  204. * Name: nxffs_startpos
  205. *
  206. * Description:
  207. * Find the position in FLASH memory where we should begin packing. That
  208. * position is the place where there is a gap between the last and the next
  209. * valid inode. On entry, the volume packing structure should be as it
  210. * was initialized by nxffx_mediacheck. on successful return, the volume
  211. * packing state structure will be updated to begin the packing operation.
  212. *
  213. * Input Parameters:
  214. * volume - The volume to be packed
  215. * pack - The volume packing state structure.
  216. * froffset - On input, this is the location where we should be searching
  217. * for the location to begin packing. On successful return, froffset
  218. * will be set to the offset in FLASH where the first inode should be
  219. * copied to. If -ENOSPC is returned -- meaning that the FLASH is full
  220. * -- then no packing can be performed. In this case, then the free
  221. * flash offset is returned through this location.
  222. *
  223. * Returned Value:
  224. * Zero on success; Otherwise, a negated errno value is returned to
  225. * indicate the nature of the failure. If -ENOSPC is returned then the
  226. * free FLASH offset is also returned.
  227. *
  228. ****************************************************************************/
  229. static inline int nxffs_startpos(FAR struct nxffs_volume_s *volume,
  230. FAR struct nxffs_pack_s *pack,
  231. off_t *froffset)
  232. {
  233. struct nxffs_blkentry_s blkentry;
  234. off_t offset = *froffset;
  235. off_t wasted;
  236. off_t nbytes;
  237. int ret;
  238. /* Loop until we find a gap of unused FLASH large enough to warrant
  239. * compacting.
  240. */
  241. for (; ; )
  242. {
  243. /* Is there wasted space between the offset where the we could have
  244. * valid data and the offset to the beginning of the first valid
  245. * inode header? NOTE: The threshold check is not accurate, there
  246. * may or may not be intervening block headers making the separation
  247. * seem larger than it is.
  248. */
  249. DEBUGASSERT(pack->src.entry.hoffset >= offset);
  250. wasted = pack->src.entry.hoffset - offset;
  251. if (wasted > CONFIG_NXFFS_PACKTHRESHOLD)
  252. {
  253. /* This is where we must begin packing. Describe the destination
  254. * inode header (only non-zero entries need to be initialized).
  255. */
  256. pack->dest.entry.name = pack->src.entry.name;
  257. pack->dest.entry.utc = pack->src.entry.utc;
  258. pack->dest.entry.datlen = pack->src.entry.datlen;
  259. /* The destination entry now "owns" the name string */
  260. pack->src.entry.name = NULL;
  261. /* Return the FLASH offset to the destination inode header */
  262. *froffset = offset;
  263. return OK;
  264. }
  265. /* Free the allocated memory in the entry */
  266. nxffs_freeentry(&pack->src.entry);
  267. /* Update the offset to the first byte at the end of the last data
  268. * block.
  269. */
  270. nbytes = 0;
  271. offset = pack->src.entry.doffset;
  272. while (nbytes < pack->src.entry.datlen)
  273. {
  274. /* Read the next data block header */
  275. ret = nxffs_nextblock(volume, offset, &blkentry);
  276. if (ret < 0)
  277. {
  278. ferr("ERROR: Failed to find next data block: %d\n", -ret);
  279. return ret;
  280. }
  281. /* Get the number of blocks and pointer to where the next
  282. * data block might lie.
  283. */
  284. nbytes += blkentry.datlen;
  285. offset = blkentry.hoffset + SIZEOF_NXFFS_DATA_HDR + blkentry.datlen;
  286. }
  287. /* Make sure there is space at this location for an inode header */
  288. nxffs_ioseek(volume, offset);
  289. if (volume->iooffset + SIZEOF_NXFFS_INODE_HDR > volume->geo.blocksize)
  290. {
  291. /* No.. not enough space here. Find the next valid block */
  292. volume->ioblock++;
  293. ret = nxffs_validblock(volume, &volume->ioblock);
  294. if (ret < 0)
  295. {
  296. /* No valid blocks? Then there is nothing we can do. Return
  297. * the end-of-flash indication.
  298. */
  299. *froffset = volume->froffset;
  300. return -ENOSPC;
  301. }
  302. volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  303. offset = nxffs_iotell(volume);
  304. }
  305. /* Get the offset to the next valid inode entry */
  306. ret = nxffs_nextentry(volume, offset, &pack->src.entry);
  307. if (ret < 0)
  308. {
  309. /* No more valid inode entries. Just return an end-of-flash error
  310. * indication. However, there could be many deleted inodes; set
  311. * volume->froffset to indicate the true free FLASH position.
  312. */
  313. *froffset = offset;
  314. return -ENOSPC;
  315. }
  316. }
  317. /* We won't get here */
  318. return -ENOSYS;
  319. }
  320. /****************************************************************************
  321. * Name: nxffs_srcsetup
  322. *
  323. * Description:
  324. * Given a valid src inode, configure the src data stream.
  325. *
  326. * Input Parameters:
  327. * volume - The volume to be packed
  328. * pack - The volume packing state structure.
  329. * offset - FLASH offset to the data block header (will be zero for zero-
  330. * files.
  331. *
  332. * Returned Value:
  333. * Zero on success; Otherwise, a negated errno value is returned to
  334. * indicate the nature of the failure.
  335. *
  336. ****************************************************************************/
  337. static int nxffs_srcsetup(FAR struct nxffs_volume_s *volume,
  338. FAR struct nxffs_pack_s *pack, off_t offset)
  339. {
  340. /* Start with the first data block */
  341. pack->src.blkoffset = offset;
  342. pack->src.blkpos = 0;
  343. /* Zero-length files have no valid data block offset */
  344. if (offset > 0)
  345. {
  346. /* Seek to the data block header, read and verify the block header */
  347. int ret = nxffs_rdblkhdr(volume, offset, &pack->src.blklen);
  348. if (ret < 0)
  349. {
  350. ferr("ERROR: Failed to verify the data block header: %d\n", -ret);
  351. }
  352. return ret;
  353. }
  354. DEBUGASSERT(pack->src.entry.datlen == 0);
  355. return OK;
  356. }
  357. /****************************************************************************
  358. * Name: nxffs_destsetup
  359. *
  360. * Description:
  361. * Given a valid dest inode, configure the dest data stream.
  362. *
  363. * Input Parameters:
  364. * volume - The volume to be packed
  365. * pack - The volume packing state structure.
  366. *
  367. * Returned Value:
  368. * Zero on success; Otherwise, a negated errno value is returned to
  369. * indicate the nature of the failure.
  370. *
  371. ****************************************************************************/
  372. static int nxffs_destsetup(FAR struct nxffs_volume_s *volume,
  373. FAR struct nxffs_pack_s *pack)
  374. {
  375. size_t mindata;
  376. int namlen;
  377. int ret;
  378. /* The destination can be in one of three of states:
  379. *
  380. * State 1: The inode position was not yet been found. This condition can
  381. * only occur on initial entry into nxffs_packblock() when there we no space
  382. * for the inode header at the end of the previous block. We must now be
  383. * at the beginning of a shiny new I/O block, so we should always have
  384. * space for a new inode header right here.
  385. */
  386. if (pack->dest.entry.hoffset == 0)
  387. {
  388. /* Is there room for an inode structure in this block? */
  389. if (pack->iooffset + SIZEOF_NXFFS_INODE_HDR > volume->geo.blocksize)
  390. {
  391. /* No.. that inode name will not fit in this block. Return an
  392. * indication that we are at the end of the block and try again
  393. * later.
  394. */
  395. return -ENOSPC;
  396. }
  397. /* The inode header will be placed at this position (but not until
  398. * we are finished.
  399. */
  400. pack->dest.entry.hoffset = nxffs_packtell(volume, pack);
  401. /* Make sure that the initialize state of the inode header memory is
  402. * erased. This is important because we may not write to inode header
  403. * until it has already been written to FLASH.
  404. */
  405. memset(&pack->iobuffer[pack->iooffset], CONFIG_NXFFS_ERASEDSTATE,
  406. SIZEOF_NXFFS_INODE_HDR);
  407. /* Then set the new FLASH offset */
  408. pack->iooffset += SIZEOF_NXFFS_INODE_HDR;
  409. }
  410. /* State 2: inode position found, inode header not written, inode name
  411. * position not determined.
  412. */
  413. if (pack->dest.entry.noffset == 0)
  414. {
  415. /* Find the offset to the string memory. Will if fit in this block?
  416. * Note: iooffset has already been incremented to account for the
  417. * size of the inode header.
  418. */
  419. namlen = strlen(pack->dest.entry.name);
  420. if (pack->iooffset + namlen > volume->geo.blocksize)
  421. {
  422. /* No.. that inode name will not fit in this block. Return an
  423. * indication that we are at the end of the block and try again
  424. * later.
  425. */
  426. return -ENOSPC;
  427. }
  428. /* Yes.. Write the inode name to the volume packing buffer now, but do
  429. * not free the name string memory yet; it will be needed later to\
  430. * calculate the header CRC.
  431. */
  432. memcpy(&pack->iobuffer[pack->iooffset], pack->dest.entry.name, namlen);
  433. /* Reserve space for the inode name */
  434. pack->dest.entry.noffset = nxffs_packtell(volume, pack);
  435. pack->iooffset += namlen;
  436. }
  437. /* State 3: Inode header not-written, inode name written. Still need the position
  438. * of the first data block.
  439. *
  440. * Deal with the special case where the source inode is a zero length file
  441. * with no data blocks to be transferred.
  442. */
  443. if (pack->src.entry.doffset > 0)
  444. {
  445. if (pack->dest.entry.doffset == 0)
  446. {
  447. /* Will the data block header plus a minimal amount of data fit in this
  448. * block? (or the whole file if the file is very small).
  449. */
  450. mindata = MIN(NXFFS_MINDATA, pack->dest.entry.datlen);
  451. if (pack->iooffset + SIZEOF_NXFFS_DATA_HDR + mindata >
  452. volume->geo.blocksize)
  453. {
  454. /* No.. return an indication that we are at the end of the block
  455. * and try again later.
  456. */
  457. ret = -ENOSPC;
  458. goto errout;
  459. }
  460. /* Yes.. reserve space for the data block header */
  461. pack->dest.entry.doffset = nxffs_packtell(volume, pack);
  462. pack->iooffset += SIZEOF_NXFFS_DATA_HDR;
  463. /* Initialize the output data stream to start with the first data block */
  464. pack->dest.blkoffset = pack->dest.entry.doffset;
  465. pack->dest.blklen = 0;
  466. pack->dest.blkpos = 0;
  467. }
  468. /* State 4: Starting a new block. Verify that there is space in the current
  469. * block for another (minimal sized) block
  470. */
  471. if (pack->dest.blkoffset == 0)
  472. {
  473. /* Will the data block header plus a minimal amount of data fit in this
  474. * block? (or the whole file if the file is very small).
  475. */
  476. mindata = MIN(NXFFS_MINDATA, pack->dest.entry.datlen);
  477. if (pack->iooffset + SIZEOF_NXFFS_DATA_HDR + mindata >
  478. volume->geo.blocksize)
  479. {
  480. /* No.. return an indication that we are at the end of the block
  481. * and try again later.
  482. */
  483. ret = -ENOSPC;
  484. goto errout;
  485. }
  486. /* Yes.. reserve space for the data block header */
  487. pack->dest.blkoffset = nxffs_packtell(volume, pack);
  488. pack->iooffset += SIZEOF_NXFFS_DATA_HDR;
  489. pack->dest.blklen = 0;
  490. pack->dest.blkpos = 0;
  491. }
  492. }
  493. ret = OK;
  494. errout:
  495. volume->froffset = nxffs_packtell(volume, pack);
  496. return ret;
  497. }
  498. /****************************************************************************
  499. * Name: nxffs_wrinodehdr
  500. *
  501. * Description:
  502. * Write the destination inode header (only) to FLASH. Note that the inode
  503. * name has already been written to FLASH (thus greatly simplifying the
  504. * the complexity of this operation).
  505. *
  506. * Input Parameters:
  507. * volume - The volume to be packed
  508. * pack - The volume packing state structure.
  509. *
  510. * Returned Value:
  511. * Zero on success; Otherwise, a negated errno value is returned to
  512. * indicate the nature of the failure (not used).
  513. *
  514. ****************************************************************************/
  515. static int nxffs_wrinodehdr(FAR struct nxffs_volume_s *volume,
  516. FAR struct nxffs_pack_s *pack)
  517. {
  518. FAR struct nxffs_inode_s *inode;
  519. off_t ioblock;
  520. uint16_t iooffset;
  521. uint32_t crc;
  522. int namlen;
  523. int ret;
  524. /* Get seek positions corresponding to the inode header location */
  525. ioblock = nxffs_getblock(volume, pack->dest.entry.hoffset);
  526. iooffset = nxffs_getoffset(volume, pack->dest.entry.hoffset, ioblock);
  527. /* The inode header is not written until all of the inode data has been
  528. * packed into its new location. As a result, there are two possibilities:
  529. *
  530. * 1. The inode header lies in the current, unwritten erase block,
  531. * 2. The inode header resides in an earlier erase block and has already
  532. * been written to FLASH.
  533. *
  534. * Recall that the inode name has already been written to FLASH. If that
  535. * were not the case, then there would be other complex possibilities.
  536. *
  537. * Case 2: Does the inode header reside in a block before the beginning
  538. * of the current erase block?
  539. */
  540. if (ioblock < pack->block0)
  541. {
  542. /* Case 2: The inode header lies in an earlier erase block that has
  543. * already been written to FLASH. In this case, if we are very
  544. * careful, we can just use the standard routine to write the inode
  545. * header that is called during the normal file close operation:
  546. */
  547. ret = nxffs_wrinode(volume, &pack->dest.entry);
  548. }
  549. else
  550. {
  551. /* Cases 1: Both the inode header and name are in the unwritten cache
  552. * memory.
  553. *
  554. * Initialize the inode header.
  555. */
  556. iooffset += (ioblock - pack->block0) * volume->geo.blocksize;
  557. inode = (FAR struct nxffs_inode_s *)&volume->pack[iooffset];
  558. memcpy(inode->magic, g_inodemagic, NXFFS_MAGICSIZE);
  559. nxffs_wrle32(inode->noffs, pack->dest.entry.noffset);
  560. nxffs_wrle32(inode->doffs, pack->dest.entry.doffset);
  561. nxffs_wrle32(inode->utc, pack->dest.entry.utc);
  562. nxffs_wrle32(inode->crc, 0);
  563. nxffs_wrle32(inode->datlen, pack->dest.entry.datlen);
  564. /* Get the length of the inode name */
  565. namlen = strlen(pack->dest.entry.name);
  566. DEBUGASSERT(namlen < CONFIG_NXFFS_MAXNAMLEN);
  567. inode->state = CONFIG_NXFFS_ERASEDSTATE;
  568. inode->namlen = namlen;
  569. /* Calculate the CRC */
  570. crc = crc32((FAR const uint8_t *)inode, SIZEOF_NXFFS_INODE_HDR);
  571. crc = crc32part((FAR const uint8_t *)pack->dest.entry.name, namlen, crc);
  572. /* Finish the inode header */
  573. inode->state = INODE_STATE_FILE;
  574. nxffs_wrle32(inode->crc, crc);
  575. /* If any open files reference this inode, then update the open file
  576. * state.
  577. */
  578. ret = nxffs_updateinode(volume, &pack->dest.entry);
  579. if (ret < 0)
  580. {
  581. ferr("ERROR: Failed to update inode info: %s\n", -ret);
  582. }
  583. }
  584. /* Reset the dest inode information */
  585. nxffs_freeentry(&pack->dest.entry);
  586. memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
  587. return ret;
  588. }
  589. /****************************************************************************
  590. * Name: nxffs_wrdatthdr
  591. *
  592. * Description:
  593. * Write the destination data block header to FLASH.
  594. *
  595. * Input Parameters:
  596. * volume - The volume to be packed
  597. * pack - The volume packing state structure.
  598. *
  599. * Returned Value:
  600. * Zero on success; Otherwise, a negated errno value is returned to
  601. * indicate the nature of the failure.
  602. *
  603. ****************************************************************************/
  604. static void nxffs_wrdathdr(FAR struct nxffs_volume_s *volume,
  605. FAR struct nxffs_pack_s *pack)
  606. {
  607. FAR struct nxffs_data_s *dathdr;
  608. off_t ioblock;
  609. uint16_t iooffset;
  610. uint32_t crc;
  611. if (pack->dest.blklen > 0)
  612. {
  613. /* Get the offset in the block corresponding to the location of the data
  614. * block header. NOTE: This must lie in the same block as we currently have
  615. * buffered.
  616. */
  617. ioblock = nxffs_getblock(volume, pack->dest.blkoffset);
  618. iooffset = nxffs_getoffset(volume, pack->dest.blkoffset, ioblock);
  619. DEBUGASSERT(pack->dest.blkoffset && ioblock == pack->ioblock);
  620. /* Write the data block header to memory */
  621. dathdr = (FAR struct nxffs_data_s *)&pack->iobuffer[iooffset];
  622. memcpy(dathdr->magic, g_datamagic, NXFFS_MAGICSIZE);
  623. nxffs_wrle32(dathdr->crc, 0);
  624. nxffs_wrle16(dathdr->datlen, pack->dest.blklen);
  625. /* Update the entire data block CRC (including the header) */
  626. crc = crc32(&pack->iobuffer[iooffset],
  627. pack->dest.blklen + SIZEOF_NXFFS_DATA_HDR);
  628. nxffs_wrle32(dathdr->crc, crc);
  629. }
  630. /* Setup state to allocate the next data block */
  631. pack->dest.blkoffset = 0;
  632. pack->dest.blklen = 0;
  633. pack->dest.blkpos = 0;
  634. }
  635. /****************************************************************************
  636. * Name: nxffs_packtransfer
  637. *
  638. * Description:
  639. * Transfer data from the source to the destination buffer.
  640. *
  641. * Input Parameters:
  642. * volume - The volume to be packed
  643. * pack - The volume packing state structure.
  644. *
  645. * Returned Value:
  646. * None.
  647. *
  648. ****************************************************************************/
  649. static void nxffs_packtransfer(FAR struct nxffs_volume_s *volume,
  650. FAR struct nxffs_pack_s *pack)
  651. {
  652. /* Determine how much data is available in the dest pack buffer */
  653. uint16_t destlen = volume->geo.blocksize - pack->iooffset;
  654. /* Dermined how much data is available in the src data block */
  655. uint16_t srclen = pack->src.blklen - pack->src.blkpos;
  656. /* Transfer the smaller of the two amounts data */
  657. uint16_t xfrlen = MIN(srclen, destlen);
  658. if (xfrlen > 0)
  659. {
  660. nxffs_ioseek(volume,
  661. pack->src.blkoffset + SIZEOF_NXFFS_DATA_HDR +
  662. pack->src.blkpos);
  663. memcpy(&pack->iobuffer[pack->iooffset],
  664. &volume->cache[volume->iooffset], xfrlen);
  665. /* Increment counts and offset for this data transfer */
  666. pack->src.fpos += xfrlen; /* Source data offsets */
  667. pack->src.blkpos += xfrlen;
  668. pack->dest.fpos += xfrlen; /* Destination data offsets */
  669. pack->dest.blkpos += xfrlen;
  670. pack->dest.blklen += xfrlen; /* Destination data block size */
  671. pack->iooffset += xfrlen; /* Destination I/O block offset */
  672. volume->iooffset += xfrlen; /* Source I/O block offset */
  673. volume->froffset += xfrlen; /* Free FLASH offset */
  674. }
  675. }
  676. /****************************************************************************
  677. * Name: nxffs_endsrcblock
  678. *
  679. * Description:
  680. * The end of a source data block has been encountered. Locate the next
  681. * source block and setup to continue the transfer.
  682. *
  683. * Input Parameters:
  684. * volume - The volume to be packed
  685. * pack - The volume packing state structure.
  686. *
  687. * Returned Value:
  688. * Zero on success; Otherwise, a negated errno value is returned to
  689. * indicate the nature of the failure.
  690. *
  691. ****************************************************************************/
  692. static int nxffs_endsrcblock(FAR struct nxffs_volume_s *volume,
  693. FAR struct nxffs_pack_s *pack)
  694. {
  695. struct nxffs_blkentry_s blkentry;
  696. off_t offset;
  697. int ret;
  698. /* Yes.. find the next data block in the source input stream. */
  699. offset = pack->src.blkoffset + SIZEOF_NXFFS_DATA_HDR + pack->src.blklen;
  700. ret = nxffs_nextblock(volume, offset, &blkentry);
  701. if (ret < 0)
  702. {
  703. ferr("ERROR: Failed to find next data block: %d\n", -ret);
  704. return ret;
  705. }
  706. /* Set up the source stream */
  707. pack->src.blkoffset = blkentry.hoffset;
  708. pack->src.blklen = blkentry.datlen;
  709. pack->src.blkpos = 0;
  710. return OK;
  711. }
  712. /****************************************************************************
  713. * Name: nxffs_packblock
  714. *
  715. * Description:
  716. * Resume packing from the source stream into the newly identified
  717. * destination block.
  718. *
  719. * Input Parameters:
  720. * volume - The volume to be packed
  721. * pack - The volume packing state structure.
  722. *
  723. * Returned Value:
  724. * Zero on success; Otherwise, a negated errno value is returned to
  725. * indicate the nature of the failure.
  726. *
  727. ****************************************************************************/
  728. static inline int nxffs_packblock(FAR struct nxffs_volume_s *volume,
  729. FAR struct nxffs_pack_s *pack)
  730. {
  731. off_t offset;
  732. int ret;
  733. /* Are we currently processing a block from the source stream? */
  734. if (pack->src.blkoffset == 0)
  735. {
  736. /* No.. setup the source stream */
  737. ret = nxffs_srcsetup(volume, pack, pack->src.entry.doffset);
  738. if (ret < 0)
  739. {
  740. ferr("ERROR: Failed to configure the src stream: %d\n", -ret);
  741. return ret;
  742. }
  743. }
  744. /* We enter here on a new block every time, so we always have to setup
  745. * the dest data stream. There should never be data block allocated at
  746. * this point in time.
  747. */
  748. DEBUGASSERT(pack->dest.blkoffset == 0 && pack->dest.blkpos == 0);
  749. ret = nxffs_destsetup(volume, pack);
  750. if (ret < 0)
  751. {
  752. /* -ENOSPC is a special return value which simply means that all of
  753. * the FLASH has been used up to the end of the current. We need to
  754. * return OK in this case and resume at the next block.
  755. */
  756. if (ret == -ENOSPC)
  757. {
  758. return OK;
  759. }
  760. else
  761. {
  762. ferr("ERROR: Failed to configure the dest stream: %d\n", -ret);
  763. return ret;
  764. }
  765. }
  766. /* Loop, transferring data from the source block to the destination pack
  767. * buffer until either (1) the source stream is exhausted, (2) the destination
  768. * block is full, or (3) an error occurs.
  769. */
  770. for (; ; )
  771. {
  772. /* Transfer data from the source buffer to the destination buffer */
  773. nxffs_packtransfer(volume, pack);
  774. /* Now, either the (1) src block has been fully transferred, (2) all
  775. * of the source data has been transferred, or (3) the destination
  776. * block is full, .. or all three.
  777. *
  778. * Check if all of the bytes in the source inode have been transferred.
  779. */
  780. if (pack->src.fpos >= pack->src.entry.datlen)
  781. {
  782. /* Write the final destination data block header and inode
  783. * headers.
  784. */
  785. nxffs_wrdathdr(volume, pack);
  786. nxffs_wrinodehdr(volume, pack);
  787. /* Find the next valid source inode */
  788. offset = pack->src.blkoffset + pack->src.blklen;
  789. memset(&pack->src, 0, sizeof(struct nxffs_packstream_s));
  790. ret = nxffs_nextentry(volume, offset, &pack->src.entry);
  791. if (ret < 0)
  792. {
  793. /* No more valid inode entries. Just return an end-of-flash error
  794. * indication.
  795. */
  796. return -ENOSPC;
  797. }
  798. /* Setup the new source stream */
  799. ret = nxffs_srcsetup(volume, pack, pack->src.entry.doffset);
  800. if (ret < 0)
  801. {
  802. return ret;
  803. }
  804. /* Setup the dest stream */
  805. memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
  806. pack->dest.entry.name = pack->src.entry.name;
  807. pack->dest.entry.utc = pack->src.entry.utc;
  808. pack->dest.entry.datlen = pack->src.entry.datlen;
  809. pack->src.entry.name = NULL;
  810. /* Is there sufficient space at the end of the I/O block to hold
  811. * the inode header?
  812. */
  813. if (pack->iooffset + SIZEOF_NXFFS_INODE_HDR > volume->geo.blocksize)
  814. {
  815. /* No, just return success... we will handle this condition when
  816. * this function is called on the next I/O block.
  817. */
  818. return OK;
  819. }
  820. /* Configure the destination stream */
  821. ret = nxffs_destsetup(volume, pack);
  822. if (ret < 0)
  823. {
  824. /* -ENOSPC is a special return value which simply means that all of the
  825. * has been used up to the end. We need to return OK in this case and
  826. * resume at the next block.
  827. */
  828. if (ret == -ENOSPC)
  829. {
  830. return OK;
  831. }
  832. else
  833. {
  834. ferr("ERROR: Failed to configure the dest stream: %d\n", -ret);
  835. return ret;
  836. }
  837. }
  838. }
  839. /* Not at the end of the source data stream. Check if we are at the
  840. * end of the current source data block.
  841. */
  842. else if (pack->src.blkpos >= pack->src.blklen)
  843. {
  844. ret = nxffs_endsrcblock(volume, pack);
  845. if (ret < 0)
  846. {
  847. return ret;
  848. }
  849. }
  850. /* Check if the destination block is full */
  851. if (pack->iooffset >= volume->geo.blocksize)
  852. {
  853. /* Yes.. Write the destination data block header and return success */
  854. nxffs_wrdathdr(volume, pack);
  855. return OK;
  856. }
  857. }
  858. return -ENOSYS;
  859. }
  860. /****************************************************************************
  861. * Name: nxffs_setupwriter
  862. *
  863. * Description:
  864. * Writing is performed at the end of the free FLASH region. When we
  865. * finish packing the other inodes, we still need to pack the partially
  866. * written file at the end of FLASH. This function performs the setup
  867. * necessary to perform that packing phase.
  868. *
  869. * Input Parameters:
  870. * volume - The volume to be packed
  871. * pack - The volume packing state structure.
  872. *
  873. * Returned Value:
  874. * If there is an active writer of the volume, its open file instance is
  875. * returned. NULL is returned otherwise.
  876. *
  877. ****************************************************************************/
  878. static FAR struct nxffs_wrfile_s *
  879. nxffs_setupwriter(FAR struct nxffs_volume_s *volume,
  880. FAR struct nxffs_pack_s *pack)
  881. {
  882. FAR struct nxffs_wrfile_s *wrfile;
  883. /* Is there a writer? */
  884. wrfile = nxffs_findwriter(volume);
  885. if (wrfile)
  886. {
  887. /* Yes... It is the activity of this write that probably initiated
  888. * this packing activity. The writer may have failed in one of several
  889. * different stages:
  890. *
  891. * hoffset == 0: The write failed early before even FLASH for the inode
  892. * header was set aside.
  893. * noffset == 0: The write failed after the inode header was set aside,
  894. * but before the inode name was written.
  895. * doffset == 0: The write failed after writing the inode name, bue
  896. * before any data blocks were written to FLASH.
  897. *
  898. * If no FLASH has been set aside for the write, then we don't need to
  899. * do anything here.
  900. */
  901. if (wrfile->ofile.entry.hoffset > 0)
  902. {
  903. /* Initialize for the packing operation. */
  904. memset(&pack->dest, 0, sizeof(struct nxffs_packstream_s));
  905. pack->dest.entry.name = strdup(wrfile->ofile.entry.name);
  906. pack->dest.entry.utc = wrfile->ofile.entry.utc;
  907. pack->dest.entry.datlen = wrfile->ofile.entry.datlen;
  908. memset(&pack->src, 0, sizeof(struct nxffs_packstream_s));
  909. memcpy(&pack->src.entry, &wrfile->ofile.entry,
  910. sizeof(struct nxffs_entry_s));
  911. pack->src.entry.name = NULL;
  912. return wrfile;
  913. }
  914. }
  915. return NULL;
  916. }
  917. /****************************************************************************
  918. * Name: nxffs_packwriter
  919. *
  920. * Description:
  921. * There is a write in progress at the time that the volume is packed.
  922. * This is the normal case because it is the write failures that trigger
  923. * the packing operation to begin with.
  924. *
  925. * Writing is performed at the end of the free FLASH region and this
  926. * implementation is restricted to a single writer. The new inode is not
  927. * written to FLASH until the writer is closed and so will not be
  928. * found by nxffs_packblock().
  929. *
  930. * Input Parameters:
  931. * volume - The volume to be packed
  932. * pack - The volume packing state structure.
  933. *
  934. * Returned Value:
  935. * Zero on success; Otherwise, a negated errno value is returned to
  936. * indicate the nature of the failure.
  937. *
  938. ****************************************************************************/
  939. static inline int nxffs_packwriter(FAR struct nxffs_volume_s *volume,
  940. FAR struct nxffs_pack_s *pack,
  941. FAR struct nxffs_wrfile_s *wrfile)
  942. {
  943. int ret;
  944. /* Are we currently processing a block from the source stream? */
  945. if (pack->src.blkoffset == 0)
  946. {
  947. /* No.. setup the source stream */
  948. ret = nxffs_srcsetup(volume, pack, pack->src.entry.doffset);
  949. if (ret < 0)
  950. {
  951. ferr("ERROR: Failed to configure the src stream: %d\n", -ret);
  952. return ret;
  953. }
  954. }
  955. /* We enter here on a new block every time, so we always have to setup
  956. * the dest data stream. There should never be data block allocated at
  957. * this point in time.
  958. */
  959. DEBUGASSERT(pack->dest.blkoffset == 0 && pack->dest.blkpos == 0);
  960. ret = nxffs_destsetup(volume, pack);
  961. if (ret < 0)
  962. {
  963. /* -ENOSPC is a special return value which simply means that all of the
  964. * has been used up to the end. We need to return OK in this case and
  965. * resume at the next block.
  966. */
  967. if (ret == -ENOSPC)
  968. {
  969. return OK;
  970. }
  971. else
  972. {
  973. ferr("ERROR: Failed to configure the dest stream: %d\n", -ret);
  974. return ret;
  975. }
  976. }
  977. /* Loop, transferring data from the source block to the destination pack
  978. * buffer until either (1) the source stream is exhausted, (2) the destination
  979. * block is full, or (3) an error occurs.
  980. */
  981. for (; ; )
  982. {
  983. /* Transfer data from the source buffer to the destination buffer */
  984. nxffs_packtransfer(volume, pack);
  985. /* Now, either the (1) src block has been fully transferred, (2) all
  986. * of the source data has been transferred, or (3) the destination
  987. * block is full, .. or all three.
  988. *
  989. * Check if all of the bytes in the source inode have been transferred.
  990. */
  991. if (pack->src.fpos >= pack->src.entry.datlen)
  992. {
  993. /* Write the final destination data block header and inode
  994. * headers.
  995. */
  996. nxffs_wrdathdr(volume, pack);
  997. /* Set the new offsets in the open file instance. */
  998. wrfile->ofile.entry.hoffset = pack->dest.entry.hoffset;
  999. wrfile->ofile.entry.noffset = pack->dest.entry.noffset;
  1000. wrfile->ofile.entry.doffset = pack->dest.entry.doffset;
  1001. /* Return an end-of-flash error to indicate that all of the write
  1002. * data has been transferred.
  1003. */
  1004. return -ENOSPC;
  1005. }
  1006. /* Not at the end of the source data stream. Check if we are at the
  1007. * end of the current source data block.
  1008. */
  1009. else if (pack->src.blkpos >= pack->src.blklen)
  1010. {
  1011. ret = nxffs_endsrcblock(volume, pack);
  1012. if (ret < 0)
  1013. {
  1014. return ret;
  1015. }
  1016. }
  1017. /* Check if the destination block is full */
  1018. if (pack->iooffset >= volume->geo.blocksize)
  1019. {
  1020. /* Yes.. Write the destination data block header and return success */
  1021. nxffs_wrdathdr(volume, pack);
  1022. return OK;
  1023. }
  1024. }
  1025. return -ENOSYS;
  1026. }
  1027. /****************************************************************************
  1028. * Public Functions
  1029. ****************************************************************************/
  1030. /****************************************************************************
  1031. * Name: nxffs_pack
  1032. *
  1033. * Description:
  1034. * Pack and re-write the filesystem in order to free up memory at the end
  1035. * of FLASH.
  1036. *
  1037. * Input Parameters:
  1038. * volume - The volume to be packed.
  1039. *
  1040. * Returned Value:
  1041. * Zero on success; Otherwise, a negated errno value is returned to
  1042. * indicate the nature of the failure.
  1043. *
  1044. ****************************************************************************/
  1045. int nxffs_pack(FAR struct nxffs_volume_s *volume)
  1046. {
  1047. struct nxffs_pack_s pack;
  1048. FAR struct nxffs_wrfile_s *wrfile;
  1049. off_t iooffset;
  1050. off_t eblock;
  1051. off_t block;
  1052. bool packed;
  1053. int i;
  1054. int ret = OK;
  1055. /* Get the offset to the first valid inode entry */
  1056. wrfile = NULL;
  1057. packed = false;
  1058. iooffset = nxffs_mediacheck(volume, &pack);
  1059. if (iooffset == 0)
  1060. {
  1061. /* Offset zero is only returned if no valid blocks were found on the
  1062. * FLASH media or if there are no valid inode entries on the FLASH after
  1063. * the first valid block. There are two possibilities: (1) there
  1064. * really is nothing on the FLASH, or (2) there is a file being written
  1065. * to the FLASH now.
  1066. */
  1067. /* Is there a writer? */
  1068. wrfile = nxffs_setupwriter(volume, &pack);
  1069. if (wrfile)
  1070. {
  1071. /* If there is a write, just set ioffset to the offset of data in
  1072. * first block. Setting 'packed' to true will suppress normal inode
  1073. * packing operation. Then we can start compacting the FLASH.
  1074. */
  1075. iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  1076. packed = true;
  1077. goto start_pack;
  1078. }
  1079. else
  1080. {
  1081. /* No, there is no write in progress. We just have an empty flash
  1082. * full of deleted files. In this case, the media needs to be re-
  1083. * formatted.
  1084. */
  1085. ret = nxffs_reformat(volume);
  1086. if (ret == OK)
  1087. {
  1088. /* The free flash offset will be in the first valid block of
  1089. * the FLASH.
  1090. */
  1091. block = 0;
  1092. ret = nxffs_validblock(volume, &block);
  1093. if (ret == OK)
  1094. {
  1095. /* Set to the offset past the block header in the first
  1096. * valid block
  1097. */
  1098. volume->froffset =
  1099. block * volume->geo.blocksize + SIZEOF_NXFFS_BLOCK_HDR;
  1100. }
  1101. }
  1102. return ret;
  1103. }
  1104. }
  1105. /* There is a valid format and valid inodes on the media.. setup up to
  1106. * begin the packing operation.
  1107. */
  1108. ret = nxffs_startpos(volume, &pack, &iooffset);
  1109. if (ret < 0)
  1110. {
  1111. /* This is a normal situation if the volume is full */
  1112. if (ret == -ENOSPC)
  1113. {
  1114. /* In the case where the volume is full, nxffs_startpos() will
  1115. * recalculate the free FLASH offset and store it in iooffset. There
  1116. * may be deleted files at the end of FLASH. In this case, we don't
  1117. * have to pack any files, we simply have to erase FLASH at the end.
  1118. * But don't do this unless there is some particularly big FLASH
  1119. * savings (otherwise, we risk wearing out these final blocks).
  1120. */
  1121. if (iooffset + CONFIG_NXFFS_TAILTHRESHOLD < volume->froffset)
  1122. {
  1123. /* Setting 'packed' to true will suppress normal inode packing
  1124. * operation.
  1125. */
  1126. packed = true;
  1127. /* Writing is performed at the end of the free FLASH region.
  1128. * If we are not packing files, we could still need to pack
  1129. * the partially written file at the end of FLASH.
  1130. */
  1131. wrfile = nxffs_setupwriter(volume, &pack);
  1132. }
  1133. /* Otherwise return OK.. meaning that there is nothing more we can
  1134. * do to recover FLASH space.
  1135. */
  1136. else
  1137. {
  1138. return OK;
  1139. }
  1140. }
  1141. else
  1142. {
  1143. ferr("ERROR: Failed to find a packing position: %d\n", -ret);
  1144. return ret;
  1145. }
  1146. }
  1147. /* Otherwise, begin pack at this src/dest block combination. Initialize
  1148. * ioblock and iooffset with the position of the first inode header. In
  1149. * this case, the FLASH offset to the first inode header is return in
  1150. * iooffset.
  1151. */
  1152. start_pack:
  1153. pack.ioblock = nxffs_getblock(volume, iooffset);
  1154. pack.iooffset = nxffs_getoffset(volume, iooffset, pack.ioblock);
  1155. volume->froffset = iooffset;
  1156. /* Then pack all erase blocks starting with the erase block that contains
  1157. * the ioblock and through the final erase block on the FLASH.
  1158. */
  1159. for (eblock = pack.ioblock / volume->blkper;
  1160. eblock < volume->geo.neraseblocks;
  1161. eblock++)
  1162. {
  1163. /* Get the starting block number of the erase block */
  1164. pack.block0 = eblock * volume->blkper;
  1165. #ifndef CONFIG_NXFFS_NAND
  1166. /* Read the erase block into the pack buffer. We need to do this even
  1167. * if we are overwriting the entire block so that we skip over
  1168. * previously marked bad blocks.
  1169. */
  1170. ret = MTD_BREAD(volume->mtd, pack.block0, volume->blkper, volume->pack);
  1171. if (ret < 0)
  1172. {
  1173. ferr("ERROR: Failed to read erase block %d: %d\n", eblock, -ret);
  1174. goto errout_with_pack;
  1175. }
  1176. #else
  1177. /* Read the entire erase block into the pack buffer, one-block-at-a-
  1178. * time. We need to do this even if we are overwriting the entire
  1179. * block so that (1) we skip over previously marked bad blocks, and
  1180. * (2) we can handle individual block read failures.
  1181. *
  1182. * For most FLASH, a read failure indicates a fatal hardware failure.
  1183. * But for NAND FLASH, the read failure probably indicates a block
  1184. * with uncorrectable bit errors.
  1185. */
  1186. /* Read each I/O block */
  1187. for (i = 0, block = pack.block0, pack.iobuffer = volume->pack;
  1188. i < volume->blkper;
  1189. i++, block++, pack.iobuffer += volume->geo.blocksize)
  1190. {
  1191. /* Read the next block in the erase block */
  1192. ret = MTD_BREAD(volume->mtd, block, 1, pack.iobuffer);
  1193. if (ret < 0)
  1194. {
  1195. /* Force a the block to be an NXFFS bad block */
  1196. ferr("ERROR: Failed to read block %d: %d\n", block, ret);
  1197. nxffs_blkinit(volume, pack.iobuffer, BLOCK_STATE_BAD);
  1198. }
  1199. }
  1200. #endif
  1201. /* Now pack each I/O block */
  1202. for (i = 0, block = pack.block0, pack.iobuffer = volume->pack;
  1203. i < volume->blkper;
  1204. i++, block++, pack.iobuffer += volume->geo.blocksize)
  1205. {
  1206. /* The first time here, the ioblock may point to an offset into
  1207. * the erase block. We just need to skip over those cases.
  1208. */
  1209. if (block >= pack.ioblock)
  1210. {
  1211. /* Set the I/O position. Note on the first time we get
  1212. * pack.iooffset will hold the offset in the first I/O block
  1213. * to the first inode header. After that, it will always
  1214. * refer to the first byte after the block header.
  1215. */
  1216. pack.ioblock = block;
  1217. /* If this is not a valid block or if we have already
  1218. * finished packing the valid inode entries, then just fall
  1219. * through, reset the FLASH memory to the erase state, and
  1220. * write the reset values to FLASH. (The first block that
  1221. * we want to process will always be valid -- we have
  1222. * already verified that).
  1223. */
  1224. if (nxffs_packvalid(&pack))
  1225. {
  1226. /* Have we finished packing inodes? */
  1227. if (!packed)
  1228. {
  1229. DEBUGASSERT(wrfile == NULL);
  1230. /* Pack inode data into this block */
  1231. ret = nxffs_packblock(volume, &pack);
  1232. if (ret < 0)
  1233. {
  1234. /* The error -ENOSPC is a special value that simply
  1235. * means that there is nothing further to be packed.
  1236. */
  1237. if (ret == -ENOSPC)
  1238. {
  1239. packed = true;
  1240. /* Writing is performed at the end of the free
  1241. * FLASH region and this implementation is restricted
  1242. * to a single writer. The new inode is not
  1243. * written to FLASH until the writer is closed
  1244. * and so will not be found by nxffs_packblock().
  1245. */
  1246. wrfile = nxffs_setupwriter(volume, &pack);
  1247. }
  1248. else
  1249. {
  1250. /* Otherwise, something really bad happened */
  1251. ferr("ERROR: Failed to pack into block %d: %d\n",
  1252. block, ret);
  1253. goto errout_with_pack;
  1254. }
  1255. }
  1256. }
  1257. /* If all of the "normal" inodes have been packed, then check if
  1258. * we need to pack the current, in-progress write operation.
  1259. */
  1260. if (wrfile)
  1261. {
  1262. DEBUGASSERT(packed == true);
  1263. /* Pack write data into this block */
  1264. ret = nxffs_packwriter(volume, &pack, wrfile);
  1265. if (ret < 0)
  1266. {
  1267. /* The error -ENOSPC is a special value that simply
  1268. * means that there is nothing further to be packed.
  1269. */
  1270. if (ret == -ENOSPC)
  1271. {
  1272. wrfile = NULL;
  1273. }
  1274. else
  1275. {
  1276. /* Otherwise, something really bad happened */
  1277. ferr("ERROR: Failed to pack into block %d: %d\n",
  1278. block, ret);
  1279. goto errout_with_pack;
  1280. }
  1281. }
  1282. }
  1283. }
  1284. /* Set any unused portion at the end of the block to the
  1285. * erased state.
  1286. */
  1287. if (pack.iooffset < volume->geo.blocksize)
  1288. {
  1289. memset(&pack.iobuffer[pack.iooffset],
  1290. CONFIG_NXFFS_ERASEDSTATE,
  1291. volume->geo.blocksize - pack.iooffset);
  1292. }
  1293. /* Next time through the loop, pack.iooffset will point to the
  1294. * first byte after the block header.
  1295. */
  1296. pack.iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  1297. }
  1298. }
  1299. /* We now have an in-memory image of how we want this erase block to
  1300. * appear. Now it is safe to erase the block.
  1301. */
  1302. ret = MTD_ERASE(volume->mtd, eblock, 1);
  1303. if (ret < 0)
  1304. {
  1305. ferr("ERROR: Failed to erase block %d [%d]: %d\n",
  1306. eblock, pack.block0, -ret);
  1307. goto errout_with_pack;
  1308. }
  1309. /* Write the packed I/O block to FLASH */
  1310. ret = MTD_BWRITE(volume->mtd, pack.block0, volume->blkper, volume->pack);
  1311. if (ret < 0)
  1312. {
  1313. ferr("ERROR: Failed to write erase block %d [%d]: %d\n",
  1314. eblock, pack.block0, -ret);
  1315. goto errout_with_pack;
  1316. }
  1317. }
  1318. errout_with_pack:
  1319. nxffs_freeentry(&pack.src.entry);
  1320. nxffs_freeentry(&pack.dest.entry);
  1321. return ret;
  1322. }