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