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