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