('paper',0,409,524,'und',0,'<p>&nbsp;</p>\r\n\r\n<h2>1 Purpose</h2>\r\n\r\n<p>The purpose of this document is to give a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical.</p>\r\n\r\n<p>This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.</p>\r\n\r\n<p>This document is updated periodically. Yaffs mechanism design changes from time to time and this document will not always reflect the most up to date code.</p>\r\n\r\n<h2>2 What is Yaffs?</h2>\r\n\r\n<p>Yaffs stands for <em>Yet Another Flash File System</em>, a term coined by Charles Manning in 2001 when suggesting that the already cluttered flash file system space could do with yet another offering – this time a flash file system designed from the ground up to work with NAND flash.</p>\r\n\r\n<p>The first release of Yaffs was developed during the end of 2001 and the beginning of 2002. By mid-2002, Yaffs was being trialled in various products. In May 2002, Yaffs was announced and a few more projects started playing with it. In June, porting to other OSs started. In September 2002, Yaffs was announced on linuxdevices.com which started a far wider uptake of Yaffs.</p>\r\n\r\n<p>Yaffs1, the original version of Yaffs, supported 512-byte page NAND devices with a SmartMedia-like memory layout.</p>\r\n\r\n<p>Yaffs2 is a follow-up to Yaffs1 extending Yaffs1 to support larger and different devices with different constraints.</p>\r\n\r\n<p>Yaffsis highly portable and has been used in many different products and applications as varied as sewing machines, point of sale, phones and aerospace. Yaffs has been used with multiple different operating systems. Native support is available for Linux, WindowsCE and eCOS while Yaffs Direct Interface provides a layer that can be used in other applications such as RTOSs. Yaffs has been used with multiple different CPUs and compilers.</p>\r\n\r\n<p>Yaffs has also been used as a NOR file system and even as a RAM file system.</p>\r\n\r\n<p>3 Design and coding strategies</p>\r\n\r\n<p>Yaffs is designed to work in multiple environments which drives the need for portability. Portability also improves the debugging and testing since it is easier to develop and debug code in an application environment than within an OS kernel. This generally allows Yaffs to progress faster with less programming resources and provides more varied testing. Primary strategies that improve portability include:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>No OS-specific features used in main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p>No compiler-specific features used in the main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p>Abstract types and functions used to allow Unicode or ASCII operation.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Simplicity is another key goal. Complexity is limited to those situations that provide significant improvements to performance or utility. Simplicity improves robustness and the ease of integration and development. Primary strategies that improve simplicity are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Single threaded model. Yaffs is locked on a per-partition basis at a high level. This is simpler than tracking lower-level locking. Yaffs Direct Interface uses a single lock for all partitions.</p>\r\n	</li>\r\n</ul>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Log structure makes for a simpler garbage collector and allocation methodology.</p>\r\n	</li>\r\n	<li>\r\n	<p>Abstractions that build layered code which is easier to understand, port and debug.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2>4 Terminology: Yaffs vs Yaffs1 vs Yaffs2</h2>\r\n\r\n<p>Before we embark on our quest to understand what Yaffs is, a terminology explanation is in order.</p>\r\n\r\n<p>There are two versions of Yaffs code: the original Yaffs code-base and the current Yaffs2 code-base. The Yaffs2 code-base supports functionality provided by the Yaffs code-base as well as being extended with new functionality to provide extra modes of operation, most importantly those required to work with larger and more modern NAND parts. The Yaffs2 code-base supports the Yaffs1 mode of operation through a backward compatibility mode.</p>\r\n\r\n<p>Unless explicitly mentioned, this text refers to the Yaffs2 code-base.</p>\r\n\r\n<p>This text uses three terms Yaffs, Yaffs1 and Yaffs2 depending on various modes of operation.</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Yaffs1 is a simpler mode of operation that uses deletion markers to track state.</p>\r\n	</li>\r\n	<li>\r\n	<p>Yaffs2 is a more complex mode of operating that was developed to support larger flash types that cannot used deletion markers.</p>\r\n	</li>\r\n	<li>\r\n	<p>Yaffs means operations common to both.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Yaffs1 originally only worked with 512-byte page devices but has been extended to support larger flash pages such as Intel M18 flash with 1k pages.</p>\r\n\r\n<p>Yaffs2 was originally designed for 1k or larger page sizes but can be used with smaller pages by using in-band tags.</p>\r\n\r\n<p>Since the Yaffs1 mode of operation is simpler to understand than the Yaffs2 mode of operation, Yaffs1 will be described first and readers interested in Yaffs2 operation should first fully acquaint themselves with the Yaffs1 mode of operation. It is suggested that this document be read sequentially rather than browsing it.</p>\r\n\r\n<h2>5 Objects</h2>\r\n\r\n<p>In Yaffs-speak, an Object is anything that is stored in the file system. These are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Regular data files</p>\r\n	</li>\r\n	<li>\r\n	<p>Directories</p>\r\n	</li>\r\n	<li>\r\n	<p>Hard-links</p>\r\n	</li>\r\n	<li>\r\n	<p>Symbolic links</p>\r\n	</li>\r\n	<li>\r\n	<p>Special objects (pipes, devices etc).</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>All objects are identified by a unique integer <em><strong>object Id</strong></em>.</p>\r\n\r\n<p>In the standard POSIX model, as used by Linux, there are inodes and directory entries (dentries).</p>\r\n\r\n<p>An inode may be a regular file, directory, or special file.</p>\r\n\r\n<p>Directory entries provide the naming mechanism that is used to locate inodes.</p>\r\n\r\n<p>Under POSIX, each inode may have zero, one or many dentries:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>One dentry per inode is commonly understood.</p>\r\n	</li>\r\n	<li>\r\n	<p>Multiple dentries per inode allow the same inode to be accessed via multiple names. This is achieved through hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p>Zero dentries per inode is less often understood. This happens when an inode is unlinked while there is still a handle open to the inode. The unlinking deletes the dentry but the inode still exists. As soon as the handle is closed the inode is deleted too.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2>6 The Yaffs NAND model</h2>\r\n\r\n<p>The memory in NAND flash is arranged in <em><strong>pages.</strong></em> A page is the unit of allocation and programming. In Yaffs, the unit of allocation is the<em> </em><em><strong>chunk</strong></em>. Typically a chunk will be the same as a NAND page, but there is flexibility to use chunks which map to multiple pages (eg. A system may have two NAND chips in parallel requiring 2x512 = 1024 byte chunks) . This distinction gives a lot of flexibility in how the system can be configured. In this text the term page and chunk are synonymous unless stated otherwise.</p>\r\n\r\n<p>Many, typically 32 to 128 but as many as a few hundred, chunks form a <em><strong>block</strong></em>. A block is the unit of erasure.</p>\r\n\r\n<p>NAND flash may be shipped with bad blocks and further blocks may go bad during the operation of the device. Thus, Yaffs is aware of bad blocks and needs to be able to detect and mark bad blocks.</p>\r\n\r\n<p>NAND flash also typically requires the use of some sort of error detection and correction code (ECC). Yaffs can either use existing ECC logic or provide its own.</p>\r\n\r\n<h2>7 How Yaffs1 stores files</h2>\r\n\r\n<p>Yaffs1 has a modified log structure, while Yaffs2 (see below) has a true log structure. A true log structured file system only ever writes sequentially. Yaffs1 uses deletion markers, which breaks the sequential write rule. Yaffs2 does not use deletion markers and is thus a true log structured file system.</p>\r\n\r\n<p>Instead of writing data in locations specific to the files, the file system data is written in the form of a sequential log, The entries in the log are all one chunk in size and can hold one of two types of chunk:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>Data chunk</strong></em>: A chunk holding regular data file contents.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Object Header</strong></em>: A descriptor for an object (directory, regular data file, hard link, soft link, special descriptor,...). This holds details such as the identifier for the parent directory, object name, etc.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Each chunk has <em><strong>tags</strong></em> associated with it. The tags comprise the following important fields (others being ignored for now):</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>ObjectId</strong></em>: Identifies which object the chunk belongs to.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>ChunkId</strong></em>: Identifies where in the file this chunk belongs. A chunkId of zero signifies that this chunk contains an objectHeader. ChunkId==1 signifies the first chunk in the file (ie. At file offset 0), chunkId==2 is the next chunk and so on.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Deletion Marker:</strong></em> (Yaffs1 only) Shows that this chunk is no longer in use.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Byte Count</strong></em>: Number of bytes of data if this is a data chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Serial Number</strong></em>: (Yaffs1 only) Serial number used to differentiate chunks with the same objectId and chunkId.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Now let\'s bring that all together in an explanation. This is a simplified explanation and skips over issues such as Soft Deletion (mentioned later in the document). For explanation purposes, we shall consider usage of fictitious NAND type that has 4 chunks per block, starting with an empty (erased) flash.</p>\r\n\r\n<p>First we\'ll create a file.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNext we write a few chunks of data to the file.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNext we close the file. This writers a new object header for the file. Notice how the previous object header is deleted.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header (length 0)</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header (length n)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nLet\'s now open the file for read/write, overwrite part of the first chunk in the file and close the file. The replaced data and object header chunks become deleted.<br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNow let\'s resize the file to zero by opening the file with O_TRUNC and closing the file. This writes a new object header with length 0 and the contents are pruned making the data chunks deleted.</p>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Delted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header (length 0).</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNote how all the pages in block 1 are now marked as deleted. This means that block 1 no longer contains useful information so we can now erase block 1 and reuse the space.</p>\r\n\r\n<p>We will now rename the file. To do this we write a new object header for the file<br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header showing new name</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nBlock 2 now contains only deleted chunks and can be erased and reused.</p>\r\n\r\n<p>Note how the tags tell us:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Which chunks belong to which object</p>\r\n	</li>\r\n	<li>\r\n	<p>The position of the chunks within a file,</p>\r\n	</li>\r\n	<li>\r\n	<p>Which chunks are current</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>With that information we can recreate the state of the files regardless of the placement of the chunks in NAND. This means that should the system fail (crash, power fail,...) at any point during the sequences above, we are still able to recover the file system state up to that point.</p>\r\n\r\n<ul>\r\n</ul>\r\n\r\n<p>Note that there are no file allocation tables or similar structures stored in the flash. This reduces the amount of writing and erasing (improving write performance) while also improving robustness. Corruption of file allocation tables and similar structures is a common failure mechanism for embedded file systems. The lack of such structures makes Yaffs more robust. Or, to put it another way, you can\'t corrupt what you don\'t store.</p>\r\n\r\n<h2>8 Garbage collection</h2>\r\n\r\n<p>As we have shown so far, when a block is made up only of deleted chunks, that block can be erased and reused. However, consider what will happen if we have many blocks which each have a few current chunks in them. Clearly we cannot erase any of the current blocks or we will destroy data. What we need to do is copy the useful data chunks off a block, deleting the originals and allowing the block to be erased and reused. This process is referred to as <em><strong>garbage collection</strong></em>.</p>\r\n\r\n<p>The Yaffs garbage collection process works as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>Find a block worth collecting (according the the heuristics below). If none found, then exit.</p>\r\n	</li>\r\n	<li>\r\n	<p>Iterate through the chunks in the block. If the chunk is in use then create a new copy and delete the original. Fix up the RAM data structures to reflect the change.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p>Once all chunks in this block have been deleted, the block can be erased ready for reuse.</p>\r\n\r\n<p>Note that although step 2 deletes the original chunk when it gets copied there is not much point in actually programming the deletion marker in flash since we\'re about to erase the block anyway. Thus Yaffs does not program the deletion marker in these cases. If power is lost part way through the operation we can still tell the difference between the original and the copy by looking at the serial number (not that it really matters anyway since the two have the same contents so either can be used).</p>\r\n\r\n<p>The heuristics to determine whether a block is worth collecting are as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>If there are many erased blocks available, then Yaffs does not need to work hard, but will instead only attempt to perform garbage collection of blocks with very few chunks in use. This is termed <em><strong>passive</strong></em> garbage collection.</p>\r\n	</li>\r\n	<li>\r\n	<p>If, however, there are very few erased blocks, Yaffs will work much harder to recover more space and will garbage collect blocks with more chunks in use. This is termed <em><strong>aggressive</strong></em> garbage collection.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p>OK, the passive/aggressive names are a bit silly, but they were contrived around midnight... give me a break!</p>\r\n\r\n<p>If garbage collection is aggressive, the whole block is collected in a single garbage collection cycle. If the collection is passive then the number of copies is reduced thus spreading the effort over many garbage collection cycles. This is done to reduce garbage collection load and improve responsiveness.</p>\r\n\r\n<p>The rationale behind the above heuristics is to delay garbage collection when possible to reduce the amount of collection that needs to be performed, thus increasing average system performance. Yet there is a conflicting goal of trying to spread the garbage collection so that it does not all happen at the same causing fluctuations in file system throughput. These conflicting goals make garbage tuning quite challenging.</p>\r\n\r\n<p>All flash file systems need some sort of garbage collector to reclaim space. The garbage collector is often a determining factor in the performance of the flash file system. Many file systems have to do a significant amount of effort in a single collection sweep. Yaffs only deals with one block at a time which constrains the amount of effort in a garbage collection cycle, thus reducing the “stall time”.</p>\r\n\r\n<p>The Yaffs garbage collection algorithm is under review with the goal of reducing “stall time” and increasing throughput.</p>\r\n\r\n<h2>9 Yaffs1 serial numbers</h2>\r\n\r\n<p>In Yaffs1, each chunk is marked with a 2-bit serial number that is incremented every time a chunk with the same tags value is replaced (eg. When a chunk is overwritten or when copied by garbage collection).</p>\r\n\r\n<p>Now let\'s suppose first that we did not have a serial number.</p>\r\n\r\n<p>Let\'s consider a rename where an object header chunk must be replaced with a new one with the new name. Yaffs could do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Delete the old chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p>Write the new chunk.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If the system fails after the delete, the system could end up with no chunks matching the relevant tags values. This could cause files to be lost (end up in lost+found). Thus, we have to write the new chunk before deleting the old chunk.</p>\r\n\r\n<p>But now consider what might happen if we did this:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Write new chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p>Delete old chunk.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If the system failed after the write we now have two chunks that match the tags. How can we tell which one is current?</p>\r\n\r\n<p>The current chunk is determined by inspecting the serial number. Since the serial number is only incremented by one before the old chunk is deleted, a 2-bit serial number is sufficient. Valid pairs are:</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Old</p>\r\n			</td>\r\n			<td>\r\n			<p>New</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>00</p>\r\n			</td>\r\n			<td>\r\n			<p>01</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>01</p>\r\n			</td>\r\n			<td>\r\n			<p>10</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>10</p>\r\n			</td>\r\n			<td>\r\n			<p>11</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>11</p>\r\n			</td>\r\n			<td>\r\n			<p>00</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<p>Thus, by inspecting the serial number attached to the old and new tags the current chunk can be determined and the old chunk can be deleted.</p>\r\n\r\n<h2>10 Yaffs2 NAND model</h2>\r\n\r\n<p>Yaffs2 is an extension of Yaffs designed to fulfill a new set of objectives to work with newer NAND types including:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Zero overwrites. Yaffs1 only ever overwrites a single byte in the spare area of the chunks to set the deletion marker. More modern NAND devices are less tolerant of overwrites and Yaffs2 performs no overwrites at all.</p>\r\n	</li>\r\n	<li>\r\n	<p>Sequential chunk writing within a block. More modern NAND reliability specifications tend to assume sequential writing. Since Yaffs2 does not write deletion markers, the writing of chunks within a block is strictly sequential.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Since Yaffs2 performs less writes, there is potential for Yaffs2 to have improved write performance.</p>\r\n\r\n<p>Yaffs2 cannot use a deletion marker because that would violate the zero overwrites mandate and alternate mechanisms are provided to determine which chunks are current and which are deleted. These mechanisms are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>Sequence Number</strong></em>: As each block is allocated, the file system\'s sequence number is incremented and each chunk in the block is marked with that sequence number. The sequence number thus provides a way of organising the log in chronological order.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Shrink Header Marker</strong></em>: The shrink header marker is used to mark object headers that are written to shrink the size of a data file. This is explained more in the accompanying text.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Note: The Yaffs2 sequence number is not the same as the Yaffs1 serial number!</p>\r\n\r\n<p>Note: We still refer to chunk deletion in Yaffs2. The chunks are marked as deleted in the RAM data structures used by garbage collection etc, but there is no deletion marker written to flash.</p>\r\n\r\n<p>The sequence number allows the chronological order to be determined which allows Yaffs to determine the sequence of events and restore the file system state. This is achieved by scanning <em><strong>backwards</strong></em> chronologically (ie. From the highest sequence number backwards to the lowest sequence number). Thus:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Since we\'re scanning backwards, the most recently written – and thus current – chunk matching an objectId:chunkId pair will be encountered first and all subsequent matching chunks must be obsolete and treated as deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p>The file length in object headers are used to trim back resized files. If an object header is encountered, then subsequent data chunks that lie beyond that file length are clearly obsolete and treated as deleted. Note that both current and obsolete object header sizes must be used to obtain an accurate reconstruction.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Explaining the purpose of the shrink header marker is a bit convoluted. The purpose of a shrink header marker is to show that this object header indicates that a file has shrunken in size, and to prevent those object headers from being deleted by garbage collection. To understand this, it is easiest to consider what would happen if the shrink header markers did not exist.</p>\r\n\r\n<p>For simplification we\'ll assume no garbage collection happens during this part of the exercise. It is safe to make that assumption since it does not alter the reasoning.</p>\r\n\r\n<p>Consider a file that has been through the following:</p>\r\n\r\n<p><code>h = open(“foo”,O_CREAT| O_RDWR, S_IREAD|S_IWRITE); /* create file */<br />\r\nwrite(h,data,51024*1024); /* write 5 MB of data /<br />\r\ntruncate(h,1024*1024); /* truncate to 1MB */<br />\r\nlseek(h,2*1024*1024, SEEK_SET); /* set the file access position to 2MB */<br />\r\nwrite(h,data,1024*1024); /* write 1MB of data */<br />\r\nclose(h);3</code><br />\r\n&nbsp;</p>\r\n\r\n<p>The file will now be 3MB in length, but there will be a “hole” between 1MB and 2MB. According to POSIX (and to aid security etc) that “hole” should always read back as zeros.</p>\r\n\r\n<p>Yaffs2 would represent that in NAND with the following sequence of chunks:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>Object header for file creation (file length = 0)</p>\r\n	</li>\r\n	<li>\r\n	<p>5MB worth of data chunks (0 to 5MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>Object header for truncation (file length = 1MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>1MB worth of data chunks (2MB to 3MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>Object header for close (file length = 3MB)</p>\r\n\r\n	<p>At this stage, only the the following data chunks are current:</p>\r\n	</li>\r\n</ol>\r\n\r\n<ul>\r\n	<li>\r\n	<p>The first 1MB of data created in step 2 above.</p>\r\n	</li>\r\n	<li>\r\n	<p>The 1MB of data created in step 4 above.</p>\r\n	</li>\r\n	<li>\r\n	<p>The object header created in step 5 above.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>But Yaffs must remember the truncation that happened in step 3, otherwise Yaffs will “forget” that there is a hole, which would cause the old data to be visible again. Thus, if Yaffs creates a hole it first writes a shrink header to indicate the start of the hole and a regular object header to mark the end of the hole.</p>\r\n\r\n<p>The shrink header modifies the behavior of the garbage collector to ensure that these shrink headers are not erased until it is safe to do so. This unfortunately has a negative impact on garbage collection in systems which make extensive use of files with holes.</p>\r\n\r\n<p>Shrink headers are also used to indicate that a file has been deleted and that the record of the file deletion is not lost.</p>\r\n\r\n<h3>10.1&nbsp;Yaffs2 extended tags</h3>\r\n\r\n<p>Yaffs2 extends the tags in object headers with extra fields to improve mount scanning performance. The operation of extended tags is described later in this document.</p>\r\n\r\n<h2>11 Bad block handling NAND error handling</h2>\r\n\r\n<p>NAND flash is designed to be very low cost and very high density. In order to get high yields, and thus keep costs down, NAND flash is typically shipped with a few bad blocks and some further blocks are expected to go bad during the use of the device. Thus, any flash file system without an effective bad block handling policy is unsuitable for NAND flash.</p>\r\n\r\n<p>Yaffs1 uses the Smart-Media style of bad block marking. This checks the sixth byte (byte 5) of the spare area. In a good block this should be 0xFF. A factory-marked bad block should be 0x00. If Yaffs1 determines that a block has gone bad it is marked bad with 0x59 (\'Y\'). Using a distinctive marker allows Yaffs-marked bad blocks to be differentiated from factory-marked bad blocks.</p>\r\n\r\n<p>Yaffs2 mode is designed to support a wider range of devices and spare-area layouts. Thus, Yaffs2 does not decide which bytes to mark but instead calls driver functions to determine whether the block is bad, or to mark it bad.</p>\r\n\r\n<p>So, when does Yaffs mark a block bad? Yaffs will mark a block bad if a read or write operation fails or if three ECC errors are detected. A block that is marked bad is retired from use, thus improving the robustness of the file system. This policy is suitable for SLC flash. MLC policies are under development.</p>\r\n\r\n<p>Further, NAND flash cells can sometimes be disturbed (corrupted) by NAND activity and charge loss. These errors are corrected by error correction codes (ECC) which may be implemented in hardware, software drivers, or within Yaffs itself. Again, any flash file system lacking effective ECC handling is unsuitable for NAND flash.</p>\r\n\r\n<p>Yaffs1 mode can use either the built in ECC or use ECC provided by the driver or hardware.</p>\r\n\r\n<p>Since Yaffs2 mode is designed for a wider range of devices, it does not provide ECC internally but requires that the driver provide the ECC.</p>\r\n\r\n<p>The ECC code supplied with Yaffs (yaffs_ecc.c) is the fastest C code implementation of a Smart Media compatible ECC algorithm that we are aware of. This code will correct any single bit error within a 256-byte data block and detect 2 errors per 256-byte data block. This is sufficient error correction to provide very high reliability on SLC-type NAND flash.</p>\r\n\r\n<p>For a more complete description of failure handling, please refer to the Yaffs error mitigation document.</p>\r\n\r\n<h2>12 RAM structures</h2>\r\n\r\n<p>While it is theoretically possible to use a log structured file system with very few RAM structures, this would provide reduce performance. Thus, significant RAM data structures are required to store enough information to provide sufficiently high performance.</p>\r\n\r\n<p>Before we jump in, it is a good idea to first figure out what purpose the RAM structures serve. The main structures are defined in yaffs_guts.h and their main purposes, are :</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Device/partition: This is named <em><strong>yaffs_dev</strong></em>. This is required to hold information relating to a Yaffs “partition” or “mount point”. Using these, rather than globals, allows Yaffs to simultaneously support multiple partitions and partitions of different types. In fact, almost other data structures mentioned here are part of, or accessed via, the structure.</p>\r\n	</li>\r\n	<li>\r\n	<p>NAND Block Information: This is named <em><strong>yaffs_block_info </strong></em>and holds the current state of the NAND blocks. Each yaffs_Device has an array of these.</p>\r\n	</li>\r\n	<li>\r\n	<p>NAND Chunk Information: This is a bitfield attached to the yaffs_Device that holds the current in-use state of each chunk in the system. There is one bit per chunk in the partition.</p>\r\n	</li>\r\n	<li>\r\n	<p>Object: This is named <em><strong>yaffs_obj</strong></em> and holds the state of an object (see section 3).There is one of these for each object in the file system, where an object is one of a regular file, directory, hard link, symbolic link or special link. There are different variants of yaffs_Objects to reflect the different data required for these different object types. Each object is uniquely identified by an objectId.</p>\r\n	</li>\r\n	<li>\r\n	<p>File structure: For each file object, Yaffs holds a tree which provides the mechanism to find the data chunks in a file. The tree consists of nodes called <em><strong>yaffs_tnode</strong></em> (tree nodes).</p>\r\n	</li>\r\n	<li>\r\n	<p>Directory structure: The directory structure allows objects to be found by name. The directory structure is built from a doubly-linked list that is rooted in the directory and binds together the sibling objects within a directory.</p>\r\n	</li>\r\n	<li>\r\n	<p>Object number hash table: The object number hash table provides a mechanism to find an object from its objectId. The objectId is hashed to select a hash bucket. Each hash bucket has a doubly-linked list of objects that belong in this hash bucket, as well as a count of the objects in the bucket.</p>\r\n	</li>\r\n	<li>\r\n	<p>Cache: Yaffs provides a read/write cache that significantly improves performance for short operations. The size of the cache can be set at run-time.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The usage of these structures is described in more detail below.</p>\r\n\r\n<p>Yaffs makes significant use of doubly-linked lists. These data structures are very useful because they provide rapid insertion, removal and traversal. They do however come at the expense of requiring two pointers rather than just one.</p>\r\n\r\n<h3>12.1 Yaffs objects</h3>\r\n\r\n<p>Each object (file, directory,...) in the system is represented by a yaffs_obj. The yaffs_obj\'s main function is to store most of the object metadata and type-specific information. This means that almost all the information about an object can be accessed immediately without reading flash.</p>\r\n\r\n<p>The metadata includes, amongst others:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>objectId: The number used to identify the object.</p>\r\n	</li>\r\n	<li>\r\n	<p>parent: A pointer to the parent directory. Not all objects have a parent. The root director and special directories for unlinked and deleted files do not have parents so this is NULL.</p>\r\n	</li>\r\n	<li>\r\n	<p>Short name: If the name is short enough to fit in a small fixed-sized array (default 16 characters) then it is stored here otherwise it must be fetched from flash each time it is requested.</p>\r\n	</li>\r\n	<li>\r\n	<p>type: Type of object.</p>\r\n	</li>\r\n	<li>\r\n	<p>Permissions, time, ownership and other attributes</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Depending on the object type, yaffs_obj also stores:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>A tnode tree, file extents etc for data files</p>\r\n	</li>\r\n	<li>\r\n	<p>A directory chain for directories</p>\r\n	</li>\r\n	<li>\r\n	<p>An equivalent object for hard links</p>\r\n	</li>\r\n	<li>\r\n	<p>A string for symbolic links</p>\r\n	</li>\r\n</ul>\r\n\r\n<h3>12.2 Look-up by object id</h3>\r\n\r\n<p>The purpose of this mechanism is to rapidly access an object by its device and object Id. This is required during garbage collection, scanning and similar operations.</p>\r\n\r\n<p>Each yaffs_dev uses a hash table for this purpose. The hash table has 256 “buckets” (tunable), each holding a doubly-linked list of the yaffs_objs in that bucket.</p>\r\n\r\n<p>The hash table\'s hash function is just masking the least significant bits of the object Id. This is a very cheap function that provides reasonable “spread”.</p>\r\n\r\n<p>Yaffs makes an attempt to assign objectIds in a way that keeps the number of objects in each hash bucket quite low, thus preventing any one hash entry getting too long.</p>\r\n\r\n<p>This mechanism is also charged with assigning new (unique) object Ids.</p>\r\n\r\n<h3>12.3 Directory structure</h3>\r\n\r\n<p>The purpose of the directory structure is to rapidly access an object by name. This is required to perform file operations such as opening, renaming etc.</p>\r\n\r\n<p>The Yaffs directory structure is formed by a tree of objects of type YAFFS_OBJECT_TYPE_DIRECTORY. These objects have a doubly linked node of children.</p>\r\n\r\n<p>Each yaffs_obj also has a doubly linked list node called sibling which chain together the siblings in a directory.</p>\r\n\r\n<p>Each Yaffs partition has a root directory.</p>\r\n\r\n<p>Each yaffs partition also has some special purpose “fake” directories which are not stored in NAND but are instead created on mounting:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>lost+found: This directory is used as a place to store any lost file parts that cannot be placed in the normal directory tree.</p>\r\n	</li>\r\n	<li>\r\n	<p>Unlinked and deleted: These are pseudo-directories that are not in the directory tree. Placing objects in these directories gives them a status of being unlinked or deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Each object has a name that is used to look up the object within the directory, Thus, each name within a directory must be unique. This rule does not apply to objects in the unlinked and deleted pseudo-directories since we never use names to look up unlinked/deleted files and there may be many files of the same name that have been deleted.</p>\r\n\r\n<p>Name resolution is sped up by two mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Short names are stored directly in the yaffs_obj so that they don\'t have to be loaded from flash.</p>\r\n	</li>\r\n	<li>\r\n	<p>Each object has a “name sum”. This is a simple hashing of the name which speeds up the matching process because it is cheaper to compare the hash than compare the whole name string.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<h2>[ibimage==216==original==none==self==null]</h2>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The above directory structure represents the following directory tree</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>/</p>\r\n			</td>\r\n			<td>\r\n			<p>Root directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a</p>\r\n			</td>\r\n			<td>\r\n			<p>Directory containing c,d,e</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/b</p>\r\n			</td>\r\n			<td>\r\n			<p>Empty directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/lost+found</p>\r\n			</td>\r\n			<td>\r\n			<p>Empty directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/c</p>\r\n			</td>\r\n			<td>\r\n			<p>Directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/d</p>\r\n			</td>\r\n			<td>\r\n			<p>Data file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/e</p>\r\n			</td>\r\n			<td>\r\n			<p>Data file</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>If a Yaffs partition has been somehow corrupted then it is possible for an object to be created during scanning but with no object header (which contains the object name) being found. The object would then not have a name. In these cases, Yaffs creates a pseudonym from the object number of the form objnnnn so that the object can be placed in the directory structure.</p>\r\n\r\n<h3>12.4 Hard links</h3>\r\n\r\n<p>In POSIX file systems, the objects (inodes in Linux terminology, vnode in BSD-speak) and the object names (dentry in Linux terminology) are independent in that an object can have zero, one or more names.</p>\r\n\r\n<p>Having files with zero links is caused by constructs such as:</p>\r\n\r\n<p><code>h = open(“foo”,O_CREAT | O_RDWR, S_IREAD | S_IWRITE); /* create file “foo” */</code></p>\r\n\r\n<p><code>unlink(“foo”); /* unlink it so it can\'t be found in the directory */</code></p>\r\n\r\n<p><code>/* we still have a handle to the file so can still access it via the handle */</code></p>\r\n\r\n<p><code>read(h,buffer,100); </code></p>\r\n\r\n<p><code>write(h,buffer,100);</code></p>\r\n\r\n<p><code>close(h); /* as soon as the handle is closed the file is deleted */</code></p>\r\n\r\n<p>Yaffs does not store separate object and name records because that would waste space and incur extra reading/writing. Instead, Yaffs is optimised for the most typical case of one name per object. That is, Yaffs stores one name per yaffs_obj and uses “cheats” to achieve other behavior:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Objects that have zero names (ie unlinked objects) are handled by renaming the object into a fake directory that is used to designate unlinked objects.</p>\r\n	</li>\r\n	<li>\r\n	<p>Multiple names object are handled by designating one of the objects the main object and having multiple hard link pseudo-objects that refer to it.</p>\r\n	</li>\r\n	<li>\r\n	<p>The inode number (vnid in BSD-speak) is a per-file system unique number that identifies the object. The Yaffs object id of the main object is presented to the user. That is, if the inode number is queried from a hardlink object then the object id of the equivalent object is returned.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The linkage between hard link pseudo-objects and their equivalent object is through two fields:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Each object has a hardLinks doubly linked list node that links the list of hard links to that object.</p>\r\n	</li>\r\n	<li>\r\n	<p>Each hard link has a pointer to the “equivalent object”.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The usage of this structure is straight forward except for one interesting case when hard links are being deleted. Consider the following sequence of events:</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p># touch a</p>\r\n			</td>\r\n			<td>\r\n			<p>Create file a</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p># ln a b</p>\r\n			</td>\r\n			<td>\r\n			<p>Create a hard link b with equivalent object a.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p># rm a</p>\r\n			</td>\r\n			<td>\r\n			<p>Remove a. The object must still exist under the name b</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p>In step 3, Yaffs cannot just delete the equivalent object since that would leave a “hanging” hard link with no equivalent object.</p>\r\n\r\n<p>We also cannot just change the type of one of the hardlink objects because that would cause the object\'s object id to change. This would be unacceptable since it would break any mechanism that use the object id (of which there are many within and outside of Yaffs).</p>\r\n\r\n<p>Thus, if an object is deleted and it has hard links to it then we do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Select one of the object\'s hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p>Rename the object to the name in the hard link.</p>\r\n	</li>\r\n	<li>\r\n	<p>Delete the hard link pseudo-object.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h3>12.5 Symbolic links and special objects</h3>\r\n\r\n<p>These object types just store values that are needed to provide a full POSIX file system.</p>\r\n\r\n<p>A symbolic link object saves a symbolic link string that is used to provide the POSIX symbolic string mechanism.</p>\r\n\r\n<p>A special object is used to store device numbers, pipes and other similar special objects.</p>\r\n\r\n<h3>12.6 File objects</h3>\r\n\r\n<p>The yaffs_Objects have a type and a variant portion which is a union of different data required to hold the state of different object types.</p>\r\n\r\n<p>The file objects have type YAFFS_OBJECT_TYPE_FILE, and an associated file variant. This stores the following main values:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>fileSize: file size</p>\r\n	</li>\r\n	<li>\r\n	<p>topLevel: depth of the Tnode tree</p>\r\n	</li>\r\n	<li>\r\n	<p>top: pointer to top of Tnode tree</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The meaning of the file size is quite obvious. This is the current length of the file. Note that a file might have holes in it caused by seeking forward past the extents of the file.</p>\r\n\r\n<p>The top and topLevel work together to control the Tnode trees.</p>\r\n\r\n<h4>12.6.1 Tnode tree</h4>\r\n\r\n<p>Each file has a tnode tree to provide the mapping from file position to actual NAND chunk address. top is a pointer to the top of the tnode tree</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>[ibimage==220==original==none==self==null]<br />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The tnode tree is made up of tnodes (tree nodes) arranged in levels, each of which holds either:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>At levels greater than 0, a tnode has 8 pointers to other tnodes in the next level down.</p>\r\n	</li>\r\n	<li>\r\n	<p>At level 0, a tnode has 16 NAND chunk Ids which identify the chunk\'s location in RAM.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>These numbers are powers of 2 which makes for simple look-ups by just applying bitmasks. The code that traverses this tree is in yaffs_FindLevel0Tnode().</p>\r\n\r\n<p>As the file size grows, more tnodes are added and as the levels fill, more levels are added. If the file is truncated or resized downwards, then the tree is pruned. If a chunk in a file is replaced (eg. data is overwritten or copied by garbage collection) then the tnode tree must be updated to reflect the change.</p>\r\n\r\n<p>Tnodes typically make up the bulk of the Yaffs\' RAM usage.</p>\r\n\r\n<h2>13 How various mechanisms work</h2>\r\n\r\n<p>Having looked at the Yaffs data structures, we now consider how some of the mechanisms in Yaffs operates. This is most easily done by walking through some operations that Yaffs performs.</p>\r\n\r\n<h3>13.1 Block and chunk management</h3>\r\n\r\n<h4>13.1.1 Block states</h4>\r\n\r\n<p>Yaffs tracks the state of each block and chunk in the partition. This information is first built up during scanning (or recovered from a checkpoint). A block may be in one of the following states (see definition of yaffs_BlockState):</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>UNKNOWN</p>\r\n			</td>\r\n			<td>\r\n			<p>The block state is not yet known.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>NEEDS_SCANNING</p>\r\n			</td>\r\n			<td>\r\n			<p>During pre-scanning it has been determined that this block has something on it and needs scanning.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>SCANNING</p>\r\n			</td>\r\n			<td>\r\n			<p>This block is currently being scanned.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>EMPTY</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has nothing in it (is erased) and may be used for allocation. The block can get to this state by being erased.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>ALLOCATING</p>\r\n			</td>\r\n			<td>\r\n			<p>This is the block currently being used for allocation by the chunk allocator.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>FULL</p>\r\n			</td>\r\n			<td>\r\n			<p>All the chunks in this block have been allocated and at least one chunk contains useful information that has not yet been deleted.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>DIRTY</p>\r\n			</td>\r\n			<td>\r\n			<p>All the chunks in this block have been allocated and all have been deleted. The block might have been in the FULL or COLLECTING state before this. This block can now be erased and returned to EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>CHECKPOINT</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has checkpoint data in it. When the checkpoint is invalidated this block will be erased and returned to the EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>COLLECTING</p>\r\n			</td>\r\n			<td>\r\n			<p>This block is being garbage collected. As soon as all live data has been inspected this block becomes DIRTY.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>DEAD</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has been retired or was marked bad. This is a terminal state.</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<p>[ibimage==224==original==none==self==null]</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The regular runtime states are; EMPTY, ALLOCATING, FULL, COLLECTING and DIRTY.</p>\r\n\r\n<p>The checkpoint state is used for checkpoint blocks.</p>\r\n\r\n<p>The DEAD state is the terminal state for blocks that were retired because they were either marked bad or an error was detected.</p>\r\n\r\n<h4>13.1.2 Block and chunk allocation</h4>\r\n\r\n<p>An available chunk must be allocated before it can be written. The chunk allocation mechanism is provided by yaffs_AllocateChunk() and is very simple. Each partition has a current block that it is allocating from. This is termed the <em><strong>allocation block</strong></em>. Chunks are allocated sequentially from the allocation block. As the chunks are allocated, block and chunk management information is updated, including the block\'s pagesInUse count and the chunk use bitmap.</p>\r\n\r\n<p>When the block is fully allocated, another empty block is selected to become the allocation block. The blocks are selected by searching upwards from the previous allocation block.</p>\r\n\r\n<p>Previous to yaffs_guts.c V1.99, Yaffs scanning would identify the allocation block being used when the file system was last shut down and would continue allocation from that point. This had the benefit of continuing to use the same block and slightly reduce garbage creation . However, doing this could potentially cause problems due to attempting to write a block that had been in the middle of a write when power was lost. Thus Yaffs now always starts allocation on a new block after a mount. This slightly increases the amount of garbage (typically by an insignificant amount) but improves robustness.</p>\r\n\r\n<h4>13.1.3 A word about wear leveling</h4>\r\n\r\n<p>Since flash memory blocks can only endure a limited number of writes and erases ( termed the flash endurance), a flash file system needs to ensure that the same few blocks are not overwritten continuously causing those blocks to wear out while the other blocks are hardly used at all.. Instead it is important that the file system spreads out the wear.</p>\r\n\r\n<p>This is extremely important in a file system such as FAT which uses a limited set of logical blocks to store the file allocation table entries which get modified frequently. If FAT is used with a static logical to physical block mapping then the file allocation table blocks will wear out far before other blocks causing premature device failure. Thus, for FAT, it is very important that the logical to physical mapping gets stirred up a bit to cause the file allocation tables to be written to different physical blocks, thus leveling out the wear.</p>\r\n\r\n<p>Wear leveling is far less of a concern in a log structured file system such as Yaffs since we always write to the end of the log and thus don\'t keep on overwriting the same blocks.</p>\r\n\r\n<p>There are basically two ways to achieve wear leveling:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Use a specifically written set of functions that actively perform wear leveling.</p>\r\n	</li>\r\n	<li>\r\n	<p>Allow a degree of wear leveling to happen as a side effect of other activity.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Yaffs takes the second approach.</p>\r\n\r\n<p>Firstly, being log structured, Yaffs inherently spreads out the wear by performing all writes in sequence. That means that we don\'t single out a few blocks for a specific purpose and write them more than others.</p>\r\n\r\n<p>Secondly, blocks are allocated serially from the erased blocks in the partition. Thus the process of erasing (after deletion of garbage collection) and then allocating blocks does tend to spread the block use and provides a level of wear leveling.</p>\r\n\r\n<p>Thus, even though there is no code to specifically perform wear leveling it does happen as a side effect of other activity.</p>\r\n\r\n<p>This strategy performs well and has survived many accelerated lifetime tests in both simulations and on real NAND-based products.</p>\r\n\r\n<h4>13.1.4 Management of yaffs_tnode and yaffs_obj structures</h4>\r\n\r\n<p>yaffs_tnodes and yaffs_objs are dynamically created and released as they are needed.</p>\r\n\r\n<p>The allocation and management could be done by just using the standard memory allocator (malloc, kmalloc etc), but this could cause performance problems, particularly in embedded systems which tend to have rather simplistic memory allocators.</p>\r\n\r\n<p>The allocation and deletion of many small structures can cause memory fragmentation causing out-of-memory and slowdown problems.</p>\r\n\r\n<p>To prevent these problems, Yaffs manages these objects internally. Yaffs creates many structures (100 or so) at a time and then allocates them singly from a free list. When structures are released they are placed on the free list for reuse.</p>\r\n\r\n<h3>13.2 Internal cache</h3>\r\n\r\n<p>This describes the standard Yaffs internal cache implementation. Alternative caches have been devised for other purposes and at some future point Yaffs might be extended to support pluggable cache policies.</p>\r\n\r\n<p>The main purpose of the internal cache is to reduce NAND access for badly behaved applications which perform many small reads and writes. Believe it or not, but there are applications out there that read or write files a few bytes at a time:</p>\r\n\r\n<p><code>while(...)<br />\r\nwrite(h,buf,1);</code></p>\r\n\r\n<p>This sort of behavior would be terrible for performance and flash memory life if each read or write operation resulted in a write to flash. Yaffs provides an internal cache to mitigate against this sort of behaviour.</p>\r\n\r\n<p>The Yaffs internal cache is called a “Short Op Cache” (short operation cache) and only comes in to play for operations which are not chunk aligned. The caching mechanism is very simple and was primarily written for less sophisticated OSs that lack their own file caching layer. The cache is still beneficial in OSs like Linux, which provide a caching layer, because it caches both the read and write path. Thus, while short reads will be serviced by the Linux page cache, the shortOpCache does address non-chunk aligned writes.</p>\r\n\r\n<p>Page aligned reads/writes bypass the cache unless that chunk is already in the cache.</p>\r\n\r\n<p>The cache is stored in an array of cache entries. The number of cache entries is set in the yaffs_Device configuration by dev-&gt;nShortOpCaches and the cache is configured during initialisation. A cache size of zero disables the cache. The cache management algorithms are simplistic and probably don\'t scale well to very large numbers. This means that it is probably best to stick with around 5 to 20 cache entries.</p>\r\n\r\n<p>Each cache entry holds:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>object id, chunk id, nbytes (the tag info of the chunk being cached)</p>\r\n	</li>\r\n	<li>\r\n	<p>The actual data bytes</p>\r\n	</li>\r\n	<li>\r\n	<p>Cache slot status (last used counter, dirty,...)</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Finding an unused cache entry is a simple operation. We just iterate through the cache entries searching for one that is not busy. If all cache entries are busy then a cache push-out is performed and then we retry the find.</p>\r\n\r\n<p>Whenever a cache entry is accessed (read or written) the last-use counter is is set to an incrementing number. This counter value is used to drive the least-recently-used (LRU) algorithm.</p>\r\n\r\n<p>Cache entries which have been modified and not yet written back to flash are marked with a dirty flag. This is tells us whether we need to write out the data during a flush or push-out.</p>\r\n\r\n<p>A push-out happens whenever the cache is full and we need to push an entry out of the cache to make space to load a new entry. Yaffs uses the LRU algorithm to select the LRU cache entry and then write out all dirty entries for that file, in ascending order, and mark these entries as free.</p>\r\n\r\n<p>Whenever a file is flushed (file flush or close), the file\'s dirty entries are written out. At certain times (eg. before an unmount or at sync()) the entire cache is flushed.</p>\r\n\r\n<p>This caching policy is gives a useful performance improvement with very simple algorithms (ie good bang-for-bucks).</p>\r\n\r\n<p>Apart from its normal caching function, the shortOpCache also does much of the heavy lifting for inband tags support since all the inband tags accesses are not chunk aligned and must typically be re-aligned. The cache makes this easy.</p>\r\n\r\n<h3>13.3 Scanning</h3>\r\n\r\n<p>Scanning is the process whereby file system state is rebuilt from scratch. This happens on mounting a Yaffs partition if no checkpoint data is available or if the mounting was told to ignore the checkpoint data.</p>\r\n\r\n<p>Scanning involves reading the tags from all the active chunks in the system so can take quite a while, but probably less time than you\'d expect.</p>\r\n\r\n<p>The scanning process is different for Yaffs1 vs Yaffs2 because of the differences mentioned before.</p>\r\n\r\n<h4>13.3.1 Yaffs1 scanning</h4>\r\n\r\n<p>Yaffs1 scanning is rather straight forward because Yaffs1 uses deletion markers.</p>\r\n\r\n<p>All we really need to do is read the tags off all the chunks in the partition and determine if the chunk is active. Order is not important and blocks could be read in any order. If the deletion marker has not been set then it is active and we need to add the chunk to the file system state as follows:</p>\r\n\r\n<p>If the chunk is a data chunk (chunkId &gt; 0) then the chunk must be added to the relevant file\'s tnode tree. If the object does not exist, then we create it in lost+found, although it has no name and other file details yet.</p>\r\n\r\n<p>If the chunk is an object header (chunkId == 0) then the object must be created (or it might already exist in lost + found and we need to fix up its details and slot it into the correct part of the directory tree.</p>\r\n\r\n<p>If at any time during scanning we find that we are reading a second chunk with the same objectId:chunkId as a previously encountered chunk then the 2-bit serial number is used to resolve the dispute.</p>\r\n\r\n<p>Once the scanning is complete we still need to fix up a few things:</p>\r\n\r\n<p>Hard links must be connected up properly (it is more efficient to defer this until after the scanning has completed).</p>\r\n\r\n<p>Also, objects which were marked as unlinked must be deleted since there are no longer handles open to these objects.</p>\r\n\r\n<h4>13.3.2 Yaffs2 scanning</h4>\r\n\r\n<p>Yaffs2 has no deletion markers which makes the scanning significantly more complex.</p>\r\n\r\n<p>The first thing that needs to be done is to pre-scan the blocks to determine the sequence number for the blocks. The list of blocks and sequence numbers is then sorted to make a chronologically ordered list.</p>\r\n\r\n<p>Yaffs then scans <em><strong>backwards</strong></em> through these blocks (ie in reverse chronological order). Thus, the first encountered objectId:chunkId pair is the current one and subsequent chunks with those tags are out of date.</p>\r\n\r\n<p>We read the tags for each chunk in each block.</p>\r\n\r\n<p>If it is a data chunk ( ie. chunkId &gt; 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>If a chunk with the same objectId:chunkId has already been found then this is not the current chunk. Delete this one.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object is marked as deleted, or corrupted, then delete it.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object does not yet exist then this chunk was written since the last object header write for this object so this chunk must be the last written chunk in the file and must have happened just before an unclean shutdown with the file still open. We can create the file and use the chunkId and nbytes to set the file extents.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object does exist and the chunk lies beyond the object\'s file scan size (extents) then it is a truncated chunk and can be deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else it is placed into the file\'s tnode tree.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If it is an object header (ie. chunkId == 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>If the object is in the deleted directory then mark the object as deleted and set the object\'s shrink size to zero.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if an object header has not yet been found for this object, then this is the most current object header and holds the most current name etc. If no data chunks have yet been found for this chunk then the size in the object header is also the file extents. The file size is used to set the file scan size since at the time it was written the file did not extend past that point and we can safely ignore further data chunks for that file that extend past that point.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else just use the file size as the scan size if it is smaller than the current file scan size.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else this an obsolete object header. It is deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>A bit more explanation is probably needed to describe the file scan size. The whole purpose of this is to determine data chunks that should be ignored due to the file being truncated.</p>\r\n\r\n<h3>13.4 Checkpoint</h3>\r\n\r\n<p>Mount scanning takes quite a lot of time and slows mounting, though perhaps by less than you\'d think. Checkpointing is a mechanism to speed the mounting by taking a snapshot of the Yaffs runtime state at unmount or sync() and then reconstituting the runtime state on re-mounting. The speed up is dramatic.</p>\r\n\r\n<p>The actual checkpoint mechanism is quite simple. A “stream” of data is written to a set of blocks which are marked as holding checkpoint data and the important runtime state is written to the stream.</p>\r\n\r\n<p>Not all state needs to be saved, only the state needed to reconstitute the structure of the runtime data. For example, file metadata need not be saved since it is very easy to load that through lazy-loading.</p>\r\n\r\n<p>The checkpoint stores data in the following order:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Start marker, including checkpoint format id.</p>\r\n	</li>\r\n	<li>\r\n	<p>yaffs_Device information</p>\r\n	</li>\r\n	<li>\r\n	<p>Block information</p>\r\n	</li>\r\n	<li>\r\n	<p>Chunk flags</p>\r\n	</li>\r\n	<li>\r\n	<p>Objects, including file structure</p>\r\n	</li>\r\n	<li>\r\n	<p>End marker</p>\r\n	</li>\r\n	<li>\r\n	<p>Checksum</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The checkpoint validity is ensured by the following mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>The data is stored using whatever ECC mechanisms are in place.</p>\r\n	</li>\r\n	<li>\r\n	<p>The start marker includes a checkpoint version so that an obsolete checkpoint will not be read if the checkpoint code changes.</p>\r\n	</li>\r\n	<li>\r\n	<p>Data is written to the stream as structured records, each with a type and size.</p>\r\n	</li>\r\n	<li>\r\n	<p>The end marker must be read when expected.</p>\r\n	</li>\r\n	<li>\r\n	<p>Two checksums are maintained across the whole checkpoint data set.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If any checks fail, the checkpoint is ignored and the state is re-scanned.</p>\r\n\r\n<p>The astute reader might notice that block state changes as we\'re writing checkpoint blocks, so how do we manage to re-constitute the block states correctly? This is achieved by the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>As we write the checkpoint, the checkpoint block allocation selects EMPTY blocks. We don\'t change the state as we\'re writing the checkpoint so their state is written to the checkpoint as EMPTY or CHECKPOINT, it does not really matter which.</p>\r\n	</li>\r\n	<li>\r\n	<p>After reading the checkpoint, we know which blocks have been used to store checkpoint data. We update those blocks to reflect the CHECKPOINT state.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>A checkpoint is invalidated by any modifications (writes, erases,...) thus any modification path also checks to see if there is a checkpoint and erases it.</p>\r\n\r\n<p>The regular Yaffs block allocator and garbage collection logic also needs to be aware of the approximate size of the checkpoint so that these can ensure enough spare space is reserved to store a checkpoint. There is a calculation that updates the value whenever the number of objects etc changes.</p>\r\n\r\n<h3>13.5 Extended tags and packed tags</h3>\r\n\r\n<p>Extended tags is an abstraction of a way to store more information in the tags, allowing for faster scan times. Packed Tags is a physical implementation of the storage of extended tags.</p>\r\n\r\n<p>When we\'re scanning we really only need to build up the structure of the file system. In other words, rebuild the directory relationships, tnode trees and such. Much of the detail such as file names, permissions etc can be lazy loaded.</p>\r\n\r\n<p>This required storing extra information, such as the parent directory and object type, in the tags of object headers. But how do we do this without enlarging the tags size?</p>\r\n\r\n<p>Object headers always have a chunkId of zero and nbytes has no meaning because it is not a data chunk. By using a single bit to identify object headers we suddenly open up a whole lot of space that we can stuff full of useful data. By being clever as to what data we use, we can pack in the important data in most circumstances.</p>\r\n\r\n<p>Sometimes, for example very large files, the packing will not work because there are insufficient bits to hold the field so we just write regular tags instead. That\'s OK since there can only be few such files in the system so the performance reduction only impacts very few files and the system level impact is unnoticeable. A simple, cheap, strategy that gives speed up 99% of the time is a very handy one.</p>\r\n\r\n<p>Other details are lazy-loaded on demand. This will happen when the object is looked up (eg. by a file open or searching for the file in the directory).</p>\r\n\r\n<h3>13.6 Block summaries</h3>\r\n\r\n<p>Scanning requires that the tags for each chunk are read so that they can be inspected. Reading the tags from each chunk can be time consuming. Thus, block summaries were added in 2011.</p>\r\n\r\n<p>Block summaries write all the tags data for chunks in the block into the last few (typically only one) chunk in a block. This allows all the tags for chunks in that block to be read in one hit. Depending on the system throughput (CPU, RAM and flash speeds), this can speed up mount time dramatically.</p>\r\n\r\n<p>If a block summary is not available for a block, then the old mechanism is still used to scan the tags on each chunk.</p>\r\n\r\n<h3>13.7 Inband tags</h3>\r\n\r\n<p>Thus far we have considered chunks where there are sufficient unallocated bytes in the spare (or out of band) area to store the tags. This means that the data part of the page is a power of 2 in size. Powers of 2 are very fast to deal with which is why most operating systems use various powers of 2 in their internals.</p>\r\n\r\n<p>But what happens if we don\'t have enough space in the spare area to store the tags? Inband tags to the rescue! With inband tags we store the tags in the flash data area alongside the data. This gives us far more flexibility in the types of storage that Yaffs can support, but breaks the power-of-2 alignment. There are three major penalties with inband tags:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Due to the loss of chunk alignment, all transactions must pass through the shortOpCache. This costs an extra memcpy().</p>\r\n	</li>\r\n	<li>\r\n	<p>Offset to chunk, free space and other calculations are no longer just shift and mask operations but instead require multiplications and divisions which can relatively expensive.</p>\r\n	</li>\r\n	<li>\r\n	<p>Reading tags during scanning etc can involve reading the whole page, making this slower. The impact is minimised by using checkpointing.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Thus, it is better, performance wise, to only use inband tags when regular tags will not work.</p>\r\n\r\n<h3>13.8 Soft deletion</h3>\r\n\r\n<p>Soft deletion is a mechanism that was added to speed up file deletion in Yaffs1 mode.</p>\r\n\r\n<p>Looking back to the description of how files are stored in Yaffs1, you can see that deleting a file involves writing the deletion marker of every data chunk in the file. This can take quite some time for large files.</p>\r\n\r\n<p>The soft deletion mechanism was added to avoid writing all those deletion markers. Instead of writing the deletion markers, soft deletion instead tracks the number of chunks in each block that belong to deleted files and leaves the clean up to the garbage collector.</p>\r\n\r\n<p>When the garbage collector looks at a block it can see what current chunks (ie chunks not yet deleted) belong to deleted files and does not copy those, thus effectively deleting the chunk. Once all the chunks associated with a deleted file have cleaned up by the garbage collector the file header object can also be deleted.</p>\r\n\r\n<p>Yaffs2 mode does not write deletion markers and thus does not use soft deletion.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n','This document gives a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical. This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.','filtered_html'),('paper',0,409,583,'und',0,'<p>&nbsp;</p>\r\n\r\n<h2>1 Purpose</h2>\r\n\r\n<p>The purpose of this document is to give a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical.</p>\r\n\r\n<p>This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.</p>\r\n\r\n<p>This document is updated periodically. Yaffs mechanism design changes from time to time and this document will not always reflect the most up to date code.</p>\r\n\r\n<h2>2 What is Yaffs?</h2>\r\n\r\n<p>Yaffs stands for <em>Yet Another Flash File System</em>, a term coined by Charles Manning in 2001 when suggesting that the already cluttered flash file system space could do with yet another offering – this time a flash file system designed from the ground up to work with NAND flash.</p>\r\n\r\n<p>The first release of Yaffs was developed during the end of 2001 and the beginning of 2002. By mid-2002, Yaffs was being trialled in various products. In May 2002, Yaffs was announced and a few more projects started playing with it. In June, porting to other OSs started. In September 2002, Yaffs was announced on linuxdevices.com which started a far wider uptake of Yaffs.</p>\r\n\r\n<p>Yaffs1, the original version of Yaffs, supported 512-byte page NAND devices with a SmartMedia-like memory layout.</p>\r\n\r\n<p>Yaffs2 is a follow-up to Yaffs1 extending Yaffs1 to support larger and different devices with different constraints.</p>\r\n\r\n<p>Yaffsis highly portable and has been used in many different products and applications as varied as sewing machines, point of sale, phones and aerospace. Yaffs has been used with multiple different operating systems. Native support is available for Linux, WindowsCE and eCOS while Yaffs Direct Interface provides a layer that can be used in other applications such as RTOSs. Yaffs has been used with multiple different CPUs and compilers.</p>\r\n\r\n<p>Yaffs has also been used as a NOR file system and even as a RAM file system.</p>\r\n\r\n<p>3 Design and coding strategies</p>\r\n\r\n<p>Yaffs is designed to work in multiple environments which drives the need for portability. Portability also improves the debugging and testing since it is easier to develop and debug code in an application environment than within an OS kernel. This generally allows Yaffs to progress faster with less programming resources and provides more varied testing. Primary strategies that improve portability include:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>No OS-specific features used in main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p>No compiler-specific features used in the main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p>Abstract types and functions used to allow Unicode or ASCII operation.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Simplicity is another key goal. Complexity is limited to those situations that provide significant improvements to performance or utility. Simplicity improves robustness and the ease of integration and development. Primary strategies that improve simplicity are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Single threaded model. Yaffs is locked on a per-partition basis at a high level. This is simpler than tracking lower-level locking. Yaffs Direct Interface uses a single lock for all partitions.</p>\r\n	</li>\r\n</ul>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Log structure makes for a simpler garbage collector and allocation methodology.</p>\r\n	</li>\r\n	<li>\r\n	<p>Abstractions that build layered code which is easier to understand, port and debug.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2>4 Terminology: Yaffs vs Yaffs1 vs Yaffs2</h2>\r\n\r\n<p>Before we embark on our quest to understand what Yaffs is, a terminology explanation is in order.</p>\r\n\r\n<p>There are two versions of Yaffs code: the original Yaffs code-base and the current Yaffs2 code-base. The Yaffs2 code-base supports functionality provided by the Yaffs code-base as well as being extended with new functionality to provide extra modes of operation, most importantly those required to work with larger and more modern NAND parts. The Yaffs2 code-base supports the Yaffs1 mode of operation through a backward compatibility mode.</p>\r\n\r\n<p>Unless explicitly mentioned, this text refers to the Yaffs2 code-base.</p>\r\n\r\n<p>This text uses three terms Yaffs, Yaffs1 and Yaffs2 depending on various modes of operation.</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Yaffs1 is a simpler mode of operation that uses deletion markers to track state.</p>\r\n	</li>\r\n	<li>\r\n	<p>Yaffs2 is a more complex mode of operating that was developed to support larger flash types that cannot used deletion markers.</p>\r\n	</li>\r\n	<li>\r\n	<p>Yaffs means operations common to both.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Yaffs1 originally only worked with 512-byte page devices but has been extended to support larger flash pages such as Intel M18 flash with 1k pages.</p>\r\n\r\n<p>Yaffs2 was originally designed for 1k or larger page sizes but can be used with smaller pages by using in-band tags.</p>\r\n\r\n<p>Since the Yaffs1 mode of operation is simpler to understand than the Yaffs2 mode of operation, Yaffs1 will be described first and readers interested in Yaffs2 operation should first fully acquaint themselves with the Yaffs1 mode of operation. It is suggested that this document be read sequentially rather than browsing it.</p>\r\n\r\n<h2>5 Objects</h2>\r\n\r\n<p>In Yaffs-speak, an Object is anything that is stored in the file system. These are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Regular data files</p>\r\n	</li>\r\n	<li>\r\n	<p>Directories</p>\r\n	</li>\r\n	<li>\r\n	<p>Hard-links</p>\r\n	</li>\r\n	<li>\r\n	<p>Symbolic links</p>\r\n	</li>\r\n	<li>\r\n	<p>Special objects (pipes, devices etc).</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>All objects are identified by a unique integer <em><strong>object Id</strong></em>.</p>\r\n\r\n<p>In the standard POSIX model, as used by Linux, there are inodes and directory entries (dentries).</p>\r\n\r\n<p>An inode may be a regular file, directory, or special file.</p>\r\n\r\n<p>Directory entries provide the naming mechanism that is used to locate inodes.</p>\r\n\r\n<p>Under POSIX, each inode may have zero, one or many dentries:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>One dentry per inode is commonly understood.</p>\r\n	</li>\r\n	<li>\r\n	<p>Multiple dentries per inode allow the same inode to be accessed via multiple names. This is achieved through hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p>Zero dentries per inode is less often understood. This happens when an inode is unlinked while there is still a handle open to the inode. The unlinking deletes the dentry but the inode still exists. As soon as the handle is closed the inode is deleted too.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2>6 The Yaffs NAND model</h2>\r\n\r\n<p>The memory in NAND flash is arranged in <em><strong>pages.</strong></em> A page is the unit of allocation and programming. In Yaffs, the unit of allocation is the<em> </em><em><strong>chunk</strong></em>. Typically a chunk will be the same as a NAND page, but there is flexibility to use chunks which map to multiple pages (eg. A system may have two NAND chips in parallel requiring 2x512 = 1024 byte chunks) . This distinction gives a lot of flexibility in how the system can be configured. In this text the term page and chunk are synonymous unless stated otherwise.</p>\r\n\r\n<p>Many, typically 32 to 128 but as many as a few hundred, chunks form a <em><strong>block</strong></em>. A block is the unit of erasure.</p>\r\n\r\n<p>NAND flash may be shipped with bad blocks and further blocks may go bad during the operation of the device. Thus, Yaffs is aware of bad blocks and needs to be able to detect and mark bad blocks.</p>\r\n\r\n<p>NAND flash also typically requires the use of some sort of error detection and correction code (ECC). Yaffs can either use existing ECC logic or provide its own.</p>\r\n\r\n<h2>7 How Yaffs1 stores files</h2>\r\n\r\n<p>Yaffs1 has a modified log structure, while Yaffs2 (see below) has a true log structure. A true log structured file system only ever writes sequentially. Yaffs1 uses deletion markers, which breaks the sequential write rule. Yaffs2 does not use deletion markers and is thus a true log structured file system.</p>\r\n\r\n<p>Instead of writing data in locations specific to the files, the file system data is written in the form of a sequential log, The entries in the log are all one chunk in size and can hold one of two types of chunk:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>Data chunk</strong></em>: A chunk holding regular data file contents.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Object Header</strong></em>: A descriptor for an object (directory, regular data file, hard link, soft link, special descriptor,...). This holds details such as the identifier for the parent directory, object name, etc.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Each chunk has <em><strong>tags</strong></em> associated with it. The tags comprise the following important fields (others being ignored for now):</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>ObjectId</strong></em>: Identifies which object the chunk belongs to.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>ChunkId</strong></em>: Identifies where in the file this chunk belongs. A chunkId of zero signifies that this chunk contains an objectHeader. ChunkId==1 signifies the first chunk in the file (ie. At file offset 0), chunkId==2 is the next chunk and so on.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Deletion Marker:</strong></em> (Yaffs1 only) Shows that this chunk is no longer in use.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Byte Count</strong></em>: Number of bytes of data if this is a data chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Serial Number</strong></em>: (Yaffs1 only) Serial number used to differentiate chunks with the same objectId and chunkId.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Now let\'s bring that all together in an explanation. This is a simplified explanation and skips over issues such as Soft Deletion (mentioned later in the document). For explanation purposes, we shall consider usage of fictitious NAND type that has 4 chunks per block, starting with an empty (erased) flash.</p>\r\n\r\n<p>First we\'ll create a file.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNext we write a few chunks of data to the file.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNext we close the file. This writers a new object header for the file. Notice how the previous object header is deleted.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header (length 0)</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header (length n)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nLet\'s now open the file for read/write, overwrite part of the first chunk in the file and close the file. The replaced data and object header chunks become deleted.<br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNow let\'s resize the file to zero by opening the file with O_TRUNC and closing the file. This writes a new object header with length 0 and the contents are pruned making the data chunks deleted.</p>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deletion</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Delted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header (length 0).</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nNote how all the pages in block 1 are now marked as deleted. This means that block 1 no longer contains useful information so we can now erase block 1 and reuse the space.</p>\r\n\r\n<p>We will now rename the file. To do this we write a new object header for the file<br />\r\n&nbsp;</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Block</p>\r\n			</td>\r\n			<td>\r\n			<p>Chunk</p>\r\n			</td>\r\n			<td>\r\n			<p>ObjectId</p>\r\n			</td>\r\n			<td>\r\n			<p>ChunkId</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>&nbsp;</p>\r\n			</td>\r\n			<td>\r\n			<p>Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Deleted</p>\r\n			</td>\r\n			<td>\r\n			<p>Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>500</p>\r\n			</td>\r\n			<td>\r\n			<p>0</p>\r\n			</td>\r\n			<td>\r\n			<p>Live</p>\r\n			</td>\r\n			<td>\r\n			<p>New object header showing new name</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\nBlock 2 now contains only deleted chunks and can be erased and reused.</p>\r\n\r\n<p>Note how the tags tell us:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Which chunks belong to which object</p>\r\n	</li>\r\n	<li>\r\n	<p>The position of the chunks within a file,</p>\r\n	</li>\r\n	<li>\r\n	<p>Which chunks are current</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>With that information we can recreate the state of the files regardless of the placement of the chunks in NAND. This means that should the system fail (crash, power fail,...) at any point during the sequences above, we are still able to recover the file system state up to that point.</p>\r\n\r\n<ul>\r\n</ul>\r\n\r\n<p>Note that there are no file allocation tables or similar structures stored in the flash. This reduces the amount of writing and erasing (improving write performance) while also improving robustness. Corruption of file allocation tables and similar structures is a common failure mechanism for embedded file systems. The lack of such structures makes Yaffs more robust. Or, to put it another way, you can\'t corrupt what you don\'t store.</p>\r\n\r\n<h2>8 Garbage collection</h2>\r\n\r\n<p>As we have shown so far, when a block is made up only of deleted chunks, that block can be erased and reused. However, consider what will happen if we have many blocks which each have a few current chunks in them. Clearly we cannot erase any of the current blocks or we will destroy data. What we need to do is copy the useful data chunks off a block, deleting the originals and allowing the block to be erased and reused. This process is referred to as <em><strong>garbage collection</strong></em>.</p>\r\n\r\n<p>The Yaffs garbage collection process works as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>Find a block worth collecting (according the the heuristics below). If none found, then exit.</p>\r\n	</li>\r\n	<li>\r\n	<p>Iterate through the chunks in the block. If the chunk is in use then create a new copy and delete the original. Fix up the RAM data structures to reflect the change.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p>Once all chunks in this block have been deleted, the block can be erased ready for reuse.</p>\r\n\r\n<p>Note that although step 2 deletes the original chunk when it gets copied there is not much point in actually programming the deletion marker in flash since we\'re about to erase the block anyway. Thus Yaffs does not program the deletion marker in these cases. If power is lost part way through the operation we can still tell the difference between the original and the copy by looking at the serial number (not that it really matters anyway since the two have the same contents so either can be used).</p>\r\n\r\n<p>The heuristics to determine whether a block is worth collecting are as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>If there are many erased blocks available, then Yaffs does not need to work hard, but will instead only attempt to perform garbage collection of blocks with very few chunks in use. This is termed <em><strong>passive</strong></em> garbage collection.</p>\r\n	</li>\r\n	<li>\r\n	<p>If, however, there are very few erased blocks, Yaffs will work much harder to recover more space and will garbage collect blocks with more chunks in use. This is termed <em><strong>aggressive</strong></em> garbage collection.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p>OK, the passive/aggressive names are a bit silly, but they were contrived around midnight... give me a break!</p>\r\n\r\n<p>If garbage collection is aggressive, the whole block is collected in a single garbage collection cycle. If the collection is passive then the number of copies is reduced thus spreading the effort over many garbage collection cycles. This is done to reduce garbage collection load and improve responsiveness.</p>\r\n\r\n<p>The rationale behind the above heuristics is to delay garbage collection when possible to reduce the amount of collection that needs to be performed, thus increasing average system performance. Yet there is a conflicting goal of trying to spread the garbage collection so that it does not all happen at the same causing fluctuations in file system throughput. These conflicting goals make garbage tuning quite challenging.</p>\r\n\r\n<p>All flash file systems need some sort of garbage collector to reclaim space. The garbage collector is often a determining factor in the performance of the flash file system. Many file systems have to do a significant amount of effort in a single collection sweep. Yaffs only deals with one block at a time which constrains the amount of effort in a garbage collection cycle, thus reducing the “stall time”.</p>\r\n\r\n<p>The Yaffs garbage collection algorithm is under review with the goal of reducing “stall time” and increasing throughput.</p>\r\n\r\n<h2>9 Yaffs1 serial numbers</h2>\r\n\r\n<p>In Yaffs1, each chunk is marked with a 2-bit serial number that is incremented every time a chunk with the same tags value is replaced (eg. When a chunk is overwritten or when copied by garbage collection).</p>\r\n\r\n<p>Now let\'s suppose first that we did not have a serial number.</p>\r\n\r\n<p>Let\'s consider a rename where an object header chunk must be replaced with a new one with the new name. Yaffs could do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Delete the old chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p>Write the new chunk.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If the system fails after the delete, the system could end up with no chunks matching the relevant tags values. This could cause files to be lost (end up in lost+found). Thus, we have to write the new chunk before deleting the old chunk.</p>\r\n\r\n<p>But now consider what might happen if we did this:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Write new chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p>Delete old chunk.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If the system failed after the write we now have two chunks that match the tags. How can we tell which one is current?</p>\r\n\r\n<p>The current chunk is determined by inspecting the serial number. Since the serial number is only incremented by one before the old chunk is deleted, a 2-bit serial number is sufficient. Valid pairs are:</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>Old</p>\r\n			</td>\r\n			<td>\r\n			<p>New</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>00</p>\r\n			</td>\r\n			<td>\r\n			<p>01</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>01</p>\r\n			</td>\r\n			<td>\r\n			<p>10</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>10</p>\r\n			</td>\r\n			<td>\r\n			<p>11</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>11</p>\r\n			</td>\r\n			<td>\r\n			<p>00</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<p>Thus, by inspecting the serial number attached to the old and new tags the current chunk can be determined and the old chunk can be deleted.</p>\r\n\r\n<h2>10 Yaffs2 NAND model</h2>\r\n\r\n<p>Yaffs2 is an extension of Yaffs designed to fulfill a new set of objectives to work with newer NAND types including:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Zero overwrites. Yaffs1 only ever overwrites a single byte in the spare area of the chunks to set the deletion marker. More modern NAND devices are less tolerant of overwrites and Yaffs2 performs no overwrites at all.</p>\r\n	</li>\r\n	<li>\r\n	<p>Sequential chunk writing within a block. More modern NAND reliability specifications tend to assume sequential writing. Since Yaffs2 does not write deletion markers, the writing of chunks within a block is strictly sequential.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Since Yaffs2 performs less writes, there is potential for Yaffs2 to have improved write performance.</p>\r\n\r\n<p>Yaffs2 cannot use a deletion marker because that would violate the zero overwrites mandate and alternate mechanisms are provided to determine which chunks are current and which are deleted. These mechanisms are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p><em><strong>Sequence Number</strong></em>: As each block is allocated, the file system\'s sequence number is incremented and each chunk in the block is marked with that sequence number. The sequence number thus provides a way of organising the log in chronological order.</p>\r\n	</li>\r\n	<li>\r\n	<p><em><strong>Shrink Header Marker</strong></em>: The shrink header marker is used to mark object headers that are written to shrink the size of a data file. This is explained more in the accompanying text.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Note: The Yaffs2 sequence number is not the same as the Yaffs1 serial number!</p>\r\n\r\n<p>Note: We still refer to chunk deletion in Yaffs2. The chunks are marked as deleted in the RAM data structures used by garbage collection etc, but there is no deletion marker written to flash.</p>\r\n\r\n<p>The sequence number allows the chronological order to be determined which allows Yaffs to determine the sequence of events and restore the file system state. This is achieved by scanning <em><strong>backwards</strong></em> chronologically (ie. From the highest sequence number backwards to the lowest sequence number). Thus:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Since we\'re scanning backwards, the most recently written – and thus current – chunk matching an objectId:chunkId pair will be encountered first and all subsequent matching chunks must be obsolete and treated as deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p>The file length in object headers are used to trim back resized files. If an object header is encountered, then subsequent data chunks that lie beyond that file length are clearly obsolete and treated as deleted. Note that both current and obsolete object header sizes must be used to obtain an accurate reconstruction.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Explaining the purpose of the shrink header marker is a bit convoluted. The purpose of a shrink header marker is to show that this object header indicates that a file has shrunken in size, and to prevent those object headers from being deleted by garbage collection. To understand this, it is easiest to consider what would happen if the shrink header markers did not exist.</p>\r\n\r\n<p>For simplification we\'ll assume no garbage collection happens during this part of the exercise. It is safe to make that assumption since it does not alter the reasoning.</p>\r\n\r\n<p>Consider a file that has been through the following:</p>\r\n\r\n<p><code>h = open(“foo”,O_CREAT| O_RDWR, S_IREAD|S_IWRITE); /* create file */<br />\r\nwrite(h,data,51024*1024); /* write 5 MB of data /<br />\r\ntruncate(h,1024*1024); /* truncate to 1MB */<br />\r\nlseek(h,2*1024*1024, SEEK_SET); /* set the file access position to 2MB */<br />\r\nwrite(h,data,1024*1024); /* write 1MB of data */<br />\r\nclose(h);3</code><br />\r\n&nbsp;</p>\r\n\r\n<p>The file will now be 3MB in length, but there will be a “hole” between 1MB and 2MB. According to POSIX (and to aid security etc) that “hole” should always read back as zeros.</p>\r\n\r\n<p>Yaffs2 would represent that in NAND with the following sequence of chunks:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p>Object header for file creation (file length = 0)</p>\r\n	</li>\r\n	<li>\r\n	<p>5MB worth of data chunks (0 to 5MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>Object header for truncation (file length = 1MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>1MB worth of data chunks (2MB to 3MB)</p>\r\n	</li>\r\n	<li>\r\n	<p>Object header for close (file length = 3MB)</p>\r\n\r\n	<p>At this stage, only the the following data chunks are current:</p>\r\n	</li>\r\n</ol>\r\n\r\n<ul>\r\n	<li>\r\n	<p>The first 1MB of data created in step 2 above.</p>\r\n	</li>\r\n	<li>\r\n	<p>The 1MB of data created in step 4 above.</p>\r\n	</li>\r\n	<li>\r\n	<p>The object header created in step 5 above.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>But Yaffs must remember the truncation that happened in step 3, otherwise Yaffs will “forget” that there is a hole, which would cause the old data to be visible again. Thus, if Yaffs creates a hole it first writes a shrink header to indicate the start of the hole and a regular object header to mark the end of the hole.</p>\r\n\r\n<p>The shrink header modifies the behavior of the garbage collector to ensure that these shrink headers are not erased until it is safe to do so. This unfortunately has a negative impact on garbage collection in systems which make extensive use of files with holes.</p>\r\n\r\n<p>Shrink headers are also used to indicate that a file has been deleted and that the record of the file deletion is not lost.</p>\r\n\r\n<h3>10.1&nbsp;Yaffs2 extended tags</h3>\r\n\r\n<p>Yaffs2 extends the tags in object headers with extra fields to improve mount scanning performance. The operation of extended tags is described later in this document.</p>\r\n\r\n<h2>11 Bad block handling NAND error handling</h2>\r\n\r\n<p>NAND flash is designed to be very low cost and very high density. In order to get high yields, and thus keep costs down, NAND flash is typically shipped with a few bad blocks and some further blocks are expected to go bad during the use of the device. Thus, any flash file system without an effective bad block handling policy is unsuitable for NAND flash.</p>\r\n\r\n<p>Yaffs1 uses the Smart-Media style of bad block marking. This checks the sixth byte (byte 5) of the spare area. In a good block this should be 0xFF. A factory-marked bad block should be 0x00. If Yaffs1 determines that a block has gone bad it is marked bad with 0x59 (\'Y\'). Using a distinctive marker allows Yaffs-marked bad blocks to be differentiated from factory-marked bad blocks.</p>\r\n\r\n<p>Yaffs2 mode is designed to support a wider range of devices and spare-area layouts. Thus, Yaffs2 does not decide which bytes to mark but instead calls driver functions to determine whether the block is bad, or to mark it bad.</p>\r\n\r\n<p>So, when does Yaffs mark a block bad? Yaffs will mark a block bad if a read or write operation fails or if three ECC errors are detected. A block that is marked bad is retired from use, thus improving the robustness of the file system. This policy is suitable for SLC flash. MLC policies are under development.</p>\r\n\r\n<p>Further, NAND flash cells can sometimes be disturbed (corrupted) by NAND activity and charge loss. These errors are corrected by error correction codes (ECC) which may be implemented in hardware, software drivers, or within Yaffs itself. Again, any flash file system lacking effective ECC handling is unsuitable for NAND flash.</p>\r\n\r\n<p>Yaffs1 mode can use either the built in ECC or use ECC provided by the driver or hardware.</p>\r\n\r\n<p>Since Yaffs2 mode is designed for a wider range of devices, it does not provide ECC internally but requires that the driver provide the ECC.</p>\r\n\r\n<p>The ECC code supplied with Yaffs (yaffs_ecc.c) is the fastest C code implementation of a Smart Media compatible ECC algorithm that we are aware of. This code will correct any single bit error within a 256-byte data block and detect 2 errors per 256-byte data block. This is sufficient error correction to provide very high reliability on SLC-type NAND flash.</p>\r\n\r\n<p>For a more complete description of failure handling, please refer to the Yaffs error mitigation document.</p>\r\n\r\n<h2>12 RAM structures</h2>\r\n\r\n<p>While it is theoretically possible to use a log structured file system with very few RAM structures, this would provide reduce performance. Thus, significant RAM data structures are required to store enough information to provide sufficiently high performance.</p>\r\n\r\n<p>Before we jump in, it is a good idea to first figure out what purpose the RAM structures serve. The main structures are defined in yaffs_guts.h and their main purposes, are :</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Device/partition: This is named <em><strong>yaffs_dev</strong></em>. This is required to hold information relating to a Yaffs “partition” or “mount point”. Using these, rather than globals, allows Yaffs to simultaneously support multiple partitions and partitions of different types. In fact, almost other data structures mentioned here are part of, or accessed via, the structure.</p>\r\n	</li>\r\n	<li>\r\n	<p>NAND Block Information: This is named <em><strong>yaffs_block_info </strong></em>and holds the current state of the NAND blocks. Each yaffs_Device has an array of these.</p>\r\n	</li>\r\n	<li>\r\n	<p>NAND Chunk Information: This is a bitfield attached to the yaffs_Device that holds the current in-use state of each chunk in the system. There is one bit per chunk in the partition.</p>\r\n	</li>\r\n	<li>\r\n	<p>Object: This is named <em><strong>yaffs_obj</strong></em> and holds the state of an object (see section 3).There is one of these for each object in the file system, where an object is one of a regular file, directory, hard link, symbolic link or special link. There are different variants of yaffs_Objects to reflect the different data required for these different object types. Each object is uniquely identified by an objectId.</p>\r\n	</li>\r\n	<li>\r\n	<p>File structure: For each file object, Yaffs holds a tree which provides the mechanism to find the data chunks in a file. The tree consists of nodes called <em><strong>yaffs_tnode</strong></em> (tree nodes).</p>\r\n	</li>\r\n	<li>\r\n	<p>Directory structure: The directory structure allows objects to be found by name. The directory structure is built from a doubly-linked list that is rooted in the directory and binds together the sibling objects within a directory.</p>\r\n	</li>\r\n	<li>\r\n	<p>Object number hash table: The object number hash table provides a mechanism to find an object from its objectId. The objectId is hashed to select a hash bucket. Each hash bucket has a doubly-linked list of objects that belong in this hash bucket, as well as a count of the objects in the bucket.</p>\r\n	</li>\r\n	<li>\r\n	<p>Cache: Yaffs provides a read/write cache that significantly improves performance for short operations. The size of the cache can be set at run-time.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The usage of these structures is described in more detail below.</p>\r\n\r\n<p>Yaffs makes significant use of doubly-linked lists. These data structures are very useful because they provide rapid insertion, removal and traversal. They do however come at the expense of requiring two pointers rather than just one.</p>\r\n\r\n<h3>12.1 Yaffs objects</h3>\r\n\r\n<p>Each object (file, directory,...) in the system is represented by a yaffs_obj. The yaffs_obj\'s main function is to store most of the object metadata and type-specific information. This means that almost all the information about an object can be accessed immediately without reading flash.</p>\r\n\r\n<p>The metadata includes, amongst others:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>objectId: The number used to identify the object.</p>\r\n	</li>\r\n	<li>\r\n	<p>parent: A pointer to the parent directory. Not all objects have a parent. The root director and special directories for unlinked and deleted files do not have parents so this is NULL.</p>\r\n	</li>\r\n	<li>\r\n	<p>Short name: If the name is short enough to fit in a small fixed-sized array (default 16 characters) then it is stored here otherwise it must be fetched from flash each time it is requested.</p>\r\n	</li>\r\n	<li>\r\n	<p>type: Type of object.</p>\r\n	</li>\r\n	<li>\r\n	<p>Permissions, time, ownership and other attributes</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Depending on the object type, yaffs_obj also stores:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>A tnode tree, file extents etc for data files</p>\r\n	</li>\r\n	<li>\r\n	<p>A directory chain for directories</p>\r\n	</li>\r\n	<li>\r\n	<p>An equivalent object for hard links</p>\r\n	</li>\r\n	<li>\r\n	<p>A string for symbolic links</p>\r\n	</li>\r\n</ul>\r\n\r\n<h3>12.2 Look-up by object id</h3>\r\n\r\n<p>The purpose of this mechanism is to rapidly access an object by its device and object Id. This is required during garbage collection, scanning and similar operations.</p>\r\n\r\n<p>Each yaffs_dev uses a hash table for this purpose. The hash table has 256 “buckets” (tunable), each holding a doubly-linked list of the yaffs_objs in that bucket.</p>\r\n\r\n<p>The hash table\'s hash function is just masking the least significant bits of the object Id. This is a very cheap function that provides reasonable “spread”.</p>\r\n\r\n<p>Yaffs makes an attempt to assign objectIds in a way that keeps the number of objects in each hash bucket quite low, thus preventing any one hash entry getting too long.</p>\r\n\r\n<p>This mechanism is also charged with assigning new (unique) object Ids.</p>\r\n\r\n<h3>12.3 Directory structure</h3>\r\n\r\n<p>The purpose of the directory structure is to rapidly access an object by name. This is required to perform file operations such as opening, renaming etc.</p>\r\n\r\n<p>The Yaffs directory structure is formed by a tree of objects of type YAFFS_OBJECT_TYPE_DIRECTORY. These objects have a doubly linked node of children.</p>\r\n\r\n<p>Each yaffs_obj also has a doubly linked list node called sibling which chain together the siblings in a directory.</p>\r\n\r\n<p>Each Yaffs partition has a root directory.</p>\r\n\r\n<p>Each yaffs partition also has some special purpose “fake” directories which are not stored in NAND but are instead created on mounting:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>lost+found: This directory is used as a place to store any lost file parts that cannot be placed in the normal directory tree.</p>\r\n	</li>\r\n	<li>\r\n	<p>Unlinked and deleted: These are pseudo-directories that are not in the directory tree. Placing objects in these directories gives them a status of being unlinked or deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Each object has a name that is used to look up the object within the directory, Thus, each name within a directory must be unique. This rule does not apply to objects in the unlinked and deleted pseudo-directories since we never use names to look up unlinked/deleted files and there may be many files of the same name that have been deleted.</p>\r\n\r\n<p>Name resolution is sped up by two mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Short names are stored directly in the yaffs_obj so that they don\'t have to be loaded from flash.</p>\r\n	</li>\r\n	<li>\r\n	<p>Each object has a “name sum”. This is a simple hashing of the name which speeds up the matching process because it is cheaper to compare the hash than compare the whole name string.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<h2>[ibimage==216==original==none==self==null]</h2>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The above directory structure represents the following directory tree</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>/</p>\r\n			</td>\r\n			<td>\r\n			<p>Root directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a</p>\r\n			</td>\r\n			<td>\r\n			<p>Directory containing c,d,e</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/b</p>\r\n			</td>\r\n			<td>\r\n			<p>Empty directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/lost+found</p>\r\n			</td>\r\n			<td>\r\n			<p>Empty directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/c</p>\r\n			</td>\r\n			<td>\r\n			<p>Directory</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/d</p>\r\n			</td>\r\n			<td>\r\n			<p>Data file</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>/a/e</p>\r\n			</td>\r\n			<td>\r\n			<p>Data file</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>If a Yaffs partition has been somehow corrupted then it is possible for an object to be created during scanning but with no object header (which contains the object name) being found. The object would then not have a name. In these cases, Yaffs creates a pseudonym from the object number of the form objnnnn so that the object can be placed in the directory structure.</p>\r\n\r\n<h3>12.4 Hard links</h3>\r\n\r\n<p>In POSIX file systems, the objects (inodes in Linux terminology, vnode in BSD-speak) and the object names (dentry in Linux terminology) are independent in that an object can have zero, one or more names.</p>\r\n\r\n<p>Having files with zero links is caused by constructs such as:</p>\r\n\r\n<p><code>h = open(“foo”,O_CREAT | O_RDWR, S_IREAD | S_IWRITE); /* create file “foo” */</code></p>\r\n\r\n<p><code>unlink(“foo”); /* unlink it so it can\'t be found in the directory */</code></p>\r\n\r\n<p><code>/* we still have a handle to the file so can still access it via the handle */</code></p>\r\n\r\n<p><code>read(h,buffer,100); </code></p>\r\n\r\n<p><code>write(h,buffer,100);</code></p>\r\n\r\n<p><code>close(h); /* as soon as the handle is closed the file is deleted */</code></p>\r\n\r\n<p>Yaffs does not store separate object and name records because that would waste space and incur extra reading/writing. Instead, Yaffs is optimised for the most typical case of one name per object. That is, Yaffs stores one name per yaffs_obj and uses “cheats” to achieve other behavior:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Objects that have zero names (ie unlinked objects) are handled by renaming the object into a fake directory that is used to designate unlinked objects.</p>\r\n	</li>\r\n	<li>\r\n	<p>Multiple names object are handled by designating one of the objects the main object and having multiple hard link pseudo-objects that refer to it.</p>\r\n	</li>\r\n	<li>\r\n	<p>The inode number (vnid in BSD-speak) is a per-file system unique number that identifies the object. The Yaffs object id of the main object is presented to the user. That is, if the inode number is queried from a hardlink object then the object id of the equivalent object is returned.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The linkage between hard link pseudo-objects and their equivalent object is through two fields:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Each object has a hardLinks doubly linked list node that links the list of hard links to that object.</p>\r\n	</li>\r\n	<li>\r\n	<p>Each hard link has a pointer to the “equivalent object”.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The usage of this structure is straight forward except for one interesting case when hard links are being deleted. Consider the following sequence of events:</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>1</p>\r\n			</td>\r\n			<td>\r\n			<p># touch a</p>\r\n			</td>\r\n			<td>\r\n			<p>Create file a</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>2</p>\r\n			</td>\r\n			<td>\r\n			<p># ln a b</p>\r\n			</td>\r\n			<td>\r\n			<p>Create a hard link b with equivalent object a.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>3</p>\r\n			</td>\r\n			<td>\r\n			<p># rm a</p>\r\n			</td>\r\n			<td>\r\n			<p>Remove a. The object must still exist under the name b</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p>In step 3, Yaffs cannot just delete the equivalent object since that would leave a “hanging” hard link with no equivalent object.</p>\r\n\r\n<p>We also cannot just change the type of one of the hardlink objects because that would cause the object\'s object id to change. This would be unacceptable since it would break any mechanism that use the object id (of which there are many within and outside of Yaffs).</p>\r\n\r\n<p>Thus, if an object is deleted and it has hard links to it then we do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Select one of the object\'s hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p>Rename the object to the name in the hard link.</p>\r\n	</li>\r\n	<li>\r\n	<p>Delete the hard link pseudo-object.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h3>12.5 Symbolic links and special objects</h3>\r\n\r\n<p>These object types just store values that are needed to provide a full POSIX file system.</p>\r\n\r\n<p>A symbolic link object saves a symbolic link string that is used to provide the POSIX symbolic string mechanism.</p>\r\n\r\n<p>A special object is used to store device numbers, pipes and other similar special objects.</p>\r\n\r\n<h3>12.6 File objects</h3>\r\n\r\n<p>The yaffs_Objects have a type and a variant portion which is a union of different data required to hold the state of different object types.</p>\r\n\r\n<p>The file objects have type YAFFS_OBJECT_TYPE_FILE, and an associated file variant. This stores the following main values:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>fileSize: file size</p>\r\n	</li>\r\n	<li>\r\n	<p>topLevel: depth of the Tnode tree</p>\r\n	</li>\r\n	<li>\r\n	<p>top: pointer to top of Tnode tree</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The meaning of the file size is quite obvious. This is the current length of the file. Note that a file might have holes in it caused by seeking forward past the extents of the file.</p>\r\n\r\n<p>The top and topLevel work together to control the Tnode trees.</p>\r\n\r\n<h4>12.6.1 Tnode tree</h4>\r\n\r\n<p>Each file has a tnode tree to provide the mapping from file position to actual NAND chunk address. top is a pointer to the top of the tnode tree</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>[ibimage==220==original==none==self==null]<br />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The tnode tree is made up of tnodes (tree nodes) arranged in levels, each of which holds either:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>At levels greater than 0, a tnode has 8 pointers to other tnodes in the next level down.</p>\r\n	</li>\r\n	<li>\r\n	<p>At level 0, a tnode has 16 NAND chunk Ids which identify the chunk\'s location in RAM.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>These numbers are powers of 2 which makes for simple look-ups by just applying bitmasks. The code that traverses this tree is in yaffs_FindLevel0Tnode().</p>\r\n\r\n<p>As the file size grows, more tnodes are added and as the levels fill, more levels are added. If the file is truncated or resized downwards, then the tree is pruned. If a chunk in a file is replaced (eg. data is overwritten or copied by garbage collection) then the tnode tree must be updated to reflect the change.</p>\r\n\r\n<p>Tnodes typically make up the bulk of the Yaffs\' RAM usage.</p>\r\n\r\n<h2>13 How various mechanisms work</h2>\r\n\r\n<p>Having looked at the Yaffs data structures, we now consider how some of the mechanisms in Yaffs operates. This is most easily done by walking through some operations that Yaffs performs.</p>\r\n\r\n<h3>13.1 Block and chunk management</h3>\r\n\r\n<h4>13.1.1 Block states</h4>\r\n\r\n<p>Yaffs tracks the state of each block and chunk in the partition. This information is first built up during scanning (or recovered from a checkpoint). A block may be in one of the following states (see definition of yaffs_BlockState):</p>\r\n\r\n<table>\r\n	<colgroup>\r\n		<col />\r\n		<col />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr>\r\n			<td>\r\n			<p>UNKNOWN</p>\r\n			</td>\r\n			<td>\r\n			<p>The block state is not yet known.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>NEEDS_SCANNING</p>\r\n			</td>\r\n			<td>\r\n			<p>During pre-scanning it has been determined that this block has something on it and needs scanning.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>SCANNING</p>\r\n			</td>\r\n			<td>\r\n			<p>This block is currently being scanned.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>EMPTY</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has nothing in it (is erased) and may be used for allocation. The block can get to this state by being erased.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>ALLOCATING</p>\r\n			</td>\r\n			<td>\r\n			<p>This is the block currently being used for allocation by the chunk allocator.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>FULL</p>\r\n			</td>\r\n			<td>\r\n			<p>All the chunks in this block have been allocated and at least one chunk contains useful information that has not yet been deleted.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>DIRTY</p>\r\n			</td>\r\n			<td>\r\n			<p>All the chunks in this block have been allocated and all have been deleted. The block might have been in the FULL or COLLECTING state before this. This block can now be erased and returned to EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>CHECKPOINT</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has checkpoint data in it. When the checkpoint is invalidated this block will be erased and returned to the EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>COLLECTING</p>\r\n			</td>\r\n			<td>\r\n			<p>This block is being garbage collected. As soon as all live data has been inspected this block becomes DIRTY.</p>\r\n			</td>\r\n		</tr>\r\n		<tr>\r\n			<td>\r\n			<p>DEAD</p>\r\n			</td>\r\n			<td>\r\n			<p>This block has been retired or was marked bad. This is a terminal state.</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p><br />\r\n&nbsp;</p>\r\n\r\n<p>[ibimage==224==original==none==self==null]</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>The regular runtime states are; EMPTY, ALLOCATING, FULL, COLLECTING and DIRTY.</p>\r\n\r\n<p>The checkpoint state is used for checkpoint blocks.</p>\r\n\r\n<p>The DEAD state is the terminal state for blocks that were retired because they were either marked bad or an error was detected.</p>\r\n\r\n<h4>13.1.2 Block and chunk allocation</h4>\r\n\r\n<p>An available chunk must be allocated before it can be written. The chunk allocation mechanism is provided by yaffs_AllocateChunk() and is very simple. Each partition has a current block that it is allocating from. This is termed the <em><strong>allocation block</strong></em>. Chunks are allocated sequentially from the allocation block. As the chunks are allocated, block and chunk management information is updated, including the block\'s pagesInUse count and the chunk use bitmap.</p>\r\n\r\n<p>When the block is fully allocated, another empty block is selected to become the allocation block. The blocks are selected by searching upwards from the previous allocation block.</p>\r\n\r\n<p>Previous to yaffs_guts.c V1.99, Yaffs scanning would identify the allocation block being used when the file system was last shut down and would continue allocation from that point. This had the benefit of continuing to use the same block and slightly reduce garbage creation . However, doing this could potentially cause problems due to attempting to write a block that had been in the middle of a write when power was lost. Thus Yaffs now always starts allocation on a new block after a mount. This slightly increases the amount of garbage (typically by an insignificant amount) but improves robustness.</p>\r\n\r\n<h4>13.1.3 A word about wear leveling</h4>\r\n\r\n<p>Since flash memory blocks can only endure a limited number of writes and erases ( termed the flash endurance), a flash file system needs to ensure that the same few blocks are not overwritten continuously causing those blocks to wear out while the other blocks are hardly used at all.. Instead it is important that the file system spreads out the wear.</p>\r\n\r\n<p>This is extremely important in a file system such as FAT which uses a limited set of logical blocks to store the file allocation table entries which get modified frequently. If FAT is used with a static logical to physical block mapping then the file allocation table blocks will wear out far before other blocks causing premature device failure. Thus, for FAT, it is very important that the logical to physical mapping gets stirred up a bit to cause the file allocation tables to be written to different physical blocks, thus leveling out the wear.</p>\r\n\r\n<p>Wear leveling is far less of a concern in a log structured file system such as Yaffs since we always write to the end of the log and thus don\'t keep on overwriting the same blocks.</p>\r\n\r\n<p>There are basically two ways to achieve wear leveling:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Use a specifically written set of functions that actively perform wear leveling.</p>\r\n	</li>\r\n	<li>\r\n	<p>Allow a degree of wear leveling to happen as a side effect of other activity.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Yaffs takes the second approach.</p>\r\n\r\n<p>Firstly, being log structured, Yaffs inherently spreads out the wear by performing all writes in sequence. That means that we don\'t single out a few blocks for a specific purpose and write them more than others.</p>\r\n\r\n<p>Secondly, blocks are allocated serially from the erased blocks in the partition. Thus the process of erasing (after deletion of garbage collection) and then allocating blocks does tend to spread the block use and provides a level of wear leveling.</p>\r\n\r\n<p>Thus, even though there is no code to specifically perform wear leveling it does happen as a side effect of other activity.</p>\r\n\r\n<p>This strategy performs well and has survived many accelerated lifetime tests in both simulations and on real NAND-based products.</p>\r\n\r\n<h4>13.1.4 Management of yaffs_tnode and yaffs_obj structures</h4>\r\n\r\n<p>yaffs_tnodes and yaffs_objs are dynamically created and released as they are needed.</p>\r\n\r\n<p>The allocation and management could be done by just using the standard memory allocator (malloc, kmalloc etc), but this could cause performance problems, particularly in embedded systems which tend to have rather simplistic memory allocators.</p>\r\n\r\n<p>The allocation and deletion of many small structures can cause memory fragmentation causing out-of-memory and slowdown problems.</p>\r\n\r\n<p>To prevent these problems, Yaffs manages these objects internally. Yaffs creates many structures (100 or so) at a time and then allocates them singly from a free list. When structures are released they are placed on the free list for reuse.</p>\r\n\r\n<h3>13.2 Internal cache</h3>\r\n\r\n<p>This describes the standard Yaffs internal cache implementation. Alternative caches have been devised for other purposes and at some future point Yaffs might be extended to support pluggable cache policies.</p>\r\n\r\n<p>The main purpose of the internal cache is to reduce NAND access for badly behaved applications which perform many small reads and writes. Believe it or not, but there are applications out there that read or write files a few bytes at a time:</p>\r\n\r\n<p><code>while(...)<br />\r\nwrite(h,buf,1);</code></p>\r\n\r\n<p>This sort of behavior would be terrible for performance and flash memory life if each read or write operation resulted in a write to flash. Yaffs provides an internal cache to mitigate against this sort of behaviour.</p>\r\n\r\n<p>The Yaffs internal cache is called a “Short Op Cache” (short operation cache) and only comes in to play for operations which are not chunk aligned. The caching mechanism is very simple and was primarily written for less sophisticated OSs that lack their own file caching layer. The cache is still beneficial in OSs like Linux, which provide a caching layer, because it caches both the read and write path. Thus, while short reads will be serviced by the Linux page cache, the shortOpCache does address non-chunk aligned writes.</p>\r\n\r\n<p>Page aligned reads/writes bypass the cache unless that chunk is already in the cache.</p>\r\n\r\n<p>The cache is stored in an array of cache entries. The number of cache entries is set in the yaffs_Device configuration by dev-&gt;nShortOpCaches and the cache is configured during initialisation. A cache size of zero disables the cache. The cache management algorithms are simplistic and probably don\'t scale well to very large numbers. This means that it is probably best to stick with around 5 to 20 cache entries.</p>\r\n\r\n<p>Each cache entry holds:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>object id, chunk id, nbytes (the tag info of the chunk being cached)</p>\r\n	</li>\r\n	<li>\r\n	<p>The actual data bytes</p>\r\n	</li>\r\n	<li>\r\n	<p>Cache slot status (last used counter, dirty,...)</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Finding an unused cache entry is a simple operation. We just iterate through the cache entries searching for one that is not busy. If all cache entries are busy then a cache push-out is performed and then we retry the find.</p>\r\n\r\n<p>Whenever a cache entry is accessed (read or written) the last-use counter is is set to an incrementing number. This counter value is used to drive the least-recently-used (LRU) algorithm.</p>\r\n\r\n<p>Cache entries which have been modified and not yet written back to flash are marked with a dirty flag. This is tells us whether we need to write out the data during a flush or push-out.</p>\r\n\r\n<p>A push-out happens whenever the cache is full and we need to push an entry out of the cache to make space to load a new entry. Yaffs uses the LRU algorithm to select the LRU cache entry and then write out all dirty entries for that file, in ascending order, and mark these entries as free.</p>\r\n\r\n<p>Whenever a file is flushed (file flush or close), the file\'s dirty entries are written out. At certain times (eg. before an unmount or at sync()) the entire cache is flushed.</p>\r\n\r\n<p>This caching policy is gives a useful performance improvement with very simple algorithms (ie good bang-for-bucks).</p>\r\n\r\n<p>Apart from its normal caching function, the shortOpCache also does much of the heavy lifting for inband tags support since all the inband tags accesses are not chunk aligned and must typically be re-aligned. The cache makes this easy.</p>\r\n\r\n<h3>13.3 Scanning</h3>\r\n\r\n<p>Scanning is the process whereby file system state is rebuilt from scratch. This happens on mounting a Yaffs partition if no checkpoint data is available or if the mounting was told to ignore the checkpoint data.</p>\r\n\r\n<p>Scanning involves reading the tags from all the active chunks in the system so can take quite a while, but probably less time than you\'d expect.</p>\r\n\r\n<p>The scanning process is different for Yaffs1 vs Yaffs2 because of the differences mentioned before.</p>\r\n\r\n<h4>13.3.1 Yaffs1 scanning</h4>\r\n\r\n<p>Yaffs1 scanning is rather straight forward because Yaffs1 uses deletion markers.</p>\r\n\r\n<p>All we really need to do is read the tags off all the chunks in the partition and determine if the chunk is active. Order is not important and blocks could be read in any order. If the deletion marker has not been set then it is active and we need to add the chunk to the file system state as follows:</p>\r\n\r\n<p>If the chunk is a data chunk (chunkId &gt; 0) then the chunk must be added to the relevant file\'s tnode tree. If the object does not exist, then we create it in lost+found, although it has no name and other file details yet.</p>\r\n\r\n<p>If the chunk is an object header (chunkId == 0) then the object must be created (or it might already exist in lost + found and we need to fix up its details and slot it into the correct part of the directory tree.</p>\r\n\r\n<p>If at any time during scanning we find that we are reading a second chunk with the same objectId:chunkId as a previously encountered chunk then the 2-bit serial number is used to resolve the dispute.</p>\r\n\r\n<p>Once the scanning is complete we still need to fix up a few things:</p>\r\n\r\n<p>Hard links must be connected up properly (it is more efficient to defer this until after the scanning has completed).</p>\r\n\r\n<p>Also, objects which were marked as unlinked must be deleted since there are no longer handles open to these objects.</p>\r\n\r\n<h4>13.3.2 Yaffs2 scanning</h4>\r\n\r\n<p>Yaffs2 has no deletion markers which makes the scanning significantly more complex.</p>\r\n\r\n<p>The first thing that needs to be done is to pre-scan the blocks to determine the sequence number for the blocks. The list of blocks and sequence numbers is then sorted to make a chronologically ordered list.</p>\r\n\r\n<p>Yaffs then scans <em><strong>backwards</strong></em> through these blocks (ie in reverse chronological order). Thus, the first encountered objectId:chunkId pair is the current one and subsequent chunks with those tags are out of date.</p>\r\n\r\n<p>We read the tags for each chunk in each block.</p>\r\n\r\n<p>If it is a data chunk ( ie. chunkId &gt; 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>If a chunk with the same objectId:chunkId has already been found then this is not the current chunk. Delete this one.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object is marked as deleted, or corrupted, then delete it.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object does not yet exist then this chunk was written since the last object header write for this object so this chunk must be the last written chunk in the file and must have happened just before an unclean shutdown with the file still open. We can create the file and use the chunkId and nbytes to set the file extents.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if the object does exist and the chunk lies beyond the object\'s file scan size (extents) then it is a truncated chunk and can be deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else it is placed into the file\'s tnode tree.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If it is an object header (ie. chunkId == 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>If the object is in the deleted directory then mark the object as deleted and set the object\'s shrink size to zero.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else if an object header has not yet been found for this object, then this is the most current object header and holds the most current name etc. If no data chunks have yet been found for this chunk then the size in the object header is also the file extents. The file size is used to set the file scan size since at the time it was written the file did not extend past that point and we can safely ignore further data chunks for that file that extend past that point.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else just use the file size as the scan size if it is smaller than the current file scan size.</p>\r\n	</li>\r\n	<li>\r\n	<p>Else this an obsolete object header. It is deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>A bit more explanation is probably needed to describe the file scan size. The whole purpose of this is to determine data chunks that should be ignored due to the file being truncated.</p>\r\n\r\n<h3>13.4 Checkpoint</h3>\r\n\r\n<p>Mount scanning takes quite a lot of time and slows mounting, though perhaps by less than you\'d think. Checkpointing is a mechanism to speed the mounting by taking a snapshot of the Yaffs runtime state at unmount or sync() and then reconstituting the runtime state on re-mounting. The speed up is dramatic.</p>\r\n\r\n<p>The actual checkpoint mechanism is quite simple. A “stream” of data is written to a set of blocks which are marked as holding checkpoint data and the important runtime state is written to the stream.</p>\r\n\r\n<p>Not all state needs to be saved, only the state needed to reconstitute the structure of the runtime data. For example, file metadata need not be saved since it is very easy to load that through lazy-loading.</p>\r\n\r\n<p>The checkpoint stores data in the following order:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Start marker, including checkpoint format id.</p>\r\n	</li>\r\n	<li>\r\n	<p>yaffs_Device information</p>\r\n	</li>\r\n	<li>\r\n	<p>Block information</p>\r\n	</li>\r\n	<li>\r\n	<p>Chunk flags</p>\r\n	</li>\r\n	<li>\r\n	<p>Objects, including file structure</p>\r\n	</li>\r\n	<li>\r\n	<p>End marker</p>\r\n	</li>\r\n	<li>\r\n	<p>Checksum</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>The checkpoint validity is ensured by the following mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>The data is stored using whatever ECC mechanisms are in place.</p>\r\n	</li>\r\n	<li>\r\n	<p>The start marker includes a checkpoint version so that an obsolete checkpoint will not be read if the checkpoint code changes.</p>\r\n	</li>\r\n	<li>\r\n	<p>Data is written to the stream as structured records, each with a type and size.</p>\r\n	</li>\r\n	<li>\r\n	<p>The end marker must be read when expected.</p>\r\n	</li>\r\n	<li>\r\n	<p>Two checksums are maintained across the whole checkpoint data set.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>If any checks fail, the checkpoint is ignored and the state is re-scanned.</p>\r\n\r\n<p>The astute reader might notice that block state changes as we\'re writing checkpoint blocks, so how do we manage to re-constitute the block states correctly? This is achieved by the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>As we write the checkpoint, the checkpoint block allocation selects EMPTY blocks. We don\'t change the state as we\'re writing the checkpoint so their state is written to the checkpoint as EMPTY or CHECKPOINT, it does not really matter which.</p>\r\n	</li>\r\n	<li>\r\n	<p>After reading the checkpoint, we know which blocks have been used to store checkpoint data. We update those blocks to reflect the CHECKPOINT state.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>A checkpoint is invalidated by any modifications (writes, erases,...) thus any modification path also checks to see if there is a checkpoint and erases it.</p>\r\n\r\n<p>The regular Yaffs block allocator and garbage collection logic also needs to be aware of the approximate size of the checkpoint so that these can ensure enough spare space is reserved to store a checkpoint. There is a calculation that updates the value whenever the number of objects etc changes.</p>\r\n\r\n<h3>13.5 Extended tags and packed tags</h3>\r\n\r\n<p>Extended tags is an abstraction of a way to store more information in the tags, allowing for faster scan times. Packed Tags is a physical implementation of the storage of extended tags.</p>\r\n\r\n<p>When we\'re scanning we really only need to build up the structure of the file system. In other words, rebuild the directory relationships, tnode trees and such. Much of the detail such as file names, permissions etc can be lazy loaded.</p>\r\n\r\n<p>This required storing extra information, such as the parent directory and object type, in the tags of object headers. But how do we do this without enlarging the tags size?</p>\r\n\r\n<p>Object headers always have a chunkId of zero and nbytes has no meaning because it is not a data chunk. By using a single bit to identify object headers we suddenly open up a whole lot of space that we can stuff full of useful data. By being clever as to what data we use, we can pack in the important data in most circumstances.</p>\r\n\r\n<p>Sometimes, for example very large files, the packing will not work because there are insufficient bits to hold the field so we just write regular tags instead. That\'s OK since there can only be few such files in the system so the performance reduction only impacts very few files and the system level impact is unnoticeable. A simple, cheap, strategy that gives speed up 99% of the time is a very handy one.</p>\r\n\r\n<p>Other details are lazy-loaded on demand. This will happen when the object is looked up (eg. by a file open or searching for the file in the directory).</p>\r\n\r\n<h3>13.6 Block summaries</h3>\r\n\r\n<p>Scanning requires that the tags for each chunk are read so that they can be inspected. Reading the tags from each chunk can be time consuming. Thus, block summaries were added in 2011.</p>\r\n\r\n<p>Block summaries write all the tags data for chunks in the block into the last few (typically only one) chunk in a block. This allows all the tags for chunks in that block to be read in one hit. Depending on the system throughput (CPU, RAM and flash speeds), this can speed up mount time dramatically.</p>\r\n\r\n<p>If a block summary is not available for a block, then the old mechanism is still used to scan the tags on each chunk.</p>\r\n\r\n<h3>13.7 Inband tags</h3>\r\n\r\n<p>Thus far we have considered chunks where there are sufficient unallocated bytes in the spare (or out of band) area to store the tags. This means that the data part of the page is a power of 2 in size. Powers of 2 are very fast to deal with which is why most operating systems use various powers of 2 in their internals.</p>\r\n\r\n<p>But what happens if we don\'t have enough space in the spare area to store the tags? Inband tags to the rescue! With inband tags we store the tags in the flash data area alongside the data. This gives us far more flexibility in the types of storage that Yaffs can support, but breaks the power-of-2 alignment. There are three major penalties with inband tags:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p>Due to the loss of chunk alignment, all transactions must pass through the shortOpCache. This costs an extra memcpy().</p>\r\n	</li>\r\n	<li>\r\n	<p>Offset to chunk, free space and other calculations are no longer just shift and mask operations but instead require multiplications and divisions which can relatively expensive.</p>\r\n	</li>\r\n	<li>\r\n	<p>Reading tags during scanning etc can involve reading the whole page, making this slower. The impact is minimised by using checkpointing.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p>Thus, it is better, performance wise, to only use inband tags when regular tags will not work.</p>\r\n\r\n<h3>13.8 Soft deletion</h3>\r\n\r\n<p>Soft deletion is a mechanism that was added to speed up file deletion in Yaffs1 mode.</p>\r\n\r\n<p>Looking back to the description of how files are stored in Yaffs1, you can see that deleting a file involves writing the deletion marker of every data chunk in the file. This can take quite some time for large files.</p>\r\n\r\n<p>The soft deletion mechanism was added to avoid writing all those deletion markers. Instead of writing the deletion markers, soft deletion instead tracks the number of chunks in each block that belong to deleted files and leaves the clean up to the garbage collector.</p>\r\n\r\n<p>When the garbage collector looks at a block it can see what current chunks (ie chunks not yet deleted) belong to deleted files and does not copy those, thus effectively deleting the chunk. Once all the chunks associated with a deleted file have cleaned up by the garbage collector the file header object can also be deleted.</p>\r\n\r\n<p>Yaffs2 mode does not write deletion markers and thus does not use soft deletion.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n','This document gives a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical. This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.','full_html');
INSERT INTO `node_revision__body` VALUES ('paper',0,409,593,'und',0,'<div dir=\"ltr\" id=\"Frame2\" style=\"position: absolute; top: 12.36cm; left: 4.16cm; width: 13.82cm; height: 3.72cm; border: none; padding: 0cm; background: #ffffff\">\r\n<p align=\"center\" class=\"frame-contents-western\"><sdfield subtype=\"TITLE\" type=\"DOCINFO\">How Yaffs Works</sdfield></p>\r\n\r\n<p align=\"center\" class=\"frame-contents-western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><font face=\"Liberation Sans, sans-serif\"><font size=\"4\" style=\"font-size: 16pt\">Charles Manning</font></font></p>\r\n\r\n<p align=\"center\" class=\"western\"><sdfield sdnum=\"2057;1033;YYYY-MM-DD\" type=\"DATETIME\">2017-06-06</sdfield></p>\r\n</div>\r\n\r\n<p><span class=\"sd-abs-pos\" style=\"position: absolute; top: 3.8cm; left: 8.12cm; width: 226px\"><img src=\"data:image/png;base64,[encoded data, 10936 characters]\" /> </span></p>\r\n\r\n<p align=\"center\" style=\"margin-top: 0.42cm; orphans: 0; widows: 0; page-break-after: avoid\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" style=\"margin-top: 0.42cm; orphans: 0; widows: 0; page-break-before: auto; page-break-after: avoid\">&nbsp;</p>\r\n\r\n<p align=\"justify\" class=\"western\">This document aims to give a reasonable explanation of most of the core mechanisms that make Yaffs work.</p>\r\n\r\n<p align=\"justify\" class=\"western\">This document should serve as a first port of call for understanding Yaffs.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\" style=\"page-break-before: always\"><br />\r\n&nbsp;</p>\r\n\r\n<h1 class=\"western\">1 Purpose</h1>\r\n\r\n<p class=\"western\">The purpose of this document is to give a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical.</p>\r\n\r\n<p class=\"western\">This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.</p>\r\n\r\n<p class=\"western\">This document is updated periodically. Yaffs mechanism design changes from time to time and this document will not always reflect the most up to date code.</p>\r\n\r\n<h1 class=\"western\">2 What is Yaffs?</h1>\r\n\r\n<p class=\"western\">Yaffs stands for <i>Yet Another Flash File System</i>, a term coined by Charles Manning in 2001 when suggesting that the already cluttered flash file system space could do with yet another offering – this time a flash file system designed from the ground up to work with NAND flash.</p>\r\n\r\n<p class=\"western\">The first release of Yaffs was developed during the end of 2001 and the beginning of 2002. By mid-2002, Yaffs was being trialled in various products. In May 2002, Yaffs was announced and a few more projects started playing with it. In June, porting to other OSs started. In September 2002, Yaffs was announced on linuxdevices.com which started a far wider uptake of Yaffs.</p>\r\n\r\n<p class=\"western\">Yaffs1, the original version of Yaffs, supported 512-byte page NAND devices with a SmartMedia-like memory layout.</p>\r\n\r\n<p class=\"western\">Yaffs2 is a follow-up to Yaffs1 extending Yaffs1 to support larger and different devices with different constraints.</p>\r\n\r\n<p class=\"western\">Yaffsis highly portable and has been used in many different products and applications as varied as sewing machines, point of sale, phones and aerospace. Yaffs has been used with multiple different operating systems. Native support is available for Linux, WindowsCE and eCOS while Yaffs Direct Interface provides a layer that can be used in other applications such as RTOSs. Yaffs has been used with multiple different CPUs and compilers.</p>\r\n\r\n<p class=\"western\">Yaffs has also been used as a NOR file system and even as a RAM file system.</p>\r\n\r\n<h1 class=\"western\">3 Design and coding strategies</h1>\r\n\r\n<p class=\"western\">Yaffs is designed to work in multiple environments which drives the need for portability. Portability also improves the debugging and testing since it is easier to develop and debug code in an application environment than within an OS kernel. This generally allows Yaffs to progress faster with less programming resources and provides more varied testing. Primary strategies that improve portability include:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">No OS-specific features used in main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">No compiler-specific features used in the main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Abstract types and functions used to allow Unicode or ASCII operation.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\" style=\"page-break-before: auto; page-break-after: avoid\">Simplicity is another key goal. Complexity is limited to those situations that provide significant improvements to performance or utility. Simplicity improves robustness and the ease of integration and development. Primary strategies that improve simplicity are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Single threaded model. Yaffs is locked on a per-partition basis at a high level. This is simpler than tracking lower-level locking. Yaffs Direct Interface uses a single lock for all partitions.</p>\r\n	</li>\r\n</ul>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Log structure makes for a simpler garbage collector and allocation methodology.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Abstractions that build layered code which is easier to understand, port and debug.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h1 class=\"western\">4 Terminology: Yaffs vs Yaffs1 vs Yaffs2</h1>\r\n\r\n<p class=\"western\">Before we embark on our quest to understand what Yaffs is, a terminology explanation is in order.</p>\r\n\r\n<p class=\"western\">There are two versions of Yaffs code: the original Yaffs code-base and the current Yaffs2 code-base. The Yaffs2 code-base supports functionality provided by the Yaffs code-base as well as being extended with new functionality to provide extra modes of operation, most importantly those required to work with larger and more modern NAND parts. The Yaffs2 code-base supports the Yaffs1 mode of operation through a backward compatibility mode.</p>\r\n\r\n<p class=\"western\">Unless explicitly mentioned, this text refers to the Yaffs2 code-base.</p>\r\n\r\n<p class=\"western\">This text uses three terms Yaffs, Yaffs1 and Yaffs2 depending on various modes of operation.</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs1 is a simpler mode of operation that uses deletion markers to track state. </font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs2 is a more complex mode of operating that was developed to support larger modern flash types, in particular MLC NAND, that cannot used deletion markers.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs means operations common to both.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Yaffs1 originally only worked with 512-byte page devices but has been extended to support larger flash pages such as Intel M18 flash with 1k pages.</p>\r\n\r\n<p class=\"western\">Yaffs2 was originally designed for 1k or larger page sizes but can be used with smaller pages by using in-band tags.</p>\r\n\r\n<p class=\"western\">Since the Yaffs1 mode of operation is simpler to understand than the Yaffs2 mode of operation, Yaffs1 will be described first and readers interested in Yaffs2 operation should first fully acquaint themselves with the Yaffs1 mode of operation. It is suggested that this document be read sequentially rather than browsing it.</p>\r\n\r\n<h1 class=\"western\">5 Objects</h1>\r\n\r\n<p class=\"western\">In Yaffs-speak, an Object is anything that is stored in the file system. These are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Regular data files</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Directories</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Hard-links</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Symbolic links</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Special objects (pipes, devices etc).</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">All objects are identified by a unique integer object id (<i><b>obj_id</b></i><i><b>)</b></i>.</p>\r\n\r\n<p class=\"western\">In the standard POSIX model, as used by Linux, there are inodes and directory entries (dentries).</p>\r\n\r\n<p class=\"western\">An inode may be a regular file, directory, or special file.</p>\r\n\r\n<p class=\"western\">Directory entries provide the naming mechanism that is used to locate inodes.</p>\r\n\r\n<p class=\"western\">Under POSIX, each inode may have zero, one or many dentries:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">One dentry per inode is commonly understood.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Multiple dentries per inode allow the same inode to be accessed via multiple names. This is achieved through hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Zero dentries per inode is less often understood. This happens when an inode is unlinked while there is still a handle open to the inode. The unlinking deletes the dentry but the inode still exists. As soon as the handle is closed the inode is deleted too.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h1 class=\"western\">6 The Yaffs NAND model</h1>\r\n\r\n<p class=\"western\">The memory in NAND flash is arranged in <i><b>pages.</b></i> A page is the unit of allocation and programming. In Yaffs, the unit of allocation is the<i> </i><i><b>chunk</b></i>. Typically a chunk will be the same as a NAND page, but there is flexibility to use chunks which map to multiple pages (eg. A system may have two NAND chips in parallel requiring 2x2048 = 4096 byte chunks) . This distinction gives a lot of flexibility in how the system can be configured. In this text the term page and chunk are synonymous unless stated otherwise.</p>\r\n\r\n<p class=\"western\">Many, typically 32 to 128 but as many as a few hundred, chunks form a <i><b>block</b></i>. A block is the unit of erasure.</p>\r\n\r\n<p class=\"western\">NAND flash may be shipped with bad blocks and further blocks may go bad during the operation of the device. Thus, Yaffs is aware of bad blocks and needs to be able to detect and mark bad blocks.</p>\r\n\r\n<p class=\"western\">NAND flash also typically requires the use of some sort of error detection and correction code (ECC). Yaffs can either use existing ECC logic or provide its own.</p>\r\n\r\n<h1 class=\"western\">7 How Yaffs1 stores files</h1>\r\n\r\n<p class=\"western\">Yaffs1 has a modified log structure, while Yaffs2 (see below) has a true log structure. A true log structured file system only ever writes sequentially. Yaffs1 uses deletion markers, which breaks the sequential write rule. Yaffs2 does not use deletion markers and is thus a true log structured file system.</p>\r\n\r\n<p class=\"western\">Instead of writing data in locations specific to the files, the file system data is written in the form of a sequential log, The entries in the log are all one chunk in size and can hold one of two types of chunk:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Data chunk</b></i>: A chunk holding regular data file contents.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Object Header</b></i>: A descriptor for an object (directory, regular data file, hard link, soft link, special descriptor,...). This holds details such as the identifier for the parent directory, object name, etc.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Each chunk has <i><b>tags</b></i> associated with it. The tags comprise the following important fields (others being ignored for now):</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Object Id (</b></i><i><b>obj_id</b></i><i><b>)</b></i>: Identifies which object the chunk belongs to.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Chunk Id (</b></i><i><b>chunk_id</b></i><i><b>)</b></i>: Identifies where in the file this chunk belongs. A chunk_id of zero signifies that this chunk contains an objectHeader. chunk_id==1 signifies the first chunk in the file (ie. At file offset 0), chunk_id==2 is the next chunk and so on.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Deletion Marker (</b></i><i><b>is_deleted</b></i><i><b>)</b></i><i><b>:</b></i> (Yaffs1 only) Shows that this chunk is no longer in use.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Byte Count</b></i><b>: </b><b>(n_bytes)</b>: Number of bytes of data if this is a data chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Serial Number </b></i><i><b>(ser</b></i><i><b>ia</b></i><i><b>l_number)</b></i>: (Yaffs1 only) Serial number used to differentiate chunks with the same obj_id and chunk_id.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\" style=\"page-break-before: always\">Now let\'s bring that all together in an explanation. This is a simplified explanation and skips over issues such as Soft Deletion (mentioned later in the document). For explanation purposes, we shall consider usage of fictitious NAND type that has 4 chunks per block, starting with an empty (erased) flash.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">First we\'ll create a file.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"56\" />\r\n		<col width=\"67\" />\r\n		<col width=\"60\" />\r\n		<col width=\"64\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\" style=\"font-weight: normal\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\" style=\"font-weight: normal\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"67\">\r\n			<p class=\"western\" style=\"font-weight: normal\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\" style=\"font-weight: normal\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\" style=\"font-weight: normal\">Deletion</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"271\">\r\n			<p class=\"western\" style=\"font-weight: normal\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td height=\"26\" sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"67\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Next we write a few chunks of data to the file.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"60\" />\r\n		<col width=\"64\" />\r\n		<col width=\"56\" />\r\n		<col width=\"290\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"290\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"290\">\r\n			<p class=\"western\">Object Header for t<font size=\"3\" style=\"font-size: 12pt\">his file (length 0)</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"290\">\r\n			<p class=\"western\">First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"290\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"290\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Next we close the file. This writers a new object header for the file. Notice how the previous object header is deleted.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"60\" />\r\n		<col width=\"68\" />\r\n		<col width=\"60\" />\r\n		<col width=\"282\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">Obsoleted object header <font size=\"2\" style=\"font-size: 10pt\">(length 0)</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"68\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"282\">\r\n			<p class=\"western\">New object header <font size=\"2\" style=\"font-size: 10pt\">(length n)</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\" style=\"page-break-before: always\">Let\'s now open the file for read/write, overwrite part of the first chunk in the file and close the file. The replaced data and object header chunks become deleted.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" style=\"page-break-inside: avoid\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"64\" />\r\n		<col width=\"64\" />\r\n		<col width=\"71\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">New first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">New object header</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Now let\'s resize the file to zero by opening the file with O_TRUNC and closing the file. This writes a new object header with length 0 and the contents are pruned making the data chunks deleted.</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"64\" />\r\n		<col width=\"64\" />\r\n		<col width=\"71\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Delted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"271\">\r\n			<p class=\"western\">New object header <font size=\"2\" style=\"font-size: 10pt\">(length 0).</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Note how all the pages in block 1 are now marked as deleted. This means that block 1 no longer contains useful information so we can now erase block 1 and reuse the space.</p>\r\n\r\n<p class=\"western\">We will now rename the file. To do this we write a new object header for the file</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" style=\"page-break-inside: avoid\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"38\" />\r\n		<col width=\"42\" />\r\n		<col width=\"58\" />\r\n		<col width=\"60\" />\r\n		<col width=\"60\" />\r\n		<col width=\"297\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"3\" style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"38\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"297\">\r\n			<p class=\"western\">New object header showing new name</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Block 2 now contains only deleted chunks and can be erased and reused.</p>\r\n\r\n<p class=\"western\">Note how the tags tell us:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Which chunks belong to which object</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">The position of the chunks within a file,</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Which chunks are current</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\" style=\"font-size: 12pt\">With that information we can recreate the state of the files regardless of the placement of the chunks in NAND. This means that should the system fail (crash, power fail,...) at any point during the sequences above, we are still able to recover the file system state up to that point.</font></p>\r\n\r\n<ul>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\" style=\"font-size: 12pt\">Note that there are no file allocation tables or similar structures stored in the flash. This reduces the amount of writing and erasing (improving write performance) while also improving robustness. Corruption of file allocation tables and similar structures is a common failure mechanism for embedded file systems. The lack of such structures makes Yaffs more robust. Or, to put it another way, you can\'t corrupt what you don\'t store.</font></p>\r\n\r\n<h1 class=\"western\">8 Garbage collection</h1>\r\n\r\n<p class=\"western\">As we have shown so far, when a block is made up only of deleted chunks, that block can be erased and reused. However, consider what will happen if we have many blocks which each have a few current chunks in them. Clearly we cannot erase any of the current blocks or we will destroy data. What we need to do is copy the useful data chunks off a block, deleting the originals and allowing the block to be erased and reused. This process is referred to as <i><b>garbage collection</b></i>.</p>\r\n\r\n<p class=\"western\">The Yaffs garbage collection process works as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">Find a block worth collecting (according the the heuristics below). If none found, then exit.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Iterate through the chunks in the block. If the chunk is in use then create a new copy and delete the original. Fix up the RAM data structures to reflect the change.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p class=\"western\">Once all chunks in this block have been deleted, the block can be erased ready for reuse.</p>\r\n\r\n<p class=\"western\">Note that although step 2 deletes the original chunk when it gets copied there is not much point in actually programming the deletion marker (is_deleted flag) in flash since we\'re about to erase the block anyway. Thus Yaffs does not program the deletion marker in these cases. If power is lost part way through the operation we can still tell the difference between the original and the copy by looking at the serial number (not that it really matters anyway since the two have the same contents so either can be used).</p>\r\n\r\n<p class=\"western\">The heuristics to determine whether a block is worth collecting are as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">If there are many erased blocks available, then Yaffs does not need to work hard, but will instead only attempt to perform garbage collection of blocks with very few chunks in use. This is termed <i><b>passive</b></i> garbage collection.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">If, however, there are very few erased blocks, Yaffs will work much harder to recover more space and will garbage collect blocks with more chunks in use. This is termed <i><b>aggressive</b></i> garbage collection.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p class=\"western\">OK, the passive/aggressive names are a bit silly, but they were contrived around midnight... give me a break!</p>\r\n\r\n<p class=\"western\">If garbage collection is aggressive, we need erased space urgency and the whole block is collected in a single garbage collection cycle. If the collection is passive then only a few chunks are collected at a time thus spreading the effort over many garbage collection cycles. This is done to reduce garbage collection load and improve responsiveness.</p>\r\n\r\n<p class=\"western\">The rationale behind the above heuristics is to delay garbage collection when possible to reduce the amount of collection that needs to be performed, thus increasing average system performance. Yet there is a conflicting goal of trying to spread the garbage collection so that it does not all happen at the same causing fluctuations in file system throughput. These conflicting goals make garbage tuning quite challenging.</p>\r\n\r\n<p class=\"western\">All flash file systems need some sort of garbage collector to reclaim space. The garbage collector is often a determining factor in the performance of the flash file system. Many file systems have to do a significant amount of effort in a single collection sweep.</p>\r\n\r\n<p class=\"western\">Yaffs only deals with one block at a time which constrains the amount of effort in a garbage collection cycle, thus reducing the “stall time”. Under normal conditions that garbage collection is performed a few chunks at a time, further reducing “stall time”.</p>\r\n\r\n<h1 class=\"western\">9 Yaffs1 serial numbers</h1>\r\n\r\n<p class=\"western\">In Yaffs1, each chunk is marked with a 2-bit serial number that is incremented every time a chunk with the same tags value is replaced (eg. When a chunk is overwritten or when copied by garbage collection).</p>\r\n\r\n<p class=\"western\">Now let\'s suppose first that we did not have a serial number.</p>\r\n\r\n<p class=\"western\">Let\'s consider a rename where an object header chunk must be replaced with a new one with the new name. Yaffs could do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Delete the old chunk.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Write the new chunk. </font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">If the system fails after the delete, the system could end up with no chunks matching the relevant tags values. This could cause files to be lost (end up in the lost+found directory). Thus, we have to write the new chunk before deleting the old chunk.</p>\r\n\r\n<p class=\"western\">But now consider what might happen if we did this:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">W<font face=\"Liberation Serif, serif\">rite new chunk.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Delete old chunk.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">If the system failed after the write we now have two chunks that match the tags. How can we tell which one is current?</p>\r\n\r\n<p class=\"western\">The current chunk is determined by inspecting the serial number. Since the serial number is only incremented by one before the old chunk is deleted, a 2-bit serial number is sufficient. Valid pairs are:</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"86\">\r\n	<colgroup>\r\n		<col width=\"35\" />\r\n		<col width=\"33\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"35\">\r\n			<p class=\"western\">Old</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"33\">\r\n			<p class=\"western\">New</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"35\">\r\n			<p class=\"western\">00</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"33\">\r\n			<p class=\"western\">01</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"35\">\r\n			<p class=\"western\">01</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"33\">\r\n			<p class=\"western\">10</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"35\">\r\n			<p class=\"western\">10</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"33\">\r\n			<p class=\"western\">11</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"35\">\r\n			<p class=\"western\">11</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"33\">\r\n			<p class=\"western\">00</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Thus, by inspecting the serial number attached to the old and new tags the current chunk can be determined and the old chunk can be deleted.</p>\r\n\r\n<h1 class=\"western\">10 Yaffs2 NAND model</h1>\r\n\r\n<p class=\"western\">Yaffs2 is an extension of Yaffs designed to fulfill a new set of objectives to work with newer NAND types including:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Zero overwrites. Yaffs1 only ever overwrites a single byte in the spare area of the chunks to set the deletion marker. More modern NAND devices are less tolerant of overwrites and Yaffs2 performs no overwrites at all.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Sequential chunk writing within a block. More modern NAND reliability specifications tend to assume sequential writing. Since Yaffs2 does not write deletion markers, the writing of chunks within a block is strictly sequential.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Some flash technologies – such as MLC – only work properly if pages are written sequentially. This is a fundamental requirement of how many MLC types perform the bit overlaying.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Since Yaffs2 performs less writes, there is potential for Yaffs2 to have improved write performance.</p>\r\n\r\n<p class=\"western\">Yaffs2 cannot use a deletion marker because that would violate the zero overwrites mandate and alternate mechanisms are provided to determine which chunks are current and which are deleted. These mechanisms are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Sequence Number </b></i><i><b>(seq_number)</b></i>: As each block is allocated, the file system\'s sequence number is incremented and each chunk in the block is marked with that sequence number. The sequence number thus provides a way of organising the log in chronological order.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Shrink Header Marker </b></i><i><b>(shrink_header)</b></i>: The shrink header marker is used to mark object headers that are written to shrink the size of a data file. This is explained more in the accompanying text.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><i><b>Note: The Yaffs2 sequence number is not the same as the Yaffs1 serial number!</b></i></p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Note: We still refer to chunk deletion in Yaffs2. The chunks are marked as deleted in the RAM data structures used by garbage collection etc, but there is no deletion marker written to flash.</p>\r\n\r\n<h2 class=\"western\">10.1 Sequence Number</h2>\r\n\r\n<p class=\"western\">The sequence number allows the chronological order to be determined which allows Yaffs to determine the sequence of events and restore the file system state. This is achieved by scanning <i><b>backwards</b></i> chronologically (ie. From the highest sequence number backwards to the lowest sequence number). Thus:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Since we\'re scanning backwards, the most recently written – and thus current – chunk matching an obj_id:chunk_id pair will be encountered first and all subsequent matching chunks must be obsolete and treated as deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The file length in object headers are used to trim back resized files. If an object header is encountered, then subsequent data chunks that lie beyond that file length are clearly obsolete and treated as deleted. Note that both current and obsolete object header sizes must be used to obtain an accurate reconstruction.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">10.2 Shrink Header</h2>\r\n\r\n<p class=\"western\">Explaining the purpose of the shrink header marker is a bit convoluted. The purpose of a shrink header marker is to show that this object header indicates that a file has shrunken in size, and to prevent those object headers from being deleted by garbage collection. To understand this, it is easiest to consider what would happen if the shrink header markers did not exist.</p>\r\n\r\n<p class=\"western\">For simplification we\'ll assume no garbage collection happens during this part of the exercise. It is safe to make that assumption since it does not alter the reasoning.</p>\r\n\r\n<p class=\"western\">Consider a file that has been through the following:</p>\r\n\r\n<p class=\"code-western\"><font face=\"Liberation Mono, monospace\"><font size=\"2\" style=\"font-size: 10pt\"><b>#define MB (1024 * 1024)</b></font></font><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"code-western\"><code class=\"western\"><font face=\"Liberation Mono, monospace\"><font size=\"2\" style=\"font-size: 9pt\">h = open(“foo”,O_CREAT| O_RDWR, S_IREAD|S_IWRITE); /* create file */</font></font></code><br />\r\n<code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">write(h,data,5*</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">MB</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">); /* write 5 MB of data /<br />\r\ntruncate(h,1*</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">MB</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">); /* truncate to 1MB */<br />\r\nlseek(h,2*</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">MB</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">, SEEK_SET); /* set the file access position to 2MB */<br />\r\nwrite(h,data,1*</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">MB</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 9pt\">); /* write 1MB of data */<br />\r\nclose(h);</font></code><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The file will now be 3MB in length, but there will be a “hole” between 1MB and 2MB. According to POSIX (and to aid security etc) that “hole” should always read back as zeros.</p>\r\n\r\n<p class=\"western\">Yaffs2 would represent that in NAND with the following sequence of chunks:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">Object header for file creation (file length = 0)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">5MB worth of data chunks (0 to 5MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object header for truncation (file length = 1MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">1MB worth of data chunks (2MB to 3MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object header for close (file length = 3MB)</p>\r\n\r\n	<p class=\"western\">At this stage, only the the following data chunks are current:</p>\r\n	</li>\r\n</ol>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">The first 1MB of data created in step 2 above.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The 1MB of data created in step 4 above.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The object header created in step 5 above.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">But Yaffs must remember the truncation that happened in step 3, otherwise Yaffs will “forget” that there is a hole, which would cause the old data to be visible again. Thus, if Yaffs creates a hole it first writes a shrink header to indicate the start of the hole and a regular object header to mark the end of the hole.</p>\r\n\r\n<p class=\"western\">The shrink header modifies the behavior of the garbage collector to ensure that these shrink headers are not erased until it is safe to do so. This unfortunately has a negative impact on garbage collection in systems which make extensive use of files with holes.</p>\r\n\r\n<p class=\"western\">Shrink headers are also used to indicate that a file has been deleted and that the record of the file deletion is not lost.</p>\r\n\r\n<h2 class=\"western\">10.3 Yaffs2 extended tags</h2>\r\n\r\n<p class=\"western\">Yaffs2 extends the tags in object headers with extra fields to improve mount scanning performance. The operation of extended tags is described later in this document.</p>\r\n\r\n<h1 class=\"western\">11 Bad block handling NAND error handling</h1>\r\n\r\n<p class=\"western\">NAND flash is designed to be very low cost and very high density. In order to get high yields, and thus keep costs down, NAND flash is typically shipped with a few bad blocks and some further blocks are expected to go bad during the use of the device. Thus, any flash file system without an effective bad block handling policy is unsuitable for NAND flash.</p>\r\n\r\n<p class=\"western\">Yaffs1 uses the Smart-Media style of bad block marking. This checks the sixth byte (byte 5) of the spare area. In a good block this should be 0xFF. A factory-marked bad block should be 0x00. If Yaffs1 determines that a block has gone bad it is marked bad with 0x59 (\'Y\'). Using a distinctive marker allows Yaffs-marked bad blocks to be differentiated from factory-marked bad blocks.</p>\r\n\r\n<p class=\"western\">Yaffs2 mode is designed to support a wider range of devices and spare-area layouts. Thus, Yaffs2 does not decide which bytes to mark but instead calls driver functions to determine whether the block is bad, or to mark it bad.</p>\r\n\r\n<p class=\"western\">So, when does Yaffs mark a block bad? Yaffs will mark a block bad if a read or write operation fails or if three ECC errors are detected. A block that is marked bad is retired from use, thus improving the robustness of the file system. This policy is suitable for SLC flash. MLC policies are under development.</p>\r\n\r\n<p class=\"western\">Further, NAND flash cells can sometimes be disturbed (corrupted) by NAND activity and charge loss. These errors are corrected by error correction codes (ECC) which may be implemented in hardware, software drivers, or within Yaffs itself. Again, any flash file system lacking effective ECC handling is unsuitable for NAND flash.</p>\r\n\r\n<p class=\"western\">Yaffs1 mode can use either the built in ECC or use ECC provided by the driver or hardware.</p>\r\n\r\n<p class=\"western\">Since Yaffs2 mode is designed for a wider range of devices, it does not provide ECC internally but requires that the driver provide the ECC.</p>\r\n\r\n<p class=\"western\">The ECC code supplied with Yaffs (yaffs_ecc.c) is the fastest C code implementation of a Smart Media compatible ECC algorithm that we are aware of. This code will correct any single bit error within a 256-byte data block and detect 2 errors per 256-byte data block. This is sufficient error correction to provide very high reliability on most SLC-type NAND flash.</p>\r\n\r\n<p class=\"western\">For a more complete description of failure handling, please refer to the Yaffs error mitigation document.</p>\r\n\r\n<h1 class=\"western\">12 RAM structures</h1>\r\n\r\n<p class=\"western\">While it is theoretically possible to use a log structured file system with very few RAM structures, this would provide reduce performance. Thus, significant RAM data structures are required to store enough information to provide sufficiently high performance.</p>\r\n\r\n<p class=\"western\">Before we jump in, it is a good idea to first figure out what purpose the RAM structures serve. The main structures are defined in yaffs_guts.h and their main purposes, are :</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Device/partition: This is named <i><b>yaffs_dev</b></i><span style=\"font-style: normal\"><span style=\"font-weight: normal\">. </span></span>This is required to hold information relating to a Yaffs “partition” or “mount point”. Using these, rather than globals, allows Yaffs to simultaneously support multiple partitions and partitions of different types. In fact, almost other data structures mentioned here are part of, or accessed via, the structure.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">NAND Block Information: This is named <i><b>yaffs_block_info </b></i>and holds the current state of the NAND blocks. Each yaffs_dev has an array of these.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">NAND Chunk Information: This is a bitfield attached to the yaffs_dev that holds the current in-use state of each chunk in the system. There is one bit per chunk in the partition.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object: This is named <i><b>yaffs_obj</b></i> and holds the state of an object (see section 3). There is one of these for each object in the file system, where an object is one of a regular file, directory, hard link, symbolic link or special link. There are different variants of yaffs_Objects to reflect the different data required for these different object types. Each object is uniquely identified by an obj_id.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">File structure: For each file object, Yaffs holds a tree which provides the mechanism to find the data chunks in a file. The tree consists of nodes called <i><b>yaffs_tnode</b></i> (tree nodes).</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Directory structure: The directory structure allows objects to be found by name. The directory structure is built from a doubly-linked list that is rooted in the directory and binds together the sibling objects within a directory.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object number hash table: The object number hash table provides a mechanism to find an object from its obj_id. The obj_id is hashed to select a hash bucket. Each hash bucket has a doubly-linked list of objects that belong in this hash bucket, as well as a count of the objects in the bucket.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Cache: Yaffs provides a read/write cache that significantly improves performance for short operations. The size of the cache can be set at run-time.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The usage of these structures is described in more detail below.</p>\r\n\r\n<p class=\"western\">Yaffs makes significant use of doubly-linked lists. These data structures are very useful because they provide rapid insertion, removal and traversal. They do however come at the expense of requiring two pointers rather than just one that would be required by a singly-linked list.</p>\r\n\r\n<h2 class=\"western\">12.1 Yaffs objects (struct yaffs_obj)</h2>\r\n\r\n<p class=\"western\">Each object (file, directory,...) in the system is represented by a yaffs_obj structure</p>\r\n\r\n<p class=\"western\">The yaffs_obj\'s main function is to store most of the object metadata and type-specific information. This means that almost all the information about an object can be accessed immediately without reading flash.</p>\r\n\r\n<p class=\"western\">The metadata includes, amongst others:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">obj_id: The number used to identify the object.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">parent: A pointer to the parent directory. Not all objects have a parent. The root director and special directories for unlinked and deleted files do not have parents so this is NULL.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">short_name: If the name is short enough to fit in a small fixed-sized array (default 16 characters) then it is stored here otherwise it must be fetched from flash each time it is requested.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">type: Type of object.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Permissions, time, ownership and other attributes</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\" style=\"font-size: 12pt\">Depending on the object type, yaffs_obj also stores:</font></p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A tnode tree, file extents etc for data files</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A directory chain for directories</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">An equivalent object for hard links</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A string for symbolic links</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">12.2 Look-up by obj_id</h2>\r\n\r\n<p class=\"western\">The purpose of this mechanism is to rapidly access an object by its device and obj_id. This is required during garbage collection, scanning and similar operations.</p>\r\n\r\n<p class=\"western\">Each yaffs_dev uses a hash table for this purpose. The hash table has 256 “buckets” (tunable), each holding a doubly-linked list of the yaffs_objs in that bucket.</p>\r\n\r\n<p class=\"western\">The hash table\'s hash function is just masking the least significant bits of the obj_id. This is a very cheap function that provides reasonable “spread”.</p>\r\n\r\n<p class=\"western\">Yaffs makes an attempt to assign obj_ids in a way that keeps the number of objects in each hash bucket quite low, thus preventing any one hash entry getting too long.</p>\r\n\r\n<p class=\"western\">This mechanism is also charged with assigning new (unique) obj_id values.</p>\r\n\r\n<h2 class=\"western\">12.3 Directory structure</h2>\r\n\r\n<p class=\"western\">The purpose of the directory structure is to rapidly access an object by name. This is required to perform file operations such as opening, renaming etc.</p>\r\n\r\n<p class=\"western\"><font size=\"3\" style=\"font-size: 12pt\">The Yaffs directory structure is formed by a tree of objects of type YAFFS_OBJECT_TYPE_DIRECTORY. These objects have a doubly linked node of children.</font></p>\r\n\r\n<p class=\"western\">Each yaffs_obj also has a doubly linked list node called sibling which chain together the siblings in a directory.</p>\r\n\r\n<p class=\"western\">Each Yaffs partition has a root directory.</p>\r\n\r\n<p class=\"western\">Each yaffs partition also has some special purpose “fake” directories which are not stored in NAND but are instead created on mounting:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">lost+found: This directory is used as a place to store any lost file parts that cannot be placed in the normal directory tree.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Unlinked and deleted: These are pseudo-directories that are not in the directory tree. Placing objects in these directories gives them a status of being unlinked or deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p align=\"left\" class=\"western\">Each object has a name that is used to look up the object within the directory, Thus, each name within a directory must be unique. This rule does not apply to objects in the unlinked and deleted pseudo-directories since we never use names to look up unlinked/deleted files and there may be many files of the same name that have been deleted.</p>\r\n\r\n<p class=\"western\">Name resolution is sped up by two mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">short_names are stored directly in the yaffs_obj so that they don\'t have to be loaded from flash.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Each object has a “name sum”. This is a simple hashing of the name which speeds up the matching process because it is cheaper to compare the hash than compare the whole name string.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><img align=\"left\" height=\"355\" name=\"Object2\" src=\"data:image/png;base64,[encoded data, 22776 characters]\" width=\"412\" /><br clear=\"left\" />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The above directory structure represents the following directory tree</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"119\" />\r\n		<col width=\"468\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/</font></font></p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Root directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/a</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Directory containing c,d,e</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/b</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Empty directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/lost+found</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Empty directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/a/c</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/a/d</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Data file</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"119\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier, monospace\"><font size=\"3\" style=\"font-size: 12pt\">/a/e</font></font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"468\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Data file</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\">&nbsp;</p>\r\n\r\n<p class=\"western\">If a Yaffs partition has been somehow corrupted then it is possible for an object to be created during scanning but with no object header (which contains the object name) being found. The object would then not have a name. In these cases, Yaffs creates a pseudonym from the object number of the form objnnnn so that the object can be placed in the directory structure.</p>\r\n\r\n<h2 class=\"western\"><br />\r\n&nbsp;</h2>\r\n\r\n<h2 class=\"western\" style=\"page-break-before: always\">12.4 Hard links</h2>\r\n\r\n<p class=\"western\">In POSIX file systems, the objects (inodes in Linux terminology, vnode in BSD-speak) and the object names (dentry in Linux terminology) are independent in that an object can have zero, one or more names.</p>\r\n\r\n<p class=\"western\">Having files with zero links is caused by constructs such as:</p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">h = open(“foo”,O_CREAT | O_RDWR, S_IREAD | S_IWRITE); /* create file “foo” */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">unlink(“foo”); /* unlink it </font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">to remove</font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\"> directory </font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">entry </font></code><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">*/</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">/* we still have a handle to the file so can still access it via the handle */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">read(h,buffer,100); </font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">write(h,buffer,100);</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\"><font face=\"Courier 10 Pitch\"><font size=\"1\" style=\"font-size: 8pt\"><code class=\"western\"><font size=\"2\" style=\"font-size: 10pt\">close(h); /* as soon as the handle is closed the file is deleted */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-left: 1.25cm; margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\">&nbsp;</p>\r\n\r\n<p class=\"western\">Yaffs does not store separate object and name records because that would waste space and incur extra reading/writing. Instead, Yaffs is optimised for the most typical case of one name per object. That is, Yaffs stores one name per yaffs_obj and uses “cheats” to achieve other behavior:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Objects that have zero names (ie unlinked objects) are handled by renaming the object into a fake directory that is used to designate unlinked objects.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Multiple names object are handled by designating one of the objects the main object and having multiple hard link pseudo-objects that refer to it.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The inode number (vnid in BSD-speak) is a per-file system unique number that identifies the object. The Yaffs obj_id of the main object is presented to the user. That is, if the inode number is queried from a hardlink object then the obj_id of the equivalent object is returned.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The linkage between hard link pseudo-objects and their equivalent object is through two fields:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Each object has a hard_links doubly linked list node that links the list of hard links to that object.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Each hard link has a equiv_obj pointer to the “equivalent object”.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\" style=\"page-break-before: always\">The usage of this structure is straight forward except for one interesting case when hard links are being deleted. Consider the following sequence of events:</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"50\" />\r\n		<col width=\"80\" />\r\n		<col width=\"449\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"50\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">1</font></p>\r\n			</td>\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"80\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\"># touch a</font></p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"449\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Create file a</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"50\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">2</font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"80\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\"># ln a b</font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"449\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Create a hard link b with equivalent object a.</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"50\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">3</font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"80\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\"># rm a</font></p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"449\">\r\n			<p align=\"left\" class=\"western\" style=\"orphans: 0; widows: 0; page-break-before: auto\"><font size=\"3\" style=\"font-size: 12pt\">Remove a. The object must still exist under the name b</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p align=\"left\" class=\"western\" style=\"margin-bottom: 0cm; orphans: 0; widows: 0; page-break-before: auto\">&nbsp;</p>\r\n\r\n<p class=\"western\">In step 3, Yaffs cannot just delete the equivalent object since that would leave a “hanging” hard link with no equivalent object.</p>\r\n\r\n<p class=\"western\">We also cannot just change the type of one of the hardlink objects because that would cause the object\'s obj_id to change. This would be unacceptable since it would break any mechanism that use the obj_id (of which there are many within and outside of Yaffs).</p>\r\n\r\n<p class=\"western\">Thus, if an object is deleted and it has hard links to it then we do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Select one of the object\'s hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Rename the object to the name in the hard link.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Delete the hard link pseudo-object.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">12.5 Symbolic links and special objects</h2>\r\n\r\n<p class=\"western\">These object types just store values that are needed to provide a full POSIX file system.</p>\r\n\r\n<p class=\"western\">A symbolic link object saves a symbolic link string that is used to provide the POSIX symbolic string mechanism.</p>\r\n\r\n<p class=\"western\">A special object is used to store entries for device numbers, pipes and other similar special objects.</p>\r\n\r\n<h2 class=\"western\">12.6 File objects</h2>\r\n\r\n<p class=\"western\">The yaffs_Objects have a type and a variant portion which is a union of different data required to hold the state of different object types.</p>\r\n\r\n<p class=\"western\">The file objects have type YAFFS_OBJECT_TYPE_FILE, and an associated file variant. This stores the following main values:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">file_size: file size</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">top_level: depth of the Tnode tree</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">top: pointer to top of Tnode tree</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The meaning of the file size is quite obvious. This is the current length of the file. Note that a file might have holes in it caused by seeking forward past the extents of the file.</p>\r\n\r\n<p class=\"western\">The top and top_level work together to control the Tnode trees.</p>\r\n\r\n<h3 class=\"western\" style=\"page-break-before: always\">12.6.1 Tnode tree</h3>\r\n\r\n<p class=\"western\">Each file has a tnode tree to provide the mapping from file position to actual NAND chunk address. top is a pointer to the top of the tnode tree</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\"><img align=\"left\" height=\"378\" name=\"Object1\" src=\"data:image/png;base64,[encoded data, 20236 characters]\" width=\"385\" /><br clear=\"left\" />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The tnode tree is made up of tnodes (tree nodes) arranged in levels, each of which holds either:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">At levels greater than 0, a tnode has 8 pointers to other tnodes in the next level down.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">At level 0, a tnode has 16 NAND chunk Ids which identify the chunk\'s location in RAM.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">These numbers are powers of 2 which makes for simple look-ups by just applying bitmasks. The code that traverses this tree is in yaffs_find_tnode_0().</p>\r\n\r\n<p class=\"western\">As the file size grows, more tnodes are added and as the levels fill, more levels are added. If the file is truncated or resized downwards, then the tree is pruned. If a chunk in a file is replaced (eg. data is overwritten or copied by garbage collection) then the tnode tree must be updated to reflect the change.</p>\r\n\r\n<p class=\"western\">Tnodes typically make up the bulk of the Yaffs\' RAM usage.</p>\r\n\r\n<h1 class=\"western\">13 How various mechanisms work</h1>\r\n\r\n<p class=\"western\">Having looked at the Yaffs data structures, we now consider how some of the mechanisms in Yaffs operates. This is most easily done by walking through some operations that Yaffs performs.</p>\r\n\r\n<h2 class=\"western\">13.1 Block and chunk management</h2>\r\n\r\n<h3 class=\"western\">13.1.1 Block states</h3>\r\n\r\n<p class=\"western\">Yaffs tracks the state of each block and chunk in the partition. This information is first built up during scanning (or recovered from a checkpoint). A block may be in one of the following states (see definition of enum yaffs_block_state):</p>\r\n\r\n<p class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"146\" />\r\n		<col width=\"441\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0.1cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">UNKNOWN</p>\r\n			</td>\r\n			<td style=\"border: 1px solid #000000; padding: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">The block state is not yet known.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">NEEDS_SCANNING</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">During pre-scanning it has been determined that this block has something on it and needs scanning.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">SCANNING</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This block is currently being scanned.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">EMPTY</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This block has nothing in it (is erased) and may be used for allocation. The block can get to this state by being erased.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">ALLOCATING</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This is the block currently being used for allocation by the chunk allocator.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">FULL</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">All the chunks in this block have been allocated and at least one chunk contains useful information that has not yet been deleted.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">DIRTY</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">All the chunks in this block have been allocated and all have been deleted. The block might have been in the FULL or COLLECTING state before this. This block can now be erased and returned to EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">CHECKPOINT</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This block has checkpoint data in it. When the checkpoint is invalidated this block will be erased and returned to the EMPTY state.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">COLLECTING</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This block is being garbage collected. As soon as all live data has been inspected this block becomes DIRTY.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: none; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0cm\" width=\"146\">\r\n			<p class=\"western\">DEAD</p>\r\n			</td>\r\n			<td style=\"border-top: none; border-bottom: 1px solid #000000; border-left: 1px solid #000000; border-right: 1px solid #000000; padding-top: 0cm; padding-bottom: 0.1cm; padding-left: 0.1cm; padding-right: 0.1cm\" width=\"441\">\r\n			<p class=\"western\">This block has been retired or was marked bad. This is a terminal state.</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><img align=\"left\" height=\"510\" name=\"Object3\" src=\"data:image/png;base64,[encoded data, 42088 characters]\" width=\"575\" /><br clear=\"left\" />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The regular runtime states are; EMPTY, ALLOCATING, FULL, COLLECTING and DIRTY.</p>\r\n\r\n<p class=\"western\">The checkpoint state is used for checkpoint blocks.</p>\r\n\r\n<p class=\"western\">The DEAD state is the terminal state for blocks that were retired because they were either marked bad or an error was detected.</p>\r\n\r\n<h3 class=\"western\">13.1.2 Block and chunk allocation</h3>\r\n\r\n<p class=\"western\">An available chunk must be allocated before it can be written. The chunk allocation mechanism is provided by yaffs_alloc_chunk() and is very simple. Each yaffs_dev has a current block that it is allocating from. This is termed the <i><b>allocation block </b></i><i><b>(alloc_block)</b></i>. Chunks are allocated sequentially from the allocation block. As the chunks are allocated, block and chunk management information is updated, including the block\'s pages_in_use count and the chunk use bitmap.</p>\r\n\r\n<p class=\"western\">When the block is fully allocated, another empty block is selected to become the allocation block. The blocks are selected by searching upwards from the previous allocation block.</p>\r\n\r\n<p class=\"western\">In older versions of Yaffs, the Yaffs scanning would identify the allocation block being used when the file system was last shut down and would continue allocation from that point. This had the benefit of continuing to use the same block and slightly reduce garbage creation. However, doing this could potentially cause problems due to attempting to write a block that had been in the middle of a write when power was lost. Thus Yaffs now always starts allocation on a new block after a mount. This slightly increases the amount of garbage (typically by an insignificant amount) but improves robustness.</p>\r\n\r\n<h3 class=\"western\">13.1.3 A word about wear leveling</h3>\r\n\r\n<p class=\"western\">Since flash memory blocks can only endure a limited number of writes and erases ( termed the flash endurance), a flash file system needs to ensure that the same few blocks are not overwritten continuously causing those blocks to wear out while the other blocks are hardly used at all.. Instead it is important that the file system spreads out the wear.</p>\r\n\r\n<p class=\"western\">This is extremely important in a file system such as FAT which uses a limited set of logical blocks to store the file allocation table entries which get modified frequently. If FAT is used with a static logical to physical block mapping then the file allocation table blocks will wear out far before other blocks causing premature device failure. Thus, for FAT, it is very important that the logical to physical mapping gets stirred up a bit to cause the file allocation tables to be written to different physical blocks, thus leveling out the wear.</p>\r\n\r\n<p class=\"western\">Wear leveling is far less of a concern in a log structured file system such as Yaffs since we always write to the end of the log and thus don\'t keep on overwriting the same blocks.</p>\r\n\r\n<p class=\"western\">There are basically two ways to achieve wear leveling:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Use a specifically written set of functions that actively perform wear leveling.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Allow a degree of wear leveling to happen as a side effect of other activity.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Yaffs takes the second approach.</p>\r\n\r\n<p class=\"western\">Firstly, being log structured, Yaffs inherently spreads out the wear by performing all writes in sequence. That means that we don\'t single out a few blocks for a specific purpose and write them more than others.</p>\r\n\r\n<p class=\"western\">Secondly, blocks are allocated serially from the erased blocks in the partition. Thus the process of erasing (after deletion of garbage collection) and then allocating blocks does tend to spread the block use and provides a level of wear leveling.</p>\r\n\r\n<p class=\"western\">Thus, even though there is no code to specifically perform wear leveling it does happen as a side effect of other activity.</p>\r\n\r\n<p class=\"western\">This strategy performs well and has survived many accelerated lifetime tests in both simulations and on real NAND-based products.</p>\r\n\r\n<h3 class=\"western\">13.1.4 Management of yaffs_tnode and yaffs_obj structures</h3>\r\n\r\n<p class=\"western\">yaffs_tnodes and yaffs_objs are dynamically created and released as they are needed.</p>\r\n\r\n<p class=\"western\">The allocation and management could be done by just using the standard memory allocator (malloc, kmalloc etc), but this could cause performance problems, particularly in embedded systems which tend to have rather simplistic memory allocators.</p>\r\n\r\n<p class=\"western\">The allocation and deletion of many small structures can cause memory fragmentation causing out-of-memory and slowdown problems.</p>\r\n\r\n<p class=\"western\">To prevent these problems, Yaffs manages these objects internally. Yaffs creates many structures (100 or so) at a time and then allocates them singly from a free list. When structures are released they are placed on the free list for reuse.</p>\r\n\r\n<h2 class=\"western\">13.2 Internal cache</h2>\r\n\r\n<p class=\"western\">This describes the standard Yaffs internal cache implementation. Alternative caches have been devised for other purposes and at some future point Yaffs might be extended to support pluggable cache policies.</p>\r\n\r\n<p class=\"western\">The main purpose of the internal cache is to reduce NAND access for badly behaved applications which perform many small reads and writes. Believe it or not, but there are applications out there that read or write files a few bytes at a time:</p>\r\n\r\n<p class=\"code-western\"><code class=\"western\">while(...)<br />\r\nwrite(h,buf,1);</code></p>\r\n\r\n<p class=\"western\" style=\"margin-left: 1.25cm\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">This sort of behavior would be terrible for performance and flash memory life if each read or write operation resulted in a flash access. Yaffs provides an internal cache to mitigate against this sort of behaviour.</p>\r\n\r\n<p class=\"western\">The Yaffs internal cache is called a “Short Op Cache” (short operation cache) and only comes in to play for operations which are not chunk aligned. The caching mechanism is very simple and was primarily written for less sophisticated OSs that lack their own file caching layer. The cache is still beneficial in OSs like Linux, which provide a caching layer, because it caches both the read and write path. Thus, while short reads will be serviced by the Linux page cache, the short op cache does address non-chunk aligned writes.</p>\r\n\r\n<p class=\"western\">Page aligned reads/writes bypass the cache unless that chunk is already in the cache, except when inband tags are used. Inband tags always require the cache because the cache is used to build up the buffer where the tags are attached to the data chunk being written.</p>\r\n\r\n<p class=\"western\">The cache is stored in an array of cache entries. The number of cache entries is set in the yaffs_dev parameters n_caches and the cache is configured during initialisation. n_caches of zero disables the cache. The cache management algorithms are simplistic and probably don\'t scale well to very large numbers. This means that it is probably best to stick with around 5 to 20 cache entries.</p>\r\n\r\n<p class=\"western\">Each cache entry holds:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">obj_id, chunk id, nbytes (the tag info of the chunk being cached)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The actual data bytes</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Cache slot status (last used counter, dirty,...)</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Finding an unused cache entry is a simple operation. We just iterate through the cache entries searching for one that is not assigned. If all cache entries are busy then a cache push-out is performed to free up a cahce entry.</p>\r\n\r\n<p class=\"western\">Whenever a cache entry is accessed (read or written) the last-use counter is is set to an incrementing number. This counter value is used to drive the least-recently-used (LRU) algorithm.</p>\r\n\r\n<p class=\"western\">Cache entries which have been modified and not yet written back to flash are marked with a dirty flag. This is tells us whether we need to write out the data during a flush or push-out.</p>\r\n\r\n<p class=\"western\">A push-out happens whenever the cache is full and we need to push an entry out of the cache to make space to load a new entry. Yaffs uses the LRU algorithm to select the LRU cache entry, write it out if needed and then assign that for use.</p>\r\n\r\n<p class=\"western\">Whenever a file is flushed (file flush or close), the file\'s dirty entries are written out. At certain times (eg. before an unmount or at sync()) the entire cache is flushed.</p>\r\n\r\n<p class=\"western\">This caching policy is gives a useful performance improvement with very simple algorithms (ie good bang-for-bucks).</p>\r\n\r\n<p class=\"western\">Apart from its normal caching function, the short op cache also does much of the heavy lifting for inband tags support since all the inband tags accesses are not chunk aligned and must typically be re-aligned. The cache makes this easy.</p>\r\n\r\n<h2 class=\"western\">13.3 Scanning</h2>\r\n\r\n<p class=\"western\">Scanning is the process whereby file system state is rebuilt from scratch. This happens on mounting a Yaffs partition if no checkpoint data is available or if the mounting was told to ignore the checkpoint data.</p>\r\n\r\n<p class=\"western\">Scanning involves reading the tags from all the active chunks in the system so can take quite a while, but probably less time than you\'d expect.</p>\r\n\r\n<p class=\"western\">The scanning process is different for Yaffs1 vs Yaffs2 because of the differences mentioned before.</p>\r\n\r\n<h3 class=\"western\">13.3.1 Yaffs1 scanning</h3>\r\n\r\n<p class=\"western\">Yaffs1 scanning is rather straight forward because Yaffs1 uses deletion markers.</p>\r\n\r\n<p class=\"western\">All we really need to do is read the tags off all the chunks in the partition and determine if the chunk is active. Order is not important and blocks could be read in any order. If the deletion marker has not been set then it is active and we need to add the chunk to the file system state as follows:</p>\r\n\r\n<p class=\"western\">If the chunk is a data chunk (chunk_id &gt; 0) then the chunk must be added to the relevant file\'s tnode tree. If the object does not exist, then we create it in lost+found, although it has no name and other file details yet.</p>\r\n\r\n<p class=\"western\">If the chunk is an object header (chunk_id == 0) then the object must be created (or it might already exist in lost + found and we need to fix up its details and slot it into the correct part of the directory tree.</p>\r\n\r\n<p class=\"western\">If at any time during scanning we find that we are reading a second chunk with the same obj_id:chunk_id as a previously encountered chunk then the 2-bit serial number is used to resolve the dispute.</p>\r\n\r\n<p class=\"western\">Once the scanning is complete we still need to fix up a few things:</p>\r\n\r\n<p class=\"western\">Hard links must be connected up properly (it is more efficient to defer this until after the scanning has completed).</p>\r\n\r\n<p class=\"western\">Also, objects which were marked as unlinked must be deleted since there are no longer handles open to these objects.</p>\r\n\r\n<h3 class=\"western\">13.3.2 Yaffs2 scanning</h3>\r\n\r\n<p class=\"western\">Yaffs2 has no deletion markers which makes the scanning significantly more complex.</p>\r\n\r\n<p class=\"western\">The first thing that needs to be done is to pre-scan the blocks to determine the sequence number for the blocks. The list of blocks and sequence numbers is then sorted to make a chronologically ordered list.</p>\r\n\r\n<p class=\"western\">Yaffs then scans <i><b>backwards</b></i> through these blocks (ie in reverse chronological order). Thus, the first encountered obj_id:chunk_id pair is the current one and subsequent chunks with those tags are out of date.</p>\r\n\r\n<p class=\"western\" style=\"margin-left: 1.25cm\">We read the tags for each chunk in each block.</p>\r\n\r\n<p class=\"western\" style=\"margin-left: 1.25cm\">If it is a data chunk ( ie. chunk_id &gt; 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">If a chunk with the same obj_id:chunk_id has already been found then this is not the current chunk. Delete this one.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else if the object is marked as deleted, or corrupted, then delete it.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else if the object does not yet exist then this chunk was written since the last object header write for this object so this chunk must be the last written chunk in the file and must have happened just before an unclean shutdown with the file still open. We can create the file and use the chunk_id and nbytes to set the file extents.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else if the object does exist and the chunk lies beyond the object\'s file scan size (extents) then it is a truncated chunk and can be deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else it is placed into the file\'s tnode tree.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\" style=\"margin-left: 1.25cm\">If it is an object header (ie. chunk_id == 0) then:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">If the object is in the deleted directory then mark the object as deleted and set the object\'s shrink size to zero.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else if an object header has not yet been found for this object, then this is the most current object header and holds the most current name etc. If no data chunks have yet been found for this chunk then the size in the object header is also the file extents. The file size is used to set the file scan size since at the time it was written the file did not extend past that point and we can safely ignore further data chunks for that file that extend past that point.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else just use the file size as the scan size if it is smaller than the current file scan size.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Else this an obsolete object header. It is deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">A bit more explanation is probably needed to describe the file scan size. The whole purpose of this is to determine data chunks that should be ignored due to the file being truncated.</p>\r\n\r\n<h2 class=\"western\">13.4 Checkpoint</h2>\r\n\r\n<p class=\"western\">Mount scanning takes quite a lot of time and slows mounting, though perhaps by less than you\'d think. Checkpointing is a mechanism to speed the mounting by taking a snapshot of the Yaffs runtime state at unmount or sync() and then reconstituting the runtime state on re-mounting. The speed up is dramatic.</p>\r\n\r\n<p class=\"western\">The actual checkpoint mechanism is quite simple. A “stream” of data is written to a set of blocks which are marked as holding checkpoint data and the important runtime state is written to the stream.</p>\r\n\r\n<p class=\"western\">Not all state needs to be saved, only the state needed to reconstitute the structure of the runtime data. For example, file metadata need not be saved since it is very easy to load that through lazy-loading.</p>\r\n\r\n<p class=\"western\">The checkpoint stores data in the following order:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Start marker, including checkpoint format id.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">yaffs_dev information</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Block information</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Chunk flags</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Objects, including file structure</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">End marker</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Checksum</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The checkpoint validity is ensured by the following mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">The data is stored using whatever ECC mechanisms are in place.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">The start marker includes a checkpoint version so that an obsolete checkpoint will not be read if the checkpoint code changes.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Data is written to the stream as structured records, each with a type and size.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">The end marker must be read when expected.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Two checksums are maintained across the whole checkpoint data set.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">If any checks fail, the checkpoint is ignored and the state is re-scanned.</p>\r\n\r\n<p class=\"western\">The astute reader might notice that block state changes as we\'re writing checkpoint blocks, so how do we manage to re-constitute the block states correctly? This is achieved by the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">As we write the checkpoint, the checkpoint block allocation selects EMPTY blocks. We don\'t change the state as we\'re writing the checkpoint so their state is written to the checkpoint as EMPTY or CHECKPOINT, it does not really matter which.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">After reading the checkpoint, we know which blocks have been used to store checkpoint data. We update those blocks to reflect the CHECKPOINT state.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">A checkpoint is invalidated by any modifications (writes, erases,...) thus any modification path also checks to see if there is a checkpoint and erases it.</p>\r\n\r\n<p class=\"western\">The regular Yaffs block allocator and garbage collection logic also needs to be aware of the approximate size of the checkpoint so that these can ensure enough spare space is reserved to store a checkpoint. There is a calculation that updates the value whenever the number of objects etc changes.</p>\r\n\r\n<h2 class=\"western\">13.5Extended tags and packed tags</h2>\r\n\r\n<p class=\"western\">Extended tags is an abstraction of a way to store more information in the tags, allowing for faster scan times. Packed Tags is a physical implementation of the storage of extended tags.</p>\r\n\r\n<p class=\"western\">When we\'re scanning we really only need to build up the structure of the file system. In other words, rebuild the directory relationships, tnode trees and such. Much of the detail such as file names, permissions etc can be lazy loaded if and when needed.</p>\r\n\r\n<p class=\"western\">This required storing extra information, such as the parent directory and object type, in the tags of object headers. But how do we do this without enlarging the tags size?</p>\r\n\r\n<p class=\"western\">Object headers always have a chunk_id of zero and n_bytes has no meaning because it is not a data chunk. By using a single bit to identify object headers we suddenly open up a whole lot of space that we can stuff full of useful data. By being clever as to what data we use, we can pack in the important data in most circumstances.</p>\r\n\r\n<p class=\"western\">Sometimes, for example very large files, the packing will not work because there are insufficient bits to hold the field so we just write regular tags instead. That\'s OK since there can only be few such files in the system so the performance reduction only impacts very few files and the system level impact is unnoticeable. A simple, cheap, strategy that gives speed up 99% of the time is a very handy one.</p>\r\n\r\n<p class=\"western\">Other details are lazy-loaded on demand. This will happen when the object is looked up (eg. by a file open or searching for the file in the directory).</p>\r\n\r\n<h2 class=\"western\">13.6 Block summaries</h2>\r\n\r\n<p class=\"western\">Scanning requires that the tags for each chunk are read so that they can be inspected. Reading the tags from each chunk can be time consuming. Thus, block summaries were added in 2011.</p>\r\n\r\n<p class=\"western\">Block summaries write all the tags data for chunks in the block into the last few (typically only one) chunk in a block. This allows all the tags for chunks in that block to be read in one hit. Depending on the system throughput (CPU, RAM and flash speeds), this can speed up mount time dramatically.</p>\r\n\r\n<p class=\"western\">Note that the block summary does not replace the tags attached to each chunk. Both are written. Thus, if the whole block is not written (and the block summary is thus not written), the chunk tags can still be read and used.</p>\r\n\r\n<p class=\"western\">If a block summary is not available for a block, then the old mechanism is still used to scan the tags on each chunk.</p>\r\n\r\n<h2 class=\"western\">13.7 Inband tags</h2>\r\n\r\n<p class=\"western\">Thus far we have considered chunks where there are sufficient unallocated bytes in the spare (or out of band) area to store the tags.</p>\r\n\r\n<p class=\"western\">But what happens if we don\'t have enough space in the spare area to store the tags? Inband tags to the rescue!</p>\r\n\r\n<p class=\"western\">With inband tags we store the tags in the flash data area alongside the data. This gives us far more flexibility in the types of storage that Yaffs can support.</p>\r\n\r\n<p class=\"western\">There are three major penalties with inband tags:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Due to the loss of chunk alignment, all transactions must pass through the short op cache. This costs an extra memcpy().</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">&nbsp;</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Reading tags during scanning etc can involve reading the whole page, making this slower. The impact is minimised by using checkpointing or block summaries.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Thus, it is better, performance wise, to only use inband tags when regular tags will not work.</p>\r\n\r\n<h2 class=\"western\">13.8 Soft deletion</h2>\r\n\r\n<p class=\"western\">Soft deletion is a mechanism that was added to speed up file deletion in Yaffs1 mode.</p>\r\n\r\n<p class=\"western\">Looking back to the description of how files are stored in Yaffs1, you can see that deleting a file involves writing the deletion marker of every data chunk in the file. This can take quite some time for large files.</p>\r\n\r\n<p class=\"western\">The soft deletion mechanism was added to avoid writing all those deletion markers. Instead of writing the deletion markers, soft deletion instead tracks the number of chunks in each block that belong to deleted files and leaves the clean up to the garbage collector.</p>\r\n\r\n<p class=\"western\">When the garbage collector looks at a block it can see what current chunks (ie chunks not yet deleted) belong to deleted files and does not copy those, thus effectively deleting the chunk. Once all the chunks associated with a deleted file have cleaned up by the garbage collector the file header object can also be deleted.</p>\r\n\r\n<p class=\"western\">Yaffs2 mode does not write deletion markers and thus does not use soft deletion.</p>\r\n','This document gives a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical. This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.','full_html'),('paper',0,409,594,'und',0,'<div dir=\"ltr\" id=\"Frame2\">\r\n<p align=\"center\" class=\"frame-contents-western\"><font face=\"Liberation Sans, sans-serif\"><font size=\"4\">Charles Manning</font></font></p>\r\n\r\n<p align=\"center\" class=\"western\"><sdfield sdnum=\"2057;1033;YYYY-MM-DD\" type=\"DATETIME\">2017-06-06</sdfield></p>\r\n</div>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<p align=\"justify\" class=\"western\">This document aims to give a reasonable explanation of most of the core mechanisms that make Yaffs work.</p>\r\n\r\n<p align=\"justify\" class=\"western\">This document should serve as a first port of call for understanding Yaffs.</p>\r\n\r\n<p>&nbsp;</p>\r\n\r\n<h1 class=\"western\">1 Purpose</h1>\r\n\r\n<p class=\"western\">The purpose of this document is to give a reasonable explanation of most of the core mechanisms that make Yaffs work. As of the time of writing this, the Yaffs2 code base includes approximately 19,000 lines of code making a very detailed discussion impractical.</p>\r\n\r\n<p class=\"western\">This document should serve as a first port of call for understanding Yaffs. Further detailed questions and discussions are welcomed on the Yaffs discussion list.</p>\r\n\r\n<p class=\"western\">This document is updated periodically. Yaffs mechanism design changes from time to time and this document will not always reflect the most up to date code.</p>\r\n\r\n<h1 class=\"western\">2 What is Yaffs?</h1>\r\n\r\n<p class=\"western\">Yaffs stands for <i>Yet Another Flash File System</i>, a term coined by Charles Manning in 2001 when suggesting that the already cluttered flash file system space could do with yet another offering – this time a flash file system designed from the ground up to work with NAND flash.</p>\r\n\r\n<p class=\"western\">The first release of Yaffs was developed during the end of 2001 and the beginning of 2002. By mid-2002, Yaffs was being trialled in various products. In May 2002, Yaffs was announced and a few more projects started playing with it. In June, porting to other OSs started. In September 2002, Yaffs was announced on linuxdevices.com which started a far wider uptake of Yaffs.</p>\r\n\r\n<p class=\"western\">Yaffs1, the original version of Yaffs, supported 512-byte page NAND devices with a SmartMedia-like memory layout.</p>\r\n\r\n<p class=\"western\">Yaffs2 is a follow-up to Yaffs1 extending Yaffs1 to support larger and different devices with different constraints.</p>\r\n\r\n<p class=\"western\">Yaffsis highly portable and has been used in many different products and applications as varied as sewing machines, point of sale, phones and aerospace. Yaffs has been used with multiple different operating systems. Native support is available for Linux, WindowsCE and eCOS while Yaffs Direct Interface provides a layer that can be used in other applications such as RTOSs. Yaffs has been used with multiple different CPUs and compilers.</p>\r\n\r\n<p class=\"western\">Yaffs has also been used as a NOR file system and even as a RAM file system.</p>\r\n\r\n<h1 class=\"western\">3 Design and coding strategies</h1>\r\n\r\n<p class=\"western\">Yaffs is designed to work in multiple environments which drives the need for portability. Portability also improves the debugging and testing since it is easier to develop and debug code in an application environment than within an OS kernel. This generally allows Yaffs to progress faster with less programming resources and provides more varied testing. Primary strategies that improve portability include:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">No OS-specific features used in main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">No compiler-specific features used in the main code body.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Abstract types and functions used to allow Unicode or ASCII operation.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Simplicity is another key goal. Complexity is limited to those situations that provide significant improvements to performance or utility. Simplicity improves robustness and the ease of integration and development. Primary strategies that improve simplicity are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Single threaded model. Yaffs is locked on a per-partition basis at a high level. This is simpler than tracking lower-level locking. Yaffs Direct Interface uses a single lock for all partitions.</p>\r\n	</li>\r\n</ul>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Log structure makes for a simpler garbage collector and allocation methodology.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Abstractions that build layered code which is easier to understand, port and debug.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h1 class=\"western\">4 Terminology: Yaffs vs Yaffs1 vs Yaffs2</h1>\r\n\r\n<p class=\"western\">Before we embark on our quest to understand what Yaffs is, a terminology explanation is in order.</p>\r\n\r\n<p class=\"western\">There are two versions of Yaffs code: the original Yaffs code-base and the current Yaffs2 code-base. The Yaffs2 code-base supports functionality provided by the Yaffs code-base as well as being extended with new functionality to provide extra modes of operation, most importantly those required to work with larger and more modern NAND parts. The Yaffs2 code-base supports the Yaffs1 mode of operation through a backward compatibility mode.</p>\r\n\r\n<p class=\"western\">Unless explicitly mentioned, this text refers to the Yaffs2 code-base.</p>\r\n\r\n<p class=\"western\">This text uses three terms Yaffs, Yaffs1 and Yaffs2 depending on various modes of operation.</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs1 is a simpler mode of operation that uses deletion markers to track state. </font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs2 is a more complex mode of operating that was developed to support larger modern flash types, in particular MLC NAND, that cannot used deletion markers.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Yaffs means operations common to both.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Yaffs1 originally only worked with 512-byte page devices but has been extended to support larger flash pages such as Intel M18 flash with 1k pages.</p>\r\n\r\n<p class=\"western\">Yaffs2 was originally designed for 1k or larger page sizes but can be used with smaller pages by using in-band tags.</p>\r\n\r\n<p class=\"western\">Since the Yaffs1 mode of operation is simpler to understand than the Yaffs2 mode of operation, Yaffs1 will be described first and readers interested in Yaffs2 operation should first fully acquaint themselves with the Yaffs1 mode of operation. It is suggested that this document be read sequentially rather than browsing it.</p>\r\n\r\n<h1 class=\"western\">5 Objects</h1>\r\n\r\n<p class=\"western\">In Yaffs-speak, an Object is anything that is stored in the file system. These are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Regular data files</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Directories</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Hard-links</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Symbolic links</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Special objects (pipes, devices etc).</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">All objects are identified by a unique integer object id (<i><b>obj_id</b></i><i><b>)</b></i>.</p>\r\n\r\n<p class=\"western\">In the standard POSIX model, as used by Linux, there are inodes and directory entries (dentries).</p>\r\n\r\n<p class=\"western\">An inode may be a regular file, directory, or special file.</p>\r\n\r\n<p class=\"western\">Directory entries provide the naming mechanism that is used to locate inodes.</p>\r\n\r\n<p class=\"western\">Under POSIX, each inode may have zero, one or many dentries:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">One dentry per inode is commonly understood.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Multiple dentries per inode allow the same inode to be accessed via multiple names. This is achieved through hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Zero dentries per inode is less often understood. This happens when an inode is unlinked while there is still a handle open to the inode. The unlinking deletes the dentry but the inode still exists. As soon as the handle is closed the inode is deleted too.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h1 class=\"western\">6 The Yaffs NAND model</h1>\r\n\r\n<p class=\"western\">The memory in NAND flash is arranged in <i><b>pages.</b></i> A page is the unit of allocation and programming. In Yaffs, the unit of allocation is the<i> </i><i><b>chunk</b></i>. Typically a chunk will be the same as a NAND page, but there is flexibility to use chunks which map to multiple pages (eg. A system may have two NAND chips in parallel requiring 2x2048 = 4096 byte chunks) . This distinction gives a lot of flexibility in how the system can be configured. In this text the term page and chunk are synonymous unless stated otherwise.</p>\r\n\r\n<p class=\"western\">Many, typically 32 to 128 but as many as a few hundred, chunks form a <i><b>block</b></i>. A block is the unit of erasure.</p>\r\n\r\n<p class=\"western\">NAND flash may be shipped with bad blocks and further blocks may go bad during the operation of the device. Thus, Yaffs is aware of bad blocks and needs to be able to detect and mark bad blocks.</p>\r\n\r\n<p class=\"western\">NAND flash also typically requires the use of some sort of error detection and correction code (ECC). Yaffs can either use existing ECC logic or provide its own.</p>\r\n\r\n<h1 class=\"western\">7 How Yaffs1 stores files</h1>\r\n\r\n<p class=\"western\">Yaffs1 has a modified log structure, while Yaffs2 (see below) has a true log structure. A true log structured file system only ever writes sequentially. Yaffs1 uses deletion markers, which breaks the sequential write rule. Yaffs2 does not use deletion markers and is thus a true log structured file system.</p>\r\n\r\n<p class=\"western\">Instead of writing data in locations specific to the files, the file system data is written in the form of a sequential log, The entries in the log are all one chunk in size and can hold one of two types of chunk:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Data chunk</b></i>: A chunk holding regular data file contents.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Object Header</b></i>: A descriptor for an object (directory, regular data file, hard link, soft link, special descriptor,...). This holds details such as the identifier for the parent directory, object name, etc.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Each chunk has <i><b>tags</b></i> associated with it. The tags comprise the following important fields (others being ignored for now):</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Object Id (</b></i><i><b>obj_id</b></i><i><b>)</b></i>: Identifies which object the chunk belongs to.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Chunk Id (</b></i><i><b>chunk_id</b></i><i><b>)</b></i>: Identifies where in the file this chunk belongs. A chunk_id of zero signifies that this chunk contains an objectHeader. chunk_id==1 signifies the first chunk in the file (ie. At file offset 0), chunk_id==2 is the next chunk and so on.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Deletion Marker (</b></i><i><b>is_deleted</b></i><i><b>)</b></i><i><b>:</b></i> (Yaffs1 only) Shows that this chunk is no longer in use.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Byte Count</b></i><b>: </b><b>(n_bytes)</b>: Number of bytes of data if this is a data chunk.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Serial Number </b></i><i><b>(ser</b></i><i><b>ia</b></i><i><b>l_number)</b></i>: (Yaffs1 only) Serial number used to differentiate chunks with the same obj_id and chunk_id.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Now let\'s bring that all together in an explanation. This is a simplified explanation and skips over issues such as Soft Deletion (mentioned later in the document). For explanation purposes, we shall consider usage of fictitious NAND type that has 4 chunks per block, starting with an empty (erased) flash.</p>\r\n\r\n<p class=\"western\"><br />\r\nFirst we\'ll create a file.</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"56\" />\r\n		<col width=\"67\" />\r\n		<col width=\"60\" />\r\n		<col width=\"64\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"67\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td height=\"26\" sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"67\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Object Header for this file (length 0)</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\nNext we write a few chunks of data to the file.</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"60\" />\r\n		<col width=\"64\" />\r\n		<col width=\"56\" />\r\n		<col width=\"290\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td width=\"290\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"290\">\r\n			<p class=\"western\">Object Header for t<font size=\"3\">his file (length 0)</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"290\">\r\n			<p class=\"western\">First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"290\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"56\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"290\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\">&nbsp;</p>\r\n\r\n<p class=\"western\">Next we close the file. This writers a new object header for the file. Notice how the previous object header is deleted.</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"60\" />\r\n		<col width=\"68\" />\r\n		<col width=\"60\" />\r\n		<col width=\"282\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">Obsoleted object header <font size=\"2\">(length 0)</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">First chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"68\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"282\">\r\n			<p class=\"western\">New object header <font size=\"2\">(length n)</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\nLet\'s now open the file for read/write, overwrite part of the first chunk in the file and close the file. The replaced data and object header chunks become deleted.</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"64\" />\r\n		<col width=\"64\" />\r\n		<col width=\"71\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">New first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">New object header</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\nNow let\'s resize the file to zero by opening the file with O_TRUNC and closing the file. This writes a new object header with length 0 and the contents are pruned making the data chunks deleted.</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"37\" />\r\n		<col width=\"48\" />\r\n		<col width=\"64\" />\r\n		<col width=\"64\" />\r\n		<col width=\"71\" />\r\n		<col width=\"271\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"37\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deletion</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete object header.</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete first chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Second chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"37\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Third chunk of data</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Delted</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"37\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"48\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"64\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"71\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"271\">\r\n			<p class=\"western\">New object header <font size=\"2\">(length 0).</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\">Note how all the pages in block 1 are now marked as deleted. This means that block 1 no longer contains useful information so we can now erase block 1 and reuse the space.</p>\r\n\r\n<p class=\"western\">We will now rename the file. To do this we write a new object header for the file</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"38\" />\r\n		<col width=\"42\" />\r\n		<col width=\"58\" />\r\n		<col width=\"60\" />\r\n		<col width=\"60\" />\r\n		<col width=\"297\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"38\">\r\n			<p class=\"western\">Block</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">Chunk</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">obj_id</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">chunk_id</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Comment</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"1\" width=\"38\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">&nbsp;</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Erased</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">1</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Deleted first chunk of file</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Obsoleted object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"2\" width=\"38\">\r\n			<p class=\"western\">2</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Deleted</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">Obsolete object header</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td sdnum=\"2057;\" sdval=\"3\" width=\"38\">\r\n			<p class=\"western\">3</p>\r\n			</td>\r\n			<td width=\"42\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"58\">\r\n			<p class=\"western\">500</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">0</p>\r\n			</td>\r\n			<td width=\"60\">\r\n			<p class=\"western\">Live</p>\r\n			</td>\r\n			<td width=\"297\">\r\n			<p class=\"western\">New object header showing new name</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\">Block 2 now contains only deleted chunks and can be erased and reused.</p>\r\n\r\n<p class=\"western\">Note how the tags tell us:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Which chunks belong to which object</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">The position of the chunks within a file,</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Which chunks are current</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\">With that information we can recreate the state of the files regardless of the placement of the chunks in NAND. This means that should the system fail (crash, power fail,...) at any point during the sequences above, we are still able to recover the file system state up to that point.</font></p>\r\n\r\n<ul>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\">Note that there are no file allocation tables or similar structures stored in the flash. This reduces the amount of writing and erasing (improving write performance) while also improving robustness. Corruption of file allocation tables and similar structures is a common failure mechanism for embedded file systems. The lack of such structures makes Yaffs more robust. Or, to put it another way, you can\'t corrupt what you don\'t store.</font></p>\r\n\r\n<h1 class=\"western\">8 Garbage collection</h1>\r\n\r\n<p class=\"western\">As we have shown so far, when a block is made up only of deleted chunks, that block can be erased and reused. However, consider what will happen if we have many blocks which each have a few current chunks in them. Clearly we cannot erase any of the current blocks or we will destroy data. What we need to do is copy the useful data chunks off a block, deleting the originals and allowing the block to be erased and reused. This process is referred to as <i><b>garbage collection</b></i>.</p>\r\n\r\n<p class=\"western\">The Yaffs garbage collection process works as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">Find a block worth collecting (according the the heuristics below). If none found, then exit.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Iterate through the chunks in the block. If the chunk is in use then create a new copy and delete the original. Fix up the RAM data structures to reflect the change.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p class=\"western\">Once all chunks in this block have been deleted, the block can be erased ready for reuse.</p>\r\n\r\n<p class=\"western\">Note that although step 2 deletes the original chunk when it gets copied there is not much point in actually programming the deletion marker (is_deleted flag) in flash since we\'re about to erase the block anyway. Thus Yaffs does not program the deletion marker in these cases. If power is lost part way through the operation we can still tell the difference between the original and the copy by looking at the serial number (not that it really matters anyway since the two have the same contents so either can be used).</p>\r\n\r\n<p class=\"western\">The heuristics to determine whether a block is worth collecting are as follows:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">If there are many erased blocks available, then Yaffs does not need to work hard, but will instead only attempt to perform garbage collection of blocks with very few chunks in use. This is termed <i><b>passive</b></i> garbage collection.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">If, however, there are very few erased blocks, Yaffs will work much harder to recover more space and will garbage collect blocks with more chunks in use. This is termed <i><b>aggressive</b></i> garbage collection.</p>\r\n	</li>\r\n</ol>\r\n\r\n<p class=\"western\">OK, the passive/aggressive names are a bit silly, but they were contrived around midnight... give me a break!</p>\r\n\r\n<p class=\"western\">If garbage collection is aggressive, we need erased space urgency and the whole block is collected in a single garbage collection cycle. If the collection is passive then only a few chunks are collected at a time thus spreading the effort over many garbage collection cycles. This is done to reduce garbage collection load and improve responsiveness.</p>\r\n\r\n<p class=\"western\">The rationale behind the above heuristics is to delay garbage collection when possible to reduce the amount of collection that needs to be performed, thus increasing average system performance. Yet there is a conflicting goal of trying to spread the garbage collection so that it does not all happen at the same causing fluctuations in file system throughput. These conflicting goals make garbage tuning quite challenging.</p>\r\n\r\n<p class=\"western\">All flash file systems need some sort of garbage collector to reclaim space. The garbage collector is often a determining factor in the performance of the flash file system. Many file systems have to do a significant amount of effort in a single collection sweep.</p>\r\n\r\n<p class=\"western\">Yaffs only deals with one block at a time which constrains the amount of effort in a garbage collection cycle, thus reducing the “stall time”. Under normal conditions that garbage collection is performed a few chunks at a time, further reducing “stall time”.</p>\r\n\r\n<h1 class=\"western\">9 Yaffs1 serial numbers</h1>\r\n\r\n<p class=\"western\">In Yaffs1, each chunk is marked with a 2-bit serial number that is incremented every time a chunk with the same tags value is replaced (eg. When a chunk is overwritten or when copied by garbage collection).</p>\r\n\r\n<p class=\"western\">Now let\'s suppose first that we did not have a serial number.</p>\r\n\r\n<p class=\"western\">Let\'s consider a rename where an object header chunk must be replaced with a new one with the new name. Yaffs could do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Delete the old chunk.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Write the new chunk. </font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">If the system fails after the delete, the system could end up with no chunks matching the relevant tags values. This could cause files to be lost (end up in the lost+found directory). Thus, we have to write the new chunk before deleting the old chunk.</p>\r\n\r\n<p class=\"western\">But now consider what might happen if we did this:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">W<font face=\"Liberation Serif, serif\">rite new chunk.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Delete old chunk.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">If the system failed after the write we now have two chunks that match the tags. How can we tell which one is current?</p>\r\n\r\n<p class=\"western\">The current chunk is determined by inspecting the serial number. Since the serial number is only incremented by one before the old chunk is deleted, a 2-bit serial number is sufficient. Valid pairs are:</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"86\">\r\n	<colgroup>\r\n		<col width=\"35\" />\r\n		<col width=\"33\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"35\">\r\n			<p class=\"western\">Old</p>\r\n			</td>\r\n			<td width=\"33\">\r\n			<p class=\"western\">New</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"35\">\r\n			<p class=\"western\">00</p>\r\n			</td>\r\n			<td width=\"33\">\r\n			<p class=\"western\">01</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"35\">\r\n			<p class=\"western\">01</p>\r\n			</td>\r\n			<td width=\"33\">\r\n			<p class=\"western\">10</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"35\">\r\n			<p class=\"western\">10</p>\r\n			</td>\r\n			<td width=\"33\">\r\n			<p class=\"western\">11</p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"35\">\r\n			<p class=\"western\">11</p>\r\n			</td>\r\n			<td width=\"33\">\r\n			<p class=\"western\">00</p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p class=\"western\"><br />\r\nThus, by inspecting the serial number attached to the old and new tags the current chunk can be determined and the old chunk can be deleted.</p>\r\n\r\n<h1 class=\"western\">10 Yaffs2 NAND model</h1>\r\n\r\n<p class=\"western\">Yaffs2 is an extension of Yaffs designed to fulfill a new set of objectives to work with newer NAND types including:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Zero overwrites. Yaffs1 only ever overwrites a single byte in the spare area of the chunks to set the deletion marker. More modern NAND devices are less tolerant of overwrites and Yaffs2 performs no overwrites at all.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Sequential chunk writing within a block. More modern NAND reliability specifications tend to assume sequential writing. Since Yaffs2 does not write deletion markers, the writing of chunks within a block is strictly sequential.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Some flash technologies – such as MLC – only work properly if pages are written sequentially. This is a fundamental requirement of how many MLC types perform the bit overlaying.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">Since Yaffs2 performs less writes, there is potential for Yaffs2 to have improved write performance.</p>\r\n\r\n<p class=\"western\">Yaffs2 cannot use a deletion marker because that would violate the zero overwrites mandate and alternate mechanisms are provided to determine which chunks are current and which are deleted. These mechanisms are:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><i><b>Sequence Number </b></i><i><b>(seq_number)</b></i>: As each block is allocated, the file system\'s sequence number is incremented and each chunk in the block is marked with that sequence number. The sequence number thus provides a way of organising the log in chronological order.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><i><b>Shrink Header Marker </b></i><i><b>(shrink_header)</b></i>: The shrink header marker is used to mark object headers that are written to shrink the size of a data file. This is explained more in the accompanying text.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p align=\"center\" class=\"western\"><i><b>Note: The Yaffs2 sequence number is not the same as the Yaffs1 serial number!</b></i></p>\r\n\r\n<p align=\"center\" class=\"western\"><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">Note: We still refer to chunk deletion in Yaffs2. The chunks are marked as deleted in the RAM data structures used by garbage collection etc, but there is no deletion marker written to flash.</p>\r\n\r\n<h2 class=\"western\">10.1 Sequence Number</h2>\r\n\r\n<p class=\"western\">The sequence number allows the chronological order to be determined which allows Yaffs to determine the sequence of events and restore the file system state. This is achieved by scanning <i><b>backwards</b></i> chronologically (ie. From the highest sequence number backwards to the lowest sequence number). Thus:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Since we\'re scanning backwards, the most recently written – and thus current – chunk matching an obj_id:chunk_id pair will be encountered first and all subsequent matching chunks must be obsolete and treated as deleted.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The file length in object headers are used to trim back resized files. If an object header is encountered, then subsequent data chunks that lie beyond that file length are clearly obsolete and treated as deleted. Note that both current and obsolete object header sizes must be used to obtain an accurate reconstruction.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">10.2 Shrink Header</h2>\r\n\r\n<p class=\"western\">Explaining the purpose of the shrink header marker is a bit convoluted. The purpose of a shrink header marker is to show that this object header indicates that a file has shrunken in size, and to prevent those object headers from being deleted by garbage collection. To understand this, it is easiest to consider what would happen if the shrink header markers did not exist.</p>\r\n\r\n<p class=\"western\">For simplification we\'ll assume no garbage collection happens during this part of the exercise. It is safe to make that assumption since it does not alter the reasoning.</p>\r\n\r\n<p class=\"western\">Consider a file that has been through the following:</p>\r\n\r\n<p class=\"code-western\"><font face=\"Liberation Mono, monospace\"><font size=\"2\"><b>#define MB (1024 * 1024)</b></font></font><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"code-western\"><code class=\"western\"><font face=\"Liberation Mono, monospace\"><font size=\"2\">h = open(“foo”,O_CREAT| O_RDWR, S_IREAD|S_IWRITE); /* create file */</font></font></code><br />\r\n<code class=\"western\"><font size=\"2\">write(h,data,5*</font></code><code class=\"western\"><font size=\"2\">MB</font></code><code class=\"western\"><font size=\"2\">); /* write 5 MB of data /<br />\r\ntruncate(h,1*</font></code><code class=\"western\"><font size=\"2\">MB</font></code><code class=\"western\"><font size=\"2\">); /* truncate to 1MB */<br />\r\nlseek(h,2*</font></code><code class=\"western\"><font size=\"2\">MB</font></code><code class=\"western\"><font size=\"2\">, SEEK_SET); /* set the file access position to 2MB */<br />\r\nwrite(h,data,1*</font></code><code class=\"western\"><font size=\"2\">MB</font></code><code class=\"western\"><font size=\"2\">); /* write 1MB of data */<br />\r\nclose(h);</font></code><br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The file will now be 3MB in length, but there will be a “hole” between 1MB and 2MB. According to POSIX (and to aid security etc) that “hole” should always read back as zeros.</p>\r\n\r\n<p class=\"western\">Yaffs2 would represent that in NAND with the following sequence of chunks:</p>\r\n\r\n<ol>\r\n	<li>\r\n	<p class=\"western\">Object header for file creation (file length = 0)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">5MB worth of data chunks (0 to 5MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object header for truncation (file length = 1MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">1MB worth of data chunks (2MB to 3MB)</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object header for close (file length = 3MB)</p>\r\n\r\n	<p class=\"western\">At this stage, only the the following data chunks are current:</p>\r\n	</li>\r\n</ol>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">The first 1MB of data created in step 2 above.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The 1MB of data created in step 4 above.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The object header created in step 5 above.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">But Yaffs must remember the truncation that happened in step 3, otherwise Yaffs will “forget” that there is a hole, which would cause the old data to be visible again. Thus, if Yaffs creates a hole it first writes a shrink header to indicate the start of the hole and a regular object header to mark the end of the hole.</p>\r\n\r\n<p class=\"western\">The shrink header modifies the behavior of the garbage collector to ensure that these shrink headers are not erased until it is safe to do so. This unfortunately has a negative impact on garbage collection in systems which make extensive use of files with holes.</p>\r\n\r\n<p class=\"western\">Shrink headers are also used to indicate that a file has been deleted and that the record of the file deletion is not lost.</p>\r\n\r\n<h2 class=\"western\">10.3 Yaffs2 extended tags</h2>\r\n\r\n<p class=\"western\">Yaffs2 extends the tags in object headers with extra fields to improve mount scanning performance. The operation of extended tags is described later in this document.</p>\r\n\r\n<h1 class=\"western\">11 Bad block handling NAND error handling</h1>\r\n\r\n<p class=\"western\">NAND flash is designed to be very low cost and very high density. In order to get high yields, and thus keep costs down, NAND flash is typically shipped with a few bad blocks and some further blocks are expected to go bad during the use of the device. Thus, any flash file system without an effective bad block handling policy is unsuitable for NAND flash.</p>\r\n\r\n<p class=\"western\">Yaffs1 uses the Smart-Media style of bad block marking. This checks the sixth byte (byte 5) of the spare area. In a good block this should be 0xFF. A factory-marked bad block should be 0x00. If Yaffs1 determines that a block has gone bad it is marked bad with 0x59 (\'Y\'). Using a distinctive marker allows Yaffs-marked bad blocks to be differentiated from factory-marked bad blocks.</p>\r\n\r\n<p class=\"western\">Yaffs2 mode is designed to support a wider range of devices and spare-area layouts. Thus, Yaffs2 does not decide which bytes to mark but instead calls driver functions to determine whether the block is bad, or to mark it bad.</p>\r\n\r\n<p class=\"western\">So, when does Yaffs mark a block bad? Yaffs will mark a block bad if a read or write operation fails or if three ECC errors are detected. A block that is marked bad is retired from use, thus improving the robustness of the file system. This policy is suitable for SLC flash. MLC policies are under development.</p>\r\n\r\n<p class=\"western\">Further, NAND flash cells can sometimes be disturbed (corrupted) by NAND activity and charge loss. These errors are corrected by error correction codes (ECC) which may be implemented in hardware, software drivers, or within Yaffs itself. Again, any flash file system lacking effective ECC handling is unsuitable for NAND flash.</p>\r\n\r\n<p class=\"western\">Yaffs1 mode can use either the built in ECC or use ECC provided by the driver or hardware.</p>\r\n\r\n<p class=\"western\">Since Yaffs2 mode is designed for a wider range of devices, it does not provide ECC internally but requires that the driver provide the ECC.</p>\r\n\r\n<p class=\"western\">The ECC code supplied with Yaffs (yaffs_ecc.c) is the fastest C code implementation of a Smart Media compatible ECC algorithm that we are aware of. This code will correct any single bit error within a 256-byte data block and detect 2 errors per 256-byte data block. This is sufficient error correction to provide very high reliability on most SLC-type NAND flash.</p>\r\n\r\n<p class=\"western\">For a more complete description of failure handling, please refer to the Yaffs error mitigation document.</p>\r\n\r\n<h1 class=\"western\">12 RAM structures</h1>\r\n\r\n<p class=\"western\">While it is theoretically possible to use a log structured file system with very few RAM structures, this would provide reduce performance. Thus, significant RAM data structures are required to store enough information to provide sufficiently high performance.</p>\r\n\r\n<p class=\"western\">Before we jump in, it is a good idea to first figure out what purpose the RAM structures serve. The main structures are defined in yaffs_guts.h and their main purposes, are :</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Device/partition: This is named <i><b>yaffs_dev</b></i><span><span>. </span></span>This is required to hold information relating to a Yaffs “partition” or “mount point”. Using these, rather than globals, allows Yaffs to simultaneously support multiple partitions and partitions of different types. In fact, almost other data structures mentioned here are part of, or accessed via, the structure.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">NAND Block Information: This is named <i><b>yaffs_block_info </b></i>and holds the current state of the NAND blocks. Each yaffs_dev has an array of these.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">NAND Chunk Information: This is a bitfield attached to the yaffs_dev that holds the current in-use state of each chunk in the system. There is one bit per chunk in the partition.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object: This is named <i><b>yaffs_obj</b></i> and holds the state of an object (see section 3). There is one of these for each object in the file system, where an object is one of a regular file, directory, hard link, symbolic link or special link. There are different variants of yaffs_Objects to reflect the different data required for these different object types. Each object is uniquely identified by an obj_id.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">File structure: For each file object, Yaffs holds a tree which provides the mechanism to find the data chunks in a file. The tree consists of nodes called <i><b>yaffs_tnode</b></i> (tree nodes).</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Directory structure: The directory structure allows objects to be found by name. The directory structure is built from a doubly-linked list that is rooted in the directory and binds together the sibling objects within a directory.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Object number hash table: The object number hash table provides a mechanism to find an object from its obj_id. The obj_id is hashed to select a hash bucket. Each hash bucket has a doubly-linked list of objects that belong in this hash bucket, as well as a count of the objects in the bucket.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Cache: Yaffs provides a read/write cache that significantly improves performance for short operations. The size of the cache can be set at run-time.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The usage of these structures is described in more detail below.</p>\r\n\r\n<p class=\"western\">Yaffs makes significant use of doubly-linked lists. These data structures are very useful because they provide rapid insertion, removal and traversal. They do however come at the expense of requiring two pointers rather than just one that would be required by a singly-linked list.</p>\r\n\r\n<h2 class=\"western\">12.1 Yaffs objects (struct yaffs_obj)</h2>\r\n\r\n<p class=\"western\">Each object (file, directory,...) in the system is represented by a yaffs_obj structure</p>\r\n\r\n<p class=\"western\">The yaffs_obj\'s main function is to store most of the object metadata and type-specific information. This means that almost all the information about an object can be accessed immediately without reading flash.</p>\r\n\r\n<p class=\"western\">The metadata includes, amongst others:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">obj_id: The number used to identify the object.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">parent: A pointer to the parent directory. Not all objects have a parent. The root director and special directories for unlinked and deleted files do not have parents so this is NULL.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">short_name: If the name is short enough to fit in a small fixed-sized array (default 16 characters) then it is stored here otherwise it must be fetched from flash each time it is requested.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">type: Type of object.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Permissions, time, ownership and other attributes</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><font size=\"3\">Depending on the object type, yaffs_obj also stores:</font></p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A tnode tree, file extents etc for data files</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A directory chain for directories</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">An equivalent object for hard links</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">A string for symbolic links</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">12.2 Look-up by obj_id</h2>\r\n\r\n<p class=\"western\">The purpose of this mechanism is to rapidly access an object by its device and obj_id. This is required during garbage collection, scanning and similar operations.</p>\r\n\r\n<p class=\"western\">Each yaffs_dev uses a hash table for this purpose. The hash table has 256 “buckets” (tunable), each holding a doubly-linked list of the yaffs_objs in that bucket.</p>\r\n\r\n<p class=\"western\">The hash table\'s hash function is just masking the least significant bits of the obj_id. This is a very cheap function that provides reasonable “spread”.</p>\r\n\r\n<p class=\"western\">Yaffs makes an attempt to assign obj_ids in a way that keeps the number of objects in each hash bucket quite low, thus preventing any one hash entry getting too long.</p>\r\n\r\n<p class=\"western\">This mechanism is also charged with assigning new (unique) obj_id values.</p>\r\n\r\n<h2 class=\"western\">12.3 Directory structure</h2>\r\n\r\n<p class=\"western\">The purpose of the directory structure is to rapidly access an object by name. This is required to perform file operations such as opening, renaming etc.</p>\r\n\r\n<p class=\"western\"><font size=\"3\">The Yaffs directory structure is formed by a tree of objects of type YAFFS_OBJECT_TYPE_DIRECTORY. These objects have a doubly linked node of children.</font></p>\r\n\r\n<p class=\"western\">Each yaffs_obj also has a doubly linked list node called sibling which chain together the siblings in a directory.</p>\r\n\r\n<p class=\"western\">Each Yaffs partition has a root directory.</p>\r\n\r\n<p class=\"western\">Each yaffs partition also has some special purpose “fake” directories which are not stored in NAND but are instead created on mounting:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">lost+found: This directory is used as a place to store any lost file parts that cannot be placed in the normal directory tree.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Unlinked and deleted: These are pseudo-directories that are not in the directory tree. Placing objects in these directories gives them a status of being unlinked or deleted.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p align=\"left\" class=\"western\">Each object has a name that is used to look up the object within the directory, Thus, each name within a directory must be unique. This rule does not apply to objects in the unlinked and deleted pseudo-directories since we never use names to look up unlinked/deleted files and there may be many files of the same name that have been deleted.</p>\r\n\r\n<p class=\"western\">Name resolution is sped up by two mechanisms:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">short_names are stored directly in the yaffs_obj so that they don\'t have to be loaded from flash.</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">Each object has a “name sum”. This is a simple hashing of the name which speeds up the matching process because it is cheaper to compare the hash than compare the whole name string.</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\"><img align=\"left\" height=\"355\" name=\"Object2\" src=\"image/png;base64,[encoded data, 22776 characters]\" width=\"412\" /><br clear=\"left\" />\r\n<br />\r\n&nbsp;</p>\r\n\r\n<p class=\"western\">The above directory structure represents the following directory tree</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"119\" />\r\n		<col width=\"468\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Root directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/a</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Directory containing c,d,e</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/b</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Empty directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/lost+found</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Empty directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/a/c</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Directory</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/a/d</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Data file</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"119\">\r\n			<p align=\"left\" class=\"western\"><font face=\"Courier, monospace\"><font size=\"3\">/a/e</font></font></p>\r\n			</td>\r\n			<td width=\"468\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Data file</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p align=\"left\" class=\"western\">&nbsp;</p>\r\n\r\n<p class=\"western\">If a Yaffs partition has been somehow corrupted then it is possible for an object to be created during scanning but with no object header (which contains the object name) being found. The object would then not have a name. In these cases, Yaffs creates a pseudonym from the object number of the form objnnnn so that the object can be placed in the directory structure.</p>\r\n\r\n<h2 class=\"western\">12.4 Hard links</h2>\r\n\r\n<p class=\"western\">In POSIX file systems, the objects (inodes in Linux terminology, vnode in BSD-speak) and the object names (dentry in Linux terminology) are independent in that an object can have zero, one or more names.</p>\r\n\r\n<p class=\"western\">Having files with zero links is caused by constructs such as:</p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">h = open(“foo”,O_CREAT | O_RDWR, S_IREAD | S_IWRITE); /* create file “foo” */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">unlink(“foo”); /* unlink it </font></code><code class=\"western\"><font size=\"2\">to remove</font></code><code class=\"western\"><font size=\"2\"> directory </font></code><code class=\"western\"><font size=\"2\">entry </font></code><code class=\"western\"><font size=\"2\">*/</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">/* we still have a handle to the file so can still access it via the handle */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">read(h,buffer,100); </font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">write(h,buffer,100);</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\"><font face=\"Courier 10 Pitch\"><font size=\"1\"><code class=\"western\"><font size=\"2\">close(h); /* as soon as the handle is closed the file is deleted */</font></code></font></font></p>\r\n\r\n<p align=\"left\" class=\"western\">Yaffs does not store separate object and name records because that would waste space and incur extra reading/writing. Instead, Yaffs is optimised for the most typical case of one name per object. That is, Yaffs stores one name per yaffs_obj and uses “cheats” to achieve other behavior:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Objects that have zero names (ie unlinked objects) are handled by renaming the object into a fake directory that is used to designate unlinked objects.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Multiple names object are handled by designating one of the objects the main object and having multiple hard link pseudo-objects that refer to it.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">The inode number (vnid in BSD-speak) is a per-file system unique number that identifies the object. The Yaffs obj_id of the main object is presented to the user. That is, if the inode number is queried from a hardlink object then the obj_id of the equivalent object is returned.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The linkage between hard link pseudo-objects and their equivalent object is through two fields:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Each object has a hard_links doubly linked list node that links the list of hard links to that object.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Each hard link has a equiv_obj pointer to the “equivalent object”.</p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">&nbsp;</p>\r\n\r\n<p class=\"western\">The usage of this structure is straight forward except for one interesting case when hard links are being deleted. Consider the following sequence of events:</p>\r\n\r\n<table cellpadding=\"4\" cellspacing=\"0\" width=\"605\">\r\n	<colgroup>\r\n		<col width=\"50\" />\r\n		<col width=\"80\" />\r\n		<col width=\"449\" />\r\n	</colgroup>\r\n	<tbody>\r\n		<tr valign=\"top\">\r\n			<td width=\"50\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">1</font></p>\r\n			</td>\r\n			<td width=\"80\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\"># touch a</font></p>\r\n			</td>\r\n			<td width=\"449\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Create file a</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"50\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">2</font></p>\r\n			</td>\r\n			<td width=\"80\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\"># ln a b</font></p>\r\n			</td>\r\n			<td width=\"449\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Create a hard link b with equivalent object a.</font></p>\r\n			</td>\r\n		</tr>\r\n		<tr valign=\"top\">\r\n			<td width=\"50\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">3</font></p>\r\n			</td>\r\n			<td width=\"80\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\"># rm a</font></p>\r\n			</td>\r\n			<td width=\"449\">\r\n			<p align=\"left\" class=\"western\"><font size=\"3\">Remove a. The object must still exist under the name b</font></p>\r\n			</td>\r\n		</tr>\r\n	</tbody>\r\n</table>\r\n\r\n<p align=\"left\" class=\"western\">&nbsp;</p>\r\n\r\n<p class=\"western\">In step 3, Yaffs cannot just delete the equivalent object since that would leave a “hanging” hard link with no equivalent object.</p>\r\n\r\n<p class=\"western\">We also cannot just change the type of one of the hardlink objects because that would cause the object\'s obj_id to change. This would be unacceptable since it would break any mechanism that use the obj_id (of which there are many within and outside of Yaffs).</p>\r\n\r\n<p class=\"western\">Thus, if an object is deleted and it has hard links to it then we do the following:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\">Select one of the object\'s hard links.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Rename the object to the name in the hard link.</p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\">Delete the hard link pseudo-object.</p>\r\n	</li>\r\n</ul>\r\n\r\n<h2 class=\"western\">12.5 Symbolic links and special objects</h2>\r\n\r\n<p class=\"western\">These object types just store values that are needed to provide a full POSIX file system.</p>\r\n\r\n<p class=\"western\">A symbolic link object saves a symbolic link string that is used to provide the POSIX symbolic string mechanism.</p>\r\n\r\n<p class=\"western\">A special object is used to store entries for device numbers, pipes and other similar special objects.</p>\r\n\r\n<h2 class=\"western\">12.6 File objects</h2>\r\n\r\n<p class=\"western\">The yaffs_Objects have a type and a variant portion which is a union of different data required to hold the state of different object types.</p>\r\n\r\n<p class=\"western\">The file objects have type YAFFS_OBJECT_TYPE_FILE, and an associated file variant. This stores the following main values:</p>\r\n\r\n<ul>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">file_size: file size</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">top_level: depth of the Tnode tree</font></p>\r\n	</li>\r\n	<li>\r\n	<p class=\"western\"><font face=\"Liberation Serif, serif\">top: pointer to top of Tnode tree</font></p>\r\n	</li>\r\n</ul>\r\n\r\n<p class=\"western\">The meaning of the file size is quite obvious. This is the current length of the file. Note that a file might have holes in it caused by seeking forward past the extents of the file.</p>\r\n\r\n<p class=\"western\">The top and top_level work together to control the Tnode trees.</p>\r\n\r\n<h3 class=\"western\">12.6.1 Tnode tree</h3>\r\n\r\n<p class=\"western\">Each file has a tnode tree to provide the mapping from file position to actual NAND chunk address. top is a pointer to the top of the tnode tree</p>\r\n\r\n<p class=\"western\">&nbsp;</p>\r\n\r\n<p class=\"western\"><img align=\"left\" height=\"378\" name=\"Object1\" src=\"image/png;base64,[encoded data, 7509 characters]