<h2>1 Background</h2><p>Cheaper CPUs and flash have driven up embedded system functionality. These increased functions often require file system storage.</p><p class="western">The original flash file storage mechanisms were the use of a flash translation layer (FTL) – a driver model which makes flash memory appear as a disk drive – in conjunction with a disk-oriented file system such as FAT. This storage methodology has various performance and robustness limitations, leading to the development of file systems designed specifically for flash memory. These are known as Flash File Systems.</p><p class="western">Flash memory has various limitations when compared with a disk. For example, flash memory pages cannot be individually re-written but instead a whole block must be erased and rewritten with the modified page. Typical FTL achieve this with various logical to physical mapping schemes. Thus emulating disk-like behaviour on flash memory adds an extra layer of software. This slows down write performance and adds extra state which is prone to corruption due to power failure.</p><p class="western">The traditional FTL-based solution always stores FA Tables in the same disk blocks. Since flash memory blocks can endure a limited number of write/erasure cycles, the FTL must move the physical location of the block around to prevent the high traffic blocks from wearing out. This requires wear levelling – a further burden.</p><p class="western">Some File Systems, FSs, try to mitigate the performance penalties by caching a lot of data to reduce writes. While this can reduce the apparent write time, it does make the FS prone to data loss in the event of a power failure.</p><p class="western">But flash memory also has advantages when compared to rotating media. There is no spin up time, making it more responsive. There is no read head so there is no read penalty if data is fragmented. Flash FSs can be designed to exploit these advantages.</p><p class="western">A true Flash File System, FFS<a class="sdfootnoteanc" href="#sdfootnote1sym" name="sdfootnote1anc"><sup>1</sup></a> on the other hand is a single code body designed to take the features and limitations of flash into consideration. FFSs can avoid using unsuitable techniques such as allocation tables and in-place rewriting.</p><p class="western">Yaffs is a FFS originally designed specifically for NAND flash, and also used with great success on NOR flash too. Yaffs development started in 2001 when 32Mbytes of flash was considered large. Since then, yaffs has been expanded to work properly with different flash types and far larger memory arrays (many GBytes).</p><p class="western">Yaffs was originally deployed in Linux, but was written in an OS-neutral way to facilitate modular development and testing. This turned out to be convenient when we ported it to other OSs such as WinCE, eCos, pSOS, ThreadX, vXworks</p><p class="western">Since then Yaffs has been used in many application areas including point-of-sale equipment, telecoms equipment (including Android phones), industrial control systems and other, many of them criticially dependent on reliability for their value.</p><h2 class="western">2 Flash File System considerations</h2><p class="western">There are many criteria that are important when selecting a FFS. These are some that out customers have found to be most important:</p><ul><li><p class="western">Robustness: It really is not worth having a file system that loses or corrupts data and prevents the system from working. Some flash file systems are only robust when synced (flushed) after files have been written. That requires extra code, slows the system and leaves the file system in a non-deterministic state until the sync has been completed. Some file systems need to perform disk repair operations (eg. check disk) in the event of a power failure. This can add considerable time to the start up, delaying system operation.</p></li><li><p class="western">Performance: In many embedded system designs, slow read/write performance can delay tasks and cause system degradation or even failure. It is thus important that read/write performance be acceptable. Note that many FFSs need to perform extra actions such as garbage collection. In some cases these actions can cause the file system to stall for a long time. It is thus important that these factors be taken into consideration. Performance can sometimes be improved by adding a write caching layer. These caching layers make the file system calls (write etc) proceed quickly, without actually writing the data to flash. This is risky as data in the cache will be lost on a power failure and syncing the file system will take a long time. Thus, most flash file systems offer either speed or robustness – not both.</p></li><li><p class="western">Proven: It is very easy for a file system vendor to provide a list of claims saying “100% power safe” or similar, but what evidence do they have to back this up? All file systems, including flash file systems, are complex bodies of software with extremely complex state. They need very significant testing to prove that they work correctly.</p></li><li><p class="western">Portable: Software is a huge investment, and it is increasingly important to be able to port the software between OSs and CPU architectures. Most flash file systems are limited to a single OS which makes it difficult to migrate software between different platforms. The portability of yaffs extends to being able to seamlessly migrate the core yaffs code into a test framework (which enhances testing) and being easy to adapt to fresh platforms</p></li></ul><h2 class="western" style="page-break-before: always;">3 How YAFFS achieves robustness and performance</h2><p class="western">Yaffs was designed from the ground up specifically to work well with NAND flash. The author had already developed another FFS and was familiar with the design challenges of flash.</p><p class="western">Yaffs is not just a disk-oriented FS adapted to work with flash, so it is not surprising that yaffs is very different in design to most other file systems. The key difference is that yaffs is a log-structured file system<a class="sdfootnoteanc" href="#sdfootnote2sym" name="sdfootnote2anc"><sup>2</sup></a>.</p><p class="western">In essence, a log structured file system writes file system changes as a sequential log. The log structure is particularly suitable because:</p><ul><li><p class="western">All writes are at the end of the log. When a file is modified, there is no need to erase and rewrite a part of the flash. Changes are just appended to the log. Thus there is no need to erase and copy old data just to change some existing pages in a file. This tends to make writes much faster. There is no need to do a lot of caching to achieve performance. Data can be written to the log immediately meaning that sync time is very low. Data robustness is improved dramatically.</p></li><li><p class="western">There are no allocation tables; this reduces the amount of data being written to the flash, and there are no tables to corrupt.</p></li><li><p class="western">Since all writes are to the end of the log there are no “high traffic” blocks. This means yaffs only needs a simple wear levelling strategy.</p></li><li><p class="western">In the event of a power failure, the file system state is easily re-created from the log. This means there is no need to perform disk repair operations to correct for a bad shutdown.</p></li></ul><p class="western">So why don't we see many log structured file systems for disk file systems? The answer is that log file systems tend to spread the writes around on the media. That is typically very problematic for mechanical storage which must physically move the read/write head from one location to another making the access time very slow. Of course that does not apply for flash memory. By designing specifically for flash, yaffs can write fast.</p><p class="western">Many FFSs need to compromise robustness and performance. Thanks to the log structure, yaffs does not need to; it can provide high performance without giving up on robustness.</p><p class="western">Those familiar with log structured file system design will know that some log structured file systems have problems with garbage collection, GC<a class="sdfootnoteanc" href="#sdfootnote3sym" name="sdfootnote3anc"><sup>3</sup></a>. Garbage collection cleans up the log and makes more free space available. Some log-structured file system designs did not pay enough attention to GC and can stall for a considerable time while it happens. Yaffs was designed differently. The potential impact of GC was considered throughout the design process. As a result, yaffs has a very simple GC model that allows a lot of flexibility in scheduling. This prevents the GC from making the file system slow and unresponsive.</p><p class="western">Most file systems are developed within the context and framework of a single OS. This typically makes the code difficult to explore, debug and port. Yaffs on the other hand was developed from the start inside a portable development/testing framework. Yaffs is then ported to various operating systems through the addition of glue code.</p><p class="western">[ibimage==236==original==none==self==null]</p><p class="western">There are many benefits to this approach:</p><ul><li><p class="western">A development framework runs in user space which provides a richer development environment than inside an OS kernel. It is much easier to attach debuggers and perform logging and create reproducible testing.</p></li><li><p class="western">The code is structured to be portable. That makes it relatively easy to port to a new OS or RTOS with a high level of confidence.</p></li><li><p class="western">There is a choice of porting interfaces allowing more flexibility to determine which porting method will be simpler. For example, the Linux wrapper accesses the yaffs core directly while the Windows CE wrapper access the Yaffs Direct Interface<a class="sdfootnoteanc" href="#sdfootnote4sym" name="sdfootnote4anc"><sup>4</sup></a>.</p></li><li><p class="western">Each different environment provides different test tools. For example, Linux provides a raft of file system test tools that do different kinds of testing to the power fail test harness. Since the vast majority of the code is in the portable yaffs core, that code gets the advantage of all test methods combined. The many millions of Android phone users that use yaffs-based devices are helping test the core code that is also used on a wide variety of different operating systems and a wide range of different device types.</p></li><li><p class="western">Its Open Source status means any interested parson can take a version from git and test it to make sure it meets their needs.</p></li></ul><h2 class="western" style="page-break-before: always;">4 Testing</h2><p class="western">It is all very well to design software to be robust, but it needs thorough testing to build the confidence of users in the author and the supplier.</p><p class="western">File systems have incredibly complex state so it is not at all surprising that OS designers find FSs some of the most challenging code to test.</p><p class="western">There is no single test methodology that can test all cases, and exhaustive state testing is impossible. The best we can do is to have a suite of test methodologies that combine to provide thorough testing.</p><p class="western">Approximately 60% of yaffs development effort is now directed towards testing and improving the test environment. We are constantly researching new methodologies.</p><p class="western">The following are descriptions of the most important test strategies used in yaffs.</p><h3 class="western">Power Fail Stress Testing</h3><p class="western">By far the most important consideration in yaffs testing is that it is robust to corruption caused by power failure. We want yaffs to provide the basis for reliable products in typical embedded systems.</p><p class="western">The first power fail testing was done by companies integrating yaffs into their products. Some of these built test jigs which would automatically interrupt power while running a real hardware device. This approach is better than nothing, but has some limitations:</p><ul><li><p class="western">It is relatively costly to set up. Flash has a limited lifetime meaning that test devices wear out with time and must be replaced.</p></li><li><p class="western">It is relatively slow since each cycle takes many seconds to boot, run the application for a while and then reboot.</p></li><li><p class="western">Many, and perhaps most, of the power interruptions will happen at times when the file system is inactive and thus be wasted cycles.</p></li></ul><p class="western">In 2008 we developed a power-fail-test environment. This simulates power failures using simulated flash memory while running various consistency checks.</p><p class="western">The benefits of a software-based test harness are numerous:</p><ul><li><p class="western">The test harness controls the point at which the simulated power failures happen. That means every test cycle counts.</p></li><li><p class="western">No special hardware is required.</p></li><li><p class="western">Test loops run faster; a quad-core computer can simulate ten to twenty power failures per second. That means about a million power failure simulations per day.</p></li></ul><p class="western">The effectiveness of the new software-based testing is dramatic.&nbsp; A few times we have tested some user-suggested changes that had passed a week or more of real-world power fail testing only to have problems caught by the software simulation in a matter of minutes.</p><p class="western">We regularly run the power fail simulation test over a weekend, testing millions of power fail cycles.</p><h3 class="western">Testing the Yaffs Direct Interface API</h3><p class="western">Yaffs Direct Interface (YDI) is a POSIX-like wrapper around the yaffs core. This provides a set of function calls; yaffs_open(), yaffs_write() and the like.</p><p class="western">Each function can return numerous error codes and has to handle numerous different parameters.</p><p class="western">Although there were already significant tests for the YDI, an extensive test harness was developed in 2010 to comprehensively test <strong>each</strong> of the error paths and generate all of the required errors.</p><p class="western">This provides a high level of confidence that the test interfaces do what they should and that POSIX-like functionality provides relatively clean porting of existing code.</p><h3 class="western">Linux Testing</h3><p class="western">The main purpose of the Linux testing is to test the Linux wrapper code. It does, however, still exercise the yaffs core code in different ways from other testing, and thus adds to the strength of the test fabric.</p><p class="western">The Linux testing does not do power fail testing but instead tests other aspects such as clean shutdowns, cache integrity and write performance. This gives a useful way to assess the effects of modifications.</p><p class="western">Linux testing is done on both real hardware and on simulated hardware (nandsim).</p><p class="western">As with the Power Fail Stress Testing described above, the simulated testing is done using:</p><h3 class="western">Fuzz Testing</h3><p class="western">Fuzz testing deliberately corrupts the flash image and ensures that yaffs still mounts. This is done to ensure that yaffs does not crash on corrupted data.</p><p class="western">Clearly fuzz testing can corrupt the contents of individual files (that's its job), but the file system integrity should not be compromised.</p><h3 class="western">Software checking</h3><p class="western">Although code checking is not actual testing per se, it does help to verify code changes.</p><p class="western">The yaffs code has been checked with Coverity<a class="sdfootnoteanc" href="#sdfootnote5sym" name="sdfootnote5anc"><sup>5</sup></a> – a market leader in code checking.</p><p class="western">We are investigating the use of KLEE and other tools.</p><h3 class="western">Open Source Use</h3><p>Each different project uses yaffs in a different way, so that collectively a wider range of operation sequences are tested.</p><p class="western">&nbsp;Many of these users participate in some way on the yaffs mailing list, helping to uncover issues and generally provide feedback.</p><h2 class="western">5 Universities</h2><p class="western">Over its lifetime, yaffs has been used as a teaching framework for various university course in many counties around the world.</p><p class="western">For example, Oregon State University uses Yaffs as a test bench for their post-graduate software testing course. They essentially test the test tools by injecting errors into the yaffs code and seeing if these are discovered. Of course this has the side effect of running these tests over the original Yaffs code too. Those interested in test methods are encouraged to read some of the OSU papers</p><p class="western">We expect to adopt and extend some of the OSU ideas and use these to extend our in-house test suite.</p><p class="western">Many other examples of academic studies of Yaffs can be found using a standard search engine. Some examples are:-</p><p class="western">Utah State University, School of Computing, used Yaffs as a Case Study in their work on Swarm Testing in 2011.&nbsp; www.cs.utah.edu/~regehr/papers/swarm12.pdf</p><p class="western">National Taiwan University, Dept of Computer Science, published on <i>Effiicient Initialization and Crash Recovery for Log-based File Systems Over Flash Memory</i><span style="font-style: normal">&nbsp;</span> Copyright 2006 ACM 1-59593-108-2/06/0004&nbsp; and http://www.cis.nctu.edu.tw/~lpchang/papers/SAC_wu_sac06.pdf</p><p class="western">DeDupe in YAFFS, K Narayan &amp; P S Vijayakrishnan, Comp Sci, U Wisconsin.&nbsp; http://pages.cs.wisc.edu/~knarayan/dedup-yaffs.pdf&nbsp; (2011)<br>&nbsp;</p><h2 class="western">6 Conclusions</h2><p class="western">Software development is an iterative process of design and testing. Robust software solutions are only possible with extensive testing.</p><p class="western">Embedded systems increasingly need to store data in file systems as a critical part of their operation. It is thus imperative to have a file system that is predictable and robust to power failure and similar interruptions.</p><p class="western">Yaffs has been designed from scratch to be robust to power failure and that robustness has been verified with a multi-pronged test strategy that is continuously being improved.</p><h3 class="western" style="page-break-before: always;">References</h3><div id="sdfootnote1"><p class="sdfootnote-western"><a class="sdfootnotesym" href="#sdfootnote1anc" name="sdfootnote1sym">1</a>Note that flash translation layers (FTLs) are erroneously referred to as flash file systems. For example, despite the name, M-Systems TrueFFS is not a flash file system but a flash translation layer (FTL).</p></div><div id="sdfootnote2"><p class="sdfootnote-western"><a class="sdfootnotesym" href="#sdfootnote2anc" name="sdfootnote2sym">2</a>This document only provides a brief overview of the yaffs architecture and design.&nbsp; For more details of the yaffs design and architecture see&nbsp; <a href="http://yaffs.net/documents/how-yaffs-works">http://yaffs.net/documents/how-yaffs-works</a></p></div><div id="sdfootnote3"><p class="sdfootnote-western"><a class="sdfootnotesym" href="#sdfootnote3anc" name="sdfootnote3sym">3</a>Refer to the “How Yaffs Works” document to understand the need for garbage collection and how yaffs does it.</p></div><div id="sdfootnote4"><p class="sdfootnote-western"><a class="sdfootnotesym" href="#sdfootnote4anc" name="sdfootnote4sym">4</a>For more information on Yaffs Direct Interface, please&nbsp; see <a href="http://yaffs.net/documents">http://yaffs.net/documents/yaffs-direct-interface </a></p></div><div id="sdfootnote5"><p class="sdfootnote-western"><a class="sdfootnotesym" href="#sdfootnote5anc" name="sdfootnote5sym">5</a>http://www.coverity.com</p></div><p>&nbsp;</p>