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