nxffs_block.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_block.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 <debug.h>
  45. #include <nuttx/mtd/mtd.h>
  46. #include "nxffs.h"
  47. /****************************************************************************
  48. * Public Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: nxffs_verifyblock
  52. *
  53. * Description:
  54. * Assure that the provided (logical) block number is in the block cache
  55. * and that it has a valid block header (i.e., proper magic and
  56. * marked good)
  57. *
  58. * Input Parameters:
  59. * volume - Describes the NXFFS volume
  60. * block - The (logical) block number to load and verify.
  61. *
  62. * Returned Value:
  63. * OK (zero( is returned on success. Otherwise, a negated errno value is
  64. * returned indicating the nature of the failure:
  65. *
  66. * -EIO is returned if we failed to read the block. If we are using
  67. * NAND memory, then this probably means that the block has
  68. * uncorrectable bit errors.
  69. * -ENOENT is returned if the block is a bad block.
  70. *
  71. ****************************************************************************/
  72. int nxffs_verifyblock(FAR struct nxffs_volume_s *volume, off_t block)
  73. {
  74. FAR struct nxffs_block_s *blkhdr;
  75. int ret;
  76. /* Make sure that the block is in the cache */
  77. ret = nxffs_rdcache(volume, block);
  78. if (ret < 0)
  79. {
  80. /* Perhaps we are at the end of the media */
  81. ferr("ERROR: Failed to read data into cache: %d\n", ret);
  82. return -EIO;
  83. }
  84. /* Check if the block has a magic number (meaning that it is not
  85. * erased) and that it is valid (meaning that it is not marked
  86. * for deletion)
  87. */
  88. blkhdr = (FAR struct nxffs_block_s *)volume->cache;
  89. if (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) == 0)
  90. {
  91. /* This does appear to be a block */
  92. if (blkhdr->state == BLOCK_STATE_GOOD)
  93. {
  94. /* The block is valid */
  95. return OK;
  96. }
  97. else if (blkhdr->state == BLOCK_STATE_BAD)
  98. {
  99. /* -ENOENT is a special indication that this is a properly marked
  100. * bad block
  101. */
  102. return -ENOENT;
  103. }
  104. }
  105. /* Whatever is here where a block header should be is invalid */
  106. return -EINVAL;
  107. }
  108. /****************************************************************************
  109. * Name: nxffs_validblock
  110. *
  111. * Description:
  112. * Find the next valid (logical) block in the volume.
  113. *
  114. * Input Parameters:
  115. * volume - Describes the NXFFS volume
  116. * block - On entry, this provides the starting block number. If the
  117. * function is succesfful, then this memory location will hold the
  118. * block number of the next valid block on return.
  119. *
  120. * Returned Value:
  121. * Zero on success otherwise a negated errno value indicating the nature
  122. * of the failure.
  123. *
  124. ****************************************************************************/
  125. int nxffs_validblock(struct nxffs_volume_s *volume, off_t *block)
  126. {
  127. off_t i;
  128. int ret;
  129. DEBUGASSERT(volume && block);
  130. /* Loop for each possible block or until a valid block is found */
  131. for (i = *block; i < volume->nblocks; i++)
  132. {
  133. /* Loop until we find a valid block */
  134. ret = nxffs_verifyblock(volume, i);
  135. if (ret == OK)
  136. {
  137. /* We found it, return the block number */
  138. *block = i;
  139. return OK;
  140. }
  141. }
  142. /* ENOSPC is special return value that means that there is no further,
  143. * valid blocks left in the volume.
  144. */
  145. ferr("ERROR: No valid block found\n");
  146. return -ENOSPC;
  147. }