2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
\r
3 * yaffs_guts.h: Configuration etc for yaffs_guts
\r
5 * Copyright (C) 2002 Aleph One Ltd.
\r
6 * for Toby Churchill Ltd and Brightstar Engineering
\r
8 * Created by Charles Manning <charles@aleph1.co.uk>
\r
10 * This program is free software; you can redistribute it and/or modify
\r
11 * it under the terms of the GNU General Public License version 2 as
\r
12 * published by the Free Software Foundation.
\r
16 #ifndef __YAFFS_GUTS_H__
\r
17 #define __YAFFS_GUTS_H__
\r
19 #include "devextras.h"
\r
23 #define YAFFS_FAIL 0
\r
25 // Y=0x59, A=0x41, S=0x53
\r
26 #define YAFFS_MAGIC 0x5941FF53
\r
28 #define YAFFS_NTNODES_LEVEL0 16
\r
29 #define YAFFS_TNODES_LEVEL0_BITS 4
\r
30 #define YAFFS_TNODES_LEVEL0_MASK 0xf
\r
32 #define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
\r
33 #define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
\r
34 #define YAFFS_TNODES_INTERNAL_MASK 0x7
\r
35 #define YAFFS_TNODES_MAX_LEVEL 6
\r
37 #define YAFFS_BYTES_PER_CHUNK 512
\r
38 #define YAFFS_CHUNK_SIZE_SHIFT 9
\r
40 #define YAFFS_BYTES_PER_SPARE 16
\r
42 #define YAFFS_CHUNKS_PER_BLOCK 32
\r
43 #define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
\r
45 #define YAFFS_MAX_CHUNK_ID 0x000FFFFF
\r
47 #define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
\r
49 #define YAFFS_ALLOCATION_NOBJECTS 100
\r
50 #define YAFFS_ALLOCATION_NTNODES 100
\r
51 #define YAFFS_ALLOCATION_NLINKS 100
\r
53 #define YAFFS_NOBJECT_BUCKETS 256
\r
56 #define YAFFS_RESERVED_BLOCKS 5
\r
58 #define YAFFS_OBJECT_SPACE 0x40000
\r
59 #define YAFFS_MAX_NAME_LENGTH 255
\r
61 #define YAFFS_MAX_ALIAS_LENGTH 159
\r
63 #define YAFFS_OBJECTID_ROOT 1
\r
64 #define YAFFS_OBJECTID_LOSTNFOUND 2
\r
66 // Tags structures in RAM
\r
67 // NB This uses bitfield. Bitfields should not stradle a u32 boundary otherwise
\r
68 // the structure size will get blown out.
\r
72 unsigned chunkId:20;
\r
73 unsigned serialNumber:2;
\r
74 unsigned byteCount:10;
\r
75 unsigned objectId:18;
\r
77 unsigned unusedStuff:2;
\r
94 __u8 pageStatus; // Currently unused, but sort of set aside to distinguish
\r
95 // unused - vs- used -vs- deleted chunks. We achieve this by
\r
96 // using the objectId tags.
\r
106 // Block data in RAM
\r
109 YAFFS_BLOCK_STATE_UddNKNOWN = 0,
\r
110 YAFFS_BLOCK_STATE_SCANNING, // Used while the block is being scanned.
\r
111 // NB Don't erase blocks while they're being scanned
\r
113 YAFFS_BLOCK_STATE_EMPTY, // This block is empty
\r
115 YAFFS_BLOCK_STATE_ALLOCATING, // This block is partially allocated.
\r
116 // This is the one currently being used for page
\r
117 // allocation. Should never be more than one of these
\r
120 YAFFS_BLOCK_STATE_FULL, // All the pages in this block have been allocated.
\r
121 // At least one page holds valid data.
\r
123 YAFFS_BLOCK_STATE_DIRTY, // All pages have been allocated and deleted.
\r
124 // Erase me, reuse me.
\r
126 YAFFS_BLOCK_STATE_DEAD = 0x99 // This block has failed and is not in use
\r
128 } yaffs_BlockState;
\r
135 __u32 pageBits; // bitmap of pages in use
\r
136 __u8 blockState; // One of the above block states
\r
137 __u8 pagesInUse; // number of pages in use
\r
141 //////////////////// Object structure ///////////////////////////
\r
142 // This is the object structure as stored on NAND
\r
146 YAFFS_OBJECT_TYPE_UNKNOWN,
\r
147 YAFFS_OBJECT_TYPE_FILE,
\r
148 YAFFS_OBJECT_TYPE_SYMLINK,
\r
149 YAFFS_OBJECT_TYPE_DIRECTORY,
\r
150 YAFFS_OBJECT_TYPE_HARDLINK,
\r
151 YAFFS_OBJECT_TYPE_SPECIAL
\r
152 } yaffs_ObjectType;
\r
156 yaffs_ObjectType type;
\r
158 // Apply to everything
\r
159 int parentObjectId;
\r
160 __u16 sum; // checksum of name
\r
161 char name[YAFFS_MAX_NAME_LENGTH + 1];
\r
163 // Thes following apply to directories, files, symlinks - not hard links
\r
164 __u32 st_mode; // protection
\r
165 __u32 st_uid; // user ID of owner
\r
166 __u32 st_gid; // group ID of owner
\r
167 __u32 st_atime; // time of last access
\r
168 __u32 st_mtime; // time of last modification
\r
169 __u32 st_ctime; // time of last change
\r
171 // File size applies to files only
\r
174 // Equivalent object id applies to hard links only.
\r
175 int equivalentObjectId;
\r
177 // Alias is for symlinks only.
\r
178 char alias[YAFFS_MAX_ALIAS_LENGTH + 1];
\r
180 __u32 st_rdev; // device stuff for block and char devices (maj/min)
\r
181 // Roowm to grow...
\r
182 __u32 roomToGrow[12];
\r
184 } yaffs_ObjectHeader;
\r
188 //////////////////// Tnode ///////////////////////////
\r
190 union yaffs_Tnode_union
\r
192 union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
\r
193 __u16 level0[YAFFS_NTNODES_LEVEL0];
\r
197 typedef union yaffs_Tnode_union yaffs_Tnode;
\r
199 struct yaffs_TnodeList_struct
\r
201 struct yaffs_TnodeList_struct *next;
\r
202 yaffs_Tnode *tnodes;
\r
205 typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
\r
209 /////////////////// Object ////////////////////////////////
\r
210 // An object can be one of:
\r
211 // - a directory (no data, has children links
\r
212 // - a regular file (data.... not prunes :->).
\r
213 // - a symlink [symbolic link] (the alias).
\r
220 __u32 scannedFileSize;
\r
223 } yaffs_FileStructure;
\r
227 struct list_head children; // list of child links
\r
228 } yaffs_DirectoryStructure;
\r
233 } yaffs_SymLinkStructure;
\r
237 struct yaffs_ObjectStruct *equivalentObject;
\r
238 __u32 equivalentObjectId;
\r
239 } yaffs_HardLinkStructure;
\r
243 yaffs_FileStructure fileVariant;
\r
244 yaffs_DirectoryStructure directoryVariant;
\r
245 yaffs_SymLinkStructure symLinkVariant;
\r
246 yaffs_HardLinkStructure hardLinkVariant;
\r
247 } yaffs_ObjectVariant;
\r
250 struct yaffs_ObjectStruct
\r
252 __u8 fake:1; // A fake object has no presence on NAND.
\r
253 __u8 renameAllowed:1;
\r
254 __u8 unlinkAllowed:1;
\r
255 __u8 dirty:1; // the object needs to be written to flash
\r
256 __u8 valid:1; // When the file system is being loaded up, this
\r
257 // object might be created before the data
\r
258 // is available (ie. file data records appear before the header).
\r
259 __u8 serial; // serial number of chunk in NAND. Store here so we don't have to
\r
260 // read back the old one to update.
\r
261 __u16 sum; // sum of the name to speed searching
\r
263 struct yaffs_DeviceStruct *myDev; // The device I'm on
\r
266 struct list_head hashLink; // list of objects in this hash bucket
\r
269 struct list_head hardLinks; // all the equivalent hard linked objects
\r
270 // live on this list
\r
271 // directory structure stuff
\r
272 struct yaffs_ObjectStruct *parent; //my parent directory
\r
273 struct list_head siblings; // siblings in a directory
\r
274 // also used for linking up the free list
\r
276 // Where's my data in NAND?
\r
277 int chunkId; // where it lives
\r
279 int nDataChunks; // really only for debugging.
\r
281 __u32 objectId; // the object id value
\r
284 __u32 st_mode; // protection
\r
285 __u32 st_uid; // user ID of owner
\r
286 __u32 st_gid; // group ID of owner
\r
287 __u32 st_atime; // time of last access
\r
288 __u32 st_mtime; // time of last modification
\r
289 __u32 st_ctime; // time of last change
\r
290 __u32 st_rdev; // device stuff for block and char devices
\r
297 yaffs_ObjectType variantType;
\r
299 yaffs_ObjectVariant variant;
\r
305 typedef struct yaffs_ObjectStruct yaffs_Object;
\r
308 struct yaffs_ObjectList_struct
\r
310 yaffs_Object *objects;
\r
311 struct yaffs_ObjectList_struct *next;
\r
314 typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
\r
318 struct list_head list;
\r
320 } yaffs_ObjectBucket;
\r
323 //////////////////// Device ////////////////////////////////
\r
325 struct yaffs_DeviceStruct
\r
327 // Entry parameters set up way early. Yaffs sets up the rest.
\r
328 __u32 nBlocks; // Size of whole device in blocks
\r
329 __u32 startBlock; // Start block we're allowed to use
\r
330 __u32 endBlock; // End block we're allowed to use
\r
331 __u16 chunkGroupBits; // 0 for devices <= 32MB. else log2(nchunks) - 16
\r
332 __u16 chunkGroupSize; // == 2^^chunkGroupBits
\r
335 void *genericDevice; // Pointer to device context
\r
336 // On an mtd this holds the mtd pointer.
\r
340 struct semaphore sem;// Semaphore for waiting on erasure.
\r
345 // NAND access functions (Must be set before calling YAFFS)
\r
347 int (*writeChunkToNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare);
\r
348 int (*readChunkFromNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare);
\r
349 int (*eraseBlockInNAND)(struct yaffs_DeviceStruct *dev,int blockInNAND);
\r
350 int (*initialiseNAND)(struct yaffs_DeviceStruct *dev);
\r
353 void (*putSuperFunc)(struct super_block *sb);
\r
356 // Runtime parameters.
\r
357 yaffs_BlockInfo *blockInfo;
\r
359 int allocationBlock;
\r
360 __u32 allocationPage;
\r
363 int nTnodesCreated;
\r
364 yaffs_Tnode *freeTnodes;
\r
366 yaffs_TnodeList *allocatedTnodeList;
\r
369 int nObjectsCreated;
\r
370 yaffs_Object *freeObjects;
\r
373 yaffs_ObjectList *allocatedObjectList;
\r
375 yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
\r
379 int currentDirtyChecker; // Used to find current dirtiest block
\r
381 int garbageCollectionRequired;
\r
383 // Operations since mount
\r
386 int nBlockErasures;
\r
388 int nRetriedWrites;
\r
390 yaffs_Object *rootDir;
\r
391 yaffs_Object *lostNFoundDir;
\r
396 typedef struct yaffs_DeviceStruct yaffs_Device;
\r
400 //////////// YAFFS Functions //////////////////
\r
402 int yaffs_GutsInitialise(yaffs_Device *dev);
\r
403 void yaffs_Deinitialise(yaffs_Device *dev);
\r
405 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
\r
409 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName);
\r
411 // generic Object functions
\r
412 int yaffs_Unlink(yaffs_Object *dir, const char *name);
\r
414 // Object access functions.
\r
415 int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize);
\r
416 int yaffs_GetObjectFileLength(yaffs_Object *obj);
\r
417 int yaffs_GetObjectInode(yaffs_Object *obj);
\r
418 unsigned yaffs_GetObjectType(yaffs_Object *obj);
\r
419 int yaffs_GetObjectLinkCount(yaffs_Object *obj);
\r
421 // Change inode attributes
\r
422 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
\r
423 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
\r
426 int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, __u32 offset, int nBytes);
\r
427 int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, __u32 offset, int nBytes);
\r
428 int yaffs_ResizeFile(yaffs_Object *obj, int newSize);
\r
430 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid);
\r
431 int yaffs_FlushFile(yaffs_Object *obj);
\r
434 // Directory operations
\r
435 yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid);
\r
436 yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir,const char *name);
\r
437 int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *));
\r
439 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,int number);
\r
442 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject);
\r
444 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
\r
446 // Symlink operations
\r
447 yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const char *name, __u32 mode, __u32 uid, __u32 gid, const char *alias);
\r
448 char *yaffs_GetSymlinkAlias(yaffs_Object *obj);
\r
450 // Special inodes (fifos, sockets and devices)
\r
451 yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,__u32 rdev);
\r
454 // Special directories
\r
455 yaffs_Object *yaffs_Root(yaffs_Device *dev);
\r
456 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
\r
460 int yaffs_DumpObject(yaffs_Object *obj);
\r
463 void yaffs_GutsTest(yaffs_Device *dev);
\r