Change st_xxx to yst_xxx to fix compilation issues
[yaffs/.git] / yaffs_guts.h
1 /*
2  * YAFFS: Yet another FFS. A NAND-flash specific file system. 
3  * yaffs_guts.h: Configuration etc for yaffs_guts
4  *
5  * Copyright (C) 2002 Aleph One Ltd.
6  *   for Toby Churchill Ltd and Brightstar Engineering
7  *
8  * Created by Charles Manning <charles@aleph1.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU Lesser General Public License version 2.1 as
12  * published by the Free Software Foundation.
13  *
14  *
15  * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
16  *
17  * $Id: yaffs_guts.h,v 1.21 2005-10-11 23:43:27 charles Exp $
18  */
19
20 #ifndef __YAFFS_GUTS_H__
21 #define __YAFFS_GUTS_H__
22
23 #include "yportenv.h"
24
25 #define YAFFS_OK        1
26 #define YAFFS_FAIL  0
27
28 // Give us a Y=0x59, 
29 // Give us an A=0x41, 
30 // Give us an FF=0xFF 
31 // Give us an S=0x53
32 // And what have we got... 
33 #define YAFFS_MAGIC                                     0x5941FF53
34
35 #define YAFFS_NTNODES_LEVEL0            16
36 #define YAFFS_TNODES_LEVEL0_BITS        4
37 #define YAFFS_TNODES_LEVEL0_MASK        0xf
38
39 #define YAFFS_NTNODES_INTERNAL          (YAFFS_NTNODES_LEVEL0 / 2)
40 #define YAFFS_TNODES_INTERNAL_BITS      (YAFFS_TNODES_LEVEL0_BITS - 1)
41 #define YAFFS_TNODES_INTERNAL_MASK      0x7
42 #define YAFFS_TNODES_MAX_LEVEL          6
43                 
44 #define YAFFS_BYTES_PER_SPARE           16
45
46 #define YAFFS_BYTES_PER_CHUNK           512
47 //#define YAFFS_CHUNK_SIZE_SHIFT                9
48
49
50 #define YAFFS_CHUNKS_PER_BLOCK          32
51 #define YAFFS_BYTES_PER_BLOCK           (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
52
53 #define YAFFS_MAX_CHUNK_ID                      0x000FFFFF
54
55 #define YAFFS_UNUSED_OBJECT_ID          0x0003FFFF
56
57 #define YAFFS_ALLOCATION_NOBJECTS       100
58 #define YAFFS_ALLOCATION_NTNODES        100
59 #define YAFFS_ALLOCATION_NLINKS         100
60
61 #define YAFFS_NOBJECT_BUCKETS           256
62
63
64 #define YAFFS_OBJECT_SPACE                      0x40000
65 #define YAFFS_MAX_NAME_LENGTH           255
66 #define YAFFS_SHORT_NAME_LENGTH         15
67
68 #define YAFFS_MAX_ALIAS_LENGTH          159
69
70 #define YAFFS_OBJECTID_ROOT                     1
71 #define YAFFS_OBJECTID_LOSTNFOUND       2
72 #define YAFFS_OBJECTID_UNLINKED         3
73
74 #define YAFFS_MAX_SHORT_OP_CACHES       20
75
76
77 // ChunkCache is used for short read/write operations.
78 typedef struct
79 {
80         struct yaffs_ObjectStruct *object;
81         int chunkId;
82         int lastUse;
83         int dirty;      
84         int nBytes;     // Only valid if the cache is dirty
85         __u8 data[YAFFS_BYTES_PER_CHUNK];
86 } yaffs_ChunkCache;
87
88 // Tags structures in RAM
89 // NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
90 // the structure size will get blown out.
91
92 typedef struct
93 {
94     unsigned chunkId:20;
95     unsigned serialNumber:2;
96     unsigned byteCount:10;
97     unsigned objectId:18;
98     unsigned ecc:12;
99     unsigned unusedStuff:2;
100 } yaffs_Tags;
101
102 typedef union
103 {
104     yaffs_Tags asTags;
105     __u8       asBytes[8];
106 } yaffs_TagsUnion;
107
108
109 // Spare structure
110 typedef struct
111 {
112     __u8  tagByte0;
113     __u8  tagByte1;
114     __u8  tagByte2;
115     __u8  tagByte3;
116     __u8  pageStatus;   // set to 0 to delete the chunk
117     __u8  blockStatus;
118     __u8  tagByte4;
119     __u8  tagByte5;
120     __u8  ecc1[3];
121     __u8  tagByte6;
122     __u8  tagByte7;
123     __u8  ecc2[3];
124 } yaffs_Spare;
125
126 //Special structure for passing through to mtd
127 struct yaffs_NANDSpare {
128         yaffs_Spare     spare;
129         int             eccres1;
130         int             eccres2;
131 };
132
133 // Block data in RAM
134
135 typedef enum {
136         YAFFS_BLOCK_STATE_UNKNOWN       = 0,
137         YAFFS_BLOCK_STATE_SCANNING,             // Used while the block is being scanned.
138                                                                         // NB Don't erase blocks while they're being scanned
139         
140         YAFFS_BLOCK_STATE_EMPTY,                // This block is empty
141         
142         YAFFS_BLOCK_STATE_ALLOCATING,   // This block is partially allocated. 
143                                                                         // This is the one currently being used for page
144                                                                         // allocation. Should never be more than one of these
145                                                         
146
147         YAFFS_BLOCK_STATE_FULL,                 // All the pages in this block have been allocated.
148                                                                         // At least one page holds valid data.
149                                                          
150         YAFFS_BLOCK_STATE_DIRTY,                // All pages have been allocated and deleted. 
151                                                                         // Erase me, reuse me.
152                                                         
153         YAFFS_BLOCK_STATE_DEAD                  // This block has failed and is not in use
154
155 } yaffs_BlockState;
156
157
158
159
160 typedef struct
161 {
162 #ifndef CONFIG_YAFFS_NO_YAFFS2
163         __u32 sequenceNumber;   // block sequence number for yaffs2
164 #endif
165         int   softDeletions:12; // number of soft deleted pages
166     int   pagesInUse:12;        // number of pages in use
167     __u32 blockState:4;         // One of the above block states
168     __u32 needsRetiring:1;      // Data has failed on this block, need to get valid data off
169                                                 // and retire the block.
170 } yaffs_BlockInfo;
171
172
173 //////////////////// Object structure ///////////////////////////
174 // This is the object structure as stored on NAND
175
176 typedef enum
177 {
178         YAFFS_OBJECT_TYPE_UNKNOWN,
179         YAFFS_OBJECT_TYPE_FILE,
180         YAFFS_OBJECT_TYPE_SYMLINK,
181         YAFFS_OBJECT_TYPE_DIRECTORY,
182         YAFFS_OBJECT_TYPE_HARDLINK,
183         YAFFS_OBJECT_TYPE_SPECIAL
184 } yaffs_ObjectType;
185
186 typedef struct
187 {
188         yaffs_ObjectType type;
189
190         // Apply to everything  
191         int   parentObjectId;
192         __u16 sum__NoLongerUsed;        // checksum of name. Calc this off the name to prevent inconsistencies
193         char  name[YAFFS_MAX_NAME_LENGTH + 1];
194
195         // Thes following apply to directories, files, symlinks - not hard links
196         __u32 yst_mode;  // protection
197
198 #ifdef CONFIG_YAFFS_WINCE
199         __u32 notForWinCE[5];
200 #else
201         __u32 yst_uid;   // user ID of owner
202         __u32 yst_gid;    // group ID of owner 
203         __u32 yst_atime; // time of last access
204         __u32 yst_mtime; // time of last modification
205         __u32 yst_ctime; // time of last change
206 #endif
207
208         // File size  applies to files only
209         int fileSize; 
210                 
211         // Equivalent object id applies to hard links only.
212         int  equivalentObjectId;
213         
214         // Alias is for symlinks only.
215         char alias[YAFFS_MAX_ALIAS_LENGTH + 1];
216         
217         __u32 yst_rdev;  // device stuff for block and char devices (maj/min)
218         
219 #ifdef CONFIG_YAFFS_WINCE
220         __u32 win_ctime[2];
221         __u32 win_atime[2];
222         __u32 win_mtime[2];
223         __u32 roomToGrow[6];
224 #else
225         __u32 roomToGrow[12];
226 #endif
227         
228 } yaffs_ObjectHeader;
229
230
231
232 ////////////////////  Tnode ///////////////////////////
233
234 union yaffs_Tnode_union
235 {
236 #ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
237         union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL+1];
238 #else
239         union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
240 #endif
241         __u16 level0[YAFFS_NTNODES_LEVEL0];
242         
243 };
244
245 typedef union yaffs_Tnode_union yaffs_Tnode;
246
247 struct yaffs_TnodeList_struct
248 {
249         struct yaffs_TnodeList_struct *next;
250         yaffs_Tnode *tnodes;
251 };
252
253 typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
254
255
256
257 ///////////////////  Object ////////////////////////////////
258 // An object can be one of:
259 // - a directory (no data, has children links
260 // - a regular file (data.... not prunes :->).
261 // - a symlink [symbolic link] (the alias).
262 // - a hard link
263
264
265 typedef struct 
266 {
267         __u32 fileSize;
268         __u32 scannedFileSize;
269         int   topLevel;
270         yaffs_Tnode *top;
271 } yaffs_FileStructure;
272
273 typedef struct
274 {
275         struct list_head children; // list of child links
276 } yaffs_DirectoryStructure;
277
278 typedef struct
279 {
280         char *alias;
281 } yaffs_SymLinkStructure;
282
283 typedef struct
284 {
285         struct yaffs_ObjectStruct *equivalentObject;
286         __u32   equivalentObjectId;
287 } yaffs_HardLinkStructure;
288
289 typedef union
290 {
291         yaffs_FileStructure fileVariant;
292         yaffs_DirectoryStructure directoryVariant;
293         yaffs_SymLinkStructure symLinkVariant;
294         yaffs_HardLinkStructure hardLinkVariant;
295 } yaffs_ObjectVariant;
296
297
298 struct  yaffs_ObjectStruct
299 {
300         __u8 deleted: 1;                // This should only apply to unlinked files.
301         __u8 softDeleted: 1;    // it has also been soft deleted
302         __u8 unlinked: 1;               // An unlinked file. The file should be in the unlinked pseudo directory.
303         __u8 fake:1;                    // A fake object has no presence on NAND.
304         __u8 renameAllowed:1;
305         __u8 unlinkAllowed:1;
306         __u8 dirty:1;                   // the object needs to be written to flash
307         __u8 valid:1;                   // When the file system is being loaded up, this 
308                                                         // object might be created before the data
309                                                         // is available (ie. file data records appear before the header).
310         __u8 serial;                    // serial number of chunk in NAND. Store here so we don't have to
311                                                         // read back the old one to update.
312         __u16 sum;                              // sum of the name to speed searching
313         
314         struct yaffs_DeviceStruct *myDev; // The device I'm on
315         
316                                                                 
317         struct list_head hashLink;      // list of objects in this hash bucket
318                                                         
319
320         struct list_head hardLinks; // all the equivalent hard linked objects
321                                                                 // live on this list
322         // directory structure stuff
323         struct yaffs_ObjectStruct  *parent;     //my parent directory
324         struct list_head siblings;      // siblings in a directory
325                                                                 // also used for linking up the free list
326                 
327         // Where's my data in NAND?
328         int chunkId;            // where it lives
329
330         int nDataChunks;        
331         
332         __u32 objectId;         // the object id value
333         
334         
335         __u32 yst_mode;         // protection
336
337 #ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
338         char shortName[YAFFS_SHORT_NAME_LENGTH+1];
339 #endif
340
341 #ifndef __KERNEL__
342         __u32 inUse;
343 #endif
344
345 #ifdef CONFIG_YAFFS_WINCE
346         __u32 win_ctime[2];
347         __u32 win_mtime[2];
348         __u32 win_atime[2];
349 #else
350         __u32 yst_uid;          // user ID of owner
351         __u32 yst_gid;          // group ID of owner 
352         __u32 yst_atime;        // time of last access
353         __u32 yst_mtime;        // time of last modification
354         __u32 yst_ctime;        // time of last change
355 #endif
356
357         __u32 yst_rdev;             // device stuff for block and char devices
358
359
360
361 #ifdef __KERNEL__
362         struct inode *myInode;
363         __u8  deferedFree;   // YAFFS has removed the object from NAND, but it is being kept
364                              // Alive until the inode is cleared to prevent inode inconsistencies.
365 #endif
366
367
368         
369         yaffs_ObjectType variantType;
370         
371         yaffs_ObjectVariant variant;
372         
373 };
374
375
376
377 typedef struct yaffs_ObjectStruct yaffs_Object;
378
379
380 struct yaffs_ObjectList_struct
381 {
382         yaffs_Object *objects;
383         struct yaffs_ObjectList_struct *next;
384 };
385
386 typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
387
388 typedef struct
389 {
390         struct list_head list;
391         int count;
392 } yaffs_ObjectBucket;
393
394
395 //////////////////// Device ////////////////////////////////
396
397 struct yaffs_DeviceStruct
398 {
399         // Entry parameters set up way early. Yaffs sets up the rest.
400         int   nBytesPerChunk;    // Should be a power of 2 >= 512
401         int   nChunksPerBlock;   // does not need to be a power of 2
402         int   startBlock;                // Start block we're allowed to use
403         int   endBlock;                  // End block we're allowed to use
404         int   nReservedBlocks;   // We want this tuneable so that we can reduce
405                                                          // reserved blocks on NOR and RAM.
406         
407         int   useNANDECC;               // Flag to decide whether or not to use NANDECC
408         int   nShortOpCaches;   // If <= 0, then short op caching is disabled, else
409                                                         // the number of short op caches (don't use too many).
410         
411         
412         void *genericDevice; // Pointer to device context
413                                                  // On an mtd this holds the mtd pointer.
414
415         // NAND access functions (Must be set before calling YAFFS)
416         
417         int (*writeChunkToNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare);
418         int (*readChunkFromNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare);
419         int (*eraseBlockInNAND)(struct yaffs_DeviceStruct *dev,int blockInNAND);        
420         int (*initialiseNAND)(struct yaffs_DeviceStruct *dev);
421
422         // Runtime parameters. Set up by YAFFS.
423         int   internalStartBlock;                // Internal version of startBlock
424         int   internalEndBlock;                  // End block we're allowed to use
425         int   blockOffset;
426         int   chunkOffset;
427         
428         
429         __u16 chunkGroupBits; // 0 for devices <= 32MB. else log2(nchunks) - 16
430         __u16 chunkGroupSize; // == 2^^chunkGroupBits
431         
432 #ifdef __KERNEL__
433
434         struct semaphore sem;// Semaphore for waiting on erasure.
435         struct semaphore grossLock; // Gross locking semaphore
436
437 #endif  
438 #ifdef __KERNEL__
439         void (*putSuperFunc)(struct super_block *sb);
440 #endif
441
442         int isMounted;
443         
444         // Block Info
445         yaffs_BlockInfo *blockInfo;
446         __u8 *chunkBits;   // bitmap of chunks in use
447         int   chunkBitmapStride; // Number of bytes of chunkBits per block. 
448                                                          //     Must be consistent with nChunksPerBlock.
449
450
451         int   nErasedBlocks;
452         int   allocationBlock;                  // Current block being allocated off
453         __u32 allocationPage;
454         int   allocationBlockFinder;    // Used to search for next allocation block
455         
456         // Runtime state
457         int   nTnodesCreated;   
458         yaffs_Tnode *freeTnodes;
459         int  nFreeTnodes;
460         yaffs_TnodeList *allocatedTnodeList;
461
462
463         int   nObjectsCreated;
464         yaffs_Object *freeObjects;
465         int   nFreeObjects;
466
467         yaffs_ObjectList *allocatedObjectList;
468
469         yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
470
471         int       nFreeChunks;
472                 
473         int   currentDirtyChecker;      // Used to find current dirtiest block
474         
475         
476         // Operations since mount
477         int nPageWrites;
478         int nPageReads;
479         int nBlockErasures;
480         int nGCCopies;
481         int garbageCollections;
482         int passiveGarbageCollections;
483         int nRetriedWrites;
484         int nRetiredBlocks;
485         int eccFixed;
486         int eccUnfixed;
487         int tagsEccFixed;
488         int tagsEccUnfixed;
489         int nDeletions;
490         int nUnmarkedDeletions;
491         
492         yaffs_Object *rootDir;
493         yaffs_Object *lostNFoundDir;
494         
495         // Buffer areas for storing data to recover from write failures
496 //      __u8            bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
497 //      yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
498         int bufferedBlock;      // Which block is buffered here?
499         int doingBufferedBlockRewrite;
500
501         yaffs_ChunkCache *srCache;
502         int srLastUse;
503
504         int cacheHits;
505
506         // Stuff for background deletion and unlinked files.
507         yaffs_Object *unlinkedDir;              // Directory where unlinked and deleted files live.
508         yaffs_Object *unlinkedDeletion; // Current file being background deleted.
509         int nDeletedFiles;                              // Count of files awaiting deletion;
510         int nUnlinkedFiles;                             // Count of unlinked files. 
511         int nBackgroundDeletions;                       // Count of background deletions.       
512         
513         __u8 *localBuffer;
514         
515 };
516
517 typedef struct yaffs_DeviceStruct yaffs_Device;
518
519
520
521 //////////// YAFFS Functions //////////////////
522
523 int yaffs_GutsInitialise(yaffs_Device *dev);
524 void yaffs_Deinitialise(yaffs_Device *dev);
525
526 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
527
528
529 // Rename
530 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName);
531
532 // generic Object functions
533 int yaffs_Unlink(yaffs_Object *dir, const char *name);
534 int yaffs_DeleteFile(yaffs_Object *obj);
535
536 // Object access functions.
537 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize);
538 int yaffs_GetObjectFileLength(yaffs_Object *obj);
539 int yaffs_GetObjectInode(yaffs_Object *obj);
540 unsigned yaffs_GetObjectType(yaffs_Object *obj);
541 int yaffs_GetObjectLinkCount(yaffs_Object *obj);
542
543 // Change inode attributes
544 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
545 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
546
547 // File operations
548 int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, __u32 offset, int nBytes);
549 int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, __u32 offset, int nBytes);
550 int yaffs_ResizeFile(yaffs_Object *obj, int newSize);
551
552 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid);
553 int yaffs_FlushFile(yaffs_Object *obj,int updateTime);
554
555
556 // Directory operations
557 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid);
558 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir,const char *name);
559 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *));
560
561 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number);
562
563 // Link operations
564 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject);
565
566 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
567
568 // Symlink operations
569 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const char *name, __u32 mode,  __u32 uid, __u32 gid, const char *alias);
570 char *yaffs_GetSymlinkAlias(yaffs_Object *obj);
571
572 // Special inodes (fifos, sockets and devices)
573 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,__u32 rdev);
574
575
576 // Special directories
577 yaffs_Object *yaffs_Root(yaffs_Device *dev);
578 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
579
580 #ifdef CONFIG_YAFFS_WINCE
581 // CONFIG_YAFFS_WINCE special stuff
582 void  yfsd_WinFileTimeNow(__u32 target[2]);
583 #endif
584
585 #ifdef __KERNEL__
586 void yaffs_HandleDeferedFree(yaffs_Object *obj);
587 #endif
588
589 // Debug dump 
590 int yaffs_DumpObject(yaffs_Object *obj);
591
592
593 void yaffs_GutsTest(yaffs_Device *dev);
594
595
596 #endif
597
598