nxffs_write.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_write.c
  3. *
  4. * Copyright (C) 2011, 2013, 2017-2018 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 <fcntl.h>
  43. #include <crc32.h>
  44. #include <assert.h>
  45. #include <errno.h>
  46. #include <debug.h>
  47. #include <nuttx/semaphore.h>
  48. #include <nuttx/fs/fs.h>
  49. #include <nuttx/mtd/mtd.h>
  50. #include "nxffs.h"
  51. /****************************************************************************
  52. * Private Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: nxffs_hdrpos
  56. *
  57. * Description:
  58. * Find a valid location for the data block header. A valid location will
  59. * have these properties:
  60. *
  61. * 1. It will lie in the free flash region.
  62. * 2. It will have enough contiguous memory to hold the entire header
  63. * PLUS some meaningful amount of data (NXFFS_MINDATA).
  64. * 3. The memory at this location will be fully erased.
  65. *
  66. * This function will only perform the checks of 1) and 2).
  67. *
  68. * Input Parameters:
  69. * volume - Describes the NXFFS volume
  70. * wrfile - Contains the current guess for the header position. On
  71. * successful return, this field will hold the selected header
  72. * position.
  73. * size - The minimum size of the current write.
  74. *
  75. * Returned Value:
  76. * Zero is returned on success. Otherwise, a negated errno value is
  77. * returned indicating the nature of the failure. Of special interest
  78. * the return error of -ENOSPC which means that the FLASH volume is
  79. * full and should be repacked.
  80. *
  81. * On successful return the following are also valid:
  82. *
  83. * wrfile->doffset - Flash offset to candidate data block header position
  84. * volume->ioblock - Read/write block number of the block containing the
  85. * header position
  86. * volume->iooffset - The offset in the block to the candidate header
  87. * position.
  88. * volume->froffset - Updated offset to the first free FLASH block.
  89. *
  90. ****************************************************************************/
  91. static inline int nxffs_hdrpos(FAR struct nxffs_volume_s *volume,
  92. FAR struct nxffs_wrfile_s *wrfile,
  93. size_t size)
  94. {
  95. int ret;
  96. /* Reserve memory for the object */
  97. ret = nxffs_wrreserve(volume, SIZEOF_NXFFS_DATA_HDR + size);
  98. if (ret == OK)
  99. {
  100. /* Save the offset to the FLASH region reserved for the data block
  101. * header
  102. */
  103. wrfile->doffset = nxffs_iotell(volume);
  104. }
  105. return ret;
  106. }
  107. /****************************************************************************
  108. * Name: nxffs_hdrerased
  109. *
  110. * Description:
  111. * Find a valid location for the data block header. A valid location will
  112. * have these properties:
  113. *
  114. * 1. It will lie in the free flash region.
  115. * 2. It will have enough contiguous memory to hold the entire header
  116. * PLUS some meaningful amount of data (NXFFS_MINDATA).
  117. * 3. The memory at this location will be fully erased.
  118. *
  119. * This function will only perform the check 3). On entry it assumes:
  120. *
  121. * volume->ioblock - Read/write block number of the block containing the
  122. * header position
  123. * volume->iooffset - The offset in the block to the candidate header
  124. * position.
  125. *
  126. * Input Parameters:
  127. * volume - Describes the NXFFS volume
  128. * wrfile - Contains the current guess for the header position. On
  129. * successful return, this field will hold the selected header
  130. * position.
  131. * size - The minimum size of the current write.
  132. *
  133. * Returned Value:
  134. * Zero is returned on success. Otherwise, a negated errno value is
  135. * returned indicating the nature of the failure. Of special interest
  136. * the return error of -ENOSPC which means that the FLASH volume is
  137. * full and should be repacked.
  138. *
  139. * On successful return the following are also valid:
  140. *
  141. * wrfile->doffset - Flash offset to candidate data block header position
  142. * volume->ioblock - Read/write block number of the block containing the
  143. * header position
  144. * volume->iooffset - The offset in the block to the candidate header
  145. * position.
  146. * volume->froffset - Updated offset to the first free FLASH block.
  147. *
  148. ****************************************************************************/
  149. static inline int nxffs_hdrerased(FAR struct nxffs_volume_s *volume,
  150. FAR struct nxffs_wrfile_s *wrfile,
  151. size_t size)
  152. {
  153. int ret;
  154. /* Find a valid location to save the inode header */
  155. ret = nxffs_wrverify(volume, SIZEOF_NXFFS_DATA_HDR + size);
  156. if (ret == OK)
  157. {
  158. /* This is where we will put the data block header */
  159. wrfile->doffset = nxffs_iotell(volume);
  160. }
  161. return ret;
  162. }
  163. /****************************************************************************
  164. * Name: nxffs_wralloc
  165. *
  166. * Description:
  167. * Allocate FLASH memory for the data block.
  168. *
  169. * Input Parameters:
  170. * volume - Describes the NXFFS volume
  171. * wrfile - Describes the open file to be written.
  172. * size - Size of the current write operation.
  173. *
  174. * Returned Value:
  175. * Zero is returned on success. Otherwise, a negated errno value is
  176. * returned indicating the nature of the failure.
  177. *
  178. ****************************************************************************/
  179. static inline int nxffs_wralloc(FAR struct nxffs_volume_s *volume,
  180. FAR struct nxffs_wrfile_s *wrfile,
  181. size_t size)
  182. {
  183. bool packed;
  184. int ret;
  185. /* Allocate FLASH memory for the data block.
  186. *
  187. * Loop until the data block header is configured or until a failure
  188. * occurs. Note that nothing is written to FLASH. The data block header
  189. * is not written until either (1) the file is closed, or (2) the data
  190. * region is fully populated.
  191. */
  192. packed = false;
  193. for (; ; )
  194. {
  195. size_t mindata = MIN(NXFFS_MINDATA, size);
  196. /* File a valid location to position the data block. Start with
  197. * the first byte in the free FLASH region.
  198. */
  199. ret = nxffs_hdrpos(volume, wrfile, mindata);
  200. if (ret == OK)
  201. {
  202. /* Find a region of memory in the block that is fully erased */
  203. ret = nxffs_hdrerased(volume, wrfile, mindata);
  204. if (ret == OK)
  205. {
  206. /* Valid memory for the data block was found. Return success. */
  207. return OK;
  208. }
  209. }
  210. /* If no valid memory is found searching to the end of the volume,
  211. * then -ENOSPC will be returned. Other errors are not handled.
  212. */
  213. if (ret != -ENOSPC || packed)
  214. {
  215. ferr("ERROR: Failed to find inode header memory: %d\n", -ret);
  216. return -ENOSPC;
  217. }
  218. /* -ENOSPC is a special case.. It means that the volume is full.
  219. * Try to pack the volume in order to free up some space.
  220. */
  221. ret = nxffs_pack(volume);
  222. if (ret < 0)
  223. {
  224. ferr("ERROR: Failed to pack the volume: %d\n", -ret);
  225. return ret;
  226. }
  227. /* After packing the volume, froffset will be updated to point to the
  228. * new free flash region. Try again.
  229. */
  230. nxffs_ioseek(volume, volume->froffset);
  231. packed = true;
  232. }
  233. /* Can't get here */
  234. return OK;
  235. }
  236. /****************************************************************************
  237. * Name: nxffs_reverify
  238. *
  239. * Description:
  240. * Verify that the partial flash data block in the volume cache is valid.
  241. * On entry, this function assumes:
  242. *
  243. * volume->ioblock - Read/write block number of the block containing the
  244. * data block.
  245. * volume->iooffset - The offset in the block to the data block.
  246. *
  247. * Input Parameters:
  248. * volume - Describes the NXFFS volume
  249. * wrfile - Describes the open file to be written.
  250. *
  251. * Returned Value:
  252. * Zero is returned on success. Otherwise, a negated errno value is
  253. * returned indicating the nature of the failure.
  254. *
  255. ****************************************************************************/
  256. static inline int nxffs_reverify(FAR struct nxffs_volume_s *volume,
  257. FAR struct nxffs_wrfile_s *wrfile)
  258. {
  259. uint32_t crc;
  260. off_t offset;
  261. if (wrfile->datlen > 0)
  262. {
  263. /* Get the offset to the start of the data */
  264. offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
  265. DEBUGASSERT(offset + wrfile->datlen <= volume->geo.blocksize);
  266. /* Calculate the CRC of the partial data block */
  267. crc = crc32(&volume->cache[offset], wrfile->datlen);
  268. /* It must match the previously calculated CRC value */
  269. if (crc != wrfile->crc)
  270. {
  271. ferr("ERROR: CRC failure\n");
  272. return -EIO;
  273. }
  274. }
  275. return OK;
  276. }
  277. /****************************************************************************
  278. * Name: nxffs_wrappend
  279. *
  280. * Description:
  281. * Append FLASH data to the data block.
  282. *
  283. * Input Parameters:
  284. * volume - Describes the NXFFS volume
  285. * wrfile - Describes the open file to be written.
  286. * buffer - Address of buffer of data to be written.
  287. * buflen - The number of bytes remaining to be written
  288. *
  289. * Returned Value:
  290. * The number of bytes written is returned on success. Otherwise, a
  291. * negated errno value is returned indicating the nature of the failure.
  292. *
  293. ****************************************************************************/
  294. static inline ssize_t nxffs_wrappend(FAR struct nxffs_volume_s *volume,
  295. FAR struct nxffs_wrfile_s *wrfile,
  296. FAR const char *buffer, size_t buflen)
  297. {
  298. ssize_t maxsize;
  299. size_t nbytestowrite;
  300. ssize_t nbytesleft;
  301. off_t offset;
  302. int ret;
  303. /* Get the offset to the start of unwritten data */
  304. offset = volume->iooffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR;
  305. /* Determine that maximum amount of data that can be written to this
  306. * block.
  307. */
  308. maxsize = volume->geo.blocksize - offset;
  309. DEBUGASSERT(maxsize > 0);
  310. /* But don't try to write over any unerased bytes */
  311. maxsize = nxffs_erased(&volume->cache[offset], maxsize);
  312. /* Write as many bytes as we can into the data buffer */
  313. nbytestowrite = MIN(maxsize, buflen);
  314. nbytesleft = maxsize - nbytestowrite;
  315. if (nbytestowrite > 0)
  316. {
  317. /* Copy the data into the volume write cache */
  318. memcpy(&volume->cache[offset], buffer, nbytestowrite);
  319. /* Increment the number of bytes written to the data block */
  320. wrfile->datlen += nbytestowrite;
  321. /* Re-calculate the CRC */
  322. offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
  323. wrfile->crc = crc32(&volume->cache[offset], wrfile->datlen);
  324. /* And write the partial write block to FLASH -- unless the data
  325. * block is full. In that case, the block will be written below.
  326. */
  327. if (nbytesleft > 0)
  328. {
  329. ret = nxffs_wrcache(volume);
  330. if (ret < 0)
  331. {
  332. ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
  333. return ret;
  334. }
  335. }
  336. }
  337. /* Check if the data block is now full */
  338. if (nbytesleft <= 0)
  339. {
  340. /* The data block is full, write the block to FLASH */
  341. ret = nxffs_wrblkhdr(volume, wrfile);
  342. if (ret < 0)
  343. {
  344. ferr("ERROR: nxffs_wrblkdhr failed: %d\n", -ret);
  345. return ret;
  346. }
  347. }
  348. /* Return the number of bytes written to FLASH this time */
  349. return nbytestowrite;
  350. }
  351. /****************************************************************************
  352. * Name: nxffs_zappend
  353. *
  354. * Description:
  355. * Zero-extend FLASH data to the data block.
  356. *
  357. * Input Parameters:
  358. * volume - Describes the NXFFS volume
  359. * wrfile - Describes the open file to be written.
  360. * nzeros - The number of bytes of zeroed data to be written
  361. *
  362. * Returned Value:
  363. * The number of zero bytes written is returned on success. Otherwise, a
  364. * negated errno value is returned indicating the nature of the failure.
  365. *
  366. ****************************************************************************/
  367. #ifdef __NO_TRUNCATE_SUPPORT__
  368. static inline ssize_t nxffs_zappend(FAR struct nxffs_volume_s *volume,
  369. FAR struct nxffs_wrfile_s *wrfile,
  370. off_t nzeros)
  371. {
  372. ssize_t maxsize;
  373. size_t nbytestoclear;
  374. ssize_t nbytesleft;
  375. off_t offset;
  376. int ret;
  377. /* Get the offset to the start of unwritten data */
  378. offset = volume->iooffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR;
  379. /* Determine that maximum amount of data that can be written to this
  380. * block.
  381. */
  382. maxsize = volume->geo.blocksize - offset;
  383. DEBUGASSERT(maxsize > 0);
  384. /* Write as many bytes as we can into the data buffer */
  385. nbytestoclear = MIN(maxsize, nzeros);
  386. nbytesleft = maxsize - nbytestoclear;
  387. if (nbytestoclear > 0)
  388. {
  389. /* Zero the data into the volume write cache */
  390. memset(&volume->cache[offset], 0, nbytestoclear);
  391. /* Increment the number of bytes written to the data block */
  392. wrfile->datlen += nbytestoclear;
  393. /* Re-calculate the CRC */
  394. offset = volume->iooffset + SIZEOF_NXFFS_DATA_HDR;
  395. wrfile->crc = crc32(&volume->cache[offset], wrfile->datlen);
  396. /* And write the partial write block to FLASH -- unless the data
  397. * block is full. In that case, the block will be written below.
  398. */
  399. if (nbytesleft > 0)
  400. {
  401. ret = nxffs_wrcache(volume);
  402. if (ret < 0)
  403. {
  404. ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
  405. return ret;
  406. }
  407. }
  408. }
  409. /* Check if the data block is now full */
  410. if (nbytesleft <= 0)
  411. {
  412. /* The data block is full, write the block to FLASH */
  413. ret = nxffs_wrblkhdr(volume, wrfile);
  414. if (ret < 0)
  415. {
  416. ferr("ERROR: nxffs_wrblkdhr failed: %d\n", -ret);
  417. return ret;
  418. }
  419. }
  420. return nbytestoclear;
  421. }
  422. #endif
  423. /****************************************************************************
  424. * Public Functions
  425. ****************************************************************************/
  426. /****************************************************************************
  427. * Name: nxffs_write
  428. *
  429. * Description:
  430. * This is an implementation of the NuttX standard file system write
  431. * method.
  432. *
  433. ****************************************************************************/
  434. ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
  435. {
  436. FAR struct nxffs_volume_s *volume;
  437. FAR struct nxffs_wrfile_s *wrfile;
  438. ssize_t remaining;
  439. ssize_t nwritten;
  440. ssize_t total;
  441. int ret;
  442. finfo("Write %d bytes to offset %d\n", buflen, filep->f_pos);
  443. /* Sanity checks */
  444. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  445. /* Recover the open file state from the struct file instance */
  446. wrfile = (FAR struct nxffs_wrfile_s *)filep->f_priv;
  447. /* Recover the volume state from the open file */
  448. volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private;
  449. DEBUGASSERT(volume != NULL);
  450. /* Get exclusive access to the volume. Note that the volume exclsem
  451. * protects the open file list.
  452. */
  453. ret = nxsem_wait(&volume->exclsem);
  454. if (ret < 0)
  455. {
  456. ferr("ERROR: nxsem_wait failed: %d\n", ret);
  457. goto errout;
  458. }
  459. /* Check if the file was opened with write access */
  460. if ((wrfile->ofile.oflags & O_WROK) == 0)
  461. {
  462. ferr("ERROR: File not open for write access\n");
  463. ret = -EACCES;
  464. goto errout_with_semaphore;
  465. }
  466. /* Loop until we successfully appended all of the data to the file (or an
  467. * error occurs)
  468. */
  469. for (total = 0; total < buflen; )
  470. {
  471. remaining = buflen - total;
  472. /* Have we already allocated the data block? */
  473. if (wrfile->doffset == 0)
  474. {
  475. /* No, allocate the data block now, re-packing if necessary. */
  476. wrfile->datlen = 0;
  477. ret = nxffs_wralloc(volume, wrfile, remaining);
  478. if (ret < 0)
  479. {
  480. ferr("ERROR: Failed to allocate a data block: %d\n", -ret);
  481. goto errout_with_semaphore;
  482. }
  483. }
  484. /* Seek to the FLASH block containing the data block */
  485. nxffs_ioseek(volume, wrfile->doffset);
  486. /* Verify that the FLASH data that was previously written is still intact */
  487. ret = nxffs_reverify(volume, wrfile);
  488. if (ret < 0)
  489. {
  490. ferr("ERROR: Failed to verify FLASH data block: %d\n", -ret);
  491. goto errout_with_semaphore;
  492. }
  493. /* Append the data to the end of the data block and write the updated
  494. * block to flash.
  495. */
  496. nwritten = nxffs_wrappend(volume, wrfile, &buffer[total], remaining);
  497. if (nwritten < 0)
  498. {
  499. ferr("ERROR: Failed to append to FLASH to a data block: %d\n", -ret);
  500. goto errout_with_semaphore;
  501. }
  502. /* Decrement the number of bytes remaining to be written */
  503. total += nwritten;
  504. }
  505. /* Success.. return the number of bytes written */
  506. ret = total;
  507. filep->f_pos = wrfile->datlen;
  508. errout_with_semaphore:
  509. nxsem_post(&volume->exclsem);
  510. errout:
  511. return ret;
  512. }
  513. /****************************************************************************
  514. * Name: nxffs_wrextend
  515. *
  516. * Description:
  517. * Zero-extend a file.
  518. *
  519. * Input Parameters:
  520. * volume - Describes the NXFFS volume
  521. * entry - Describes the new inode entry
  522. * length - The new, extended length of the file
  523. *
  524. * Assumptions:
  525. * The caller holds the NXFFS semaphore.
  526. * The caller has verified that the file is writable.
  527. *
  528. ****************************************************************************/
  529. #ifdef __NO_TRUNCATE_SUPPORT__
  530. int nxffs_wrextend(FAR struct nxffs_volume_s *volume,
  531. FAR struct nxffs_wrfile_s *wrfile, off_t length)
  532. {
  533. ssize_t remaining;
  534. ssize_t nwritten;
  535. off_t oldsize;
  536. int ret;
  537. finfo("Extend file to %ld bytes to offset %d\n", (long)length);
  538. DEBUGASSERT(volume != NULL && wrfile != NULL);
  539. oldsize = wrfile->ofile.entry.datlen;
  540. DEBUGASSERT(length > oldsize);
  541. /* Loop until we successfully appended all of the data to the file (or an
  542. * error occurs)
  543. */
  544. remaining = length - oldsize;
  545. while (remaining > 0)
  546. {
  547. /* Have we already allocated the data block? */
  548. if (wrfile->doffset == 0)
  549. {
  550. /* No, allocate the data block now, re-packing if necessary. */
  551. wrfile->datlen = 0;
  552. ret = nxffs_wralloc(volume, wrfile, remaining);
  553. if (ret < 0)
  554. {
  555. ferr("ERROR: Failed to allocate a data block: %d\n", -ret);
  556. return ret;
  557. }
  558. }
  559. /* Seek to the FLASH block containing the data block */
  560. nxffs_ioseek(volume, wrfile->doffset);
  561. /* Verify that the FLASH data that was previously written is still intact */
  562. ret = nxffs_reverify(volume, wrfile);
  563. if (ret < 0)
  564. {
  565. ferr("ERROR: Failed to verify FLASH data block: %d\n", -ret);
  566. return ret;
  567. }
  568. /* Append the data to the end of the data block and write the updated
  569. * block to flash.
  570. */
  571. nwritten = nxffs_zappend(volume, wrfile, remaining);
  572. if (nwritten < 0)
  573. {
  574. ferr("ERROR: Failed to zero extend FLASH data block: %d\n", -ret);
  575. return (int)nwritten;
  576. }
  577. /* Decrement the number of bytes remaining to be written */
  578. remaining -= nwritten;
  579. }
  580. return OK;
  581. }
  582. #endif
  583. /****************************************************************************
  584. * Name: nxffs_wrreserve
  585. *
  586. * Description:
  587. * Find a valid location for a file system object of 'size'. A valid
  588. * location will have these properties:
  589. *
  590. * 1. It will lie in the free flash region.
  591. * 2. It will have enough contiguous memory to hold the entire object
  592. * 3. The memory at this location will be fully erased.
  593. *
  594. * This function will only perform the checks of 1) and 2). The
  595. * end-of-filesystem offset, froffset, is update past this memory which,
  596. * in effect, reserves the memory.
  597. *
  598. * Input Parameters:
  599. * volume - Describes the NXFFS volume
  600. * size - The size of the object to be reserved.
  601. *
  602. * Returned Value:
  603. * Zero is returned on success. Otherwise, a negated errno value is
  604. * returned indicating the nature of the failure. Of special interest
  605. * the return error of -ENOSPC which means that the FLASH volume is
  606. * full and should be repacked.
  607. *
  608. * On successful return the following are also valid:
  609. *
  610. * volume->ioblock - Read/write block number of the block containing the
  611. * candidate oject position
  612. * volume->iooffset - The offset in the block to the candidate object
  613. * position.
  614. * volume->froffset - Updated offset to the first free FLASH block after
  615. * the reserved memory.
  616. *
  617. ****************************************************************************/
  618. int nxffs_wrreserve(FAR struct nxffs_volume_s *volume, size_t size)
  619. {
  620. int ret;
  621. /* Seek to the beginning of the free FLASH region */
  622. nxffs_ioseek(volume, volume->froffset);
  623. /* Check for a seek past the end of the volume */
  624. if (volume->ioblock >= volume->nblocks)
  625. {
  626. /* Return -ENOSPC to indicate that the volume is full */
  627. return -ENOSPC;
  628. }
  629. /* Skip over block headers */
  630. if (volume->iooffset < SIZEOF_NXFFS_BLOCK_HDR)
  631. {
  632. volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  633. }
  634. /* Make sure that there is space there to hold the entire object */
  635. if (volume->iooffset + size > volume->geo.blocksize)
  636. {
  637. /* We will need to skip to the next block. But first, check if we are
  638. * already at the final block.
  639. */
  640. if (volume->ioblock + 1 >= volume->nblocks)
  641. {
  642. /* Return -ENOSPC to indicate that the volume is full */
  643. ferr("ERROR: No space in last block\n");
  644. return -ENOSPC;
  645. }
  646. /* This is not the last block in the volume, so just seek to the
  647. * beginning of the next, valid block.
  648. */
  649. volume->ioblock++;
  650. ret = nxffs_validblock(volume, &volume->ioblock);
  651. if (ret < 0)
  652. {
  653. ferr("ERROR: No more valid blocks\n");
  654. return ret;
  655. }
  656. volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  657. }
  658. /* Update the pointer to the first next free FLASH memory -- reserving this
  659. * block of memory.
  660. */
  661. volume->froffset = nxffs_iotell(volume) + size;
  662. return OK;
  663. }
  664. /****************************************************************************
  665. * Name: nxffs_wrverify
  666. *
  667. * Description:
  668. * Find a valid location for the object. A valid location will have
  669. * these properties:
  670. *
  671. * 1. It will lie in the free flash region.
  672. * 2. It will have enough contiguous memory to hold the entire header
  673. * (excluding the file name which may lie in the next block).
  674. * 3. The memory at this location will be fully erased.
  675. *
  676. * This function will only perform the check 3). On entry it assumes the
  677. * following settings (left by nxffs_wrreserve()):
  678. *
  679. * volume->ioblock - Read/write block number of the block containing the
  680. * candidate oject position
  681. * volume->iooffset - The offset in the block to the candidate object
  682. * position.
  683. *
  684. * Input Parameters:
  685. * volume - Describes the NXFFS volume
  686. * size - The size of the object to be verifed.
  687. *
  688. * Returned Value:
  689. * Zero is returned on success. Otherwise, a negated errno value is
  690. * returned indicating the nature of the failure. Of special interest
  691. * the return error of -ENOSPC which means that the FLASH volume is
  692. * full and should be repacked.
  693. *
  694. * On successful return the following are also valid:
  695. *
  696. * volume->ioblock - Read/write block number of the block containing the
  697. * verified object position
  698. * volume->iooffset - The offset in the block to the verified object
  699. * position.
  700. * volume->froffset - Updated offset to the first free FLASH block.
  701. *
  702. ****************************************************************************/
  703. int nxffs_wrverify(FAR struct nxffs_volume_s *volume, size_t size)
  704. {
  705. uint16_t iooffset;
  706. int nerased;
  707. int ret;
  708. int i;
  709. /* Search to the very last block in the volume if we have to */
  710. while (volume->ioblock < volume->nblocks)
  711. {
  712. /* Make sure that the block is in memory */
  713. ret = nxffs_rdcache(volume, volume->ioblock);
  714. if (ret < 0)
  715. {
  716. /* Ignore the error... just skip to the next block. This should
  717. * never happen with normal FLASH, but could occur with NAND if
  718. * the block has uncorrectable bit errors.
  719. */
  720. ferr("ERROR: Failed to read block %d: %d\n",
  721. volume->ioblock, -ret);
  722. }
  723. /* Search to the very end of this block if we have to */
  724. else
  725. {
  726. iooffset = volume->iooffset;
  727. nerased = 0;
  728. for (i = volume->iooffset; i < volume->geo.blocksize; i++)
  729. {
  730. /* Is this byte erased? */
  731. if (volume->cache[i] == CONFIG_NXFFS_ERASEDSTATE)
  732. {
  733. /* Yes.. increment the count of contiguous, erased bytes */
  734. nerased++;
  735. /* Is the whole header memory erased? */
  736. if (nerased >= size)
  737. {
  738. /* Yes.. this this is where we will put the object */
  739. off_t offset =
  740. volume->ioblock * volume->geo.blocksize + iooffset;
  741. /* Update the free flash offset and return success */
  742. volume->froffset = offset + size;
  743. return OK;
  744. }
  745. }
  746. /* This byte is not erased! (It should be unless the block is
  747. * bad)
  748. */
  749. else
  750. {
  751. nerased = 0;
  752. iooffset = i + 1;
  753. }
  754. }
  755. }
  756. /* If we get here, then either (1) this block is not read-able, or
  757. * (2) we have looked at every byte in the block and did not find
  758. * any sequence of erased bytes long enough to hold the object.
  759. * Skip to the next, valid block.
  760. */
  761. volume->ioblock++;
  762. ret = nxffs_validblock(volume, &volume->ioblock);
  763. if (ret < 0)
  764. {
  765. ferr("ERROR: No more valid blocks\n");
  766. return ret;
  767. }
  768. volume->iooffset = SIZEOF_NXFFS_BLOCK_HDR;
  769. volume->froffset = volume->ioblock * volume->geo.blocksize +
  770. SIZEOF_NXFFS_BLOCK_HDR;
  771. }
  772. /* Return -ENOSPC if there is no erased memory left in the volume for
  773. * the object.
  774. */
  775. ferr("ERROR: Not enough memory left to hold the file header\n");
  776. return -ENOSPC;
  777. }
  778. /****************************************************************************
  779. * Name: nxffs_wrblkhdr
  780. *
  781. * Description:
  782. * Write the block header information. This is done (1) whenever the end-
  783. * block is encountered and (2) also when the file is closed in order to
  784. * flush the final block of data to FLASH.
  785. *
  786. * Input Parameters:
  787. * volume - Describes the state of the NXFFS volume
  788. * wrfile - Describes the state of the open file
  789. *
  790. * Returned Value:
  791. * Zero is returned on success; Otherwise, a negated errno value is
  792. * returned to indicate the nature of the failure.
  793. *
  794. ****************************************************************************/
  795. int nxffs_wrblkhdr(FAR struct nxffs_volume_s *volume,
  796. FAR struct nxffs_wrfile_s *wrfile)
  797. {
  798. FAR struct nxffs_data_s *dathdr;
  799. int ret;
  800. /* Write the data block header to memory */
  801. nxffs_ioseek(volume, wrfile->doffset);
  802. dathdr = (FAR struct nxffs_data_s *)&volume->cache[volume->iooffset];
  803. memcpy(dathdr->magic, g_datamagic, NXFFS_MAGICSIZE);
  804. nxffs_wrle32(dathdr->crc, 0);
  805. nxffs_wrle16(dathdr->datlen, wrfile->datlen);
  806. /* Update the entire data block CRC (including the header) */
  807. wrfile->crc = crc32(&volume->cache[volume->iooffset],
  808. wrfile->datlen + SIZEOF_NXFFS_DATA_HDR);
  809. nxffs_wrle32(dathdr->crc, wrfile->crc);
  810. /* And write the data block to FLASH */
  811. ret = nxffs_wrcache(volume);
  812. if (ret < 0)
  813. {
  814. ferr("ERROR: nxffs_wrcache failed: %d\n", -ret);
  815. goto errout;
  816. }
  817. /* After the block has been successfully written to flash, update the inode
  818. * statistics and reset the write state.
  819. *
  820. * volume:
  821. * froffset - The offset the next free FLASH region. Set to just after
  822. * the inode data block that we just wrote. This is where we will
  823. * begin the search for the next inode header or data block.
  824. */
  825. volume->froffset = (wrfile->doffset + wrfile->datlen + SIZEOF_NXFFS_DATA_HDR);
  826. /* wrfile->file.entry:
  827. * datlen: Total file length accumulated so far. When the file is
  828. * closed, this will hold the file length.
  829. * doffset: Offset to the first data block. Only the offset to the
  830. * first data block is saved.
  831. */
  832. wrfile->ofile.entry.datlen += wrfile->datlen;
  833. if (wrfile->ofile.entry.doffset == 0)
  834. {
  835. wrfile->ofile.entry.doffset = wrfile->doffset;
  836. }
  837. /* Return success */
  838. ret = OK;
  839. errout:
  840. wrfile->crc = 0;
  841. wrfile->doffset = 0;
  842. wrfile->datlen = 0;
  843. return ret;
  844. }