From: aleph1 Date: Mon, 20 May 2002 17:41:34 +0000 (+0000) Subject: Initial revision X-Git-Url: http://aleph1.co.uk/gitweb/?a=commitdiff_plain;h=cefeada5db633fd3c37cde66e2e62c73052c18bd;p=yaffs%2F.git Initial revision --- cefeada5db633fd3c37cde66e2e62c73052c18bd diff --git a/Documentation/sv9349341.gif b/Documentation/sv9349341.gif new file mode 100644 index 0000000..4e8ea84 Binary files /dev/null and b/Documentation/sv9349341.gif differ diff --git a/Documentation/sv9929930.gif b/Documentation/sv9929930.gif new file mode 100644 index 0000000..0bb870a Binary files /dev/null and b/Documentation/sv9929930.gif differ diff --git a/Documentation/taffs-todo.html b/Documentation/taffs-todo.html new file mode 100644 index 0000000..1a81f4c --- /dev/null +++ b/Documentation/taffs-todo.html @@ -0,0 +1,34 @@ + + + + + + + + + + + + +

YAFFS Todo

+
    +
  1. Locking

    +
  2. Fix statfs. df does not give correct results.

    +
  3. Fix disk full condition. +

    +
  4. ECC and write failure handling.

    +
  5. Add symlinks and links to VFS interface.

    +
  6. Add memory mapping (to allow file execution).

    +
  7. Test with nandemul mtd.

    +
  8. Discuss improved NAND page interface with mtd group.

    +
+

NANDEmul MTD

+
    +
  1. Complete and test.

    +
  2. +

    +
+



+

+ + \ No newline at end of file diff --git a/Documentation/tnode.drawing.sda b/Documentation/tnode.drawing.sda new file mode 100644 index 0000000..13f1465 Binary files /dev/null and b/Documentation/tnode.drawing.sda differ diff --git a/Documentation/yaffs-0.3.html b/Documentation/yaffs-0.3.html new file mode 100644 index 0000000..ae4b46c --- /dev/null +++ b/Documentation/yaffs-0.3.html @@ -0,0 +1,504 @@ + + + + + + + + + + + + +

YAFFS (yet another Flash File System)

+

Version 0.3
Charles Manning (and Wookey), December 2001

+



+

+

Revision History

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

V0.0

+
+

20/12/01

+
+

First draft

+
+

V0.1

+
+

11/01/02

+
+

Minor corrections & + cosmetics.
Change use of data status byte.

+
+

V0.2

+
+

28/01/02

+
+

Added observations about + inodes, file headers and hard links.

+
+

V0.3

+
+

26/02/02

+
+

W:Added some general + observations on compatibility, partitions and bootloading.

+
+



+

+

Scope

+

The purpose of this document is to outline a potential +NAND-friendly file system for Linux.

+

Background

+

There are already various flash-file systems (FFSs) or block +drivers for flash (on top of which a regular FS runs). There are pros +and cons with all of these. +

+

Flash memory has quite a few constraints which will not be +addressed here. Various approaches are available to work around these +constraints to provide a file system. It is important to recognise +that "flash" includes both NOR and NAND flash which have +different sets of constraints. It is easy to be mislead by the +generic term "flash" into thinking that approaches +appropriate for NOR flash are immediately suitable for NAND flash.

+

The NAND block drivers (eg. SmartMedia [currently not available +for Linux] and DiskOnChip NFTL) typically use FAT16 as the file +system. This isn't too robust and nor is it that flash-friendly. +These block drivers provide a logical to physical mapping layer to +emulate rewritable blocks that look like disk sectors. When used with +FAT16, these file systems work reasonably well. They have a low +memory footprint and scale well. Like all FAT based systems they are +prone to corruption ("lost clusters etc").

+

The other approach is to design an entire file system which does +not work through a block driver layer and is flash-friendly. This +allows more scope to work around issues.

+

Currently, two Linux file systems that support NOR flash very well +are JFFS and its derivative JFFS2. Both of these are based on the +principles of journaling (hence the leading J) which significantly +increases robustness - a particularly important feature in embedded +systems. Unfortunately neither of these file systems scale +particularly well in both boot time and RAM usage. Scaling is +particularly relevant when one considers that a 16MB NOR array would +be considered large while a 128MB NAND is available as a single chip.

+

JFFS requires a RAM-based jffs_node structure for each journalling +node in the flash. Each of these nodes is 48 bytes. JFFS2 makes a +significant improvement here by reducing the equivalent structure +(jffs2_raw_node_ref) to 16 bytes. Still, at say an average node-size +of 512 bytes, a 128MB NAND might need 250000 of these ... 4MB!

+

Both JFFS and JFFS2 require scanning the flash array at boot time +to find the journaling nodes and determine the file structures. Since +NAND is large, slow, serially accessed and needs ECC this does not +scale well and will take an unacceptably long boot time for the +target systems. As a thumb-suck, the scanning of a 128MB NAND array +might take approx 25 seconds.

+

The intentions of the design sketched here are:

+ +

This FS is intended primarily for internal NAND rather than removable NAND +(SM cards). On removable SM cards Smartmedia compatibility is likely to be +important so SM/FAT will normally be used, although of course YAFFS makes a +lot of sense if reliability is more important than compatibility.

+ +

Overview

+

Here follows a simplified overview of YAFFS.

+

YAFFS uses a physical flash format similar to SmartMedia. This is +done for various reasons:

+ +

Some of the fields are obviously different to reflect the +different usage. Despite the similarities YAFFS is not actually compatible +with SM/FAT. SM cards need to be reformatted to switch from using SM/FAT to +YAFFS or vice versa.

+

File data is stored in fixed size "chunks" consistent +with the size of a page (ie. 512 bytes). Each page is marked with a +file id and chunk number. These tags are stored in the "spare +data" region of the flash. The chunk number is determined by +dividing the file position by the chunk size.

+

When data in a file is overwritten, the relevant chunks are +replaced by writing new pages to flash containing the new data but +the same tags. The overwritten data is marked as "discarded". +

+

File "headers" are stored as a single page, marked so as +to be differentiated from data pages.

+

Pages are also marked with a short (2 bit) serial number that +increments each time the page at this position is incremented. The +reason for this is that if power loss/crash/other act of demonic +forces happens before the replaced page is marked as discarded, it is +possible to have two pages with the same tags. The serial number is +used to arbitrate.

+

A block containing only discarded pages (termed a dirty block) +is an obvious candidate for garbage collection. Otherwise valid pages +can be copied off a block thus rendering the whole block discarded +and ready for garbage collection.

+

In theory you don't need to hold the file structure in RAM... you +could just scan the whole flash looking for pages when you need them. +In practice though you'd want better file access times than that! The +mechanism proposed here is to have a list of __u16 page addresses +associated with each file. Since there are 218 pages in a +128MB NAND, a __u16 is insufficient to uniquely identify a page but +is does identify a group of 4 pages - a small enough region to search +exhaustively. This mechanism is clearly expandable to larger NAND +devices - within reason. The RAM overhead with this approach is +approx 2 bytes per page - 512kB of RAM for a whole 128MB NAND.

+

Boot-time scanning to build the file structure lists should +require just one pass reading NAND. Since only the the spare data +needs to be read, this should be relatively fast ( approx 3 seconds +for 128MB). Improvements can be achieved by partitioning the NAND. +ie. mount the critical partition first then mount the data partition +afterwards.

+

Various runtime improvements can be achieved by changing the +"chunk size" to 1024 bytes or more. However this would +likely reduce flash efficiency. As always, life is a compromise....

+



+

+

Spare area details

+

The following table summarizes the layout of the spare area of +each page.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Byte #

+
+

SmartMedia usage

+
+

YAFFS usage

+
+

0..511

+
+

Data

+
+

Data. either file data or file header depending on tags

+
+

512..515

+
+

Reserved

+
+

Tags

+
+

516

+
+

Data status byte. Not used in SM code from Samsung

+
+

Data status byte. If more than 4 bits are zero, then this page + is discarded.

+
+

517

+
+

Block status byte

+
+

Block status byte

+
+

518..519

+
+

Block address

+
+

Tags

+
+

520..522

+
+

ECC on second 256 bytes part of data

+
+

ECC on second 256 bytes of data

+
+

523..524

+
+

Block address

+
+

Tags

+
+

525..527

+
+

ECC on first 256 bytes part of data

+
+

ECC on first 256 bytes part of data

+
+



+

+

The block status is a reserved value that shows whether the block +is damaged.

+

The data status tracks whether the page is valid. If less than 4 +bits are zero, then the page is valid otherwise it is discarded.

+

There are 8 bytes (64 bits) for use by YAFFS tags. This is +partitioned as follows:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Number of bits

+
+

Usage

+
+

18

+
+

18-bit file id. ie. Limit of 218 (over 260000) + files. File id 0 is not valid and indicates a deleted page. File + Id 0x3FFFF i is also not valid.

+
+

2

+
+

2-bit serial number.

+
+

20

+
+

20-bit page id within file. Limit of 220 pages per + file. ie. over 500MB file max size. Page id 0 means the file + header for this file.

+
+

10

+
+

10-bit counter of the number of bytes used in the page.

+
+

12

+
+

12-bit ECC on tags.

+
+

2

+
+

Unused. Keep as 1.

+
+

64

+
+

Total

+
+



+

+

A bit of explanation on the usage of some of these fields:

+

file Id is synonymous with inode.

+

The serial number is incremented each time a page with the same +file_id:page_id is rewritten (because of data changes or copy during +garbage collection). When a page is replaced, there is a brief period +during which there are two pages with the same id. The serial number +resolves this. Since there should never be a difference of less than +more than one, a two-bit counter is sufficient to determine which is +the current page.

+

When the page is rewritten, the file id, these data status byte +and the 12-bit ECC are all written to zero.

+

The byte counter indicates how many bytes are valid in this page. +Since the page would not exist if it contains zero bytes, this field +should thus hold 512 for all pages except the last page in the file. +The use of counters means that the file length integrity is preserved +while the file is open without having to constantly update the file +length in the file header. The file header only needs to be refreshed +when the file is closed (rather than whenever it is appended to). +This field is wide enough to allow expansion to 1024-byte "chunks".

+

File "headers" come in two flavours:

+ +

A directory also appears as a file (ie. has an inode and hard +link(s)) but has no data.

+

The 12-bit ECC applies to only the tag data uses a similar +algorithm to the 22-bit ECCs used for file system data. They are kept +independent.

+

RAM data details

+

Block management details are reasonably obvious and, I feel, don't +need to be addressed here apart from stating that there will be a +structure to track block status (eg. number of pages in use, failed +blocks, and which are candidates for garbage collection etc).

+

The files need an indexing structure of sorts to locate and track +the pages in the file. Some sort of tree structure should work rather +well. The look-up needs to be quite efficient in both CPU time and +space.

+

Page allocation and garbage collection

+

Pages are allocated sequentially from the currently selected +block. When all the pages in the block are filled, another clean +block is selected for allocation. At least two or three clean blocks +are reserved for garbage collection purposes. If there are +insufficient clean blocks available, then a dirty block ( ie one +containing only discarded pages) is erased to free it up as a clean +block. If no dirty blocks are available, then the dirtiest block is +selected for garbage collection.

+

Garbage collection is performed by copying the valid data pages +into new data pages thus rendering all the pages in this block dirty +and freeing it up for erasure. I also like the idea of selecting a +block at random some small percentage of the time - thus reducing the +chance of wear differences.

+

Relative to NOR, NAND writes and erases very fast. Therefore +garbage collection might be performed on-demand (eg. during a write) +without significant degradation in performance. Alternatively garbage +collection might be delegated to a kernel tasklet.

+

Relative to JFFSx, the garbage collection presented here is +incredibly simple - thanks mainly to the use of fixed-size pages +instead of journaling nodes.

+

Flash writing

+

As presented here, YAFFS only writes to the page's data area once +and to the spare area twice (once when new page is written and once when it +gets stomped on) before an erasure. This is within the limits of the most +restrictive NAND flashes.

+ +

Wear leveling

+

No wear leveling is explicitly used here. Instead we rely on two +"strategies":

+ +

Partitioning

+

Partitioning is not included in this spec, but could be added if +required.

+

Bootloading

+

Bootloaders cannot just read files direct from NAND due to the high +probability of bad blocks. Because YAFFS is quite simple it will be +relatively straightforward for bootloaders to read from it (eg reading a +kernel).

+ +

Conclusion

+

YAFFS is very simple. It is also NAND-friendly, is relatively +frugal with resources (especially RAM) and boots quickly. Like JFFSx +it has journaling which makes it far more robust than FAT.

+

While it might seem high-risk to develop YAFFS, it is probably +about the same amount of effort as implementing changes to JFFS to +get it to work effectively within the constraints of NAND. A +resulting JFFSx system would still require significant amounts of RAM +and have long boot times.

+

While YAFFS is indeed a new file system internally, much of the +file system glue code (including inode management, link management +etc) can likely be stolen from JFFSx. +

+



+

+



+

+



+

+



+

+



+

+



+

+



+

+



+

+



+

+ + \ No newline at end of file diff --git a/Documentation/yaffs-notes2.html b/Documentation/yaffs-notes2.html new file mode 100644 index 0000000..a2f2344 --- /dev/null +++ b/Documentation/yaffs-notes2.html @@ -0,0 +1,719 @@ + + + + + + + + + + + + +

YAFFS Development Notes

+



+

+



+

+

Build options

+

Yaffs can be built as either a kernel module (ie. a Linux file +system) or as an application.

+

Of course the goal is to build yaffs as a file system running on +top of real NAND flash, but yaffs can be built to run on a RAM +emulation layer for in-kernel testing without NAND flash in the +system.

+

Building as an application allows the yaffs_guts algorithms to be +tested/debugged in a more friendly debugging environment. The test +harness is in yaffsdev.c

+

Trying it out

+

YAFFS can be built to run with either mtd or RAM emulation, or +both. The file system that interfaces to mtd is called yaffs, +the file system that uses internal ram is called yaffsram. +YAFFS simultaneously supports both if they are enabled.

