README.txt 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. NXFFS README
  2. ^^^^^^^^^^^^
  3. This README file contains information about the implemenation of the NuttX
  4. wear-leveling FLASH file system, NXFFS.
  5. Contents:
  6. General NXFFS organization
  7. General operation
  8. Headers
  9. NXFFS Limitations
  10. Multiple Writers
  11. ioctls
  12. Things to Do
  13. General NXFFS organization
  14. ==========================
  15. The following example assumes 4 logical blocks per FLASH erase block. The
  16. actual relationship is determined by the FLASH geometry reported by the MTD
  17. driver.
  18. ERASE LOGICAL Inodes begin with a inode header. inode may
  19. BLOCK BLOCK CONTENTS be marked as "deleted," pending re-packing.
  20. n 4*n --+--------------+
  21. |BBBBBBBBBBBBBB| Logic block header
  22. |IIIIIIIIIIIIII| Inodes begin with a inode header
  23. |DDDDDDDDDDDDDD| Data block containing inode data block
  24. | (Inode Data) |
  25. 4*n+1 --+--------------+
  26. |BBBBBBBBBBBBBB| Logic block header
  27. |DDDDDDDDDDDDDD| Inodes may consist of multiple data blocks
  28. | (Inode Data) |
  29. |IIIIIIIIIIIIII| Next inode header
  30. | | Possibly a few unused bytes at the end of a block
  31. 4*n+2 --+--------------+
  32. |BBBBBBBBBBBBBB| Logic block header
  33. |DDDDDDDDDDDDDD|
  34. | (Inode Data) |
  35. 4*n+3 --+--------------+
  36. |BBBBBBBBBBBBBB| Logic block header
  37. |IIIIIIIIIIIIII| Next inode header
  38. |DDDDDDDDDDDDDD|
  39. | (Inode Data) |
  40. n+1 4*(n+1) --+--------------+
  41. |BBBBBBBBBBBBBB| Logic block header
  42. | | All FLASH is unused after the end of the final
  43. | | inode.
  44. --+--------------+
  45. General operation
  46. =================
  47. Inodes are written starting at the beginning of FLASH. As inodes are
  48. deleted, they are marked as deleted but not removed. As new inodes are
  49. written, allocations proceed to toward the end of the FLASH -- thus,
  50. supporting wear leveling by using all FLASH blocks equally.
  51. When the FLASH becomes full (no more space at the end of the FLASH), a
  52. re-packing operation must be performed: All inodes marked deleted are
  53. finally removed and the remaining inodes are packed at the beginning of
  54. the FLASH. Allocations then continue at the freed FLASH memory at the
  55. end of the FLASH.
  56. Headers
  57. =======
  58. BLOCK HEADER:
  59. The block header is used to determine if the block has every been
  60. formatted and also indicates bad blocks which should never be used.
  61. INODE HEADER:
  62. Each inode begins with an inode header that contains, among other things,
  63. the name of the inode, the offset to the first data block, and the
  64. length of the inode data.
  65. At present, the only kind of inode support is a file. So for now, the
  66. term file and inode are interchangeable.
  67. INODE DATA HEADER:
  68. Inode data is enclosed in a data header. For a given inode, there
  69. is at most one inode data block per logical block. If the inode data
  70. spans more than one logical block, then the inode data may be enclosed
  71. in multiple data blocks, one per logical block.
  72. NXFFS Limitations
  73. =================
  74. This implementation is very simple as, as a result, has several limitations
  75. that you should be aware before opting to use NXFFS:
  76. 1. Since the files are contiguous in FLASH and since allocations always
  77. proceed toward the end of the FLASH, there can only be one file opened
  78. for writing at a time. Multiple files may be opened for reading.
  79. 2. Files may not be increased in size after they have been closed. The
  80. O_APPEND open flag is not supported.
  81. 3. Files are always written sequential. Seeking within a file opened for
  82. writing will not work.
  83. 4. There are no directories, however, '/' may be used within a file name
  84. string providing some illusion of directories.
  85. 5. Files may be opened for reading or for writing, but not both: The O_RDWR
  86. open flag is not supported.
  87. 6. The re-packing process occurs only during a write when the free FLASH
  88. memory at the end of the FLASH is exhausted. Thus, occasionally, file
  89. writing may take a long time.
  90. 7. Another limitation is that there can be only a single NXFFS volume
  91. mounted at any time. This has to do with the fact that we bind to
  92. an MTD driver (instead of a block driver) and bypass all of the normal
  93. mount operations.
  94. Multiple Writers
  95. ================
  96. As mentioned in the limitations above, there can be only one file opened
  97. for writing at a time. If one thread has a file opened for writing and
  98. another thread attempts to open a file for writing, then that second
  99. thread will be blocked and will have to wait for the first thread to
  100. close the file.
  101. Such behavior may or may not be a problem for your application, depending
  102. (1) how long the first thread keeps the file open for writing and (2) how
  103. critical the behavior of the second thread is. Note that writing to FLASH
  104. can always trigger a major FLASH reorganization and, hence, there is no
  105. way to guarantee the first condition: The first thread may have the file
  106. open for a long time even if it only intends to write a small amount.
  107. Also note that a deadlock condition would occur if the SAME thread
  108. attempted to open two files for writing. The thread would would be
  109. blocked waiting for itself to close the first file.
  110. ioctls
  111. ======
  112. The file system supports to ioctls:
  113. FIOC_REFORMAT: Will force the flash to be erased and a fresh, empty
  114. NXFFS file system to be written on it.
  115. FIOC_OPTIMIZE: Will force immediate repacking of the file system. This
  116. will avoid the delays to repack the file system in the emergency case
  117. when all of the FLASH memory has been used. Instead, you can defer
  118. the garbage collection to time when the system is not busy. Calling
  119. this function on a thrashing file system will increase the amount of
  120. wear on the FLASH if you use this frequently!
  121. Things to Do
  122. ============
  123. - The statfs() implementation is minimal. It should have some calculation
  124. of the f_bfree, f_bavail, f_files, f_ffree return values.
  125. - There are too many allocs and frees. More structures may need to be
  126. pre-allocated.
  127. - The file name is always extracted and held in allocated, variable-length
  128. memory. The file name is not used during reading and eliminating the
  129. file name in the entry structure would improve performance.
  130. - There is a big inefficiency in reading. On each read, the logic searches
  131. for the read position from the beginning of the file each time. This
  132. may be necessary whenever an lseek() is done, but not in general. Read
  133. performance could be improved by keeping FLASH offset and read positional
  134. information in the read open file structure.
  135. - Fault tolerance must be improved. We need to be absolutely certain that
  136. any FLASH errors do not cause the file system to behavior incorrectly.
  137. - Wear leveling might be improved (?). Files are re-packed at the front
  138. of FLASH as part of the clean-up operation. However, that means the files
  139. that are not modified often become fixed in place at the beginning of
  140. FLASH. This reduces the size of the pool moving files at the end of the
  141. FLASH. As the file system becomes more filled with fixed files at the
  142. front of the device, the level of wear on the blocks at the end of the
  143. FLASH increases.
  144. - When the time comes to reorganization the FLASH, the system may be
  145. unavailable for a long time. That is a bad behavior. What is needed,
  146. I think, is a garbage collection task that runs periodically so that
  147. when the big reorganization event occurs, most of the work is already
  148. done. That garbage collection should search for valid blocks that no
  149. longer contain valid data. It should pre-erase them, put them in
  150. a good but empty state... all ready for file system re-organization.
  151. NOTE: There is the FIOC_OPTIMIZE IOCTL command that can be used by an
  152. application for force garbage collection when the system is not busy.
  153. If used judiciously by the application, this can eliminate the problem.
  154. - And worse, when NXFSS reorganization the FLASH a power cycle can
  155. damage the file system content if it happens at the wrong time.
  156. - The current design does not permit re-opening of files for write access
  157. unless the file is truncated to zero length. This effectively prohibits
  158. implementation of a proper truncate() method which should alter the
  159. size of a previously written file. There is some fragmentary logic in
  160. place but even this is conditioned out with __NO_TRUNCATE_SUPPORT__.