nxffs_blockstats.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_blockstats.c
  3. *
  4. * Copyright (C) 2011 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 <debug.h>
  44. #include <nuttx/mtd/mtd.h>
  45. #include "nxffs.h"
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: nxffs_blockstats
  51. *
  52. * Description:
  53. * Analyze the NXFFS volume. This operation must be performed when the
  54. * volume is first mounted in order to detect if the volume has been
  55. * formatted and contains a usable NXFFS file system.
  56. *
  57. * Input Parameters:
  58. * volume - Describes the current NXFFS volume.
  59. * stats - On return, will hold nformation describing the state of the
  60. * volume.
  61. *
  62. * Returned Value:
  63. * Negated errnos are returned only in the case of MTD reported failures.
  64. * Nothing in the volume data itself will generate errors.
  65. *
  66. ****************************************************************************/
  67. int nxffs_blockstats(FAR struct nxffs_volume_s *volume,
  68. FAR struct nxffs_blkstats_s *stats)
  69. {
  70. #ifndef CONFIG_NXFFS_NAND
  71. FAR uint8_t *bptr; /* Pointer to next block data */
  72. int lblock; /* Logical block index */
  73. #endif
  74. off_t ioblock; /* I/O block number */
  75. int ret;
  76. /* Process each erase block */
  77. memset(stats, 0, sizeof(struct nxffs_blkstats_s));
  78. #ifndef CONFIG_NXFFS_NAND
  79. for (ioblock = 0; ioblock < volume->nblocks; ioblock += volume->blkper)
  80. {
  81. /* Read the full erase block */
  82. ret = MTD_BREAD(volume->mtd, ioblock, volume->blkper, volume->pack);
  83. if (ret < volume->blkper)
  84. {
  85. ferr("ERROR: Failed to read erase block %d: %d\n",
  86. ioblock / volume->blkper, ret);
  87. return ret;
  88. }
  89. /* Then examine each logical block in the erase block */
  90. for (bptr = volume->pack, lblock = 0;
  91. lblock < volume->blkper;
  92. bptr += volume->geo.blocksize, lblock++)
  93. {
  94. /* We read the block successfully, now check for errors tagged
  95. * in the NXFFS data.
  96. */
  97. FAR struct nxffs_block_s *blkhdr = (FAR struct nxffs_block_s *)bptr;
  98. /* Increment the total count of blocks examined */
  99. stats->nblocks++;
  100. /* Collect statistics.
  101. *
  102. * Check if this is a block that should be recognized by NXFFS.
  103. */
  104. if (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) != 0)
  105. {
  106. /* Nope.. block must not be formatted */
  107. stats->nunformat++;
  108. }
  109. else if (blkhdr->state == BLOCK_STATE_BAD)
  110. {
  111. /* The block is marked as bad */
  112. stats->nbad++;
  113. }
  114. else if (blkhdr->state == BLOCK_STATE_GOOD)
  115. {
  116. /* The block is marked as good */
  117. stats-> ngood++;
  118. }
  119. else
  120. {
  121. /* The good/bad mark is not recognized. Let's call this
  122. * corrupt (vs. unformatted).
  123. */
  124. stats->ncorrupt++;
  125. }
  126. }
  127. }
  128. finfo("Number blocks: %d\n", stats->nblocks);
  129. finfo(" Good blocks: %d\n", stats->ngood);
  130. finfo(" Bad blocks: %d\n", stats->nbad);
  131. finfo(" Unformatted blocks: %d\n", stats->nunformat);
  132. finfo(" Corrupt blocks: %d\n", stats->ncorrupt);
  133. #else
  134. for (ioblock = 0; ioblock < volume->nblocks; ioblock++)
  135. {
  136. /* Increment the total count of blocks examined */
  137. stats->nblocks++;
  138. /* Read each logical block, one at a time. We could read all of the
  139. * blocks in the erase block into volume->pack at once. But this would
  140. * be a problem for NAND which may generate read errors due to bad ECC
  141. * on individual blocks.
  142. */
  143. ret = MTD_BREAD(volume->mtd, ioblock, 1, volume->pack);
  144. if (ret < 1)
  145. {
  146. /* This should not happen at all on most kinds of FLASH. But a
  147. * bad read will happen normally with a NAND device that has
  148. * uncorrectable blocks. So, just for NAND, we keep the count
  149. * of unreadable blocks.
  150. */
  151. ferr("ERROR: Failed to read block %d: %d\n", ioblock, ret);
  152. /* Increment the count of un-readable blocks */
  153. stats->nbadread++;
  154. }
  155. else
  156. {
  157. /* We read the block successfully, now check for errors tagged
  158. * in the NXFFS data.
  159. */
  160. FAR struct nxffs_block_s *blkhdr = (FAR struct nxffs_block_s *)volume->pack;
  161. /* Collect statistics.
  162. *
  163. * Check if this is a block that should be recognized by NXFFS.
  164. */
  165. if (memcmp(blkhdr->magic, g_blockmagic, NXFFS_MAGICSIZE) != 0)
  166. {
  167. /* Nope.. block must not be formatted */
  168. stats->nunformat++;
  169. }
  170. else if (blkhdr->state == BLOCK_STATE_BAD)
  171. {
  172. /* The block is marked as bad */
  173. stats->nbad++;
  174. }
  175. else if (blkhdr->state == BLOCK_STATE_GOOD)
  176. {
  177. /* The block is marked as good */
  178. stats-> ngood++;
  179. }
  180. else
  181. {
  182. /* The good/bad mark is not recognized. Let's call this
  183. * corrupt (vs. unformatted).
  184. */
  185. stats->ncorrupt++;
  186. }
  187. }
  188. }
  189. finfo("Number blocks: %d\n", stats->nblocks);
  190. finfo(" Good blocks: %d\n", stats->ngood);
  191. finfo(" Bad blocks: %d\n", stats->nbad);
  192. finfo(" Unformatted blocks: %d\n", stats->nunformat);
  193. finfo(" Corrupt blocks: %d\n", stats->ncorrupt);
  194. finfo(" Unreadable blocks: %d\n", stats->nbadread);
  195. #endif
  196. return OK;
  197. }