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