+
    +
  1. Hack the Makefile and change the KERNELDIR define to your + kernel directory.

    +
  2. If you don't have mtd support in your kernel, then you might + need to turn off USE_MTD otherwise the yaffs module might not load.

    +
  3. Type make clean; make + to build yaffs

    +
  4. Load yaffs as a module by typing /sbin/insmod + yaffs.o (ya gotta be root!).

    +
  5. Create a mount point eg. mkdir + /mnt/y

    +
  6. To mount the RAM emulation version of yaffs, mount + -t yaffsram none /mnt/y

    +
  7. Alternatively, + to mount the mtd version of yaffs, mount -t yaffs + /dev/mtd0 /mnt/y

    +
+



+

+

YAFFS Data structures

+

All data types are defined in yaffs_guts.h

+

Yaffs uses the following major objects:

+ +

The Tnodes and Objects are allocated in groups to reduce memory +allocation/freeing overheads. Freed up tnodes and objects are kept in +a free list and re-used.

+

yaffs_Object

+



+

+

struct + yaffs_ObjectStruct

+

{

+

__u8 +fake:1; // A fake object has no presence on NAND.

+

__u8 +renameAllowed:1; // Are we allowed to rename it?

+

__u8 +unlinkAllowed:1; // Are we allowed to unlink it?

+

__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

+

+

__u32 +objectId; // the object id value

+

+

+

__u32 +st_mode; // protection

+

__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

+


+

+

+

yaffs_ObjectType +variantType;

+

+

yaffs_ObjectVariant +variant;

+

+

};

+



+

+

Obvious stuff skipped....

+

fake, renameAllowed, unlinkAllowed are special flags for handling +"fake" objects which live in the file system but do not +live on NAND. Currently there are only two such objects: the root +object and the lost+found directory. None of these may be renamed or +unlinked.

+

serial, sum: see yaffs_ObjectHeader.

+

dirty indicates that the object's contents has changed and a new +yaffs_ObjectHeader must be written to flash.

+

valid indicates that the object has been loaded up. This is only +used during scanning (yaffs_Scan()) since we can know of an object's +existance - and thus need to create the object header - before we +encounter the associated yaffs_ObjectHeader.

+

hashlink is a list of Objects in the same hash bucket. +

+

The four variants hold extra info:

+

Files hold the file size and the top level and pointer to the +tnode tree for the file.

+

Directories hold a list of children objects.

+

Symlinks hold a pointer to the alias string. This is probably +inefficient, but that probably does not matter since we don't expect +to see many of these. +

+

Hardlinks hold information to identify the equivalent object.

+



+

+



+

+

File structure (tnodes)

+

File structures are done with a tiered indexing structure

+



+

+




+

+

The file structure is maintained by a tree structure. Depending +where it is in the tree, each tree node (tnode) (the +blue/things) holds either:

+ +

When the file starts out, it is assigned only one low-level tnode. +When the file expands past what a single tnode can hold, then it is +assigned a second tnode and an internal node is added to point to the +two tnodes. As the file grows, more low-level tnodes and high level +tnodes are added.

+

Traversing the tnode tree to find a particular page in a file is +quite simple: each internal tnode is selected from by using 3 bits of +the page offset in the file. The level 0 page resolves 4 bits.

+

