yaffs_guts.h 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895
  1. /*
  2. * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2007 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU Lesser General Public License version 2.1 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14. */
  15. #ifndef __YAFFS_GUTS_H__
  16. #define __YAFFS_GUTS_H__
  17. #include "devextras.h"
  18. #include "yportenv.h"
  19. #define YAFFS_OK 1
  20. #define YAFFS_FAIL 0
  21. /* Give us a Y=0x59,
  22. * Give us an A=0x41,
  23. * Give us an FF=0xFF
  24. * Give us an S=0x53
  25. * And what have we got...
  26. */
  27. #define YAFFS_MAGIC 0x5941FF53
  28. #define YAFFS_NTNODES_LEVEL0 16
  29. #define YAFFS_TNODES_LEVEL0_BITS 4
  30. #define YAFFS_TNODES_LEVEL0_MASK 0xf
  31. #define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  32. #define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  33. #define YAFFS_TNODES_INTERNAL_MASK 0x7
  34. #define YAFFS_TNODES_MAX_LEVEL 6
  35. #ifndef CONFIG_YAFFS_NO_YAFFS1
  36. #define YAFFS_BYTES_PER_SPARE 16
  37. #define YAFFS_BYTES_PER_CHUNK 512
  38. #define YAFFS_CHUNK_SIZE_SHIFT 9
  39. #define YAFFS_CHUNKS_PER_BLOCK 32
  40. #define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  41. #endif
  42. #define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  43. #define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  44. #define YAFFS_MAX_CHUNK_ID 0x000FFFFF
  45. #define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
  46. #define YAFFS_ALLOCATION_NOBJECTS 100
  47. #define YAFFS_ALLOCATION_NTNODES 100
  48. #define YAFFS_ALLOCATION_NLINKS 100
  49. #define YAFFS_NOBJECT_BUCKETS 256
  50. #define YAFFS_OBJECT_SPACE 0x40000
  51. #define YAFFS_CHECKPOINT_VERSION 3
  52. #ifdef CONFIG_YAFFS_UNICODE
  53. #define YAFFS_MAX_NAME_LENGTH 127
  54. #define YAFFS_MAX_ALIAS_LENGTH 79
  55. #else
  56. #define YAFFS_MAX_NAME_LENGTH 255
  57. #define YAFFS_MAX_ALIAS_LENGTH 159
  58. #endif
  59. #define YAFFS_SHORT_NAME_LENGTH 15
  60. /* Some special object ids for pseudo objects */
  61. #define YAFFS_OBJECTID_ROOT 1
  62. #define YAFFS_OBJECTID_LOSTNFOUND 2
  63. #define YAFFS_OBJECTID_UNLINKED 3
  64. #define YAFFS_OBJECTID_DELETED 4
  65. /* Sseudo object ids for checkpointing */
  66. #define YAFFS_OBJECTID_SB_HEADER 0x10
  67. #define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  68. #define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  69. /* */
  70. #define YAFFS_MAX_SHORT_OP_CACHES 20
  71. #define YAFFS_N_TEMP_BUFFERS 4
  72. /* Sequence numbers are used in YAFFS2 to determine block allocation order.
  73. * The range is limited slightly to help distinguish bad numbers from good.
  74. * This also allows us to perhaps in the future use special numbers for
  75. * special purposes.
  76. * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
  77. * and is a larger number than the lifetime of a 2GB device.
  78. */
  79. #define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  80. #define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
  81. /* ChunkCache is used for short read/write operations.*/
  82. typedef struct {
  83. struct yaffs_ObjectStruct *object;
  84. int chunkId;
  85. int lastUse;
  86. int dirty;
  87. int nBytes; /* Only valid if the cache is dirty */
  88. int locked; /* Can't push out or flush while locked. */
  89. #ifdef CONFIG_YAFFS_YAFFS2
  90. __u8 *data;
  91. #else
  92. __u8 data[YAFFS_BYTES_PER_CHUNK];
  93. #endif
  94. } yaffs_ChunkCache;
  95. /* Tags structures in RAM
  96. * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
  97. * the structure size will get blown out.
  98. */
  99. #ifndef CONFIG_YAFFS_NO_YAFFS1
  100. typedef struct {
  101. unsigned chunkId:20;
  102. unsigned serialNumber:2;
  103. unsigned byteCount:10;
  104. unsigned objectId:18;
  105. unsigned ecc:12;
  106. unsigned unusedStuff:2;
  107. } yaffs_Tags;
  108. typedef union {
  109. yaffs_Tags asTags;
  110. __u8 asBytes[8];
  111. } yaffs_TagsUnion;
  112. #endif
  113. /* Stuff used for extended tags in YAFFS2 */
  114. typedef enum {
  115. YAFFS_ECC_RESULT_UNKNOWN,
  116. YAFFS_ECC_RESULT_NO_ERROR,
  117. YAFFS_ECC_RESULT_FIXED,
  118. YAFFS_ECC_RESULT_UNFIXED
  119. } yaffs_ECCResult;
  120. typedef enum {
  121. YAFFS_OBJECT_TYPE_UNKNOWN,
  122. YAFFS_OBJECT_TYPE_FILE,
  123. YAFFS_OBJECT_TYPE_SYMLINK,
  124. YAFFS_OBJECT_TYPE_DIRECTORY,
  125. YAFFS_OBJECT_TYPE_HARDLINK,
  126. YAFFS_OBJECT_TYPE_SPECIAL
  127. } yaffs_ObjectType;
  128. #define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  129. typedef struct {
  130. unsigned validMarker0;
  131. unsigned chunkUsed; /* Status of the chunk: used or unused */
  132. unsigned objectId; /* If 0 then this is not part of an object (unused) */
  133. unsigned chunkId; /* If 0 then this is a header, else a data chunk */
  134. unsigned byteCount; /* Only valid for data chunks */
  135. /* The following stuff only has meaning when we read */
  136. yaffs_ECCResult eccResult;
  137. unsigned blockBad;
  138. /* YAFFS 1 stuff */
  139. unsigned chunkDeleted; /* The chunk is marked deleted */
  140. unsigned serialNumber; /* Yaffs1 2-bit serial number */
  141. /* YAFFS2 stuff */
  142. unsigned sequenceNumber; /* The sequence number of this block */
  143. /* Extra info if this is an object header (YAFFS2 only) */
  144. unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
  145. unsigned extraParentObjectId; /* The parent object */
  146. unsigned extraIsShrinkHeader; /* Is it a shrink header? */
  147. unsigned extraShadows; /* Does this shadow another object? */
  148. yaffs_ObjectType extraObjectType; /* What object type? */
  149. unsigned extraFileLength; /* Length if it is a file */
  150. unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
  151. unsigned validMarker1;
  152. } yaffs_ExtendedTags;
  153. /* Spare structure for YAFFS1 */
  154. typedef struct {
  155. __u8 tagByte0;
  156. __u8 tagByte1;
  157. __u8 tagByte2;
  158. __u8 tagByte3;
  159. __u8 pageStatus; /* set to 0 to delete the chunk */
  160. __u8 blockStatus;
  161. __u8 tagByte4;
  162. __u8 tagByte5;
  163. __u8 ecc1[3];
  164. __u8 tagByte6;
  165. __u8 tagByte7;
  166. __u8 ecc2[3];
  167. } yaffs_Spare;
  168. /*Special structure for passing through to mtd */
  169. struct yaffs_NANDSpare {
  170. yaffs_Spare spare;
  171. int eccres1;
  172. int eccres2;
  173. };
  174. /* Block data in RAM */
  175. typedef enum {
  176. YAFFS_BLOCK_STATE_UNKNOWN = 0,
  177. YAFFS_BLOCK_STATE_SCANNING,
  178. YAFFS_BLOCK_STATE_NEEDS_SCANNING,
  179. /* The block might have something on it (ie it is allocating or full, perhaps empty)
  180. * but it needs to be scanned to determine its true state.
  181. * This state is only valid during yaffs_Scan.
  182. * NB We tolerate empty because the pre-scanner might be incapable of deciding
  183. * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
  184. */
  185. YAFFS_BLOCK_STATE_EMPTY,
  186. /* This block is empty */
  187. YAFFS_BLOCK_STATE_ALLOCATING,
  188. /* This block is partially allocated.
  189. * At least one page holds valid data.
  190. * This is the one currently being used for page
  191. * allocation. Should never be more than one of these
  192. */
  193. YAFFS_BLOCK_STATE_FULL,
  194. /* All the pages in this block have been allocated.
  195. */
  196. YAFFS_BLOCK_STATE_DIRTY,
  197. /* All pages have been allocated and deleted.
  198. * Erase me, reuse me.
  199. */
  200. YAFFS_BLOCK_STATE_CHECKPOINT,
  201. /* This block is assigned to holding checkpoint data.
  202. */
  203. YAFFS_BLOCK_STATE_COLLECTING,
  204. /* This block is being garbage collected */
  205. YAFFS_BLOCK_STATE_DEAD
  206. /* This block has failed and is not in use */
  207. } yaffs_BlockState;
  208. #define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  209. typedef struct {
  210. int softDeletions:10; /* number of soft deleted pages */
  211. int pagesInUse:10; /* number of pages in use */
  212. __u32 blockState:4; /* One of the above block states. NB use unsigned because enum is sometimes an int */
  213. __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
  214. /* and retire the block. */
  215. __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
  216. __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
  217. It should be prioritised for GC */
  218. __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
  219. #ifdef CONFIG_YAFFS_YAFFS2
  220. __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
  221. __u32 sequenceNumber; /* block sequence number for yaffs2 */
  222. #endif
  223. } yaffs_BlockInfo;
  224. /* -------------------------- Object structure -------------------------------*/
  225. /* This is the object structure as stored on NAND */
  226. typedef struct {
  227. yaffs_ObjectType type;
  228. /* Apply to everything */
  229. int parentObjectId;
  230. __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
  231. YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  232. /* Thes following apply to directories, files, symlinks - not hard links */
  233. __u32 yst_mode; /* protection */
  234. #ifdef CONFIG_YAFFS_WINCE
  235. __u32 notForWinCE[5];
  236. #else
  237. __u32 yst_uid;
  238. __u32 yst_gid;
  239. __u32 yst_atime;
  240. __u32 yst_mtime;
  241. __u32 yst_ctime;
  242. #endif
  243. /* File size applies to files only */
  244. int fileSize;
  245. /* Equivalent object id applies to hard links only. */
  246. int equivalentObjectId;
  247. /* Alias is for symlinks only. */
  248. YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  249. __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
  250. #ifdef CONFIG_YAFFS_WINCE
  251. __u32 win_ctime[2];
  252. __u32 win_atime[2];
  253. __u32 win_mtime[2];
  254. __u32 roomToGrow[4];
  255. #else
  256. __u32 roomToGrow[10];
  257. #endif
  258. int shadowsObject; /* This object header shadows the specified object if > 0 */
  259. /* isShrink applies to object headers written when we shrink the file (ie resize) */
  260. __u32 isShrink;
  261. } yaffs_ObjectHeader;
  262. /*--------------------------- Tnode -------------------------- */
  263. union yaffs_Tnode_union {
  264. #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
  265. union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
  266. #else
  267. union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
  268. #endif
  269. /* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
  270. };
  271. typedef union yaffs_Tnode_union yaffs_Tnode;
  272. struct yaffs_TnodeList_struct {
  273. struct yaffs_TnodeList_struct *next;
  274. yaffs_Tnode *tnodes;
  275. };
  276. typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
  277. /*------------------------ Object -----------------------------*/
  278. /* An object can be one of:
  279. * - a directory (no data, has children links
  280. * - a regular file (data.... not prunes :->).
  281. * - a symlink [symbolic link] (the alias).
  282. * - a hard link
  283. */
  284. typedef struct {
  285. __u32 fileSize;
  286. __u32 scannedFileSize;
  287. __u32 shrinkSize;
  288. int topLevel;
  289. yaffs_Tnode *top;
  290. } yaffs_FileStructure;
  291. typedef struct {
  292. struct list_head children; /* list of child links */
  293. } yaffs_DirectoryStructure;
  294. typedef struct {
  295. YCHAR *alias;
  296. } yaffs_SymLinkStructure;
  297. typedef struct {
  298. struct yaffs_ObjectStruct *equivalentObject;
  299. __u32 equivalentObjectId;
  300. } yaffs_HardLinkStructure;
  301. typedef union {
  302. yaffs_FileStructure fileVariant;
  303. yaffs_DirectoryStructure directoryVariant;
  304. yaffs_SymLinkStructure symLinkVariant;
  305. yaffs_HardLinkStructure hardLinkVariant;
  306. } yaffs_ObjectVariant;
  307. struct yaffs_ObjectStruct {
  308. __u8 deleted:1; /* This should only apply to unlinked files. */
  309. __u8 softDeleted:1; /* it has also been soft deleted */
  310. __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
  311. __u8 fake:1; /* A fake object has no presence on NAND. */
  312. __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
  313. __u8 unlinkAllowed:1;
  314. __u8 dirty:1; /* the object needs to be written to flash */
  315. __u8 valid:1; /* When the file system is being loaded up, this
  316. * object might be created before the data
  317. * is available (ie. file data records appear before the header).
  318. */
  319. __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
  320. __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
  321. * still in the inode cache. Free of object is defered.
  322. * until the inode is released.
  323. */
  324. __u8 serial; /* serial number of chunk in NAND. Cached here */
  325. __u16 sum; /* sum of the name to speed searching */
  326. struct yaffs_DeviceStruct *myDev; /* The device I'm on */
  327. struct list_head hashLink; /* list of objects in this hash bucket */
  328. struct list_head hardLinks; /* all the equivalent hard linked objects */
  329. /* directory structure stuff */
  330. /* also used for linking up the free list */
  331. struct yaffs_ObjectStruct *parent;
  332. struct list_head siblings;
  333. /* Where's my object header in NAND? */
  334. int chunkId;
  335. int nDataChunks; /* Number of data chunks attached to the file. */
  336. __u32 objectId; /* the object id value */
  337. __u32 yst_mode;
  338. #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
  339. YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
  340. #endif
  341. #ifndef __KERNEL__
  342. __u32 inUse;
  343. #endif
  344. #ifdef CONFIG_YAFFS_WINCE
  345. __u32 win_ctime[2];
  346. __u32 win_mtime[2];
  347. __u32 win_atime[2];
  348. #else
  349. __u32 yst_uid;
  350. __u32 yst_gid;
  351. __u32 yst_atime;
  352. __u32 yst_mtime;
  353. __u32 yst_ctime;
  354. #endif
  355. __u32 yst_rdev;
  356. #ifdef __KERNEL__
  357. struct inode *myInode;
  358. #endif
  359. yaffs_ObjectType variantType;
  360. yaffs_ObjectVariant variant;
  361. };
  362. typedef struct yaffs_ObjectStruct yaffs_Object;
  363. struct yaffs_ObjectList_struct {
  364. yaffs_Object *objects;
  365. struct yaffs_ObjectList_struct *next;
  366. };
  367. typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
  368. typedef struct {
  369. struct list_head list;
  370. int count;
  371. } yaffs_ObjectBucket;
  372. /* yaffs_CheckpointObject holds the definition of an object as dumped
  373. * by checkpointing.
  374. */
  375. typedef struct {
  376. int structType;
  377. __u32 objectId;
  378. __u32 parentId;
  379. int chunkId;
  380. yaffs_ObjectType variantType:3;
  381. __u8 deleted:1;
  382. __u8 softDeleted:1;
  383. __u8 unlinked:1;
  384. __u8 fake:1;
  385. __u8 renameAllowed:1;
  386. __u8 unlinkAllowed:1;
  387. __u8 serial;
  388. int nDataChunks;
  389. __u32 fileSizeOrEquivalentObjectId;
  390. }yaffs_CheckpointObject;
  391. /*--------------------- Temporary buffers ----------------
  392. *
  393. * These are chunk-sized working buffers. Each device has a few
  394. */
  395. typedef struct {
  396. __u8 *buffer;
  397. int line; /* track from whence this buffer was allocated */
  398. int maxLine;
  399. } yaffs_TempBuffer;
  400. /*----------------- Device ---------------------------------*/
  401. struct yaffs_DeviceStruct {
  402. struct list_head devList;
  403. const char *name;
  404. /* Entry parameters set up way early. Yaffs sets up the rest.*/
  405. int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
  406. int nChunksPerBlock; /* does not need to be a power of 2 */
  407. int nBytesPerSpare; /* spare area size */
  408. int startBlock; /* Start block we're allowed to use */
  409. int endBlock; /* End block we're allowed to use */
  410. int nReservedBlocks; /* We want this tuneable so that we can reduce */
  411. /* reserved blocks on NOR and RAM. */
  412. /* Stuff used by the shared space checkpointing mechanism */
  413. /* If this value is zero, then this mechanism is disabled */
  414. int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
  415. int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
  416. * the number of short op caches (don't use too many)
  417. */
  418. int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
  419. int useNANDECC; /* Flag to decide whether or not to use NANDECC */
  420. void *genericDevice; /* Pointer to device context
  421. * On an mtd this holds the mtd pointer.
  422. */
  423. void *superBlock;
  424. /* NAND access functions (Must be set before calling YAFFS)*/
  425. int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
  426. int chunkInNAND, const __u8 * data,
  427. const yaffs_Spare * spare);
  428. int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
  429. int chunkInNAND, __u8 * data,
  430. yaffs_Spare * spare);
  431. int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
  432. int blockInNAND);
  433. int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
  434. #ifdef CONFIG_YAFFS_YAFFS2
  435. int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
  436. int chunkInNAND, const __u8 * data,
  437. const yaffs_ExtendedTags * tags);
  438. int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
  439. int chunkInNAND, __u8 * data,
  440. yaffs_ExtendedTags * tags);
  441. int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
  442. int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
  443. yaffs_BlockState * state, int *sequenceNumber);
  444. #endif
  445. int isYaffs2;
  446. /* The removeObjectCallback function must be supplied by OS flavours that
  447. * need it. The Linux kernel does not use this, but yaffs direct does use
  448. * it to implement the faster readdir
  449. */
  450. void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
  451. /* Callback to mark the superblock dirsty */
  452. void (*markSuperBlockDirty)(void * superblock);
  453. int wideTnodesDisabled; /* Set to disable wide tnodes */
  454. /* End of stuff that must be set before initialisation. */
  455. /* Checkpoint control. Can be set before or after initialisation */
  456. __u8 skipCheckpointRead;
  457. __u8 skipCheckpointWrite;
  458. /* Runtime parameters. Set up by YAFFS. */
  459. __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
  460. __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
  461. /* Stuff to support wide tnodes */
  462. __u32 tnodeWidth;
  463. __u32 tnodeMask;
  464. /* Stuff to support various file offses to chunk/offset translations */
  465. /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
  466. __u32 crumbMask;
  467. __u32 crumbShift;
  468. __u32 crumbsPerChunk;
  469. /* Straight shifting for nDataBytesPerChunk being a power of 2 */
  470. __u32 chunkShift;
  471. __u32 chunkMask;
  472. #ifdef __KERNEL__
  473. struct semaphore sem; /* Semaphore for waiting on erasure.*/
  474. struct semaphore grossLock; /* Gross locking semaphore */
  475. __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
  476. * at compile time so we have to allocate it.
  477. */
  478. void (*putSuperFunc) (struct super_block * sb);
  479. #endif
  480. int isMounted;
  481. int isCheckpointed;
  482. /* Stuff to support block offsetting to support start block zero */
  483. int internalStartBlock;
  484. int internalEndBlock;
  485. int blockOffset;
  486. int chunkOffset;
  487. /* Runtime checkpointing stuff */
  488. int checkpointPageSequence; /* running sequence number of checkpoint pages */
  489. int checkpointByteCount;
  490. int checkpointByteOffset;
  491. __u8 *checkpointBuffer;
  492. int checkpointOpenForWrite;
  493. int blocksInCheckpoint;
  494. int checkpointCurrentChunk;
  495. int checkpointCurrentBlock;
  496. int checkpointNextBlock;
  497. int *checkpointBlockList;
  498. int checkpointMaxBlocks;
  499. __u32 checkpointSum;
  500. __u32 checkpointXor;
  501. /* Block Info */
  502. yaffs_BlockInfo *blockInfo;
  503. __u8 *chunkBits; /* bitmap of chunks in use */
  504. unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
  505. unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
  506. int chunkBitmapStride; /* Number of bytes of chunkBits per block.
  507. * Must be consistent with nChunksPerBlock.
  508. */
  509. int nErasedBlocks;
  510. int allocationBlock; /* Current block being allocated off */
  511. __u32 allocationPage;
  512. int allocationBlockFinder; /* Used to search for next allocation block */
  513. /* Runtime state */
  514. int nTnodesCreated;
  515. yaffs_Tnode *freeTnodes;
  516. int nFreeTnodes;
  517. yaffs_TnodeList *allocatedTnodeList;
  518. int isDoingGC;
  519. int nObjectsCreated;
  520. yaffs_Object *freeObjects;
  521. int nFreeObjects;
  522. yaffs_ObjectList *allocatedObjectList;
  523. yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
  524. int nFreeChunks;
  525. int currentDirtyChecker; /* Used to find current dirtiest block */
  526. __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
  527. /* Statistcs */
  528. int nPageWrites;
  529. int nPageReads;
  530. int nBlockErasures;
  531. int nErasureFailures;
  532. int nGCCopies;
  533. int garbageCollections;
  534. int passiveGarbageCollections;
  535. int nRetriedWrites;
  536. int nRetiredBlocks;
  537. int eccFixed;
  538. int eccUnfixed;
  539. int tagsEccFixed;
  540. int tagsEccUnfixed;
  541. int nDeletions;
  542. int nUnmarkedDeletions;
  543. int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
  544. /* Special directories */
  545. yaffs_Object *rootDir;
  546. yaffs_Object *lostNFoundDir;
  547. /* Buffer areas for storing data to recover from write failures TODO
  548. * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
  549. * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
  550. */
  551. int bufferedBlock; /* Which block is buffered here? */
  552. int doingBufferedBlockRewrite;
  553. yaffs_ChunkCache *srCache;
  554. int srLastUse;
  555. int cacheHits;
  556. /* Stuff for background deletion and unlinked files.*/
  557. yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
  558. yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
  559. yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
  560. int nDeletedFiles; /* Count of files awaiting deletion;*/
  561. int nUnlinkedFiles; /* Count of unlinked files. */
  562. int nBackgroundDeletions; /* Count of background deletions. */
  563. yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
  564. int maxTemp;
  565. int unmanagedTempAllocations;
  566. int unmanagedTempDeallocations;
  567. /* yaffs2 runtime stuff */
  568. unsigned sequenceNumber; /* Sequence number of currently allocating block */
  569. unsigned oldestDirtySequence;
  570. };
  571. typedef struct yaffs_DeviceStruct yaffs_Device;
  572. /* The static layout of bllock usage etc is stored in the super block header */
  573. typedef struct {
  574. int StructType;
  575. int version;
  576. int checkpointStartBlock;
  577. int checkpointEndBlock;
  578. int startBlock;
  579. int endBlock;
  580. int rfu[100];
  581. } yaffs_SuperBlockHeader;
  582. /* The CheckpointDevice structure holds the device information that changes at runtime and
  583. * must be preserved over unmount/mount cycles.
  584. */
  585. typedef struct {
  586. int structType;
  587. int nErasedBlocks;
  588. int allocationBlock; /* Current block being allocated off */
  589. __u32 allocationPage;
  590. int nFreeChunks;
  591. int nDeletedFiles; /* Count of files awaiting deletion;*/
  592. int nUnlinkedFiles; /* Count of unlinked files. */
  593. int nBackgroundDeletions; /* Count of background deletions. */
  594. /* yaffs2 runtime stuff */
  595. unsigned sequenceNumber; /* Sequence number of currently allocating block */
  596. unsigned oldestDirtySequence;
  597. } yaffs_CheckpointDevice;
  598. typedef struct {
  599. int structType;
  600. __u32 magic;
  601. __u32 version;
  602. __u32 head;
  603. } yaffs_CheckpointValidity;
  604. /* Function to manipulate block info */
  605. static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
  606. {
  607. if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
  608. T(YAFFS_TRACE_ERROR,
  609. (TSTR
  610. ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
  611. blk));
  612. YBUG();
  613. }
  614. return &dev->blockInfo[blk - dev->internalStartBlock];
  615. }
  616. /*----------------------- YAFFS Functions -----------------------*/
  617. int yaffs_GutsInitialise(yaffs_Device * dev);
  618. void yaffs_Deinitialise(yaffs_Device * dev);
  619. int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
  620. int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
  621. yaffs_Object * newDir, const YCHAR * newName);
  622. int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
  623. int yaffs_DeleteFile(yaffs_Object * obj);
  624. int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
  625. int yaffs_GetObjectFileLength(yaffs_Object * obj);
  626. int yaffs_GetObjectInode(yaffs_Object * obj);
  627. unsigned yaffs_GetObjectType(yaffs_Object * obj);
  628. int yaffs_GetObjectLinkCount(yaffs_Object * obj);
  629. int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
  630. int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
  631. /* File operations */
  632. int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
  633. int nBytes);
  634. int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
  635. int nBytes, int writeThrough);
  636. int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
  637. yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
  638. __u32 mode, __u32 uid, __u32 gid);
  639. int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
  640. /* Flushing and checkpointing */
  641. void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
  642. int yaffs_CheckpointSave(yaffs_Device *dev);
  643. int yaffs_CheckpointRestore(yaffs_Device *dev);
  644. /* Directory operations */
  645. yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
  646. __u32 mode, __u32 uid, __u32 gid);
  647. yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
  648. int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
  649. int (*fn) (yaffs_Object *));
  650. yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
  651. /* Link operations */
  652. yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
  653. yaffs_Object * equivalentObject);
  654. yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
  655. /* Symlink operations */
  656. yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
  657. __u32 mode, __u32 uid, __u32 gid,
  658. const YCHAR * alias);
  659. YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
  660. /* Special inodes (fifos, sockets and devices) */
  661. yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
  662. __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
  663. /* Special directories */
  664. yaffs_Object *yaffs_Root(yaffs_Device * dev);
  665. yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
  666. #ifdef CONFIG_YAFFS_WINCE
  667. /* CONFIG_YAFFS_WINCE special stuff */
  668. void yfsd_WinFileTimeNow(__u32 target[2]);
  669. #endif
  670. #ifdef __KERNEL__
  671. void yaffs_HandleDeferedFree(yaffs_Object * obj);
  672. #endif
  673. /* Debug dump */
  674. int yaffs_DumpObject(yaffs_Object * obj);
  675. void yaffs_GutsTest(yaffs_Device * dev);
  676. /* A few useful functions */
  677. void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
  678. void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
  679. int yaffs_CheckFF(__u8 * buffer, int nBytes);
  680. void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
  681. #endif