2 * YAFFS: Yet another FFS. A NAND-flash specific file system.
3 * yaffs_guts.h: Configuration etc for yaffs_guts
5 * Copyright (C) 2002 Aleph One Ltd.
6 * for Toby Churchill Ltd and Brightstar Engineering
8 * Created by Charles Manning <charles@aleph1.co.uk>
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.
15 * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
17 * $Id: yaffs_guts.h,v 1.10 2002-09-27 20:50:50 charles Exp $
20 #ifndef __YAFFS_GUTS_H__
21 #define __YAFFS_GUTS_H__
23 #include "devextras.h"
33 // And what have we got...
34 #define YAFFS_MAGIC 0x5941FF53
36 #define YAFFS_NTNODES_LEVEL0 16
37 #define YAFFS_TNODES_LEVEL0_BITS 4
38 #define YAFFS_TNODES_LEVEL0_MASK 0xf
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
45 #define YAFFS_BYTES_PER_CHUNK 512
46 #define YAFFS_CHUNK_SIZE_SHIFT 9
48 #define YAFFS_BYTES_PER_SPARE 16
50 #define YAFFS_CHUNKS_PER_BLOCK 32
51 #define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
53 #define YAFFS_MAX_CHUNK_ID 0x000FFFFF
55 #define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
57 #define YAFFS_ALLOCATION_NOBJECTS 100
58 #define YAFFS_ALLOCATION_NTNODES 100
59 #define YAFFS_ALLOCATION_NLINKS 100
61 #define YAFFS_NOBJECT_BUCKETS 256
64 #define YAFFS_RESERVED_BLOCKS 5
66 #define YAFFS_OBJECT_SPACE 0x40000
67 #define YAFFS_MAX_NAME_LENGTH 255
69 #define YAFFS_MAX_ALIAS_LENGTH 159
71 #define YAFFS_OBJECTID_ROOT 1
72 #define YAFFS_OBJECTID_LOSTNFOUND 2
73 #define YAFFS_OBJECTID_UNLINKED 3
75 #define YAFFS_N_CACHE_CHUNKS 10
79 // Force the short operation cache on for WinCE
81 #define CONFIG_YAFFS_SHORT_OP_CACHE
85 // ChunkCache is used for short read/write operations.
92 __u8 data[YAFFS_BYTES_PER_CHUNK];
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.
102 unsigned serialNumber:2;
103 unsigned byteCount:10;
104 unsigned objectId:18;
106 unsigned unusedStuff:2;
123 __u8 pageStatus; // set to 0 to delete the chunk
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
140 YAFFS_BLOCK_STATE_EMPTY, // This block is empty
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
147 YAFFS_BLOCK_STATE_FULL, // All the pages in this block have been allocated.
148 // At least one page holds valid data.
150 YAFFS_BLOCK_STATE_DIRTY, // All pages have been allocated and deleted.
151 // Erase me, reuse me.
153 YAFFS_BLOCK_STATE_DEAD = 0x77 // This block has failed and is not in use
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.
170 //////////////////// Object structure ///////////////////////////
171 // This is the object structure as stored on NAND
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
185 yaffs_ObjectType type;
187 // Apply to everything
189 __u16 sum; // checksum of name
190 char name[YAFFS_MAX_NAME_LENGTH + 1];
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
200 // File size applies to files only
203 // Equivalent object id applies to hard links only.
204 int equivalentObjectId;
206 // Alias is for symlinks only.
207 char alias[YAFFS_MAX_ALIAS_LENGTH + 1];
209 __u32 st_rdev; // device stuff for block and char devices (maj/min)
211 __u32 roomToGrow[12];
213 } yaffs_ObjectHeader;
217 //////////////////// Tnode ///////////////////////////
219 union yaffs_Tnode_union
221 union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
222 __u16 level0[YAFFS_NTNODES_LEVEL0];
226 typedef union yaffs_Tnode_union yaffs_Tnode;
228 struct yaffs_TnodeList_struct
230 struct yaffs_TnodeList_struct *next;
234 typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
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).
249 __u32 scannedFileSize;
252 } yaffs_FileStructure;
256 struct list_head children; // list of child links
257 } yaffs_DirectoryStructure;
262 } yaffs_SymLinkStructure;
266 struct yaffs_ObjectStruct *equivalentObject;
267 __u32 equivalentObjectId;
268 } yaffs_HardLinkStructure;
272 yaffs_FileStructure fileVariant;
273 yaffs_DirectoryStructure directoryVariant;
274 yaffs_SymLinkStructure symLinkVariant;
275 yaffs_HardLinkStructure hardLinkVariant;
276 } yaffs_ObjectVariant;
279 struct yaffs_ObjectStruct
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
294 struct yaffs_DeviceStruct *myDev; // The device I'm on
297 struct list_head hashLink; // list of objects in this hash bucket
300 struct list_head hardLinks; // all the equivalent hard linked objects
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
307 // Where's my data in NAND?
308 int chunkId; // where it lives
312 __u32 objectId; // the object id value
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
328 struct inode *myInode;
333 yaffs_ObjectType variantType;
335 yaffs_ObjectVariant variant;
341 typedef struct yaffs_ObjectStruct yaffs_Object;
344 struct yaffs_ObjectList_struct
346 yaffs_Object *objects;
347 struct yaffs_ObjectList_struct *next;
350 typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
354 struct list_head list;
356 } yaffs_ObjectBucket;
359 //////////////////// Device ////////////////////////////////
361 struct yaffs_DeviceStruct
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
371 void *genericDevice; // Pointer to device context
372 // On an mtd this holds the mtd pointer.
376 struct semaphore sem;// Semaphore for waiting on erasure.
377 struct semaphore grossLock; // Gross locking semaphore
382 // NAND access functions (Must be set before calling YAFFS)
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);
390 void (*putSuperFunc)(struct super_block *sb);
393 // Runtime parameters.
394 yaffs_BlockInfo *blockInfo;
397 __u32 allocationPage;
401 yaffs_Tnode *freeTnodes;
403 yaffs_TnodeList *allocatedTnodeList;
407 yaffs_Object *freeObjects;
410 yaffs_ObjectList *allocatedObjectList;
412 yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
416 int currentDirtyChecker; // Used to find current dirtiest block
418 int garbageCollectionRequired;
420 // Operations since mount
425 int garbageCollections;
433 yaffs_Object *rootDir;
434 yaffs_Object *lostNFoundDir;
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;
442 int blockSelectedForGC;
444 #ifdef CONFIG_YAFFS_SHORT_OP_CACHE
445 yaffs_ChunkCache srCache[YAFFS_N_CACHE_CHUNKS];
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.
459 typedef struct yaffs_DeviceStruct yaffs_Device;
463 //////////// YAFFS Functions //////////////////
465 int yaffs_GutsInitialise(yaffs_Device *dev);
466 void yaffs_Deinitialise(yaffs_Device *dev);
468 int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev);
472 int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName);
474 // generic Object functions
475 int yaffs_Unlink(yaffs_Object *dir, const char *name);
476 int yaffs_DeleteFile(yaffs_Object *obj);
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);
485 // Change inode attributes
486 int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr);
487 int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr);
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);
494 yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid);
495 int yaffs_FlushFile(yaffs_Object *obj);
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 *));
503 yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,int number);
506 yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject);
508 yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj);
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);
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);
518 // Special directories
519 yaffs_Object *yaffs_Root(yaffs_Device *dev);
520 yaffs_Object *yaffs_LostNFound(yaffs_Device *dev);
524 int yaffs_DumpObject(yaffs_Object *obj);
527 void yaffs_GutsTest(yaffs_Device *dev);