nxffs.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /****************************************************************************
  2. * fs/nxffs/nxffs.h
  3. *
  4. * Copyright (C) 2011, 2013, 2015, 2017-2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * References: Linux/Documentation/filesystems/romfs.txt
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. * 3. Neither the name NuttX nor the names of its contributors may be
  21. * used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  25. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  27. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  28. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  29. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  30. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  31. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  32. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ****************************************************************************/
  38. #ifndef __FS_NXFFS_NXFFS_H
  39. #define __FS_NXFFS_NXFFS_H
  40. /****************************************************************************
  41. * Included Files
  42. ****************************************************************************/
  43. #include <nuttx/config.h>
  44. #include <sys/types.h>
  45. #include <stdint.h>
  46. #include <stdbool.h>
  47. #include <semaphore.h>
  48. #include <nuttx/mtd/mtd.h>
  49. #include <nuttx/fs/nxffs.h>
  50. /****************************************************************************
  51. * Pre-processor Definitions
  52. ****************************************************************************/
  53. /* NXFFS Definitions ********************************************************/
  54. /* General NXFFS organization. The following example assumes 4 logical
  55. * blocks per FLASH erase block. The actual relationship is determined by
  56. * the FLASH geometry reported by the MTD driver.
  57. *
  58. * ERASE LOGICAL Inodes begin with a inode header. inode may
  59. * BLOCK BLOCK CONTENTS be marked as "deleted," pending re-packing.
  60. * n 4*n --+--------------+
  61. * |BBBBBBBBBBBBBB| Logic block header
  62. * |IIIIIIIIIIIIII| Inodes begin with a inode header
  63. * |DDDDDDDDDDDDDD| Data block containing inode data block
  64. * | (Inode Data) |
  65. * 4*n+1 --+--------------+
  66. * |BBBBBBBBBBBBBB| Logic block header
  67. * |DDDDDDDDDDDDDD| Inodes may consist of multiple data blocks
  68. * | (Inode Data) |
  69. * |IIIIIIIIIIIIII| Next inode header
  70. * | | Possibly a few unused bytes at the end of a block
  71. * 4*n+2 --+--------------+
  72. * |BBBBBBBBBBBBBB| Logic block header
  73. * |DDDDDDDDDDDDDD|
  74. * | (Inode Data) |
  75. * 4*n+3 --+--------------+
  76. * |BBBBBBBBBBBBBB| Logic block header
  77. * |IIIIIIIIIIIIII| Next inode header
  78. * |DDDDDDDDDDDDDD|
  79. * | (Inode Data) |
  80. * n+1 4*(n+1) --+--------------+
  81. * |BBBBBBBBBBBBBB| Logic block header
  82. * | | All FLASH is unused after the end of the final
  83. * | | inode.
  84. * --+--------------+
  85. *
  86. * General operation:
  87. * Inodes are written starting at the beginning of FLASH. As inodes are
  88. * deleted, they are marked as deleted but not removed. As new inodes are
  89. * written, allocations proceed to toward the end of the FLASH -- thus,
  90. * supporting wear leveling by using all FLASH blocks equally.
  91. *
  92. * When the FLASH becomes full (no more space at the end of the FLASH), a
  93. * re-packing operation must be performed: All inodes marked deleted are
  94. * finally removed and the remaining inodes are packed at the beginning of
  95. * the FLASH. Allocations then continue at the freed FLASH memory at the
  96. * end of the FLASH.
  97. *
  98. * BLOCK HEADER:
  99. * The block header is used to determine if the block has every been
  100. * formatted and also indicates bad blocks which should never be used.
  101. *
  102. * INODE HEADER:
  103. * Each inode begins with an inode header that contains, among other things,
  104. * the name of the inode, the offset to the first data block, and the
  105. * length of the inode data.
  106. *
  107. * At present, the only kind of inode support is a file. So for now, the
  108. * term file and inode are interchangeable.
  109. *
  110. * INODE DATA HEADER:
  111. * Inode data is enclosed in a data header. For a given inode, there
  112. * is at most one inode data block per logical block. If the inode data
  113. * spans more than one logical block, then the inode data may be enclosed
  114. * in multiple data blocks, one per logical block.
  115. *
  116. * NXFFS Limitations:
  117. * 1. Since the files are contiguous in FLASH and since allocations always
  118. * proceed toward the end of the FLASH, there can only be one file opened
  119. * for writing at a time. Multiple files may be opened for reading.
  120. * 2. Files may not be increased in size after they have been closed. The
  121. * O_APPEND open flag is not supported.
  122. * 3. Files are always written sequential. Seeking within a file opened for
  123. * writing will not work.
  124. * 4. There are no directories, however, '/' may be used within a file name
  125. * string providing some illusion of directories.
  126. * 5. Files may be opened for reading or for writing, but not both: The O_RDWR
  127. * open flag is not supported.
  128. * 6. The re-packing process occurs only during a write when the free FLASH
  129. * memory at the end of the FLASH is exhausted. Thus, occasionally, file
  130. * writing may take a long time.
  131. * 7. Another limitation is that there can be only a single NXFFS volume
  132. * mounted at any time. This has to do with the fact that we bind to
  133. * an MTD driver (instead of a block driver) and bypass all of the normal
  134. * mount operations.
  135. */
  136. /* Values for logical block state. Basically, there are only two, perhaps
  137. * three, states:
  138. *
  139. * BLOCK_STATE_GOOD - The block is not known to be bad.
  140. * BLOCK_STATE_BAD - An error was found on the block and it is marked bad.
  141. * Other values - The block is bad and has an invalid state.
  142. *
  143. * Care is taken so that the GOOD to BAD transition only involves burning
  144. * bits from the erased to non-erased state.
  145. */
  146. #define BLOCK_STATE_GOOD (CONFIG_NXFFS_ERASEDSTATE ^ 0x44)
  147. #define BLOCK_STATE_BAD (CONFIG_NXFFS_ERASEDSTATE ^ 0x55)
  148. /* Values for NXFFS inode state. Similar there are 2 (maybe 3) inode states:
  149. *
  150. * INODE_STATE_FILE - The inode is a valid usuable, file
  151. * INODE_STATE_DELETED - The inode has been deleted.
  152. * Other values - The inode is bad and has an invalid state.
  153. *
  154. * Care is taken so that the VALID to DELETED transition only involves burning
  155. * bits from the erased to non-erased state.
  156. */
  157. #define INODE_STATE_FILE (CONFIG_NXFFS_ERASEDSTATE ^ 0x22)
  158. #define INODE_STATE_DELETED (CONFIG_NXFFS_ERASEDSTATE ^ 0xaa)
  159. /* Number of bytes in an the NXFFS magic sequences */
  160. #define NXFFS_MAGICSIZE 4
  161. /* When we allocate FLASH for a new inode data block, we will require that
  162. * space is available to hold this minimum number of data bytes in addition
  163. * to the size of the data block headeer.
  164. */
  165. #define NXFFS_MINDATA 16
  166. /* Internal definitions *****************************************************/
  167. /* If we encounter this number of erased bytes, we assume that all of the
  168. * flash beyond this point is erased.
  169. */
  170. #define NXFFS_NERASED 128
  171. /* Quasi-standard definitions */
  172. #ifndef MIN
  173. # define MIN(a,b) (a < b ? a : b)
  174. #endif
  175. #ifndef MAX
  176. # define MAX(a,b) (a > b ? a : b)
  177. #endif
  178. /****************************************************************************
  179. * Public Types
  180. ****************************************************************************/
  181. /* This structure defines each packed block on the FLASH media */
  182. struct nxffs_block_s
  183. {
  184. uint8_t magic[4]; /* 0-3: Magic number for valid block */
  185. uint8_t state; /* 4: Block state: See BLOCK_STATE_* */
  186. };
  187. #define SIZEOF_NXFFS_BLOCK_HDR 5
  188. /* This structure defines each packed NXFFS inode header on the FLASH media */
  189. struct nxffs_inode_s
  190. {
  191. uint8_t magic[4]; /* 0-3: Magic number for valid inode */
  192. uint8_t state; /* 4: Inode state: See INODE_STATE_* */
  193. uint8_t namlen; /* 5: Length of the inode name */
  194. uint8_t noffs[4]; /* 6-9: FLASH offset to the file name */
  195. uint8_t doffs[4]; /* 10-13: FLASH offset to the first data block */
  196. uint8_t utc[4]; /* 14-17: Creation time */
  197. uint8_t crc[4]; /* 18-21: CRC32 */
  198. uint8_t datlen[4]; /* 22-25: Length of data in bytes */
  199. };
  200. #define SIZEOF_NXFFS_INODE_HDR 26
  201. /* This structure defines each packed NXFFS data header on the FLASH media */
  202. struct nxffs_data_s
  203. {
  204. uint8_t magic[4]; /* 0-3: Magic number for valid data */
  205. uint8_t crc[4]; /* 4-7: CRC32 */
  206. uint8_t datlen[2]; /* 8-9: Length of data in bytes */
  207. };
  208. #define SIZEOF_NXFFS_DATA_HDR 10
  209. /* This is an in-memory representation of the NXFFS inode as extracted from
  210. * FLASH and with additional state information.
  211. */
  212. struct nxffs_entry_s
  213. {
  214. off_t hoffset; /* FLASH offset to the inode header */
  215. off_t noffset; /* FLASH offset to the inode name */
  216. off_t doffset; /* FLASH offset to the first data header */
  217. FAR char *name; /* inode name */
  218. uint32_t utc; /* Time stamp */
  219. uint32_t datlen; /* Length of inode data */
  220. };
  221. /* This structure describes int in-memory representation of the data block */
  222. struct nxffs_blkentry_s
  223. {
  224. off_t hoffset; /* Offset to the block data header */
  225. uint16_t datlen; /* Length of data following the header */
  226. uint16_t foffset; /* Offset to start of data */
  227. };
  228. /* This structure describes the state of one open file. This structure
  229. * is protected by the volume semaphore.
  230. */
  231. struct nxffs_ofile_s
  232. {
  233. struct nxffs_ofile_s *flink; /* Supports a singly linked list */
  234. int16_t crefs; /* Reference count */
  235. mode_t oflags; /* Open mode */
  236. struct nxffs_entry_s entry; /* Describes the NXFFS inode entry */
  237. };
  238. /* A file opened for writing require some additional information */
  239. struct nxffs_wrfile_s
  240. {
  241. /* The following fields provide the common open file information. */
  242. struct nxffs_ofile_s ofile;
  243. /* The following fields are required to support the write operation */
  244. bool truncate; /* Delete a file of the same name */
  245. uint16_t datlen; /* Number of bytes written in data block */
  246. off_t doffset; /* FLASH offset to the current data header */
  247. uint32_t crc; /* Accumulated data block CRC */
  248. };
  249. /* This structure represents the overall state of on NXFFS instance. */
  250. struct nxffs_volume_s
  251. {
  252. FAR struct mtd_dev_s *mtd; /* Supports FLASH access */
  253. sem_t exclsem; /* Used to assure thread-safe access */
  254. sem_t wrsem; /* Enforces single writer restriction */
  255. struct mtd_geometry_s geo; /* Device geometry */
  256. uint8_t blkper; /* R/W blocks per erase block */
  257. uint16_t iooffset; /* Next offset in read/write access (in ioblock) */
  258. off_t inoffset; /* Offset to the first valid inode header */
  259. off_t froffset; /* Offset to the first free byte */
  260. off_t nblocks; /* Number of R/W blocks on volume */
  261. off_t ioblock; /* Current block number being accessed */
  262. off_t cblock; /* Starting block number in cache */
  263. FAR struct nxffs_ofile_s *ofiles; /* A singly-linked list of open files */
  264. FAR uint8_t *cache; /* On cached erase block for general I/O */
  265. FAR uint8_t *pack; /* A full erase block to support packing */
  266. };
  267. /* This structure describes the state of the blocks on the NXFFS volume */
  268. struct nxffs_blkstats_s
  269. {
  270. off_t nblocks; /* Total number of FLASH blocks */
  271. off_t ngood; /* Number of good FLASH blocks found */
  272. off_t nbad; /* Number of well-formatted FLASH blocks marked as bad */
  273. off_t nunformat; /* Number of unformatted FLASH blocks */
  274. off_t ncorrupt; /* Number of blocks with corrupted format info */
  275. off_t nbadread; /* Number of blocks that could not be read */
  276. };
  277. /****************************************************************************
  278. * Public Data
  279. ****************************************************************************/
  280. /* The magic number that appears that the beginning of each NXFFS (logical)
  281. * block
  282. */
  283. extern const uint8_t g_blockmagic[NXFFS_MAGICSIZE];
  284. /* The magic number that appears that the beginning of each NXFFS inode */
  285. extern const uint8_t g_inodemagic[NXFFS_MAGICSIZE];
  286. /* The magic number that appears that the beginning of each NXFFS inode
  287. * data block.
  288. */
  289. extern const uint8_t g_datamagic[NXFFS_MAGICSIZE];
  290. /* If CONFIG_NXFFS_PREALLOCATED is defined, then this is the single, pre-
  291. * allocated NXFFS volume instance.
  292. */
  293. #ifdef CONFIG_NXFFS_PREALLOCATED
  294. extern struct nxffs_volume_s g_volume;
  295. #endif
  296. /****************************************************************************
  297. * Public Function Prototypes
  298. ****************************************************************************/
  299. /****************************************************************************
  300. * Name: nxffs_limits
  301. *
  302. * Description:
  303. * Recalculate file system limits: (1) the FLASH offset to the first,
  304. * valid inode, and (2) the FLASH offset to the first, unused byte after
  305. * the last inode (invalid or not).
  306. *
  307. * The first, lower limit must be recalculated: (1) initially, (2)
  308. * whenever the first inode is deleted, or (3) whenever inode is moved
  309. * as part of the file system packing operation.
  310. *
  311. * The second, upper limit must be (1) incremented whenever new file
  312. * data is written, or (2) recalculated as part of the file system packing
  313. * operation.
  314. *
  315. * Input Parameters:
  316. * volume - Identifies the NXFFS volume
  317. *
  318. * Returned Value:
  319. * Zero on success. Otherwise, a negated error is returned indicating the
  320. * nature of the failure.
  321. *
  322. * Defined in nxffs_initialize.c
  323. *
  324. ****************************************************************************/
  325. int nxffs_limits(FAR struct nxffs_volume_s *volume);
  326. /****************************************************************************
  327. * Name: nxffs_rdle16
  328. *
  329. * Description:
  330. * Get a (possibly unaligned) 16-bit little endian value.
  331. *
  332. * Input Parameters:
  333. * val - A pointer to the first byte of the little endian value.
  334. *
  335. * Returned Value:
  336. * A uint16_t representing the whole 16-bit integer value
  337. *
  338. * Defined in nxffs_util.c
  339. *
  340. ****************************************************************************/
  341. uint16_t nxffs_rdle16(FAR const uint8_t *val);
  342. /****************************************************************************
  343. * Name: nxffs_wrle16
  344. *
  345. * Description:
  346. * Put a (possibly unaligned) 16-bit little endian value.
  347. *
  348. * Input Parameters:
  349. * dest - A pointer to the first byte to save the little endian value.
  350. * val - The 16-bit value to be saved.
  351. *
  352. * Returned Value:
  353. * None
  354. *
  355. * Defined in nxffs_util.c
  356. *
  357. ****************************************************************************/
  358. void nxffs_wrle16(uint8_t *dest, uint16_t val);
  359. /****************************************************************************
  360. * Name: nxffs_rdle32
  361. *
  362. * Description:
  363. * Get a (possibly unaligned) 32-bit little endian value.
  364. *
  365. * Input Parameters:
  366. * val - A pointer to the first byte of the little endian value.
  367. *
  368. * Returned Value:
  369. * A uint32_t representing the whole 32-bit integer value
  370. *
  371. * Defined in nxffs_util.c
  372. *
  373. ****************************************************************************/
  374. uint32_t nxffs_rdle32(FAR const uint8_t *val);
  375. /****************************************************************************
  376. * Name: nxffs_wrle32
  377. *
  378. * Description:
  379. * Put a (possibly unaligned) 32-bit little endian value.
  380. *
  381. * Input Parameters:
  382. * dest - A pointer to the first byte to save the little endian value.
  383. * val - The 32-bit value to be saved.
  384. *
  385. * Returned Value:
  386. * None
  387. *
  388. * Defined in nxffs_util.c
  389. *
  390. ****************************************************************************/
  391. void nxffs_wrle32(uint8_t *dest, uint32_t val);
  392. /****************************************************************************
  393. * Name: nxffs_erased
  394. *
  395. * Description:
  396. * Check if a block of memory is in the erased state.
  397. *
  398. * Input Parameters:
  399. * buffer - Address of the start of the memory to check.
  400. * buflen - The number of bytes to check.
  401. *
  402. * Returned Value:
  403. * The number of erased bytes found at the beginning of the memory region.
  404. *
  405. * Defined in nxffs_util.c
  406. *
  407. ****************************************************************************/
  408. size_t nxffs_erased(FAR const uint8_t *buffer, size_t buflen);
  409. /****************************************************************************
  410. * Name: nxffs_rdcache
  411. *
  412. * Description:
  413. * Read one I/O block into the volume cache memory.
  414. *
  415. * Input Parameters:
  416. * volume - Describes the current volume
  417. * block - The first logical block to read
  418. *
  419. * Returned Value:
  420. * Negated errnos are returned only in the case of MTD reported failures.
  421. * Nothing in the volume data itself will generate errors.
  422. *
  423. * Defined in nxffs_cache.c
  424. *
  425. ****************************************************************************/
  426. int nxffs_rdcache(FAR struct nxffs_volume_s *volume, off_t block);
  427. /****************************************************************************
  428. * Name: nxffs_wrcache
  429. *
  430. * Description:
  431. * Write one or more logical blocks from the volume cache memory.
  432. *
  433. * Input Parameters:
  434. * volume - Describes the current volume
  435. *
  436. * Returned Value:
  437. * Negated errnos are returned only in the case of MTD reported failures.
  438. *
  439. * Defined in nxffs_cache.c
  440. *
  441. ****************************************************************************/
  442. int nxffs_wrcache(FAR struct nxffs_volume_s *volume);
  443. /****************************************************************************
  444. * Name: nxffs_ioseek
  445. *
  446. * Description:
  447. * Seek to a position in FLASH memory. This simply sets up the offsets
  448. * and pointer values. This is a necessary step prior to using
  449. * nxffs_getc().
  450. *
  451. * Input Parameters:
  452. * volume - Describes the NXFFS volume
  453. * offset - The physical offset in bytes from the beginning of the FLASH
  454. * in bytes.
  455. *
  456. * Defined in nxffs_cache.c
  457. *
  458. ****************************************************************************/
  459. void nxffs_ioseek(FAR struct nxffs_volume_s *volume, off_t offset);
  460. /****************************************************************************
  461. * Name: nxffs_iotell
  462. *
  463. * Description:
  464. * Report the current position.
  465. *
  466. * Input Parameters:
  467. * volume - Describes the NXFFS volume
  468. *
  469. * Returned Value:
  470. * The offset from the beginning of FLASH to the current seek position.
  471. *
  472. * Defined in nxffs_cache.c
  473. *
  474. ****************************************************************************/
  475. off_t nxffs_iotell(FAR struct nxffs_volume_s *volume);
  476. /****************************************************************************
  477. * Name: nxffs_getc
  478. *
  479. * Description:
  480. * Get the next byte from FLASH. This function allows the data in the
  481. * formatted FLASH blocks to be read as a continuous byte stream, skipping
  482. * over bad blocks and block headers as necessary.
  483. *
  484. * Input Parameters:
  485. * volume - Describes the NXFFS volume. The parameters ioblock and iooffset
  486. * in the volume structure determine the behavior of nxffs_getc().
  487. * reserve - If less than this much space is available at the end of the
  488. * block, then skip to the next block.
  489. *
  490. * Returned Value:
  491. * Zero is returned on success. Otherwise, a negated errno indicating the
  492. * nature of the failure.
  493. *
  494. * Defined in nxffs_cache.c
  495. *
  496. ****************************************************************************/
  497. int nxffs_getc(FAR struct nxffs_volume_s *volume, uint16_t reserve);
  498. /****************************************************************************
  499. * Name: nxffs_freeentry
  500. *
  501. * Description:
  502. * The inode values returned by nxffs_nextentry() include allocated memory
  503. * (specifically, the file name string). This function should be called
  504. * to dispose of that memory when the inode entry is no longer needed.
  505. *
  506. * Note that the nxffs_entry_s containing structure is not freed. The
  507. * caller may call kmm_free upon return of this function if necessary to
  508. * free the entry container.
  509. *
  510. * Input Parameters:
  511. * entry - The entry to be freed.
  512. *
  513. * Returned Value:
  514. * None
  515. *
  516. * Defined in nxffs_inode.c
  517. *
  518. ****************************************************************************/
  519. void nxffs_freeentry(FAR struct nxffs_entry_s *entry);
  520. /****************************************************************************
  521. * Name: nxffs_nextentry
  522. *
  523. * Description:
  524. * Search for the next valid inode starting at the provided FLASH offset.
  525. *
  526. * Input Parameters:
  527. * volume - Describes the NXFFS volume.
  528. * offset - The FLASH memory offset to begin searching.
  529. * entry - A pointer to memory provided by the caller in which to return
  530. * the inode description.
  531. *
  532. * Returned Value:
  533. * Zero is returned on success. Otherwise, a negated errno is returned
  534. * that indicates the nature of the failure.
  535. *
  536. * Defined in nxffs_inode.c
  537. *
  538. ****************************************************************************/
  539. int nxffs_nextentry(FAR struct nxffs_volume_s *volume, off_t offset,
  540. FAR struct nxffs_entry_s *entry);
  541. /****************************************************************************
  542. * Name: nxffs_findinode
  543. *
  544. * Description:
  545. * Search for an inode with the provided name starting with the first
  546. * valid inode and proceeding to the end FLASH or until the matching
  547. * inode is found.
  548. *
  549. * Input Parameters:
  550. * volume - Describes the NXFFS volume
  551. * name - The name of the inode to find
  552. * entry - The location to return information about the inode.
  553. *
  554. * Returned Value:
  555. * Zero is returned on success. Otherwise, a negated errno is returned
  556. * that indicates the nature of the failure.
  557. *
  558. * Defined in nxffs_inode.c
  559. *
  560. ****************************************************************************/
  561. int nxffs_findinode(FAR struct nxffs_volume_s *volume, FAR const char *name,
  562. FAR struct nxffs_entry_s *entry);
  563. /****************************************************************************
  564. * Name: nxffs_inodeend
  565. *
  566. * Description:
  567. * Return an *approximiate* FLASH offset to end of the inode data. The
  568. * returned value is guaranteed to be be less then or equal to the offset
  569. * of the thing-of-interest in FLASH. Parsing for interesting things
  570. * can begin at that point.
  571. *
  572. * Assumption: The inode header has been verified by the caller and is
  573. * known to contain valid data.
  574. *
  575. * Input Parameters:
  576. * volume - Describes the NXFFS volume
  577. * entry - Describes the inode.
  578. *
  579. * Returned Value:
  580. * A FLASH offset to the (approximate) end of the inode data. No errors
  581. * are detected.
  582. *
  583. * Defined in nxffs_inode.c
  584. *
  585. ****************************************************************************/
  586. off_t nxffs_inodeend(FAR struct nxffs_volume_s *volume,
  587. FAR struct nxffs_entry_s *entry);
  588. /****************************************************************************
  589. * Name: nxffs_verifyblock
  590. *
  591. * Description:
  592. * Assure that the provided (logical) block number is in the block cache
  593. * and that it has a valid block header (i.e., proper magic and
  594. * marked good)
  595. *
  596. * Input Parameters:
  597. * volume - Describes the NXFFS volume
  598. * block - The (logical) block number to load and verify.
  599. *
  600. * Returned Value:
  601. * OK (zero( is returned on success. Otherwise, a negated errno value is
  602. * returned indicating the nature of the failure:
  603. *
  604. * -EIO is returned if we failed to read the block. If we are using
  605. * NAND memory, then this probably means that the block has
  606. * uncorrectable bit errors.
  607. * -ENOENT is returned if the block is a bad block.
  608. *
  609. * Defined in nxffs_block.c
  610. *
  611. ****************************************************************************/
  612. int nxffs_verifyblock(FAR struct nxffs_volume_s *volume, off_t block);
  613. /****************************************************************************
  614. * Name: nxffs_validblock
  615. *
  616. * Description:
  617. * Find the next valid (logical) block in the volume.
  618. *
  619. * Input Parameters:
  620. * volume - Describes the NXFFS volume
  621. * block - On entry, this provides the starting block number. If the
  622. * function is succesfful, then this memory location will hold the
  623. * block number of the next valid block on return.
  624. *
  625. * Returned Value:
  626. * Zero on success otherwise a negated errno value indicating the nature
  627. * of the failure.
  628. *
  629. * Defined in nxffs_block.c
  630. *
  631. ****************************************************************************/
  632. int nxffs_validblock(struct nxffs_volume_s *volume, off_t *block);
  633. /****************************************************************************
  634. * Name: nxffs_blockstats
  635. *
  636. * Description:
  637. * Analyze the NXFFS volume. This operation must be performed when the
  638. * volume is first mounted in order to detect if the volume has been
  639. * formatted and contains a usable NXFFS file system.
  640. *
  641. * Input Parameters:
  642. * volume - Describes the current NXFFS volume.
  643. * stats - On return, will hold nformation describing the state of the
  644. * volume.
  645. *
  646. * Returned Value:
  647. * Negated errnos are returned only in the case of MTD reported failures.
  648. * Nothing in the volume data itself will generate errors.
  649. *
  650. * Defined in nxffs_blockstats.c
  651. *
  652. ****************************************************************************/
  653. int nxffs_blockstats(FAR struct nxffs_volume_s *volume,
  654. FAR struct nxffs_blkstats_s *stats);
  655. /****************************************************************************
  656. * Name: nxffs_reformat
  657. *
  658. * Description:
  659. * Erase and reformat the entire volume. Verify each block and mark
  660. * improperly erased blocks as bad.
  661. *
  662. * Input Parameters:
  663. * volume - Describes the NXFFS volume to be reformatted.
  664. *
  665. * Returned Value:
  666. * Zero on success or a negated errno on a failure. Failures will be
  667. * returned n the case of MTD reported failures o.
  668. * Nothing in the volume data itself will generate errors.
  669. *
  670. * Defined in nxffs_reformat.c
  671. *
  672. ****************************************************************************/
  673. int nxffs_reformat(FAR struct nxffs_volume_s *volume);
  674. /****************************************************************************
  675. * Name: nxffs_blkinit
  676. *
  677. * Description:
  678. * Initialize an NXFFS block to the erased state with the specified block
  679. * status.
  680. *
  681. * Input Parameters:
  682. * volume - Describes the NXFFS volume (needed for the blocksize).
  683. * blkptr - Pointer to the logic block to initialize.
  684. * state - Either BLOCK_STATE_GOOD or BLOCK_STATE_BAD.
  685. *
  686. * Returned Value:
  687. * None.
  688. *
  689. ****************************************************************************/
  690. void nxffs_blkinit(FAR struct nxffs_volume_s *volume, FAR uint8_t *blkptr,
  691. uint8_t state);
  692. /****************************************************************************
  693. * Name: nxffs_findofile
  694. *
  695. * Description:
  696. * Search the list of already opened files to see if the inode of this
  697. * name is one of the opened files.
  698. *
  699. * Input Parameters:
  700. * volume - Describes the NXFFS volume.
  701. * name - The name of the inode to check.
  702. *
  703. * Returned Value:
  704. * If an inode of this name is found in the list of opened inodes, then
  705. * a reference to the open file structure is returned. NULL is returned
  706. * otherwise.
  707. *
  708. * Defined in nxffs_open.c
  709. *
  710. ****************************************************************************/
  711. FAR struct nxffs_ofile_s *nxffs_findofile(FAR struct nxffs_volume_s *volume,
  712. FAR const char *name);
  713. /****************************************************************************
  714. * Name: nxffs_findwriter
  715. *
  716. * Description:
  717. * Search the list of already opened files and return the open file
  718. * instance for the write.
  719. *
  720. * Input Parameters:
  721. * volume - Describes the NXFFS volume.
  722. *
  723. * Returned Value:
  724. * If there is an active writer of the volume, its open file instance is
  725. * returned. NULL is returned otherwise.
  726. *
  727. * Defined in nxffs_open.c
  728. *
  729. ****************************************************************************/
  730. FAR struct nxffs_wrfile_s *nxffs_findwriter(FAR struct nxffs_volume_s *volume);
  731. /****************************************************************************
  732. * Name: nxffs_wrinode
  733. *
  734. * Description:
  735. * Write the inode header (only to FLASH. This is done in two contexts:
  736. *
  737. * 1. When an inode is closed, or
  738. * 2. As part of the file system packing logic when an inode is moved.
  739. *
  740. * Note that in either case, the inode name has already been written to
  741. * FLASH.
  742. *
  743. * Input Parameters:
  744. * volume - Describes the NXFFS volume
  745. * entry - Describes the inode header to write
  746. *
  747. * Returned Value:
  748. * Zero is returned on success; Otherwise, a negated errno value is returned
  749. * indicating the nature of the failure.
  750. *
  751. * Defined in nxffs_open.c
  752. *
  753. ****************************************************************************/
  754. int nxffs_wrinode(FAR struct nxffs_volume_s *volume,
  755. FAR struct nxffs_entry_s *entry);
  756. /****************************************************************************
  757. * Name: nxffs_updateinode
  758. *
  759. * Description:
  760. * The packing logic has moved an inode. Check if any open files are using
  761. * this inode and, if so, move the data in the open file structure as well.
  762. *
  763. * Input Parameters:
  764. * volume - Describes the NXFFS volume
  765. * entry - Describes the new inode entry
  766. *
  767. * Returned Value:
  768. * Zero is returned on success; Otherwise, a negated errno value is returned
  769. * indicating the nature of the failure.
  770. *
  771. ****************************************************************************/
  772. int nxffs_updateinode(FAR struct nxffs_volume_s *volume,
  773. FAR struct nxffs_entry_s *entry);
  774. /****************************************************************************
  775. * Name: nxffs_wrextend
  776. *
  777. * Description:
  778. * Zero-extend a file.
  779. *
  780. * Input Parameters:
  781. * volume - Describes the NXFFS volume
  782. * entry - Describes the new inode entry
  783. * length - The new, extended length of the file
  784. *
  785. * Assumptions:
  786. * The caller holds the NXFFS semaphore.
  787. * The caller has verified that the file is writable.
  788. *
  789. ****************************************************************************/
  790. #ifdef __NO_TRUNCATE_SUPPORT__
  791. int nxffs_wrextend(FAR struct nxffs_volume_s *volume,
  792. FAR struct nxffs_wrfile_s *wrfile, off_t length);
  793. #endif
  794. /****************************************************************************
  795. * Name: nxffs_wrreserve
  796. *
  797. * Description:
  798. * Find a valid location for a file system object of 'size'. A valid
  799. * location will have these properties:
  800. *
  801. * 1. It will lie in the free flash region.
  802. * 2. It will have enough contiguous memory to hold the entire object
  803. * 3. The memory at this location will be fully erased.
  804. *
  805. * This function will only perform the checks of 1) and 2). The
  806. * end-of-filesystem offset, froffset, is update past this memory which,
  807. * in effect, reserves the memory.
  808. *
  809. * Input Parameters:
  810. * volume - Describes the NXFFS volume
  811. * size - The size of the object to be reserved.
  812. *
  813. * Returned Value:
  814. * Zero is returned on success. Otherwise, a negated errno value is
  815. * returned indicating the nature of the failure. Of special interest
  816. * the return error of -ENOSPC which means that the FLASH volume is
  817. * full and should be repacked.
  818. *
  819. * On successful return the following are also valid:
  820. *
  821. * volume->ioblock - Read/write block number of the block containing the
  822. * candidate oject position
  823. * volume->iooffset - The offset in the block to the candidate object
  824. * position.
  825. * volume->froffset - Updated offset to the first free FLASH block after
  826. * the reserved memory.
  827. *
  828. * Defined in nxffs_write.c
  829. *
  830. ****************************************************************************/
  831. int nxffs_wrreserve(FAR struct nxffs_volume_s *volume, size_t size);
  832. /****************************************************************************
  833. * Name: nxffs_wrverify
  834. *
  835. * Description:
  836. * Find a valid location for the object. A valid location will have
  837. * these properties:
  838. *
  839. * 1. It will lie in the free flash region.
  840. * 2. It will have enough contiguous memory to hold the entire header
  841. * (excluding the file name which may lie in the next block).
  842. * 3. The memory at this location will be fully erased.
  843. *
  844. * This function will only perform the check 3). On entry it assumes the
  845. * following settings (left by nxffs_wrreserve()):
  846. *
  847. * volume->ioblock - Read/write block number of the block containing the
  848. * candidate oject position
  849. * volume->iooffset - The offset in the block to the candidate object
  850. * position.
  851. *
  852. * Input Parameters:
  853. * volume - Describes the NXFFS volume
  854. * size - The size of the object to be verifed.
  855. *
  856. * Returned Value:
  857. * Zero is returned on success. Otherwise, a negated errno value is
  858. * returned indicating the nature of the failure. Of special interest
  859. * the return error of -ENOSPC which means that the FLASH volume is
  860. * full and should be repacked.
  861. *
  862. * On successful return the following are also valid:
  863. *
  864. * volume->ioblock - Read/write block number of the block containing the
  865. * verified object position
  866. * volume->iooffset - The offset in the block to the verified object
  867. * position.
  868. * volume->froffset - Updated offset to the first free FLASH block.
  869. *
  870. * Defined in nxffs_write.c
  871. *
  872. ****************************************************************************/
  873. int nxffs_wrverify(FAR struct nxffs_volume_s *volume, size_t size);
  874. /****************************************************************************
  875. * Name: nxffs_wrblkhdr
  876. *
  877. * Description:
  878. * Write the block header information. This is done (1) whenever the end-
  879. * block is encountered and (2) also when the file is closed in order to
  880. * flush the final block of data to FLASH.
  881. *
  882. * Input Parameters:
  883. * volume - Describes the state of the NXFFS volume
  884. * wrfile - Describes the state of the open file
  885. *
  886. * Returned Value:
  887. * Zero is returned on success; Otherwise, a negated errno value is
  888. * returned to indicate the nature of the failure.
  889. *
  890. * Defined in nxffs_write.c
  891. *
  892. ****************************************************************************/
  893. int nxffs_wrblkhdr(FAR struct nxffs_volume_s *volume,
  894. FAR struct nxffs_wrfile_s *wrfile);
  895. /****************************************************************************
  896. * Name: nxffs_nextblock
  897. *
  898. * Description:
  899. * Search for the next valid data block starting at the provided
  900. * FLASH offset.
  901. *
  902. * Input Parameters:
  903. * volume - Describes the NXFFS volume.
  904. * datlen - A memory location to return the data block length.
  905. *
  906. * Returned Value:
  907. * Zero is returned on success. Otherwise, a negated errno is returned
  908. * that indicates the nature of the failure.
  909. *
  910. * Defined in nxffs_read.c
  911. *
  912. ****************************************************************************/
  913. int nxffs_nextblock(FAR struct nxffs_volume_s *volume, off_t offset,
  914. FAR struct nxffs_blkentry_s *blkentry);
  915. /****************************************************************************
  916. * Name: nxffs_rdblkhdr
  917. *
  918. * Description:
  919. * Read and verify the data block header at the specified offset.
  920. *
  921. * Input Parameters:
  922. * volume - Describes the current volume.
  923. * offset - The byte offset from the beginning of FLASH where the data block
  924. * header is expected.
  925. * datlen - A memory location to return the data block length.
  926. *
  927. * Returned Value:
  928. * Zero on success. Otherwise, a negated errno value is returned
  929. * indicating the nature of the failure.
  930. *
  931. * Defined in nxffs_read.c
  932. *
  933. ****************************************************************************/
  934. int nxffs_rdblkhdr(FAR struct nxffs_volume_s *volume, off_t offset,
  935. FAR uint16_t *datlen);
  936. /****************************************************************************
  937. * Name: nxffs_rminode
  938. *
  939. * Description:
  940. * Remove an inode from FLASH. This is the internal implementation of
  941. * the file system unlinke operation.
  942. *
  943. * Input Parameters:
  944. * volume - Describes the NXFFS volume.
  945. * name - the name of the inode to be deleted.
  946. *
  947. * Returned Value:
  948. * Zero is returned if the inode is successfully deleted. Otherwise, a
  949. * negated errno value is returned indicating the nature of the failure.
  950. *
  951. ****************************************************************************/
  952. int nxffs_rminode(FAR struct nxffs_volume_s *volume, FAR const char *name);
  953. /****************************************************************************
  954. * Name: nxffs_pack
  955. *
  956. * Description:
  957. * Pack and re-write the filesystem in order to free up memory at the end
  958. * of FLASH.
  959. *
  960. * Input Parameters:
  961. * volume - The volume to be packed.
  962. *
  963. * Returned Value:
  964. * Zero on success; Otherwise, a negated errno value is returned to
  965. * indicate the nature of the failure.
  966. *
  967. ****************************************************************************/
  968. int nxffs_pack(FAR struct nxffs_volume_s *volume);
  969. /****************************************************************************
  970. * Standard mountpoint operation methods
  971. *
  972. * Description:
  973. * See include/nuttx/fs/fs.h
  974. *
  975. * - nxffs_open() and nxffs_close() are defined in nxffs_open.c
  976. * - nxffs_read() is defined in nxffs_read.c
  977. * - nxffs_write() is defined in nxffs_write.c
  978. * - nxffs_ioctl() is defined in nxffs_ioctl.c
  979. * - nxffs_dup() is defined in nxffs_open.c
  980. * - nxffs_opendir(), nxffs_readdir(), and nxffs_rewindir() are defined in
  981. * nxffs_dirent.c
  982. * - nxffs_bind() and nxffs_unbind() are defined in nxffs_initialize.c
  983. * - nxffs_stat() and nxffs_statfs() are defined in nxffs_stat.c
  984. * - nxffs_unlink() is defined nxffs_unlink.c
  985. *
  986. ****************************************************************************/
  987. struct file; /* Forward references */
  988. struct inode;
  989. struct fs_dirent_s;
  990. struct statfs;
  991. struct stat;
  992. int nxffs_open(FAR struct file *filep, FAR const char *relpath, int oflags,
  993. mode_t mode);
  994. int nxffs_close(FAR struct file *filep);
  995. ssize_t nxffs_read(FAR struct file *filep, FAR char *buffer, size_t buflen);
  996. ssize_t nxffs_write(FAR struct file *filep, FAR const char *buffer,
  997. size_t buflen);
  998. int nxffs_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  999. int nxffs_dup(FAR const struct file *oldp, FAR struct file *newp);
  1000. int nxffs_fstat(FAR const struct file *filep, FAR struct stat *buf);
  1001. #ifdef __NO_TRUNCATE_SUPPORT__
  1002. int nxffs_truncate(FAR struct file *filep, off_t length);
  1003. #endif
  1004. int nxffs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
  1005. FAR struct fs_dirent_s *dir);
  1006. int nxffs_readdir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir);
  1007. int nxffs_rewinddir(FAR struct inode *mountpt, FAR struct fs_dirent_s *dir);
  1008. int nxffs_bind(FAR struct inode *blkdriver, FAR const void *data,
  1009. FAR void **handle);
  1010. int nxffs_unbind(FAR void *handle, FAR struct inode **blkdriver,
  1011. unsigned int flags);
  1012. int nxffs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf);
  1013. int nxffs_stat(FAR struct inode *mountpt, FAR const char *relpath,
  1014. FAR struct stat *buf);
  1015. int nxffs_unlink(FAR struct inode *mountpt, FAR const char *relpath);
  1016. #endif /* __FS_NXFFS_NXFFS_H */