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