For example, finding page 0x235 (ie. the one starting at file +position 0x46800 would proceed as follows:

+

0x235 is 0000001000110101, partitioned as follows. +000.000.100.011.0101

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Level

+
+

Bits

+
+

Selected value

+
+

3 or more if they exist

+
+

>= 10

+
+

Zero

+
+

2

+
+

9 to 7

+
+

100 binary = 4

+
+

1

+
+

6 to 4

+
+

011 binary = 3

+
+

0

+
+

3 to 0

+
+

0101 binary = 5

+
+



+

+

Free tnodes are stored in a list. When the list is exhausted, more +are allocated.

+

Each tnode takes 32 bytes. Each file needs at least one level 0 +tnode. How many do we need? A full 16MB file system needs at least +16MB/512/16 = 2000 level zero tnodes. More for internal tnodes. More +for files smaller than optimal.

+

The tree grows as required. When the file is resized to a smaller +size then it is pruned.

+



+

+

NAND data

+

Data is stored on NAND in "chunks". Currently each chunk +is the same size as a NAND flash page (ie. 512 bytes + 16 byte +spare). In the future we might decide to allow for different chunk +sizes. Chunks can hold either:

+ +

The 16 byte spare area contains:

+ +

The tags are made up as follows:

+

typedef +struct

+

{

+

+ unsigned chunkId:20; //chunk number in file

+

+ unsigned serialNumber:2; //serial number for chunk

+

+ unsigned byteCount:10; //number of bytes of data used in this +chunk

+

+ unsigned objectId:18; //the object id that this chunk belongs +to. +

+

+ unsigned ecc:12; //ECC on tags

+

+ unsigned unusedStuff:2; //unused

+

} +yaffs_Tags;

+


+

+

A chunkId of zero indicates that this chunk holds a +yaffs_ObjectHeader. A non zero value indicates that this is a data +chunk and the position of the chunk in the file (ie. the first chunk +- at offset 0 - has chunkId 1). See yaffs_guts.c:yaffs_Scan () to see +how this is done.

+

When a chunk is repalced (eg. file details changed or a part of a +file was overwritten), the new chunk is written before the old chunk +is deleted. This means that we don't lose data if there is a power +loss after the new chunk is created but before the old one is +discarded, but it does mean that we can encounter the situation where +we find both the old and the new chunks in flash. The serialNumber is +incremented each time the chunk is written. 2 bits is sufficient to +resolve any ambiguity.

+

bytecount applies only to data chunks and tells how many of the +data bytes are valid.

+

objectId says which object this chunk belongs to.

+

ecc is used to perform error correction on the tags. Another ecc +field is used to error correct the data.

+



+

+

yaffs_ObjectHeader

+

typedef +struct

+

{

+

yaffs_ObjectType +type;

+


+

+

// +Apply to everything

+

int + parentObjectId;

+

__u16 +sum; // checksum of name

+

char + name[YAFFS_MAX_NAME_LENGTH + 1];

+


+

+

// +Thes following apply to directories, files, symlinks - not hard links

+

__u32 +st_mode; // protection

+

__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

+

+

// +File size applies to files only

+

int +fileSize; +

+

+

// +Equivalent object id applies to hard links only.

+

int + equivalentObjectId;

+

+ // alias only applies to symlinks

+

char +alias[YAFFS_MAX_ALIAS_LENGTH + 1];

+

+

+

} +yaffs_ObjectHeader;

+


+

+

A yaffs_ObjectHeader is stored in NAND for every yaffs_Object.

+

type holds the type of yaffs_Object (file,directory,hardlink or +symlink).

+

parentObject is the objectId of this object's parent. name holds +the object's name. Together these form the directory structure of the +file system. Also worth mention is sum. This is a "checksum" +on the name which speeds directory searching (ie. when searching the +directory we only compare the name for those entries where sum +matches).

+

Obvious stuff skipped....

+

equivalentObjectId is used by hardlinks. A hardlink to an object +uses this field to specify the object that this links to. This way of +doing things is a bit different than the normal Linux way of doing +things (ie. keeping the links distinct from the inode) but is simpler +and uses less space except for a few corner cases with hardlinks.

+

alias is used by symlinks to hold the symlink alias string. This +limits the size of the symlink alias. In future we should expand +YAFFS to use data chunks to store aliases too long to fit into the +yaffs_ObjectHeader.

+



+

+

NAND Interface

+

All NAND access is performed via four functions pointed to by +yaffs_Device. At the moment a chunk is a page.

+ +

In the Readxxx and Writexxx functions, the data and/or spare +pointers may be NULL in which case these data transfers are ignored.

+

A quick note about NAND:

+ +



+

+

mkyaffs

+

mkyaffs is the tool to format a NAND mtd to be used for YAFFS. +This is quite simple, just erase all the undamaged blocks. YAFFS +treats erased blocks as free (empty) space.

+



+

+

Expected performance

+

The following numbers should give an indication of the performance +we should expect from YAFFS.

+

As an example, I'll use the following numbers. Since the hardware +is capable of 50ns read/write, these numbers allow for some other +ovberheads. Clearly though, the performance can be degraded in +various ways.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Seek

+
+

10uS/page

+
+

Read

+
+

100nS/byte

+
+

Write

+
+

100nS/byte

+
+

Program

+
+

200uS/page

+
+

Erase

+
+

2mS/block

+
+



+

+

From this we can derive some higher-level numbers:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Operation

+
+

Time

+
+

Calculation

+
+

Read spare

+
+

12uS

+
+

seek + 16 * read

+
+

Read page

+
+

63 uS

+
+

seek + 528 * read

+
+

Write page

+
+

326 uS

+
+

seek + 528 * write + program + read page (for verification)

+
+

Discard page

+
+

212 uS

+
+

seek + 16 * write + program

+
+

Overwrite page

+
+

538 uS

+
+

write page + discard page

+
+

Erase overhead per page

+
+

63uS

+
+

erase/32

+
+



+

+

From this we can infer the following flash access times:

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Operation

+
+

Time

+
+

Calculation

+
+

Read 1MB file

+
+

0.13s (about 7.5 MB/s)

+
+

2000 * read page

+
+

Write 1MB (clean system)

+
+

0.53s (about 1.8 MB/s)

+
+

2000 * write page

+
+

Overwrite 1MB file (no gc)

+
+

1.08s (about 0.9MB/s)

+
+

2000 * overwrite page

+
+

Overwrite 1MB with 50% gc

+
+

2.4s (about 0.4 MB/s)

+
+

2000 * overwrite page + 2000 * page copy (== overwrite page) + + 4000 * erase overhead

+
+

Delete 1MB file

+
+

0.43s (about 2.2 MB/s)

+
+

2000 * discard page

+
+

Delete 1MB file with 50% gc

+
+

0.49s (about 2.0MB/s)

+
+

2000 * discard page + 1000 * erase overhead

+
+



+

+



+

+



+

+ + \ No newline at end of file diff --git a/Documentation/yaffs_report20020211.html b/Documentation/yaffs_report20020211.html new file mode 100644 index 0000000..212eb88 --- /dev/null +++ b/Documentation/yaffs_report20020211.html @@ -0,0 +1,81 @@ + + + + + + + + + + + + +

YAFFS Progress 11 Feb 2002

+

Approach

+

C. A. R. Hoare, in his 1980 ACM Turing Award lecture, told of +two ways of constructing a software design: "One way is to make +it so simple that there are obviously no deficiencies and the other +way is to make it so complicated that there are no obvious +deficiencies."

+

The approach I'm taking with Yaffs is to design and make notes up +front as much as possible. I find this helps make the coding/debug +phase go way faster and more predictably.

+

Although obviously the code ends up in the kernel, I'm abstracting +out kernel services so that I develop/debug in user space using +vanilla dev tools. I like to use Source Navigator because it has a +nice xref capability though a few quirks too). Thus, yaffs code will +be tested in application space wrapped in a test harness which calls +the yaffs functions. and sits on top of a NAND emulation layer (just +two big disk files). I can then do things like force ecc failures +etc at will to test recovery logic and check booting etc. This +general approach has served me well in the past and I expect it will +this time too.

+

Design

+

At this stage all the major design work is done. Basically there +are 3 in RAM management "objects":

+ +

There is a need for speed in certain areas, such as rapidly +locating inodes, links data pages during certain scenarios (eg. +garbage collection). These have been identified and suitable look-up +structures are in place.

+

All dynamic data structures are allocated from pools rather than +one at a time. This is more efficient time-wise and simplifies +clean-up.

+

Code

+

Much of the code for the management layers is already in place, as +is some of the tag management code. Some of the code has already been +tested.

+

I have started with coding the management layer since this is the +most critical part to get right. Every now and then I have coded up +some other section to verify that what I have will work.

+

Time

+

So far I've logged 65 hours on yaffs (though it chews up +significant null cycles too :-)). I believe this is comfortably +within budget. I was not able to make much progress through Christmas +and the week of January, but am able to give a lot more effort now.

+



+

+



+

+ + \ No newline at end of file diff --git a/Documentation/yaffs_report20020304.html b/Documentation/yaffs_report20020304.html new file mode 100644 index 0000000..afc7569 --- /dev/null +++ b/Documentation/yaffs_report20020304.html @@ -0,0 +1,57 @@ + + + + + + + + + + + + +

YAFFS Progress 4 March 2002

+



+

+

Design

+

A few of the design areas needed to be tweaked slightly without +significant impact.

+

The only real significance is that what is termed a yaffs_Inode +really becomes an object Id. Where an 'object' is a file, directory, +symlink or hard link. This differs from the *nix concept of an inode +in that a hard link is not an inode. yaffs_Inode should really be +renamed to yaffs_Object to reflect this.

+

Rge reason for introducing the object is to provide identifiers +for the objects stored on NAND. For example, each hardlink (which +does not have a distinct inode) needs an object Id for storage on +

+

Code

+

Pretty much all the guts of the code is there except for:

+ +

Some of the code has already been tested.

+

I will now be testing out the guts on a NAND emulation layer in an +application. This provides better debugging. I hope to have this +running by 11/03/2002. +

+

Once the guts looks solid on the NAND emulation, I will graft it +on to the VFS and mtd - at which point it becomes a real file +system.

+

Time

+

So far I've logged 90 hours on yaffs. I will not be charging for +approx 15 hours I have spent swotting up on the Linux VFS. I still +believe this is on budget.

+



+

+



+

+



+

+ + \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..a0d8138 --- /dev/null +++ b/Makefile @@ -0,0 +1,38 @@ +#Makefile for YAFFS as a module +# +# NB this is not yet suitable for putting into the kernel tree. +# 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. + +## Change or override KERNELDIR to your kernel +## comment out USE_xxxx if you don't want these features. + +KERNELDIR = /usr/src/kernel-headers-2.4.18 +USE_RAM_FOR_TEST = -DYAFFS_RAM_ENABLED +USE_MTD = -DYAFFS_MTD_ENABLED + + +CFLAGS = -D__KERNEL__ -DMODULE $(USE_RAM_FOR_TEST) $(USE_MTD) -I$(KERNELDIR)/include -O2 -Wall + + +OBJS = yaffs_fs.o yaffs_guts.o yaffs_ramem.o yaffs_mtdif.o + + +all: yaffs.o + +$(OBJS): %.o: %.c + gcc -c $(CFLAGS) $< -o $@ + +yaffs.o: $(OBJS) + ld -r $(OBJS) -o $@ + +clean: + rm -f $(OBJS) core diff --git a/devextras.h b/devextras.h new file mode 100644 index 0000000..a28135f --- /dev/null +++ b/devextras.h @@ -0,0 +1,257 @@ +/* + * 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 General Public License version 2 as + * published by the Free Software Foundation. + * + * 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. + * + */ + +#ifndef __EXTRAS_H__ +#define __EXTRAS_H__ + +#ifndef __KERNEL__ + +// 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) + + + +#define CURRENT_TIME 0 + + +/* + * 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 + +#include + +/* + * 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 +#include +#include +#include +#include + + +#endif + + + + +#endif + diff --git a/snMakefile b/snMakefile new file mode 100644 index 0000000..4557fa5 --- /dev/null +++ b/snMakefile @@ -0,0 +1,79 @@ +######################################################### +# Makefile auto generated by Cygnus Source Navigator. +# Target: yaffsdev_RAM Date: May 03 2002 Time: 07:11:35 PM +# + + +.SUFFIXES: .cc .class .java .cxx .C .cpp .o .c .l .y + + +VPATH = /opt/aleph1/yaffs/. + + +YACC = bison +LEX = flex +JAVA = gcj +CC = gcc +CPP = g++ +YACC_FLAGS = +LEX_FLAGS = +JAVA_FLAGS = +CC_FLAGS = -g -Wall +CPP_FLAGS = +YACC_INCLUDES = +LEX_INCLUDES = +JAVA_INCLUDES = +CC_INCLUDES = +CPP_INCLUDES = +YACC_DEFINES = +LEX_DEFINES = +JAVA_DEFINES = +CC_DEFINES = +CPP_DEFINES = +yaffsdev_RAM_LIBS = +LINKER = gcc +LINKER_FLAGS = +LINKER_ENTRY = +yaffsdev_RAM_OBJECTS = yaffs_guts.o yaffs_ramem.o yaffsdev.o + +all: yaffsdev + +yaffsdev: $(yaffsdev_RAM_OBJECTS) + $(LINKER) -o yaffsdev $(LINKER_ENTRY) $(LINKER_FLAGS) $(yaffsdev_RAM_OBJECTS) $(yaffsdev_RAM_LIBS) + +.y.c: + $(YACC) $< $(YACC_FLAGS) $(YACC_DEFINES) $(YACC_INCLUDES) + + +.l.c: + $(LEX) -t $< > $@ $(LEX_FLAGS) $(LEX_DEFINES) $(LEX_INCLUDES) + + +.java.o: + $(JAVA) -c $< $(JAVA_FLAGS) $(JAVA_DEFINES) $(JAVA_INCLUDES) +.class: + $(JAVA) -c $< $(JAVA_FLAGS) $(JAVA_DEFINES) $(JAVA_INCLUDES) + + +.c.o: + $(CC) -c $< $(CC_FLAGS) $(CC_DEFINES) $(CC_INCLUDES) + + +.cpp.o: + $(CPP) -c $< $(CPP_FLAGS) $(CPP_DEFINES) $(CPP_INCLUDES) +.C.o: + $(CPP) -c $< $(CPP_FLAGS) $(CPP_DEFINES) $(CPP_INCLUDES) +.cc.o: + $(CPP) -c $< $(CPP_FLAGS) $(CPP_DEFINES) $(CPP_INCLUDES) +.cxx.o: + $(CPP) -c $< $(CPP_FLAGS) $(CPP_DEFINES) $(CPP_INCLUDES) + + +yaffs_guts.o: /opt/aleph1/yaffs/yaffs_guts.h /opt/aleph1/yaffs/yaffsinterface.h /opt/aleph1/yaffs/yportenv.h +yaffs_ramem.o: yaffs_nandemul.h /opt/aleph1/yaffs/devextras.h /opt/aleph1/yaffs/yaffs_guts.h /opt/aleph1/yaffs/yaffsinterface.h /opt/aleph1/yaffs/yportenv.h +yaffsdev.o: yaffs_nandemul.h /usr/include/stdio.h /usr/include/stdlib.h /usr/include/string.h /opt/aleph1/yaffs/yaffs_guts.h /opt/aleph1/yaffs/yaffsinterface.h + +clean: + rm -f *.o + rm -f yaffsdev + diff --git a/utils/Makefile b/utils/Makefile new file mode 100644 index 0000000..fdc64a3 --- /dev/null +++ b/utils/Makefile @@ -0,0 +1,34 @@ +#Makefile for mkyaffs +# +# NB this is not yet suitable for putting into the kernel tree. +# 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. + +## Change or override KERNELDIR to your kernel + +KERNELDIR = /usr/src/kernel-headers-2.4.18 + +CFLAGS = -I$(KERNELDIR)/include -O2 -Wall + + +OBJS = mkyaffs.o + + +all: mkyaffs + +$(OBJS): %.o: %.c + gcc -c $(CFLAGS) $< -o $@ + +mkyaffs: $(OBJS) + gcc -o $@ $(OBJS) + +clean: + rm -f $(OBJS) core diff --git a/utils/mkyaffs b/utils/mkyaffs new file mode 100755 index 0000000..27e3a08 Binary files /dev/null and b/utils/mkyaffs differ diff --git a/utils/mkyaffs.c b/utils/mkyaffs.c new file mode 100644 index 0000000..7fd2b0b --- /dev/null +++ b/utils/mkyaffs.c @@ -0,0 +1,150 @@ +/* + * YAFFS: Yet another FFS. A NAND-flash specific file system. + * mkyaffs.c Format a chunk of NAND for 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. + * + * Acknowledgement: + * This file is crafted from nandtest.c by Miguel Freitas (miguel@cetuc.puc-rio.br) + * and Steven J. Hill (sjhill@cotw.com) + * + * Overview: + * Formatting a YAFFS device is very simple. Just erase all undamaged blocks. + * NB Don't erase blocks maked as damaged. + */ + +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +// 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 countBits[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 +}; + +/* + * Buffer arrays used for running tests + */ + +unsigned char oobbuf[16]; + + +/* + * Main program + */ +int main(int argc, char **argv) +{ + unsigned long addr; + int bs, fd, i; + struct mtd_oob_buf oob = {0, 16, (unsigned char *) &oobbuf}; + mtd_info_t meminfo; + erase_info_t erase; + + /* Make sure a device was specified */ + if(argc < 2) { + printf("usage: %s \n", argv[0]); + exit(1); + } + + /* Open the device */ + if((fd = open(argv[1], O_RDWR)) == -1) { + perror("open flash"); + exit(1); + } + + /* Fill in MTD device capability structure */ + if(ioctl(fd, MEMGETINFO, &meminfo) != 0) { + perror("MEMGETINFO"); + close(fd); + exit(1); + } + + /* Make sure device page sizes are valid */ + if( !(meminfo.oobsize == 16 && meminfo.oobblock == 512)) + { + printf("Unknown flash (not normal NAND)\n"); + close(fd); + exit(1); + } + + + for(addr = 0; addr < meminfo.size; addr += meminfo.erasesize) + { + /* Read the OOB data to determine if the block is valid. + * If the block is damaged, then byte 5 of the OOB data will + * have at least 2 zero bits. + */ + oob.start = addr; + oob.length = 16; + oob.ptr = oobbuf; + if (ioctl(fd, MEMREADOOB, &oob) != 0) + { + perror("ioctl(MEMREADOOB)"); + close(fd); + exit(1); + } + + if(countBits[oobbuf[5]] < 7) + { + printf("Block at 0x08%lx is damaged and is not being formatted\n",addr); + } + else + { + /* Erase this block */ + erase.start = addr; + erase.length = meminfo.erasesize; + printf("Erasing block at 0x08%lx\n",addr); + if(ioctl(fd, MEMERASE, &erase) != 0) + { + perror("\nMTD Erase failure\n"); + close(fd); + exit(1); + } + + } + + } + + + /* All the tests succeeded */ + printf("OK\n"); + close(fd); + return 0; +} + diff --git a/yaffs-header.c b/yaffs-header.c new file mode 100644 index 0000000..7cb71e0 --- /dev/null +++ b/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. + * + */ diff --git a/yaffs_fileem.c b/yaffs_fileem.c new file mode 100644 index 0000000..a137fb4 --- /dev/null +++ b/yaffs_fileem.c @@ -0,0 +1,145 @@ +/* + * 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_nandif.h" +#include "yaffs_guts.h" +#include "yaffsinterface.h" + +#include +#include +#include +#include +#include +#include +#include + +#define FILE_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) + + +static int h; +static __u8 ffChunk[528]; + + + +static void CheckInit(void) +{ + static int initialised = 0; + + int length; + + + if(!initialised) + { + memset(ffChunk,0xFF,528); + + h = open("yaffs-em-file" , O_RDWR | O_CREAT, S_IREAD | S_IWRITE); + if(h < 0) + { + perror("Fatal error opening yaffs emulation file"); + exit(1); + } + initialised = 1; + + length = lseek(h,0,SEEK_END); + if(length != FILE_SIZE_IN_BYTES) + { + // Create file contents + int i; + + printf("Creating emulation file...\n"); + for(i = 0; i < FILE_SIZE_IN_BLOCKS; i++) + { + yaffs_EraseBlockInNAND(i); + } + } + } +} + +int yaffs_WriteChunkToNAND(int chunkInNAND,__u8 *data, yaffs_Spare *spare) +{ + int pos; + + pos = chunkInNAND * 528; + + CheckInit(); + + + if(data) + { + lseek(h,pos,SEEK_SET); + write(h,data,512); + } + + pos += 512; + + if(spare) + { + lseek(h,pos,SEEK_SET); + write(h,spare,16); + } + + return YAFFS_OK; +} + + +int yaffs_ReadChunkFromNAND(int chunkInNAND, __u8 *data, yaffs_Spare *spare) +{ + int pos; + + pos = chunkInNAND * 528; + + + CheckInit(); + + if(data) + { + lseek(h,pos,SEEK_SET); + read(h,data,512); + } + + pos += 512; + + if(spare) + { + lseek(h,pos,SEEK_SET); + read(h,spare,16); + } + + return YAFFS_OK; +} + + +int yaffs_EraseBlockInNAND(int blockInNAND) +{ + int i; + + CheckInit(); + + printf("Erasing block %d\n",blockInNAND); + + lseek(h,blockInNAND * BLOCK_SIZE,SEEK_SET); + for(i = 0; i < 32; i++) + { + write(h,ffChunk,528); + } + return YAFFS_OK; +} + diff --git a/yaffs_fs.c b/yaffs_fs.c new file mode 100644 index 0000000..a0ada39 --- /dev/null +++ b/yaffs_fs.c @@ -0,0 +1,879 @@ +/* + * 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. + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "yaffs_guts.h" + +#ifdef YAFFS_RAM_ENABLED +#include "yaffs_nandemul.h" +// 2 MB of RAM for emulation +#define YAFFS_RAM_EMULATION_SIZE 0x200000 +#endif // YAFFS_RAM_ENABLED + +#ifdef YAFFS_MTD_ENABLED +#include +#include "yaffs_mtdif.h" +#endif //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) +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp) + + + +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_sync_object(struct file * file, struct dentry *dentry, int datasync); + +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir); + +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode); +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry); +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); +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev); +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); + +static int yaffs_statfs(struct super_block *sb, struct statfs *buf); +static void yaffs_read_inode (struct inode *inode); +static struct super_block *yaffs_read_super(struct super_block * sb, void * data, int silent); +static void yaffs_put_inode (struct inode *inode); + +//static int yaffs_readpage(struct file*,struct page * + +static struct address_space_operations yaffs_address_ops = { +// readpage: yaffs_readpage, +// prepare_write: yaffs_prepare_write, +// commit_write: yaffs_commit_write +}; + + +static struct file_operations yaffs_file_operations = { + read: yaffs_file_read, + write: yaffs_file_write, +// mmap: generic_file_mmap, + fsync: yaffs_sync_object, +}; + + +static struct inode_operations yaffs_file_inode_operations = { + setattr: yaffs_setattr, +}; + + + +static struct file_operations yaffs_dir_operations = { + read: generic_read_dir, + readdir: yaffs_readdir, + fsync: yaffs_sync_object, +}; + +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 super_operations yaffs_super_ops = { + statfs: yaffs_statfs, + read_inode: yaffs_read_inode, + put_inode: yaffs_put_inode, +// read_inode: +// remount_fs: +// clear_inode: +}; + + +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 + */ +static struct dentry * yaffs_lookup(struct inode *dir, struct dentry *dentry) +{ + yaffs_Object *obj; + struct inode *inode; + + + T((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); + + if(obj) + { + T((KERN_DEBUG"yaffs_lookup found %d\n",obj->objectId)); + + inode = yaffs_get_inode(dir->i_sb, obj->st_mode,0,obj); + + if(inode) + { + T((KERN_DEBUG"yaffs_loookup looks good\n")); + dget(dentry); // try to solve directory bug + d_add(dentry,inode); + return dentry; + } + + } + else + { + T((KERN_DEBUG"yaffs_lookup not found\n")); + + } + return NULL; + +} + +// For now put inode is just for debugging +static void yaffs_put_inode(struct inode *inode) +{ + T(("yaffs_put_inode: ino %d, count %d\n",(int)inode->i_ino, atomic_read(&inode->i_count))); +} + +#ifdef YAFFS_ADDRESS_OPS +static int yaffs_readpage(struct file *file, struct page * page) +{ + T((KERN_DEBUG"yaffs_readpage\n")); + + // TODO + return 0; +} + +static int yaffs_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to) +{ + T((KERN_DEBUG"yaffs_prepare_write\n")); + + //TODO + return 0; +} + +static int yaffs_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to) +{ + + struct inode *inode = page->mapping->host; + loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; + + T((KERN_DEBUG"yaffs_commit_write\n")); + + return 0; +} + +#endif //YAFFS_ADDRESS_OPS + + +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 = YAFFS_BYTES_PER_CHUNK; + inode->i_blocks = 0; + inode->i_rdev = NODEV; + inode->i_mapping->a_ops = &yaffs_address_ops; + inode->i_atime = obj->st_atime; + inode->i_mtime = obj->st_mtime; + inode->i_ctime = obj->st_ctime; + inode->i_size = yaffs_GetObjectFileLength(obj); + inode->i_nlink = yaffs_GetObjectLinkCount(obj); + + T((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: + // init_special_inode(inode, mode, dev); + break; + case S_IFREG: // file + inode->i_op = &yaffs_file_inode_operations; + inode->i_fop = &yaffs_file_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 = &page_symlink_inode_operations; + break; + } + + + inode->u.generic_ip = obj; + + } + else + { + T((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((KERN_DEBUG"yaffs_get_inode for object %d\n",obj->objectId)); + + inode = iget(sb,obj->objectId); + + // No need to call this since iget calls it via the read_inode callback + // yaffs_FillInodeFromObject(inode,obj); + + 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; + + T((KERN_DEBUG"yaffs_file_read\n")); + + obj = yaffs_DentryToObject(f->f_dentry); + 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; + } + ipos = *pos; + T((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; + + obj = yaffs_DentryToObject(f->f_dentry); + inode = f->f_dentry->d_inode; + nWritten = yaffs_WriteDataToFile(obj,buf,*pos,n); + ipos = *pos; + T((KERN_DEBUG"yaffs_file_write writing %d bytes, %d written at %d\n",n,nWritten,ipos)); + if(nWritten > 0) + { + *pos += nWritten; + ipos = *pos; + if(*pos > inode->i_size) + { + inode->i_size = *pos; + T((KERN_DEBUG"yaffs_file_write size updated to %d\n",ipos)); + } + + } + return nWritten; +} + + +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir) +{ + yaffs_Object *obj; + 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); + + offset = f->f_pos; + + T(("yaffs_readdir: starting at %d\n",(int)offset)); + + if(offset == 0) + { + T((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((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; + + //down(&obj->sem); + + + 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((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: + + //up(&obj->sem); + + out: + return 0; +} + + +/* + * File creation. Allocate an inode, and we're done.. + */ +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode, int dev) +{ + struct inode *inode; + + yaffs_Object *obj = NULL; + yaffs_Object *parent = yaffs_InodeToObject(dir); + + int error = -ENOSPC; + + if(parent) + { + T((KERN_DEBUG"yaffs_mknod: parent object %d type %d\n", + parent->objectId,parent->variantType)); + } + else + { + T((KERN_DEBUG"yaffs_mknod: could not get parent object\n")); + return -EPERM; + } + + T(("yaffs_mknod: making oject for %s, mode %x\n", + dentry->d_name.name, mode)); + + switch (mode & S_IFMT) + { + default: + + break; + case S_IFREG: // file + T((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((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((KERN_DEBUG"yaffs_mknod: making file\n")); + obj = NULL; // Todo + break; + } + + if(obj) + { + inode = yaffs_get_inode(dir->i_sb, mode, dev, obj); + +// did not fix dir bug if((mode & S_IFMT) == S_IFDIR) atomic_inc(&inode->i_count); + + d_instantiate(dentry, inode); + T((KERN_DEBUG"yaffs_mknod created object %d count = %d\n",obj->objectId,atomic_read(&inode->i_count))); + error = 0; + } + else + { + T((KERN_DEBUG"yaffs_mknod failed making object\n")); + error = -ENOMEM; + } + + + return error; +} + +static int yaffs_mkdir(struct inode * dir, struct dentry * dentry, int mode) +{ + int retVal; + T((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; +} + +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode) +{ + T((KERN_DEBUG"yaffs_create\n")); + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0); +} + + +static int yaffs_unlink(struct inode * dir, struct dentry *dentry) +{ + + T((KERN_DEBUG"yaffs_unlink\n")); + + if(yaffs_Unlink(yaffs_InodeToObject(dir),dentry->d_name.name) == YAFFS_OK) + { + return 0; + } + else + { + return -ENOTEMPTY; + } +} + + +/* + * Link a file.. + */ +static int yaffs_link(struct dentry *old_dentry, struct inode * dir, struct dentry * dentry) +{ + struct inode *inode = old_dentry->d_inode; + + T((KERN_DEBUG"yaffs_link\n")); + + return -EPERM; //Todo + + + if (S_ISDIR(inode->i_mode)) + return -EPERM; + + + return 0; +} + + +static int yaffs_symlink(struct inode * dir, struct dentry *dentry, const char * symname) +{ + int error; + + T((KERN_DEBUG"yaffs_symlink\n")); + + + return -ENOMEM; //Todo + + error = yaffs_mknod(dir, dentry, S_IFLNK | S_IRWXUGO, 0); + return error; +} + +static int yaffs_sync_object(struct file * file, struct dentry *dentry, int datasync) +{ + + T((KERN_DEBUG"yaffs_sync_object\n")); + yaffs_FlushFile(yaffs_DentryToObject(dentry)); + return 0; +} + +/* + * The VFS layer already does all the dentry stuff for rename. + */ +static int yaffs_rename(struct inode * old_dir, struct dentry *old_dentry, struct inode * new_dir,struct dentry *new_dentry) +{ + + + if( yaffs_RenameObject(yaffs_InodeToObject(old_dir),old_dentry->d_name.name, + yaffs_InodeToObject(new_dir),new_dentry->d_name.name) == YAFFS_OK) + { + return 0; + } + else + { + return -ENOTEMPTY; + } + + +} + +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr) +{ + struct inode *inode = dentry->d_inode; + int error; + + T((KERN_DEBUG"yaffs_setattr\n")); + + if((error = inode_change_ok(inode,attr)) == 0) + { + + if(yaffs_SetAttributes(yaffs_InodeToObject(inode),attr) == YAFFS_OK) + { + error = 0; + } + else + { + error = -EPERM; + } + + inode_setattr(inode,attr); + } + return error; +} + +static int yaffs_statfs(struct super_block *sb, struct statfs *buf) +{ + T((KERN_DEBUG"yaffs_statfs\n")); + + buf->f_type = YAFFS_MAGIC; + buf->f_bsize = YAFFS_BYTES_PER_CHUNK; + buf->f_namelen = 255; + buf->f_blocks = yaffs_SuperToDevice(sb)->nBlocks * YAFFS_CHUNKS_PER_BLOCK; + buf->f_files = 0; + buf->f_ffree = 0; + buf->f_bavail = yaffs_GetNumberOfFreeChunks(yaffs_SuperToDevice(sb)); + return 0; +} + +static void yaffs_read_inode (struct inode *inode) +{ + + yaffs_Object *obj ; + + T((KERN_DEBUG"yaffs_read_inode for %d\n",(int)inode->i_ino)); + + obj = yaffs_FindObjectByNumber(yaffs_SuperToDevice(inode->i_sb),inode->i_ino); + + yaffs_FillInodeFromObject(inode,obj); +} + + +static struct super_block *yaffs_internal_read_super(int useRam, struct super_block * sb, void * data, int silent) +{ + struct inode * inode; + struct dentry * root; + yaffs_Device *dev; + + + T(("yaffs_read_super:\n")); + sb->s_blocksize = YAFFS_BYTES_PER_CHUNK; + sb->s_blocksize_bits = YAFFS_CHUNK_SIZE_SHIFT; + sb->s_magic = YAFFS_MAGIC; + sb->s_op = &yaffs_super_ops; + + if(!sb) + printk(KERN_INFO"sb is NULL\n"); + else if(!sb->s_dev) + printk(KERN_INFO"sb->s_dev is NULL\n"); + else if(! kdevname(sb->s_dev)) + printk(KERN_INFO"kdevname is NULL\n"); + else + printk(KERN_INFO"dev is %d name is \"%s\"\n", sb->s_dev, kdevname(sb->s_dev)); + + + if(useRam) + { + +#if 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. + + dev->nBlocks = YAFFS_RAM_EMULATION_SIZE / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + dev->startBlock = 1; // Don't use block 0 + dev->endBlock = dev->nBlocks - 1; + + dev->writeChunkToNAND = nandemul_WriteChunkToNAND; + dev->readChunkFromNAND = nandemul_ReadChunkFromNAND; + dev->eraseBlockInNAND = nandemul_EraseBlockInNAND; + dev->initialiseNAND = nandemul_InitialiseNAND; +#endif + + } + else + { +#ifdef YAFFS_MTD_ENABLED + struct mtd_info *mtd; + + // Hope it's a NAND mtd + 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; + } + if(mtd->type != MTD_NANDFLASH) + { + printk(KERN_DEBUG "yaffs: MTD device is not NAND it's type %d\n", mtd->type); + return NULL; + } + + if(!mtd->erase || + !mtd->read || + !mtd->write || + !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 + + 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 = mtd; + + // Set up the memory size parameters.... + + dev->nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + dev->startBlock = 1; // Don't use block 0 + dev->endBlock = dev->nBlocks - 1; + + // ... and the functions. + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND; + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND; + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND; + dev->initialiseNAND = nandmtd_InitialiseNAND; + +#endif + } + + + + yaffs_GutsInitialise(yaffs_SuperToDevice(sb)); + T(("yaffs_read_super: guts initialised\n")); + + // Create root inode + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,yaffs_Root(yaffs_SuperToDevice(sb))); + if (!inode) + return NULL; + + T(("yaffs_read_super: got root inode\n")); + + + root = d_alloc_root(inode); + + T(("yaffs_read_super: d_alloc_root done\n")); + + if (!root) { + iput(inode); + return NULL; + } + sb->s_root = root; + + T(("yaffs_read_super: done\n")); + return sb; +} + +#ifdef YAFFS_MTD_ENABLED +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 + +#ifdef YAFFS_RAM_ENABLED + +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 // YAFFS_RAM_ENABLED + + +static struct proc_dir_entry *my_proc_entry; + + +static int yaffs_proc_read( + char *page, + char **start, + off_t offset, + int count, + int *eof, + void *data + ) +{ + + static char my_buffer[1000]; + + if (offset > 0) return 0; + + /* Fill the buffer and get its length */ + sprintf( my_buffer, + "YAFFS built:"__DATE__ " "__TIME__"\n" + + ); + + strcpy(page,my_buffer); + return strlen(my_buffer); +} + +static int __init init_yaffs_fs(void) +{ + int error = 0; + + printk(KERN_DEBUG "yaffs " __DATE__ " " __TIME__ " Initialisation\n"); + /* 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 YAFFS_RAM_ENABLED + + error = register_filesystem(&yaffs_ram_fs_type); + if(error) + { + return error; + } +#endif //YAFFS_RAM_ENABLED + +#ifdef YAFFS_MTD_ENABLED + error = register_filesystem(&yaffs_fs_type); + if(error) + { +#ifdef YAFFS_RAM_ENABLED + unregister_filesystem(&yaffs_ram_fs_type); +#endif //YAFFS_RAM_ENABLED + } +#endif // 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 YAFFS_RAM_ENABLED + unregister_filesystem(&yaffs_ram_fs_type); +#endif +#ifdef 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"); + diff --git a/yaffs_guts.c b/yaffs_guts.c new file mode 100644 index 0000000..6bc6136 --- /dev/null +++ b/yaffs_guts.c @@ -0,0 +1,3079 @@ +/* + * 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 + +#include "yportenv.h" + +#include "yaffsinterface.h" +#include "yaffs_guts.h" + + +#if 0 +#define T(x) printf x +#else +#define T(x) +#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_countBits[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 +}; + + + +// Device info +//static yaffs_Device *yaffs_device; +//yaffs_Object *yaffs_rootDir; +//yaffs_Object *yaffs_lostNFound; + + + +// Local prototypes +static int yaffs_CheckObjectHashSanity(yaffs_Device *dev); +static void yaffs_LoadTagsIntoSpare(yaffs_Spare *sparePtr, yaffs_Tags *tagsPtr); +static void yaffs_GetTagsFromSpare(yaffs_Spare *sparePtr,yaffs_Tags *tagsPtr); +static int yaffs_PutChunkIntoFile(yaffs_Object *in,int chunkInInode, int chunkInNAND); + +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); +static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId); +static void yaffs_RemoveObjectFromDirectory(yaffs_Object *obj); +static int yaffs_CheckStructures(void); +static yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object *obj); + +loff_t yaffs_GetFileSize(yaffs_Object *obj); + + +static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve); + +#if YAFFS_PARANOID +static int yaffs_CheckFileSanity(yaffs_Object *in) +#else +#define yaffs_CheckFileSanity(in) +#endif + +static int __inline__ 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) +{ + return dev->writeChunkToNAND(dev,chunkInNAND,data,spare); +} + +int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,int chunkInNAND, __u8 *data, yaffs_Spare *spare) +{ + return dev->readChunkFromNAND(dev,chunkInNAND,data,spare); +} + +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,int blockInNAND) +{ + 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 = 0; + + do{ + chunk = yaffs_AllocateChunk(dev,useReserve); + + if(chunk >= 0) + { + writeOk = yaffs_WriteChunkToNAND(dev,chunk,data,spare); + if(writeOk) + { + //Todo read-back and verify + // If verify fails, then delete this chunk and try again + } + } + } while(chunk >= 0 && ! writeOk); + + return chunk; +} + + + + +///////////////////////// 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; + + while (*bname) + { + sum += (*bname) * i; + i++; + bname++; + } + return sum; +} + + +void yaffs_CalcECC(const __u8 *buffer, yaffs_Spare *spare) +{ + + // Todo do nothing now. Need to put in ecc + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xFF; + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xFF; +} + +void yaffs_CalcTagsECC(yaffs_Tags *tags) +{ + // Todo don't do anything yet. Need to calculate ecc + tags->ecc = 0xFFFFFFFF; +} + + + +///////////////////////// 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) + { + YALERT("Could not malloc tnodes"); + return YAFFS_FAIL; + } + + // Hook them into the free list + for(i = 0; i < nTnodes - 1; i++) + { + newTnodes[i].internal[0] = &newTnodes[i+1]; + } + + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes; + dev->freeTnodes = newTnodes; + dev->nFreeTnodes+= nTnodes; + dev->nTnodesCreated += nTnodes; + + // Now add this bunch of tnodes to a list for freeing up. + + tnl = YMALLOC(sizeof(yaffs_TnodeList)); + if(!tnl) + { + YALERT("Could not add tnodes to management list"); + } + else + { + tnl->tnodes = newTnodes; + tnl->next = dev->allocatedTnodeList; + dev->allocatedTnodeList = tnl; + } + + + YINFO("Tnodes created"); + + + 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; + 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) +{ + tn->internal[0] = dev->freeTnodes; + dev->freeTnodes = tn; + dev->nFreeTnodes++; +} + + +static void yaffs_DeinitialiseTnodes(yaffs_Device*dev) +{ + // Free the list of allocated tnodes + + while(dev->allocatedTnodeList) + { + YFREE(dev->allocatedTnodeList->tnodes); + dev->allocatedTnodeList = dev->allocatedTnodeList->next; + } + + 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; + +} + +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; + } + + } +} + +////////////////// 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; + + __u32 i; + __u32 l; + + + T(("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) + + i = chunkId >> (dev->chunkGroupBits + YAFFS_TNODES_LEVEL0_BITS); + requiredTallness = 0; + while(i) + { + i >>= YAFFS_TNODES_INTERNAL_BITS; + requiredTallness++; + } + + T((" required=%d",requiredTallness)); + + + if(requiredTallness > fStruct->topLevel) + { + // Not tall enough,gotta make the tree taller + for(i = fStruct->topLevel; i < requiredTallness; i++) + { + T((" add new top")); + + tn = yaffs_GetTnode(dev); + + if(tn) + { + tn->internal[0] = fStruct->top; + fStruct->top = tn; + } + else + { + YALERT("No more tnodes"); + } + } + + fStruct->topLevel = requiredTallness; + } + + + // Traverse down to level 0, adding anything we need + + l = fStruct->topLevel; + tn = fStruct->top; + while (l > 0 && tn) + { + i = (chunkId >> (dev->chunkGroupBits +YAFFS_TNODES_LEVEL0_BITS + (l-1) * YAFFS_TNODES_INTERNAL_BITS)) & + YAFFS_TNODES_INTERNAL_MASK; + + T((" [%d:%d]",l,i)); + + if(!tn->internal[i]) + { + T((" added")); + + tn->internal[i] = yaffs_GetTnode(dev); + } + + tn = tn->internal[i]; + l--; + + } + + T(("\n")); + + return tn; +} + + +// Pruning removes any part of the file structure tree that is beyond the +// bounds of the file. +// 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) + { + YALERT("Could not allocate more objects"); + return YAFFS_FAIL; + } + + // Hook them into the free list + for(i = 0; i < nObjects - 1; i++) + { + (yaffs_Object *)newObjects[i].siblings.next = &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) + { + YALERT("Could not add Objects to management list"); + } + else + { + list->objects = newObjects; + list->next = dev->allocatedObjectList; + dev->allocatedObjectList = list; + } + + + YINFO("Objects created"); + + + 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->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->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; + + yaffs_UnhashObject(tn); + + // Link into the free list. + (yaffs_Object *)(tn->siblings.next) = dev->freeObjects; + dev->freeObjects = tn; + dev->nFreeObjects++; +} + + + + +static void yaffs_DeinitialiseObjects(yaffs_Device *dev) +{ + // Free the list of allocated Objects + + while( dev->allocatedObjectList) + { + YFREE(dev->allocatedObjectList->objects); + dev->allocatedObjectList = dev->allocatedObjectList->next; + } + + 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; + + int n = 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(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,int number) +{ + int bucket = yaffs_HashFunction(number); + struct list_head *i; + yaffs_Object *in; + + list_for_each(i,&dev->objectBucket[bucket].list) + { + // Lookm if it is in the list + 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; + theObject->myDev = dev; + yaffs_HashObject(theObject); + theObject->variantType = type; + theObject->st_atime = theObject->st_mtime = theObject->st_ctime = CURRENT_TIME; + + switch(type) + { + case YAFFS_OBJECT_TYPE_FILE: + theObject->variant.fileVariant.fileSize = 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: + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + break; + case YAFFS_OBJECT_TYPE_UNKNOWN: + // todo this should not happen + } + } + + 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. +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) +{ + yaffs_Object *in; + + yaffs_Device *dev = parent->myDev; + + in = yaffs_CreateNewObject(dev,-1,type); + + if(in) + { + in->chunkId = -1; + in->valid = 1; + in->variantType = type; + + in->st_mode = mode; + in->st_uid = uid; + in->st_gid = gid; + in->st_atime = in->st_mtime = in->st_ctime = CURRENT_TIME; + + in->sum = yaffs_CalcNameSum(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_UNKNOWN: + } + + yaffs_UpdateObjectHeader(in,name); + + } + + 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); +} + +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); +} + +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); +} + +// 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)) + { + return equivalentObject; + } + else + { + return NULL; + } + +} + + +static int yaffs_ChangeObjectName(yaffs_Object *obj, yaffs_Object *newDir, const char *newName) +{ + //yaffs_Device *dev = obj->myDev; + + if(newDir == NULL) + { + newDir = obj->parent; // use the old directory + } + + // Only proceed if the new name does not exist and + // if we're putting it into a directory. + if(!yaffs_FindObjectByName(newDir,newName) && + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) + { + obj->sum = yaffs_CalcNameSum(newName); + obj->dirty = 1; + yaffs_AddObjectToDirectory(newDir,obj); + + return yaffs_UpdateObjectHeader(obj,newName); + } + + return YAFFS_FAIL; +} + +int yaffs_RenameObject(yaffs_Object *oldDir, const char *oldName, yaffs_Object *newDir, const char *newName) +{ + yaffs_Object *obj; + + obj = yaffs_FindObjectByName(oldDir,oldName); + if(obj && obj->renameAllowed) + { + return yaffs_ChangeObjectName(obj,newDir,newName); + } + 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) + { + YALERT("Inode hash inconsistency"); + ok = YAFFS_FAIL; + } + } + + return ok; +} + +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); + } + +} + + + +/////////////////////////// Block Management and Page Allocation /////////////////// + + +static void yaffs_InitialiseBlocks(yaffs_Device *dev) +{ + dev->blockInfo = YMALLOC(dev->nBlocks * sizeof(yaffs_BlockInfo)); + memset(dev->blockInfo,0,dev->nBlocks * sizeof(yaffs_BlockInfo)); + dev->allocationBlock = -1; // force it to get a new one +} + +static void yaffs_DeinitialiseBlocks(yaffs_Device *dev) +{ + YFREE(dev->blockInfo); +} + +// FindDiretiestBlock is used to select the dirtiest block (or close enough) +// for garbage collection. + +static int yaffs_FindDirtiestBlock(yaffs_Device *dev) +{ + + int b = dev->currentDirtyChecker; + + int i; + int dirtiest = -1; + int pagesInUse = 100; // silly big number + + for(i = dev->startBlock; i <= dev->endBlock && pagesInUse > 2 ; i++) + { + b++; + if (b > dev->endBlock) + { + b = dev->startBlock; + } + + if(dev->blockInfo[b].blockState == YAFFS_BLOCK_STATE_FULL && + (dev->blockInfo)[b].pagesInUse < pagesInUse) + { + dirtiest = b; + pagesInUse = (dev->blockInfo)[b].pagesInUse; + } + } + + dev->currentDirtyChecker = b; + + return dirtiest; +} + + +static int yaffs_FindBlockForAllocation(yaffs_Device *dev,int useReserve) +{ + int i; + + if(useReserve && dev->nErasedBlocks < 1) + { + // Hoosterman we've got a problem. + // Can't get space to gc + return -1; + } + else if(!useReserve && dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS) + { + // We are not in GC, so we hold some in reserve so we can get + // a gc done. + } + + // Find an empty block. + + for(i = dev->startBlock; i <= dev->endBlock; i++) + { + + if(dev->blockInfo[i].blockState == YAFFS_BLOCK_STATE_EMPTY) + { + dev->blockInfo[i].blockState = YAFFS_BLOCK_STATE_ALLOCATING; + dev->nErasedBlocks--; + if(dev->nErasedBlocks <= YAFFS_RESERVED_BLOCKS) + { + dev->garbageCollectionRequired = 1; + } + + return i; + } + } + + return -1; +} + + +static void yaffs_BlockBecameDirty(yaffs_Device *dev,int blockNo) +{ + yaffs_BlockInfo *bi = &dev->blockInfo[blockNo]; + + // Mark as dirty, erase it and mark as clean. + bi->blockState = YAFFS_BLOCK_STATE_DIRTY; + yaffs_EraseBlockInNAND(dev,blockNo); + bi->blockState = YAFFS_BLOCK_STATE_EMPTY; + dev->nErasedBlocks++; + bi->pagesInUse = 0; + bi->pageBits = 0; + + T(("Erased block %d\n",blockNo)); +} + + +static int yaffs_AllocateChunk(yaffs_Device *dev,int useReserve) +{ + int retVal; + + if(dev->allocationBlock < 0) + { + // Get next block to allocate off + dev->allocationBlock = yaffs_FindBlockForAllocation(dev,useReserve); + dev->allocationPage = 0; + } + + // Next page please.... + if(dev->allocationBlock >= 0) + { + retVal = (dev->allocationBlock * YAFFS_CHUNKS_PER_BLOCK) + + dev->allocationPage; + dev->blockInfo[dev->allocationBlock].pagesInUse++; + dev->blockInfo[dev->allocationBlock].pageBits |= + (1 << (dev->allocationPage)); + + dev->allocationPage++; + + dev->nFreeChunks--; + + // If the block is full set the state to full + if(dev->allocationPage >= YAFFS_CHUNKS_PER_BLOCK) + { + dev->blockInfo[dev->allocationBlock].blockState = YAFFS_BLOCK_STATE_FULL; + dev->allocationBlock = -1; + } + +#ifdef YAFFS_PARANOID + if(yaffs_CheckChunkErased(retVal) == YAFFS_FAIL) + { + T(("..................Trying to allocate non-erased page %d\n",retVal)); + } +#endif + return retVal; + + } + T(("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!\n")); + + return -1; +} + + +int yaffs_GarbageCollectBlock(yaffs_Device *dev,int block) +{ + int oldChunk; + int newChunk; + __u32 mask; + + + yaffs_Spare spare; + yaffs_Tags tags; + __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + + yaffs_BlockInfo *bi = &dev->blockInfo[block]; + + yaffs_Object *object; + + T(("Collecting block %d n %d bits %x\n",block, bi->pagesInUse, bi->pageBits)); + + for(mask = 1,oldChunk = block * YAFFS_CHUNKS_PER_BLOCK; + mask && bi->pageBits; + mask <<= 1, oldChunk++ ) + { + if(bi->pageBits & mask) + { + + // This page is in use and needs to be copied off + + T(("copying page %x from %d to %d\n",mask,oldChunk,newChunk)); + + yaffs_ReadChunkFromNAND(dev,oldChunk,buffer, &spare); + + yaffs_GetTagsFromSpare(&spare,&tags); + tags.serialNumber++; + yaffs_LoadTagsIntoSpare(&spare,&tags); + +#if 0 + newChunk = yaffs_AllocatePage(dev,1); + if(newChunk < 0) + { + return YAFFS_FAIL; + } + + yaffs_WriteChunkToNAND(dev,newChunk, buffer, &spare); + +#else + newChunk = yaffs_WriteNewChunkToNAND(dev, buffer, &spare,1); +#endif + if(newChunk < 0) + { + return YAFFS_FAIL; + } + + object = yaffs_FindObjectByNumber(dev,tags.objectId); + + // Ok, now fix up the Tnodes etc. + + if(tags.chunkId == 0) + { + // It's a header + object->chunkId = newChunk; + } + else + { + // It's a data chunk + yaffs_PutChunkIntoFile(object, tags.chunkId, newChunk); + + } + + yaffs_DeleteChunk(dev,oldChunk); + + } + } + + return YAFFS_OK; +} + +int yaffs_CheckGarbageCollection(yaffs_Device *dev) +{ + int block; + + if(dev->garbageCollectionRequired) + { + dev->garbageCollectionRequired = 0; + block = yaffs_FindDirtiestBlock(dev); + if(block >= 0) + { + return yaffs_GarbageCollectBlock(dev,block); + } + else + { + return YAFFS_FAIL; + } + } + + return 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_Spare *sparePtr,yaffs_Tags *tagsPtr) +{ + yaffs_TagsUnion *tu = (yaffs_TagsUnion *)tagsPtr; + + 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; + + // Todo Check ECC on tags +} + +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) +{ + if(tags) + { + yaffs_Spare spare; + if(yaffs_ReadChunkFromNAND(dev,chunkInNAND,NULL,&spare) == YAFFS_OK) + { + yaffs_GetTagsFromSpare(&spare,tags); + return YAFFS_OK; + } + else + { + return YAFFS_FAIL; + } + } + + return YAFFS_OK; +} + +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(buffer) + { + yaffs_CalcECC(buffer,&spare); + } + + yaffs_LoadTagsIntoSpare(&spare,tags); + + return yaffs_WriteChunkToNAND(dev,chunkInNAND,buffer,&spare); + +} + +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(buffer) + { + yaffs_CalcECC(buffer,&spare); + } + + yaffs_LoadTagsIntoSpare(&spare,tags); + + return yaffs_WriteNewChunkToNAND(dev,buffer,&spare,useReserve); + +} + + + + +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; + 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; i < dev->chunkGroupSize && !found; i++) + { + yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags); + if(tags->chunkId == chunkInInode && + tags->objectId == in->objectId) + { + // 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; + + 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; i < dev->chunkGroupSize && !found; i++) + { + yaffs_ReadChunkTagsFromNAND(dev,theChunk,tags); + if(tags->chunkId == chunkInInode && + tags->objectId == in->objectId) + { + // 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; +} + + +#if 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; + + + 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 + YAFFS_BYTES_PER_CHUNK -1)/YAFFS_BYTES_PER_CHUNK; + + for(chunk = 1; chunk <= nChunks; chunk++) + { + tn = yaffs_FindLevel0Tnode(&in->variant.fileVariant, chunk); + + if(tn) + { + + theChunk = tn->level0[chunk & YAFFS_TNODES_LEVEL0_MASK] << dev->chunkGroupBits; + + + yaffs_ReadChunkTagsFromNAND(theChunk,tags); + if(tags->chunkId == chunk && + tags->objectId == in->objectId) + { + // 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) +{ + yaffs_Tnode *tn; + yaffs_Device *dev = in->myDev; + + tn = yaffs_AddOrFindLevel0Tnode(dev,&in->variant.fileVariant, chunkInInode); + tn->level0[chunkInInode & YAFFS_TNODES_LEVEL0_MASK] = chunkInNAND; + 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); + } + else + { + return 0; + } + +} + + +static void yaffs_DeleteChunk(yaffs_Device *dev,int chunkId) +{ + int block = chunkId / YAFFS_CHUNKS_PER_BLOCK; + int page = chunkId % YAFFS_CHUNKS_PER_BLOCK; + yaffs_Tags tags; + + // Mark the deleted NAND page as deleted + tags.chunkId = 0; + tags.objectId = 0; + tags.byteCount = 0; + tags.ecc = 0; + + yaffs_WriteChunkWithTagsToNAND(dev,chunkId,NULL,&tags); + + + // Pull out of the management area. + if( dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_ALLOCATING || + dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_FULL) + { + dev->nFreeChunks++; + + dev->blockInfo[block].pageBits &= ~(1 << page); + dev->blockInfo[block].pagesInUse--; + + if( dev->blockInfo[block].pagesInUse == 0 && + dev->blockInfo[block].blockState == YAFFS_BLOCK_STATE_FULL) + { + yaffs_BlockBecameDirty(dev,block); + } + + } + else + { + T(("Bad news deteing 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); + + + #if 0 + // Create new chunk in NAND + newChunkId = yaffs_AllocatePage(dev,useReserve); + + + if(newChunkId >= 0) + { + + + yaffs_WriteChunkWithTagsToNAND(dev,newChunkId,buffer,&newTags); + + yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId); + + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId); + + } + + yaffs_CheckFileSanity(in); + + return newChunkId; + } + + + return -1; +#else + + newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,buffer,&newTags,useReserve); + if(newChunkId >= 0) + { + yaffs_PutChunkIntoFile(in,chunkInInode,newChunkId); + + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId); + + } + + yaffs_CheckFileSanity(in); + } + return newChunkId; + +#endif + + + +} + + +// 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) +{ + + 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) + { + + yaffs_CheckGarbageCollection(dev); + + memset(bufferNew,0xFF,YAFFS_BYTES_PER_CHUNK); + + prevChunkId = in->chunkId; + + if(prevChunkId >= 0) + { + yaffs_ReadChunkFromNAND(dev,prevChunkId,bufferOld,NULL); + } + + // Header data + oh->type = in->variantType; + + oh->st_mode = in->st_mode; + 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->parentObjectId = in->parent->objectId; + 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 + { + memcpy(oh->name, ohOld->name,YAFFS_MAX_NAME_LENGTH + 1); + } + + switch(in->variantType) + { + case YAFFS_OBJECT_TYPE_UNKNOWN: // Todo got a problem + 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_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); + + + +#if 0 + // Create new chunk in NAND + newChunkId = yaffs_AllocatePage(dev,1); + + if(newChunkId >= 0) + { + + yaffs_WriteChunkWithTagsToNAND(dev,newChunkId,bufferNew,&newTags); + + in->chunkId = newChunkId; + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId); + } + + in->dirty = 0; + return newChunkId; + } + + return -1; +#else + // Create new chunk in NAND + newChunkId = yaffs_WriteNewChunkWithTagsToNAND(dev,bufferNew,&newTags,1); + + if(newChunkId >= 0) + { + + in->chunkId = newChunkId; + + if(prevChunkId >= 0) + { + yaffs_DeleteChunk(dev,prevChunkId); + } + + in->dirty = 0; + } + + return newChunkId; + +#endif + } + return 0; +} + + + +///////////////////////// 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) +{ + +// yaffs_Device *dev = in->myDev; + + __u8 localBuffer[YAFFS_BYTES_PER_CHUNK]; + + int chunk; + int start; + int nToCopy; + int n = nBytes; + int nDone = 0; + + 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; + } + + if(nToCopy != YAFFS_BYTES_PER_CHUNK) + { + // An incomplete start or end chunk (or maybe both start and end chunk) + // Read into the local buffer then copy... + + yaffs_ReadChunkDataFromObject(in,chunk,localBuffer); + memcpy(buffer,&localBuffer[start],nToCopy); + } + else + { + // A full chunk. Read directly into the supplied buffer. + yaffs_ReadChunkDataFromObject(in,chunk,buffer); + } + + n -= nToCopy; + offset += nToCopy; + buffer += nToCopy; + nDone += nToCopy; + + } + + return nDone; +} + + +int yaffs_WriteDataToFile(yaffs_Object *in,const __u8 * buffer, __u32 offset, int nBytes) +{ + __u8 localBuffer[YAFFS_BYTES_PER_CHUNK]; + + int chunk; + int start; + int nToCopy; + int n = nBytes; + int nDone = 0; + int nToWriteBack; + int endOfWrite = offset+nBytes; + int chunkWritten = 0; + + 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; + nToWriteBack = (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) + // Read into the local buffer then copy, then copy over and write back. + + yaffs_ReadChunkDataFromObject(in,chunk,localBuffer); + + memcpy(&localBuffer[start],buffer,nToCopy); + + chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,localBuffer,nToWriteBack,0); + + T(("Write with readback to chunk %d %d\n",chunk,chunkWritten)); + + } + else + { + // A full chunk. Write directly from the supplied buffer. + chunkWritten = yaffs_WriteChunkDataToObject(in,chunk,buffer,YAFFS_BYTES_PER_CHUNK,0); + T(("Write to chunk %d %d\n",chunk,chunkWritten)); + } + + if(chunkWritten >= 0) + { + n -= nToCopy; + offset += nToCopy; + buffer += nToCopy; + nDone += nToCopy; + } + + } + + // Update file object + + if(endOfWrite > in->variant.fileVariant.fileSize) + { + in->variant.fileVariant.fileSize = endOfWrite; + } + + in->dirty = 1; + in->st_mtime = CURRENT_TIME; + + return nDone; +} + + +int yaffs_ResizeFile(yaffs_Object *in, int newSize) +{ + int i; + int chunkId; + int oldFileSize = in->variant.fileVariant.fileSize; + int sizeOfLastChunk = newSize % YAFFS_BYTES_PER_CHUNK; + + yaffs_Device *dev = in->myDev; + + __u8 localBuffer[YAFFS_BYTES_PER_CHUNK]; + + if(in->variantType != YAFFS_OBJECT_TYPE_FILE) + { + return yaffs_GetFileSize(in); + } + + if(newSize < oldFileSize) + { + + int lastDel = 1 + oldFileSize/YAFFS_BYTES_PER_CHUNK; + + int startDel = 1 + (newSize + YAFFS_BYTES_PER_CHUNK - 1)/ + YAFFS_BYTES_PER_CHUNK; + + for(i = startDel; i <= lastDel; 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 < 0 || chunkId >= (dev->endBlock * 32)) + { + T(("Found daft chunkId %d for %d\n",chunkId,i)); + } + else + { + yaffs_DeleteChunk(dev,chunkId); + } + } + + + if(sizeOfLastChunk != 0) + { + int lastChunk = 1+ newSize/YAFFS_BYTES_PER_CHUNK; + + // Got to read and rewrite the last chunk with its new size. + yaffs_ReadChunkDataFromObject(in,lastChunk,localBuffer); + + yaffs_WriteChunkDataToObject(in,lastChunk,localBuffer,sizeOfLastChunk,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 retVal; + if(in->dirty) + { + retVal = yaffs_UpdateObjectHeader(in,NULL); + } + else + { + retVal = YAFFS_OK; + } + + return retVal; + +} + + +static int yaffs_DoGenericObjectDeletion(yaffs_Object *in) +{ + yaffs_RemoveObjectFromDirectory(in); + yaffs_DeleteChunk(in->myDev,in->chunkId); + 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_DeleteFile(yaffs_Object *in) +{ + // Delete the file data & tnodes + yaffs_ResizeFile(in,0); + yaffs_FreeTnode(in->myDev,in->variant.fileVariant.top); + + return yaffs_DoGenericObjectDeletion(in); +} + +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 int yaffs_UnlinkWorker(yaffs_Object *obj) +{ + + + if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) + { + return yaffs_DeleteHardLink(obj); + } + else if(!list_empty(&obj->hardLinks)) + { +#if 0 + // Curve ball: We're unlinking an object that has a hardlink. + // Therefore we can't really delete the object. + // Instead, we do the following: + // - Select a hardlink. + // - Re-type a hardlink as the equivalent object and populate the fields, including the + // objectId. Updating the object id is important so that all the hardlinks do not need + // to be rewritten. + // - Update the equivalet object pointers. + // - Delete all object. + + yaffs_Object *hl; + struct list_head *i; + + + yaffs_RemoveObjectFromDirectory(obj); + + + + hl = list_entry(obj->hardLinks.next, yaffs_Object,hardLinks); + + hl->dirty = 1; + hl->st_mode = obj->st_mode; + hl->st_uid = obj->st_uid; + hl->st_gid = obj->st_gid; + hl->st_atime = obj->st_atime; + hl->st_mtime = obj->st_mtime; + hl->st_ctime = obj->st_ctime; + + hl->variantType = obj->variantType; + + switch(hl->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: + case YAFFS_OBJECT_TYPE_SYMLINK: + // These types are OK to just copy across. + hl->variant = obj->variant; + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: + // Fix the list up + list_add(&hl->variant.directoryVariant.children, + &obj->variant.directoryVariant.children); + list_del(&obj->variant.directoryVariant.children); + + // Now change all the directory children to point to the new parent. + list_for_each(i,&hl->variant.directoryVariant.children) + { + list_entry(i,yaffs_Object,siblings)->parent = hl; + } + break; + + case YAFFS_OBJECT_TYPE_HARDLINK: + case YAFFS_OBJECT_TYPE_UNKNOWN: + // Should not be either of these types. + } + + // Now fix up the hardlink chain + list_del(&obj->hardLinks); + + list_for_each(i,&hl->hardLinks) + { + list_entry(i,yaffs_Object,hardLinks)->variant.hardLinkVariant.equivalentObject = hl; + list_entry(i,yaffs_Object,hardLinks)->variant.hardLinkVariant.equivalentObjectId = hl->objectId; + } + + // Now fix up the hash links. + yaffs_UnhashObject(hl); + hl->objectId = obj->objectId; + yaffs_HashObject(hl); + + // Update the hardlink which has become an object + yaffs_UpdateObjectHeader(hl,NULL); + + // Finally throw away the deleted object + yaffs_DeleteChunk(obj->myDev,obj->chunkId); + yaffs_FreeObject(obj); + + return YAFFS_OK; +#else + // 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); + if(retVal == YAFFS_OK) + { + retVal = yaffs_DoGenericObjectDeletion(hl); + } + return retVal; + +#endif + + + } + else + { + switch(obj->variantType) + { + case YAFFS_OBJECT_TYPE_FILE: + return yaffs_DeleteFile(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 ///////////////// + + +static int yaffs_Scan(yaffs_Device *dev) +{ + yaffs_Spare spare; + yaffs_Tags tags; + int blk; + int chunk; + int c; + int deleted; + int inuse; + yaffs_BlockState state; + yaffs_Object *hardList = NULL; + yaffs_Object *hl; + + __u32 pageBits; + + yaffs_ObjectHeader *oh; + yaffs_Object *in; + yaffs_Object *parent; + + __u8 chunkData[YAFFS_BYTES_PER_CHUNK]; + + for(blk = dev->startBlock; blk <= dev->endBlock; blk++) + { + deleted = 0; + pageBits = 0; + inuse = 0; + state = YAFFS_BLOCK_STATE_UNKNOWN; + + for(c = 0; c < YAFFS_CHUNKS_PER_BLOCK && + state == YAFFS_BLOCK_STATE_UNKNOWN; c++) + { + // Read the spare area and decide what to do + chunk = blk * YAFFS_CHUNKS_PER_BLOCK + c; + yaffs_ReadChunkFromNAND(dev,chunk,NULL,&spare); + + + // Is this a valid block? + if(yaffs_countBits[spare.blockStatus] >= 7) + { + // This block looks ok, now what's in this chunk? + yaffs_GetTagsFromSpare(&spare,&tags); + + 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((" allocating %d %d\n",blk,c)); + state = YAFFS_BLOCK_STATE_ALLOCATING; + dev->allocationBlock = blk; + dev->allocationPage = c; + } + + dev->nFreeChunks += (YAFFS_CHUNKS_PER_BLOCK - c); + } + else if(tags.objectId == 0) + { + // A deleted chunk + deleted++; + dev->nFreeChunks ++; + T((" %d %d deleted\n",blk,c)); + } + else if(tags.chunkId > 0) + { + // A data chunk. + inuse++; + pageBits |= ( 1 <type); + if(in->valid) + { + // todo we have already filled this one. We have + // a duplicate. Need to fix + } + + // we don't have a duplicate... + + in->valid = 1; + in->variantType = oh->type; + + in->st_mode = oh->st_mode; + 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->chunkId = chunk; + + in->sum = oh->sum; + 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 + // Todo ... handle + } + + yaffs_AddObjectToDirectory(parent,in); + + // 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: + in->variant.fileVariant.fileSize = oh->fileSize; + break; + case YAFFS_OBJECT_TYPE_HARDLINK: + in->variant.hardLinkVariant.equivalentObjectId = oh->equivalentObjectId; + (yaffs_Object *)(in->hardLinks.next) = hardList; + hardList = in; + break; + case YAFFS_OBJECT_TYPE_DIRECTORY: // 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)); + } + } + else + { + // it's a bad block + state = YAFFS_BLOCK_STATE_DEAD; + } + } + + if(state == YAFFS_BLOCK_STATE_UNKNOWN) + { + // If we got this far, then the block is fully allocated. + // ie. Full or Dirty + state = (inuse) ? YAFFS_BLOCK_STATE_FULL : YAFFS_BLOCK_STATE_DIRTY; + + } + + dev->blockInfo[blk].pageBits = pageBits; + dev->blockInfo[blk].pagesInUse = inuse; + dev->blockInfo[blk].blockState = state; + + } + + // Todo fix up the hard link chains + while(hardList) + { + hl = hardList; + hardList = (yaffs_Object *)(hardList->hardLinks.next); + + in = yaffs_FindObjectByNumber(dev,hl->variant.hardLinkVariant.equivalentObjectId); + + if(in) + { + hl->variant.hardLinkVariant.equivalentObject=in; + list_add(&hl->hardLinks,&in->hardLinks); + } + else + { + //Todo Need to report this better. + hl->variant.hardLinkVariant.equivalentObject=NULL; + INIT_LIST_HEAD(&hl->hardLinks); + + } + + } + + + + 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; +} + +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; + __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *)buffer; + + yaffs_Object *l; + + sum = yaffs_CalcNameSum(name); + + list_for_each(i,&directory->variant.directoryVariant.children) + { + l = list_entry(i, yaffs_Object,siblings); + + // Special case for lost-n-found + if(l->objectId == YAFFS_OBJECTID_LOSTNFOUND) + { + if(strcmp(name,YAFFS_LOSTNFOUND_NAME) == 0) + { + return l; + } + } + else if(l->sum == sum) + { + // Do a real check + yaffs_ReadChunkFromNAND(l->myDev,l->chunkId,buffer,NULL); + if(strcmp(name,oh->name) == 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) + { + l = list_entry(i, yaffs_Object,siblings); + if(!fn(l)) + { + return YAFFS_FAIL; + } + } + + return YAFFS_OK; + +} + + +// GetEquivalentObject dereferences any hard links to get to the +// actual object. + +static 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 + { + __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); + } + 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 = 1; // the object itself + struct list_head *i; + + list_for_each(i,&obj->hardLinks) + { + count++; + } + 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; + 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(""); + } +} + + +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 = attr->ia_atime; + if(valid & ATTR_CTIME) obj->st_ctime = attr->ia_ctime; + if(valid & ATTR_MTIME) obj->st_mtime = attr->ia_mtime; + + if(valid & ATTR_SIZE) yaffs_ResizeFile(obj,attr->ia_size); + + yaffs_UpdateObjectHeader(obj,NULL); + + 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; + + attr->ia_atime = obj->st_atime; valid |= ATTR_ATIME; + attr->ia_ctime = obj->st_ctime; valid |= ATTR_CTIME; + 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; + +} + + + +int yaffs_DumpObject(yaffs_Object *obj) +{ + __u8 buffer[YAFFS_BYTES_PER_CHUNK]; + char name[256]; +// 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); + + YPRINTF(("Object %d \"%s\"\n dirty %d valid %d serial %d sum %d chunk %d type %d size %d\n", + 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 nChunks,x; + int bits; + + + dev = dev; + + if(!yaffs_CheckStructures()) + { + return YAFFS_FAIL; + } + + + // OK now calculate a few things for the device + // Calculate chunkGroupBits. + // If there are 64k or less chunks then this is 1 + // Else it is log2(nChunks) - 16 + // + x = nChunks = YAFFS_CHUNKS_PER_BLOCK * dev->nBlocks; + + for(bits = 0, x = nChunks; (x & 1) == 0; bits++) + { + x >>= 1; + } + + if( x != 1) + { + // Not a power of 2 + YPRINTF(("nBlocks should be a power of 2 but is %u\n", + dev->nBlocks)); + return YAFFS_FAIL; + } + + if(bits <= 16) + { + dev->chunkGroupBits = 0; + dev->chunkGroupSize = 1; + } + else + { + dev->chunkGroupBits = bits - 16; + dev->chunkGroupSize = nChunks/0x10000; + } + + // More device initialisation + dev->garbageCollectionRequired = 0; + dev->currentDirtyChecker = 0; + + yaffs_InitialiseBlocks(dev); + + yaffs_InitialiseTnodes(dev); + + yaffs_InitialiseObjects(dev); + + + // Initialise the root and lost and found directories + dev->lostNFoundDir = dev->rootDir = NULL; + dev->rootDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_ROOT,YAFFS_ROOT_MODE | S_IFDIR); + dev->lostNFoundDir = yaffs_CreateFakeDirectory(dev,YAFFS_OBJECTID_LOSTNFOUND,YAFFS_ROOT_MODE | S_IFDIR); + yaffs_AddObjectToDirectory(dev->rootDir,dev->lostNFoundDir); + + // Now scan the flash. + yaffs_Scan(dev); + + + return YAFFS_OK; + +} + +void yaffs_Deinitialise(yaffs_Device *dev) +{ + yaffs_DeinitialiseBlocks(dev); + yaffs_DeinitialiseTnodes(dev); + yaffs_DeinitialiseObjects(dev); + +} + +int yaffs_GetNumberOfFreeChunks(yaffs_Device *dev) +{ + int nFree = dev->nFreeChunks - (YAFFS_CHUNKS_PER_BLOCK * YAFFS_RESERVED_BLOCKS); + + return (nFree < 0) ? 0 : nFree; + +} + + +/////////////////// YAFFS test code ////////////////////////////////// + +#define yaffs_CheckStruct(structure,syze, name) \ + if(sizeof(structure) != syze) \ + { YPRINTF(("%s should be %d but is %d\n",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") + yaffs_CheckStruct(yaffs_Tnode,2* YAFFS_NTNODES_LEVEL0,"yaffs_Tnode") + yaffs_CheckStruct(yaffs_ObjectHeader,512,"yaffs_ObjectHeader") + + + return YAFFS_OK; +} + +void yaffs_GutsTest(yaffs_Device *dev) +{ + + if(yaffs_CheckStructures() != YAFFS_OK) + { + YPRINTF(("One or more structures malformed-- aborting\n")); + } + else + { + YPRINTF(("Structures OK\n")); + } + + yaffs_TnodeTest(dev); + yaffs_ObjectTest(dev); +} + + diff --git a/yaffs_guts.h b/yaffs_guts.h new file mode 100644 index 0000000..d1e2973 --- /dev/null +++ b/yaffs_guts.h @@ -0,0 +1,431 @@ +/* + * 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 General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#ifndef __YAFFS_GUTS_H__ +#define __YAFFS_GUTS_H__ + +#include "devextras.h" + + +#define YAFFS_OK 1 +#define YAFFS_FAIL 0 + +// Y=0x59, A=0x41, S=0x53 +#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_CHUNK 512 +#define YAFFS_CHUNK_SIZE_SHIFT 9 + +#define YAFFS_BYTES_PER_SPARE 16 + +#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_RESERVED_BLOCKS 5 + +#define YAFFS_OBJECT_SPACE 0x40000 +#define YAFFS_MAX_NAME_LENGTH 255 + +#define YAFFS_MAX_ALIAS_LENGTH 211 + +#define YAFFS_OBJECTID_ROOT 1 +#define YAFFS_ROOT_MODE 0666 +#define YAFFS_OBJECTID_LOSTNFOUND 2 +#define YAFFS_LOSTNFOUND_NAME "lost+found" + +// Tags structures in RAM +// NB This uses bitfield. Bitfields should not stradle 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; + __u8 blockStatus; + __u8 tagByte4; + __u8 tagByte5; + __u8 ecc1[3]; + __u8 tagByte6; + __u8 tagByte7; + __u8 ecc2[3]; +} yaffs_Spare; + +// Block data in RAM + +typedef enum { + YAFFS_BLOCK_STATE_UNKNOWN = 0, + + 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 = 0x99 // This block has failed and is not in use + +} yaffs_BlockState; + + + + +typedef struct +{ + __u32 pageBits; // bitmap of pages in use + __u8 blockState; // One of the above block states + __u8 pagesInUse; // number of pages in use +} 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_ObjectType; + +typedef struct +{ + yaffs_ObjectType type; + + // Apply to everything + int parentObjectId; + __u16 sum; // checksum of name + char name[YAFFS_MAX_NAME_LENGTH + 1]; + + // Thes following apply to directories, files, symlinks - not hard links + __u32 st_mode; // protection + __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 + + // File size applies to files only + int fileSize; + + // Equivalent object id applies to hard links only. + int equivalentObjectId; + char alias[YAFFS_MAX_ALIAS_LENGTH + 1]; + + +} yaffs_ObjectHeader; + + + +//////////////////// Tnode /////////////////////////// + +union yaffs_Tnode_union +{ + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL]; + __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 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 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 + + __u32 objectId; // the object id value + + + __u32 st_mode; // protection + __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 + + + 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; + __u32 count; +} yaffs_ObjectBucket; + + +//////////////////// Device //////////////////////////////// + +struct yaffs_DeviceStruct +{ + // Entry parameters set up way early. Yaffs sets up the rest. + __u32 nBlocks; // Size of whole device in blocks + __u32 startBlock; // Start block we're allowed to use + __u32 endBlock; // End block we're allowed to use + __u16 chunkGroupBits; // 0 for devices <= 32MB. else log2(nchunks) - 16 + __u16 chunkGroupSize; // == 2^^chunkGroupBits + + + void *genericDevice; // Pointer to device context + // On an mtd this holds the mtd pointer. + struct semaphore sem;// Semaphore for waiting on erasure. + + + // 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. + yaffs_BlockInfo *blockInfo; + int nErasedBlocks; + int allocationBlock; + __u32 allocationPage; + + // 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 + + int garbageCollectionRequired; + + yaffs_Object *rootDir; + yaffs_Object *lostNFoundDir; + + +}; + + 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); + +// 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); + + +// 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,int number); + +// Link operations +yaffs_Object *yaffs_Link(yaffs_Object *parent, const char *name, yaffs_Object *equivalentObject); + +// 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 directories +yaffs_Object *yaffs_Root(yaffs_Device *dev); +yaffs_Object *yaffs_LostNFound(yaffs_Device *dev); + + +// Debug dump +int yaffs_DumpObject(yaffs_Object *obj); + + +void yaffs_GutsTest(yaffs_Device *dev); + + +#endif diff --git a/yaffs_mtdif.c b/yaffs_mtdif.c new file mode 100644 index 0000000..3f1e924 --- /dev/null +++ b/yaffs_mtdif.c @@ -0,0 +1,104 @@ +/* + * 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. + * + */ + #ifdef YAFFS_MTD_ENABLED + +#include "yportenv.h" + +#include "yaffs_mtdif.h" + +#include "linux/mtd/mtd.h" +#include "linux/types.h" + + +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; + + loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK; + + + __u8 *spareAsBytes = (__u8 *)spare; + + if(data) + mtd->write(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data); + if(spare) + mtd->write_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes); + + return YAFFS_OK; +} + + +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; + + loff_t addr = ((loff_t)chunkInNAND) * YAFFS_BYTES_PER_CHUNK; + + __u8 *spareAsBytes = (__u8 *)spare; + + if(data) + mtd->read(mtd,addr,YAFFS_BYTES_PER_CHUNK,&dummy,data); + if(spare) + mtd->read_oob(mtd,addr,YAFFS_BYTES_PER_SPARE,&dummy,spareAsBytes); + + return YAFFS_OK; +} + + +static void nandmtd_EraseCallback(struct erase_info *ei) +{ + yaffs_Device *dev = (yaffs_Device *)ei->priv; + up(&dev->sem); +} + + +int nandmtd_EraseBlockInNAND(yaffs_Device *dev, int blockNumber) +{ + + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice); + __u32 addr = ((loff_t) blockNumber) * YAFFS_BYTES_PER_BLOCK; + struct erase_info ei; + + ei.mtd = mtd; + ei.addr = addr; + ei.len = YAFFS_BYTES_PER_BLOCK; + ei.time = 1000; + ei.retries = 2; + ei.callback = nandmtd_EraseCallback; + ei.priv = (u_long)dev; + + // Todo finish off the ei if required + + sema_init(&dev->sem,0); + + mtd->erase(mtd,&ei); + + + down(&dev->sem); // Wait for the erasure to complete + + //Todo, check result of erasure + + return YAFFS_OK; +} + +int nandmtd_InitialiseNAND(yaffs_Device *dev) +{ + return YAFFS_OK; +} + +#endif // YAFFS_MTD_ENABLED + diff --git a/yaffs_mtdif.h b/yaffs_mtdif.h new file mode 100644 index 0000000..6dc2e1b --- /dev/null +++ b/yaffs_mtdif.h @@ -0,0 +1,29 @@ +/* + * 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 General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#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 + + + + diff --git a/yaffs_nandemul.h b/yaffs_nandemul.h new file mode 100644 index 0000000..f233408 --- /dev/null +++ b/yaffs_nandemul.h @@ -0,0 +1,34 @@ +/* + * 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. + * + * yaffs_nandemul.h: Interface to emulated NAND functions + * + */ + +#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 + diff --git a/yaffs_ramem.c b/yaffs_ramem.c new file mode 100644 index 0000000..7143dff --- /dev/null +++ b/yaffs_ramem.c @@ -0,0 +1,291 @@ +/* + * 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. + +#ifdef 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 T(x) YPRINTF(x) + +#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_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; + } + +} + + +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(("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]++; + } + + 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(("Attempt to erase non-existant block %d\n",blockNumber)); + } + else if(ned.block[blockNumber]->damaged) + { + T(("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 + diff --git a/yaffs_super.c b/yaffs_super.c new file mode 100644 index 0000000..04dc17e --- /dev/null +++ b/yaffs_super.c @@ -0,0 +1,401 @@ +/* + * JFFS2 -- Journalling Flash File System, Version 2. + * + * Copyright (C) 2001 Red Hat, Inc. + * + * Created by David Woodhouse + * + * The original JFFS, from which the design for JFFS2 was derived, + * was designed and implemented by Axis Communications AB. + * + * The contents of this file are subject to the Red Hat eCos Public + * License Version 1.1 (the "Licence"); you may not use this file + * except in compliance with the Licence. You may obtain a copy of + * the Licence at http://www.redhat.com/ + * + * Software distributed under the Licence is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. + * See the Licence for the specific language governing rights and + * limitations under the Licence. + * + * The Original Code is JFFS2 - Journalling Flash File System, version 2 + * + * Alternatively, the contents of this file may be used under the + * terms of the GNU General Public License version 2 (the "GPL"), in + * which case the provisions of the GPL are applicable instead of the + * above. If you wish to allow the use of your version of this file + * only under the terms of the GPL and not to allow others to use your + * version of this file under the RHEPL, indicate your decision by + * deleting the provisions above and replace them with the notice and + * other provisions required by the GPL. If you do not delete the + * provisions above, a recipient may use your version of this file + * under either the RHEPL or the GPL. + * + * $Id: yaffs_super.c,v 1.1 2002-05-20 17:42:08 aleph1 Exp $ + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "nodelist.h" + +#ifndef MTD_BLOCK_MAJOR +#define MTD_BLOCK_MAJOR 31 +#endif + +extern void jffs2_read_inode (struct inode *); +void jffs2_put_super (struct super_block *); +void jffs2_write_super (struct super_block *); +static int jffs2_statfs (struct super_block *, struct statfs *); +int jffs2_remount_fs (struct super_block *, int *, char *); +extern void jffs2_clear_inode (struct inode *); + +static struct super_operations jffs2_super_operations = +{ + read_inode: jffs2_read_inode, +// delete_inode: jffs2_delete_inode, + put_super: jffs2_put_super, + write_super: jffs2_write_super, + statfs: jffs2_statfs, + remount_fs: jffs2_remount_fs, + clear_inode: jffs2_clear_inode +}; + +static int jffs2_statfs(struct super_block *sb, struct statfs *buf) +{ + struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); + unsigned long avail; + + buf->f_type = JFFS2_SUPER_MAGIC; + buf->f_bsize = 1 << PAGE_SHIFT; + buf->f_blocks = c->flash_size >> PAGE_SHIFT; + buf->f_files = 0; + buf->f_ffree = 0; + buf->f_namelen = JFFS2_MAX_NAME_LEN; + + spin_lock_bh(&c->erase_completion_lock); + + avail = c->dirty_size + c->free_size; + if (avail > c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE) + avail -= c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE; + else + avail = 0; + + buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT; + +#if CONFIG_JFFS2_FS_DEBUG > 0 + printk(KERN_DEBUG "STATFS:\n"); + printk(KERN_DEBUG "flash_size: %08x\n", c->flash_size); + printk(KERN_DEBUG "used_size: %08x\n", c->used_size); + printk(KERN_DEBUG "dirty_size: %08x\n", c->dirty_size); + printk(KERN_DEBUG "free_size: %08x\n", c->free_size); + printk(KERN_DEBUG "erasing_size: %08x\n", c->erasing_size); + printk(KERN_DEBUG "bad_size: %08x\n", c->bad_size); + printk(KERN_DEBUG "sector_size: %08x\n", c->sector_size); + + if (c->nextblock) { + printk(KERN_DEBUG "nextblock: 0x%08x\n", c->nextblock->offset); + } else { + printk(KERN_DEBUG "nextblock: NULL\n"); + } + if (c->gcblock) { + printk(KERN_DEBUG "gcblock: 0x%08x\n", c->gcblock->offset); + } else { + printk(KERN_DEBUG "gcblock: NULL\n"); + } + if (list_empty(&c->clean_list)) { + printk(KERN_DEBUG "clean_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->clean_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "clean_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->dirty_list)) { + printk(KERN_DEBUG "dirty_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->dirty_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "dirty_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->erasing_list)) { + printk(KERN_DEBUG "erasing_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->erasing_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "erasing_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->erase_pending_list)) { + printk(KERN_DEBUG "erase_pending_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->erase_pending_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "erase_pending_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->free_list)) { + printk(KERN_DEBUG "free_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->free_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "free_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->bad_list)) { + printk(KERN_DEBUG "bad_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->bad_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "bad_list: %08x\n", jeb->offset); + } + } + if (list_empty(&c->bad_used_list)) { + printk(KERN_DEBUG "bad_used_list: empty\n"); + } else { + struct list_head *this; + + list_for_each(this, &c->bad_used_list) { + struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list); + printk(KERN_DEBUG "bad_used_list: %08x\n", jeb->offset); + } + } +#endif /* CONFIG_JFFS2_FS_DEBUG */ + + spin_unlock_bh(&c->erase_completion_lock); + + + return 0; +} + +static struct super_block *jffs2_read_super(struct super_block *sb, void *data, int silent) +{ + struct jffs2_sb_info *c; + struct inode *root_i; + int i; + + D1(printk(KERN_DEBUG "jffs2: read_super for device %s\n", kdevname(sb->s_dev))); + + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) { + if (!silent) + printk(KERN_DEBUG "jffs2: attempt to mount non-MTD device %s\n", kdevname(sb->s_dev)); + return NULL; + } + + c = JFFS2_SB_INFO(sb); + memset(c, 0, sizeof(*c)); + + c->mtd = get_mtd_device(NULL, MINOR(sb->s_dev)); + if (!c->mtd) { + D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev))); + return NULL; + } + c->sector_size = c->mtd->erasesize; + c->free_size = c->flash_size = c->mtd->size; + c->nr_blocks = c->mtd->size / c->mtd->erasesize; + c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL); + if (!c->blocks) + goto out_mtd; + for (i=0; inr_blocks; i++) { + INIT_LIST_HEAD(&c->blocks[i].list); + c->blocks[i].offset = i * c->sector_size; + c->blocks[i].free_size = c->sector_size; + c->blocks[i].dirty_size = 0; + c->blocks[i].used_size = 0; + c->blocks[i].first_node = NULL; + c->blocks[i].last_node = NULL; + } + + spin_lock_init(&c->nodelist_lock); + init_MUTEX(&c->alloc_sem); + init_waitqueue_head(&c->erase_wait); + spin_lock_init(&c->erase_completion_lock); + spin_lock_init(&c->inocache_lock); + + INIT_LIST_HEAD(&c->clean_list); + INIT_LIST_HEAD(&c->dirty_list); + INIT_LIST_HEAD(&c->erasing_list); + INIT_LIST_HEAD(&c->erase_pending_list); + INIT_LIST_HEAD(&c->erase_complete_list); + INIT_LIST_HEAD(&c->free_list); + INIT_LIST_HEAD(&c->bad_list); + INIT_LIST_HEAD(&c->bad_used_list); + c->highest_ino = 1; + + if (jffs2_build_filesystem(c)) { + D1(printk(KERN_DEBUG "build_fs failed\n")); + goto out_nodes; + } + sb->s_op = &jffs2_super_operations; + + D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n")); + root_i = iget(sb, 1); + if (is_bad_inode(root_i)) { + D1(printk(KERN_WARNING "get root inode failed\n")); + goto out_nodes; + } + + D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n")); + sb->s_root = d_alloc_root(root_i); + if (!sb->s_root) + goto out_root_i; + +#if LINUX_VERSION_CODE >= 0x20403 + sb->s_maxbytes = 0xFFFFFFFF; +#endif + sb->s_blocksize = PAGE_CACHE_SIZE; + sb->s_blocksize_bits = PAGE_CACHE_SHIFT; + sb->s_magic = JFFS2_SUPER_MAGIC; + if (!(sb->s_flags & MS_RDONLY)) + jffs2_start_garbage_collect_thread(c); + return sb; + + out_root_i: + iput(root_i); + out_nodes: + jffs2_free_ino_caches(c); + jffs2_free_raw_node_refs(c); + kfree(c->blocks); + out_mtd: + put_mtd_device(c->mtd); + return NULL; +} + +void jffs2_put_super (struct super_block *sb) +{ + struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); + + D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n")); + + if (!(sb->s_flags & MS_RDONLY)) + jffs2_stop_garbage_collect_thread(c); + jffs2_free_ino_caches(c); + jffs2_free_raw_node_refs(c); + kfree(c->blocks); + if (c->mtd->sync) + c->mtd->sync(c->mtd); + put_mtd_device(c->mtd); + + D1(printk(KERN_DEBUG "jffs2_put_super returning\n")); +} + +int jffs2_remount_fs (struct super_block *sb, int *flags, char *data) +{ + struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); + + if (c->flags & JFFS2_SB_FLAG_RO && !(sb->s_flags & MS_RDONLY)) + return -EROFS; + + /* We stop if it was running, then restart if it needs to. + This also catches the case where it was stopped and this + is just a remount to restart it */ + if (!(sb->s_flags & MS_RDONLY)) + jffs2_stop_garbage_collect_thread(c); + + if (!(*flags & MS_RDONLY)) + jffs2_start_garbage_collect_thread(c); + + sb->s_flags = (sb->s_flags & ~MS_RDONLY)|(*flags & MS_RDONLY); + + return 0; +} + +void jffs2_write_super (struct super_block *sb) +{ + struct jffs2_sb_info *c = JFFS2_SB_INFO(sb); + sb->s_dirt = 0; + + if (sb->s_flags & MS_RDONLY) + return; + + jffs2_garbage_collect_trigger(c); + jffs2_erase_pending_blocks(c); + jffs2_mark_erased_blocks(c); +} + + +static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super); + +static int __init init_jffs2_fs(void) +{ + int ret; + + printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n"); + +#ifdef JFFS2_OUT_OF_KERNEL + /* sanity checks. Could we do these at compile time? */ + if (sizeof(struct jffs2_sb_info) > sizeof (((struct super_block *)NULL)->u)) { + printk(KERN_ERR "JFFS2 error: struct jffs2_sb_info (%d bytes) doesn't fit in the super_block union (%d bytes)\n", + sizeof(struct jffs2_sb_info), sizeof (((struct super_block *)NULL)->u)); + return -EIO; + } + + if (sizeof(struct jffs2_inode_info) > sizeof (((struct inode *)NULL)->u)) { + printk(KERN_ERR "JFFS2 error: struct jffs2_inode_info (%d bytes) doesn't fit in the inode union (%d bytes)\n", + sizeof(struct jffs2_inode_info), sizeof (((struct inode *)NULL)->u)); + return -EIO; + } +#endif + + ret = jffs2_create_slab_caches(); + if (ret) { + printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n"); + return ret; + } + ret = register_filesystem(&jffs2_fs_type); + if (ret) { + printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n"); + jffs2_destroy_slab_caches(); + } + return ret; +} + +static void __exit exit_jffs2_fs(void) +{ + jffs2_destroy_slab_caches(); + unregister_filesystem(&jffs2_fs_type); +} + +module_init(init_jffs2_fs); +module_exit(exit_jffs2_fs); + +MODULE_DESCRIPTION("The Journalling Flash File System, v2"); +MODULE_AUTHOR("Red Hat, Inc."); +MODULE_LICENSE("GPL"); // Actually dual-licensed, but it doesn't matter for + // the sake of this tag. It's Free Software. +/* + * 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. + * + */ diff --git a/yaffsdev b/yaffsdev new file mode 100755 index 0000000..6e51051 Binary files /dev/null and b/yaffsdev differ diff --git a/yaffsdev.c b/yaffsdev.c new file mode 100644 index 0000000..64368f4 --- /dev/null +++ b/yaffsdev.c @@ -0,0 +1,303 @@ +/* + * 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 "yaffs_nandemul.h" +#include "yaffs_guts.h" +#include + +#include +#include + + + + + +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); + + 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\n"); + yaffs_ResizeFile(f,2000); + + yaffs_DumpObject(f); + + yaffs_FlushFile(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("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_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + printf("Flush\n"); + + yaffs_FlushFile(f); + + printf("File length is %d\n",yaffs_GetObjectFileLength(f)); + + sl = yaffs_MknodSymLink(yaffs_Root(dev),"sym-link",0,0,0,"/tmp/alias"); + + + 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\n"); + yaffs_ResizeFile(f,2000); + + yaffs_DumpObject(f); + + lnf = yaffs_FindObjectByName(yaffs_Root(dev),YAFFS_LOSTNFOUND_NAME); + + + + yaffs_FlushFile(f); + + + printf("Unlink file: %d\n",yaffs_Unlink(yaffs_Root(dev),"Rename")); + + yaffs_ApplyToDirectoryChildren(yaffs_Root(dev),yaffs_DumpObject); + + // Create a directory and play with it + + + printf("Create directory and play with it\n"); + + 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); + +} + +int main(int argc,char *argv[]) +{ + device.nBlocks = (2 * 1024 * 1024) / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK); + device.startBlock = 1; // Don't use block 0 + device.endBlock = device.nBlocks - 1; + + device.writeChunkToNAND = nandemul_WriteChunkToNAND; + device.readChunkFromNAND = nandemul_ReadChunkFromNAND; + device.eraseBlockInNAND = nandemul_EraseBlockInNAND; + device.initialiseNAND = nandemul_InitialiseNAND; + + yaffs_GutsInitialise(&device); + + // yaffs_GutsTest(); + + TestTime(&device); + + exit(0); +} diff --git a/yaffsdev.proj b/yaffsdev.proj new file mode 100644 index 0000000..2335511 Binary files /dev/null and b/yaffsdev.proj differ diff --git a/yaffsinterface.h b/yaffsinterface.h new file mode 100644 index 0000000..972a04d --- /dev/null +++ b/yaffsinterface.h @@ -0,0 +1,23 @@ +/* + * 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 General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#ifndef __YAFFSINTERFACE_H__ +#define __YAFFSINTERFACE_H__ + + +int yaffs_Initialise(unsigned nBlocks); + +#endif + diff --git a/yportenv.h b/yportenv.h new file mode 100644 index 0000000..fc84fc5 --- /dev/null +++ b/yportenv.h @@ -0,0 +1,56 @@ +/* + * 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 General Public License version 2 as + * published by the Free Software Foundation. + * + */ + +#ifndef __PORTENV_H__ +#define __PORTENV_H__ + +#ifndef __KERNEL__ + +#include "stdlib.h" +#include "stdio.h" +#include "string.h" + +#define YPRINTF(x) printf x +#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) + +#else + +#include "linux/kernel.h" +#include "linux/mm.h" +#include "linux/string.h" +#include "linux/slab.h" + +#define YPRINTF(x) printk x +#define YMALLOC(x) kmalloc(x,GFP_KERNEL) +#define YFREE(x) kfree(x) + +#endif + + + +#define YINFO(s) YPRINTF((KERN_DEBUG __FILE__ " %d %s\n",__LINE__,s)) +#define YALERT(s) YINFO(s) + +#endif + + + +