nxffs_pack.c 51 KB

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