Index: linux-2.6.9/fs/yaffs/yaffs_ecc.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_ecc.c @@ -0,0 +1,194 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * + * yaffs_ecc.c: ECC generation/correction algorithms. + * + * Copyright (C) 2002 Aleph One Ltd. + * + * Created by Charles Manning + * + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + */ + + /* + * This code implements the ECC algorithm used in SmartMedia. + * + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. + * The two unused bit are set to 1. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC + * blocks are used on a 512-byte NAND page. + * + */ + +// Table generated by gen-ecc.c +// Using a table means we do not have to calculate p1..p4 and p1'..p4' +// for each byte of data. These are instead provided in a table in bits7..2. +// Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore +// this bytes influence on the line parity. + +const char *yaffs_ecc_c_version = "$Id: yaffs_ecc.c,v 1.3 2004/04/04 03:47:02 charles Exp $"; + + +#include "yaffs_ecc.h" + +static const unsigned char column_parity_table[] = { +0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, +0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, +0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, +0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, +0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, +0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, +0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, +0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, +0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0, 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9, +0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55, 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c, +0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59, 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30, +0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc, 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5, +0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65, 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c, +0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0, 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99, +0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc, 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95, +0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69, 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00, +}; + + +static int yaffs_CountBits(unsigned char x) +{ + int r = 0; + while(x) + { + if(x & 1) r++; + x >>= 1; + } + return r; +} + +void yaffs_ECCCalculate(const unsigned char *data,unsigned char *ecc) +{ + unsigned int i; + + unsigned char col_parity = 0; + unsigned char line_parity = 0; + unsigned char line_parity_prime = 0; + unsigned char t; + unsigned char b; + + for(i = 0; i < 256; i++) + { + b = column_parity_table[*data++]; + col_parity ^= b; + + if(b & 0x01) // odd number of bits in the byte + { + line_parity ^= i; + line_parity_prime ^= ~i; + } + + } + + ecc[2] = (~col_parity) | 0x03; + + t = 0; + if(line_parity & 0x80) t |= 0x80; + if(line_parity_prime & 0x80) t |= 0x40; + if(line_parity & 0x40) t |= 0x20; + if(line_parity_prime & 0x40) t |= 0x10; + if(line_parity & 0x20) t |= 0x08; + if(line_parity_prime & 0x20) t |= 0x04; + if(line_parity & 0x10) t |= 0x02; + if(line_parity_prime & 0x10) t |= 0x01; + ecc[1] = ~t; + + t = 0; + if(line_parity & 0x08) t |= 0x80; + if(line_parity_prime & 0x08) t |= 0x40; + if(line_parity & 0x04) t |= 0x20; + if(line_parity_prime & 0x04) t |= 0x10; + if(line_parity & 0x02) t |= 0x08; + if(line_parity_prime & 0x02) t |= 0x04; + if(line_parity & 0x01) t |= 0x02; + if(line_parity_prime & 0x01) t |= 0x01; + ecc[0] = ~t; + +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER + // Swap the bytes into the wrong order + t = ecc[0]; + ecc[0] = ecc[1]; + ecc[1] = t; +#endif +} + +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc, const unsigned char *test_ecc) +{ + unsigned char d0, d1, d2; // deltas + + d0 = read_ecc[0] ^ test_ecc[0]; + d1 = read_ecc[1] ^ test_ecc[1]; + d2 = read_ecc[2] ^ test_ecc[2]; + + + + if((d0 | d1 | d2) == 0) + { + // no error + return 0; + } + + if( ((d0 ^ (d0 >> 1)) & 0x55) == 0x55 && + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 && + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) + { + // Single bit (recoverable) error in data + + unsigned byte; + unsigned bit; + +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER + // swap the bytes to correct for the wrong order + unsigned char t; + + t = d0; + d0 = d1; + d1 = t; +#endif + + bit = byte = 0; + + + if(d1 & 0x80) byte |= 0x80; + if(d1 & 0x20) byte |= 0x40; + if(d1 & 0x08) byte |= 0x20; + if(d1 & 0x02) byte |= 0x10; + if(d0 & 0x80) byte |= 0x08; + if(d0 & 0x20) byte |= 0x04; + if(d0 & 0x08) byte |= 0x02; + if(d0 & 0x02) byte |= 0x01; + + if(d2 & 0x80) bit |= 0x04; + if(d2 & 0x20) bit |= 0x02; + if(d2 & 0x08) bit |= 0x01; + + data[byte] ^= (1 << bit); + + return 1; + } + + if((yaffs_CountBits(d0)+yaffs_CountBits(d1)+yaffs_CountBits(d2)) == 1) + { + // Reccoverable error in ecc + + read_ecc[0] = test_ecc[0]; + read_ecc[1] = test_ecc[1]; + read_ecc[2] = test_ecc[2]; + + return 1; + } + + // Unrecoverable error + + return -1; + + +} Index: linux-2.6.9/fs/yaffs/yportenv.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yportenv.h @@ -0,0 +1,137 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yportenv.h: Portable services used by yaffs. This is done to allow + * simple migration from kernel space into app space for testing. + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + * $Id: yportenv.h,v 1.14 2004/10/10 18:03:35 charles Exp $ + * + */ + +#ifndef __YPORTENV_H__ +#define __YPORTENV_H__ + + +#if defined CONFIG_YAFFS_WINCE + +#include "ywinceenv.h" + +#elif defined __KERNEL__ + + + +// Linux kernel +#include "linux/kernel.h" +#include "linux/version.h" +#include "linux/mm.h" +#include "linux/string.h" +#include "linux/slab.h" + + +#define YAFFS_LOSTNFOUND_NAME "lost+found" +#define YAFFS_LOSTNFOUND_PREFIX "obj" + +//#define YPRINTF(x) printk x +#define YMALLOC(x) kmalloc(x,GFP_KERNEL) +#define YFREE(x) kfree(x) + +#define YAFFS_ROOT_MODE 0666 +#define YAFFS_LOSTNFOUND_MODE 0666 + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec +#define Y_TIME_CONVERT(x) (x).tv_sec +#else +#define Y_CURRENT_TIME CURRENT_TIME +#define Y_TIME_CONVERT(x) (x) +#endif + +#define yaffs_SumCompare(x,y) ((x) == (y)) +#define yaffs_strcmp(a,b) strcmp(a,b) + +#define TENDSTR "\n" +#define TSTR(x) KERN_DEBUG x +#define TOUT(p) printk p + + +#elif defined CONFIG_YAFFS_DIRECT + +// Direct interface +#include "ydirectenv.h" + +#elif defined CONFIG_YAFFS_UTIL + +// Stuff for YAFFS utilities + +#include "stdlib.h" +#include "stdio.h" +#include "string.h" + +#include "devextras.h" + +#define YMALLOC(x) malloc(x) +#define YFREE(x) free(x) + + +//#define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) +//#define YALERT(s) YINFO(s) + + +#define TENDSTR "\n" +#define TSTR(x) x +#define TOUT(p) printf p + + +#define YAFFS_LOSTNFOUND_NAME "lost+found" +#define YAFFS_LOSTNFOUND_PREFIX "obj" +//#define YPRINTF(x) printf x + + +#define CURRENT_TIME 0 +#define YAFFS_ROOT_MODE 0666 +#define YAFFS_LOSTNFOUND_MODE 0666 + +#define yaffs_SumCompare(x,y) ((x) == (y)) +#define yaffs_strcmp(a,b) strcmp(a,b) + +#else +// Should have specified a configuration type +#error Unknown configuration + +#endif + + +extern unsigned yaffs_traceMask; + +#define YAFFS_TRACE_ERROR 0x0001 +#define YAFFS_TRACE_OS 0x0002 +#define YAFFS_TRACE_ALLOCATE 0x0004 +#define YAFFS_TRACE_SCAN 0x0008 +#define YAFFS_TRACE_BAD_BLOCKS 0x0010 +#define YAFFS_TRACE_ERASE 0x0020 +#define YAFFS_TRACE_GC 0x0040 +#define YAFFS_TRACE_TRACING 0x0100 +#define YAFFS_TRACE_ALWAYS 0x0200 +#define YAFFS_TRACE_BUG 0x8000 + +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ERROR)) TOUT(p);} while(0) + + +#ifndef CONFIG_YAFFS_WINCE +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__)) +#endif + +#endif + + Index: linux-2.6.9/fs/yaffs/yaffsdev.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffsdev.c @@ -0,0 +1,731 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffsdev.c + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#include "yaffsinterface.h" +#include "yportenv.h" + +#if YAFFS_FILEEM +#include "yaffs_fileem.h" +#else +#include "yaffs_nandemul.h" +#endif + +#include "yaffs_guts.h" +#include + +#include +#include +#include +#include + + + +unsigned yaffs_traceMask = 0xFFFFFFFF; + +yaffs_Device device; + + +char *testStr = "this is a test string"; + +char *testStr2 = "abcdefghijklmnopqrstuvwxyz1234567890"; + +void TestTimexxx(yaffs_Device *dev) +{ + yaffs_Object *f; + int x; + + + printf("Start\n"); + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + + x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename"); + +} + + +void TestTimeasasas(yaffs_Device *dev) +{ + yaffs_Object *f; + int x; + int i; + int b; + char data[200]; + int written; + + + printf("Start\n"); + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + + x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename"); + + + + for(i = 0; i < 10000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2)); + } + + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + printf("Flush\n"); + + yaffs_FlushFile(f,1); + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + yaffs_DumpObject(f); + + printf("Resize to 3000\n"); + yaffs_ResizeFile(f,3000); + printf("Resize to 2048\n"); + yaffs_ResizeFile(f,2048); + + yaffs_DumpObject(f); + + yaffs_FlushFile(f,1); + + + + +} + +void TestTimeBigDeletes(yaffs_Device *dev) +{ + yaffs_Object *f; + yaffs_Object *sl; + yaffs_Object *lnf; + + yaffs_Object *hl1; + yaffs_Object *hl2; + yaffs_Object *hl3; + yaffs_Object *d, *df; + + int x; + int i; + int b; + char data[200]; + + char * alias; + int written; + + + printf("Exisiting objects\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + printf("Exisiting objects in lost+found\n"); + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + yaffs_ApplyToDirectoryChildren(lnf,yaffs_DumpObject); + + printf("Start\n"); + + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + for(i = 0; i < 100000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2)); + } + + yaffs_FlushFile(f,1); + yaffs_DeleteFile(f); + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + for(i = 0; i < 100000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2)); + } + + yaffs_FlushFile(f,1); + yaffs_DeleteFile(f); + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + for(i = 0; i < 100000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2)); + } + + yaffs_FlushFile(f,1); + yaffs_DeleteFile(f); + +} + +void TestTime(yaffs_Device *dev) +{ + yaffs_Object *f; + yaffs_Object *sl; + yaffs_Object *lnf; + + yaffs_Object *hl1; + yaffs_Object *hl2; + yaffs_Object *hl3; + yaffs_Object *d, *df; + + int x; + int i; + int b; + char data[200]; + + char * alias; + int written; + + + printf("Exisiting objects\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + printf("Exisiting objects in lost+found\n"); + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + yaffs_ApplyToDirectoryChildren(lnf,yaffs_DumpObject); + + printf("Start\n"); + + + // Test the problem of: + // Create file + // Delete file + // Create file with same name + // Delete file <== crash + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + yaffs_Unlink(yaffs_Root(dev),"Name1"); + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + yaffs_Unlink(yaffs_Root(dev),"Name1"); + + + + // Other tests + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + + x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename"); + +// Write a file with overwrite, then read it back and save outside yaffs +// This is to test that write caching works. + + for(i = 0; i < 100; i++) + { + unsigned char x[500]; + + memset(x,i,500); + + written = yaffs_WriteDataToFile(f,x,i*500,500); + } + + for(i = 0; i < 100; i++) + { + unsigned char x; + + x = i+5; + + written = yaffs_WriteDataToFile(f,&x,i*500 + 2,1); + } + + + { + int h; + + h = open("bork",O_RDWR | O_CREAT | O_TRUNC,0666); + for(i = 0; i < 100; i++) + { + unsigned char x[500]; + + yaffs_ReadDataFromFile(f,x,i*500,500); + write(h,x,500); + } + close(h); + + + } + +// Big write to fill disk + + written = 1; + for(i = 0; i < 1000000 && written > 0; i++) + { + unsigned char x[500]; + + memset(x,i,500); + + written = yaffs_WriteDataToFile(f,x,i*500,500); + } + if(written <= 0) + { + printf("YAFFS full\n"); + } + + // some short reads + for(i = 1000; i < 50000; i+=2) + { + yaffs_ReadDataFromFile(f,data,i,20); + } + + + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + printf("Flush\n"); + + yaffs_FlushFile(f,1); + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + printf("File length is %d\n",yaffs_GetObjectFileLength(f)); + + sl = yaffs_MknodSymLink(yaffs_Root(dev),"sym-link",0,0,0,"/tmp/alias"); + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + printf("\n\nsymlink alias is \"%s\"\n",alias = yaffs_GetSymlinkAlias(sl)); + + free(alias); + + printf("Unlink symlink %d\n",yaffs_Unlink(yaffs_Root(dev),"sym-link")); + + + yaffs_ReadDataFromFile(f,data,1000,50); + data[50] = 0; + + printf("Read data is \"%s\"\n",data); + + yaffs_DumpObject(f); + + printf("Resize 3000\n"); + yaffs_ResizeFile(f,3000); + + printf("Resize 2050\n"); + yaffs_ResizeFile(f,2050); + printf("Resize 2049\n"); + yaffs_ResizeFile(f,2049); + printf("Resize 2048\n"); + yaffs_ResizeFile(f,2048); + + + printf("Resize 2000\n"); + yaffs_ResizeFile(f,2000); + + yaffs_DumpObject(f); + + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + + + + yaffs_FlushFile(f,1); + + + printf("Unlink file: %d\n",yaffs_Unlink(yaffs_Root(dev),"Rename")); + + yaffs_DeleteFile(f); + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + // Create a directory and play with it + + + printf("Find or Create directory and play with it\n"); + d = yaffs_FindObjectByName(yaffs_Root(dev),"direct"); + if(!d) + { + d = yaffs_MknodDirectory(yaffs_Root(dev),"direct",0,0,0); + } + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + yaffs_ApplyToDirectoryChildren(d,yaffs_DumpObject); + + printf("Make file in directory\n"); + + df = yaffs_MknodFile(d,"file-in-directory",0,0,0); + yaffs_ApplyToDirectoryChildren(d,yaffs_DumpObject); + + + // Do some stuff with hardlinks + // + // NB Deleting hardlinked objects can mess up pointers to hardlinks. + // The mechanism is as follows: + // * If you unlink a file,softlink or directory that has one or more hardlinks, + // then the object is renamed to one of the hardlinks and that hardlink is unlinked. + // This means that a pointer to a hardlink so deleted will point to an invalid address. + // Thus, make sure that pointers to hardlinks are immediately dereferenced. + + + printf("Hard link tests\n"); + + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + hl1 = yaffs_Link(yaffs_Root(dev),"HardLink 1",f); + hl2 = yaffs_Link(yaffs_Root(dev),"HardLink 2",f); + hl3 = yaffs_Link(yaffs_Root(dev),"HardLink 3",hl2); + + printf("\n\nHard links created\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + yaffs_Unlink(yaffs_Root(dev),"HardLink 1"); + printf("\n\nHard link deleted\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + yaffs_Unlink(yaffs_Root(dev),"Name1"); + printf("\n\nHard linked file deleted\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + yaffs_Unlink(yaffs_Root(dev),"HardLink 2"); + printf("\n\nHard link 2 deleted\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + yaffs_Unlink(yaffs_Root(dev),"HardLink 3"); + + printf("\n\nHard link 3 deleted\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + // NB We don't allow unlinking or rename of the root or lost+found + // We allow setting attributes, but these must not be written to + // NAND since they are not real objects. + + printf("Attempt to rename lost+found - should have failed\n"); + x = yaffs_RenameObject(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME,NULL,"Renamed"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + f = yaffs_MknodFile(yaffs_Root(dev),"pfile",0,0,0); + if(f) + { + yaffs_WriteDataToFile(f,testStr,0,strlen(testStr)); + } + + yaffs_Link(yaffs_Root(dev),"phl4",f); +} + +void TestTimeDeleteFocussed(yaffs_Device *dev) +{ + yaffs_Object *f; + yaffs_Object *lnf; + + + int x; + int i; + int b; + int written; + + + printf("Exisiting objects\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + printf("Exisiting objects in lost+found\n"); + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + yaffs_ApplyToDirectoryChildren(lnf,yaffs_DumpObject); + + printf("Start\n"); + + + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + + x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename"); + + for(i = 0; i < 100000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,i,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,i,strlen(testStr2)); + } + + + + yaffs_FlushFile(f,1); + + + printf("Unlink file: %d\n",yaffs_Unlink(yaffs_Root(dev),"Rename")); + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + +} + +void TestTimeTnodeFocussed(yaffs_Device *dev) +{ + yaffs_Object *f; + yaffs_Object *lnf; + + + int x; + int i; + int b; + int written; + + + printf("Exisiting objects\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + printf("Exisiting objects in lost+found\n"); + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + yaffs_ApplyToDirectoryChildren(lnf,yaffs_DumpObject); + + printf("Start\n"); + + + + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + + x = yaffs_RenameObject(yaffs_Root(dev),"Name1",NULL,"Rename"); + + for(i = 0; i < 10000; i+=20) + { + + b++; + if(b & 1) + written = yaffs_WriteDataToFile(f,testStr,0,strlen(testStr)); + else + written = yaffs_WriteDataToFile(f,testStr2,0,strlen(testStr2)); + } + +} +void TestTimeBackgroundDeleteFocussed(yaffs_Device *dev) +{ + yaffs_Object *f; + yaffs_Object *lnf; + + + int x; + int i,j; + int b; + int written; + + + printf("Exisiting objects\n"); + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + printf("Exisiting objects in lost+found\n"); + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + yaffs_ApplyToDirectoryChildren(lnf,yaffs_DumpObject); + + printf("Start\n"); + + + for(j = 0; j < 20; j++) + { + printf("Run %d\n",j); + + f = yaffs_FindObjectByName(yaffs_Root(dev),"Name1"); + if(f) + { + printf("Found\n"); + } + else + { + f = yaffs_MknodFile(yaffs_Root(dev),"Name1",0,0,0); + printf("Created\n"); + } + + printf("@@@@@@@ Run %d, object %d\n",j,f->objectId); + + for(i = 0; i < 1000000; i+=20) + { + + written = yaffs_WriteDataToFile(f,testStr,i,100); + } + + yaffs_FlushFile(f,1); + + yaffs_DeleteFile(f); + } + +} + +int main(int argc,char *argv[]) +{ + + int nBlocks; + +#if YAFFS_FILEEM + nBlocks =(2 * 1024 * 1024) / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK) ; + device.writeChunkToNAND = yaffs_FEWriteChunkToNAND; + device.readChunkFromNAND = yaffs_FEReadChunkFromNAND; + device.eraseBlockInNAND = yaffs_FEEraseBlockInNAND; + device.initialiseNAND = yaffs_FEInitialiseNAND; + + printf("Testing on file emulation\n"); +#else + nBlocks = (2 * 1024 * 1024) / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + device.writeChunkToNAND = nandemul_WriteChunkToNAND; + device.readChunkFromNAND = nandemul_ReadChunkFromNAND; + device.eraseBlockInNAND = nandemul_EraseBlockInNAND; + device.initialiseNAND = nandemul_InitialiseNAND; + + printf("Testing on RAM emulation\n"); +#endif + +#ifdef YAFFS_START + device.startBlock = YAFFS_START; // Don't use block 0 + device.endBlock = YAFFS_END; +#else + device.startBlock = 1; // Don't use block 0 + device.endBlock = nBlocks; +#endif + + device.nShortOpCaches = 10; + + + yaffs_GutsInitialise(&device); + + // yaffs_GutsTest(); + + TestTimeBackgroundDeleteFocussed(&device); + + printf("Cache hits %d\n",device.cacheHits); + printf("Retired blocks %d\n",device.nRetiredBlocks); + + printf("Deletions %d\n",device.nDeletions); + printf("Unmarked deletions %d\n",device.nUnmarkedDeletions); + + exit(0); +} Index: linux-2.6.9/fs/yaffs/yaffs_mtdif.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_mtdif.h @@ -0,0 +1,33 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_mtdif.h NAND mtd interface wrappers + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + * $Id: yaffs_mtdif.h,v 1.4 2002/09/27 20:50:50 charles Exp $ + */ + +#ifndef __YAFFS_MTDIF_H__ +#define __YAFFS_MTDIF_H__ + +#include "yaffs_guts.h" + +int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare); +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare); +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber); +int nandmtd_InitialiseNAND(yaffs_Device *dev); +#endif + + + + Index: linux-2.6.9/fs/yaffs/yaffsinterface.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffsinterface.h @@ -0,0 +1,25 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffsinterface.h: Interface to the guts of yaffs. + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + */ + +#ifndef __YAFFSINTERFACE_H__ +#define __YAFFSINTERFACE_H__ + + +int yaffs_Initialise(unsigned nBlocks); + +#endif + Index: linux-2.6.9/fs/yaffs/yaffs_guts.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_guts.c @@ -0,0 +1,5012 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_guts.c The main guts of YAFFS + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + //yaffs_guts.c + +const char *yaffs_guts_c_version="$Id: yaffs_guts.c,v 1.37 2004/10/20 20:12:43 charles Exp $"; + +#include "yportenv.h" + +#include "yaffsinterface.h" +#include "yaffs_guts.h" + +#define YAFFS_PASSIVE_GC_CHUNKS 2 + +#if 0 +// Use Steven Hill's ECC struff instead +// External functions for ECC on data +void nand_calculate_ecc (const u_char *dat, u_char *ecc_code); +int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc); +#define yaffs_ECCCalculate(data,ecc) nand_calculate_ecc(data,ecc) +#define yaffs_ECCCorrect(data,read_ecc,calc_ecc) nand_correct_ecc(data,read_ecc,calc_ecc) +#else +#include "yaffs_ecc.h" +#endif + +// countBits is a quick way of counting the number of bits in a byte. +// ie. countBits[n] holds the number of 1 bits in a byte with the value n. + +static const char yaffs_countBitsTable[256] = +{ +0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4, +1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, +1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, +1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, +2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6, +3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, +3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7, +4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8 +}; + +static int yaffs_CountBits(__u8 x) +{ + int retVal; + retVal = yaffs_countBitsTable[x]; + return retVal; +} + + + +// Local prototypes +static int yaffs_CheckObjectHashSanity(yaffs_Device *dev); +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr); +static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr); +static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan); + +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type); +static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj); +static int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force); +static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND); +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj); +static int yaffs_CheckStructures(void); +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit); +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in); + +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device *dev,int blockNo); + +// Robustification +static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND); +static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND); +static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND); +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare); +static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare); + +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND); + +static int yaffs_UnlinkWorker(yaffs_Object *obj); +static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj); + +static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1); + + + +loff_t yaffs_GetFileSize(yaffs_Object *obj); + +static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted); +static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted); + + +static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve); + +#ifdef YAFFS_PARANOID +static int yaffs_CheckFileSanity(yaffs_Object *in); +#else +#define yaffs_CheckFileSanity(in) +#endif + +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in); +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId); + +// Chunk bitmap manipulations + +static __inline __u8 *yaffs_BlockBits(yaffs_Device *dev, int blk) +{ + if(blk < dev->startBlock || blk > dev->endBlock) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),blk)); + YBUG(); + } + return dev->chunkBits + (dev->chunkBitmapStride * (blk - dev->startBlock)); +} + +static __inline__ void yaffs_ClearChunkBits(yaffs_Device *dev,int blk) +{ + __u8 *blkBits = yaffs_BlockBits(dev,blk); + + memset(blkBits,0,dev->chunkBitmapStride); +} + +static __inline__ void yaffs_ClearChunkBit(yaffs_Device *dev,int blk,int chunk) +{ + __u8 *blkBits = yaffs_BlockBits(dev,blk); + + blkBits[chunk/8] &= ~ (1<<(chunk & 7)); +} + +static __inline__ void yaffs_SetChunkBit(yaffs_Device *dev,int blk,int chunk) +{ + __u8 *blkBits = yaffs_BlockBits(dev,blk); + + blkBits[chunk/8] |= (1<<(chunk & 7)); +} + +static __inline__ int yaffs_CheckChunkBit(yaffs_Device *dev,int blk,int chunk) +{ + __u8 *blkBits = yaffs_BlockBits(dev,blk); + return (blkBits[chunk/8] & (1<<(chunk & 7))) ? 1 :0; +} + +static __inline__ int yaffs_StillSomeChunkBits(yaffs_Device *dev,int blk) +{ + __u8 *blkBits = yaffs_BlockBits(dev,blk); + int i; + for(i = 0; i < dev->chunkBitmapStride; i++) + { + if(*blkBits) return 1; + blkBits++; + } + return 0; +} + +// Function to manipulate block info +static __inline__ yaffs_BlockInfo* yaffs_GetBlockInfo(yaffs_Device *dev, int blk) +{ + if(blk < dev->startBlock || blk > dev->endBlock) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),blk)); + YBUG(); + } + return &dev->blockInfo[blk - dev->startBlock]; +} + + +static __inline__ int yaffs_HashFunction(int n) +{ + return (n % YAFFS_NOBJECT_BUCKETS); +} + + +yaffs_Object *yaffs_Root(yaffs_Device *dev) +{ + return dev->rootDir; +} + +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev) +{ + return dev->lostNFoundDir; +} + + +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare) +{ + if(chunkInNAND < dev->startBlock * dev->nChunksPerBlock) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d is not valid" TENDSTR),chunkInNAND)); + return YAFFS_FAIL; + } + + dev->nPageWrites++; + return dev->writeChunkToNAND(dev,chunkInNAND,data,spare); +} + + + +int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev, + int chunkInNAND, + __u8 *data, + yaffs_Spare *spare, + int doErrorCorrection) +{ + int retVal; + yaffs_Spare localSpare; + + dev->nPageReads++; + + + + + if(!spare && data) + { + // If we don't have a real spare, then we use a local one. + // Need this for the calculation of the ecc + spare = &localSpare; + } + + + if(!dev->useNANDECC) + { + retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,spare); + if(data && doErrorCorrection) + { + // Do ECC correction + //Todo handle any errors + int eccResult1,eccResult2; + __u8 calcEcc[3]; + + yaffs_ECCCalculate(data,calcEcc); + eccResult1 = yaffs_ECCCorrect (data,spare->ecc1, calcEcc); + yaffs_ECCCalculate(&data[256],calcEcc); + eccResult2 = yaffs_ECCCorrect(&data[256],spare->ecc2, calcEcc); + + if(eccResult1>0) + { + T(YAFFS_TRACE_ERROR, (TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND)); + dev->eccFixed++; + } + else if(eccResult1<0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND)); + dev->eccUnfixed++; + } + + if(eccResult2>0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND)); + dev->eccFixed++; + } + else if(eccResult2<0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND)); + dev->eccUnfixed++; + } + + if(eccResult1 || eccResult2) + { + // Hoosterman, we had a data problem on this page + yaffs_HandleReadDataError(dev,chunkInNAND); + } + } + } + else + { + // Must allocate enough memory for spare+2*sizeof(int) for ecc results from device. + struct yaffs_NANDSpare nspare; + retVal = dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare*)&nspare); + memcpy (spare, &nspare, sizeof(yaffs_Spare)); + if(data && doErrorCorrection) + { + if(nspare.eccres1>0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:0" TENDSTR),chunkInNAND)); + } + else if(nspare.eccres1<0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:0" TENDSTR),chunkInNAND)); + } + + if(nspare.eccres2>0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error fix performed on chunk %d:1" TENDSTR),chunkInNAND)); + } + else if(nspare.eccres2<0) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>>ecc error unfixed on chunk %d:1" TENDSTR),chunkInNAND)); + } + + if(nspare.eccres1 || nspare.eccres2) + { + // Hoosterman, we had a data problem on this page + yaffs_HandleReadDataError(dev,chunkInNAND); + } + + } + } + return retVal; +} + + +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,int chunkInNAND) +{ + + static int init = 0; + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK]; + static __u8 data[YAFFS_BYTES_PER_CHUNK]; + // Might as well always allocate the larger size for dev->useNANDECC == true; + static __u8 spare[sizeof(struct yaffs_NANDSpare)]; + + dev->readChunkFromNAND(dev,chunkInNAND,data,(yaffs_Spare *)spare); + + if(!init) + { + memset(cmpbuf,0xff,YAFFS_BYTES_PER_CHUNK); + init = 1; + } + + if(memcmp(cmpbuf,data,YAFFS_BYTES_PER_CHUNK)) return YAFFS_FAIL; + if(memcmp(cmpbuf,spare,16)) return YAFFS_FAIL; + + + return YAFFS_OK; + +} + + + +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND) +{ + dev->nBlockErasures++; + return dev->eraseBlockInNAND(dev,blockInNAND); +} + +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev) +{ + return dev->initialiseNAND(dev); +} + +static int yaffs_WriteNewChunkToNAND(struct yaffs_DeviceStruct *dev, const __u8 *data, yaffs_Spare *spare,int useReserve) +{ + int chunk; + + int writeOk = 1; + int attempts = 0; + + unsigned char rbData[YAFFS_BYTES_PER_CHUNK]; + yaffs_Spare rbSpare; + + do{ + chunk = yaffs_AllocateChunk(dev,useReserve); + + if(chunk >= 0) + { + + // First check this chunk is erased... +#ifndef CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK + writeOk = yaffs_CheckChunkErased(dev,chunk); +#endif + if(!writeOk) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs chunk %d was not erased" TENDSTR),chunk)); + } + else + { + writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare); + } + attempts++; + if(writeOk) + { + // Readback & verify + // If verify fails, then delete this chunk and try again + // To verify we compare everything except the block and + // page status bytes. + // NB We check a raw read without ECC correction applied + yaffs_ReadChunkFromNAND(dev,chunk,rbData,&rbSpare,0); + +#ifndef CONFIG_YAFFS_DISABLE_WRITE_VERIFY + if(!yaffs_VerifyCompare(data,rbData,spare,&rbSpare)) + { + // Didn't verify + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write verify failed on chunk %d" TENDSTR), chunk)); + + writeOk = 0; + } +#endif + + } + if(writeOk) + { + // Copy the data into the write buffer. + // NB We do this at the end to prevent duplicates in the case of a write error. + //Todo + yaffs_HandleWriteChunkOk(dev,chunk,data,spare); + } + else + { + yaffs_HandleWriteChunkError(dev,chunk); + } + } + + } while(chunk >= 0 && ! writeOk); + + if(attempts > 1) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> yaffs write required %d attempts" TENDSTR),attempts)); + dev->nRetriedWrites+= (attempts - 1); + } + + return chunk; +} + +/// +// Functions for robustisizing +// +// + +static void yaffs_RetireBlock(yaffs_Device *dev,int blockInNAND) +{ + // Ding the blockStatus in the first two pages of the block. + + yaffs_Spare spare; + + memset(&spare, 0xff,sizeof(yaffs_Spare)); + + spare.blockStatus = 0; + + // TODO change this retirement marking for other NAND types + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL , &spare); + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1, NULL , &spare); + + yaffs_GetBlockInfo(dev,blockInNAND)->blockState = YAFFS_BLOCK_STATE_DEAD; + dev->nRetiredBlocks++; +} + + + +static int yaffs_RewriteBufferedBlock(yaffs_Device *dev) +{ + dev->doingBufferedBlockRewrite = 1; + // + // Remove erased chunks + // Rewrite existing chunks to a new block + // Set current write block to the new block + + dev->doingBufferedBlockRewrite = 0; + + return 1; +} + + +static void yaffs_HandleReadDataError(yaffs_Device *dev,int chunkInNAND) +{ + int blockInNAND = chunkInNAND/dev->nChunksPerBlock; + + // Mark the block for retirement + yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1; + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>>Block %d marked for retirement" TENDSTR),blockInNAND)); + + + //TODO + // Just do a garbage collection on the affected block then retire the block + // NB recursion +} + + +static void yaffs_CheckWrittenBlock(yaffs_Device *dev,int chunkInNAND) +{ +} + +static void yaffs_HandleWriteChunkOk(yaffs_Device *dev,int chunkInNAND,const __u8 *data, const yaffs_Spare *spare) +{ +} + +static void yaffs_HandleUpdateChunk(yaffs_Device *dev,int chunkInNAND, const yaffs_Spare *spare) +{ +} + +static void yaffs_HandleWriteChunkError(yaffs_Device *dev,int chunkInNAND) +{ + int blockInNAND = chunkInNAND/dev->nChunksPerBlock; + + // Mark the block for retirement + yaffs_GetBlockInfo(dev,blockInNAND)->needsRetiring = 1; + // Delete the chunk + yaffs_DeleteChunk(dev,chunkInNAND,1); +} + + + + +static int yaffs_VerifyCompare(const __u8 *d0, const __u8 * d1, const yaffs_Spare *s0, const yaffs_Spare *s1) +{ + + + if( memcmp(d0,d1,YAFFS_BYTES_PER_CHUNK) != 0 || + s0->tagByte0 != s1->tagByte0 || + s0->tagByte1 != s1->tagByte1 || + s0->tagByte2 != s1->tagByte2 || + s0->tagByte3 != s1->tagByte3 || + s0->tagByte4 != s1->tagByte4 || + s0->tagByte5 != s1->tagByte5 || + s0->tagByte6 != s1->tagByte6 || + s0->tagByte7 != s1->tagByte7 || + s0->ecc1[0] != s1->ecc1[0] || + s0->ecc1[1] != s1->ecc1[1] || + s0->ecc1[2] != s1->ecc1[2] || + s0->ecc2[0] != s1->ecc2[0] || + s0->ecc2[1] != s1->ecc2[1] || + s0->ecc2[2] != s1->ecc2[2] ) + { + return 0; + } + + return 1; +} + + +///////////////////////// Object management ////////////////// +// List of spare objects +// The list is hooked together using the first pointer +// in the object + +// static yaffs_Object *yaffs_freeObjects = NULL; + +// static int yaffs_nFreeObjects; + +// static yaffs_ObjectList *yaffs_allocatedObjectList = NULL; + +// static yaffs_ObjectBucket yaffs_objectBucket[YAFFS_NOBJECT_BUCKETS]; + + +static __u16 yaffs_CalcNameSum(const char *name) +{ + __u16 sum = 0; + __u16 i = 1; + + __u8 *bname = (__u8 *)name; + if(bname) + { + while ((*bname) && (i <=YAFFS_MAX_NAME_LENGTH)) + { + +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE + sum += toupper(*bname) * i; +#else + sum += (*bname) * i; +#endif + i++; + bname++; + } + } + return sum; +} + +void yaffs_SetObjectName(yaffs_Object *obj, const char *name) +{ +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM + if(name && strlen(name) <= YAFFS_SHORT_NAME_LENGTH) + { + strcpy(obj->shortName,name); + } + else + { + obj->shortName[0]='\0'; + } +#endif + obj->sum = yaffs_CalcNameSum(name); +} + +void yaffs_CalcECC(const __u8 *data, yaffs_Spare *spare) +{ + yaffs_ECCCalculate(data , spare->ecc1); + yaffs_ECCCalculate(&data[256] , spare->ecc2); +} + +void yaffs_CalcTagsECC(yaffs_Tags *tags) +{ + // Calculate an ecc + + unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes; + unsigned i,j; + unsigned ecc = 0; + unsigned bit = 0; + + tags->ecc = 0; + + for(i = 0; i < 8; i++) + { + for(j = 1; j &0xff; j<<=1) + { + bit++; + if(b[i] & j) + { + ecc ^= bit; + } + } + } + + tags->ecc = ecc; + + +} + +int yaffs_CheckECCOnTags(yaffs_Tags *tags) +{ + unsigned ecc = tags->ecc; + + yaffs_CalcTagsECC(tags); + + ecc ^= tags->ecc; + + if(ecc && ecc <= 64) + { + // TODO: Handle the failure better. Retire? + unsigned char *b = ((yaffs_TagsUnion *)tags)->asBytes; + + ecc--; + + b[ecc / 8] ^= (1 << (ecc & 7)); + + // Now recvalc the ecc + yaffs_CalcTagsECC(tags); + + return 1; // recovered error + } + else if(ecc) + { + // Wierd ecc failure value + // TODO Need to do somethiong here + return -1; //unrecovered error + } + + return 0; +} + + +///////////////////////// TNODES /////////////////////// + +// List of spare tnodes +// The list is hooked together using the first pointer +// in the tnode. + +//static yaffs_Tnode *yaffs_freeTnodes = NULL; + +// static int yaffs_nFreeTnodes; + +//static yaffs_TnodeList *yaffs_allocatedTnodeList = NULL; + + + +// yaffs_CreateTnodes creates a bunch more tnodes and +// adds them to the tnode free list. +// Don't use this function directly + +static int yaffs_CreateTnodes(yaffs_Device *dev,int nTnodes) +{ + int i; + yaffs_Tnode *newTnodes; + yaffs_TnodeList *tnl; + + if(nTnodes < 1) return YAFFS_OK; + + // make these things + + newTnodes = YMALLOC(nTnodes * sizeof(yaffs_Tnode)); + + if (!newTnodes) + { + T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not allocate Tnodes"TENDSTR))); + return YAFFS_FAIL; + } + + // Hook them into the free list + for(i = 0; i < nTnodes - 1; i++) + { + newTnodes[i].internal[0] = &newTnodes[i+1]; +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = 1; +#endif + } + + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes; +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = 1; +#endif + dev->freeTnodes = newTnodes; + dev->nFreeTnodes+= nTnodes; + dev->nTnodesCreated += nTnodes; + + // Now add this bunch of tnodes to a list for freeing up. + // NB If we can't add this to the management list it isn't fatal + // but it just means we can't free this bunch of tnodes later. + tnl = YMALLOC(sizeof(yaffs_TnodeList)); + if(!tnl) + { + T(YAFFS_TRACE_ERROR,(TSTR("yaffs: Could not add tnodes to management list" TENDSTR))); + + } + else + { + tnl->tnodes = newTnodes; + tnl->next = dev->allocatedTnodeList; + dev->allocatedTnodeList = tnl; + } + + + T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Tnodes added" TENDSTR))); + + + return YAFFS_OK; +} + + +// GetTnode gets us a clean tnode. Tries to make allocate more if we run out +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device *dev) +{ + yaffs_Tnode *tn = NULL; + + // If there are none left make more + if(!dev->freeTnodes) + { + yaffs_CreateTnodes(dev,YAFFS_ALLOCATION_NTNODES); + } + + if(dev->freeTnodes) + { + tn = dev->freeTnodes; +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG + if(tn->internal[YAFFS_NTNODES_INTERNAL] != 1) + { + // Hoosterman, this thing looks like it isn't in the list + T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 1" TENDSTR))); + } +#endif + dev->freeTnodes = dev->freeTnodes->internal[0]; + dev->nFreeTnodes--; + // zero out + memset(tn,0,sizeof(yaffs_Tnode)); + } + + + return tn; +} + + +// FreeTnode frees up a tnode and puts it back on the free list +static void yaffs_FreeTnode(yaffs_Device*dev, yaffs_Tnode *tn) +{ + if(tn) + { +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG + if(tn->internal[YAFFS_NTNODES_INTERNAL] != 0) + { + // Hoosterman, this thing looks like it is already in the list + T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: Tnode list bug 2" TENDSTR))); + } + tn->internal[YAFFS_NTNODES_INTERNAL] = 1; +#endif + tn->internal[0] = dev->freeTnodes; + dev->freeTnodes = tn; + dev->nFreeTnodes++; + } +} + + +static void yaffs_DeinitialiseTnodes(yaffs_Device*dev) +{ + // Free the list of allocated tnodes + yaffs_TnodeList *tmp; + + while(dev->allocatedTnodeList) + { + tmp = dev->allocatedTnodeList->next; + + YFREE(dev->allocatedTnodeList->tnodes); + YFREE(dev->allocatedTnodeList); + dev->allocatedTnodeList = tmp; + + } + + dev->freeTnodes = NULL; + dev->nFreeTnodes = 0; +} + +static void yaffs_InitialiseTnodes(yaffs_Device*dev) +{ + dev->allocatedTnodeList = NULL; + dev->freeTnodes = NULL; + dev->nFreeTnodes = 0; + dev->nTnodesCreated = 0; + +} + +#if 0 +void yaffs_TnodeTest(yaffs_Device *dev) +{ + + int i; + int j; + yaffs_Tnode *tn[1000]; + + YINFO("Testing TNodes"); + + for(j = 0; j < 50; j++) + { + for(i = 0; i < 1000; i++) + { + tn[i] = yaffs_GetTnode(dev); + if(!tn[i]) + { + YALERT("Getting tnode failed"); + } + } + for(i = 0; i < 1000; i+=3) + { + yaffs_FreeTnode(dev,tn[i]); + tn[i] = NULL; + } + + } +} +#endif + + +////////////////// END OF TNODE MANIPULATION /////////////////////////// + +/////////////// Functions to manipulate the look-up tree (made up of tnodes) +// The look up tree is represented by the top tnode and the number of topLevel +// in the tree. 0 means only the level 0 tnode is in the tree. + + +// FindLevel0Tnode finds the level 0 tnode, if one exists. +// Used when reading..... +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device *dev,yaffs_FileStructure *fStruct, __u32 chunkId) +{ + + yaffs_Tnode *tn = fStruct->top; + __u32 i; + int requiredTallness; + int level = fStruct->topLevel; + + // Check sane level and chunk Id + if(level < 0 || level > YAFFS_TNODES_MAX_LEVEL) + { +// char str[50]; +// sprintf(str,"Bad level %d",level); +// YALERT(str); + return NULL; + } + + if(chunkId > YAFFS_MAX_CHUNK_ID) + { +// char str[50]; +// sprintf(str,"Bad chunkId %d",chunkId); +// YALERT(str); + return NULL; + } + + // First check we're tall enough (ie enough topLevel) + + i = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS); + requiredTallness = 0; + while(i) + { + i >>= YAFFS_TNODES_INTERNAL_BITS; + requiredTallness++; + } + + + if(requiredTallness > fStruct->topLevel) + { + // Not tall enough, so we can't find it, return NULL. + return NULL; + } + + + // Traverse down to level 0 + while (level > 0 && tn) + { + tn = tn->internal[(chunkId >>(/* dev->chunkGroupBits + */ YAFFS_TNODES_LEVEL0_BITS + (level-1) * YAFFS_TNODES_INTERNAL_BITS)) & + YAFFS_TNODES_INTERNAL_MASK]; + level--; + + } + + return tn; +} + +// AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree. +// This happens in two steps: +// 1. If the tree isn't tall enough, then make it taller. +// 2. Scan down the tree towards the level 0 tnode adding tnodes if required. +// +// Used when modifying the tree. +// +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device *dev, yaffs_FileStructure *fStruct, __u32 chunkId) +{ + + yaffs_Tnode *tn; + + int requiredTallness; + int i; + int l; + + __u32 x; + + + //T((TSTR("AddOrFind topLevel=%d, chunk=%d"),fStruct->topLevel,chunkId)); + + // Check sane level and page Id + if(fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) + { +// char str[50]; +// sprintf(str,"Bad level %d",fStruct->topLevel); +// YALERT(str); + return NULL; + } + + if(chunkId > YAFFS_MAX_CHUNK_ID) + { +// char str[50]; +// sprintf(str,"Bad chunkId %d",chunkId); +// YALERT(str); + return NULL; + } + + // First check we're tall enough (ie enough topLevel) + + x = chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS); + requiredTallness = 0; + while(x) + { + x >>= YAFFS_TNODES_INTERNAL_BITS; + requiredTallness++; + } + + //T((TSTR(" required=%d"),requiredTallness)); + + + if(requiredTallness > fStruct->topLevel) + { + // Not tall enough,gotta make the tree taller + for(i = fStruct->topLevel; i < requiredTallness; i++) + { + //T((TSTR(" add new top"))); + + tn = yaffs_GetTnode(dev); + + if(tn) + { + tn->internal[0] = fStruct->top; + fStruct->top = tn; + } + else + { + T(YAFFS_TRACE_ERROR,(TSTR("yaffs: no more tnodes" TENDSTR))); + } + } + + fStruct->topLevel = requiredTallness; + } + + + // Traverse down to level 0, adding anything we need + + l = fStruct->topLevel; + tn = fStruct->top; + while (l > 0 && tn) + { + x = (chunkId >> (/*dev->chunkGroupBits + */YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & + YAFFS_TNODES_INTERNAL_MASK; + + //T((TSTR(" [%d:%d]"),l,i)); + + if(!tn->internal[x]) + { + //T((TSTR(" added"))); + + tn->internal[x] = yaffs_GetTnode(dev); + } + + tn = tn->internal[x]; + l--; + + } + + //TSTR(TENDSTR))); + + return tn; +} + +// DeleteWorker scans backwards through the tnode tree and deletes all the +// chunks and tnodes in the file +// Returns 1 if the tree was deleted. Returns 0 if it stopped early due to hitting the limit and the delete is incomplete. + +static int yaffs_DeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset,int *limit) +{ + int i; + int chunkInInode; + int theChunk; + yaffs_Tags tags; + int found; + int chunkDeleted; + int allDone = 1; + + + if(tn) + { + if(level > 0) + { + + for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--) + { + if(tn->internal[i]) + { + if(limit && (*limit) < 0) + { + allDone = 0; + } + else + { + allDone = yaffs_DeleteWorker(in,tn->internal[i],level - 1, + (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i ,limit); + } + if(allDone) + { + yaffs_FreeTnode(in->myDev,tn->internal[i]); + tn->internal[i] = NULL; + } + } + + } + return (allDone) ? 1 : 0; + } + else if(level == 0) + { + int hitLimit = 0; + + for(i = YAFFS_NTNODES_LEVEL0 -1; i >= 0 && !hitLimit; i--) + { + if(tn->level0[i]) + { + int j; + + chunkInInode = (chunkOffset << YAFFS_TNODES_LEVEL0_BITS ) + i; + + theChunk = tn->level0[i] << in->myDev->chunkGroupBits; + + // Now we need to search for it + for(j = 0,found = 0; theChunk && j < in->myDev->chunkGroupSize && !found; j++) + { + yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,&tags,&chunkDeleted); + if(yaffs_TagsMatch(&tags,in->objectId,chunkInInode,chunkDeleted)) + { + // found it; + found = 1; + + } + else + { + theChunk++; + } + } + + if(found) + { + yaffs_DeleteChunk(in->myDev,theChunk,1); + in->nDataChunks--; + if(limit) + { + *limit = *limit-1; + if(*limit <= 0) + { + hitLimit = 1; + } + } + + } + + tn->level0[i] = 0; + } + + } + return (i < 0) ? 1 : 0; + + + } + + } + + return 1; + +} + +// SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file. +// All soft deleting does is increment the block's softdelete count and pulls the chunk out +// of the tnode. +// THus, essentially this is the same as DeleteWorker except that the chunks are soft deleted. +// +static int yaffs_SoftDeleteWorker(yaffs_Object *in, yaffs_Tnode *tn, __u32 level, int chunkOffset) +{ + int i; + int chunkInInode; + int theChunk; + yaffs_BlockInfo *theBlock; + yaffs_Tags tags; + int found; + int chunkDeleted; + int allDone = 1; + + + if(tn) + { + if(level > 0) + { + + for(i = YAFFS_NTNODES_INTERNAL -1; allDone && i >= 0; i--) + { + if(tn->internal[i]) + { + allDone = yaffs_SoftDeleteWorker(in,tn->internal[i],level - 1, + (chunkOffset << YAFFS_TNODES_INTERNAL_BITS ) + i); + if(allDone) + { + yaffs_FreeTnode(in->myDev,tn->internal[i]); + tn->internal[i] = NULL; + } + else + { + //Hoosterman... how could this happen. + } + } + } + return (allDone) ? 1 : 0; + } + else if(level == 0) + { + + for(i = YAFFS_NTNODES_LEVEL0 -1; i >=0; i--) + { + if(tn->level0[i]) + { + // Note this does not find the real chunk, only the chunk group. + // We make an assumption that a chunk group is niot larger than a block. + theChunk = (tn->level0[i] << in->myDev->chunkGroupBits); + T(YAFFS_TRACE_SCAN,(TSTR("soft delete tch %d cgb %d chunk %d" TENDSTR), + tn->level0[i],in->myDev->chunkGroupBits,theChunk)); + + theBlock = yaffs_GetBlockInfo(in->myDev, theChunk/in->myDev->nChunksPerBlock); + if(theBlock) + { + theBlock->softDeletions++; + } + tn->level0[i] = 0; + } + + } + return 1; + + } + + } + + return 1; + +} + + + +static void yaffs_SoftDeleteFile(yaffs_Object *obj) +{ + if(obj->deleted && + obj->variantType == YAFFS_OBJECT_TYPE_FILE && + !obj->softDeleted) + { + if(obj->nDataChunks <= 0) + { + // Empty file, just delete it immediately + yaffs_FreeTnode(obj->myDev,obj->variant.fileVariant.top); + obj->variant.fileVariant.top = NULL; + T(YAFFS_TRACE_TRACING,(TSTR("yaffs: Deleting empty file %d" TENDSTR),obj->objectId)); + yaffs_DoGenericObjectDeletion(obj); + } + else + { + yaffs_SoftDeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0); + obj->softDeleted = 1; + } + } +} + + + + + +// Pruning removes any part of the file structure tree that is beyond the +// bounds of the file (ie that does not point to chunks). +// +// A file should only get pruned when its size is reduced. +// +// Before pruning, the chunks must be pulled from the tree and the +// level 0 tnode entries must be zeroed out. +// Could also use this for file deletion, but that's probably better handled +// by a special case. + +// yaffs_PruneWorker should only be called by yaffs_PruneFileStructure() + +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device *dev, yaffs_Tnode *tn, __u32 level, int del0) +{ + int i; + int hasData; + + if(tn) + { + hasData = 0; + + for(i = 0; i < YAFFS_NTNODES_INTERNAL; i++) + { + if(tn->internal[i] && level > 0) + { + tn->internal[i] = yaffs_PruneWorker(dev,tn->internal[i],level - 1, ( i == 0) ? del0 : 1); + } + + if(tn->internal[i]) + { + hasData++; + } + } + + if(hasData == 0 && del0) + { + // Free and return NULL + + yaffs_FreeTnode(dev,tn); + tn = NULL; + } + + } + + return tn; + +} + +static int yaffs_PruneFileStructure(yaffs_Device *dev, yaffs_FileStructure *fStruct) +{ + int i; + int hasData; + int done = 0; + yaffs_Tnode *tn; + + if(fStruct->topLevel > 0) + { + fStruct->top = yaffs_PruneWorker(dev,fStruct->top, fStruct->topLevel,0); + + // Now we have a tree with all the non-zero branches NULL but the height + // is the same as it was. + // Let's see if we can trim internal tnodes to shorten the tree. + // We can do this if only the 0th element in the tnode is in use + // (ie all the non-zero are NULL) + + while(fStruct->topLevel && !done) + { + tn = fStruct->top; + + hasData = 0; + for(i = 1; i internal[i]) + { + hasData++; + } + } + + if(!hasData) + { + fStruct->top = tn->internal[0]; + fStruct->topLevel--; + yaffs_FreeTnode(dev,tn); + } + else + { + done = 1; + } + } + } + + return YAFFS_OK; +} + + + + + +/////////////////////// End of File Structure functions. ///////////////// + +// yaffs_CreateFreeObjects creates a bunch more objects and +// adds them to the object free list. +static int yaffs_CreateFreeObjects(yaffs_Device *dev, int nObjects) +{ + int i; + yaffs_Object *newObjects; + yaffs_ObjectList *list; + + if(nObjects < 1) return YAFFS_OK; + + // make these things + + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object)); + + if (!newObjects) + { + T(YAFFS_TRACE_ALLOCATE,(TSTR("yaffs: Could not allocate more objects" TENDSTR))); + return YAFFS_FAIL; + } + + // Hook them into the free list + for(i = 0; i < nObjects - 1; i++) + { + newObjects[i].siblings.next = (struct list_head *)(&newObjects[i+1]); + } + + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects; + dev->freeObjects = newObjects; + dev->nFreeObjects+= nObjects; + dev->nObjectsCreated+= nObjects; + + // Now add this bunch of Objects to a list for freeing up. + + list = YMALLOC(sizeof(yaffs_ObjectList)); + if(!list) + { + T(YAFFS_TRACE_ALLOCATE,(TSTR("Could not add objects to management list" TENDSTR))); + } + else + { + list->objects = newObjects; + list->next = dev->allocatedObjectList; + dev->allocatedObjectList = list; + } + + + + return YAFFS_OK; +} + + +// AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device *dev) +{ + yaffs_Object *tn = NULL; + + // If there are none left make more + if(!dev->freeObjects) + { + yaffs_CreateFreeObjects(dev,YAFFS_ALLOCATION_NOBJECTS); + } + + if(dev->freeObjects) + { + tn = dev->freeObjects; + dev->freeObjects = (yaffs_Object *)(dev->freeObjects->siblings.next); + dev->nFreeObjects--; + + // Now sweeten it up... + + memset(tn,0,sizeof(yaffs_Object)); + tn->myDev = dev; + tn->chunkId = -1; + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN; + INIT_LIST_HEAD(&(tn->hardLinks)); + INIT_LIST_HEAD(&(tn->hashLink)); + INIT_LIST_HEAD(&tn->siblings); + + // Add it to the lost and found directory. + // NB Can't put root or lostNFound in lostNFound so + // check if lostNFound exists first + if(dev->lostNFoundDir) + { + yaffs_AddObjectToDirectory(dev->lostNFoundDir,tn); + } + } + + + return tn; +} + +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device *dev,int number,__u32 mode) +{ + + yaffs_Object *obj = yaffs_CreateNewObject(dev,number,YAFFS_OBJECT_TYPE_DIRECTORY); + if(obj) + { + obj->fake = 1; // it is fake so it has no NAND presence... + obj->renameAllowed= 0; // ... and we're not allowed to rename it... + obj->unlinkAllowed= 0; // ... or unlink it + obj->deleted = 0; + obj->unlinked = 0; + obj->st_mode = mode; + obj->myDev = dev; + obj->chunkId = 0; // Not a valid chunk. + } + + return obj; + +} + + +static void yaffs_UnhashObject(yaffs_Object *tn) +{ + int bucket; + yaffs_Device *dev = tn->myDev; + + + // If it is still linked into the bucket list, free from the list + if(!list_empty(&tn->hashLink)) + { + list_del_init(&tn->hashLink); + bucket = yaffs_HashFunction(tn->objectId); + dev->objectBucket[bucket].count--; + } + +} + + +// FreeObject frees up a Object and puts it back on the free list +static void yaffs_FreeObject(yaffs_Object *tn) +{ + + yaffs_Device *dev = tn->myDev; + +#ifdef __KERNEL__ + if(tn->myInode) + { + // We're still hooked up to a cached inode. + // Don't delete now, but mark for later deletion + tn->deferedFree = 1; + return; + } +#endif + + yaffs_UnhashObject(tn); + + // Link into the free list. + tn->siblings.next = (struct list_head *)(dev->freeObjects); + dev->freeObjects = tn; + dev->nFreeObjects++; +} + + +#ifdef __KERNEL__ + +void yaffs_HandleDeferedFree(yaffs_Object *obj) +{ + if(obj->deferedFree) + { + yaffs_FreeObject(obj); + } +} + +#endif + + + +static void yaffs_DeinitialiseObjects(yaffs_Device *dev) +{ + // Free the list of allocated Objects + + yaffs_ObjectList *tmp; + + while( dev->allocatedObjectList) + { + tmp = dev->allocatedObjectList->next; + YFREE(dev->allocatedObjectList->objects); + YFREE(dev->allocatedObjectList); + + dev->allocatedObjectList = tmp; + } + + dev->freeObjects = NULL; + dev->nFreeObjects = 0; +} + +static void yaffs_InitialiseObjects(yaffs_Device *dev) +{ + int i; + + dev->allocatedObjectList = NULL; + dev->freeObjects = NULL; + dev->nFreeObjects = 0; + + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) + { + INIT_LIST_HEAD(&dev->objectBucket[i].list); + dev->objectBucket[i].count = 0; + } + +} + + + + + + +int yaffs_FindNiceObjectBucket(yaffs_Device *dev) +{ + static int x = 0; + int i; + int l = 999; + int lowest = 999999; + + + // First let's see if we can find one that's empty. + + for(i = 0; i < 10 && lowest > 0; i++) + { + x++; + x %= YAFFS_NOBJECT_BUCKETS; + if(dev->objectBucket[x].count < lowest) + { + lowest = dev->objectBucket[x].count; + l = x; + } + + } + + // If we didn't find an empty list, then try + // looking a bit further for a short one + + for(i = 0; i < 10 && lowest > 3; i++) + { + x++; + x %= YAFFS_NOBJECT_BUCKETS; + if(dev->objectBucket[x].count < lowest) + { + lowest = dev->objectBucket[x].count; + l = x; + } + + } + + return l; +} + +static int yaffs_CreateNewObjectNumber(yaffs_Device *dev) +{ + int bucket = yaffs_FindNiceObjectBucket(dev); + + // Now find an object value that has not already been taken + // by scanning the list. + + int found = 0; + struct list_head *i; + + __u32 n = (__u32)bucket; + + //yaffs_CheckObjectHashSanity(); + + while(!found) + { + found = 1; + n += YAFFS_NOBJECT_BUCKETS; + if(1 ||dev->objectBucket[bucket].count > 0) + { + list_for_each(i,&dev->objectBucket[bucket].list) + { + // If there is already one in the list + if(i && list_entry(i, yaffs_Object,hashLink)->objectId == n) + { + found = 0; + } + } + } + } + + //T(("bucket %d count %d inode %d\n",bucket,yaffs_objectBucket[bucket].count,n); + + return n; +} + +void yaffs_HashObject(yaffs_Object *in) +{ + int bucket = yaffs_HashFunction(in->objectId); + yaffs_Device *dev = in->myDev; + + if(!list_empty(&in->hashLink)) + { + //YINFO("!!!"); + } + + + list_add(&in->hashLink,&dev->objectBucket[bucket].list); + dev->objectBucket[bucket].count++; + +} + +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number) +{ + int bucket = yaffs_HashFunction(number); + struct list_head *i; + yaffs_Object *in; + + list_for_each(i,&dev->objectBucket[bucket].list) + { + // Look if it is in the list + if(i) + { + in = list_entry(i, yaffs_Object,hashLink); + if(in->objectId == number) + { + return in; + } + } + } + + return NULL; +} + + + +yaffs_Object *yaffs_CreateNewObject(yaffs_Device *dev,int number,yaffs_ObjectType type) +{ + + yaffs_Object *theObject; + + if(number < 0) + { + number = yaffs_CreateNewObjectNumber(dev); + } + + theObject = yaffs_AllocateEmptyObject(dev); + + if(theObject) + { + theObject->fake = 0; + theObject->renameAllowed = 1; + theObject->unlinkAllowed = 1; + theObject->objectId = number; + yaffs_HashObject(theObject); + theObject->variantType = type; +#ifdef CONFIG_YAFFS_WINCE + yfsd_WinFileTimeNow(theObject->win_atime); + theObject->win_ctime[0] = theObject->win_mtime[0] = theObject->win_atime[0]; + theObject->win_ctime[1] = theObject->win_mtime[1] = theObject->win_atime[1]; + +#else + + theObject->st_atime = theObject->st_mtime = theObject->st_ctime = Y_CURRENT_TIME; + +#endif + switch(type) + { + case YAFFS_OBJECT_TYPE_FILE: + theObject->variant.fileVariant.fileSize = 0; + theObject->variant.fileVariant.scannedFileSize = 0; + theObject->variant.fileVariant.topLevel = 0; + theObject->variant.fileVariant.top = yaffs_GetTnode(dev); + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + INIT_LIST_HEAD(&theObject->variant.directoryVariant.children); + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + // No action required + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + // No action required + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + // No action required + break; + case YAFFS_OBJECT_TYPE_UNKNOWN: + // todo this should not happen + break; + } + } + + return theObject; +} + +yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device *dev, int number,yaffs_ObjectType type) +{ + yaffs_Object *theObject = NULL; + + if(number > 0) + { + theObject = yaffs_FindObjectByNumber(dev,number); + } + + if(!theObject) + { + theObject = yaffs_CreateNewObject(dev,number,type); + } + + return theObject; + +} + +char *yaffs_CloneString(const char *str) +{ + char *newStr = NULL; + + if(str && *str) + { + newStr = YMALLOC(strlen(str) + 1); + strcpy(newStr,str); + } + + return newStr; + +} + +// +// Mknod (create) a new object. +// equivalentObject only has meaning for a hard link; +// aliasString only has meaning for a sumlink. +// rdev only has meaning for devices (a subset of special objects) +yaffs_Object *yaffs_MknodObject( yaffs_ObjectType type, + yaffs_Object *parent, + const char *name, + __u32 mode, + __u32 uid, + __u32 gid, + yaffs_Object *equivalentObject, + const char *aliasString, + __u32 rdev) +{ + yaffs_Object *in; + + yaffs_Device *dev = parent->myDev; + + // Check if the entry exists. If it does then fail the call since we don't want a dup. + if(yaffs_FindObjectByName(parent,name)) + { + return NULL; + } + + in = yaffs_CreateNewObject(dev,-1,type); + + if(in) + { + in->chunkId = -1; + in->valid = 1; + in->variantType = type; + + in->st_mode = mode; + +#ifdef CONFIG_YAFFS_WINCE + yfsd_WinFileTimeNow(in->win_atime); + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0]; + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1]; + +#else + + in->st_atime = in->st_mtime = in->st_ctime = Y_CURRENT_TIME; + in->st_rdev = rdev; + in->st_uid = uid; + in->st_gid = gid; +#endif + in->nDataChunks = 0; + + yaffs_SetObjectName(in,name); + in->dirty = 1; + + yaffs_AddObjectToDirectory(parent,in); + + in->myDev = parent->myDev; + + + switch(type) + { + case YAFFS_OBJECT_TYPE_SYMLINK: + in->variant.symLinkVariant.alias = yaffs_CloneString(aliasString); + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + in->variant.hardLinkVariant.equivalentObject = equivalentObject; + in->variant.hardLinkVariant.equivalentObjectId = equivalentObject->objectId; + list_add(&in->hardLinks,&equivalentObject->hardLinks); + break; + case YAFFS_OBJECT_TYPE_FILE: // do nothing + case YAFFS_OBJECT_TYPE_DIRECTORY: // do nothing + case YAFFS_OBJECT_TYPE_SPECIAL: // do nothing + case YAFFS_OBJECT_TYPE_UNKNOWN: + break; + } + + if(/*yaffs_GetNumberOfFreeChunks(dev) <= 0 || */ + yaffs_UpdateObjectHeader(in,name,0) < 0) + { + // Could not create the object header, fail the creation + yaffs_AbortHalfCreatedObject(in); + in = NULL; + } + + } + + return in; +} + +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid) +{ + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE,parent,name,mode,uid,gid,NULL,NULL,0); +} + +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid) +{ + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,0); +} + +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid, __u32 rdev) +{ + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY,parent,name,mode,uid,gid,NULL,NULL,rdev); +} + +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,const char *alias) +{ + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK,parent,name,mode,uid,gid,NULL,alias,0); +} + +// NB yaffs_Link returns the object id of the equivalent object. +yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject) +{ + // Get the real object in case we were fed a hard link as an equivalent object + equivalentObject = yaffs_GetEquivalentObject(equivalentObject); + + if(yaffs_MknodObject(YAFFS_OBJECT_TYPE_HARDLINK,parent,name,0,0,0,equivalentObject,NULL,0)) + { + return equivalentObject; + } + else + { + return NULL; + } + +} + + +static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName,int force) +{ + int unlinkOp; + + if(newDir == NULL) + { + newDir = obj->parent; // use the old directory + } + + unlinkOp = (newDir == obj->myDev->unlinkedDir && obj->variantType == YAFFS_OBJECT_TYPE_FILE); + + // If the object is a file going into the unlinked directory, then it is OK to just stuff it in since + // duplicate names are allowed. + // Otherwise only proceed if the new name does not exist and if we're putting it into a directory. + if( (unlinkOp|| + force || + !yaffs_FindObjectByName(newDir,newName)) && + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) + { + yaffs_SetObjectName(obj,newName); + obj->dirty = 1; + + yaffs_AddObjectToDirectory(newDir,obj); + + if(unlinkOp) obj->unlinked = 1; + + + if(yaffs_UpdateObjectHeader(obj,newName,0) >= 0) + { + return YAFFS_OK; + } + } + + return YAFFS_FAIL; +} + + + +int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName) +{ + yaffs_Object *obj; + int force = 0; + +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE + // Special case for WinCE. + // While look-up is case insensitive, the name isn't. + // THerefore we might want to change x.txt to X.txt + if(oldDir == newDir && _stricmp(oldName,newName) == 0) + { + force = 1; + } +#endif + + obj = yaffs_FindObjectByName(oldDir,oldName); + if(obj && obj->renameAllowed) + { + return yaffs_ChangeObjectName(obj,newDir,newName,force); + } + return YAFFS_FAIL; +} + + + +static int yaffs_CheckObjectHashSanity(yaffs_Device *dev) +{ + // Scan the buckets and check that the lists + // have as many members as the count says there are + int bucket; + int countEm; + struct list_head *j; + int ok = YAFFS_OK; + + for(bucket = 0; bucket < YAFFS_NOBJECT_BUCKETS; bucket++) + { + countEm = 0; + + list_for_each(j,&dev->objectBucket[bucket].list) + { + countEm++; + } + + if(countEm != dev->objectBucket[bucket].count) + { + T(YAFFS_TRACE_ERROR,(TSTR("Inode hash inconsistency" TENDSTR))); + ok = YAFFS_FAIL; + } + } + + return ok; +} + +#if 0 +void yaffs_ObjectTest(yaffs_Device *dev) +{ + yaffs_Object *in[1000]; + int inNo[1000]; + yaffs_Object *inold[1000]; + int i; + int j; + + memset(in,0,1000*sizeof(yaffs_Object *)); + memset(inold,0,1000*sizeof(yaffs_Object *)); + + yaffs_CheckObjectHashSanity(dev); + + for(j = 0; j < 10; j++) + { + //T(("%d\n",j)); + + for(i = 0; i < 1000; i++) + { + in[i] = yaffs_CreateNewObject(dev,-1,YAFFS_OBJECT_TYPE_FILE); + if(!in[i]) + { + YINFO("No more inodes"); + } + else + { + inNo[i] = in[i]->objectId; + } + } + + for(i = 0; i < 1000; i++) + { + if(yaffs_FindObjectByNumber(dev,inNo[i]) != in[i]) + { + //T(("Differnce in look up test\n")); + } + else + { + // T(("Look up ok\n")); + } + } + + yaffs_CheckObjectHashSanity(dev); + + for(i = 0; i < 1000; i+=3) + { + yaffs_FreeObject(in[i]); + in[i] = NULL; + } + + + yaffs_CheckObjectHashSanity(dev); + } + +} + +#endif + +/////////////////////////// Block Management and Page Allocation /////////////////// + + +static int yaffs_InitialiseBlocks(yaffs_Device *dev,int nBlocks) +{ + dev->allocationBlock = -1; // force it to get a new one + //Todo we're assuming the malloc will pass. + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo)); + // Set up dynamic blockinfo stuff. + dev->chunkBitmapStride = (dev->nChunksPerBlock+7)/8; + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks); + if(dev->blockInfo && dev->chunkBits) + { + memset(dev->blockInfo,0,nBlocks * sizeof(yaffs_BlockInfo)); + memset(dev->chunkBits,0,dev->chunkBitmapStride * nBlocks); + return YAFFS_OK; + } + + return YAFFS_FAIL; + +} + +static void yaffs_DeinitialiseBlocks(yaffs_Device *dev) +{ + YFREE(dev->blockInfo); + dev->blockInfo = NULL; + YFREE(dev->chunkBits); + dev->chunkBits = NULL; +} + +// FindDiretiestBlock is used to select the dirtiest block (or close enough) +// for garbage collection. + +static int yaffs_FindDirtiestBlock(yaffs_Device *dev,int aggressive) +{ + + int b = dev->currentDirtyChecker; + + int i; + int iterations; + int dirtiest = -1; + int pagesInUse; + yaffs_BlockInfo *bi; + + // If we're doing aggressive GC then we are happy to take a less-dirty block, and + // search further. + + pagesInUse = (aggressive)? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1; + if(aggressive) + { + iterations = dev->endBlock - dev->startBlock + 1; + } + else + { + iterations = dev->endBlock - dev->startBlock + 1; + iterations = iterations / 16; + if(iterations > 200) + { + iterations = 200; + } + } + + for(i = 0; i <= iterations && pagesInUse > 0 ; i++) + { + b++; + if ( b < dev->startBlock || b > dev->endBlock) + { + b = dev->startBlock; + } + + if(b < dev->startBlock || b > dev->endBlock) + { + T(YAFFS_TRACE_ERROR,(TSTR("**>> Block %d is not valid" TENDSTR),b)); + YBUG(); + } + + bi = yaffs_GetBlockInfo(dev,b); + + if(bi->blockState == YAFFS_BLOCK_STATE_FULL && + (bi->pagesInUse - bi->softDeletions )< pagesInUse) + { + dirtiest = b; + pagesInUse = (bi->pagesInUse - bi->softDeletions); + } + } + + dev->currentDirtyChecker = b; + + return dirtiest; +} + + +static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo) +{ + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,blockNo); + + int erasedOk = 0; + + // If the block is still healthy erase it and mark as clean. + // If the block has had a data failure, then retire it. + bi->blockState = YAFFS_BLOCK_STATE_DIRTY; + + if(!bi->needsRetiring) + { + erasedOk = yaffs_EraseBlockInNAND(dev,blockNo); + if(!erasedOk) + { + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Erasure failed %d" TENDSTR),blockNo)); + } + } + + if( erasedOk ) + { + // Clean it up... + bi->blockState = YAFFS_BLOCK_STATE_EMPTY; + dev->nErasedBlocks++; + bi->pagesInUse = 0; + bi->softDeletions = 0; + yaffs_ClearChunkBits(dev,blockNo); + + T(YAFFS_TRACE_ERASE,(TSTR("Erased block %d" TENDSTR),blockNo)); + } + else + { + yaffs_RetireBlock(dev,blockNo); + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,(TSTR("**>> Block %d retired" TENDSTR),blockNo)); + } +} + + +static int yaffs_FindBlockForAllocation(yaffs_Device *dev) +{ + int i; + + yaffs_BlockInfo *bi; + + if(dev->nErasedBlocks < 1) + { + // Hoosterman we've got a problem. + // Can't get space to gc + T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during gc" TENDSTR))); + + return -1; + } + + // Find an empty block. + + for(i = dev->startBlock; i <= dev->endBlock; i++) + { + dev->allocationBlockFinder++; + if(dev->allocationBlockFinder startBlock || dev->allocationBlockFinder> dev->endBlock) + { + dev->allocationBlockFinder = dev->startBlock; + } + + bi = yaffs_GetBlockInfo(dev,dev->allocationBlockFinder); + + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY) + { + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING; + dev->nErasedBlocks--; + return dev->allocationBlockFinder; + } + } + + return -1; +} + + + +static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve) +{ + int retVal; + yaffs_BlockInfo *bi; + + if(dev->allocationBlock < 0) + { + // Get next block to allocate off + dev->allocationBlock = yaffs_FindBlockForAllocation(dev); + dev->allocationPage = 0; + } + + if(!useReserve && dev->nErasedBlocks <= dev->nReservedBlocks) + { + // Not enough space to allocate unless we're allowed to use the reserve. + return -1; + } + + // Next page please.... + if(dev->allocationBlock >= 0) + { + bi = yaffs_GetBlockInfo(dev,dev->allocationBlock); + + retVal = (dev->allocationBlock * dev->nChunksPerBlock) + + dev->allocationPage; + bi->pagesInUse++; + yaffs_SetChunkBit(dev,dev->allocationBlock,dev->allocationPage); + + dev->allocationPage++; + + dev->nFreeChunks--; + + // If the block is full set the state to full + if(dev->allocationPage >= dev->nChunksPerBlock) + { + bi->blockState = YAFFS_BLOCK_STATE_FULL; + dev->allocationBlock = -1; + } + + + return retVal; + + } + T(YAFFS_TRACE_ERROR,(TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR))); + + return -1; +} + +// To determine if we have enough space we just look at the +// number of erased blocks. +// The cache is allowed to use reserved blocks. + +static int yaffs_CheckSpaceForChunkCache(yaffs_Device *dev) +{ + return (dev->nErasedBlocks >= dev->nReservedBlocks); +} + + +static int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block) +{ + int oldChunk; + int newChunk; + int chunkInBlock; + int markNAND; + + + yaffs_Spare spare; + yaffs_Tags tags; + __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + +// yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,block); + + yaffs_Object *object; + + //T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits)); + + for(chunkInBlock = 0,oldChunk = block * dev->nChunksPerBlock; + chunkInBlock < dev->nChunksPerBlock && yaffs_StillSomeChunkBits(dev,block); + chunkInBlock++, oldChunk++ ) + { + if(yaffs_CheckChunkBit(dev,block,chunkInBlock)) + { + + // This page is in use and might need to be copied off + + markNAND = 1; + + //T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk)); + + yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare,1); + + yaffs_GetTagsFromSpare(dev,&spare,&tags); + + object = yaffs_FindObjectByNumber(dev,tags.objectId); + + if(object && object->deleted && tags.chunkId != 0) + { + // Data chunk in a deleted file, throw it away + // It's a deleted data chunk, + // No need to copy this, just forget about it and fix up the + // object. + + //yaffs_PutChunkIntoFile(object, tags.chunkId, 0,0); + object->nDataChunks--; + + if(object->nDataChunks <= 0) + { + // Time to delete the file too + yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top); + object->variant.fileVariant.top = NULL; + T(YAFFS_TRACE_TRACING,(TSTR("yaffs: About to finally delete object %d" TENDSTR),object->objectId)); + yaffs_DoGenericObjectDeletion(object); + } + markNAND = 0; + } + else if( 0 /* Todo object && object->deleted && object->nDataChunks == 0 */) + { + // Deleted object header with no data chunks. + // Can be discarded and the file deleted. + object->chunkId = 0; + yaffs_FreeTnode(object->myDev,object->variant.fileVariant.top); + object->variant.fileVariant.top = NULL; + yaffs_DoGenericObjectDeletion(object); + + } + else if(object) + { + // It's either a data chunk in a live file or + // an ObjectHeader, so we're interested in it. + // NB Need to keep the ObjectHeaders of deleted files + // until the whole file has been deleted off + tags.serialNumber++; + yaffs_LoadTagsIntoSpare(&spare,&tags); + + dev->nGCCopies++; + + newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1); + + if(newChunk < 0) + { + return YAFFS_FAIL; + } + + // Ok, now fix up the Tnodes etc. + + if(tags.chunkId == 0) + { + // It's a header + object->chunkId = newChunk; + object->serial = tags.serialNumber; + } + else + { + // It's a data chunk + yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk,0); + } + } + + yaffs_DeleteChunk(dev,oldChunk,markNAND); + + } + } + + return YAFFS_OK; +} + + +static yaffs_Object *yaffs_FindDeletedUnlinkedFile(yaffs_Device *dev) +{ + // find a file to delete + struct list_head *i; + yaffs_Object *l; + + + //Scan the unlinked files looking for one to delete + list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children) + { + if(i) + { + l = list_entry(i, yaffs_Object,siblings); + if(l->deleted) + { + return l; + } + } + } + return NULL; +} + + +static void yaffs_DoUnlinkedFileDeletion(yaffs_Device *dev) +{ + // This does background deletion on unlinked files.. only deleted ones. + // If we don't have a file we're working on then find one + if(!dev->unlinkedDeletion && dev->nDeletedFiles > 0) + { + dev->unlinkedDeletion = yaffs_FindDeletedUnlinkedFile(dev); + } + + // OK, we're working on a file... + if(dev->unlinkedDeletion) + { + yaffs_Object *obj = dev->unlinkedDeletion; + int delresult; + int limit; // Number of chunks to delete in a file. + // NB this can be exceeded, but not by much. + + limit = -1; + + delresult = yaffs_DeleteWorker(obj, obj->variant.fileVariant.top, obj->variant.fileVariant.topLevel, 0,&limit); + + if(obj->nDataChunks == 0) + { + // Done all the deleting of data chunks. + // Now dump the header and clean up + yaffs_FreeTnode(dev,obj->variant.fileVariant.top); + obj->variant.fileVariant.top = NULL; + yaffs_DoGenericObjectDeletion(obj); + dev->nDeletedFiles--; + dev->nUnlinkedFiles--; + dev->nBackgroundDeletions++; + dev->unlinkedDeletion = NULL; + } + } +} + + +#if 0 +#define YAFFS_GARBAGE_COLLECT_LOW_WATER 2 +static int yaffs_CheckGarbageCollection(yaffs_Device *dev) +{ + int block; + int aggressive=0; + + //yaffs_DoUnlinkedFileDeletion(dev); + + if(dev->nErasedBlocks <= (dev->nReservedBlocks + YAFFS_GARBAGE_COLLECT_LOW_WATER)) + { + aggressive = 1; + } + + if(aggressive) + { + block = yaffs_FindDirtiestBlock(dev,aggressive); + + if(block >= 0) + { + dev->garbageCollections++; + return yaffs_GarbageCollectBlock(dev,block); + } + else + { + return YAFFS_FAIL; + } + } + + return YAFFS_OK; +} +#endif + +// New garbage collector +// If we're very low on erased blocks then we do aggressive garbage collection +// otherwise we do "passive" garbage collection. +// Aggressive gc looks further (whole array) and will accept dirtier blocks. +// Passive gc only inspects smaller areas and will only accept cleaner blocks. +// +// The idea is to help clear out space in a more spread-out manner. +// Dunno if it really does anything useful. +// +static int yaffs_CheckGarbageCollection(yaffs_Device *dev) +{ + int block; + int aggressive=0; + + //yaffs_DoUnlinkedFileDeletion(dev); + + if(dev->nErasedBlocks <= (dev->nReservedBlocks + 1)) + { + aggressive = 1; + } + + block = yaffs_FindDirtiestBlock(dev,aggressive); + + if(block >= 0) + { + dev->garbageCollections++; + if(!aggressive) + { + dev->passiveGarbageCollections++; + } + + T(YAFFS_TRACE_GC,(TSTR("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),dev->nErasedBlocks,aggressive)); + + return yaffs_GarbageCollectBlock(dev,block); + } + + return aggressive ? YAFFS_FAIL : YAFFS_OK; +} + + +//////////////////////////// TAGS /////////////////////////////////////// + +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr) +{ + yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr; + + yaffs_CalcTagsECC(tagsPtr); + + sparePtr->tagByte0 = tu->asBytes[0]; + sparePtr->tagByte1 = tu->asBytes[1]; + sparePtr->tagByte2 = tu->asBytes[2]; + sparePtr->tagByte3 = tu->asBytes[3]; + sparePtr->tagByte4 = tu->asBytes[4]; + sparePtr->tagByte5 = tu->asBytes[5]; + sparePtr->tagByte6 = tu->asBytes[6]; + sparePtr->tagByte7 = tu->asBytes[7]; +} + +static void yaffs_GetTagsFromSpare(yaffs_Device *dev, yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr) +{ + yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr; + int result; + + tu->asBytes[0]= sparePtr->tagByte0; + tu->asBytes[1]= sparePtr->tagByte1; + tu->asBytes[2]= sparePtr->tagByte2; + tu->asBytes[3]= sparePtr->tagByte3; + tu->asBytes[4]= sparePtr->tagByte4; + tu->asBytes[5]= sparePtr->tagByte5; + tu->asBytes[6]= sparePtr->tagByte6; + tu->asBytes[7]= sparePtr->tagByte7; + + result = yaffs_CheckECCOnTags(tagsPtr); + if(result> 0) + { + dev->tagsEccFixed++; + } + else if(result <0) + { + dev->tagsEccUnfixed++; + } +} + +static void yaffs_SpareInitialise(yaffs_Spare *spare) +{ + memset(spare,0xFF,sizeof(yaffs_Spare)); +} + +static int yaffs_ReadChunkTagsFromNAND(yaffs_Device *dev,int chunkInNAND, yaffs_Tags *tags, int *chunkDeleted) +{ + if(tags) + { + yaffs_Spare spare; + if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare,1) == YAFFS_OK) + { + *chunkDeleted = (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0; + yaffs_GetTagsFromSpare(dev,&spare,tags); + return YAFFS_OK; + } + else + { + return YAFFS_FAIL; + } + } + + return YAFFS_OK; +} + +#if 0 +static int yaffs_WriteChunkWithTagsToNAND(yaffs_Device *dev,int chunkInNAND, const __u8 *buffer, yaffs_Tags *tags) +{ + // NB There must be tags, data is optional + // If there is data, then an ECC is calculated on it. + + yaffs_Spare spare; + + if(!tags) + { + return YAFFS_FAIL; + } + + yaffs_SpareInitialise(&spare); + + if(!dev->useNANDECC && buffer) + { + yaffs_CalcECC(buffer,&spare); + } + + yaffs_LoadTagsIntoSpare(&spare,tags); + + return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare); + +} +#endif + + +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device *dev, const __u8 *buffer, yaffs_Tags *tags, int useReserve) +{ + // NB There must be tags, data is optional + // If there is data, then an ECC is calculated on it. + + yaffs_Spare spare; + + if(!tags) + { + return YAFFS_FAIL; + } + + yaffs_SpareInitialise(&spare); + + if(!dev->useNANDECC && buffer) + { + yaffs_CalcECC(buffer,&spare); + } + + yaffs_LoadTagsIntoSpare(&spare,tags); + + return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve); + +} + +static int yaffs_TagsMatch(const yaffs_Tags *tags, int objectId, int chunkInObject, int chunkDeleted) +{ + return ( tags->chunkId == chunkInObject && + tags->objectId == objectId && + !chunkDeleted) ? 1 : 0; + +} + + + +int yaffs_FindChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags) +{ + //Get the Tnode, then get the level 0 offset chunk offset + yaffs_Tnode *tn; + int theChunk = -1; + yaffs_Tags localTags; + int i; + int found = 0; + int chunkDeleted; + + yaffs_Device *dev = in->myDev; + + + if(!tags) + { + // Passed a NULL, so use our own tags space + tags = &localTags; + } + + tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode); + + if(tn) + { + theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits; + + // Now we need to do the shifting etc and search for it + for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++) + { + yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted); + if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted)) + { + // found it; + found = 1; + } + else + { + theChunk++; + } + } + } + return found ? theChunk : -1; +} + +int yaffs_FindAndDeleteChunkInFile(yaffs_Object *in,int chunkInInode,yaffs_Tags *tags) +{ + //Get the Tnode, then get the level 0 offset chunk offset + yaffs_Tnode *tn; + int theChunk = -1; + yaffs_Tags localTags; + int i; + int found = 0; + yaffs_Device *dev = in->myDev; + int chunkDeleted; + + if(!tags) + { + // Passed a NULL, so use our own tags space + tags = &localTags; + } + + tn = yaffs_FindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode); + + if(tn) + { + + theChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits; + + // Now we need to do the shifting etc and search for it + for(i = 0,found = 0; theChunk && i < dev->chunkGroupSize && !found; i++) + { + yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags,&chunkDeleted); + if(yaffs_TagsMatch(tags,in->objectId,chunkInInode,chunkDeleted)) + { + // found it; + found = 1; + } + else + { + theChunk++; + } + } + + // Delete the entry in the filestructure + if(found) + { + tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = 0; + } + } + else + { + //T(("No level 0 found for %d\n", chunkInInode)); + } + + if(!found) + { + //T(("Could not find %d to delete\n",chunkInInode)); + } + return found ? theChunk : -1; +} + + +#ifdef YAFFS_PARANOID + +static int yaffs_CheckFileSanity(yaffs_Object *in) +{ + int chunk; + int nChunks; + int fSize; + int failed = 0; + int objId; + yaffs_Tnode *tn; + yaffs_Tags localTags; + yaffs_Tags *tags = &localTags; + int theChunk; + int chunkDeleted; + + + if(in->variantType != YAFFS_OBJECT_TYPE_FILE) + { + //T(("Object not a file\n")); + return YAFFS_FAIL; + } + + objId = in->objectId; + fSize = in->variant.fileVariant.fileSize; + nChunks = (fSize + in->myDev->nBytesPerChunk -1)/in->myDev->nBytesPerChunk; + + for(chunk = 1; chunk <= nChunks; chunk++) + { + tn = yaffs_FindLevel0Tnode(in->myDev,&in->variant.fileVariant, chunk); + + if(tn) + { + + theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << in->myDev->chunkGroupBits; + + + yaffs_ReadChunkTagsFromNAND(in->myDev,theChunk,tags,&chunkDeleted); + if(yaffs_TagsMatch(tags,in->objectId,chunk,chunkDeleted)) + { + // found it; + + } + else + { + //T(("File problem file [%d,%d] NAND %d tags[%d,%d]\n", + // objId,chunk,theChunk,tags->chunkId,tags->objectId); + + failed = 1; + + } + + } + else + { + //T(("No level 0 found for %d\n", chunk)); + } + } + + return failed ? YAFFS_FAIL : YAFFS_OK; +} + +#endif + +static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND, int inScan) +{ + yaffs_Tnode *tn; + yaffs_Device *dev = in->myDev; + int existingChunk; + yaffs_Tags existingTags; + yaffs_Tags newTags; + unsigned existingSerial, newSerial; + + int newChunkDeleted; + + + tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode); + if(!tn) + { + return YAFFS_FAIL; + } + + existingChunk = tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK]; + + if(inScan) + { + // If we're scanning then we need to test for duplicates + // NB This does not need to be efficient since it should only ever + // happen when the power fails during a write, then only one + // chunk should ever be affected. + + + if(existingChunk != 0) + { + // NB Right now existing chunk will not be real chunkId if the device >= 32MB + // thus we have to do a FindChunkInFile to get the real chunk id. + // + // We have a duplicate now we need to decide which one to use + // To do this we get both sets of tags and compare serial numbers. + yaffs_ReadChunkTagsFromNAND(dev,chunkInNAND, &newTags,&newChunkDeleted); + + + // Do a proper find + existingChunk = yaffs_FindChunkInFile(in,chunkInInode, &existingTags); + + if(existingChunk <=0) + { + //Hoosterman - how did this happen? + + T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: existing chunk < 0 in scan" TENDSTR))); + + } + + + // NB The deleted flags should be false, otherwise the chunks will + // not be loaded during a scan + + newSerial = newTags.serialNumber; + existingSerial = existingTags.serialNumber; + + if( existingChunk <= 0 || + ((existingSerial+1) & 3) == newSerial) + { + // Use new + // Delete the old one and drop through to update the tnode + yaffs_DeleteChunk(dev,existingChunk,1); + } + else + { + // Use existing. + // Delete the new one and return early so that the tnode isn't changed + yaffs_DeleteChunk(dev,chunkInNAND,1); + return YAFFS_OK; + } + } + + } + + if(existingChunk == 0) + { + in->nDataChunks++; + } + + tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = (chunkInNAND >> dev->chunkGroupBits); + + return YAFFS_OK; +} + + + +int yaffs_ReadChunkDataFromObject(yaffs_Object *in,int chunkInInode, __u8 *buffer) +{ + int chunkInNAND = yaffs_FindChunkInFile(in,chunkInInode,NULL); + + if(chunkInNAND >= 0) + { + return yaffs_ReadChunkFromNAND(in->myDev,chunkInNAND,buffer,NULL,1); + } + else + { + memset(buffer,0,YAFFS_BYTES_PER_CHUNK); + return 0; + } + +} + + +static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId,int markNAND) +{ + int block; + int page; + yaffs_Spare spare; + yaffs_BlockInfo *bi; + + if(chunkId <= 0) return; + + dev->nDeletions++; + block = chunkId / dev->nChunksPerBlock; + page = chunkId % dev->nChunksPerBlock; + + if(markNAND) + { + yaffs_SpareInitialise(&spare); + +#ifdef CONFIG_MTD_NAND_VERIFY_WRITE + + //read data before write, to ensure correct ecc + //if we're using MTD verification under Linux + yaffs_ReadChunkFromNAND(dev,chunkId,NULL,&spare,0); +#endif + + spare.pageStatus = 0; // To mark it as deleted. + + + yaffs_WriteChunkToNAND(dev,chunkId,NULL,&spare); + yaffs_HandleUpdateChunk(dev,chunkId,&spare); + } + else + { + dev->nUnmarkedDeletions++; + } + + bi = yaffs_GetBlockInfo(dev,block); + + + // Pull out of the management area. + // If the whole block became dirty, this will kick off an erasure. + if( bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || + bi->blockState == YAFFS_BLOCK_STATE_FULL) + { + dev->nFreeChunks++; + + yaffs_ClearChunkBit(dev,block,page); + bi->pagesInUse--; + + if(bi->pagesInUse == 0 && + bi->blockState == YAFFS_BLOCK_STATE_FULL) + { + yaffs_BlockBecameDirty(dev,block); + } + + } + else + { + // T(("Bad news deleting chunk %d\n",chunkId)); + } + +} + + + + +int yaffs_WriteChunkDataToObject(yaffs_Object *in,int chunkInInode, const __u8 *buffer,int nBytes,int useReserve) +{ + // Find old chunk Need to do this to get serial number + // Write new one and patch into tree. + // Invalidate old tags. + + int prevChunkId; + yaffs_Tags prevTags; + + int newChunkId; + yaffs_Tags newTags; + + yaffs_Device *dev = in->myDev; + + yaffs_CheckGarbageCollection(dev); + + // Get the previous chunk at this location in the file if it exists + prevChunkId = yaffs_FindChunkInFile(in,chunkInInode,&prevTags); + + // Set up new tags + newTags.chunkId = chunkInInode; + newTags.objectId = in->objectId; + newTags.serialNumber = (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1; + newTags.byteCount = nBytes; + newTags.unusedStuff = 0xFFFFFFFF; + + yaffs_CalcTagsECC(&newTags); + + newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve); + if(newChunkId >= 0) + { + yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId,0); + + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId,1); + + } + + yaffs_CheckFileSanity(in); + } + return newChunkId; + + + + + +} + + +// UpdateObjectHeader updates the header on NAND for an object. +// If name is not NULL, then that new name is used. +// +int yaffs_UpdateObjectHeader(yaffs_Object *in,const char *name, int force) +{ + + yaffs_Device *dev = in->myDev; + + int prevChunkId; + + int newChunkId; + yaffs_Tags newTags; + __u8 bufferNew[YAFFS_BYTES_PER_CHUNK]; + __u8 bufferOld[YAFFS_BYTES_PER_CHUNK]; + + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)bufferNew; + yaffs_ObjectHeader *ohOld = (yaffs_ObjectHeader *)bufferOld; + + + if(!in->fake || force) + { + + yaffs_CheckGarbageCollection(dev); + + memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK); + + prevChunkId = in->chunkId; + + if(prevChunkId >= 0) + { + yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL,1); + } + + // Header data + oh->type = in->variantType; + + oh->st_mode = in->st_mode; + +#ifdef CONFIG_YAFFS_WINCE + oh->win_atime[0] = in->win_atime[0]; + oh->win_ctime[0] = in->win_ctime[0]; + oh->win_mtime[0] = in->win_mtime[0]; + oh->win_atime[1] = in->win_atime[1]; + oh->win_ctime[1] = in->win_ctime[1]; + oh->win_mtime[1] = in->win_mtime[1]; +#else + oh->st_uid = in->st_uid; + oh->st_gid = in->st_gid; + oh->st_atime = in->st_atime; + oh->st_mtime = in->st_mtime; + oh->st_ctime = in->st_ctime; + oh->st_rdev = in->st_rdev; +#endif + if(in->parent) + { + oh->parentObjectId = in->parent->objectId; + } + else + { + oh->parentObjectId = 0; + } + + //oh->sum = in->sum; + if(name && *name) + { + memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1); + strncpy(oh->name,name,YAFFS_MAX_NAME_LENGTH); + } + else if(prevChunkId) + { + memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1); + } + else + { + memset(oh->name,0,YAFFS_MAX_NAME_LENGTH + 1); + } + + switch(in->variantType) + { + case YAFFS_OBJECT_TYPE_UNKNOWN: + // Should not happen + break; + case YAFFS_OBJECT_TYPE_FILE: + oh->fileSize = in->variant.fileVariant.fileSize; + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + oh->equivalentObjectId = in->variant.hardLinkVariant.equivalentObjectId; + break; + case YAFFS_OBJECT_TYPE_SPECIAL: + // Do nothing + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + // Do nothing + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + strncpy(oh->alias,in->variant.symLinkVariant.alias,YAFFS_MAX_ALIAS_LENGTH); + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0; + break; + } + + // Tags + in->serial++; + newTags.chunkId = 0; + newTags.objectId = in->objectId; + newTags.serialNumber = in->serial; + newTags.byteCount = 0xFFFFFFFF; + newTags.unusedStuff = 0xFFFFFFFF; + + yaffs_CalcTagsECC(&newTags); + + + // Create new chunk in NAND + newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags, (prevChunkId >= 0) ? 1 : 0 ); + + if(newChunkId >= 0) + { + + in->chunkId = newChunkId; + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId,1); + } + + in->dirty = 0; + } + + return newChunkId; + + } + return 0; +} + + +/////////////////////// Short Operations Cache //////////////////////////////// +// In many siturations where there is no high level buffering (eg WinCE) a lot of +// reads might be short sequential reads, and a lot of writes may be short +// sequential writes. eg. scanning/writing a jpeg file. +// In these cases, a short read/write cache can provide a huge perfomance benefit +// with dumb-as-a-rock code. +// There are a limited number (~10) of cache chunks per device so that we don't +// need a very intelligent search. + + + + + +static void yaffs_FlushFilesChunkCache(yaffs_Object *obj) +{ + yaffs_Device *dev = obj->myDev; + int lowest; + int i; + yaffs_ChunkCache *cache; + int chunkWritten; + int nBytes; + int nCaches = obj->myDev->nShortOpCaches; + + if (nCaches > 0) + { + do{ + cache = NULL; + + // Find the dirty cache for this object with the lowest chunk id. + for(i = 0; i < nCaches; i++) + { + if(dev->srCache[i].object == obj && + dev->srCache[i].dirty) + { + if(!cache || dev->srCache[i].chunkId < lowest) + { + cache = &dev->srCache[i]; + lowest = cache->chunkId; + } + } + } + + if(cache) + { + //Write it out + +#if 0 + nBytes = cache->object->variant.fileVariant.fileSize - ((cache->chunkId -1) * YAFFS_BYTES_PER_CHUNK); + + if(nBytes > YAFFS_BYTES_PER_CHUNK) + { + nBytes= YAFFS_BYTES_PER_CHUNK; + } +#endif + chunkWritten = yaffs_WriteChunkDataToObject(cache->object, + cache->chunkId, + cache->data, + cache->nBytes,1); + + cache->dirty = 0; + cache->object = NULL; + } + + } while(cache && chunkWritten > 0); + + if(cache) + { + //Hoosterman, disk full while writing cache out. + T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: no space during cache write" TENDSTR))); + + } + } + +} + + +// Grab us a chunk for use. +// First look for an empty one. +// Then look for the least recently used non-dirty one. +// Then look for the least recently used dirty one...., flush and look again. +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device *dev) +{ + int i; + int usage; + int theOne; + + if(dev->nShortOpCaches > 0) + { + for(i = 0; i < dev->nShortOpCaches; i++) + { + if(!dev->srCache[i].object) + { + //T(("Grabbing empty %d\n",i)); + + //printf("Grabbing empty %d\n",i); + + return &dev->srCache[i]; + } + } + + return NULL; + + theOne = -1; + usage = 0; // just to stop the compiler grizzling + + for(i = 0; i < dev->nShortOpCaches; i++) + { + if(!dev->srCache[i].dirty && + ((dev->srCache[i].lastUse < usage && theOne >= 0)|| + theOne < 0)) + { + usage = dev->srCache[i].lastUse; + theOne = i; + } + } + + //T(("Grabbing non-empty %d\n",theOne)); + + //if(theOne >= 0) printf("Grabbed non-empty cache %d\n",theOne); + + return theOne >= 0 ? &dev->srCache[theOne] : NULL; + } + else + { + return NULL; + } + +} + + +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device *dev) +{ + yaffs_ChunkCache *cache; + yaffs_Object *theObj; + int usage; + int i; + int pushout; + + if(dev->nShortOpCaches > 0) + { + // Try find a non-dirty one... + + cache = yaffs_GrabChunkCacheWorker(dev); + + if(!cache) + { + // They were all dirty, find the last recently used object and flush + // its cache, then find again. + // NB what's here is not very accurate, we actually flush the object + // the last recently used page. + + theObj = dev->srCache[0].object; + usage = dev->srCache[0].lastUse; + cache = &dev->srCache[0]; + pushout = 0; + + for(i = 1; i < dev->nShortOpCaches; i++) + { + if( dev->srCache[i].object && + dev->srCache[i].lastUse < usage) + { + usage = dev->srCache[i].lastUse; + theObj = dev->srCache[i].object; + cache = &dev->srCache[i]; + pushout = i; + } + } + + if(!cache || cache->dirty) + { + + //printf("Dirty "); + yaffs_FlushFilesChunkCache(theObj); + + // Try again + cache = yaffs_GrabChunkCacheWorker(dev); + } + else + { + //printf(" pushout %d\n",pushout); + } + + } + + return cache; + } + else + return NULL; + +} + + +// Find a cached chunk +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object *obj, int chunkId) +{ + yaffs_Device *dev = obj->myDev; + int i; + if(dev->nShortOpCaches > 0) + { + for(i = 0; i < dev->nShortOpCaches; i++) + { + if(dev->srCache[i].object == obj && + dev->srCache[i].chunkId == chunkId) + { + dev->cacheHits++; + + return &dev->srCache[i]; + } + } + } + return NULL; +} + +// Mark the chunk for the least recently used algorithym +static void yaffs_UseChunkCache(yaffs_Device *dev, yaffs_ChunkCache *cache, int isAWrite) +{ + + if(dev->nShortOpCaches > 0) + { + if( dev->srLastUse < 0 || + dev->srLastUse > 100000000) + { + // Reset the cache usages + int i; + for(i = 1; i < dev->nShortOpCaches; i++) + { + dev->srCache[i].lastUse = 0; + } + dev->srLastUse = 0; + } + + dev->srLastUse++; + + cache->lastUse = dev->srLastUse; + + if(isAWrite) + { + cache->dirty = 1; + } + } +} + +// Invalidate a single cache page. +// Do this when a whole page gets written, +// ie the short cache for this page is no longer valid. +static void yaffs_InvalidateChunkCache(yaffs_Object *object, int chunkId) +{ + if(object->myDev->nShortOpCaches > 0) + { + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object,chunkId); + + if(cache) + { + cache->object = NULL; + } + } +} + + +// Invalidate all the cache pages associated with this object +// Do this whenever ther file is deleted or resized. +static void yaffs_InvalidateWholeChunkCache(yaffs_Object *in) +{ + int i; + yaffs_Device *dev = in->myDev; + + if(dev->nShortOpCaches > 0) + { + // Now invalidate it. + for(i = 0; i < dev->nShortOpCaches; i++) + { + if(dev->srCache[i].object == in) + { + dev->srCache[i].object = NULL; + } + } + } +} + + + + + +///////////////////////// File read/write /////////////////////////////// +// Read and write have very similar structures. +// In general the read/write has three parts to it +// * An incomplete chunk to start with (if the read/write is not chunk-aligned) +// * Some complete chunks +// * An incomplete chunk to end off with +// +// Curve-balls: the first chunk might also be the last chunk. + +int yaffs_ReadDataFromFile(yaffs_Object *in, __u8 * buffer, __u32 offset, int nBytes) +{ + + + int chunk; + int start; + int nToCopy; + int n = nBytes; + int nDone = 0; + yaffs_ChunkCache *cache; + + yaffs_Device *dev; + + dev = in->myDev; + + while(n > 0) + { + chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; // The first chunk is 1 + start = offset % YAFFS_BYTES_PER_CHUNK; + + // OK now check for the curveball where the start and end are in + // the same chunk. + if( (start + n) < YAFFS_BYTES_PER_CHUNK) + { + nToCopy = n; + } + else + { + nToCopy = YAFFS_BYTES_PER_CHUNK - start; + } + + cache = yaffs_FindChunkCache(in,chunk); + + // If the chunk is already in the cache or it is less than a whole chunk + // then use the cache (if there is caching) + // else bypass the cache. + if( cache || nToCopy != YAFFS_BYTES_PER_CHUNK) + { + if(dev->nShortOpCaches > 0) + { + + // If we can't find the data in the cache, then load it up. + + if(!cache) + { + cache = yaffs_GrabChunkCache(in->myDev); + cache->object = in; + cache->chunkId = chunk; + cache->dirty = 0; + yaffs_ReadChunkDataFromObject(in,chunk,cache->data); + cache->nBytes = 0; + } + + yaffs_UseChunkCache(dev,cache,0); + + memcpy(buffer,&cache->data[start],nToCopy); + } + else + { + // Read into the local buffer then copy... + yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer); + memcpy(buffer,&dev->localBuffer[start],nToCopy); + } + + } + else + { +#ifdef CONFIG_YAFFS_WINCE + + // Under WinCE can't do direct transfer. Need to use a local buffer. + // This is because we otherwise screw up WinCE's memory mapper + yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer); + memcpy(buffer,dev->localBuffer,YAFFS_BYTES_PER_CHUNK); +#else + // A full chunk. Read directly into the supplied buffer. + yaffs_ReadChunkDataFromObject(in,chunk,buffer); +#endif + } + + n -= nToCopy; + offset += nToCopy; + buffer += nToCopy; + nDone += nToCopy; + + } + + return nDone; +} + + + +int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes) +{ + + int chunk; + int start; + int nToCopy; + int n = nBytes; + int nDone = 0; + int nToWriteBack; + int startOfWrite = offset; + int chunkWritten = 0; + int nBytesRead; + + yaffs_Device *dev; + + dev = in->myDev; + + + while(n > 0 && chunkWritten >= 0) + { + chunk = offset / YAFFS_BYTES_PER_CHUNK + 1; + start = offset % YAFFS_BYTES_PER_CHUNK; + + + // OK now check for the curveball where the start and end are in + // the same chunk. + + if( (start + n) < YAFFS_BYTES_PER_CHUNK) + { + nToCopy = n; + + // Now folks, to calculate how many bytes to write back.... + // If we're overwriting and not writing to then end of file then + // we need to write back as much as was there before. + + nBytesRead = in->variant.fileVariant.fileSize - ((chunk -1) * YAFFS_BYTES_PER_CHUNK); + + if(nBytesRead > YAFFS_BYTES_PER_CHUNK) + { + nBytesRead = YAFFS_BYTES_PER_CHUNK; + } + + nToWriteBack = (nBytesRead > (start + n)) ? nBytesRead : (start +n); + + } + else + { + nToCopy = YAFFS_BYTES_PER_CHUNK - start; + nToWriteBack = YAFFS_BYTES_PER_CHUNK; + } + + if(nToCopy != YAFFS_BYTES_PER_CHUNK) + { + // An incomplete start or end chunk (or maybe both start and end chunk) + if(dev->nShortOpCaches > 0) + { + yaffs_ChunkCache *cache; + // If we can't find the data in the cache, then load it up. + cache = yaffs_FindChunkCache(in,chunk); + if(!cache && yaffs_CheckSpaceForChunkCache(in->myDev)) + { + cache = yaffs_GrabChunkCache(in->myDev); + cache->object = in; + cache->chunkId = chunk; + cache->dirty = 0; + yaffs_ReadChunkDataFromObject(in,chunk,cache->data); + } + + if(cache) + { + yaffs_UseChunkCache(dev,cache,1); + memcpy(&cache->data[start],buffer,nToCopy); + cache->nBytes = nToWriteBack; + } + else + { + chunkWritten = -1; // fail the write + } + } + else + { + // An incomplete start or end chunk (or maybe both start and end chunk) + // Read into the local buffer then copy, then copy over and write back. + + yaffs_ReadChunkDataFromObject(in,chunk,dev->localBuffer); + + memcpy(&dev->localBuffer[start],buffer,nToCopy); + + chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,nToWriteBack,0); + + //T(("Write with readback to chunk %d %d start %d copied %d wrote back %d\n",chunk,chunkWritten,start, nToCopy, nToWriteBack)); + } + + } + else + { + +#ifdef CONFIG_YAFFS_WINCE + // Under WinCE can't do direct transfer. Need to use a local buffer. + // This is because we otherwise screw up WinCE's memory mapper + memcpy(dev->localBuffer,buffer,YAFFS_BYTES_PER_CHUNK); + chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,dev->localBuffer,YAFFS_BYTES_PER_CHUNK,0); +#else + // A full chunk. Write directly from the supplied buffer. + chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0); +#endif + // Since we've overwritten the cached data, we better invalidate it. + yaffs_InvalidateChunkCache(in,chunk); + //T(("Write to chunk %d %d\n",chunk,chunkWritten)); + } + + if(chunkWritten >= 0) + { + n -= nToCopy; + offset += nToCopy; + buffer += nToCopy; + nDone += nToCopy; + } + + } + + // Update file object + + if((startOfWrite + nDone) > in->variant.fileVariant.fileSize) + { + in->variant.fileVariant.fileSize = (startOfWrite + nDone); + } + + in->dirty = 1; + + return nDone; +} + + +int yaffs_ResizeFile(yaffs_Object *in, int newSize) +{ + int i; + int chunkId; + int oldFileSize = in->variant.fileVariant.fileSize; + int sizeOfPartialChunk = newSize % YAFFS_BYTES_PER_CHUNK; + + yaffs_Device *dev = in->myDev; + + + yaffs_FlushFilesChunkCache(in); + yaffs_InvalidateWholeChunkCache(in); + + if(in->variantType != YAFFS_OBJECT_TYPE_FILE) + { + return yaffs_GetFileSize(in); + } + + if(newSize < oldFileSize) + { + + int lastDel = 1 + (oldFileSize-1)/YAFFS_BYTES_PER_CHUNK; + + int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/ + YAFFS_BYTES_PER_CHUNK; + + // Delete backwards so that we don't end up with holes if + // power is lost part-way through the operation. + for(i = lastDel; i >= startDel; i--) + { + // NB this could be optimised somewhat, + // eg. could retrieve the tags and write them without + // using yaffs_DeleteChunk + + chunkId = yaffs_FindAndDeleteChunkInFile(in,i,NULL); + if(chunkId < (dev->startBlock * 32) || chunkId >= ((dev->endBlock+1) * 32)) + { + //T(("Found daft chunkId %d for %d\n",chunkId,i)); + } + else + { + in->nDataChunks--; + yaffs_DeleteChunk(dev,chunkId,1); + } + } + + + if(sizeOfPartialChunk != 0) + { + int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK; + + // Got to read and rewrite the last chunk with its new size. + // NB Got to zero pad to nuke old data + yaffs_ReadChunkDataFromObject(in,lastChunk,dev->localBuffer); + memset(dev->localBuffer + sizeOfPartialChunk,0, YAFFS_BYTES_PER_CHUNK - sizeOfPartialChunk); + + yaffs_WriteChunkDataToObject(in,lastChunk,dev->localBuffer,sizeOfPartialChunk,1); + + } + + in->variant.fileVariant.fileSize = newSize; + + yaffs_PruneFileStructure(dev,&in->variant.fileVariant); + + return newSize; + + } + else + { + return oldFileSize; + } +} + + +loff_t yaffs_GetFileSize(yaffs_Object *obj) +{ + obj = yaffs_GetEquivalentObject(obj); + + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: + return obj->variant.fileVariant.fileSize; + case YAFFS_OBJECT_TYPE_SYMLINK: + return strlen(obj->variant.symLinkVariant.alias); + default: + return 0; + } +} + + + +// yaffs_FlushFile() updates the file's +// objectId in NAND + +int yaffs_FlushFile(yaffs_Object *in, int updateTime) +{ + int retVal; + if(in->dirty) + { + //T(("flushing object header\n")); + + yaffs_FlushFilesChunkCache(in); + if(updateTime) + { +#ifdef CONFIG_YAFFS_WINCE + yfsd_WinFileTimeNow(in->win_mtime); +#else + in->st_mtime = Y_CURRENT_TIME; +#endif + } + + retVal = (yaffs_UpdateObjectHeader(in,NULL,0) >= 0)? YAFFS_OK : YAFFS_FAIL; + } + else + { + retVal = YAFFS_OK; + } + + return retVal; + +} + + +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in) +{ + + // First off, invalidate the file's data in the cache, without flushing. + yaffs_InvalidateWholeChunkCache(in); + + yaffs_RemoveObjectFromDirectory(in); + yaffs_DeleteChunk(in->myDev,in->chunkId,1); +#if 0 +#ifdef __KERNEL__ + if(in->myInode) + { + in->myInode->u.generic_ip = NULL; + in->myInode = NULL; + } +#endif +#endif + yaffs_FreeObject(in); + return YAFFS_OK; + +} + +// yaffs_DeleteFile deletes the whole file data +// and the inode associated with the file. +// It does not delete the links associated with the file. +static int yaffs_UnlinkFile(yaffs_Object *in) +{ + +#ifdef CONFIG_YAFFS_DISABLE_BACKGROUND_DELETION + + // Delete the file data & tnodes + + yaffs_DeleteWorker(in, in->variant.fileVariant.top, in->variant.fileVariant.topLevel, 0,NULL); + + + yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top); + + return yaffs_DoGenericObjectDeletion(in); +#else + int retVal; + int immediateDeletion=0; + retVal = yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,NULL,0); + if(1 || // Ignore the result of the change name. This will only fail + // if the disk was completely full (an error condition) + // We ignore the error so we can delete files to recover the disk + retVal == YAFFS_OK) + { + //in->unlinked = 1; + //in->myDev->nUnlinkedFiles++; + //in->renameAllowed = 0; +#ifdef __KERNEL__ + if(!in->myInode) + { + // Might be open at present, + // Caught by delete_inode in yaffs_fs.c + immediateDeletion = 1; + + } +#else + if(in->inUse <= 0) + { + immediateDeletion = 1; + + } +#endif + + if(immediateDeletion) + { + T(YAFFS_TRACE_TRACING,(TSTR("yaffs: immediate deletion of file %d" TENDSTR),in->objectId)); + in->deleted=1; + in->myDev->nDeletedFiles++; + yaffs_SoftDeleteFile(in); + } + + } + return retVal; + + +#endif +} + +int yaffs_DeleteFile(yaffs_Object *in) +{ + int retVal = YAFFS_OK; + + if(in->nDataChunks > 0) + { + // Use soft deletion + if(!in->unlinked) + { + retVal = yaffs_UnlinkFile(in); + } + if(retVal == YAFFS_OK && + in->unlinked && + !in->deleted) + { + in->deleted = 1; + in->myDev->nDeletedFiles++; + yaffs_SoftDeleteFile(in); + } + return in->deleted ? YAFFS_OK : YAFFS_FAIL; + } + else + { + // The file has no data chunks so we toss it immediately + yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top); + in->variant.fileVariant.top = NULL; + yaffs_DoGenericObjectDeletion(in); + + return YAFFS_OK; + } +} + +static int yaffs_DeleteDirectory(yaffs_Object *in) +{ + //First check that the directory is empty. + if(list_empty(&in->variant.directoryVariant.children)) + { + return yaffs_DoGenericObjectDeletion(in); + } + + return YAFFS_FAIL; + +} + +static int yaffs_DeleteSymLink(yaffs_Object *in) +{ + YFREE(in->variant.symLinkVariant.alias); + + return yaffs_DoGenericObjectDeletion(in); +} + +static int yaffs_DeleteHardLink(yaffs_Object *in) +{ + // remove this hardlink from the list assocaited with the equivalent + // object + list_del(&in->hardLinks); + return yaffs_DoGenericObjectDeletion(in); +} + + +static void yaffs_AbortHalfCreatedObject(yaffs_Object *obj) +{ + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: yaffs_DeleteFile(obj); break; + case YAFFS_OBJECT_TYPE_DIRECTORY: yaffs_DeleteDirectory(obj); break; + case YAFFS_OBJECT_TYPE_SYMLINK: yaffs_DeleteSymLink(obj); break; + case YAFFS_OBJECT_TYPE_HARDLINK: yaffs_DeleteHardLink(obj); break; + case YAFFS_OBJECT_TYPE_SPECIAL: yaffs_DoGenericObjectDeletion(obj); break; + case YAFFS_OBJECT_TYPE_UNKNOWN: break; // should not happen. + } +} + + +static int yaffs_UnlinkWorker(yaffs_Object *obj) +{ + + + if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) + { + return yaffs_DeleteHardLink(obj); + } + else if(!list_empty(&obj->hardLinks)) + { + // Curve ball: We're unlinking an object that has a hardlink. + // + // This problem arises because we are not strictly following + // The Linux link/inode model. + // + // We can't really delete the object. + // Instead, we do the following: + // - Select a hardlink. + // - Unhook it from the hard links + // - Unhook it from its parent directory (so that the rename can work) + // - Rename the object to the hardlink's name. + // - Delete the hardlink + + + yaffs_Object *hl; + int retVal; + char name[YAFFS_MAX_NAME_LENGTH+1]; + + hl = list_entry(obj->hardLinks.next,yaffs_Object,hardLinks); + + list_del_init(&hl->hardLinks); + list_del_init(&hl->siblings); + + yaffs_GetObjectName(hl,name,YAFFS_MAX_NAME_LENGTH+1); + + retVal = yaffs_ChangeObjectName(obj, hl->parent, name,0); + + if(retVal == YAFFS_OK) + { + retVal = yaffs_DoGenericObjectDeletion(hl); + } + return retVal; + + } + else + { + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: + return yaffs_UnlinkFile(obj); + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + return yaffs_DeleteDirectory(obj); + break; + case YAFFS_OBJECT_TYPE_SYMLINK: + return yaffs_DeleteSymLink(obj); + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + case YAFFS_OBJECT_TYPE_UNKNOWN: + default: + return YAFFS_FAIL; + } + } +} + +int yaffs_Unlink(yaffs_Object *dir, const char *name) +{ + yaffs_Object *obj; + + obj = yaffs_FindObjectByName(dir,name); + + if(obj && obj->unlinkAllowed) + { + return yaffs_UnlinkWorker(obj); + } + + return YAFFS_FAIL; + +} + +//////////////// Initialisation Scanning ///////////////// + + + +// For now we use the SmartMedia check. +// We look at the blockStatus byte in the first two chunks +// These must be 0xFF to pass as OK. +// todo: this function needs to be modifyable foir different NAND types +// and different chunk sizes. Suggest make this into a per-device configurable +// function. +static int yaffs_IsBlockBad(yaffs_Device *dev, int blk) +{ + yaffs_Spare spare; + + yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock,NULL,&spare,1); +#if 1 + if(yaffs_CountBits(spare.blockStatus) < 7) + { + return 1; + } +#else + if(spare.blockStatus != 0xFF) + { + return 1; + } +#endif + yaffs_ReadChunkFromNAND(dev,blk * dev->nChunksPerBlock + 1,NULL,&spare,1); + +#if 1 + if(yaffs_CountBits(spare.blockStatus) < 7) + { + return 1; + } +#else + if(spare.blockStatus != 0xFF) + { + return 1; + } +#endif + + return 0; + +} + +static int yaffs_Scan(yaffs_Device *dev) +{ + yaffs_Spare spare; + yaffs_Tags tags; + int blk; + int chunk; + int c; + int deleted; + yaffs_BlockState state; + yaffs_Object *hardList = NULL; + yaffs_Object *hl; + yaffs_BlockInfo *bi; + + + yaffs_ObjectHeader *oh; + yaffs_Object *in; + yaffs_Object *parent; + + __u8 chunkData[YAFFS_BYTES_PER_CHUNK]; + + + // Scan all the blocks... + + for(blk = dev->startBlock; blk <= dev->endBlock; blk++) + { + deleted = 0; + bi = yaffs_GetBlockInfo(dev,blk); + yaffs_ClearChunkBits(dev,blk); + bi->pagesInUse = 0; + bi->softDeletions = 0; + state = YAFFS_BLOCK_STATE_SCANNING; + + + if(yaffs_IsBlockBad(dev,blk)) + { + state = YAFFS_BLOCK_STATE_DEAD; + T(YAFFS_TRACE_BAD_BLOCKS,(TSTR("block %d is bad" TENDSTR),blk)); + } + + // Read each chunk in the block. + + for(c = 0; c < dev->nChunksPerBlock && + state == YAFFS_BLOCK_STATE_SCANNING; c++) + { + // Read the spare area and decide what to do + chunk = blk * dev->nChunksPerBlock + c; + + yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare,1); + + + // This block looks ok, now what's in this chunk? + yaffs_GetTagsFromSpare(dev,&spare,&tags); + + if(yaffs_CountBits(spare.pageStatus) < 6) + { + // A deleted chunk + deleted++; + dev->nFreeChunks ++; + //T((" %d %d deleted\n",blk,c)); + } + else if(tags.objectId == YAFFS_UNUSED_OBJECT_ID) + { + // An unassigned chunk in the block + // This means that either the block is empty or + // this is the one being allocated from + + if(c == 0) + { + // the block is unused + state = YAFFS_BLOCK_STATE_EMPTY; + dev->nErasedBlocks++; + } + else + { + // this is the block being allocated from + T(YAFFS_TRACE_SCAN,(TSTR(" Allocating from %d %d" TENDSTR),blk,c)); + state = YAFFS_BLOCK_STATE_ALLOCATING; + dev->allocationBlock = blk; + dev->allocationPage = c; + } + + dev->nFreeChunks += (dev->nChunksPerBlock - c); + } + else if(tags.chunkId > 0) + { + int endpos; + // A data chunk. + yaffs_SetChunkBit(dev,blk,c); + bi->pagesInUse++; + + in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,YAFFS_OBJECT_TYPE_FILE); + // PutChunkIntoFIle checks for a clash (two data chunks with + // the same chunkId). + yaffs_PutChunkIntoFile(in,tags.chunkId,chunk,1); + endpos = (tags.chunkId - 1)* YAFFS_BYTES_PER_CHUNK + tags.byteCount; + if(in->variant.fileVariant.scannedFileSize variant.fileVariant.scannedFileSize = endpos; +#ifndef CONFIG_YAFFS_USE_HEADER_FILE_SIZE + in->variant.fileVariant.fileSize = + in->variant.fileVariant.scannedFileSize; +#endif + + } + //T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); + } + else + { + // chunkId == 0, so it is an ObjectHeader. + // Thus, we read in the object header and make the object + yaffs_SetChunkBit(dev,blk,c); + bi->pagesInUse++; + + yaffs_ReadChunkFromNAND(dev,chunk,chunkData,NULL,1); + + oh = (yaffs_ObjectHeader *)chunkData; + + in = yaffs_FindOrCreateObjectByNumber(dev,tags.objectId,oh->type); + + if(in->valid) + { + // We have already filled this one. We have a duplicate and need to resolve it. + + unsigned existingSerial = in->serial; + unsigned newSerial = tags.serialNumber; + + if(((existingSerial+1) & 3) == newSerial) + { + // Use new one - destroy the exisiting one + yaffs_DeleteChunk(dev,in->chunkId,1); + in->valid = 0; + } + else + { + // Use existing - destroy this one. + yaffs_DeleteChunk(dev,chunk,1); + } + } + + if(!in->valid && + (tags.objectId == YAFFS_OBJECTID_ROOT || + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) + { + // We only load some info, don't fiddle with directory structure + in->valid = 1; + in->variantType = oh->type; + + in->st_mode = oh->st_mode; +#ifdef CONFIG_YAFFS_WINCE + in->win_atime[0] = oh->win_atime[0]; + in->win_ctime[0] = oh->win_ctime[0]; + in->win_mtime[0] = oh->win_mtime[0]; + in->win_atime[1] = oh->win_atime[1]; + in->win_ctime[1] = oh->win_ctime[1]; + in->win_mtime[1] = oh->win_mtime[1]; +#else + in->st_uid = oh->st_uid; + in->st_gid = oh->st_gid; + in->st_atime = oh->st_atime; + in->st_mtime = oh->st_mtime; + in->st_ctime = oh->st_ctime; + in->st_rdev = oh->st_rdev; +#endif + in->chunkId = chunk; + + } + else if(!in->valid) + { + // we need to load this info + + in->valid = 1; + in->variantType = oh->type; + + in->st_mode = oh->st_mode; +#ifdef CONFIG_YAFFS_WINCE + in->win_atime[0] = oh->win_atime[0]; + in->win_ctime[0] = oh->win_ctime[0]; + in->win_mtime[0] = oh->win_mtime[0]; + in->win_atime[1] = oh->win_atime[1]; + in->win_ctime[1] = oh->win_ctime[1]; + in->win_mtime[1] = oh->win_mtime[1]; +#else + in->st_uid = oh->st_uid; + in->st_gid = oh->st_gid; + in->st_atime = oh->st_atime; + in->st_mtime = oh->st_mtime; + in->st_ctime = oh->st_ctime; + in->st_rdev = oh->st_rdev; +#endif + in->chunkId = chunk; + + yaffs_SetObjectName(in,oh->name); + in->dirty = 0; + + // directory stuff... + // hook up to parent + + parent = yaffs_FindOrCreateObjectByNumber(dev,oh->parentObjectId,YAFFS_OBJECT_TYPE_DIRECTORY); + if(parent->variantType == YAFFS_OBJECT_TYPE_UNKNOWN) + { + // Set up as a directory + parent->variantType = YAFFS_OBJECT_TYPE_DIRECTORY; + INIT_LIST_HEAD(&parent->variant.directoryVariant.children); + } + else if(parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) + { + // Hoosterman, another problem.... + // We're trying to use a non-directory as a directory + + T(YAFFS_TRACE_ERROR, (TSTR("yaffs tragedy: attempting to use non-directory as a directory in scan. Put in lost+found." TENDSTR))); + parent = dev->lostNFoundDir; + + } + + yaffs_AddObjectToDirectory(parent,in); + if(parent == dev->unlinkedDir) + { + in->deleted = 1; // If it is unlinked at start up then it wants deleting + dev->nDeletedFiles++; + } + + // Note re hardlinks. + // Since we might scan a hardlink before its equivalent object is scanned + // we put them all in a list. + // After scanning is complete, we should have all the objects, so we run through this + // list and fix up all the chains. + + switch(in->variantType) + { + case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem + break; + case YAFFS_OBJECT_TYPE_FILE: +#ifdef CONFIG_YAFFS_USE_HEADER_FILE_SIZE + in->variant.fileVariant.fileSize = oh->fileSize; +#endif + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId; + in->hardLinks.next = (struct list_head *)hardList; + hardList = in; + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: // Do nothing + break; + case YAFFS_OBJECT_TYPE_SPECIAL: // Do nothing + break; + case YAFFS_OBJECT_TYPE_SYMLINK: // Do nothing + in->variant.symLinkVariant.alias = yaffs_CloneString(oh->alias); + break; + } + //T((" %d %d header %d \"%s\" type %d\n",blk,c,tags.objectId,oh->name,in->variantType)); + } + } + } + + if(state == YAFFS_BLOCK_STATE_SCANNING) + { + // If we got this far while scanning, then the block is fully allocated. + state = YAFFS_BLOCK_STATE_FULL; + } + + bi->blockState = state; + + // Now let's see if it was dirty + if( bi->pagesInUse == 0 && + bi->blockState == YAFFS_BLOCK_STATE_FULL) + { + yaffs_BlockBecameDirty(dev,blk); + } + + } + + // Fix up the hard link chains. + // We should now have scanned all the objects, now it's time to add these + // hardlinks. + while(hardList) + { + hl = hardList; + hardList = (yaffs_Object *)(hardList->hardLinks.next); + + in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId); + + if(in) + { + // Add the hardlink pointers + hl->variant.hardLinkVariant.equivalentObject=in; + list_add(&hl->hardLinks,&in->hardLinks); + } + else + { + //Todo Need to report/handle this better. + // Got a problem... hardlink to a non-existant object + hl->variant.hardLinkVariant.equivalentObject=NULL; + INIT_LIST_HEAD(&hl->hardLinks); + + } + + } + + { + struct list_head *i; + struct list_head *n; + + yaffs_Object *l; + // Soft delete all the unlinked files + list_for_each_safe(i,n,&dev->unlinkedDir->variant.directoryVariant.children) + { + if(i) + { + l = list_entry(i, yaffs_Object,siblings); + if(l->deleted) + { + yaffs_SoftDeleteFile(l); + } + } + } + } + + + return YAFFS_OK; +} + + +////////////////////////// Directory Functions ///////////////////////// + + +static void yaffs_AddObjectToDirectory(yaffs_Object *directory, yaffs_Object *obj) +{ + + if(obj->siblings.prev == NULL) + { + // Not initialised + INIT_LIST_HEAD(&obj->siblings); + + } + else if(!list_empty(&obj->siblings)) + { + // If it is holed up somewhere else, un hook it + list_del_init(&obj->siblings); + } + // Now add it + list_add(&obj->siblings,&directory->variant.directoryVariant.children); + obj->parent = directory; + + if(directory == obj->myDev->unlinkedDir) + { + obj->unlinked = 1; + obj->myDev->nUnlinkedFiles++; + obj->renameAllowed = 0; + } +} + +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj) +{ + list_del_init(&obj->siblings); + obj->parent = NULL; +} + +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *directory,const char *name) +{ + int sum; + + struct list_head *i; + char buffer[YAFFS_MAX_NAME_LENGTH+1]; + + yaffs_Object *l; + + sum = yaffs_CalcNameSum(name); + + list_for_each(i,&directory->variant.directoryVariant.children) + { + if(i) + { + l = list_entry(i, yaffs_Object,siblings); + + // Special case for lost-n-found + if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND) + { + if(yaffs_strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0) + { + return l; + } + } + else if(yaffs_SumCompare(l->sum, sum)) + { + // Do a real check + yaffs_GetObjectName(l,buffer,YAFFS_MAX_NAME_LENGTH); + if(yaffs_strcmp(name,buffer) == 0) + { + return l; + } + + } + } + } + + return NULL; +} + + +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *)) +{ + struct list_head *i; + yaffs_Object *l; + + + list_for_each(i,&theDir->variant.directoryVariant.children) + { + if(i) + { + l = list_entry(i, yaffs_Object,siblings); + if(l && !fn(l)) + { + return YAFFS_FAIL; + } + } + } + + return YAFFS_OK; + +} + + +// GetEquivalentObject dereferences any hard links to get to the +// actual object. + +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj) +{ + if(obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) + { + // We want the object id of the equivalent object, not this one + obj = obj->variant.hardLinkVariant.equivalentObject; + } + return obj; + +} + +int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize) +{ + memset(name,0,buffSize); + + if(obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) + { + strncpy(name,YAFFS_LOSTNFOUND_NAME,buffSize - 1); + } + else if(obj->chunkId <= 0) + { + char locName[20]; + // make up a name + sprintf(locName,"%s%d",YAFFS_LOSTNFOUND_PREFIX,obj->objectId); + strncpy(name,locName,buffSize - 1); + + } +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM + else if(obj->shortName[0]) + { + strcpy(name,obj->shortName); + } +#endif + else + { + __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer; + + memset(buffer,0,YAFFS_BYTES_PER_CHUNK); + + if(obj->chunkId >= 0) + { + yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL,1); + } + strncpy(name,oh->name,buffSize - 1); + } + + return strlen(name); +} + +int yaffs_GetObjectFileLength(yaffs_Object *obj) +{ + + // Dereference any hard linking + obj = yaffs_GetEquivalentObject(obj); + + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) + { + return obj->variant.fileVariant.fileSize; + } + if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) + { + return strlen(obj->variant.symLinkVariant.alias); + } + else + { + // Only a directory should drop through to here + return YAFFS_BYTES_PER_CHUNK; + } +} + +int yaffs_GetObjectLinkCount(yaffs_Object *obj) +{ + int count = 0; + struct list_head *i; + + if(!obj->unlinked) + { + count++; // the object itself + } + list_for_each(i,&obj->hardLinks) + { + count++; // add the hard links; + } + return count; + +} + + +int yaffs_GetObjectInode(yaffs_Object *obj) +{ + obj = yaffs_GetEquivalentObject(obj); + + return obj->objectId; +} + +unsigned yaffs_GetObjectType(yaffs_Object *obj) +{ + obj = yaffs_GetEquivalentObject(obj); + + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: return DT_REG; break; + case YAFFS_OBJECT_TYPE_DIRECTORY: return DT_DIR; break; + case YAFFS_OBJECT_TYPE_SYMLINK: return DT_LNK; break; + case YAFFS_OBJECT_TYPE_HARDLINK: return DT_REG; break; + case YAFFS_OBJECT_TYPE_SPECIAL: + if(S_ISFIFO(obj->st_mode)) return DT_FIFO; + if(S_ISCHR(obj->st_mode)) return DT_CHR; + if(S_ISBLK(obj->st_mode)) return DT_BLK; + if(S_ISSOCK(obj->st_mode)) return DT_SOCK; + default: return DT_REG; break; + } +} + +char *yaffs_GetSymlinkAlias(yaffs_Object *obj) +{ + obj = yaffs_GetEquivalentObject(obj); + if(obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) + { + return yaffs_CloneString(obj->variant.symLinkVariant.alias); + } + else + { + return yaffs_CloneString(""); + } +} + +#ifndef CONFIG_YAFFS_WINCE + +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr) +{ + unsigned int valid = attr->ia_valid; + + if(valid & ATTR_MODE) obj->st_mode = attr->ia_mode; + if(valid & ATTR_UID) obj->st_uid = attr->ia_uid; + if(valid & ATTR_GID) obj->st_gid = attr->ia_gid; + + if(valid & ATTR_ATIME) obj->st_atime = Y_TIME_CONVERT(attr->ia_atime); + if(valid & ATTR_CTIME) obj->st_ctime = Y_TIME_CONVERT(attr->ia_ctime); + if(valid & ATTR_MTIME) obj->st_mtime = Y_TIME_CONVERT(attr->ia_mtime); + + + if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size); + + yaffs_UpdateObjectHeader(obj,NULL,1); + + return YAFFS_OK; + +} + +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr) +{ + unsigned int valid = 0; + + attr->ia_mode = obj->st_mode; valid |= ATTR_MODE; + attr->ia_uid = obj->st_uid; valid |= ATTR_UID; + attr->ia_gid = obj->st_gid; valid |= ATTR_GID; + + + Y_TIME_CONVERT(attr->ia_atime) = obj->st_atime; valid |= ATTR_ATIME; + Y_TIME_CONVERT(attr->ia_ctime) = obj->st_ctime; valid |= ATTR_CTIME; + Y_TIME_CONVERT(attr->ia_mtime) = obj->st_mtime; valid |= ATTR_MTIME; + + attr->ia_size = yaffs_GetFileSize(obj); valid |= ATTR_SIZE; + + attr->ia_valid = valid; + + return YAFFS_OK; + +} + +#endif + +int yaffs_DumpObject(yaffs_Object *obj) +{ +// __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + char name[257]; +// yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer; + +// memset(buffer,0,YAFFS_BYTES_PER_CHUNK); + +// if(obj->chunkId >= 0) +// { +// yaffs_ReadChunkFromNAND(obj->myDev,obj->chunkId,buffer,NULL); +// } + + yaffs_GetObjectName(obj,name,256); + + T(YAFFS_TRACE_ALWAYS,(TSTR("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n" TENDSTR), + obj->objectId,yaffs_GetObjectInode(obj), name, obj->dirty, obj->valid, obj->serial, + obj->sum, obj->chunkId, yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj))); + +#if 0 + YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d\n", + obj->objectId, oh->name, obj->dirty, obj->valid, obj->serial, + obj->sum, obj->chunkId)); + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: + YPRINTF((" FILE length %d\n",obj->variant.fileVariant.fileSize)); + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + YPRINTF((" DIRECTORY\n")); + break; + case YAFFS_OBJECT_TYPE_HARDLINK: //todo + case YAFFS_OBJECT_TYPE_SYMLINK: + case YAFFS_OBJECT_TYPE_UNKNOWN: + default: + } +#endif + + return YAFFS_OK; +} + + +///////////////////////// Initialisation code /////////////////////////// + + + +int yaffs_GutsInitialise(yaffs_Device *dev) +{ + unsigned x; + int bits; + int extraBits; + int nBlocks; + + if( dev->nBytesPerChunk != YAFFS_BYTES_PER_CHUNK || + dev->nChunksPerBlock < 2 || + dev->nReservedBlocks < 2 || + dev->startBlock <= 0 || + dev->endBlock <= 0 || + dev->endBlock <= (dev->startBlock + dev->nReservedBlocks) + ) + { + //these parameters must be set before stating yaffs + // Other parameters startBlock, + return YAFFS_FAIL; + } + + + + if(!yaffs_CheckStructures()) + { + T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs_CheckStructures failed\n" TENDSTR))); + return YAFFS_FAIL; + } + + if(dev->isMounted) + { + T(YAFFS_TRACE_ALWAYS,(TSTR("yaffs: device already mounted\n" TENDSTR))); + return YAFFS_FAIL; + } + + dev->isMounted = 1; + + if(dev->startBlock <= 0 || + (dev->endBlock - dev->startBlock) < 10) + { + T(YAFFS_TRACE_ALWAYS,(TSTR("startBlock %d or endBlock %d invalid\n" TENDSTR), + dev->startBlock, dev->endBlock)); + return YAFFS_FAIL; + } + + nBlocks = dev->endBlock - dev->startBlock + 1; + + + + // OK now calculate a few things for the device + // Calculate chunkGroupBits. + // We need to find the next power of 2 > than endBlock + + x = dev->nChunksPerBlock * (dev->endBlock+1); + + for(bits = extraBits = 0; x > 1; bits++) + { + if(x & 1) extraBits++; + x >>= 1; + } + + if(extraBits > 0) bits++; + + + // Level0 Tnodes are 16 bits, so if the bitwidth of the + // chunk range we're using is greater than 16 we need + // to figure out chunk shift and chunkGroupSize + if(bits <= 16) + { + dev->chunkGroupBits = 0; + } + else + { + dev->chunkGroupBits = bits - 16; + } + + dev->chunkGroupSize = 1 << dev->chunkGroupBits; + + if(dev->nChunksPerBlock < dev->chunkGroupSize) + { + // We have a problem because the soft delete won't work if + // the chunk group size > chunks per block. + // This can be remedied by using larger "virtual blocks". + + return YAFFS_FAIL; + } + + + + + // More device initialisation + dev->garbageCollections = 0; + dev->passiveGarbageCollections = 0; + dev->currentDirtyChecker = 0; + dev->bufferedBlock = -1; + dev->doingBufferedBlockRewrite = 0; + dev->nDeletedFiles = 0; + dev->nBackgroundDeletions=0; + dev->nUnlinkedFiles = 0; + dev->eccFixed=0; + dev->eccUnfixed=0; + dev->tagsEccFixed=0; + dev->tagsEccUnfixed=0; + + dev->localBuffer = YMALLOC(dev->nBytesPerChunk); + + + + + yaffs_InitialiseBlocks(dev,nBlocks); + + yaffs_InitialiseTnodes(dev); + + yaffs_InitialiseObjects(dev); + + if(dev->nShortOpCaches > 0) + { + int i; + + if(dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) + { + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES; + } + + dev->srCache = YMALLOC( dev->nShortOpCaches * sizeof(yaffs_ChunkCache)); + + for(i=0; i < dev->nShortOpCaches; i++) + { + dev->srCache[i].object = NULL; + dev->srCache[i].lastUse = 0; + dev->srCache[i].dirty = 0; + } + dev->srLastUse = 0; + } + + dev->cacheHits = 0; + + + // Initialise the unlinked, root and lost and found directories + dev->lostNFoundDir = dev->rootDir = dev->unlinkedDir = NULL; + + dev->unlinkedDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_UNLINKED, S_IFDIR); + + dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR); + dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_LOSTNFOUND_MODE | S_IFDIR); + yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir); + + + // Now scan the flash. + yaffs_Scan(dev); + + // Zero out stats + dev->nPageReads = 0; + dev->nPageWrites = 0; + dev->nBlockErasures = 0; + dev->nGCCopies = 0; + dev->nRetriedWrites = 0; + dev->nRetiredBlocks = 0; + + + return YAFFS_OK; + +} + +void yaffs_Deinitialise(yaffs_Device *dev) +{ + if(dev->isMounted) + { + + yaffs_DeinitialiseBlocks(dev); + yaffs_DeinitialiseTnodes(dev); + yaffs_DeinitialiseObjects(dev); + if(dev->nShortOpCaches > 0) + YFREE(dev->srCache); + YFREE(dev->localBuffer); + } + +} + +#if 0 + +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev) +{ + int nFree = dev->nFreeChunks - (dev->nChunksPerBlock * YAFFS_RESERVED_BLOCKS); + + struct list_head *i; + yaffs_Object *l; + + + // To the free chunks add the chunks that are in the deleted unlinked files. + list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children) + { + l = list_entry(i, yaffs_Object,siblings); + if(l->deleted) + { + nFree++; + nFree += l->nDataChunks; + } + } + + + // printf("___________ nFreeChunks is %d nFree is %d\n",dev->nFreeChunks,nFree); + + if(nFree < 0) nFree = 0; + + return nFree; + +} + +#endif + +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev) +{ + int nFree; + int pending; + int b; + int nDirtyCacheChunks=0; + + yaffs_BlockInfo *blk; + + struct list_head *i; + yaffs_Object *l; + + for(nFree = 0, b = dev->startBlock; b <= dev->endBlock; b++) + { + blk = yaffs_GetBlockInfo(dev,b); + + switch(blk->blockState) + { + case YAFFS_BLOCK_STATE_EMPTY: + case YAFFS_BLOCK_STATE_ALLOCATING: + case YAFFS_BLOCK_STATE_FULL: nFree += (dev->nChunksPerBlock - blk->pagesInUse); break; + default: break; + } + } + + + pending = 0; + + // To the free chunks add the chunks that are in the deleted unlinked files. + list_for_each(i,&dev->unlinkedDir->variant.directoryVariant.children) + { + if(i) + { + l = list_entry(i, yaffs_Object,siblings); + if(l->deleted) + { + pending++; + pending += l->nDataChunks; + } + } + } + + + + //printf("___________ really free is %d, pending %d, nFree is %d\n",nFree,pending, nFree+pending); + + if(nFree != dev->nFreeChunks) + { + // printf("___________Different! really free is %d, nFreeChunks %d\n",nFree dev->nFreeChunks); + } + + nFree += pending; + + // Now count the number of dirty chunks in the cache and subtract those + + { + int i; + for(i = 0; i < dev->nShortOpCaches; i++) + { + if(dev->srCache[i].dirty) nDirtyCacheChunks++; + } + } + + nFree -= nDirtyCacheChunks; + + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock); + + if(nFree < 0) nFree = 0; + + return nFree; + +} + + + +/////////////////// YAFFS test code ////////////////////////////////// + +#define yaffs_CheckStruct(structure,syze, name) \ + if(sizeof(structure) != syze) \ + { \ + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),name,syze,sizeof(structure))); \ + return YAFFS_FAIL; \ + } + + +static int yaffs_CheckStructures(void) +{ + yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") + yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") + yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG + yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode") +#endif + yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader") + + + return YAFFS_OK; +} + +#if 0 +void yaffs_GutsTest(yaffs_Device *dev) +{ + + if(yaffs_CheckStructures() != YAFFS_OK) + { + T(YAFFS_TRACE_ALWAYS,(TSTR("One or more structures malformed-- aborting\n" TENDSTR))); + return; + } + + yaffs_TnodeTest(dev); + yaffs_ObjectTest(dev); +} +#endif + + + + + + Index: linux-2.6.9/fs/yaffs/yaffs_fileem.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_fileem.c @@ -0,0 +1,271 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_fileem.c NAND emulation on top of files + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + //yaffs_fileem.c + +#include "yaffs_fileem.h" +#include "yaffs_guts.h" +#include "yaffsinterface.h" + +#include +#include +#include +#include +#include +#include +#include + +#define FILE_SIZE_IN_MEG 2 + +// #define YAFFS_ERROR_TESTING + +#define BLOCK_SIZE (32 * 528) +#define BLOCKS_PER_MEG ((1024*1024)/(32 * 512)) +#define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG) +#define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE) + + +static int h; +static __u8 ffChunk[528]; + +static int eraseDisplayEnabled; + +static int markedBadBlocks[] = { 1, 4, -1}; + +static int IsAMarkedBadBlock(int blk) +{ +#if YAFFS_ERROR_TESTING + int *m = markedBadBlocks; + + while(*m >= 0) + { + if(*m == blk) return 1; + m++; + } +#endif + return 0; +} + + +static __u8 yaffs_WriteFailCorruption(int chunkInNAND) +{ +#if YAFFS_ERROR_TESTING + + // Whole blocks that fail + switch(chunkInNAND/YAFFS_CHUNKS_PER_BLOCK) + { + case 50: + case 52: + return 7; + } + + // Single blocks that fail + switch(chunkInNAND) + { + case 2000: + case 2003: + case 3000: + case 3001: + return 3;// ding two bits + case 2001: + case 3003: + case 3004: + case 3005: + case 3006: + case 3007: return 1;// ding one bit + + + } +#endif + return 0; +} + +static void yaffs_ModifyWriteData(int chunkInNAND,__u8 *data) +{ +#if YAFFS_ERROR_TESTING + if(data) + { + *data ^= yaffs_WriteFailCorruption(chunkInNAND); + } +#endif +} + +static __u8 yaffs_ReadFailCorruption(int chunkInNAND) +{ + switch(chunkInNAND) + { +#if YAFFS_ERROR_TESTING + case 500: + return 3;// ding two bits + case 700: + case 750: + return 1;// ding one bit + +#endif + default: return 0; + + } +} + +static void yaffs_ModifyReadData(int chunkInNAND,__u8 *data) +{ +#if YAFFS_ERROR_TESTING + if(data) + { + *data ^= yaffs_ReadFailCorruption(chunkInNAND); + } +#endif +} + + + + + +static void CheckInit(yaffs_Device *dev) +{ + static int initialised = 0; + + int length; + int nWritten; + + + if(!initialised) + { + memset(ffChunk,0xFF,528); + +//#ifdef YAFFS_DUMP +// h = open("yaffs-em-file" , O_RDONLY); +//#else + h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE); +//#endif + if(h < 0) + { + perror("Fatal error opening yaffs emulation file"); + exit(1); + } + initialised = 1; + + length = lseek(h,0,SEEK_END); + nWritten = 528; + while(length < FILE_SIZE_IN_BYTES && nWritten == 528) + { + write(h,ffChunk,528); + length = lseek(h,0,SEEK_END); + } + if(nWritten != 528) + { + perror("Fatal error expanding yaffs emulation file"); + exit(1); + + } + + close(h); + +#ifdef YAFFS_DUMP + h = open("yaffs-em-file" , O_RDONLY); +#else + h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE); +#endif + } +} + +int yaffs_FEWriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare) +{ + __u8 localData[512]; + + int pos; + + pos = chunkInNAND * 528; + + CheckInit(dev); + + + if(data) + { + memcpy(localData,data,512); + yaffs_ModifyWriteData(chunkInNAND,localData); + lseek(h,pos,SEEK_SET); + write(h,localData,512); + } + + pos += 512; + + if(spare) + { + lseek(h,pos,SEEK_SET); + write(h,spare,16); + } + + return YAFFS_OK; +} + + +int yaffs_FEReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare) +{ + int pos; + + pos = chunkInNAND * 528; + + + CheckInit(dev); + + if(data) + { + lseek(h,pos,SEEK_SET); + read(h,data,512); + yaffs_ModifyReadData(chunkInNAND,data); + } + + pos += 512; + + if(spare) + { + lseek(h,pos,SEEK_SET); + read(h,spare,16); + } + + return YAFFS_OK; +} + + +int yaffs_FEEraseBlockInNAND(yaffs_Device *dev,int blockInNAND) +{ + int i; + + CheckInit(dev); + + if(eraseDisplayEnabled) + { + printf("Erasing block %d\n",blockInNAND); + } + + lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET); + for(i = 0; i < 32; i++) + { + write(h,ffChunk,528); + } + + switch(blockInNAND) + { + case 10: + case 15: return YAFFS_FAIL; + + } + return YAFFS_OK; +} + +int yaffs_FEInitialiseNAND(yaffs_Device *dev) +{ + return YAFFS_OK; +} Index: linux-2.6.9/fs/yaffs/nand_ecc.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/nand_ecc.c @@ -0,0 +1,223 @@ +/* + * drivers/mtd/nand_ecc.c + * + * Copyright (C) 2000 Steven J. Hill (sjhill@cotw.com) + * Toshiba America Electronics Components, Inc. + * + * $Id: nand_ecc.c,v 1.3 2002/09/27 20:50:50 charles Exp $ + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * version 2.1 as published by the Free Software Foundation. + * + * This file contains an ECC algorithm from Toshiba that detects and + * corrects 1 bit errors in a 256 byte block of data. + */ + // Minor tweak by Charles Manning to prevent exporting symbols + // when compiled in with yaffs. + +const char *nand_ecc_c_version = "$Id: nand_ecc.c,v 1.3 2002/09/27 20:50:50 charles Exp $"; + +#if 0 +#include +#include +#include +#endif + +#include "yportenv.h" + +/* + * Pre-calculated 256-way 1 byte column parity + */ +static const u_char nand_ecc_precalc_table[] = { + 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00, + 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65, + 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66, + 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03, + 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69, + 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c, + 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f, + 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a, + 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a, + 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f, + 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c, + 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69, + 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03, + 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66, + 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65, + 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00 +}; + + +/* + * Creates non-inverted ECC code from line parity + */ +static void nand_trans_result(u_char reg2, u_char reg3, + u_char *ecc_code) +{ + u_char a, b, i, tmp1, tmp2; + + /* Initialize variables */ + a = b = 0x80; + tmp1 = tmp2 = 0; + + /* Calculate first ECC byte */ + for (i = 0; i < 4; i++) { + if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */ + tmp1 |= b; + b >>= 1; + if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */ + tmp1 |= b; + b >>= 1; + a >>= 1; + } + + /* Calculate second ECC byte */ + b = 0x80; + for (i = 0; i < 4; i++) { + if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */ + tmp2 |= b; + b >>= 1; + if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */ + tmp2 |= b; + b >>= 1; + a >>= 1; + } + + /* Store two of the ECC bytes */ + ecc_code[0] = tmp1; + ecc_code[1] = tmp2; +} + +/* + * Calculate 3 byte ECC code for 256 byte block + */ +void nand_calculate_ecc (const u_char *dat, u_char *ecc_code) +{ + u_char idx, reg1, reg2, reg3; + int j; + + /* Initialize variables */ + reg1 = reg2 = reg3 = 0; + ecc_code[0] = ecc_code[1] = ecc_code[2] = 0; + + /* Build up column parity */ + for(j = 0; j < 256; j++) { + + /* Get CP0 - CP5 from table */ + idx = nand_ecc_precalc_table[dat[j]]; + reg1 ^= (idx & 0x3f); + + /* All bit XOR = 1 ? */ + if (idx & 0x40) { + reg3 ^= (u_char) j; + reg2 ^= ~((u_char) j); + } + } + + /* Create non-inverted ECC code from line parity */ + nand_trans_result(reg2, reg3, ecc_code); + + /* Calculate final ECC code */ + ecc_code[0] = ~ecc_code[0]; + ecc_code[1] = ~ecc_code[1]; + ecc_code[2] = ((~reg1) << 2) | 0x03; +} + +/* + * Detect and correct a 1 bit error for 256 byte block + */ +int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc) +{ + u_char a, b, c, d1, d2, d3, add, bit, i; + + /* Do error detection */ + d1 = calc_ecc[0] ^ read_ecc[0]; + d2 = calc_ecc[1] ^ read_ecc[1]; + d3 = calc_ecc[2] ^ read_ecc[2]; + + if ((d1 | d2 | d3) == 0) { + /* No errors */ + return 0; + } + else { + a = (d1 ^ (d1 >> 1)) & 0x55; + b = (d2 ^ (d2 >> 1)) & 0x55; + c = (d3 ^ (d3 >> 1)) & 0x54; + + /* Found and will correct single bit error in the data */ + if ((a == 0x55) && (b == 0x55) && (c == 0x54)) { + c = 0x80; + add = 0; + a = 0x80; + for (i=0; i<4; i++) { + if (d1 & c) + add |= a; + c >>= 2; + a >>= 1; + } + c = 0x80; + for (i=0; i<4; i++) { + if (d2 & c) + add |= a; + c >>= 2; + a >>= 1; + } + bit = 0; + b = 0x04; + c = 0x80; + for (i=0; i<3; i++) { + if (d3 & c) + bit |= b; + c >>= 2; + b >>= 1; + } + b = 0x01; + a = dat[add]; + a ^= (b << bit); + dat[add] = a; + return 1; + } + else { + i = 0; + while (d1) { + if (d1 & 0x01) + ++i; + d1 >>= 1; + } + while (d2) { + if (d2 & 0x01) + ++i; + d2 >>= 1; + } + while (d3) { + if (d3 & 0x01) + ++i; + d3 >>= 1; + } + if (i == 1) { + /* ECC Code Error Correction */ + read_ecc[0] = calc_ecc[0]; + read_ecc[1] = calc_ecc[1]; + read_ecc[2] = calc_ecc[2]; + return 2; + } + else { + /* Uncorrectable Error */ + return -1; + } + } + } + + /* Should never happen */ + return -1; +} + +#if 0 +EXPORT_SYMBOL(nand_calculate_ecc); +EXPORT_SYMBOL(nand_correct_data); + +MODULE_LICENSE("GPL"); +MODULE_AUTHOR("Steven J. Hill "); +MODULE_DESCRIPTION("Generic NAND ECC support"); +#endif Index: linux-2.6.9/fs/yaffs/yaffs_ramem.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_ramem.c @@ -0,0 +1,312 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_ramem.c NAND emulation on top of a chunk of RAM + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + //yaffs_ramem.c + // Since this creates the RAM block at start up it is pretty useless for testing the scanner. + +const char *yaffs_ramem_c_version = "$Id: yaffs_ramem.c,v 1.6 2002/11/26 01:15:37 charles Exp $"; + +#ifndef __KERNEL__ +#define CONFIG_YAFFS_RAM_ENABLED +#endif + +#ifdef CONFIG_YAFFS_RAM_ENABLED + +#include "yportenv.h" + +#include "yaffs_nandemul.h" +#include "yaffs_guts.h" +#include "yaffsinterface.h" +#include "devextras.h" + + +#define EM_SIZE_IN_MEG 2 + +#define BLOCK_SIZE (32 * 528) +#define BLOCKS_PER_MEG ((1024*1024)/(32 * 512)) +#define FILE_SIZE_IN_BLOCKS (FILE_SIZE_IN_MEG * BLOCKS_PER_MEG) +#define FILE_SIZE_IN_BYTES (FILE_SIZE_IN_BLOCKS * BLOCK_SIZE) + + + +#define DEFAULT_SIZE_IN_MB 2 + +typedef struct +{ + __u8 data[528]; // Data + spare + int count[3]; // The programming count for each area of + // the page (0..255,256..511,512..527 + int empty; // is this empty? +} nandemul_Page; + +typedef struct +{ + nandemul_Page page[32]; // The pages in the block + __u8 damaged; // Is the block damaged? + +} nandemul_Block; + + + +typedef struct +{ + nandemul_Block **block; + int nBlocks; +} nandemul_Device; + +static nandemul_Device ned; + +int sizeInMB = DEFAULT_SIZE_IN_MB; + + +static void nandemul_yield(int n) +{ +#ifdef __KERNEL__ + if(n > 0) schedule_timeout(n); +#endif + +} + + +static void nandemul_ReallyEraseBlock(int blockNumber) +{ + int i; + + nandemul_Block *theBlock = ned.block[blockNumber]; + + for(i = 0; i < 32; i++) + { + memset(theBlock->page[i].data,0xff,528); + theBlock->page[i].count[0] = 0; + theBlock->page[i].count[1] = 0; + theBlock->page[i].count[2] = 0; + theBlock->page[i].empty = 1; + nandemul_yield(2); + } + +} + + +int nandemul_CalcNBlocks(void) +{ + switch(sizeInMB) + { + case 8: + case 16: + case 32: + case 64: + case 128: + case 256: + case 512: + break; + default: + sizeInMB = DEFAULT_SIZE_IN_MB; + } + return sizeInMB * 64; +} + + + +static int CheckInit(void) +{ + static int initialised = 0; + + int i; + int fail = 0; + int nBlocks; + int nAllocated = 0; + + if(initialised) + { + return YAFFS_OK; + } + + + nBlocks = nandemul_CalcNBlocks(); + + ned.block = YMALLOC(sizeof(nandemul_Block *) * nBlocks); + + if(!ned.block) return 0; + + for(i=0; i damaged = 0; + nAllocated++; + } + } + + if(fail) + { + for(i = 0; i < nAllocated; i++) + { + YFREE(ned.block[i]); + } + YFREE(ned.block); + + T(YAFFS_TRACE_ALWAYS,("Allocation failed, could only allocate %dMB of %dMB requested.\n", + nAllocated/64,sizeInMB)); + return 0; + } + + ned.nBlocks = nBlocks; + + initialised = 1; + + return 1; +} + +int nandemul_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare) +{ + int blk; + int pg; + int i; + + __u8 *x; + + __u8 *spareAsBytes = (__u8 *)spare; + + + CheckInit(); + + blk = chunkInNAND/32; + pg = chunkInNAND%32; + + + if(data) + { + x = ned.block[blk]->page[pg].data; + + for(i = 0; i < 512; i++) + { + x[i] &=data[i]; + } + + ned.block[blk]->page[pg].count[0]++; + ned.block[blk]->page[pg].count[1]++; + ned.block[blk]->page[pg].empty = 0; + } + + + if(spare) + { + x = &ned.block[blk]->page[pg].data[512]; + + for(i = 0; i < 16; i++) + { + x[i] &=spareAsBytes[i]; + } + ned.block[blk]->page[pg].count[2]++; + } + + if(spare || data) + { + nandemul_yield(1); + } + + return YAFFS_OK; +} + + +int nandemul_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare) +{ + int blk; + int pg; + + + CheckInit(); + + blk = chunkInNAND/32; + pg = chunkInNAND%32; + + + if(data) + { + memcpy(data,ned.block[blk]->page[pg].data,512); + } + + + if(spare) + { + memcpy(spare,&ned.block[blk]->page[pg].data[512],16); + } + + return YAFFS_OK; +} + + +int nandemul_CheckChunkErased(yaffs_Device *dev,int chunkInNAND) +{ + int blk; + int pg; + int i; + + + CheckInit(); + + blk = chunkInNAND/32; + pg = chunkInNAND%32; + + + for(i = 0; i < 528; i++) + { + if(ned.block[blk]->page[pg].data[i] != 0xFF) + { + return YAFFS_FAIL; + } + } + + return YAFFS_OK; + +} + +int nandemul_EraseBlockInNAND(yaffs_Device *dev, int blockNumber) +{ + + CheckInit(); + + if(blockNumber < 0 || blockNumber >= ned.nBlocks) + { + T(YAFFS_TRACE_ALWAYS,("Attempt to erase non-existant block %d\n",blockNumber)); + } + else if(ned.block[blockNumber]->damaged) + { + T(YAFFS_TRACE_ALWAYS,("Attempt to erase damaged block %d\n",blockNumber)); + } + else + { + nandemul_ReallyEraseBlock(blockNumber); + } + + return YAFFS_OK; +} + +int nandemul_InitialiseNAND(yaffs_Device *dev) +{ + return YAFFS_OK; +} + +#endif //YAFFS_RAM_ENABLED + Index: linux-2.6.9/fs/yaffs/yaffs_ecc.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_ecc.h @@ -0,0 +1,30 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * + * yaffs_ecc.c: ECC generation/correction algorithms. + * + * Copyright (C) 2002 Aleph One Ltd. + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + + /* + * This code implements the ECC algorithm used in SmartMedia. + * + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes. + * The two unused bit are set to 1. + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC + * blocks are used on a 512-byte NAND page. + * + */ + +#ifndef __YAFFS_ECC_H__ +#define __YAFFS_ECC_H__ +void yaffs_ECCCalculate(const unsigned char *data,unsigned char *ecc); +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc, const unsigned char *test_ecc); +#endif Index: linux-2.6.9/fs/yaffs/yaffs_nandemul.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_nandemul.h @@ -0,0 +1,38 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + * yaffs_nandemul.h: Interface to emulated NAND functions + * + * $Id: yaffs_nandemul.h,v 1.4 2002/09/27 20:50:50 charles Exp $ + */ + +#ifndef __YAFFS_NANDEMUL_H__ +#define __YAFFS_NANDEMUL_H__ + +#include "yaffs_guts.h" + + +/* WriteChunkToNAND and ReadChunkFromNAND are used with two pointers. + * If either of these pointers are null, then that field will not be + * transferred. + */ + +int nandemul_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare); +int nandemul_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare); +int nandemul_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND); +int nandemul_InitialiseNAND(struct yaffs_DeviceStruct *dev); + +#endif + Index: linux-2.6.9/fs/yaffs/yaffs_fs.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_fs.c @@ -0,0 +1,1709 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_fs.c + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This is the file system front-end to YAFFS that hooks it up to + * the VFS. + * + * Special notes: + * >> sb->u.generic_sbp points to the yaffs_Device associated with this superblock + * >> inode->u.generic_ip points to the associated yaffs_Object. + * + * + * Acknowledgements: + * * Luc van OostenRyck for numerous patches. + * * Nick Bane for numerous patches. + * * Nick Bane for 2.5/2.6 integration. + * * Andras Toth for mknod rdev issue. + * * Michael Fischer for finding the problem with inode inconsistency. + * * Some code bodily lifted from JFFS2. + */ + + +const char *yaffs_fs_c_version = "$Id: yaffs_fs.c,v 1.35 2004/10/20 20:12:43 charles Exp $"; +extern const char *yaffs_guts_c_version; + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) + +#include /* Added NCB 15-8-2003 */ +#include +#define UnlockPage(p) unlock_page(p) +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags) +//#define kdevname(x) cdevname(to_kdev_t(x)) +#define kdevname(x) "(unavailable)" // temporary fix + +#else + +#include + +#endif + + +#include + +#include "yportenv.h" +#include "yaffs_guts.h" + + + + +unsigned yaffs_traceMask = YAFFS_TRACE_ALWAYS | YAFFS_TRACE_BAD_BLOCKS; + +#ifdef CONFIG_YAFFS_RAM_ENABLED +#include "yaffs_nandemul.h" +// 2 MB of RAM for emulation +#define YAFFS_RAM_EMULATION_SIZE 0x200000 +#endif //CONFIG_YAFFS_RAM_ENABLED + +#ifdef CONFIG_YAFFS_MTD_ENABLED +#include +#include "yaffs_mtdif.h" +#endif //CONFIG_YAFFS_MTD_ENABLED + +//#define T(x) printk x + + + +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)((iptr)->u.generic_ip)) +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode) +//NCB #define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp) + +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info) +#else +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp) +#endif + + +static void yaffs_put_super(struct super_block *sb); + +static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos); +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos); +static int yaffs_file_flush(struct file* file); + +static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync); + +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir); + +//#if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */ +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n); +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n); +#else +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode); +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry); +#endif +static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry); +static int yaffs_unlink(struct inode * dir, struct dentry *dentry); +static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname); +static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode); + +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev); +#else +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev); +#endif +static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry); +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr); + +//#if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */ +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf); +#else +static int yaffs_statfs(struct super_block *sb, struct statfs *buf); +#endif +static void yaffs_read_inode (struct inode *inode); + +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data); +#else +static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent); +#endif + +static void yaffs_put_inode (struct inode *inode); +static void yaffs_delete_inode(struct inode *); +static void yaffs_clear_inode(struct inode *); + +static int yaffs_readpage(struct file *file, struct page * page); +static int yaffs_writepage(struct page *page); +static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to); +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to); + +static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen); +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd); + + + + +static struct address_space_operations yaffs_file_address_operations = { + readpage: yaffs_readpage, + writepage: yaffs_writepage, + prepare_write: yaffs_prepare_write, + commit_write: yaffs_commit_write +}; + + +static struct file_operations yaffs_file_operations = { +#ifdef CONFIG_YAFFS_USE_GENERIC_RW + read: generic_file_read, + write: generic_file_write, +#else + read: yaffs_file_read, + write: yaffs_file_write, +#endif + mmap: generic_file_mmap, + flush: yaffs_file_flush, + fsync: yaffs_sync_object, +}; + + +static struct inode_operations yaffs_file_inode_operations = { + setattr: yaffs_setattr, +}; + + +struct inode_operations yaffs_symlink_inode_operations = +{ + readlink: yaffs_readlink, + follow_link: yaffs_follow_link, + setattr: yaffs_setattr +}; + +static struct inode_operations yaffs_dir_inode_operations = { + create: yaffs_create, + lookup: yaffs_lookup, + link: yaffs_link, + unlink: yaffs_unlink, + symlink: yaffs_symlink, + mkdir: yaffs_mkdir, + rmdir: yaffs_unlink, + mknod: yaffs_mknod, + rename: yaffs_rename, + setattr: yaffs_setattr, +}; + +static struct file_operations yaffs_dir_operations = { + read: generic_read_dir, + readdir: yaffs_readdir, + fsync: yaffs_sync_object, +}; + + +static struct super_operations yaffs_super_ops = { + statfs: yaffs_statfs, + read_inode: yaffs_read_inode, + put_inode: yaffs_put_inode, + put_super: yaffs_put_super, +// remount_fs: + delete_inode: yaffs_delete_inode, + clear_inode: yaffs_clear_inode, +}; + + + +static void yaffs_GrossLock(yaffs_Device *dev) +{ + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs locking\n")); + + down(&dev->grossLock); +} + +static void yaffs_GrossUnlock(yaffs_Device *dev) +{ + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs unlocking\n")); + up(&dev->grossLock); + +} + +static int yaffs_readlink(struct dentry *dentry, char *buffer, int buflen) +{ + unsigned char *alias; + int ret; + + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; + + + yaffs_GrossLock(dev); + + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); + + yaffs_GrossUnlock(dev); + + if(!alias) + return -ENOMEM; + + ret = vfs_readlink(dentry, buffer, buflen, alias); + kfree(alias); + return ret; +} + +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd) +{ + unsigned char *alias; + int ret; + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev; + + + yaffs_GrossLock(dev); + + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry)); + + yaffs_GrossUnlock(dev); + + if(!alias) + return -ENOMEM; + + ret = vfs_follow_link(nd,alias); + kfree(alias); + return ret; +} + + +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj); + +/* + * Lookup is used to find objects in the fs + */ +//#if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */ +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) + +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *n) +#else +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry) +#endif +{ + yaffs_Object *obj; + struct inode *inode = NULL; // NCB 2.5/2.6 needs NULL here + + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev; + + + yaffs_GrossLock(dev); + + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup for %d:%s\n",yaffs_InodeToObject(dir)->objectId,dentry->d_name.name)); + + obj = yaffs_FindObjectByName(yaffs_InodeToObject(dir),dentry->d_name.name); + + obj = yaffs_GetEquivalentObject(obj); // in case it was a hardlink + + + + if(obj) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId)); + + inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj); + + if(inode) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_loookup dentry \n")); +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to d_add even if NULL inode */ +#if 0 + //dget(dentry); // try to solve directory bug + d_add(dentry,inode); + + yaffs_GrossUnlock(dev); + + // return dentry; + return NULL; +#endif + } + + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_lookup not found\n")); + + } + yaffs_GrossUnlock(dev); + +/* added NCB for 2.5/6 compatability - forces add even if inode is NULL which creates dentry hash*/ + d_add(dentry,inode); + + return NULL; + // return (ERR_PTR(-EIO)); + +} + +// For now put inode is just for debugging +// Put inode is called when the inode **structure** is put. +static void yaffs_put_inode(struct inode *inode) +{ + T(YAFFS_TRACE_OS,("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count))); + +} + +// clear is called to tell the fs to release any per-inode data it holds +static void yaffs_clear_inode(struct inode *inode) +{ + yaffs_Object *obj; + yaffs_Device *dev; + + obj = yaffs_InodeToObject(inode); + + T(YAFFS_TRACE_OS,("yaffs_clear_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count), + obj ? "object exists" : "null object")); + + if(obj) + { + dev = obj->myDev; + yaffs_GrossLock(dev); + + // Clear the association between the inode ant the yaffs_Object. + obj->myInode = NULL; + inode->u.generic_ip = NULL; + + // If the object freeing was deferred, then the real free happens now. + // This should fix the inode inconsistency problem. + + yaffs_HandleDeferedFree(obj); + + yaffs_GrossUnlock(dev); + } + + +} + +// delete is called when the link count is zero and the inode +// is put (ie. nobody wants to know about it anymore, time to +// delete the file). +// NB Must call clear_inode() +static void yaffs_delete_inode(struct inode *inode) +{ + yaffs_Object *obj = yaffs_InodeToObject(inode); + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,("yaffs_delete_inode: ino %d, count %d %s\n",(int)inode->i_ino, atomic_read(&inode->i_count), + obj ? "object exists" : "null object")); + + if(obj) + { + dev = obj->myDev; + yaffs_GrossLock(dev); + yaffs_DeleteFile(obj); + yaffs_GrossUnlock(dev); + } + clear_inode(inode); +} + + +static int yaffs_file_flush(struct file* file) +{ + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry); + + yaffs_Device *dev = obj->myDev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_flush object %d (%s)\n",obj->objectId, + obj->dirty ? "dirty" : "clean")); + + yaffs_GrossLock(dev); + + yaffs_FlushFile(obj,1); + + yaffs_GrossUnlock(dev); + + return 0; +} + + + +static int yaffs_readpage_nolock(struct file *f, struct page * pg) +{ + // Lifted from jffs2 + + yaffs_Object *obj; + unsigned char *pg_buf; + int ret; + + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage at %08x, size %08x\n", + (unsigned)(pg->index << PAGE_CACHE_SHIFT), (unsigned)PAGE_CACHE_SIZE)); + + obj = yaffs_DentryToObject(f->f_dentry); + + dev = obj->myDev; + + + if (!PageLocked(pg)) + PAGE_BUG(pg); + + pg_buf = kmap(pg); + /* FIXME: Can kmap fail? */ + + yaffs_GrossLock(dev); + + ret = yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE); + + yaffs_GrossUnlock(dev); + + if(ret >= 0) ret = 0; + + if (ret) { + ClearPageUptodate(pg); + SetPageError(pg); + } else { + SetPageUptodate(pg); + ClearPageError(pg); + } + + flush_dcache_page(pg); + kunmap(pg); + + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readpage done\n")); + return ret; +} + +static int yaffs_readpage_unlock(struct file *f, struct page *pg) +{ + int ret = yaffs_readpage_nolock(f,pg); + UnlockPage(pg); + return ret; +} + +static int yaffs_readpage(struct file *f, struct page * pg) +{ + return yaffs_readpage_unlock(f,pg); +} + +// writepage inspired by/stolen from smbfs +// + +static int yaffs_writepage(struct page *page) +{ + struct address_space *mapping = page->mapping; + struct inode *inode; + unsigned long end_index; + char *buffer; + yaffs_Object *obj; + int nWritten = 0; + unsigned nBytes; + + if (!mapping) + BUG(); + inode = mapping->host; + if (!inode) + BUG(); + + end_index = inode->i_size >> PAGE_CACHE_SHIFT; + + /* easy case */ + if (page->index < end_index) + { + nBytes = PAGE_CACHE_SIZE; + } + else + { + nBytes = inode->i_size & (PAGE_CACHE_SIZE-1); + } + // What's happening here? + ///* OK, are we completely out? */ + //if (page->index >= end_index+1 || !offset) + // return -EIO; + + get_page(page); + + + buffer = kmap(page); + + obj = yaffs_InodeToObject(inode); + yaffs_GrossLock(obj->myDev); + + + nWritten = yaffs_WriteDataToFile(obj,buffer,page->index << PAGE_CACHE_SHIFT,nBytes); + + yaffs_GrossUnlock(obj->myDev); + + kunmap(page); + SetPageUptodate(page); + UnlockPage(page); + put_page(page); + + return (nWritten == nBytes) ? 0 : -ENOSPC; +} + + + +static int yaffs_prepare_write(struct file *f, struct page *pg, unsigned offset, unsigned to) +{ + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_prepair_write\n")); + if(!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE)) + return yaffs_readpage_nolock(f,pg); + + return 0; + +} + +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset, unsigned to) +{ + + void *addr = page_address(pg) + offset; + loff_t pos = (((loff_t)pg->index) << PAGE_CACHE_SHIFT) + offset; + int nBytes = to - offset; + int nWritten; + + unsigned spos = pos; + unsigned saddr = (unsigned)addr; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write addr %x pos %x nBytes %d\n",saddr,spos,nBytes)); + + nWritten = yaffs_file_write(f,addr, nBytes, &pos); + + if(nWritten != nBytes) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write not same size nWritten %d nBytes %d\n",nWritten,nBytes)); + SetPageError(pg); + ClearPageUptodate(pg); + } + else + { + SetPageUptodate(pg); + } + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_commit_write returning %d\n",nWritten)); + + return nWritten; + +} + + + +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object *obj) +{ + if (inode && obj) + { + inode->i_ino = obj->objectId; + inode->i_mode = obj->st_mode; + inode->i_uid = obj->st_uid; + inode->i_gid = obj->st_gid; + inode->i_blksize = inode->i_sb->s_blocksize; +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) + + inode->i_rdev = old_decode_dev(obj->st_rdev); + inode->i_atime.tv_sec = (time_t)(obj->st_atime); + inode->i_atime.tv_nsec = 0; + inode->i_mtime.tv_sec = (time_t)obj->st_mtime; + inode->i_mtime.tv_nsec =0; + inode->i_ctime.tv_sec = (time_t)obj->st_ctime; + inode->i_ctime.tv_nsec = 0; +#else + inode->i_rdev = obj->st_rdev; + inode->i_atime = obj->st_atime; + inode->i_mtime = obj->st_mtime; + inode->i_ctime = obj->st_ctime; +#endif + inode->i_size = yaffs_GetObjectFileLength(obj); + inode->i_blocks = (inode->i_size + 511) >> 9; + + inode->i_nlink = yaffs_GetObjectLinkCount(obj); + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FillInode mode %x uid %d gid %d size %d count %d\n", + inode->i_mode, inode->i_uid, inode->i_gid, (int)inode->i_size, atomic_read(&inode->i_count))); + + switch (obj->st_mode & S_IFMT) + { + default: // fifo, device or socket + init_special_inode(inode, obj->st_mode,(dev_t)(obj->st_rdev)); + break; + case S_IFREG: // file + inode->i_op = &yaffs_file_inode_operations; + inode->i_fop = &yaffs_file_operations; + inode->i_mapping->a_ops = &yaffs_file_address_operations; + break; + case S_IFDIR: // directory + inode->i_op = &yaffs_dir_inode_operations; + inode->i_fop = &yaffs_dir_operations; + break; + case S_IFLNK: // symlink + inode->i_op = &yaffs_symlink_inode_operations; + break; + } + + + inode->u.generic_ip = obj; + obj->myInode = inode; + + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_FileInode invalid parameters\n")); + } + +} + +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,yaffs_Object *obj) +{ + struct inode * inode; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId)); + + inode = iget(sb,obj->objectId); + + // NB Side effect: iget calls back to yaffs_read_inode(). + // iget also increments the inode's i_count + + return inode; +} + +static ssize_t yaffs_file_read(struct file *f, char *buf, size_t n, loff_t *pos) +{ + yaffs_Object *obj; + int nRead,ipos; + struct inode *inode; + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read\n")); + + obj = yaffs_DentryToObject(f->f_dentry); + + dev = obj->myDev; + + yaffs_GrossLock(dev); + + inode = f->f_dentry->d_inode; + + if (*pos < inode->i_size) + { + if (*pos + n > inode->i_size) + { + n = inode->i_size - *pos; + } + } + else + { + n = 0; + } + + nRead = yaffs_ReadDataFromFile(obj,buf,*pos,n); + if(nRead > 0) + { + f->f_pos += nRead; + } + + yaffs_GrossUnlock(dev); + + ipos = *pos; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_read read %d bytes, %d read at %d\n",n,nRead,ipos)); + return nRead; + +} + + +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n, loff_t *pos) +{ + yaffs_Object *obj; + int nWritten,ipos; + struct inode *inode; + yaffs_Device *dev; + + + obj = yaffs_DentryToObject(f->f_dentry); + + dev = obj->myDev; + + yaffs_GrossLock(dev); + + inode = f->f_dentry->d_inode; + + if(!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) + { + ipos = inode->i_size; + } + else + { + ipos = *pos; + } + + + if(!obj) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write: hey obj is null!\n")); + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write about to write writing %d bytes to object %d at %d\n",n,obj->objectId,ipos)); + } + + nWritten = yaffs_WriteDataToFile(obj,buf,ipos,n); + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos)); + if(nWritten > 0) + { + ipos += nWritten; + *pos = ipos; + if(ipos > inode->i_size) + { + inode->i_size = ipos; + inode->i_blocks = (ipos + 511)>>9; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_file_write size updated to %d bytes, %d blocks\n",ipos,(int)(inode->i_blocks))); + } + + } + yaffs_GrossUnlock(dev); + + return nWritten != n ? -ENOSPC : nWritten; +} + + +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) +{ + yaffs_Object *obj; + yaffs_Device *dev; + struct inode *inode = f->f_dentry->d_inode; + unsigned long offset, curoffs; + struct list_head *i; + yaffs_Object *l; + + char name[YAFFS_MAX_NAME_LENGTH +1]; + + obj = yaffs_DentryToObject(f->f_dentry); + dev = obj->myDev; + + yaffs_GrossLock(dev); + + offset = f->f_pos; + + T(YAFFS_TRACE_OS,("yaffs_readdir: starting at %d\n",(int)offset)); + + if(offset == 0) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry . ino %d \n",(int)inode->i_ino)); + if(filldir(dirent,".",1,offset,inode->i_ino,DT_DIR) < 0) + { + goto out; + } + offset++; + f->f_pos++; + } + if(offset == 1) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: entry .. ino %d \n",(int)f->f_dentry->d_parent->d_inode->i_ino)); + if(filldir(dirent,"..",2,offset,f->f_dentry->d_parent->d_inode->i_ino,DT_DIR) < 0) + { + goto out; + } + offset++; + f->f_pos++; + } + + curoffs = 1; + + list_for_each(i,&obj->variant.directoryVariant.children) + { + curoffs++; + if(curoffs >= offset) + { + l = list_entry(i, yaffs_Object,siblings); + + yaffs_GetObjectName(l,name,YAFFS_MAX_NAME_LENGTH+1); + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_readdir: %s inode %d\n",name,yaffs_GetObjectInode(l))); + + if(filldir(dirent, + name, + strlen(name), + offset, + yaffs_GetObjectInode(l), + yaffs_GetObjectType(l)) + < 0) + { + goto up_and_out; + } + + offset++; + f->f_pos++; + } + } + + up_and_out: + out: + + yaffs_GrossUnlock(dev); + + return 0; +} + + +/* + * File creation. Allocate an inode, and we're done.. + */ +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t rdev) +#else +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int rdev) +#endif +{ + struct inode *inode; + + yaffs_Object *obj = NULL; + yaffs_Device *dev; + + yaffs_Object *parent = yaffs_InodeToObject(dir); + + int error = -ENOSPC; + + if(parent) + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: parent object %d type %d\n", + parent->objectId,parent->variantType)); + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: could not get parent object\n")); + return -EPERM; + } + + T(YAFFS_TRACE_OS,("yaffs_mknod: making oject for %s, mode %x dev %x\n", + dentry->d_name.name, mode,rdev)); + + dev = parent->myDev; + + yaffs_GrossLock(dev); + + switch (mode & S_IFMT) + { + default: + // Special (socket, fifo, device...) + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making special\n")); + obj = yaffs_MknodSpecial(parent,dentry->d_name.name,mode,current->uid, current->gid,rdev); + break; + case S_IFREG: // file + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n")); + obj = yaffs_MknodFile(parent,dentry->d_name.name,mode,current->uid, current->gid); + break; + case S_IFDIR: // directory + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making directory\n")); + obj = yaffs_MknodDirectory(parent,dentry->d_name.name,mode,current->uid, current->gid); + break; + case S_IFLNK: // symlink + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod: making file\n")); + obj = NULL; // Do we ever get here? + break; + } + + if(obj) + { + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj); + d_instantiate(dentry, inode); + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count))); + error = 0; + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mknod failed making object\n")); + error = -ENOMEM; + } + + yaffs_GrossUnlock(dev); + + return error; +} + +static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode) +{ + int retVal; + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_mkdir\n")); + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0); +#if 0 + // attempt to fix dir bug - didn't work + if(!retVal) + { + dget(dentry); + } +#endif + return retVal; +} + +//#if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */ +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *n) +#else +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode) +#endif +{ + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_create\n")); + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0); +} + + +static int yaffs_unlink(struct inode * dir, struct dentry *dentry) +{ + int retVal; + + yaffs_Device *dev; + + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_unlink %d:%s\n",(int)(dir->i_ino),dentry->d_name.name)); + + dev = yaffs_InodeToObject(dir)->myDev; + + yaffs_GrossLock(dev); + + + retVal = yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name); + + + yaffs_GrossUnlock(dev); + + if( retVal == YAFFS_OK) + { + dentry->d_inode->i_nlink--; + mark_inode_dirty(dentry->d_inode); + return 0; + } + else + { + return -ENOTEMPTY; + } +} + + +/* + * Create a link... + */ +static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry) +{ + struct inode *inode = old_dentry->d_inode; + yaffs_Object *obj = NULL; + yaffs_Object *link=NULL; + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link\n")); + + obj = yaffs_InodeToObject(inode); + dev = obj->myDev; + + yaffs_GrossLock(dev); + + if (!S_ISDIR(inode->i_mode)) // Don't link directories + { + link = yaffs_Link(yaffs_InodeToObject(dir),dentry->d_name.name,obj); + } + + + if(link) + { + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj); + d_instantiate(dentry, old_dentry->d_inode); + atomic_inc(&old_dentry->d_inode->i_count); + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_link link count %d i_count %d\n", + old_dentry->d_inode->i_nlink,atomic_read(&old_dentry->d_inode->i_count))); + + } + + yaffs_GrossUnlock(dev); + + + if(link) + { + + return 0; + } + + + return -EPERM; +} + + +static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname) +{ + yaffs_Object *obj; + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_symlink\n")); + + dev = yaffs_InodeToObject(dir)->myDev; + yaffs_GrossLock(dev); + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name, + S_IFLNK | S_IRWXUGO, current->uid, current->gid, + symname); + yaffs_GrossUnlock(dev); + + if(obj) + { + + struct inode* inode; + + inode = yaffs_get_inode(dir->i_sb, obj->st_mode, 0, obj); + d_instantiate(dentry, inode); + T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink created OK\n")); + return 0; + } + else + { + T(YAFFS_TRACE_OS,(KERN_DEBUG"symlink not created\n")); + + } + + return -ENOMEM; +} + +static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync) +{ + + yaffs_Object *obj; + yaffs_Device *dev; + + obj = yaffs_DentryToObject(dentry); + + dev = obj->myDev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_sync_object\n")); + yaffs_GrossLock(dev); + yaffs_FlushFile(obj,1); + yaffs_GrossUnlock(dev); + return 0; +} + +/* + * The VFS layer already does all the dentry stuff for rename. + * + * NB: POSIX says you can rename an object over an old object of the same name + */ +static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry) +{ + yaffs_Device *dev; + int retVal = YAFFS_FAIL; + int removed = 0; + yaffs_Object *target; + + dev = yaffs_InodeToObject(old_dir)->myDev; + + yaffs_GrossLock(dev); + + // Check if the target is an existing directory that is not empty. + target = yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),new_dentry->d_name.name); + + if(target && + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY && + !list_empty(&target->variant.directoryVariant.children)) + { + retVal = YAFFS_FAIL; + } + else + { + + // Unlink the target if it exists + removed = yaffs_Unlink(yaffs_InodeToObject(new_dir),new_dentry->d_name.name); + + + retVal = yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name, + yaffs_InodeToObject(new_dir),new_dentry->d_name.name); + + } + yaffs_GrossUnlock(dev); + + if(retVal == YAFFS_OK) + { + if(removed == YAFFS_OK) + { + new_dentry->d_inode->i_nlink--; + mark_inode_dirty(new_dentry->d_inode); + } + + return 0; + } + else + { + return -ENOTEMPTY; + } + + +} + +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) +{ + struct inode *inode = dentry->d_inode; + int error; + yaffs_Device *dev; + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_setattr of object %d\n",yaffs_InodeToObject(inode)->objectId)); + + if((error = inode_change_ok(inode,attr)) == 0) + { + + dev = yaffs_InodeToObject(inode)->myDev; + yaffs_GrossLock(dev); + if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK) + { + error = 0; + } + else + { + error = -EPERM; + } + yaffs_GrossUnlock(dev); + inode_setattr(inode,attr); + } + return error; +} + +//#if defined(CONFIG_KERNEL_2_5) /* Added NCB 185-8-2003 */ +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf) +#else +static int yaffs_statfs(struct super_block *sb, struct statfs *buf) +#endif +{ + yaffs_Device *dev = yaffs_SuperToDevice(sb); + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_statfs\n")); + + yaffs_GrossLock(dev); + + buf->f_type = YAFFS_MAGIC; + buf->f_bsize = sb->s_blocksize; + buf->f_namelen = 255; + buf->f_blocks = (dev->endBlock - dev->startBlock + 1) * YAFFS_CHUNKS_PER_BLOCK/ + (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK); + buf->f_files = 0; + buf->f_ffree = 0; + buf->f_bfree = yaffs_GetNumberOfFreeChunks(dev)/ + (sb->s_blocksize/YAFFS_BYTES_PER_CHUNK); + buf->f_bavail = buf->f_bfree; + + yaffs_GrossUnlock(dev); + return 0; +} + +static void yaffs_read_inode (struct inode *inode) +{ + // NB This is called as a side effect of other functions and + // thus gross locking should always be in place already. + + yaffs_Object *obj ; + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb); + + T(YAFFS_TRACE_OS,(KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino)); + + obj = yaffs_FindObjectByNumber(dev,inode->i_ino); + + yaffs_FillInodeFromObject(inode,obj); + +} + + + +static yaffs_Device *yaffs_dev; +static yaffs_Device *yaffsram_dev; + + + +static void yaffs_put_super(struct super_block *sb) +{ + yaffs_Device *dev = yaffs_SuperToDevice(sb); + + yaffs_GrossLock(dev); + if(dev->putSuperFunc) + { + dev->putSuperFunc(sb); + } + yaffs_Deinitialise(dev); + yaffs_GrossUnlock(dev); + + if(dev == yaffs_dev) yaffs_dev = NULL; + if(dev == yaffsram_dev) yaffsram_dev = NULL; + + kfree(dev); +} + + +#ifdef CONFIG_YAFFS_MTD_ENABLED + +static void yaffs_MTDPutSuper(struct super_block *sb) +{ + + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice; + + if(mtd->sync) + { + mtd->sync(mtd); + } + + put_mtd_device(mtd); +} + +#endif + + +static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent) +{ + int nBlocks; + struct inode * inode; + struct dentry * root; + yaffs_Device *dev; + + sb->s_magic = YAFFS_MAGIC; + sb->s_op = &yaffs_super_ops; + + if(!sb) + printk(KERN_INFO"yaffs: sb is NULL\n"); + else if(!sb->s_dev) + printk(KERN_INFO"yaffs: sb->s_dev is NULL\n"); + else if(! kdevname(sb->s_dev)) + printk(KERN_INFO"yaffs: kdevname is NULL\n"); + else + printk(KERN_INFO"yaffs: dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev)); + + + +#ifdef CONFIG_YAFFS_USE_CHUNK_SIZE + sb->s_blocksize = YAFFS_BYTES_PER_CHUNK; + sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT; +#else + sb->s_blocksize = PAGE_CACHE_SIZE; + sb->s_blocksize_bits = PAGE_CACHE_SHIFT; +#endif + T(YAFFS_TRACE_OS,("yaffs_read_super: %s block size %d\n", useRam ? "RAM" : "MTD",(int)(sb->s_blocksize))); + +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY + T(YAFFS_TRACE_OS,("yaffs: Write verification disabled. All guarantees null and void\n")); +#endif + + + + if(useRam) + { + +#ifdef CONFIG_YAFFS_RAM_ENABLED + // Set the yaffs_Device up for ram emulation + + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL); + if(!dev) + { + // Deep shit could not allocate device structure + printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n"); + return NULL; + } + + memset(dev,0,sizeof(yaffs_Device)); + dev->genericDevice = NULL; // Not used for RAM emulation. + + nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + dev->startBlock = 1; // Don't use block 0 + dev->endBlock = nBlocks - 1; + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK; + dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK; + dev->nReservedBlocks = 5; + + dev->writeChunkToNAND = nandemul_WriteChunkToNAND; + dev->readChunkFromNAND = nandemul_ReadChunkFromNAND; + dev->eraseBlockInNAND = nandemul_EraseBlockInNAND; + dev->initialiseNAND = nandemul_InitialiseNAND; + + yaffsram_dev = dev; + +#endif + + } + else + { +#ifdef CONFIG_YAFFS_MTD_ENABLED + struct mtd_info *mtd; + + printk(KERN_DEBUG "yaffs: Attempting MTD mount on %u.%u, \"%s\"\n", + MAJOR(sb->s_dev),MINOR(sb->s_dev),kdevname(sb->s_dev)); + + // Check it's an mtd device..... + if(MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) + { + return NULL; // This isn't an mtd device + } + + // Get the device + mtd = get_mtd_device(NULL, MINOR(sb->s_dev)); + if (!mtd) + { + printk(KERN_DEBUG "yaffs: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)); + return NULL; + } + + // Check it's NAND + if(mtd->type != MTD_NANDFLASH) + { + printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type); + return NULL; + } + + //printk(KERN_DEBUG" erase %x\n",mtd->erase); + //printk(KERN_DEBUG" read %x\n",mtd->read); + //printk(KERN_DEBUG" write %x\n",mtd->write); + //printk(KERN_DEBUG" readoob %x\n",mtd->read_oob); + //printk(KERN_DEBUG" writeoob %x\n",mtd->write_oob); + //printk(KERN_DEBUG" oobblock %x\n",mtd->oobblock); + //printk(KERN_DEBUG" oobsize %x\n",mtd->oobsize); + + + if(!mtd->erase || + !mtd->read || + !mtd->write || +#ifndef CONFIG_YAFFS_USE_OLD_MTD + !mtd->write_ecc || + !mtd->read_ecc || +#endif + !mtd->read_oob || + !mtd->write_oob ) + { + printk(KERN_DEBUG "yaffs: MTD device does not support required functions\n"); + return NULL; + } + + if(mtd->oobblock != YAFFS_BYTES_PER_CHUNK || + mtd->oobsize != YAFFS_BYTES_PER_SPARE) + { + printk(KERN_DEBUG "yaffs: MTD device does not support have the right page sizes\n"); + return NULL; + } + + + // OK, so if we got here, we have an MTD that's NAND and looks + // like it has the right capabilities + // Set the yaffs_Device up for ram emulation + +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL); +#else + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device),GFP_KERNEL); +#endif + if(!dev) + { + // Deep shit could not allocate device structure + printk(KERN_DEBUG"yaffs_read_super: Failed trying to allocate yaffs_Device. Terminating debug.\n"); + return NULL; + } + + memset(dev,0,sizeof(yaffs_Device)); + dev->genericDevice = mtd; + + // Set up the memory size parameters.... + + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + dev->startBlock = 1; // Don't use block 0 + dev->endBlock = nBlocks - 1; + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK; + dev->nBytesPerChunk = YAFFS_BYTES_PER_CHUNK; + dev->nReservedBlocks = 5; + dev->nShortOpCaches = 10; // Enable short op caching + + + // ... and the functions. + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND; + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND; + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND; + dev->initialiseNAND = nandmtd_InitialiseNAND; + + dev->putSuperFunc = yaffs_MTDPutSuper; + +#ifdef CONFIG_YAFFS_USE_NANDECC + dev->useNANDECC = 1; +#endif + + yaffs_dev = dev; + +#endif + } + + init_MUTEX(&dev->grossLock); + + + yaffs_GrossLock(dev); + yaffs_GutsInitialise(yaffs_SuperToDevice(sb)); + + T(YAFFS_TRACE_OS,("yaffs_read_super: guts initialised\n")); + + // Create root inode + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb))); + + yaffs_GrossUnlock(dev); + + if (!inode) + return NULL; + +// added NCB + inode->i_op = & yaffs_dir_inode_operations; + inode->i_fop = & yaffs_dir_operations; + + T(YAFFS_TRACE_OS,("yaffs_read_super: got root inode\n")); + + + root = d_alloc_root(inode); + + T(YAFFS_TRACE_OS,("yaffs_read_super: d_alloc_root done\n")); + + if (!root) { + iput(inode); + return NULL; + } + sb->s_root = root; + + T(YAFFS_TRACE_OS,("yaffs_read_super: done\n")); + return sb; +} + +static int yaffs_internal_read_super_ram(struct super_block * sb, void * data, int silent) +{ + return yaffs_internal_read_super(1,sb,data,silent) ? 0 : -1; +} +static int yaffs_internal_read_super_mtd(struct super_block * sb, void * data, int silent) +{ + return yaffs_internal_read_super(0,sb,data,silent) ? 0 : -1; +} + + +#ifdef CONFIG_YAFFS_MTD_ENABLED +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static struct super_block *yaffs_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data) +{ + + return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_mtd); +} + +/* changes NCB 2.5.70 */ +//static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV); +static struct file_system_type yaffs_fs_type = { + .owner = THIS_MODULE, + .name = "yaffs", + .get_sb = yaffs_read_super, +// .kill_sb = kill_block_super, + .kill_sb = kill_litter_super, + .fs_flags = FS_REQUIRES_DEV, +}; +#else +static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent) +{ + return yaffs_internal_read_super(0,sb,data,silent); +} + +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV); +#endif +#endif + +#ifdef CONFIG_YAFFS_RAM_ENABLED + +//#if defined(CONFIG_KERNEL_2_5) +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0)) +static struct super_block *yaffs_ram_read_super(struct file_system_type * fs, int flags, const char *dev_name, void *data) +{ + + return get_sb_bdev(fs, flags, dev_name, data, yaffs_internal_read_super_ram); +} + +/* changes NCB 2.5.70 */ +//static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super, FS_REQUIRES_DEV); +static struct file_system_type yaffs_fs_type = { + .owner = THIS_MODULE, + .name = "yaffsram", + .get_sb = yaffs_ram_read_super, +// .kill_sb = kill_block_super, + .kill_sb = kill_litter_super, + .fs_flags = FS_SINGLE, +}; +#else +static struct super_block *yaffs_ram_read_super(struct super_block * sb, void * data, int silent) +{ + return yaffs_internal_read_super(1,sb,data,silent); +} + +static DECLARE_FSTYPE(yaffs_ram_fs_type, "yaffsram", yaffs_ram_read_super, FS_SINGLE); +#endif +#endif // CONFIG_YAFFS_RAM_ENABLED + + +static struct proc_dir_entry *my_proc_entry; +static struct proc_dir_entry *my_proc_ram_write_entry; + +static char * yaffs_dump_dev(char *buf,yaffs_Device *dev,char *name) +{ + buf +=sprintf(buf,"\nDevice %s\n",name); + buf +=sprintf(buf,"startBlock......... %d\n",dev->startBlock); + buf +=sprintf(buf,"endBlock........... %d\n",dev->endBlock); + buf +=sprintf(buf,"chunkGroupBits..... %d\n",dev->chunkGroupBits); + buf +=sprintf(buf,"chunkGroupSize..... %d\n",dev->chunkGroupSize); + buf +=sprintf(buf,"nErasedBlocks...... %d\n",dev->nErasedBlocks); + buf +=sprintf(buf,"nTnodesCreated..... %d\n",dev->nTnodesCreated); + buf +=sprintf(buf,"nFreeTnodes........ %d\n",dev->nFreeTnodes); + buf +=sprintf(buf,"nObjectsCreated.... %d\n",dev->nObjectsCreated); + buf +=sprintf(buf,"nFreeObjects....... %d\n",dev->nFreeObjects); + buf +=sprintf(buf,"nFreeChunks........ %d\n",dev->nFreeChunks); + buf +=sprintf(buf,"nPageWrites........ %d\n",dev->nPageWrites); + buf +=sprintf(buf,"nPageReads......... %d\n",dev->nPageReads); + buf +=sprintf(buf,"nBlockErasures..... %d\n",dev->nBlockErasures); + buf +=sprintf(buf,"nGCCopies.......... %d\n",dev->nGCCopies); + buf +=sprintf(buf,"garbageCollections. %d\n",dev->garbageCollections); + buf +=sprintf(buf,"passiveGCs......... %d\n",dev->passiveGarbageCollections); + buf +=sprintf(buf,"nRetriedWrites..... %d\n",dev->nRetriedWrites); + buf +=sprintf(buf,"nRetireBlocks...... %d\n",dev->nRetiredBlocks); + buf +=sprintf(buf,"eccFixed........... %d\n",dev->eccFixed); + buf +=sprintf(buf,"eccUnfixed......... %d\n",dev->eccUnfixed); + buf +=sprintf(buf,"tagsEccFixed....... %d\n",dev->tagsEccFixed); + buf +=sprintf(buf,"tagsEccUnfixed..... %d\n",dev->tagsEccUnfixed); + buf +=sprintf(buf,"cacheHits.......... %d\n",dev->cacheHits); + buf +=sprintf(buf,"nDeletedFiles...... %d\n",dev->nDeletedFiles); + buf +=sprintf(buf,"nUnlinkedFiles..... %d\n",dev->nUnlinkedFiles); + buf +=sprintf(buf,"nBackgroudDeletions %d\n",dev->nBackgroundDeletions); + buf +=sprintf(buf,"useNANDECC......... %d\n",dev->useNANDECC); + + + return buf; +} + +static int yaffs_proc_read( + char *page, + char **start, + off_t offset, + int count, + int *eof, + void *data + ) +{ + + char my_buffer[3000]; + char *buf; + buf = my_buffer; + + if (offset > 0) return 0; + + /* Fill the buffer and get its length */ + buf +=sprintf(buf,"YAFFS built:"__DATE__ " "__TIME__"\n%s\n%s\n", yaffs_fs_c_version,yaffs_guts_c_version); + + if(yaffs_dev) buf = yaffs_dump_dev(buf,yaffs_dev,"yaffs"); + if(yaffsram_dev) buf = yaffs_dump_dev(buf,yaffsram_dev,"yaffsram"); + + + strcpy(page,my_buffer); + return strlen(my_buffer); +} + + +static int yaffs_proc_ram_write( + char *page, + char **start, + off_t offset, + int count, + int *eof, + void *data + ) +{ + + printk(KERN_DEBUG "yaffs write size %d\n",count); + return count; +} + +static int __init init_yaffs_fs(void) +{ + int error = 0; + + yaffs_dev = yaffsram_dev = NULL; + + printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n"); +#ifdef CONFIG_YAFFS_USE_GENERIC_RW + printk(KERN_DEBUG "yaffs is using generic read/write (caching)\n"); +#else + printk(KERN_DEBUG "yaffs is using direct read/write (uncached)\n"); +#endif + + + /* Install the proc_fs entry */ + my_proc_entry = create_proc_read_entry("yaffs", + S_IRUGO | S_IFREG, + &proc_root, + yaffs_proc_read, + NULL); + if(!my_proc_entry) + { + return -ENOMEM; + } + +#ifdef CONFIG_YAFFS_RAM_ENABLED +#if 0 + my_proc_ram_write_entry = create_proc_entry("yaffs_ram", + S_IRUGO | S_IFREG, + &proc_root); + + if(!my_proc_ram_write_entry) + { + return -ENOMEM; + } + else + { + my_proc_ram_write_entry->write_proc = yaffs_proc_ram_write; + } +#endif + + error = register_filesystem(&yaffs_ram_fs_type); + if(error) + { + return error; + } +#endif //CONFIG_YAFFS_RAM_ENABLED + +#ifdef CONFIG_YAFFS_MTD_ENABLED + error = register_filesystem(&yaffs_fs_type); + if(error) + { +#ifdef CONFIG_YAFFS_RAM_ENABLED + unregister_filesystem(&yaffs_ram_fs_type); +#endif //CONFIG_YAFFS_RAM_ENABLED + } +#endif // CONFIG_YAFFS_MTD_ENABLED + + return error; +} + +static void __exit exit_yaffs_fs(void) +{ + printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Clean up\n"); + + remove_proc_entry("yaffs",&proc_root); + +#ifdef CONFIG_YAFFS_RAM_ENABLED + unregister_filesystem(&yaffs_ram_fs_type); +#endif +#ifdef CONFIG_YAFFS_MTD_ENABLED + unregister_filesystem(&yaffs_fs_type); +#endif + +} + +module_init(init_yaffs_fs) +module_exit(exit_yaffs_fs) + +MODULE_DESCRIPTION("YAFFS - a NAND specific flash file system"); +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002"); +MODULE_LICENSE("GPL"); + Index: linux-2.6.9/fs/yaffs/yaffs_guts.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_guts.h @@ -0,0 +1,593 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_guts.h: Configuration etc for yaffs_guts + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + * $Id: yaffs_guts.h,v 1.18 2004/10/20 20:12:43 charles Exp $ + */ + +#ifndef __YAFFS_GUTS_H__ +#define __YAFFS_GUTS_H__ + +#include "yportenv.h" + +#define YAFFS_OK 1 +#define YAFFS_FAIL 0 + +// Give us a Y=0x59, +// Give us an A=0x41, +// Give us an FF=0xFF +// Give us an S=0x53 +// And what have we got... +#define YAFFS_MAGIC 0x5941FF53 + +#define YAFFS_NTNODES_LEVEL0 16 +#define YAFFS_TNODES_LEVEL0_BITS 4 +#define YAFFS_TNODES_LEVEL0_MASK 0xf + +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2) +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1) +#define YAFFS_TNODES_INTERNAL_MASK 0x7 +#define YAFFS_TNODES_MAX_LEVEL 6 + +#define YAFFS_BYTES_PER_SPARE 16 + +#define YAFFS_BYTES_PER_CHUNK 512 +//#define YAFFS_CHUNK_SIZE_SHIFT 9 + + +#define YAFFS_CHUNKS_PER_BLOCK 32 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK) + +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF + +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF + +#define YAFFS_ALLOCATION_NOBJECTS 100 +#define YAFFS_ALLOCATION_NTNODES 100 +#define YAFFS_ALLOCATION_NLINKS 100 + +#define YAFFS_NOBJECT_BUCKETS 256 + + +#define YAFFS_OBJECT_SPACE 0x40000 +#define YAFFS_MAX_NAME_LENGTH 255 +#define YAFFS_SHORT_NAME_LENGTH 15 + +#define YAFFS_MAX_ALIAS_LENGTH 159 + +#define YAFFS_OBJECTID_ROOT 1 +#define YAFFS_OBJECTID_LOSTNFOUND 2 +#define YAFFS_OBJECTID_UNLINKED 3 + +#define YAFFS_MAX_SHORT_OP_CACHES 20 + + +// ChunkCache is used for short read/write operations. +typedef struct +{ + struct yaffs_ObjectStruct *object; + int chunkId; + int lastUse; + int dirty; + int nBytes; // Only valid if the cache is dirty + __u8 data[YAFFS_BYTES_PER_CHUNK]; +} yaffs_ChunkCache; + +// Tags structures in RAM +// NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise +// the structure size will get blown out. + +typedef struct +{ + unsigned chunkId:20; + unsigned serialNumber:2; + unsigned byteCount:10; + unsigned objectId:18; + unsigned ecc:12; + unsigned unusedStuff:2; +} yaffs_Tags; + +typedef union +{ + yaffs_Tags asTags; + __u8 asBytes[8]; +} yaffs_TagsUnion; + + +// Spare structure +typedef struct +{ + __u8 tagByte0; + __u8 tagByte1; + __u8 tagByte2; + __u8 tagByte3; + __u8 pageStatus; // set to 0 to delete the chunk + __u8 blockStatus; + __u8 tagByte4; + __u8 tagByte5; + __u8 ecc1[3]; + __u8 tagByte6; + __u8 tagByte7; + __u8 ecc2[3]; +} yaffs_Spare; + +//Special structure for passing through to mtd +struct yaffs_NANDSpare { + yaffs_Spare spare; + int eccres1; + int eccres2; +}; + +// Block data in RAM + +typedef enum { + YAFFS_BLOCK_STATE_UNKNOWN = 0, + YAFFS_BLOCK_STATE_SCANNING, // Used while the block is being scanned. + // NB Don't erase blocks while they're being scanned + + YAFFS_BLOCK_STATE_EMPTY, // This block is empty + + YAFFS_BLOCK_STATE_ALLOCATING, // This block is partially allocated. + // This is the one currently being used for page + // allocation. Should never be more than one of these + + + YAFFS_BLOCK_STATE_FULL, // All the pages in this block have been allocated. + // At least one page holds valid data. + + YAFFS_BLOCK_STATE_DIRTY, // All pages have been allocated and deleted. + // Erase me, reuse me. + + YAFFS_BLOCK_STATE_DEAD // This block has failed and is not in use + +} yaffs_BlockState; + + + + +typedef struct +{ +#ifndef CONFIG_YAFFS_NO_YAFFS2 + __u32 sequenceNumber; // block sequence number for yaffs2 +#endif + int softDeletions:8; // number of soft deleted pages + int pagesInUse:8; // number of pages in use + __u32 blockState:4; // One of the above block states + __u32 needsRetiring:1; // Data has failed on this block, need to get valid data off + // and retire the block. +} yaffs_BlockInfo; + + +//////////////////// Object structure /////////////////////////// +// This is the object structure as stored on NAND + +typedef enum +{ + YAFFS_OBJECT_TYPE_UNKNOWN, + YAFFS_OBJECT_TYPE_FILE, + YAFFS_OBJECT_TYPE_SYMLINK, + YAFFS_OBJECT_TYPE_DIRECTORY, + YAFFS_OBJECT_TYPE_HARDLINK, + YAFFS_OBJECT_TYPE_SPECIAL +} yaffs_ObjectType; + +typedef struct +{ + yaffs_ObjectType type; + + // Apply to everything + int parentObjectId; + __u16 sum__NoLongerUsed; // checksum of name. Calc this off the name to prevent inconsistencies + char name[YAFFS_MAX_NAME_LENGTH + 1]; + + // Thes following apply to directories, files, symlinks - not hard links + __u32 st_mode; // protection + +#ifdef CONFIG_YAFFS_WINCE + __u32 notForWinCE[5]; +#else + __u32 st_uid; // user ID of owner + __u32 st_gid; // group ID of owner + __u32 st_atime; // time of last access + __u32 st_mtime; // time of last modification + __u32 st_ctime; // time of last change +#endif + + // File size applies to files only + int fileSize; + + // Equivalent object id applies to hard links only. + int equivalentObjectId; + + // Alias is for symlinks only. + char alias[YAFFS_MAX_ALIAS_LENGTH + 1]; + + __u32 st_rdev; // device stuff for block and char devices (maj/min) + +#ifdef CONFIG_YAFFS_WINCE + __u32 win_ctime[2]; + __u32 win_atime[2]; + __u32 win_mtime[2]; + __u32 roomToGrow[6]; +#else + __u32 roomToGrow[12]; +#endif + +} yaffs_ObjectHeader; + + + +//////////////////// Tnode /////////////////////////// + +union yaffs_Tnode_union +{ +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL+1]; +#else + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL]; +#endif + __u16 level0[YAFFS_NTNODES_LEVEL0]; + +}; + +typedef union yaffs_Tnode_union yaffs_Tnode; + +struct yaffs_TnodeList_struct +{ + struct yaffs_TnodeList_struct *next; + yaffs_Tnode *tnodes; +}; + +typedef struct yaffs_TnodeList_struct yaffs_TnodeList; + + + +/////////////////// Object //////////////////////////////// +// An object can be one of: +// - a directory (no data, has children links +// - a regular file (data.... not prunes :->). +// - a symlink [symbolic link] (the alias). +// - a hard link + + +typedef struct +{ + __u32 fileSize; + __u32 scannedFileSize; + int topLevel; + yaffs_Tnode *top; +} yaffs_FileStructure; + +typedef struct +{ + struct list_head children; // list of child links +} yaffs_DirectoryStructure; + +typedef struct +{ + char *alias; +} yaffs_SymLinkStructure; + +typedef struct +{ + struct yaffs_ObjectStruct *equivalentObject; + __u32 equivalentObjectId; +} yaffs_HardLinkStructure; + +typedef union +{ + yaffs_FileStructure fileVariant; + yaffs_DirectoryStructure directoryVariant; + yaffs_SymLinkStructure symLinkVariant; + yaffs_HardLinkStructure hardLinkVariant; +} yaffs_ObjectVariant; + + +struct yaffs_ObjectStruct +{ + __u8 deleted: 1; // This should only apply to unlinked files. + __u8 softDeleted: 1; // it has also been soft deleted + __u8 unlinked: 1; // An unlinked file. The file should be in the unlinked pseudo directory. + __u8 fake:1; // A fake object has no presence on NAND. + __u8 renameAllowed:1; + __u8 unlinkAllowed:1; + __u8 dirty:1; // the object needs to be written to flash + __u8 valid:1; // When the file system is being loaded up, this + // object might be created before the data + // is available (ie. file data records appear before the header). + __u8 serial; // serial number of chunk in NAND. Store here so we don't have to + // read back the old one to update. + __u16 sum; // sum of the name to speed searching + + struct yaffs_DeviceStruct *myDev; // The device I'm on + + + struct list_head hashLink; // list of objects in this hash bucket + + + struct list_head hardLinks; // all the equivalent hard linked objects + // live on this list + // directory structure stuff + struct yaffs_ObjectStruct *parent; //my parent directory + struct list_head siblings; // siblings in a directory + // also used for linking up the free list + + // Where's my data in NAND? + int chunkId; // where it lives + + int nDataChunks; + + __u32 objectId; // the object id value + + + __u32 st_mode; // protection + +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM + char shortName[YAFFS_SHORT_NAME_LENGTH+1]; +#endif + +#ifndef __KERNEL__ + __u32 inUse; +#endif + +#ifdef CONFIG_YAFFS_WINCE + __u32 win_ctime[2]; + __u32 win_mtime[2]; + __u32 win_atime[2]; +#else + __u32 st_uid; // user ID of owner + __u32 st_gid; // group ID of owner + __u32 st_atime; // time of last access + __u32 st_mtime; // time of last modification + __u32 st_ctime; // time of last change +#endif + + __u32 st_rdev; // device stuff for block and char devices + + + +#ifdef __KERNEL__ + struct inode *myInode; + __u8 deferedFree; // YAFFS has removed the object from NAND, but it is being kept + // Alive until the inode is cleared to prevent inode inconsistencies. +#endif + + + + yaffs_ObjectType variantType; + + yaffs_ObjectVariant variant; + +}; + + + +typedef struct yaffs_ObjectStruct yaffs_Object; + + +struct yaffs_ObjectList_struct +{ + yaffs_Object *objects; + struct yaffs_ObjectList_struct *next; +}; + +typedef struct yaffs_ObjectList_struct yaffs_ObjectList; + +typedef struct +{ + struct list_head list; + int count; +} yaffs_ObjectBucket; + + +//////////////////// Device //////////////////////////////// + +struct yaffs_DeviceStruct +{ + // Entry parameters set up way early. Yaffs sets up the rest. + int nBytesPerChunk; // Should be a power of 2 >= 512 + int nChunksPerBlock; // does not need to be a power of 2 + int startBlock; // Start block we're allowed to use + int endBlock; // End block we're allowed to use + int nReservedBlocks; // We want this tuneable so that we can reduce + // reserved blocks on NOR and RAM. + + int useNANDECC; // Flag to decide whether or not to use NANDECC + int nShortOpCaches; // If <= 0, then short op caching is disabled, else + // the number of short op caches (don't use too many). + + + void *genericDevice; // Pointer to device context + // On an mtd this holds the mtd pointer. + + // NAND access functions (Must be set before calling YAFFS) + + int (*writeChunkToNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, const __u8 *data, yaffs_Spare *spare); + int (*readChunkFromNAND)(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare); + int (*eraseBlockInNAND)(struct yaffs_DeviceStruct *dev,int blockInNAND); + int (*initialiseNAND)(struct yaffs_DeviceStruct *dev); + + // Runtime parameters. Set up by YAFFS. + + __u16 chunkGroupBits; // 0 for devices <= 32MB. else log2(nchunks) - 16 + __u16 chunkGroupSize; // == 2^^chunkGroupBits + +#ifdef __KERNEL__ + + struct semaphore sem;// Semaphore for waiting on erasure. + struct semaphore grossLock; // Gross locking semaphore + +#endif +#ifdef __KERNEL__ + void (*putSuperFunc)(struct super_block *sb); +#endif + + int isMounted; + + // Block Info + yaffs_BlockInfo *blockInfo; + __u8 *chunkBits; // bitmap of chunks in use + int chunkBitmapStride; // Number of bytes of chunkBits per block. + // Must be consistent with nChunksPerBlock. + + + int nErasedBlocks; + int allocationBlock; // Current block being allocated off + __u32 allocationPage; + int allocationBlockFinder; // Used to search for next allocation block + + // Runtime state + int nTnodesCreated; + yaffs_Tnode *freeTnodes; + int nFreeTnodes; + yaffs_TnodeList *allocatedTnodeList; + + + int nObjectsCreated; + yaffs_Object *freeObjects; + int nFreeObjects; + + yaffs_ObjectList *allocatedObjectList; + + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS]; + + int nFreeChunks; + + int currentDirtyChecker; // Used to find current dirtiest block + + + // Operations since mount + int nPageWrites; + int nPageReads; + int nBlockErasures; + int nGCCopies; + int garbageCollections; + int passiveGarbageCollections; + int nRetriedWrites; + int nRetiredBlocks; + int eccFixed; + int eccUnfixed; + int tagsEccFixed; + int tagsEccUnfixed; + int nDeletions; + int nUnmarkedDeletions; + + yaffs_Object *rootDir; + yaffs_Object *lostNFoundDir; + + // Buffer areas for storing data to recover from write failures +// __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK]; +// yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK]; + int bufferedBlock; // Which block is buffered here? + int doingBufferedBlockRewrite; + + yaffs_ChunkCache *srCache; + int srLastUse; + + int cacheHits; + + // Stuff for background deletion and unlinked files. + yaffs_Object *unlinkedDir; // Directory where unlinked and deleted files live. + yaffs_Object *unlinkedDeletion; // Current file being background deleted. + int nDeletedFiles; // Count of files awaiting deletion; + int nUnlinkedFiles; // Count of unlinked files. + int nBackgroundDeletions; // Count of background deletions. + + __u8 *localBuffer; + +}; + +typedef struct yaffs_DeviceStruct yaffs_Device; + + + +//////////// YAFFS Functions ////////////////// + +int yaffs_GutsInitialise(yaffs_Device *dev); +void yaffs_Deinitialise(yaffs_Device *dev); + +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev); + + +// Rename +int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName); + +// generic Object functions +int yaffs_Unlink(yaffs_Object *dir, const char *name); +int yaffs_DeleteFile(yaffs_Object *obj); + +// Object access functions. +int yaffs_GetObjectName(yaffs_Object *obj,char *name,int buffSize); +int yaffs_GetObjectFileLength(yaffs_Object *obj); +int yaffs_GetObjectInode(yaffs_Object *obj); +unsigned yaffs_GetObjectType(yaffs_Object *obj); +int yaffs_GetObjectLinkCount(yaffs_Object *obj); + +// Change inode attributes +int yaffs_SetAttributes(yaffs_Object *obj, struct iattr *attr); +int yaffs_GetAttributes(yaffs_Object *obj, struct iattr *attr); + +// File operations +int yaffs_ReadDataFromFile(yaffs_Object *obj, __u8 *buffer, __u32 offset, int nBytes); +int yaffs_WriteDataToFile(yaffs_Object *obj, const __u8 *buffer, __u32 offset, int nBytes); +int yaffs_ResizeFile(yaffs_Object *obj, int newSize); + +yaffs_Object *yaffs_MknodFile(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid); +int yaffs_FlushFile(yaffs_Object *obj,int updateTime); + + +// Directory operations +yaffs_Object *yaffs_MknodDirectory(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid); +yaffs_Object *yaffs_FindObjectByName(yaffs_Object *theDir,const char *name); +int yaffs_ApplyToDirectoryChildren(yaffs_Object *theDir,int (*fn)(yaffs_Object *)); + +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device *dev,__u32 number); + +// Link operations +yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject); + +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj); + +// Symlink operations +yaffs_Object *yaffs_MknodSymLink(yaffs_Object *parent, const char *name, __u32 mode, __u32 uid, __u32 gid, const char *alias); +char *yaffs_GetSymlinkAlias(yaffs_Object *obj); + +// Special inodes (fifos, sockets and devices) +yaffs_Object *yaffs_MknodSpecial(yaffs_Object *parent,const char *name, __u32 mode, __u32 uid, __u32 gid,__u32 rdev); + + +// Special directories +yaffs_Object *yaffs_Root(yaffs_Device *dev); +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev); + +#ifdef CONFIG_YAFFS_WINCE +// CONFIG_YAFFS_WINCE special stuff +void yfsd_WinFileTimeNow(__u32 target[2]); +#endif + +#ifdef __KERNEL__ +void yaffs_HandleDeferedFree(yaffs_Object *obj); +#endif + +// Debug dump +int yaffs_DumpObject(yaffs_Object *obj); + + +void yaffs_GutsTest(yaffs_Device *dev); + + +#endif + + Index: linux-2.6.9/fs/yaffs/devextras.h =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/devextras.h @@ -0,0 +1,271 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * devextras.h + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License version 2.1 as + * published by the Free Software Foundation. + * + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL. + * + * This file is just holds extra declarations used during development. + * Most of these are from kernel includes placed here so we can use them in + * applications. + * + * $Id: devextras.h,v 1.5 2002/09/27 20:50:50 charles Exp $ + * + */ + +#ifndef __EXTRAS_H__ +#define __EXTRAS_H__ + +#if defined WIN32 +#define __inline__ __inline +#define new newHack +#endif + +#if !(defined __KERNEL__) || (defined WIN32) + +// User space defines + +typedef unsigned char __u8; +typedef unsigned short __u16; +typedef unsigned __u32; + + +/* + * Simple doubly linked list implementation. + * + * Some of the internal functions ("__xxx") are useful when + * manipulating whole lists rather than single entries, as + * sometimes we already know the next/prev entries and we can + * generate better code by using them directly rather than + * using the generic single-entry routines. + */ + + #define prefetch(x) 1 + + +struct list_head { + struct list_head *next, *prev; +}; + +#define LIST_HEAD_INIT(name) { &(name), &(name) } + +#define LIST_HEAD(name) \ + struct list_head name = LIST_HEAD_INIT(name) + +#define INIT_LIST_HEAD(ptr) do { \ + (ptr)->next = (ptr); (ptr)->prev = (ptr); \ +} while (0) + +/* + * Insert a new entry between two known consecutive entries. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static __inline__ void __list_add(struct list_head * new, + struct list_head * prev, + struct list_head * next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/** + * list_add - add a new entry + * @new: new entry to be added + * @head: list head to add it after + * + * Insert a new entry after the specified head. + * This is good for implementing stacks. + */ +static __inline__ void list_add(struct list_head *new, struct list_head *head) +{ + __list_add(new, head, head->next); +} + +/** + * list_add_tail - add a new entry + * @new: new entry to be added + * @head: list head to add it before + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static __inline__ void list_add_tail(struct list_head *new, struct list_head *head) +{ + __list_add(new, head->prev, head); +} + +/* + * Delete a list entry by making the prev/next entries + * point to each other. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static __inline__ void __list_del(struct list_head * prev, + struct list_head * next) +{ + next->prev = prev; + prev->next = next; +} + +/** + * list_del - deletes entry from list. + * @entry: the element to delete from the list. + * Note: list_empty on entry does not return true after this, the entry is in an undefined state. + */ +static __inline__ void list_del(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); +} + +/** + * list_del_init - deletes entry from list and reinitialize it. + * @entry: the element to delete from the list. + */ +static __inline__ void list_del_init(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + INIT_LIST_HEAD(entry); +} + +/** + * list_empty - tests whether a list is empty + * @head: the list to test. + */ +static __inline__ int list_empty(struct list_head *head) +{ + return head->next == head; +} + +/** + * list_splice - join two lists + * @list: the new list to add. + * @head: the place to add it in the first list. + */ +static __inline__ void list_splice(struct list_head *list, struct list_head *head) +{ + struct list_head *first = list->next; + + if (first != list) { + struct list_head *last = list->prev; + struct list_head *at = head->next; + + first->prev = head; + head->next = first; + + last->next = at; + at->prev = last; + } +} + +/** + * list_entry - get the struct for this entry + * @ptr: the &struct list_head pointer. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_struct within the struct. + */ +#define list_entry(ptr, type, member) \ + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member))) + +/** + * list_for_each - iterate over a list + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + */ +#define list_for_each(pos, head) \ + for (pos = (head)->next, prefetch(pos->next); pos != (head); \ + pos = pos->next, prefetch(pos->next)) + +/** + * list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the &struct list_head to use as a loop counter. + * @n: another &struct list_head to use as temporary storage + * @head: the head for your list. + */ +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + + + + +/* + * File types + */ +#define DT_UNKNOWN 0 +#define DT_FIFO 1 +#define DT_CHR 2 +#define DT_DIR 4 +#define DT_BLK 6 +#define DT_REG 8 +#define DT_LNK 10 +#define DT_SOCK 12 +#define DT_WHT 14 + +#ifndef WIN32 +#include +#endif + +/* + * Attribute flags. These should be or-ed together to figure out what + * has been changed! + */ +#define ATTR_MODE 1 +#define ATTR_UID 2 +#define ATTR_GID 4 +#define ATTR_SIZE 8 +#define ATTR_ATIME 16 +#define ATTR_MTIME 32 +#define ATTR_CTIME 64 +#define ATTR_ATIME_SET 128 +#define ATTR_MTIME_SET 256 +#define ATTR_FORCE 512 /* Not a change, but a change it */ +#define ATTR_ATTR_FLAG 1024 + + +struct iattr { + unsigned int ia_valid; + unsigned ia_mode; + unsigned ia_uid; + unsigned ia_gid; + unsigned ia_size; + unsigned ia_atime; + unsigned ia_mtime; + unsigned ia_ctime; + unsigned int ia_attr_flags; +}; + +#define KERN_DEBUG + + +#else + +#ifndef WIN32 +#include +#include +#include +#include +#endif + +#endif + + + +#if defined WIN32 +#undef new +#endif + +#endif + Index: linux-2.6.9/fs/yaffs/yaffs_mtdif.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs_mtdif.c @@ -0,0 +1,167 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * yaffs_mtdif.c NAND mtd wrapper functions. + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +const char *yaffs_mtdif_c_version = "$Id: yaffs_mtdif.c,v 1.10 2004/09/19 08:14:50 charles Exp $"; + +#ifdef CONFIG_YAFFS_MTD_ENABLED + +#include "yportenv.h" + +#include "yaffs_mtdif.h" + +#include "linux/mtd/mtd.h" +#include "linux/types.h" +#include "linux/time.h" + +#ifndef CONFIG_YAFFS_USE_OLD_MTD +#include "linux/mtd/nand.h" +#endif + +struct nand_oobinfo yaffs_oobinfo = { + useecc: 1, +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,8)) +// this is for versions of mtd nand driver in kernel 2.6.8 and later + eccbytes: 6, +#endif + eccpos: {8, 9, 10, 13, 14, 15} +}; + +struct nand_oobinfo yaffs_noeccinfo = { + useecc: 0, +}; + + +int nandmtd_WriteChunkToNAND(yaffs_Device *dev,int chunkInNAND,const __u8 *data, yaffs_Spare *spare) +{ + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice); + size_t dummy; + int retval = 0; + + loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk; + + __u8 *spareAsBytes = (__u8 *)spare; + +#ifndef CONFIG_YAFFS_USE_OLD_MTD + if(data && spare) + { + if(dev->useNANDECC) + mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo); + else + mtd->write_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo); + } + else + { +#endif + if(data) + retval = mtd->write(mtd,addr,dev->nBytesPerChunk,&dummy,data); + if(spare) + retval = mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes); +#ifndef CONFIG_YAFFS_USE_OLD_MTD + } +#endif + + if (retval == 0) + return YAFFS_OK; + else + return YAFFS_FAIL; +} + +int nandmtd_ReadChunkFromNAND(yaffs_Device *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare) +{ + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice); + size_t dummy; + int retval = 0; + + loff_t addr = ((loff_t)chunkInNAND) * dev->nBytesPerChunk; + + __u8 *spareAsBytes = (__u8 *)spare; + +#ifndef CONFIG_YAFFS_USE_OLD_MTD + if(data && spare) + { + if(dev->useNANDECC) + { // Careful, this call adds 2 ints to the end of the spare data. Calling function should + // allocate enough memory for spare, i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. + retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_oobinfo); + } + else + { + retval = mtd->read_ecc(mtd,addr,dev->nBytesPerChunk,&dummy,data,spareAsBytes,&yaffs_noeccinfo); + } + } + else + { +#endif + if(data) + retval = mtd->read(mtd,addr,dev->nBytesPerChunk,&dummy,data); + if(spare) + retval = mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes); +#ifndef CONFIG_YAFFS_USE_OLD_MTD + } +#endif + + if (retval == 0) + return YAFFS_OK; + else + return YAFFS_FAIL; +} + +// Callback not needed for NAND +#if 0 +static void nandmtd_EraseCallback(struct erase_info *ei) +{ + yaffs_Device *dev = (yaffs_Device *)ei->priv; + up(&dev->sem); +} +#endif + + +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber) +{ + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice); + __u32 addr = ((loff_t) blockNumber) * dev->nBytesPerChunk * dev->nChunksPerBlock; + struct erase_info ei; + int retval = 0; + + ei.mtd = mtd; + ei.addr = addr; + ei.len = dev->nBytesPerChunk * dev->nChunksPerBlock; + ei.time = 1000; + ei.retries = 2; + ei.callback = NULL; + ei.priv = (u_long)dev; + + // Todo finish off the ei if required + + sema_init(&dev->sem,0); + + retval = mtd->erase(mtd,&ei); + + //No need for callback + // down(&dev->sem); // Wait for the erasure to complete + + if (retval == 0) + return YAFFS_OK; + else + return YAFFS_FAIL; +} + +int nandmtd_InitialiseNAND(yaffs_Device *dev) +{ + return YAFFS_OK; +} + +#endif // CONFIG_YAFFS_MTD_ENABLED + Index: linux-2.6.9/fs/yaffs/yaffs-header.c =================================================================== --- /dev/null +++ linux-2.6.9/fs/yaffs/yaffs-header.c @@ -0,0 +1,13 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * + * Copyright (C) 2002 Aleph One Ltd. + * for Toby Churchill Ltd and Brightstar Engineering + * + * Created by Charles Manning + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + */