1 Index: linux-2.6.23-rc6/fs/Kconfig
2 ===================================================================
3 --- linux-2.6.23-rc6.orig/fs/Kconfig 2007-09-21 16:24:06.000000000 +0800
4 +++ linux-2.6.23-rc6/fs/Kconfig 2007-09-21 16:24:08.000000000 +0800
7 source "fs/xfs/Kconfig"
8 source "fs/gfs2/Kconfig"
9 +source "fs/yaffs2/Kconfig"
12 tristate "OCFS2 file system support"
13 Index: linux-2.6.23-rc6/fs/Makefile
14 ===================================================================
15 --- linux-2.6.23-rc6.orig/fs/Makefile 2007-09-21 16:24:06.000000000 +0800
16 +++ linux-2.6.23-rc6/fs/Makefile 2007-09-21 16:24:08.000000000 +0800
18 obj-$(CONFIG_DEBUG_FS) += debugfs/
19 obj-$(CONFIG_OCFS2_FS) += ocfs2/
20 obj-$(CONFIG_GFS2_FS) += gfs2/
21 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
22 Index: linux-2.6.23-rc6/fs/yaffs2/Kconfig
23 ===================================================================
24 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
25 +++ linux-2.6.23-rc6/fs/yaffs2/Kconfig 2007-09-21 16:24:08.000000000 +0800
28 +# YAFFS file system configurations
32 + tristate "YAFFS2 file system support"
38 + YAFFS2, or Yet Another Flash Filing System, is a filing system
39 + optimised for NAND Flash chips.
41 + To compile the YAFFS2 file system support as a module, choose M
42 + here: the module will be called yaffs2.
46 + Further information on YAFFS2 is available at
47 + <http://www.aleph1.co.uk/yaffs/>.
50 + bool "512 byte / page devices"
54 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
56 + Not needed for 2K-page devices.
60 +config YAFFS_9BYTE_TAGS
61 + bool "Use older-style on-NAND data format with pageStatus byte"
62 + depends on YAFFS_YAFFS1
66 + Older-style on-NAND data format has a "pageStatus" byte to record
67 + chunk/page state. This byte is zero when the page is discarded.
68 + Choose this option if you have existing on-NAND data using this
69 + format that you need to continue to support. New data written
70 + also uses the older-style format. Note: Use of this option
71 + generally requires that MTD's oob layout be adjusted to use the
72 + older-style format. See notes on tags formats and MTD versions.
76 +config YAFFS_DOES_ECC
77 + bool "Lets Yaffs do its own ECC"
78 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
81 + This enables Yaffs to use its own ECC functions instead of using
82 + the ones from the generic MTD-NAND driver.
86 +config YAFFS_ECC_WRONG_ORDER
87 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
88 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
91 + This makes yaffs_ecc.c use the same ecc byte order as Steven
92 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
93 + order as SmartMedia.
98 + bool "2048 byte (or larger) / page devices"
102 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
106 +config YAFFS_AUTO_YAFFS2
107 + bool "Autoselect yaffs2 format"
108 + depends on YAFFS_YAFFS2
111 + Without this, you need to explicitely use yaffs2 as the file
112 + system type. With this, you can say "yaffs" and yaffs or yaffs2
113 + will be used depending on the device page size (yaffs on
114 + 512-byte page devices, yaffs2 on 2K page devices).
118 +config YAFFS_DISABLE_LAZY_LOAD
119 + bool "Disable lazy loading"
120 + depends on YAFFS_YAFFS2
123 + "Lazy loading" defers loading file details until they are
124 + required. This saves mount time, but makes the first look-up
127 + Lazy loading will only happen if enabled by this option being 'n'
128 + and if the appropriate tags are available, else yaffs2 will
129 + automatically fall back to immediate loading and do the right
132 + Lazy laoding will be required by checkpointing.
134 + Setting this to 'y' will disable lazy loading.
138 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
139 + int "Reserved blocks for checkpointing"
140 + depends on YAFFS_YAFFS2
143 + Give the number of Blocks to reserve for checkpointing.
144 + Checkpointing saves the state at unmount so that mounting is
145 + much faster as a scan of all the flash to regenerate this state
146 + is not needed. These Blocks are reserved per partition, so if
147 + you have very small partitions the default (10) may be a mess
148 + for you. You can set this value to 0, but that does not mean
149 + checkpointing is disabled at all. There only won't be any
150 + specially reserved blocks for checkpointing, so if there is
151 + enough free space on the filesystem, it will be used for
154 + If unsure, leave at default (10), but don't wonder if there are
155 + always 2MB used on your large page device partition (10 x 2k
156 + pagesize). When using small partitions or when being very small
157 + on space, you probably want to set this to zero.
159 +config YAFFS_DISABLE_WIDE_TNODES
160 + bool "Turn off wide tnodes"
161 + depends on YAFFS_FS
164 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
165 + page devices and >=128MB for 2k page devices. They use slightly
166 + more RAM but are faster since they eliminate chunk group
169 + Setting this to 'y' will force tnode width to 16 bits and save
170 + memory but make large arrays slower.
174 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
175 + bool "Force chunk erase check"
176 + depends on YAFFS_FS
179 + Normally YAFFS only checks chunks before writing until an erased
180 + chunk is found. This helps to detect any partially written
181 + chunks that might have happened due to power loss.
183 + Enabling this forces on the test that chunks are erased in flash
184 + before writing to them. This takes more time but is potentially
187 + Suggest setting Y during development and ironing out driver
188 + issues etc. Suggest setting to N if you want faster writing.
192 +config YAFFS_SHORT_NAMES_IN_RAM
193 + bool "Cache short names in RAM"
194 + depends on YAFFS_FS
197 + If this config is set, then short names are stored with the
198 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
199 + but makes look-ups faster.
202 Index: linux-2.6.23-rc6/fs/yaffs2/Makefile
203 ===================================================================
204 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
205 +++ linux-2.6.23-rc6/fs/yaffs2/Makefile 2007-09-21 16:24:08.000000000 +0800
208 +# Makefile for the linux YAFFS filesystem routines.
211 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
213 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
214 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
215 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
216 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
217 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
218 Index: linux-2.6.23-rc6/fs/yaffs2/devextras.h
219 ===================================================================
220 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
221 +++ linux-2.6.23-rc6/fs/yaffs2/devextras.h 2007-09-21 16:24:08.000000000 +0800
224 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
226 + * Copyright (C) 2002-2007 Aleph One Ltd.
227 + * for Toby Churchill Ltd and Brightstar Engineering
229 + * Created by Charles Manning <charles@aleph1.co.uk>
231 + * This program is free software; you can redistribute it and/or modify
232 + * it under the terms of the GNU Lesser General Public License version 2.1 as
233 + * published by the Free Software Foundation.
235 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
239 + * This file is just holds extra declarations used during development.
240 + * Most of these are from kernel includes placed here so we can use them in
245 +#ifndef __EXTRAS_H__
246 +#define __EXTRAS_H__
249 +#define __inline__ __inline
253 +#if !(defined __KERNEL__) || (defined WIN32)
255 +/* User space defines */
257 +typedef unsigned char __u8;
258 +typedef unsigned short __u16;
259 +typedef unsigned __u32;
262 + * Simple doubly linked list implementation.
264 + * Some of the internal functions ("__xxx") are useful when
265 + * manipulating whole lists rather than single entries, as
266 + * sometimes we already know the next/prev entries and we can
267 + * generate better code by using them directly rather than
268 + * using the generic single-entry routines.
271 +#define prefetch(x) 1
274 + struct list_head *next, *prev;
277 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
279 +#define LIST_HEAD(name) \
280 + struct list_head name = LIST_HEAD_INIT(name)
282 +#define INIT_LIST_HEAD(ptr) do { \
283 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
287 + * Insert a new entry between two known consecutive entries.
289 + * This is only for internal list manipulation where we know
290 + * the prev/next entries already!
292 +static __inline__ void __list_add(struct list_head *new,
293 + struct list_head *prev,
294 + struct list_head *next)
303 + * list_add - add a new entry
304 + * @new: new entry to be added
305 + * @head: list head to add it after
307 + * Insert a new entry after the specified head.
308 + * This is good for implementing stacks.
310 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
312 + __list_add(new, head, head->next);
316 + * list_add_tail - add a new entry
317 + * @new: new entry to be added
318 + * @head: list head to add it before
320 + * Insert a new entry before the specified head.
321 + * This is useful for implementing queues.
323 +static __inline__ void list_add_tail(struct list_head *new,
324 + struct list_head *head)
326 + __list_add(new, head->prev, head);
330 + * Delete a list entry by making the prev/next entries
331 + * point to each other.
333 + * This is only for internal list manipulation where we know
334 + * the prev/next entries already!
336 +static __inline__ void __list_del(struct list_head *prev,
337 + struct list_head *next)
344 + * list_del - deletes entry from list.
345 + * @entry: the element to delete from the list.
346 + * Note: list_empty on entry does not return true after this, the entry is
347 + * in an undefined state.
349 +static __inline__ void list_del(struct list_head *entry)
351 + __list_del(entry->prev, entry->next);
355 + * list_del_init - deletes entry from list and reinitialize it.
356 + * @entry: the element to delete from the list.
358 +static __inline__ void list_del_init(struct list_head *entry)
360 + __list_del(entry->prev, entry->next);
361 + INIT_LIST_HEAD(entry);
365 + * list_empty - tests whether a list is empty
366 + * @head: the list to test.
368 +static __inline__ int list_empty(struct list_head *head)
370 + return head->next == head;
374 + * list_splice - join two lists
375 + * @list: the new list to add.
376 + * @head: the place to add it in the first list.
378 +static __inline__ void list_splice(struct list_head *list,
379 + struct list_head *head)
381 + struct list_head *first = list->next;
383 + if (first != list) {
384 + struct list_head *last = list->prev;
385 + struct list_head *at = head->next;
387 + first->prev = head;
388 + head->next = first;
396 + * list_entry - get the struct for this entry
397 + * @ptr: the &struct list_head pointer.
398 + * @type: the type of the struct this is embedded in.
399 + * @member: the name of the list_struct within the struct.
401 +#define list_entry(ptr, type, member) \
402 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
405 + * list_for_each - iterate over a list
406 + * @pos: the &struct list_head to use as a loop counter.
407 + * @head: the head for your list.
409 +#define list_for_each(pos, head) \
410 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
411 + pos = pos->next, prefetch(pos->next))
414 + * list_for_each_safe - iterate over a list safe against removal
416 + * @pos: the &struct list_head to use as a loop counter.
417 + * @n: another &struct list_head to use as temporary storage
418 + * @head: the head for your list.
420 +#define list_for_each_safe(pos, n, head) \
421 + for (pos = (head)->next, n = pos->next; pos != (head); \
422 + pos = n, n = pos->next)
427 +#define DT_UNKNOWN 0
438 +#include <sys/stat.h>
442 + * Attribute flags. These should be or-ed together to figure out what
443 + * has been changed!
449 +#define ATTR_ATIME 16
450 +#define ATTR_MTIME 32
451 +#define ATTR_CTIME 64
452 +#define ATTR_ATIME_SET 128
453 +#define ATTR_MTIME_SET 256
454 +#define ATTR_FORCE 512 /* Not a change, but a change it */
455 +#define ATTR_ATTR_FLAG 1024
458 + unsigned int ia_valid;
466 + unsigned int ia_attr_flags;
474 +#include <linux/types.h>
475 +#include <linux/list.h>
476 +#include <linux/fs.h>
477 +#include <linux/stat.h>
487 Index: linux-2.6.23-rc6/fs/yaffs2/moduleconfig.h
488 ===================================================================
489 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
490 +++ linux-2.6.23-rc6/fs/yaffs2/moduleconfig.h 2007-09-21 16:24:08.000000000 +0800
493 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
495 + * Copyright (C) 2002-2007 Aleph One Ltd.
496 + * for Toby Churchill Ltd and Brightstar Engineering
498 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
500 + * This program is free software; you can redistribute it and/or modify
501 + * it under the terms of the GNU Lesser General Public License version 2.1 as
502 + * published by the Free Software Foundation.
504 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
507 +#ifndef __YAFFS_CONFIG_H__
508 +#define __YAFFS_CONFIG_H__
510 +#ifdef YAFFS_OUT_OF_TREE
512 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
513 +#define CONFIG_YAFFS_FS
514 +#define CONFIG_YAFFS_YAFFS1
515 +#define CONFIG_YAFFS_YAFFS2
517 +/* These options are independent of each other. Select those that matter. */
519 +/* Default: Not selected */
520 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
521 +//#define CONFIG_YAFFS_DOES_ECC
523 +/* Default: Not selected */
524 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
525 +/* CONFIG_YAFFS_DOES_ECC is set */
526 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
528 +/* Default: Selected */
529 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
530 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
532 +/* Default: Selected */
533 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
534 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
537 +/* Meaning: set the count of blocks to reserve for checkpointing */
538 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
541 +Older-style on-NAND data format has a "pageStatus" byte to record
542 +chunk/page state. This byte is zeroed when the page is discarded.
543 +Choose this option if you have existing on-NAND data in this format
544 +that you need to continue to support. New data written also uses the
546 +Note: Use of this option generally requires that MTD's oob layout be
547 +adjusted to use the older-style format. See notes on tags formats and
550 +/* Default: Not selected */
551 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
552 +#define CONFIG_YAFFS_9BYTE_TAGS
554 +#endif /* YAFFS_OUT_OF_TREE */
556 +#endif /* __YAFFS_CONFIG_H__ */
557 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.c
558 ===================================================================
559 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
560 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.c 2007-09-21 16:24:08.000000000 +0800
563 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
565 + * Copyright (C) 2002-2007 Aleph One Ltd.
566 + * for Toby Churchill Ltd and Brightstar Engineering
568 + * Created by Charles Manning <charles@aleph1.co.uk>
570 + * This program is free software; you can redistribute it and/or modify
571 + * it under the terms of the GNU General Public License version 2 as
572 + * published by the Free Software Foundation.
575 +const char *yaffs_checkptrw_c_version =
576 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
579 +#include "yaffs_checkptrw.h"
582 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
585 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
587 + T(YAFFS_TRACE_CHECKPOINT,
588 + (TSTR("checkpt blocks available = %d" TENDSTR),
592 + return (blocksAvailable <= 0) ? 0 : 1;
596 +static int yaffs_CheckpointErase(yaffs_Device *dev)
602 + if(!dev->eraseBlockInNAND)
604 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
605 + dev->internalStartBlock,dev->internalEndBlock));
607 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
608 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
609 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
610 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
611 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
612 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
613 + dev->nErasedBlocks++;
614 + dev->nFreeChunks += dev->nChunksPerBlock;
617 + dev->markNANDBlockBad(dev,i);
618 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
623 + dev->blocksInCheckpoint = 0;
629 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
632 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
633 + T(YAFFS_TRACE_CHECKPOINT,
634 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
635 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
637 + if(dev->checkpointNextBlock >= 0 &&
638 + dev->checkpointNextBlock <= dev->internalEndBlock &&
639 + blocksAvailable > 0){
641 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
642 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
643 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
644 + dev->checkpointNextBlock = i + 1;
645 + dev->checkpointCurrentBlock = i;
646 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
651 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
653 + dev->checkpointNextBlock = -1;
654 + dev->checkpointCurrentBlock = -1;
657 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
660 + yaffs_ExtendedTags tags;
662 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
663 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
665 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
666 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
667 + int chunk = i * dev->nChunksPerBlock;
668 + int realignedChunk = chunk - dev->chunkOffset;
670 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
671 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
672 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
674 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
675 + /* Right kind of block */
676 + dev->checkpointNextBlock = tags.objectId;
677 + dev->checkpointCurrentBlock = i;
678 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
679 + dev->blocksInCheckpoint++;
680 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
685 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
687 + dev->checkpointNextBlock = -1;
688 + dev->checkpointCurrentBlock = -1;
692 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
695 + /* Got the functions we need? */
696 + if (!dev->writeChunkWithTagsToNAND ||
697 + !dev->readChunkWithTagsFromNAND ||
698 + !dev->eraseBlockInNAND ||
699 + !dev->markNANDBlockBad)
702 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
705 + if(!dev->checkpointBuffer)
706 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
707 + if(!dev->checkpointBuffer)
711 + dev->checkpointPageSequence = 0;
713 + dev->checkpointOpenForWrite = forWriting;
715 + dev->checkpointByteCount = 0;
716 + dev->checkpointSum = 0;
717 + dev->checkpointXor = 0;
718 + dev->checkpointCurrentBlock = -1;
719 + dev->checkpointCurrentChunk = -1;
720 + dev->checkpointNextBlock = dev->internalStartBlock;
722 + /* Erase all the blocks in the checkpoint area */
724 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
725 + dev->checkpointByteOffset = 0;
726 + return yaffs_CheckpointErase(dev);
731 + /* Set to a value that will kick off a read */
732 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
733 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
734 + * going to be way more than we need */
735 + dev->blocksInCheckpoint = 0;
736 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
737 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
738 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
739 + dev->checkpointBlockList[i] = -1;
745 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
747 + __u32 compositeSum;
748 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
749 + *sum = compositeSum;
753 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
757 + int realignedChunk;
759 + yaffs_ExtendedTags tags;
761 + if(dev->checkpointCurrentBlock < 0){
762 + yaffs_CheckpointFindNextErasedBlock(dev);
763 + dev->checkpointCurrentChunk = 0;
766 + if(dev->checkpointCurrentBlock < 0)
769 + tags.chunkDeleted = 0;
770 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
771 + tags.chunkId = dev->checkpointPageSequence + 1;
772 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
773 + tags.byteCount = dev->nDataBytesPerChunk;
774 + if(dev->checkpointCurrentChunk == 0){
775 + /* First chunk we write for the block? Set block state to
777 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
778 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
779 + dev->blocksInCheckpoint++;
782 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
785 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
786 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
788 + realignedChunk = chunk - dev->chunkOffset;
790 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
791 + dev->checkpointByteOffset = 0;
792 + dev->checkpointPageSequence++;
793 + dev->checkpointCurrentChunk++;
794 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
795 + dev->checkpointCurrentChunk = 0;
796 + dev->checkpointCurrentBlock = -1;
798 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
804 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
810 + __u8 * dataBytes = (__u8 *)data;
814 + if(!dev->checkpointBuffer)
817 + if(!dev->checkpointOpenForWrite)
820 + while(i < nBytes && ok) {
824 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
825 + dev->checkpointSum += *dataBytes;
826 + dev->checkpointXor ^= *dataBytes;
828 + dev->checkpointByteOffset++;
831 + dev->checkpointByteCount++;
834 + if(dev->checkpointByteOffset < 0 ||
835 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
836 + ok = yaffs_CheckpointFlushBuffer(dev);
843 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
847 + yaffs_ExtendedTags tags;
851 + int realignedChunk;
853 + __u8 *dataBytes = (__u8 *)data;
855 + if(!dev->checkpointBuffer)
858 + if(dev->checkpointOpenForWrite)
861 + while(i < nBytes && ok) {
864 + if(dev->checkpointByteOffset < 0 ||
865 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
867 + if(dev->checkpointCurrentBlock < 0){
868 + yaffs_CheckpointFindNextCheckpointBlock(dev);
869 + dev->checkpointCurrentChunk = 0;
872 + if(dev->checkpointCurrentBlock < 0)
876 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
877 + dev->checkpointCurrentChunk;
879 + realignedChunk = chunk - dev->chunkOffset;
881 + /* read in the next chunk */
882 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
883 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
884 + dev->checkpointBuffer,
887 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
888 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
891 + dev->checkpointByteOffset = 0;
892 + dev->checkpointPageSequence++;
893 + dev->checkpointCurrentChunk++;
895 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
896 + dev->checkpointCurrentBlock = -1;
901 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
902 + dev->checkpointSum += *dataBytes;
903 + dev->checkpointXor ^= *dataBytes;
904 + dev->checkpointByteOffset++;
907 + dev->checkpointByteCount++;
914 +int yaffs_CheckpointClose(yaffs_Device *dev)
917 + if(dev->checkpointOpenForWrite){
918 + if(dev->checkpointByteOffset != 0)
919 + yaffs_CheckpointFlushBuffer(dev);
922 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
923 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
924 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
925 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
927 + // Todo this looks odd...
930 + YFREE(dev->checkpointBlockList);
931 + dev->checkpointBlockList = NULL;
934 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
935 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
938 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
939 + dev->checkpointByteCount));
941 + if(dev->checkpointBuffer){
942 + /* free the buffer */
943 + YFREE(dev->checkpointBuffer);
944 + dev->checkpointBuffer = NULL;
952 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
954 + /* Erase the first checksum block */
956 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
958 + if(!yaffs_CheckpointSpaceOk(dev))
961 + return yaffs_CheckpointErase(dev);
966 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.h
967 ===================================================================
968 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
969 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_checkptrw.h 2007-09-21 16:24:08.000000000 +0800
972 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
974 + * Copyright (C) 2002-2007 Aleph One Ltd.
975 + * for Toby Churchill Ltd and Brightstar Engineering
977 + * Created by Charles Manning <charles@aleph1.co.uk>
979 + * This program is free software; you can redistribute it and/or modify
980 + * it under the terms of the GNU Lesser General Public License version 2.1 as
981 + * published by the Free Software Foundation.
983 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
986 +#ifndef __YAFFS_CHECKPTRW_H__
987 +#define __YAFFS_CHECKPTRW_H__
989 +#include "yaffs_guts.h"
991 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
993 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
995 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
997 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
999 +int yaffs_CheckpointClose(yaffs_Device *dev);
1001 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
1006 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.c
1007 ===================================================================
1008 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1009 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.c 2007-09-21 16:24:08.000000000 +0800
1012 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1014 + * Copyright (C) 2002-2007 Aleph One Ltd.
1015 + * for Toby Churchill Ltd and Brightstar Engineering
1017 + * Created by Charles Manning <charles@aleph1.co.uk>
1019 + * This program is free software; you can redistribute it and/or modify
1020 + * it under the terms of the GNU General Public License version 2 as
1021 + * published by the Free Software Foundation.
1025 + * This code implements the ECC algorithm used in SmartMedia.
1027 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1028 + * The two unused bit are set to 1.
1029 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1030 + * blocks are used on a 512-byte NAND page.
1034 +/* Table generated by gen-ecc.c
1035 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1036 + * for each byte of data. These are instead provided in a table in bits7..2.
1037 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1038 + * this bytes influence on the line parity.
1041 +const char *yaffs_ecc_c_version =
1042 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1044 +#include "yportenv.h"
1046 +#include "yaffs_ecc.h"
1048 +static const unsigned char column_parity_table[] = {
1049 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1050 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1051 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1052 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1053 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1054 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1055 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1056 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1057 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1058 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1059 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1060 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1061 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1062 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1063 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1064 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1065 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1066 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1067 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1068 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1069 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1070 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1071 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1072 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1073 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1074 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1075 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1076 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1077 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1078 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1079 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1080 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1083 +/* Count the bits in an unsigned char or a U32 */
1085 +static int yaffs_CountBits(unsigned char x)
1096 +static int yaffs_CountBits32(unsigned x)
1107 +/* Calculate the ECC for a 256-byte block of data */
1108 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1112 + unsigned char col_parity = 0;
1113 + unsigned char line_parity = 0;
1114 + unsigned char line_parity_prime = 0;
1118 + for (i = 0; i < 256; i++) {
1119 + b = column_parity_table[*data++];
1122 + if (b & 0x01) // odd number of bits in the byte
1125 + line_parity_prime ^= ~i;
1130 + ecc[2] = (~col_parity) | 0x03;
1133 + if (line_parity & 0x80)
1135 + if (line_parity_prime & 0x80)
1137 + if (line_parity & 0x40)
1139 + if (line_parity_prime & 0x40)
1141 + if (line_parity & 0x20)
1143 + if (line_parity_prime & 0x20)
1145 + if (line_parity & 0x10)
1147 + if (line_parity_prime & 0x10)
1152 + if (line_parity & 0x08)
1154 + if (line_parity_prime & 0x08)
1156 + if (line_parity & 0x04)
1158 + if (line_parity_prime & 0x04)
1160 + if (line_parity & 0x02)
1162 + if (line_parity_prime & 0x02)
1164 + if (line_parity & 0x01)
1166 + if (line_parity_prime & 0x01)
1170 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1171 + // Swap the bytes into the wrong order
1179 +/* Correct the ECC on a 256 byte block of data */
1181 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1182 + const unsigned char *test_ecc)
1184 + unsigned char d0, d1, d2; /* deltas */
1186 + d0 = read_ecc[0] ^ test_ecc[0];
1187 + d1 = read_ecc[1] ^ test_ecc[1];
1188 + d2 = read_ecc[2] ^ test_ecc[2];
1190 + if ((d0 | d1 | d2) == 0)
1191 + return 0; /* no error */
1193 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1194 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1195 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1196 + /* Single bit (recoverable) error in data */
1201 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1202 + // swap the bytes to correct for the wrong order
1236 + data[byte] ^= (1 << bit);
1238 + return 1; /* Corrected the error */
1241 + if ((yaffs_CountBits(d0) +
1242 + yaffs_CountBits(d1) +
1243 + yaffs_CountBits(d2)) == 1) {
1244 + /* Reccoverable error in ecc */
1246 + read_ecc[0] = test_ecc[0];
1247 + read_ecc[1] = test_ecc[1];
1248 + read_ecc[2] = test_ecc[2];
1250 + return 1; /* Corrected the error */
1253 + /* Unrecoverable error */
1261 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1263 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1264 + yaffs_ECCOther * eccOther)
1268 + unsigned char col_parity = 0;
1269 + unsigned line_parity = 0;
1270 + unsigned line_parity_prime = 0;
1273 + for (i = 0; i < nBytes; i++) {
1274 + b = column_parity_table[*data++];
1278 + /* odd number of bits in the byte */
1280 + line_parity_prime ^= ~i;
1285 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1286 + eccOther->lineParity = line_parity;
1287 + eccOther->lineParityPrime = line_parity_prime;
1290 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1291 + yaffs_ECCOther * read_ecc,
1292 + const yaffs_ECCOther * test_ecc)
1294 + unsigned char cDelta; /* column parity delta */
1295 + unsigned lDelta; /* line parity delta */
1296 + unsigned lDeltaPrime; /* line parity delta */
1299 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1300 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1301 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1303 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1304 + return 0; /* no error */
1306 + if (lDelta == ~lDeltaPrime &&
1307 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1309 + /* Single bit (recoverable) error in data */
1313 + if (cDelta & 0x20)
1315 + if (cDelta & 0x08)
1317 + if (cDelta & 0x02)
1320 + if(lDelta >= nBytes)
1323 + data[lDelta] ^= (1 << bit);
1325 + return 1; /* corrected */
1328 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1329 + yaffs_CountBits(cDelta)) == 1) {
1330 + /* Reccoverable error in ecc */
1332 + *read_ecc = *test_ecc;
1333 + return 1; /* corrected */
1336 + /* Unrecoverable error */
1342 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.h
1343 ===================================================================
1344 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1345 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_ecc.h 2007-09-21 16:24:08.000000000 +0800
1348 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1350 + * Copyright (C) 2002-2007 Aleph One Ltd.
1351 + * for Toby Churchill Ltd and Brightstar Engineering
1353 + * Created by Charles Manning <charles@aleph1.co.uk>
1355 + * This program is free software; you can redistribute it and/or modify
1356 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1357 + * published by the Free Software Foundation.
1359 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1363 + * This code implements the ECC algorithm used in SmartMedia.
1365 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1366 + * The two unused bit are set to 1.
1367 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1368 + * blocks are used on a 512-byte NAND page.
1372 +#ifndef __YAFFS_ECC_H__
1373 +#define __YAFFS_ECC_H__
1376 + unsigned char colParity;
1377 + unsigned lineParity;
1378 + unsigned lineParityPrime;
1381 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1382 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1383 + const unsigned char *test_ecc);
1385 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1386 + yaffs_ECCOther * ecc);
1387 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1388 + yaffs_ECCOther * read_ecc,
1389 + const yaffs_ECCOther * test_ecc);
1391 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_fs.c
1392 ===================================================================
1393 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
1394 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_fs.c 2007-09-21 16:24:08.000000000 +0800
1397 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1399 + * Copyright (C) 2002-2007 Aleph One Ltd.
1400 + * for Toby Churchill Ltd and Brightstar Engineering
1402 + * Created by Charles Manning <charles@aleph1.co.uk>
1403 + * Acknowledgements:
1404 + * Luc van OostenRyck for numerous patches.
1405 + * Nick Bane for numerous patches.
1406 + * Nick Bane for 2.5/2.6 integration.
1407 + * Andras Toth for mknod rdev issue.
1408 + * Michael Fischer for finding the problem with inode inconsistency.
1409 + * Some code bodily lifted from JFFS
1411 + * This program is free software; you can redistribute it and/or modify
1412 + * it under the terms of the GNU General Public License version 2 as
1413 + * published by the Free Software Foundation.
1418 + * This is the file system front-end to YAFFS that hooks it up to
1422 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1424 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1426 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1429 +const char *yaffs_fs_c_version =
1430 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1431 +extern const char *yaffs_guts_c_version;
1433 +#include <linux/version.h>
1434 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1435 +#include <linux/config.h>
1437 +#include <linux/kernel.h>
1438 +#include <linux/module.h>
1439 +#include <linux/slab.h>
1440 +#include <linux/init.h>
1441 +#include <linux/list.h>
1442 +#include <linux/fs.h>
1443 +#include <linux/proc_fs.h>
1444 +#include <linux/smp_lock.h>
1445 +#include <linux/pagemap.h>
1446 +#include <linux/mtd/mtd.h>
1447 +#include <linux/interrupt.h>
1448 +#include <linux/string.h>
1449 +#include <linux/ctype.h>
1451 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1453 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1454 +#include <asm/statfs.h>
1455 +#define UnlockPage(p) unlock_page(p)
1456 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1458 +/* FIXME: use sb->s_id instead ? */
1459 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1463 +#include <linux/locks.h>
1464 +#define BDEVNAME_SIZE 0
1465 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1467 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1468 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1474 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1475 +#define WRITE_SIZE_STR "writesize"
1476 +#define WRITE_SIZE(mtd) (mtd)->writesize
1478 +#define WRITE_SIZE_STR "oobblock"
1479 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1482 +#include <asm/uaccess.h>
1484 +#include "yportenv.h"
1485 +#include "yaffs_guts.h"
1487 +#include <linux/mtd/mtd.h>
1488 +#include "yaffs_mtdif.h"
1489 +#include "yaffs_mtdif1.h"
1490 +#include "yaffs_mtdif2.h"
1492 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1493 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1495 +/* Module Parameters */
1496 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1497 +module_param(yaffs_traceMask,uint,0644);
1498 +module_param(yaffs_wr_attempts,uint,0644);
1500 +MODULE_PARM(yaffs_traceMask,"i");
1501 +MODULE_PARM(yaffs_wr_attempts,"i");
1504 +/*#define T(x) printk x */
1506 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1507 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1509 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1512 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1513 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1515 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1516 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1518 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1521 +static void yaffs_put_super(struct super_block *sb);
1523 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1526 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1527 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1529 +static int yaffs_file_flush(struct file *file);
1532 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1535 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1537 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1538 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1539 + struct nameidata *n);
1540 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1541 + struct nameidata *n);
1543 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1544 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1546 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1547 + struct dentry *dentry);
1548 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1549 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1550 + const char *symname);
1551 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1553 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1554 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1557 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1560 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1561 + struct inode *new_dir, struct dentry *new_dentry);
1562 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1564 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1565 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1566 +static void yaffs_write_super(struct super_block *sb);
1568 +static int yaffs_sync_fs(struct super_block *sb);
1569 +static int yaffs_write_super(struct super_block *sb);
1572 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1573 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1574 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1575 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1577 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1579 +static void yaffs_read_inode(struct inode *inode);
1581 +static void yaffs_put_inode(struct inode *inode);
1582 +static void yaffs_delete_inode(struct inode *);
1583 +static void yaffs_clear_inode(struct inode *);
1585 +static int yaffs_readpage(struct file *file, struct page *page);
1586 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1587 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1589 +static int yaffs_writepage(struct page *page);
1591 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1592 + unsigned offset, unsigned to);
1593 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1596 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1598 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1599 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1601 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1604 +static struct address_space_operations yaffs_file_address_operations = {
1605 + .readpage = yaffs_readpage,
1606 + .writepage = yaffs_writepage,
1607 + .prepare_write = yaffs_prepare_write,
1608 + .commit_write = yaffs_commit_write,
1611 +static struct file_operations yaffs_file_operations = {
1612 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1613 + .read = do_sync_read,
1614 + .write = do_sync_write,
1615 + .aio_read = generic_file_aio_read,
1616 + .aio_write = generic_file_aio_write,
1618 + .read = generic_file_read,
1619 + .write = generic_file_write,
1621 + .mmap = generic_file_mmap,
1622 + .flush = yaffs_file_flush,
1623 + .fsync = yaffs_sync_object,
1624 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1625 + .sendfile = generic_file_sendfile,
1630 +static struct inode_operations yaffs_file_inode_operations = {
1631 + .setattr = yaffs_setattr,
1634 +static struct inode_operations yaffs_symlink_inode_operations = {
1635 + .readlink = yaffs_readlink,
1636 + .follow_link = yaffs_follow_link,
1637 + .setattr = yaffs_setattr,
1640 +static struct inode_operations yaffs_dir_inode_operations = {
1641 + .create = yaffs_create,
1642 + .lookup = yaffs_lookup,
1643 + .link = yaffs_link,
1644 + .unlink = yaffs_unlink,
1645 + .symlink = yaffs_symlink,
1646 + .mkdir = yaffs_mkdir,
1647 + .rmdir = yaffs_unlink,
1648 + .mknod = yaffs_mknod,
1649 + .rename = yaffs_rename,
1650 + .setattr = yaffs_setattr,
1653 +static struct file_operations yaffs_dir_operations = {
1654 + .read = generic_read_dir,
1655 + .readdir = yaffs_readdir,
1656 + .fsync = yaffs_sync_object,
1659 +static struct super_operations yaffs_super_ops = {
1660 + .statfs = yaffs_statfs,
1661 + .read_inode = yaffs_read_inode,
1662 + .put_inode = yaffs_put_inode,
1663 + .put_super = yaffs_put_super,
1664 + .delete_inode = yaffs_delete_inode,
1665 + .clear_inode = yaffs_clear_inode,
1666 + .sync_fs = yaffs_sync_fs,
1667 + .write_super = yaffs_write_super,
1670 +static void yaffs_GrossLock(yaffs_Device * dev)
1672 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1674 + down(&dev->grossLock);
1677 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1679 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1680 + up(&dev->grossLock);
1684 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1687 + unsigned char *alias;
1690 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1692 + yaffs_GrossLock(dev);
1694 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1696 + yaffs_GrossUnlock(dev);
1701 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1706 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1707 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1709 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1712 + unsigned char *alias;
1714 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1716 + yaffs_GrossLock(dev);
1718 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1720 + yaffs_GrossUnlock(dev);
1728 + ret = vfs_follow_link(nd, alias);
1731 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1732 + return ERR_PTR (ret);
1738 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1739 + yaffs_Object * obj);
1742 + * Lookup is used to find objects in the fs
1744 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1746 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1747 + struct nameidata *n)
1749 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1752 + yaffs_Object *obj;
1753 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1755 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1757 + yaffs_GrossLock(dev);
1760 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1761 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1764 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1765 + dentry->d_name.name);
1767 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1769 + /* Can't hold gross lock when calling yaffs_get_inode() */
1770 + yaffs_GrossUnlock(dev);
1774 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1776 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1780 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1781 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1782 + * d_add even if NULL inode */
1784 + /*dget(dentry); // try to solve directory bug */
1785 + d_add(dentry, inode);
1787 + /* return dentry; */
1793 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1797 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1798 + * NULL which creates dentry hash */
1799 + d_add(dentry, inode);
1802 + /* return (ERR_PTR(-EIO)); */
1806 +/* For now put inode is just for debugging
1807 + * Put inode is called when the inode **structure** is put.
1809 +static void yaffs_put_inode(struct inode *inode)
1812 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1813 + atomic_read(&inode->i_count)));
1817 +/* clear is called to tell the fs to release any per-inode data it holds */
1818 +static void yaffs_clear_inode(struct inode *inode)
1820 + yaffs_Object *obj;
1821 + yaffs_Device *dev;
1823 + obj = yaffs_InodeToObject(inode);
1826 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1827 + atomic_read(&inode->i_count),
1828 + obj ? "object exists" : "null object"));
1832 + yaffs_GrossLock(dev);
1834 + /* Clear the association between the inode and
1835 + * the yaffs_Object.
1837 + obj->myInode = NULL;
1838 + yaffs_InodeToObjectLV(inode) = NULL;
1840 + /* If the object freeing was deferred, then the real
1841 + * free happens now.
1842 + * This should fix the inode inconsistency problem.
1845 + yaffs_HandleDeferedFree(obj);
1847 + yaffs_GrossUnlock(dev);
1852 +/* delete is called when the link count is zero and the inode
1853 + * is put (ie. nobody wants to know about it anymore, time to
1854 + * delete the file).
1855 + * NB Must call clear_inode()
1857 +static void yaffs_delete_inode(struct inode *inode)
1859 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1860 + yaffs_Device *dev;
1863 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1864 + atomic_read(&inode->i_count),
1865 + obj ? "object exists" : "null object"));
1869 + yaffs_GrossLock(dev);
1870 + yaffs_DeleteFile(obj);
1871 + yaffs_GrossUnlock(dev);
1873 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1874 + truncate_inode_pages (&inode->i_data, 0);
1876 + clear_inode(inode);
1879 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1880 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1882 +static int yaffs_file_flush(struct file *file)
1885 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1887 + yaffs_Device *dev = obj->myDev;
1890 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1891 + obj->dirty ? "dirty" : "clean"));
1893 + yaffs_GrossLock(dev);
1895 + yaffs_FlushFile(obj, 1);
1897 + yaffs_GrossUnlock(dev);
1902 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1904 + /* Lifted from jffs2 */
1906 + yaffs_Object *obj;
1907 + unsigned char *pg_buf;
1910 + yaffs_Device *dev;
1912 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1913 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1914 + (unsigned)PAGE_CACHE_SIZE));
1916 + obj = yaffs_DentryToObject(f->f_dentry);
1920 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1921 + BUG_ON(!PageLocked(pg));
1923 + if (!PageLocked(pg))
1927 + pg_buf = kmap(pg);
1928 + /* FIXME: Can kmap fail? */
1930 + yaffs_GrossLock(dev);
1933 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1936 + yaffs_GrossUnlock(dev);
1942 + ClearPageUptodate(pg);
1945 + SetPageUptodate(pg);
1946 + ClearPageError(pg);
1949 + flush_dcache_page(pg);
1952 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1956 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1958 + int ret = yaffs_readpage_nolock(f, pg);
1963 +static int yaffs_readpage(struct file *f, struct page *pg)
1965 + return yaffs_readpage_unlock(f, pg);
1968 +/* writepage inspired by/stolen from smbfs */
1970 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1971 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1973 +static int yaffs_writepage(struct page *page)
1976 + struct address_space *mapping = page->mapping;
1977 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1978 + struct inode *inode;
1979 + unsigned long end_index;
1981 + yaffs_Object *obj;
1987 + inode = mapping->host;
1991 + if (offset > inode->i_size) {
1994 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1995 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1996 + (unsigned)inode->i_size));
1998 + (KERN_DEBUG " -> don't care!!\n"));
1999 + unlock_page(page);
2003 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
2006 + if (page->index < end_index) {
2007 + nBytes = PAGE_CACHE_SIZE;
2009 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
2014 + buffer = kmap(page);
2016 + obj = yaffs_InodeToObject(inode);
2017 + yaffs_GrossLock(obj->myDev);
2020 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
2021 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
2023 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
2024 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2027 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2031 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2032 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2034 + yaffs_GrossUnlock(obj->myDev);
2037 + SetPageUptodate(page);
2041 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2044 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2045 + unsigned offset, unsigned to)
2048 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2049 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2050 + return yaffs_readpage_nolock(f, pg);
2056 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2060 + void *addr = page_address(pg) + offset;
2061 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2062 + int nBytes = to - offset;
2065 + unsigned spos = pos;
2066 + unsigned saddr = (unsigned)addr;
2069 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2072 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2074 + if (nWritten != nBytes) {
2077 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2078 + nWritten, nBytes));
2080 + ClearPageUptodate(pg);
2082 + SetPageUptodate(pg);
2086 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2087 + nWritten == nBytes ? 0 : nWritten));
2089 + return nWritten == nBytes ? 0 : nWritten;
2093 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2095 + if (inode && obj) {
2098 + /* Check mode against the variant type and attempt to repair if broken. */
2099 + __u32 mode = obj->yst_mode;
2100 + switch( obj->variantType ){
2101 + case YAFFS_OBJECT_TYPE_FILE :
2102 + if( ! S_ISREG(mode) ){
2103 + obj->yst_mode &= ~S_IFMT;
2104 + obj->yst_mode |= S_IFREG;
2108 + case YAFFS_OBJECT_TYPE_SYMLINK :
2109 + if( ! S_ISLNK(mode) ){
2110 + obj->yst_mode &= ~S_IFMT;
2111 + obj->yst_mode |= S_IFLNK;
2115 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2116 + if( ! S_ISDIR(mode) ){
2117 + obj->yst_mode &= ~S_IFMT;
2118 + obj->yst_mode |= S_IFDIR;
2122 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2123 + case YAFFS_OBJECT_TYPE_HARDLINK :
2124 + case YAFFS_OBJECT_TYPE_SPECIAL :
2130 + inode->i_ino = obj->objectId;
2131 + inode->i_mode = obj->yst_mode;
2132 + inode->i_uid = obj->yst_uid;
2133 + inode->i_gid = obj->yst_gid;
2134 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2135 + inode->i_blksize = inode->i_sb->s_blocksize;
2137 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2139 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2140 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2141 + inode->i_atime.tv_nsec = 0;
2142 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2143 + inode->i_mtime.tv_nsec = 0;
2144 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2145 + inode->i_ctime.tv_nsec = 0;
2147 + inode->i_rdev = obj->yst_rdev;
2148 + inode->i_atime = obj->yst_atime;
2149 + inode->i_mtime = obj->yst_mtime;
2150 + inode->i_ctime = obj->yst_ctime;
2152 + inode->i_size = yaffs_GetObjectFileLength(obj);
2153 + inode->i_blocks = (inode->i_size + 511) >> 9;
2155 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2159 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2160 + inode->i_mode, inode->i_uid, inode->i_gid,
2161 + (int)inode->i_size, atomic_read(&inode->i_count)));
2163 + switch (obj->yst_mode & S_IFMT) {
2164 + default: /* fifo, device or socket */
2165 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2166 + init_special_inode(inode, obj->yst_mode,
2167 + old_decode_dev(obj->yst_rdev));
2169 + init_special_inode(inode, obj->yst_mode,
2170 + (dev_t) (obj->yst_rdev));
2173 + case S_IFREG: /* file */
2174 + inode->i_op = &yaffs_file_inode_operations;
2175 + inode->i_fop = &yaffs_file_operations;
2176 + inode->i_mapping->a_ops =
2177 + &yaffs_file_address_operations;
2179 + case S_IFDIR: /* directory */
2180 + inode->i_op = &yaffs_dir_inode_operations;
2181 + inode->i_fop = &yaffs_dir_operations;
2183 + case S_IFLNK: /* symlink */
2184 + inode->i_op = &yaffs_symlink_inode_operations;
2188 + yaffs_InodeToObjectLV(inode) = obj;
2190 + obj->myInode = inode;
2194 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2199 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2200 + yaffs_Object * obj)
2202 + struct inode *inode;
2206 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2213 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2219 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2221 + inode = iget(sb, obj->objectId);
2223 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2224 + /* iget also increments the inode's i_count */
2225 + /* NB You can't be holding grossLock or deadlock will happen! */
2230 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2233 + yaffs_Object *obj;
2234 + int nWritten, ipos;
2235 + struct inode *inode;
2236 + yaffs_Device *dev;
2238 + obj = yaffs_DentryToObject(f->f_dentry);
2242 + yaffs_GrossLock(dev);
2244 + inode = f->f_dentry->d_inode;
2246 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2247 + ipos = inode->i_size;
2254 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2258 + "yaffs_file_write about to write writing %d bytes"
2259 + "to object %d at %d\n",
2260 + n, obj->objectId, ipos));
2263 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2266 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2267 + n, nWritten, ipos));
2268 + if (nWritten > 0) {
2271 + if (ipos > inode->i_size) {
2272 + inode->i_size = ipos;
2273 + inode->i_blocks = (ipos + 511) >> 9;
2277 + "yaffs_file_write size updated to %d bytes, "
2279 + ipos, (int)(inode->i_blocks)));
2283 + yaffs_GrossUnlock(dev);
2284 + return nWritten == 0 ? -ENOSPC : nWritten;
2287 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2289 + yaffs_Object *obj;
2290 + yaffs_Device *dev;
2291 + struct inode *inode = f->f_dentry->d_inode;
2292 + unsigned long offset, curoffs;
2293 + struct list_head *i;
2296 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2298 + obj = yaffs_DentryToObject(f->f_dentry);
2301 + yaffs_GrossLock(dev);
2303 + offset = f->f_pos;
2305 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2307 + if (offset == 0) {
2309 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2310 + (int)inode->i_ino));
2311 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2318 + if (offset == 1) {
2320 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2321 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2323 + (dirent, "..", 2, offset,
2324 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2333 + /* If the directory has changed since the open or last call to
2334 + readdir, rewind to after the 2 canned entries. */
2336 + if (f->f_version != inode->i_version) {
2338 + f->f_pos = offset;
2339 + f->f_version = inode->i_version;
2342 + list_for_each(i, &obj->variant.directoryVariant.children) {
2344 + if (curoffs >= offset) {
2345 + l = list_entry(i, yaffs_Object, siblings);
2347 + yaffs_GetObjectName(l, name,
2348 + YAFFS_MAX_NAME_LENGTH + 1);
2350 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2351 + yaffs_GetObjectInode(l)));
2353 + if (filldir(dirent,
2357 + yaffs_GetObjectInode(l),
2358 + yaffs_GetObjectType(l))
2371 + yaffs_GrossUnlock(dev);
2377 + * File creation. Allocate an inode, and we're done..
2379 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2380 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2383 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2387 + struct inode *inode;
2389 + yaffs_Object *obj = NULL;
2390 + yaffs_Device *dev;
2392 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2394 + int error = -ENOSPC;
2395 + uid_t uid = current->fsuid;
2396 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2398 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2403 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2404 + parent->objectId, parent->variantType));
2407 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2411 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2412 + "mode %x dev %x\n",
2413 + dentry->d_name.name, mode, rdev));
2415 + dev = parent->myDev;
2417 + yaffs_GrossLock(dev);
2419 + switch (mode & S_IFMT) {
2421 + /* Special (socket, fifo, device...) */
2422 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2423 + "yaffs_mknod: making special\n"));
2424 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2426 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2427 + gid, old_encode_dev(rdev));
2430 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2434 + case S_IFREG: /* file */
2435 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2437 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2440 + case S_IFDIR: /* directory */
2442 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2444 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2447 + case S_IFLNK: /* symlink */
2448 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2449 + obj = NULL; /* Do we ever get here? */
2453 + /* Can not call yaffs_get_inode() with gross lock held */
2454 + yaffs_GrossUnlock(dev);
2457 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2458 + d_instantiate(dentry, inode);
2460 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2461 + obj->objectId, atomic_read(&inode->i_count)));
2465 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2472 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2475 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2476 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2478 + /* attempt to fix dir bug - didn't work */
2486 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2487 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2488 + struct nameidata *n)
2490 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2493 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2494 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2497 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2501 + yaffs_Device *dev;
2504 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2505 + dentry->d_name.name));
2507 + dev = yaffs_InodeToObject(dir)->myDev;
2509 + yaffs_GrossLock(dev);
2511 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2513 + if (retVal == YAFFS_OK) {
2514 + dentry->d_inode->i_nlink--;
2516 + yaffs_GrossUnlock(dev);
2517 + mark_inode_dirty(dentry->d_inode);
2520 + yaffs_GrossUnlock(dev);
2521 + return -ENOTEMPTY;
2525 + * Create a link...
2527 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2528 + struct dentry *dentry)
2530 + struct inode *inode = old_dentry->d_inode;
2531 + yaffs_Object *obj = NULL;
2532 + yaffs_Object *link = NULL;
2533 + yaffs_Device *dev;
2535 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2537 + obj = yaffs_InodeToObject(inode);
2540 + yaffs_GrossLock(dev);
2542 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2545 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2550 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2551 + d_instantiate(dentry, old_dentry->d_inode);
2552 + atomic_inc(&old_dentry->d_inode->i_count);
2554 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2555 + old_dentry->d_inode->i_nlink,
2556 + atomic_read(&old_dentry->d_inode->i_count)));
2560 + yaffs_GrossUnlock(dev);
2570 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2571 + const char *symname)
2573 + yaffs_Object *obj;
2574 + yaffs_Device *dev;
2575 + uid_t uid = current->fsuid;
2576 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2578 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2580 + dev = yaffs_InodeToObject(dir)->myDev;
2581 + yaffs_GrossLock(dev);
2582 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2583 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2584 + yaffs_GrossUnlock(dev);
2588 + struct inode *inode;
2590 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2591 + d_instantiate(dentry, inode);
2592 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2595 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2602 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2606 + yaffs_Object *obj;
2607 + yaffs_Device *dev;
2609 + obj = yaffs_DentryToObject(dentry);
2613 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2614 + yaffs_GrossLock(dev);
2615 + yaffs_FlushFile(obj, 1);
2616 + yaffs_GrossUnlock(dev);
2621 + * The VFS layer already does all the dentry stuff for rename.
2623 + * NB: POSIX says you can rename an object over an old object of the same name
2625 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2626 + struct inode *new_dir, struct dentry *new_dentry)
2628 + yaffs_Device *dev;
2629 + int retVal = YAFFS_FAIL;
2630 + yaffs_Object *target;
2632 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2633 + dev = yaffs_InodeToObject(old_dir)->myDev;
2635 + yaffs_GrossLock(dev);
2637 + /* Check if the target is an existing directory that is not empty. */
2639 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2640 + new_dentry->d_name.name);
2645 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2646 + !list_empty(&target->variant.directoryVariant.children)) {
2648 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2650 + retVal = YAFFS_FAIL;
2653 + /* Now does unlinking internally using shadowing mechanism */
2654 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2657 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2658 + old_dentry->d_name.name,
2659 + yaffs_InodeToObject(new_dir),
2660 + new_dentry->d_name.name);
2663 + yaffs_GrossUnlock(dev);
2665 + if (retVal == YAFFS_OK) {
2667 + new_dentry->d_inode->i_nlink--;
2668 + mark_inode_dirty(new_dentry->d_inode);
2673 + return -ENOTEMPTY;
2678 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2680 + struct inode *inode = dentry->d_inode;
2682 + yaffs_Device *dev;
2685 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2686 + yaffs_InodeToObject(inode)->objectId));
2688 + if ((error = inode_change_ok(inode, attr)) == 0) {
2690 + dev = yaffs_InodeToObject(inode)->myDev;
2691 + yaffs_GrossLock(dev);
2692 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2698 + yaffs_GrossUnlock(dev);
2700 + error = inode_setattr(inode, attr);
2705 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2706 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2708 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2709 + struct super_block *sb = dentry->d_sb;
2710 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2711 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2713 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2715 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2717 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2720 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2722 + yaffs_GrossLock(dev);
2724 + buf->f_type = YAFFS_MAGIC;
2725 + buf->f_bsize = sb->s_blocksize;
2726 + buf->f_namelen = 255;
2727 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2730 + (dev->endBlock - dev->startBlock +
2731 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2732 + dev->nDataBytesPerChunk);
2734 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2735 + dev->nDataBytesPerChunk);
2739 + (dev->endBlock - dev->startBlock +
2740 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2743 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2748 + buf->f_bavail = buf->f_bfree;
2750 + yaffs_GrossUnlock(dev);
2756 +static int yaffs_do_sync_fs(struct super_block *sb)
2759 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2760 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2763 + yaffs_GrossLock(dev);
2766 + yaffs_CheckpointSave(dev);
2768 + yaffs_GrossUnlock(dev);
2776 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2777 +static void yaffs_write_super(struct super_block *sb)
2779 +static int yaffs_write_super(struct super_block *sb)
2783 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2784 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2785 + return 0; /* yaffs_do_sync_fs(sb);*/
2790 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2791 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2793 +static int yaffs_sync_fs(struct super_block *sb)
2797 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2799 + return 0; /* yaffs_do_sync_fs(sb);*/
2804 +static void yaffs_read_inode(struct inode *inode)
2806 + /* NB This is called as a side effect of other functions, but
2807 + * we had to release the lock to prevent deadlocks, so
2808 + * need to lock again.
2811 + yaffs_Object *obj;
2812 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2815 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2817 + yaffs_GrossLock(dev);
2819 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2821 + yaffs_FillInodeFromObject(inode, obj);
2823 + yaffs_GrossUnlock(dev);
2826 +static LIST_HEAD(yaffs_dev_list);
2828 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2830 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2832 + if( *flags & MS_RDONLY ) {
2833 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2836 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2838 + yaffs_GrossLock(dev);
2840 + yaffs_FlushEntireDeviceCache(dev);
2842 + yaffs_CheckpointSave(dev);
2847 + yaffs_GrossUnlock(dev);
2851 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2857 +static void yaffs_put_super(struct super_block *sb)
2859 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2861 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2863 + yaffs_GrossLock(dev);
2865 + yaffs_FlushEntireDeviceCache(dev);
2867 + yaffs_CheckpointSave(dev);
2869 + if (dev->putSuperFunc) {
2870 + dev->putSuperFunc(sb);
2873 + yaffs_Deinitialise(dev);
2875 + yaffs_GrossUnlock(dev);
2877 + /* we assume this is protected by lock_kernel() in mount/umount */
2878 + list_del(&dev->devList);
2880 + if(dev->spareBuffer){
2881 + YFREE(dev->spareBuffer);
2882 + dev->spareBuffer = NULL;
2889 +static void yaffs_MTDPutSuper(struct super_block *sb)
2892 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2898 + put_mtd_device(mtd);
2902 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2904 + struct super_block *sb = (struct super_block *)vsb;
2906 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2913 + int skip_checkpoint_read;
2914 + int skip_checkpoint_write;
2918 +#define MAX_OPT_LEN 20
2919 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2921 + char cur_opt[MAX_OPT_LEN+1];
2925 + /* Parse through the options which is a comma seperated list */
2927 + while(options_str && *options_str && !error){
2928 + memset(cur_opt,0,MAX_OPT_LEN+1);
2931 + while(*options_str && *options_str != ','){
2932 + if(p < MAX_OPT_LEN){
2933 + cur_opt[p] = *options_str;
2939 + if(!strcmp(cur_opt,"inband-tags"))
2940 + options->inband_tags = 1;
2941 + else if(!strcmp(cur_opt,"no-cache"))
2942 + options->no_cache = 1;
2943 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2944 + options->skip_checkpoint_read = 1;
2945 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2946 + options->skip_checkpoint_write = 1;
2947 + else if(!strcmp(cur_opt,"no-checkpoint")){
2948 + options->skip_checkpoint_read = 1;
2949 + options->skip_checkpoint_write = 1;
2951 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2960 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2961 + struct super_block *sb,
2962 + void *data, int silent)
2965 + struct inode *inode = NULL;
2966 + struct dentry *root;
2967 + yaffs_Device *dev = 0;
2968 + char devname_buf[BDEVNAME_SIZE + 1];
2969 + struct mtd_info *mtd;
2971 + char *data_str = (char *)data;
2973 + yaffs_options options;
2975 + sb->s_magic = YAFFS_MAGIC;
2976 + sb->s_op = &yaffs_super_ops;
2979 + printk(KERN_INFO "yaffs: sb is NULL\n");
2980 + else if (!sb->s_dev)
2981 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2982 + else if (!yaffs_devname(sb, devname_buf))
2983 + printk(KERN_INFO "yaffs: devname is NULL\n");
2985 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2987 + yaffs_devname(sb, devname_buf));
2992 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2994 + memset(&options,0,sizeof(options));
2996 + if(yaffs_parse_options(&options,data_str)){
2997 + /* Option parsing failed */
3002 + sb->s_blocksize = PAGE_CACHE_SIZE;
3003 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
3004 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
3006 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
3008 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
3010 + ("yaffs: Write verification disabled. All guarantees "
3011 + "null and void\n"));
3014 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
3016 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
3017 + yaffs_devname(sb, devname_buf)));
3019 + /* Check it's an mtd device..... */
3020 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
3021 + return NULL; /* This isn't an mtd device */
3023 + /* Get the device */
3024 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
3026 + T(YAFFS_TRACE_ALWAYS,
3027 + ("yaffs: MTD device #%u doesn't appear to exist\n",
3028 + MINOR(sb->s_dev)));
3031 + /* Check it's NAND */
3032 + if (mtd->type != MTD_NANDFLASH) {
3033 + T(YAFFS_TRACE_ALWAYS,
3034 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3038 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3039 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3040 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3041 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3042 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3043 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3044 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3045 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3046 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3047 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3048 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3050 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3052 + if (yaffsVersion == 1 &&
3053 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3054 + mtd->writesize >= 2048) {
3056 + mtd->oobblock >= 2048) {
3058 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3062 + /* Added NCB 26/5/2006 for completeness */
3063 + if (yaffsVersion == 2 &&
3064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3065 + mtd->writesize == 512) {
3067 + mtd->oobblock == 512) {
3069 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3075 + if (yaffsVersion == 2) {
3076 + /* Check for version 2 style functions */
3077 + if (!mtd->erase ||
3078 + !mtd->block_isbad ||
3079 + !mtd->block_markbad ||
3082 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3083 + !mtd->read_oob || !mtd->write_oob) {
3085 + !mtd->write_ecc ||
3086 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3088 + T(YAFFS_TRACE_ALWAYS,
3089 + ("yaffs: MTD device does not support required "
3094 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3095 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3097 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3099 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3100 + T(YAFFS_TRACE_ALWAYS,
3101 + ("yaffs: MTD device does not have the "
3102 + "right page sizes\n"));
3106 + /* Check for V1 style functions */
3107 + if (!mtd->erase ||
3110 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3111 + !mtd->read_oob || !mtd->write_oob) {
3113 + !mtd->write_ecc ||
3114 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3116 + T(YAFFS_TRACE_ALWAYS,
3117 + ("yaffs: MTD device does not support required "
3122 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3123 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3124 + T(YAFFS_TRACE_ALWAYS,
3125 + ("yaffs: MTD device does not support have the "
3126 + "right page sizes\n"));
3131 + /* OK, so if we got here, we have an MTD that's NAND and looks
3132 + * like it has the right capabilities
3133 + * Set the yaffs_Device up for mtd
3136 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3137 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3139 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3142 + /* Deep shit could not allocate device structure */
3143 + T(YAFFS_TRACE_ALWAYS,
3144 + ("yaffs_read_super: Failed trying to allocate "
3145 + "yaffs_Device. \n"));
3149 + memset(dev, 0, sizeof(yaffs_Device));
3150 + dev->genericDevice = mtd;
3151 + dev->name = mtd->name;
3153 + /* Set up the memory size parameters.... */
3155 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3156 + dev->startBlock = 0;
3157 + dev->endBlock = nBlocks - 1;
3158 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3159 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3160 + dev->nReservedBlocks = 5;
3161 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3163 + /* ... and the functions. */
3164 + if (yaffsVersion == 2) {
3165 + dev->writeChunkWithTagsToNAND =
3166 + nandmtd2_WriteChunkWithTagsToNAND;
3167 + dev->readChunkWithTagsFromNAND =
3168 + nandmtd2_ReadChunkWithTagsFromNAND;
3169 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3170 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3171 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3172 + dev->isYaffs2 = 1;
3173 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3174 + dev->nDataBytesPerChunk = mtd->writesize;
3175 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3177 + dev->nDataBytesPerChunk = mtd->oobblock;
3178 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3180 + nBlocks = mtd->size / mtd->erasesize;
3182 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3183 + dev->startBlock = 0;
3184 + dev->endBlock = nBlocks - 1;
3186 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3187 + /* use the MTD interface in yaffs_mtdif1.c */
3188 + dev->writeChunkWithTagsToNAND =
3189 + nandmtd1_WriteChunkWithTagsToNAND;
3190 + dev->readChunkWithTagsFromNAND =
3191 + nandmtd1_ReadChunkWithTagsFromNAND;
3192 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3193 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3195 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3196 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3198 + dev->isYaffs2 = 0;
3200 + /* ... and common functions */
3201 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3202 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3204 + dev->putSuperFunc = yaffs_MTDPutSuper;
3206 + dev->superBlock = (void *)sb;
3207 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3210 +#ifndef CONFIG_YAFFS_DOES_ECC
3211 + dev->useNANDECC = 1;
3214 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3215 + dev->wideTnodesDisabled = 1;
3218 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3219 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3221 + /* we assume this is protected by lock_kernel() in mount/umount */
3222 + list_add_tail(&dev->devList, &yaffs_dev_list);
3224 + init_MUTEX(&dev->grossLock);
3226 + yaffs_GrossLock(dev);
3228 + err = yaffs_GutsInitialise(dev);
3231 + ("yaffs_read_super: guts initialised %s\n",
3232 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3234 + /* Release lock before yaffs_get_inode() */
3235 + yaffs_GrossUnlock(dev);
3237 + /* Create root inode */
3238 + if (err == YAFFS_OK)
3239 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3245 + inode->i_op = &yaffs_dir_inode_operations;
3246 + inode->i_fop = &yaffs_dir_operations;
3248 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3250 + root = d_alloc_root(inode);
3252 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3258 + sb->s_root = root;
3260 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3265 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3266 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3269 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3272 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3273 +static int yaffs_read_super(struct file_system_type *fs,
3274 + int flags, const char *dev_name,
3275 + void *data, struct vfsmount *mnt)
3278 + return get_sb_bdev(fs, flags, dev_name, data,
3279 + yaffs_internal_read_super_mtd, mnt);
3282 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3283 + int flags, const char *dev_name,
3287 + return get_sb_bdev(fs, flags, dev_name, data,
3288 + yaffs_internal_read_super_mtd);
3292 +static struct file_system_type yaffs_fs_type = {
3293 + .owner = THIS_MODULE,
3295 + .get_sb = yaffs_read_super,
3296 + .kill_sb = kill_block_super,
3297 + .fs_flags = FS_REQUIRES_DEV,
3300 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3303 + return yaffs_internal_read_super(1, sb, data, silent);
3306 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3311 +#ifdef CONFIG_YAFFS_YAFFS2
3313 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3314 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3317 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3320 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3321 +static int yaffs2_read_super(struct file_system_type *fs,
3322 + int flags, const char *dev_name, void *data,
3323 + struct vfsmount *mnt)
3325 + return get_sb_bdev(fs, flags, dev_name, data,
3326 + yaffs2_internal_read_super_mtd, mnt);
3329 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3330 + int flags, const char *dev_name,
3334 + return get_sb_bdev(fs, flags, dev_name, data,
3335 + yaffs2_internal_read_super_mtd);
3339 +static struct file_system_type yaffs2_fs_type = {
3340 + .owner = THIS_MODULE,
3342 + .get_sb = yaffs2_read_super,
3343 + .kill_sb = kill_block_super,
3344 + .fs_flags = FS_REQUIRES_DEV,
3347 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3348 + void *data, int silent)
3350 + return yaffs_internal_read_super(2, sb, data, silent);
3353 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3357 +#endif /* CONFIG_YAFFS_YAFFS2 */
3359 +static struct proc_dir_entry *my_proc_entry;
3361 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3363 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3364 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3365 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3366 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3367 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3368 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3369 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3370 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3371 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3372 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3373 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3374 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3375 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3376 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3377 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3378 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3379 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3380 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3382 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3384 + sprintf(buf, "passiveGCs......... %d\n",
3385 + dev->passiveGarbageCollections);
3386 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3387 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3388 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3389 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3390 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3391 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3392 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3393 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3394 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3395 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3397 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3398 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3399 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3404 +static int yaffs_proc_read(char *page,
3406 + off_t offset, int count, int *eof, void *data)
3408 + struct list_head *item;
3410 + int step = offset;
3413 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3414 + * We use 'offset' (*ppos) to indicate where we are in devList.
3415 + * This also assumes the user has posted a read buffer large
3416 + * enough to hold the complete output; but that's life in /proc.
3419 + *(int *)start = 1;
3421 + /* Print header first */
3423 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3424 + "\n%s\n%s\n", yaffs_fs_c_version,
3425 + yaffs_guts_c_version);
3428 + /* hold lock_kernel while traversing yaffs_dev_list */
3431 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3432 + list_for_each(item, &yaffs_dev_list) {
3433 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3438 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3439 + buf = yaffs_dump_dev(buf, dev);
3444 + return buf - page < count ? buf - page : count;
3448 + * Set the verbosity of the warnings and error messages.
3450 + * Note that the names can only be a..z or _ with the current code.
3455 + unsigned mask_bitfield;
3457 + {"allocate", YAFFS_TRACE_ALLOCATE},
3458 + {"always", YAFFS_TRACE_ALWAYS},
3459 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3460 + {"buffers", YAFFS_TRACE_BUFFERS},
3461 + {"bug", YAFFS_TRACE_BUG},
3462 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3463 + {"deletion", YAFFS_TRACE_DELETION},
3464 + {"erase", YAFFS_TRACE_ERASE},
3465 + {"error", YAFFS_TRACE_ERROR},
3466 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3467 + {"gc", YAFFS_TRACE_GC},
3468 + {"mtd", YAFFS_TRACE_MTD},
3469 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3470 + {"os", YAFFS_TRACE_OS},
3471 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3472 + {"scan", YAFFS_TRACE_SCAN},
3473 + {"tracing", YAFFS_TRACE_TRACING},
3475 + {"verify", YAFFS_TRACE_VERIFY},
3476 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3477 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3478 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3480 + {"write", YAFFS_TRACE_WRITE},
3481 + {"all", 0xffffffff},
3486 +#define MAX_MASK_NAME_LENGTH 40
3487 +static int yaffs_proc_write(struct file *file, const char *buf,
3488 + unsigned long count, void *data)
3490 + unsigned rg = 0, mask_bitfield;
3494 + char substring[MAX_MASK_NAME_LENGTH+1];
3500 + rg = yaffs_traceMask;
3502 + while (!done && (pos < count)) {
3504 + while ((pos < count) && isspace(buf[pos])) {
3508 + switch (buf[pos]) {
3522 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3523 + if (end > buf + pos) {
3524 + mask_name = "numeral";
3525 + len = end - (buf + pos);
3528 + for(x = buf + pos, i = 0;
3529 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3530 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3531 + substring[i] = *x;
3532 + substring[i] = '\0';
3534 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3535 + //len = strlen(mask_flags[i].mask_name);
3536 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3537 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3538 + mask_name = mask_flags[i].mask_name;
3539 + mask_bitfield = mask_flags[i].mask_bitfield;
3546 + if (mask_name != NULL) {
3551 + rg &= ~mask_bitfield;
3554 + rg |= mask_bitfield;
3557 + rg = mask_bitfield;
3560 + rg |= mask_bitfield;
3566 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3568 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3570 + if (rg & YAFFS_TRACE_ALWAYS) {
3571 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3573 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3574 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3581 +/* Stuff to handle installation of file systems */
3582 +struct file_system_to_install {
3583 + struct file_system_type *fst;
3587 +static struct file_system_to_install fs_to_install[] = {
3588 +//#ifdef CONFIG_YAFFS_YAFFS1
3589 + {&yaffs_fs_type, 0},
3591 +//#ifdef CONFIG_YAFFS_YAFFS2
3592 + {&yaffs2_fs_type, 0},
3597 +static int __init init_yaffs_fs(void)
3600 + struct file_system_to_install *fsinst;
3602 + T(YAFFS_TRACE_ALWAYS,
3603 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3605 + /* Install the proc_fs entry */
3606 + my_proc_entry = create_proc_entry("yaffs",
3607 + S_IRUGO | S_IFREG,
3610 + if (my_proc_entry) {
3611 + my_proc_entry->write_proc = yaffs_proc_write;
3612 + my_proc_entry->read_proc = yaffs_proc_read;
3613 + my_proc_entry->data = NULL;
3618 + /* Now add the file system entries */
3620 + fsinst = fs_to_install;
3622 + while (fsinst->fst && !error) {
3623 + error = register_filesystem(fsinst->fst);
3625 + fsinst->installed = 1;
3630 + /* Any errors? uninstall */
3632 + fsinst = fs_to_install;
3634 + while (fsinst->fst) {
3635 + if (fsinst->installed) {
3636 + unregister_filesystem(fsinst->fst);
3637 + fsinst->installed = 0;
3646 +static void __exit exit_yaffs_fs(void)
3649 + struct file_system_to_install *fsinst;
3651 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3652 + " removing. \n"));
3654 + remove_proc_entry("yaffs", &proc_root);
3656 + fsinst = fs_to_install;
3658 + while (fsinst->fst) {
3659 + if (fsinst->installed) {
3660 + unregister_filesystem(fsinst->fst);
3661 + fsinst->installed = 0;
3668 +module_init(init_yaffs_fs)
3669 +module_exit(exit_yaffs_fs)
3671 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3672 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3673 +MODULE_LICENSE("GPL");
3674 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.c
3675 ===================================================================
3676 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
3677 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.c 2007-09-21 16:24:08.000000000 +0800
3680 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3682 + * Copyright (C) 2002-2007 Aleph One Ltd.
3683 + * for Toby Churchill Ltd and Brightstar Engineering
3685 + * Created by Charles Manning <charles@aleph1.co.uk>
3687 + * This program is free software; you can redistribute it and/or modify
3688 + * it under the terms of the GNU General Public License version 2 as
3689 + * published by the Free Software Foundation.
3692 +const char *yaffs_guts_c_version =
3693 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3695 +#include "yportenv.h"
3697 +#include "yaffsinterface.h"
3698 +#include "yaffs_guts.h"
3699 +#include "yaffs_tagsvalidity.h"
3701 +#include "yaffs_tagscompat.h"
3702 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3703 +#include "yaffs_qsort.h"
3705 +#include "yaffs_nand.h"
3707 +#include "yaffs_checkptrw.h"
3709 +#include "yaffs_nand.h"
3710 +#include "yaffs_packedtags2.h"
3713 +#ifdef CONFIG_YAFFS_WINCE
3714 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3715 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3718 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3720 +#include "yaffs_ecc.h"
3723 +/* Robustification (if it ever comes about...) */
3724 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3725 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3726 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3727 + const __u8 * data,
3728 + const yaffs_ExtendedTags * tags);
3729 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3730 + const yaffs_ExtendedTags * tags);
3732 +/* Other local prototypes */
3733 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3734 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3736 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3738 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3739 + const __u8 * buffer,
3740 + yaffs_ExtendedTags * tags,
3742 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3743 + int chunkInNAND, int inScan);
3745 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3746 + yaffs_ObjectType type);
3747 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3748 + yaffs_Object * obj);
3749 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3750 + int force, int isShrink, int shadows);
3751 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3752 +static int yaffs_CheckStructures(void);
3753 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3754 + int chunkOffset, int *limit);
3755 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3757 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3759 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3760 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3763 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3766 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3767 +static void yaffs_DestroyObject(yaffs_Object * obj);
3769 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3770 + int chunkInObject);
3772 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3774 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3776 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3778 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3780 +#ifdef YAFFS_PARANOID
3781 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3783 +#define yaffs_CheckFileSanity(in)
3786 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3787 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3789 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3791 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3792 + yaffs_ExtendedTags * tags);
3794 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
3795 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
3796 + yaffs_FileStructure * fStruct,
3800 +/* Function to calculate chunk and offset */
3802 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3804 + if(dev->chunkShift){
3805 + /* Easy-peasy power of 2 case */
3806 + *chunk = (__u32)(addr >> dev->chunkShift);
3807 + *offset = (__u32)(addr & dev->chunkMask);
3809 + else if(dev->crumbsPerChunk)
3811 + /* Case where we're using "crumbs" */
3812 + *offset = (__u32)(addr & dev->crumbMask);
3813 + addr >>= dev->crumbShift;
3814 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3815 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3821 +/* Function to return the number of shifts for a power of 2 greater than or equal
3822 + * to the given number
3823 + * Note we don't try to cater for all possible numbers and this does not have to
3824 + * be hellishly efficient.
3827 +static __u32 ShiftsGE(__u32 x)
3832 + nShifts = extraBits = 0;
3835 + if(x & 1) extraBits++;
3846 +/* Function to return the number of shifts to get a 1 in bit 0
3849 +static __u32 ShiftDiv(__u32 x)
3868 + * Temporary buffer manipulations.
3871 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3874 + __u8 *buf = (__u8 *)1;
3876 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3878 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
3879 + dev->tempBuffer[i].line = 0; /* not in use */
3880 + dev->tempBuffer[i].buffer = buf =
3881 + YMALLOC_DMA(dev->nDataBytesPerChunk);
3884 + return buf ? YAFFS_OK : YAFFS_FAIL;
3888 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3891 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3892 + if (dev->tempBuffer[i].line == 0) {
3893 + dev->tempBuffer[i].line = lineNo;
3894 + if ((i + 1) > dev->maxTemp) {
3895 + dev->maxTemp = i + 1;
3896 + for (j = 0; j <= i; j++)
3897 + dev->tempBuffer[j].maxLine =
3898 + dev->tempBuffer[j].line;
3901 + return dev->tempBuffer[i].buffer;
3905 + T(YAFFS_TRACE_BUFFERS,
3906 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3908 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3909 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3911 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3914 + * If we got here then we have to allocate an unmanaged one
3915 + * This is not good.
3918 + dev->unmanagedTempAllocations++;
3919 + return YMALLOC(dev->nDataBytesPerChunk);
3923 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3927 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3928 + if (dev->tempBuffer[i].buffer == buffer) {
3929 + dev->tempBuffer[i].line = 0;
3935 + /* assume it is an unmanaged one. */
3936 + T(YAFFS_TRACE_BUFFERS,
3937 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3940 + dev->unmanagedTempDeallocations++;
3946 + * Determine if we have a managed buffer.
3948 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3951 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3952 + if (dev->tempBuffer[i].buffer == buffer)
3957 + for (i = 0; i < dev->nShortOpCaches; i++) {
3958 + if( dev->srCache[i].data == buffer )
3963 + if (buffer == dev->checkpointBuffer)
3966 + T(YAFFS_TRACE_ALWAYS,
3967 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3974 + * Chunk bitmap manipulations
3977 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3979 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3980 + T(YAFFS_TRACE_ERROR,
3981 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3985 + return dev->chunkBits +
3986 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3989 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3991 + if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
3992 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
3993 + T(YAFFS_TRACE_ERROR,
3994 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
3999 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
4001 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4003 + memset(blkBits, 0, dev->chunkBitmapStride);
4006 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
4008 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4010 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4012 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
4015 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
4017 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4019 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4021 + blkBits[chunk / 8] |= (1 << (chunk & 7));
4024 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
4026 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4027 + yaffs_VerifyChunkBitId(dev,blk,chunk);
4029 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4032 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4034 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4036 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4044 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4046 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4049 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4050 + __u8 x = *blkBits;
4063 + * Verification code
4066 +static int yaffs_SkipVerification(yaffs_Device *dev)
4068 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4071 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4073 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4076 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4078 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4081 +static const char * blockStateName[] = {
4094 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4099 + if(yaffs_SkipVerification(dev))
4102 + /* Report illegal runtime states */
4103 + if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4104 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
4106 + switch(bi->blockState){
4107 + case YAFFS_BLOCK_STATE_UNKNOWN:
4108 + case YAFFS_BLOCK_STATE_SCANNING:
4109 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4110 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
4111 + n,blockStateName[bi->blockState]));
4114 + /* Check pages in use and soft deletions are legal */
4116 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4118 + if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4119 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4120 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4121 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4122 + n,bi->pagesInUse,bi->softDeletions));
4125 + /* Check chunk bitmap legal */
4126 + inUse = yaffs_CountChunkBits(dev,n);
4127 + if(inUse != bi->pagesInUse)
4128 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4129 + n,bi->pagesInUse,inUse));
4131 + /* Check that the sequence number is valid.
4132 + * Ten million is legal, but is very unlikely
4134 + if(dev->isYaffs2 &&
4135 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4136 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
4137 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4138 + n,bi->sequenceNumber));
4142 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4144 + yaffs_VerifyBlock(dev,bi,n);
4146 + /* After collection the block should be in the erased state */
4147 + /* TODO: This will need to change if we do partial gc */
4149 + if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
4150 + T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4151 + n,bi->blockState));
4155 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4158 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4159 + int nIllegalBlockStates = 0;
4162 + if(yaffs_SkipVerification(dev))
4165 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4168 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4169 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4170 + yaffs_VerifyBlock(dev,bi,i);
4172 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4173 + nBlocksPerState[bi->blockState]++;
4175 + nIllegalBlockStates++;
4179 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4180 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4182 + T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
4183 + if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4184 + T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
4186 + for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4187 + T(YAFFS_TRACE_VERIFY,
4188 + (TSTR("%s %d blocks"TENDSTR),
4189 + blockStateName[i],nBlocksPerState[i]));
4191 + if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4192 + T(YAFFS_TRACE_VERIFY,
4193 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4194 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4196 + if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4197 + T(YAFFS_TRACE_VERIFY,
4198 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4199 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4201 + if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4202 + T(YAFFS_TRACE_VERIFY,
4203 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4204 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4206 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4211 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4212 + * case those tests will not be performed.
4214 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4216 + if(yaffs_SkipVerification(obj->myDev))
4219 + if(!(tags && obj && oh)){
4220 + T(YAFFS_TRACE_VERIFY,
4221 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4222 + (__u32)tags,(__u32)obj,(__u32)oh));
4226 + if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4227 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4228 + T(YAFFS_TRACE_VERIFY,
4229 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4230 + tags->objectId, oh->type));
4232 + if(tags->objectId != obj->objectId)
4233 + T(YAFFS_TRACE_VERIFY,
4234 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4235 + tags->objectId, obj->objectId));
4239 + * Check that the object's parent ids match if parentCheck requested.
4241 + * Tests do not apply to the root object.
4244 + if(parentCheck && tags->objectId > 1 && !obj->parent)
4245 + T(YAFFS_TRACE_VERIFY,
4246 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4247 + tags->objectId, oh->parentObjectId));
4250 + if(parentCheck && obj->parent &&
4251 + oh->parentObjectId != obj->parent->objectId &&
4252 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4253 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4254 + T(YAFFS_TRACE_VERIFY,
4255 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4256 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4259 + if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4260 + T(YAFFS_TRACE_VERIFY,
4261 + (TSTR("Obj %d header name is NULL"TENDSTR),
4264 + if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4265 + T(YAFFS_TRACE_VERIFY,
4266 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4272 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4273 + __u32 level, int chunkOffset)
4276 + yaffs_Device *dev = obj->myDev;
4278 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4283 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4284 + if (tn->internal[i]) {
4285 + ok = yaffs_VerifyTnodeWorker(obj,
4288 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4291 + } else if (level == 0) {
4293 + yaffs_ExtendedTags tags;
4294 + __u32 objectId = obj->objectId;
4296 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4298 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4299 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4302 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4303 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4304 + if(tags.objectId != objectId || tags.chunkId != chunkOffset){
4305 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4306 + objectId, chunkOffset, theChunk,
4307 + tags.objectId, tags.chunkId));
4320 +static void yaffs_VerifyFile(yaffs_Object *obj)
4322 + int requiredTallness;
4323 + int actualTallness;
4328 + yaffs_Device *dev;
4329 + yaffs_ExtendedTags tags;
4333 + if(obj && yaffs_SkipVerification(obj->myDev))
4337 + objectId = obj->objectId;
4339 + /* Check file size is consistent with tnode depth */
4340 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4341 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4342 + requiredTallness = 0;
4344 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4345 + requiredTallness++;
4348 + actualTallness = obj->variant.fileVariant.topLevel;
4350 + if(requiredTallness > actualTallness )
4351 + T(YAFFS_TRACE_VERIFY,
4352 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4353 + obj->objectId,actualTallness, requiredTallness));
4356 + /* Check that the chunks in the tnode tree are all correct.
4357 + * We do this by scanning through the tnode tree and
4358 + * checking the tags for every chunk match.
4361 + if(yaffs_SkipNANDVerification(dev))
4364 + for(i = 1; i <= lastChunk; i++){
4365 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
4368 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4370 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4371 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4372 + if(tags.objectId != objectId || tags.chunkId != i){
4373 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4374 + objectId, i, theChunk,
4375 + tags.objectId, tags.chunkId));
4384 +static void yaffs_VerifyDirectory(yaffs_Object *obj)
4386 + if(obj && yaffs_SkipVerification(obj->myDev))
4391 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4393 + if(obj && yaffs_SkipVerification(obj->myDev))
4396 + /* Verify sane equivalent object */
4399 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4401 + if(obj && yaffs_SkipVerification(obj->myDev))
4404 + /* Verify symlink string */
4407 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4409 + if(obj && yaffs_SkipVerification(obj->myDev))
4413 +static void yaffs_VerifyObject(yaffs_Object *obj)
4415 + yaffs_Device *dev;
4421 + __u32 chunkIsLive;
4428 + if(yaffs_SkipVerification(dev))
4431 + /* Check sane object header chunk */
4433 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4434 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4436 + chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
4437 + chunkIsLive = chunkIdOk &&
4438 + yaffs_CheckChunkBit(dev,
4439 + obj->chunkId / dev->nChunksPerBlock,
4440 + obj->chunkId % dev->nChunksPerBlock);
4442 + (!chunkIdOk || !chunkIsLive)) {
4443 + T(YAFFS_TRACE_VERIFY,
4444 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4445 + obj->objectId,obj->chunkId,
4446 + chunkIdOk ? "" : ",out of range",
4447 + chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
4450 + if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
4451 + yaffs_ExtendedTags tags;
4452 + yaffs_ObjectHeader *oh;
4453 + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
4455 + oh = (yaffs_ObjectHeader *)buffer;
4457 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
4459 + yaffs_VerifyObjectHeader(obj,oh,&tags,1);
4461 + yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
4464 + /* Verify it has a parent */
4465 + if(obj && !obj->fake &&
4466 + (!obj->parent || obj->parent->myDev != dev)){
4467 + T(YAFFS_TRACE_VERIFY,
4468 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4469 + obj->objectId,obj->parent));
4472 + /* Verify parent is a directory */
4473 + if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
4474 + T(YAFFS_TRACE_VERIFY,
4475 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4476 + obj->objectId,obj->parent->variantType));
4479 + switch(obj->variantType){
4480 + case YAFFS_OBJECT_TYPE_FILE:
4481 + yaffs_VerifyFile(obj);
4483 + case YAFFS_OBJECT_TYPE_SYMLINK:
4484 + yaffs_VerifySymlink(obj);
4486 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4487 + yaffs_VerifyDirectory(obj);
4489 + case YAFFS_OBJECT_TYPE_HARDLINK:
4490 + yaffs_VerifyHardLink(obj);
4492 + case YAFFS_OBJECT_TYPE_SPECIAL:
4493 + yaffs_VerifySpecial(obj);
4495 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4497 + T(YAFFS_TRACE_VERIFY,
4498 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4499 + obj->objectId,obj->variantType));
4506 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4508 + yaffs_Object *obj;
4510 + struct list_head *lh;
4512 + if(yaffs_SkipVerification(dev))
4515 + /* Iterate through the objects in each hash entry */
4517 + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
4518 + list_for_each(lh, &dev->objectBucket[i].list) {
4520 + obj = list_entry(lh, yaffs_Object, hashLink);
4521 + yaffs_VerifyObject(obj);
4530 + * Simple hash function. Needs to have a reasonable spread
4533 +static Y_INLINE int yaffs_HashFunction(int n)
4536 + return (n % YAFFS_NOBJECT_BUCKETS);
4540 + * Access functions to useful fake objects
4543 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
4545 + return dev->rootDir;
4548 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
4550 + return dev->lostNFoundDir;
4555 + * Erased NAND checking functions
4558 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
4560 + /* Horrible, slow implementation */
4561 + while (nBytes--) {
4562 + if (*buffer != 0xFF)
4569 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4573 + int retval = YAFFS_OK;
4574 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4575 + yaffs_ExtendedTags tags;
4578 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4580 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4581 + retval = YAFFS_FAIL;
4584 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4585 + T(YAFFS_TRACE_NANDACCESS,
4586 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4587 + retval = YAFFS_FAIL;
4590 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4597 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4598 + const __u8 * data,
4599 + yaffs_ExtendedTags * tags,
4606 + yaffs_InvalidateCheckpoint(dev);
4609 + yaffs_BlockInfo *bi = 0;
4612 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4618 + /* First check this chunk is erased, if it needs
4619 + * checking. The checking policy (unless forced
4620 + * always on) is as follows:
4622 + * Check the first page we try to write in a block.
4623 + * If the check passes then we don't need to check any
4624 + * more. If the check fails, we check again...
4625 + * If the block has been erased, we don't need to check.
4627 + * However, if the block has been prioritised for gc,
4628 + * then we think there might be something odd about
4629 + * this block and stop using it.
4631 + * Rationale: We should only ever see chunks that have
4632 + * not been erased if there was a partially written
4633 + * chunk due to power loss. This checking policy should
4634 + * catch that case with very few checks and thus save a
4635 + * lot of checks that are most likely not needed.
4637 + if (bi->gcPrioritise) {
4638 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4639 + /* try another chunk */
4643 + /* let's give it a try */
4646 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4647 + bi->skipErasedCheck = 0;
4649 + if (!bi->skipErasedCheck) {
4650 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4651 + if (erasedOk != YAFFS_OK) {
4652 + T(YAFFS_TRACE_ERROR,
4653 + (TSTR ("**>> yaffs chunk %d was not erased"
4654 + TENDSTR), chunk));
4656 + /* try another chunk */
4659 + bi->skipErasedCheck = 1;
4662 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4664 + if (writeOk != YAFFS_OK) {
4665 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4666 + /* try another chunk */
4670 + /* Copy the data into the robustification buffer */
4671 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4673 + } while (writeOk != YAFFS_OK && attempts < yaffs_wr_attempts);
4675 + if (attempts > 1) {
4676 + T(YAFFS_TRACE_ERROR,
4677 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4680 + dev->nRetriedWrites += (attempts - 1);
4687 + * Block retiring for handling a broken block.
4690 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
4692 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4694 + yaffs_InvalidateCheckpoint(dev);
4696 + yaffs_MarkBlockBad(dev, blockInNAND);
4698 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
4699 + bi->gcPrioritise = 0;
4700 + bi->needsRetiring = 0;
4702 + dev->nRetiredBlocks++;
4706 + * Functions for robustisizing TODO
4710 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
4711 + const __u8 * data,
4712 + const yaffs_ExtendedTags * tags)
4716 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
4717 + const yaffs_ExtendedTags * tags)
4721 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
4723 + if(!bi->gcPrioritise){
4724 + bi->gcPrioritise = 1;
4725 + dev->hasPendingPrioritisedGCs = 1;
4726 + bi->chunkErrorStrikes ++;
4728 + if(bi->chunkErrorStrikes > 3){
4729 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
4730 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
4737 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
4740 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
4741 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4743 + yaffs_HandleChunkError(dev,bi);
4747 + /* Was an actual write failure, so mark the block for retirement */
4748 + bi->needsRetiring = 1;
4749 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4750 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
4755 + /* Delete the chunk */
4756 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
4760 +/*---------------- Name handling functions ------------*/
4762 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
4767 + YUCHAR *bname = (YUCHAR *) name;
4769 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
4771 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4772 + sum += yaffs_toupper(*bname) * i;
4774 + sum += (*bname) * i;
4783 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4785 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4786 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4787 + yaffs_strcpy(obj->shortName, name);
4789 + obj->shortName[0] = _Y('\0');
4792 + obj->sum = yaffs_CalcNameSum(name);
4795 +/*-------------------- TNODES -------------------
4797 + * List of spare tnodes
4798 + * The list is hooked together using the first pointer
4802 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4803 + * adds them to the tnode free list.
4804 + * Don't use this function directly
4807 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4811 + yaffs_Tnode *newTnodes;
4813 + yaffs_Tnode *curr;
4814 + yaffs_Tnode *next;
4815 + yaffs_TnodeList *tnl;
4820 + /* Calculate the tnode size in bytes for variable width tnode support.
4821 + * Must be a multiple of 32-bits */
4822 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4824 + /* make these things */
4826 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4827 + mem = (__u8 *)newTnodes;
4830 + T(YAFFS_TRACE_ERROR,
4831 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4832 + return YAFFS_FAIL;
4835 + /* Hook them into the free list */
4837 + for (i = 0; i < nTnodes - 1; i++) {
4838 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4839 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4840 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4844 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4845 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4846 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4848 + dev->freeTnodes = newTnodes;
4850 + /* New hookup for wide tnodes */
4851 + for(i = 0; i < nTnodes -1; i++) {
4852 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4853 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4854 + curr->internal[0] = next;
4857 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4858 + curr->internal[0] = dev->freeTnodes;
4859 + dev->freeTnodes = (yaffs_Tnode *)mem;
4864 + dev->nFreeTnodes += nTnodes;
4865 + dev->nTnodesCreated += nTnodes;
4867 + /* Now add this bunch of tnodes to a list for freeing up.
4868 + * NB If we can't add this to the management list it isn't fatal
4869 + * but it just means we can't free this bunch of tnodes later.
4872 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4874 + T(YAFFS_TRACE_ERROR,
4876 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4877 + return YAFFS_FAIL;
4880 + tnl->tnodes = newTnodes;
4881 + tnl->next = dev->allocatedTnodeList;
4882 + dev->allocatedTnodeList = tnl;
4885 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4890 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4892 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4894 + yaffs_Tnode *tn = NULL;
4896 + /* If there are none left make more */
4897 + if (!dev->freeTnodes) {
4898 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4901 + if (dev->freeTnodes) {
4902 + tn = dev->freeTnodes;
4903 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4904 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4905 + /* Hoosterman, this thing looks like it isn't in the list */
4906 + T(YAFFS_TRACE_ALWAYS,
4907 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4910 + dev->freeTnodes = dev->freeTnodes->internal[0];
4911 + dev->nFreeTnodes--;
4917 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4919 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4922 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4927 +/* FreeTnode frees up a tnode and puts it back on the free list */
4928 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4931 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4932 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4933 + /* Hoosterman, this thing looks like it is already in the list */
4934 + T(YAFFS_TRACE_ALWAYS,
4935 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4937 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4939 + tn->internal[0] = dev->freeTnodes;
4940 + dev->freeTnodes = tn;
4941 + dev->nFreeTnodes++;
4945 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4947 + /* Free the list of allocated tnodes */
4948 + yaffs_TnodeList *tmp;
4950 + while (dev->allocatedTnodeList) {
4951 + tmp = dev->allocatedTnodeList->next;
4953 + YFREE(dev->allocatedTnodeList->tnodes);
4954 + YFREE(dev->allocatedTnodeList);
4955 + dev->allocatedTnodeList = tmp;
4959 + dev->freeTnodes = NULL;
4960 + dev->nFreeTnodes = 0;
4963 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4965 + dev->allocatedTnodeList = NULL;
4966 + dev->freeTnodes = NULL;
4967 + dev->nFreeTnodes = 0;
4968 + dev->nTnodesCreated = 0;
4973 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4975 + __u32 *map = (__u32 *)tn;
4981 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4982 + val >>= dev->chunkGroupBits;
4984 + bitInMap = pos * dev->tnodeWidth;
4985 + wordInMap = bitInMap /32;
4986 + bitInWord = bitInMap & (32 -1);
4988 + mask = dev->tnodeMask << bitInWord;
4990 + map[wordInMap] &= ~mask;
4991 + map[wordInMap] |= (mask & (val << bitInWord));
4993 + if(dev->tnodeWidth > (32-bitInWord)) {
4994 + bitInWord = (32 - bitInWord);
4996 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4997 + map[wordInMap] &= ~mask;
4998 + map[wordInMap] |= (mask & (val >> bitInWord));
5002 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
5004 + __u32 *map = (__u32 *)tn;
5010 + pos &= YAFFS_TNODES_LEVEL0_MASK;
5012 + bitInMap = pos * dev->tnodeWidth;
5013 + wordInMap = bitInMap /32;
5014 + bitInWord = bitInMap & (32 -1);
5016 + val = map[wordInMap] >> bitInWord;
5018 + if(dev->tnodeWidth > (32-bitInWord)) {
5019 + bitInWord = (32 - bitInWord);
5021 + val |= (map[wordInMap] << bitInWord);
5024 + val &= dev->tnodeMask;
5025 + val <<= dev->chunkGroupBits;
5030 +/* ------------------- End of individual tnode manipulation -----------------*/
5032 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5033 + * The look up tree is represented by the top tnode and the number of topLevel
5034 + * in the tree. 0 means only the level 0 tnode is in the tree.
5037 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5038 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
5039 + yaffs_FileStructure * fStruct,
5043 + yaffs_Tnode *tn = fStruct->top;
5045 + int requiredTallness;
5046 + int level = fStruct->topLevel;
5048 + /* Check sane level and chunk Id */
5049 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
5053 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5057 + /* First check we're tall enough (ie enough topLevel) */
5059 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5060 + requiredTallness = 0;
5062 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5063 + requiredTallness++;
5066 + if (requiredTallness > fStruct->topLevel) {
5067 + /* Not tall enough, so we can't find it, return NULL. */
5071 + /* Traverse down to level 0 */
5072 + while (level > 0 && tn) {
5074 + internal[(chunkId >>
5075 + ( YAFFS_TNODES_LEVEL0_BITS +
5077 + YAFFS_TNODES_INTERNAL_BITS)
5079 + YAFFS_TNODES_INTERNAL_MASK];
5087 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5088 + * This happens in two steps:
5089 + * 1. If the tree isn't tall enough, then make it taller.
5090 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5092 + * Used when modifying the tree.
5094 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5095 + * be plugged into the ttree.
5098 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
5099 + yaffs_FileStructure * fStruct,
5101 + yaffs_Tnode *passedTn)
5104 + int requiredTallness;
5112 + /* Check sane level and page Id */
5113 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
5117 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5121 + /* First check we're tall enough (ie enough topLevel) */
5123 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5124 + requiredTallness = 0;
5126 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5127 + requiredTallness++;
5131 + if (requiredTallness > fStruct->topLevel) {
5132 + /* Not tall enough,gotta make the tree taller */
5133 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5135 + tn = yaffs_GetTnode(dev);
5138 + tn->internal[0] = fStruct->top;
5139 + fStruct->top = tn;
5141 + T(YAFFS_TRACE_ERROR,
5142 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5146 + fStruct->topLevel = requiredTallness;
5149 + /* Traverse down to level 0, adding anything we need */
5151 + l = fStruct->topLevel;
5152 + tn = fStruct->top;
5155 + while (l > 0 && tn) {
5157 + ( YAFFS_TNODES_LEVEL0_BITS +
5158 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5159 + YAFFS_TNODES_INTERNAL_MASK;
5162 + if((l>1) && !tn->internal[x]){
5163 + /* Add missing non-level-zero tnode */
5164 + tn->internal[x] = yaffs_GetTnode(dev);
5166 + } else if(l == 1) {
5167 + /* Looking from level 1 at level 0 */
5169 + /* If we already have one, then release it.*/
5170 + if(tn->internal[x])
5171 + yaffs_FreeTnode(dev,tn->internal[x]);
5172 + tn->internal[x] = passedTn;
5174 + } else if(!tn->internal[x]) {
5175 + /* Don't have one, none passed in */
5176 + tn->internal[x] = yaffs_GetTnode(dev);
5180 + tn = tn->internal[x];
5184 + /* top is level 0 */
5186 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5187 + yaffs_FreeTnode(dev,passedTn);
5194 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
5195 + yaffs_ExtendedTags * tags, int objectId,
5200 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5201 + if (yaffs_CheckChunkBit
5202 + (dev, theChunk / dev->nChunksPerBlock,
5203 + theChunk % dev->nChunksPerBlock)) {
5204 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5206 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5218 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5219 + * chunks and tnodes in the file
5220 + * Returns 1 if the tree was deleted.
5221 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5224 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
5225 + int chunkOffset, int *limit)
5230 + yaffs_ExtendedTags tags;
5232 + yaffs_Device *dev = in->myDev;
5239 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5241 + if (tn->internal[i]) {
5242 + if (limit && (*limit) < 0) {
5246 + yaffs_DeleteWorker(in,
5254 + YAFFS_TNODES_INTERNAL_BITS)
5259 + yaffs_FreeTnode(dev,
5262 + tn->internal[i] = NULL;
5267 + return (allDone) ? 1 : 0;
5268 + } else if (level == 0) {
5271 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5273 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5278 + YAFFS_TNODES_LEVEL0_BITS) + i;
5281 + yaffs_FindChunkInGroup(dev,
5287 + if (foundChunk > 0) {
5288 + yaffs_DeleteChunk(dev,
5291 + in->nDataChunks--;
5293 + *limit = *limit - 1;
5294 + if (*limit <= 0) {
5301 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5305 + return (i < 0) ? 1 : 0;
5315 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
5318 + yaffs_BlockInfo *theBlock;
5320 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5322 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5324 + theBlock->softDeletions++;
5325 + dev->nFreeChunks++;
5329 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5330 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5332 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5335 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
5336 + __u32 level, int chunkOffset)
5341 + yaffs_Device *dev = in->myDev;
5346 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5348 + if (tn->internal[i]) {
5350 + yaffs_SoftDeleteWorker(in,
5356 + YAFFS_TNODES_INTERNAL_BITS)
5359 + yaffs_FreeTnode(dev,
5362 + tn->internal[i] = NULL;
5364 + /* Hoosterman... how could this happen? */
5368 + return (allDone) ? 1 : 0;
5369 + } else if (level == 0) {
5371 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5372 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5374 + /* Note this does not find the real chunk, only the chunk group.
5375 + * We make an assumption that a chunk group is not larger than
5378 + yaffs_SoftDeleteChunk(dev, theChunk);
5379 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5393 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
5395 + if (obj->deleted &&
5396 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5397 + if (obj->nDataChunks <= 0) {
5398 + /* Empty file with no duplicate object headers, just delete it immediately */
5399 + yaffs_FreeTnode(obj->myDev,
5400 + obj->variant.fileVariant.top);
5401 + obj->variant.fileVariant.top = NULL;
5402 + T(YAFFS_TRACE_TRACING,
5403 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5405 + yaffs_DoGenericObjectDeletion(obj);
5407 + yaffs_SoftDeleteWorker(obj,
5408 + obj->variant.fileVariant.top,
5409 + obj->variant.fileVariant.
5411 + obj->softDeleted = 1;
5416 +/* Pruning removes any part of the file structure tree that is beyond the
5417 + * bounds of the file (ie that does not point to chunks).
5419 + * A file should only get pruned when its size is reduced.
5421 + * Before pruning, the chunks must be pulled from the tree and the
5422 + * level 0 tnode entries must be zeroed out.
5423 + * Could also use this for file deletion, but that's probably better handled
5424 + * by a special case.
5427 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
5428 + __u32 level, int del0)
5436 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5437 + if (tn->internal[i] && level > 0) {
5439 + yaffs_PruneWorker(dev, tn->internal[i],
5441 + (i == 0) ? del0 : 1);
5444 + if (tn->internal[i]) {
5449 + if (hasData == 0 && del0) {
5450 + /* Free and return NULL */
5452 + yaffs_FreeTnode(dev, tn);
5462 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
5463 + yaffs_FileStructure * fStruct)
5470 + if (fStruct->topLevel > 0) {
5472 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5474 + /* Now we have a tree with all the non-zero branches NULL but the height
5475 + * is the same as it was.
5476 + * Let's see if we can trim internal tnodes to shorten the tree.
5477 + * We can do this if only the 0th element in the tnode is in use
5478 + * (ie all the non-zero are NULL)
5481 + while (fStruct->topLevel && !done) {
5482 + tn = fStruct->top;
5485 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5486 + if (tn->internal[i]) {
5492 + fStruct->top = tn->internal[0];
5493 + fStruct->topLevel--;
5494 + yaffs_FreeTnode(dev, tn);
5504 +/*-------------------- End of File Structure functions.-------------------*/
5506 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5507 + * adds them to the object free list.
5509 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
5512 + yaffs_Object *newObjects;
5513 + yaffs_ObjectList *list;
5518 + /* make these things */
5519 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5520 + list = YMALLOC(sizeof(yaffs_ObjectList));
5522 + if (!newObjects || !list) {
5524 + YFREE(newObjects);
5527 + T(YAFFS_TRACE_ALLOCATE,
5528 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5529 + return YAFFS_FAIL;
5532 + /* Hook them into the free list */
5533 + for (i = 0; i < nObjects - 1; i++) {
5534 + newObjects[i].siblings.next =
5535 + (struct list_head *)(&newObjects[i + 1]);
5538 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5539 + dev->freeObjects = newObjects;
5540 + dev->nFreeObjects += nObjects;
5541 + dev->nObjectsCreated += nObjects;
5543 + /* Now add this bunch of Objects to a list for freeing up. */
5545 + list->objects = newObjects;
5546 + list->next = dev->allocatedObjectList;
5547 + dev->allocatedObjectList = list;
5553 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5554 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
5556 + yaffs_Object *tn = NULL;
5558 + /* If there are none left make more */
5559 + if (!dev->freeObjects) {
5560 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5563 + if (dev->freeObjects) {
5564 + tn = dev->freeObjects;
5565 + dev->freeObjects =
5566 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5567 + dev->nFreeObjects--;
5569 + /* Now sweeten it up... */
5571 + memset(tn, 0, sizeof(yaffs_Object));
5574 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5575 + INIT_LIST_HEAD(&(tn->hardLinks));
5576 + INIT_LIST_HEAD(&(tn->hashLink));
5577 + INIT_LIST_HEAD(&tn->siblings);
5579 + /* Add it to the lost and found directory.
5580 + * NB Can't put root or lostNFound in lostNFound so
5581 + * check if lostNFound exists first
5583 + if (dev->lostNFoundDir) {
5584 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5591 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
5595 + yaffs_Object *obj =
5596 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5598 + obj->fake = 1; /* it is fake so it has no NAND presence... */
5599 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5600 + obj->unlinkAllowed = 0; /* ... or unlink it */
5602 + obj->unlinked = 0;
5603 + obj->yst_mode = mode;
5605 + obj->chunkId = 0; /* Not a valid chunk. */
5612 +static void yaffs_UnhashObject(yaffs_Object * tn)
5615 + yaffs_Device *dev = tn->myDev;
5617 + /* If it is still linked into the bucket list, free from the list */
5618 + if (!list_empty(&tn->hashLink)) {
5619 + list_del_init(&tn->hashLink);
5620 + bucket = yaffs_HashFunction(tn->objectId);
5621 + dev->objectBucket[bucket].count--;
5626 +/* FreeObject frees up a Object and puts it back on the free list */
5627 +static void yaffs_FreeObject(yaffs_Object * tn)
5630 + yaffs_Device *dev = tn->myDev;
5633 + if (tn->myInode) {
5634 + /* We're still hooked up to a cached inode.
5635 + * Don't delete now, but mark for later deletion
5637 + tn->deferedFree = 1;
5642 + yaffs_UnhashObject(tn);
5644 + /* Link into the free list. */
5645 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
5646 + dev->freeObjects = tn;
5647 + dev->nFreeObjects++;
5652 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
5654 + if (obj->deferedFree) {
5655 + yaffs_FreeObject(obj);
5661 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
5663 + /* Free the list of allocated Objects */
5665 + yaffs_ObjectList *tmp;
5667 + while (dev->allocatedObjectList) {
5668 + tmp = dev->allocatedObjectList->next;
5669 + YFREE(dev->allocatedObjectList->objects);
5670 + YFREE(dev->allocatedObjectList);
5672 + dev->allocatedObjectList = tmp;
5675 + dev->freeObjects = NULL;
5676 + dev->nFreeObjects = 0;
5679 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
5683 + dev->allocatedObjectList = NULL;
5684 + dev->freeObjects = NULL;
5685 + dev->nFreeObjects = 0;
5687 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5688 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
5689 + dev->objectBucket[i].count = 0;
5694 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
5699 + int lowest = 999999;
5701 + /* First let's see if we can find one that's empty. */
5703 + for (i = 0; i < 10 && lowest > 0; i++) {
5705 + x %= YAFFS_NOBJECT_BUCKETS;
5706 + if (dev->objectBucket[x].count < lowest) {
5707 + lowest = dev->objectBucket[x].count;
5713 + /* If we didn't find an empty list, then try
5714 + * looking a bit further for a short one
5717 + for (i = 0; i < 10 && lowest > 3; i++) {
5719 + x %= YAFFS_NOBJECT_BUCKETS;
5720 + if (dev->objectBucket[x].count < lowest) {
5721 + lowest = dev->objectBucket[x].count;
5730 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
5732 + int bucket = yaffs_FindNiceObjectBucket(dev);
5734 + /* Now find an object value that has not already been taken
5735 + * by scanning the list.
5739 + struct list_head *i;
5741 + __u32 n = (__u32) bucket;
5743 + /* yaffs_CheckObjectHashSanity(); */
5747 + n += YAFFS_NOBJECT_BUCKETS;
5748 + if (1 || dev->objectBucket[bucket].count > 0) {
5749 + list_for_each(i, &dev->objectBucket[bucket].list) {
5750 + /* If there is already one in the list */
5752 + && list_entry(i, yaffs_Object,
5753 + hashLink)->objectId == n) {
5764 +static void yaffs_HashObject(yaffs_Object * in)
5766 + int bucket = yaffs_HashFunction(in->objectId);
5767 + yaffs_Device *dev = in->myDev;
5769 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
5770 + dev->objectBucket[bucket].count++;
5774 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5776 + int bucket = yaffs_HashFunction(number);
5777 + struct list_head *i;
5780 + list_for_each(i, &dev->objectBucket[bucket].list) {
5781 + /* Look if it is in the list */
5783 + in = list_entry(i, yaffs_Object, hashLink);
5784 + if (in->objectId == number) {
5786 + /* Don't tell the VFS about this one if it is defered free */
5787 + if (in->deferedFree)
5799 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5800 + yaffs_ObjectType type)
5803 + yaffs_Object *theObject;
5807 + number = yaffs_CreateNewObjectNumber(dev);
5810 + theObject = yaffs_AllocateEmptyObject(dev);
5814 + if(type == YAFFS_OBJECT_TYPE_FILE){
5815 + tn = yaffs_GetTnode(dev);
5817 + yaffs_FreeObject(theObject);
5825 + theObject->fake = 0;
5826 + theObject->renameAllowed = 1;
5827 + theObject->unlinkAllowed = 1;
5828 + theObject->objectId = number;
5829 + yaffs_HashObject(theObject);
5830 + theObject->variantType = type;
5831 +#ifdef CONFIG_YAFFS_WINCE
5832 + yfsd_WinFileTimeNow(theObject->win_atime);
5833 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5834 + theObject->win_atime[0];
5835 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5836 + theObject->win_atime[1];
5840 + theObject->yst_atime = theObject->yst_mtime =
5841 + theObject->yst_ctime = Y_CURRENT_TIME;
5844 + case YAFFS_OBJECT_TYPE_FILE:
5845 + theObject->variant.fileVariant.fileSize = 0;
5846 + theObject->variant.fileVariant.scannedFileSize = 0;
5847 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5848 + theObject->variant.fileVariant.topLevel = 0;
5849 + theObject->variant.fileVariant.top = tn;
5851 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5852 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5855 + case YAFFS_OBJECT_TYPE_SYMLINK:
5856 + case YAFFS_OBJECT_TYPE_HARDLINK:
5857 + case YAFFS_OBJECT_TYPE_SPECIAL:
5858 + /* No action required */
5860 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5861 + /* todo this should not happen */
5869 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5871 + yaffs_ObjectType type)
5873 + yaffs_Object *theObject = NULL;
5876 + theObject = yaffs_FindObjectByNumber(dev, number);
5880 + theObject = yaffs_CreateNewObject(dev, number, type);
5888 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5890 + YCHAR *newStr = NULL;
5892 + if (str && *str) {
5893 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5895 + yaffs_strcpy(newStr, str);
5903 + * Mknod (create) a new object.
5904 + * equivalentObject only has meaning for a hard link;
5905 + * aliasString only has meaning for a sumlink.
5906 + * rdev only has meaning for devices (a subset of special objects)
5909 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5910 + yaffs_Object * parent,
5911 + const YCHAR * name,
5915 + yaffs_Object * equivalentObject,
5916 + const YCHAR * aliasString, __u32 rdev)
5921 + yaffs_Device *dev = parent->myDev;
5923 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5924 + if (yaffs_FindObjectByName(parent, name)) {
5928 + in = yaffs_CreateNewObject(dev, -1, type);
5930 + if(type == YAFFS_OBJECT_TYPE_SYMLINK){
5931 + str = yaffs_CloneString(aliasString);
5933 + yaffs_FreeObject(in);
5943 + in->variantType = type;
5945 + in->yst_mode = mode;
5947 +#ifdef CONFIG_YAFFS_WINCE
5948 + yfsd_WinFileTimeNow(in->win_atime);
5949 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5950 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5953 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5955 + in->yst_rdev = rdev;
5956 + in->yst_uid = uid;
5957 + in->yst_gid = gid;
5959 + in->nDataChunks = 0;
5961 + yaffs_SetObjectName(in, name);
5964 + yaffs_AddObjectToDirectory(parent, in);
5966 + in->myDev = parent->myDev;
5969 + case YAFFS_OBJECT_TYPE_SYMLINK:
5970 + in->variant.symLinkVariant.alias = str;
5972 + case YAFFS_OBJECT_TYPE_HARDLINK:
5973 + in->variant.hardLinkVariant.equivalentObject =
5975 + in->variant.hardLinkVariant.equivalentObjectId =
5976 + equivalentObject->objectId;
5977 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5979 + case YAFFS_OBJECT_TYPE_FILE:
5980 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5981 + case YAFFS_OBJECT_TYPE_SPECIAL:
5982 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5987 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5988 + /* Could not create the object header, fail the creation */
5989 + yaffs_DestroyObject(in);
5998 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5999 + __u32 mode, __u32 uid, __u32 gid)
6001 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
6002 + uid, gid, NULL, NULL, 0);
6005 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
6006 + __u32 mode, __u32 uid, __u32 gid)
6008 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
6009 + mode, uid, gid, NULL, NULL, 0);
6012 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
6013 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
6015 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
6016 + uid, gid, NULL, NULL, rdev);
6019 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
6020 + __u32 mode, __u32 uid, __u32 gid,
6021 + const YCHAR * alias)
6023 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
6024 + uid, gid, NULL, alias, 0);
6027 +/* yaffs_Link returns the object id of the equivalent object.*/
6028 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
6029 + yaffs_Object * equivalentObject)
6031 + /* Get the real object in case we were fed a hard link as an equivalent object */
6032 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6034 + if (yaffs_MknodObject
6035 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6036 + equivalentObject, NULL, 0)) {
6037 + return equivalentObject;
6044 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
6045 + const YCHAR * newName, int force, int shadows)
6050 + yaffs_Object *existingTarget;
6052 + if (newDir == NULL) {
6053 + newDir = obj->parent; /* use the old directory */
6056 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6057 + T(YAFFS_TRACE_ALWAYS,
6059 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
6064 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6065 + if (obj->myDev->isYaffs2) {
6066 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6068 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6069 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6072 + deleteOp = (newDir == obj->myDev->deletedDir);
6074 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6076 + /* If the object is a file going into the unlinked directory,
6077 + * then it is OK to just stuff it in since duplicate names are allowed.
6078 + * else only proceed if the new name does not exist and if we're putting
6079 + * it into a directory.
6085 + !existingTarget) &&
6086 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6087 + yaffs_SetObjectName(obj, newName);
6090 + yaffs_AddObjectToDirectory(newDir, obj);
6093 + obj->unlinked = 1;
6095 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6096 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
6100 + return YAFFS_FAIL;
6103 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
6104 + yaffs_Object * newDir, const YCHAR * newName)
6106 + yaffs_Object *obj;
6107 + yaffs_Object *existingTarget;
6110 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6111 + /* Special case for case insemsitive systems (eg. WinCE).
6112 + * While look-up is case insensitive, the name isn't.
6113 + * Therefore we might want to change x.txt to X.txt
6115 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
6120 + obj = yaffs_FindObjectByName(oldDir, oldName);
6121 + /* Check new name to long. */
6122 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
6123 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
6124 + /* ENAMETOOLONG */
6125 + return YAFFS_FAIL;
6126 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
6127 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6128 + /* ENAMETOOLONG */
6129 + return YAFFS_FAIL;
6131 + if (obj && obj->renameAllowed) {
6133 + /* Now do the handling for an existing target, if there is one */
6135 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6136 + if (existingTarget &&
6137 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6138 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
6139 + /* There is a target that is a non-empty directory, so we fail */
6140 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6141 + } else if (existingTarget && existingTarget != obj) {
6142 + /* Nuke the target first, using shadowing,
6143 + * but only if it isn't the same object
6145 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6146 + existingTarget->objectId);
6147 + yaffs_UnlinkObject(existingTarget);
6150 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6152 + return YAFFS_FAIL;
6155 +/*------------------------- Block Management and Page Allocation ----------------*/
6157 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
6159 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6161 + dev->blockInfo = NULL;
6162 + dev->chunkBits = NULL;
6164 + dev->allocationBlock = -1; /* force it to get a new one */
6166 + /* If the first allocation strategy fails, thry the alternate one */
6167 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6168 + if(!dev->blockInfo){
6169 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6170 + dev->blockInfoAlt = 1;
6173 + dev->blockInfoAlt = 0;
6175 + if(dev->blockInfo){
6177 + /* Set up dynamic blockinfo stuff. */
6178 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6179 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6180 + if(!dev->chunkBits){
6181 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6182 + dev->chunkBitsAlt = 1;
6185 + dev->chunkBitsAlt = 0;
6188 + if (dev->blockInfo && dev->chunkBits) {
6189 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6190 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6194 + return YAFFS_FAIL;
6198 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
6200 + if(dev->blockInfoAlt && dev->blockInfo)
6201 + YFREE_ALT(dev->blockInfo);
6202 + else if(dev->blockInfo)
6203 + YFREE(dev->blockInfo);
6205 + dev->blockInfoAlt = 0;
6207 + dev->blockInfo = NULL;
6209 + if(dev->chunkBitsAlt && dev->chunkBits)
6210 + YFREE_ALT(dev->chunkBits);
6211 + else if(dev->chunkBits)
6212 + YFREE(dev->chunkBits);
6213 + dev->chunkBitsAlt = 0;
6214 + dev->chunkBits = NULL;
6217 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
6218 + yaffs_BlockInfo * bi)
6222 + yaffs_BlockInfo *b;
6224 + if (!dev->isYaffs2)
6225 + return 1; /* disqualification only applies to yaffs2. */
6227 + if (!bi->hasShrinkHeader)
6228 + return 1; /* can gc */
6230 + /* Find the oldest dirty sequence number if we don't know it and save it
6231 + * so we don't have to keep recomputing it.
6233 + if (!dev->oldestDirtySequence) {
6234 + seq = dev->sequenceNumber;
6236 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6238 + b = yaffs_GetBlockInfo(dev, i);
6239 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6240 + (b->pagesInUse - b->softDeletions) <
6241 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6242 + seq = b->sequenceNumber;
6245 + dev->oldestDirtySequence = seq;
6248 + /* Can't do gc of this block if there are any blocks older than this one that have
6249 + * discarded pages.
6251 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6255 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6256 + * for garbage collection.
6259 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
6263 + int b = dev->currentDirtyChecker;
6267 + int dirtiest = -1;
6268 + int pagesInUse = 0;
6269 + int prioritised=0;
6270 + yaffs_BlockInfo *bi;
6271 + int pendingPrioritisedExist = 0;
6273 + /* First let's see if we need to grab a prioritised block */
6274 + if(dev->hasPendingPrioritisedGCs){
6275 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
6277 + bi = yaffs_GetBlockInfo(dev, i);
6278 + //yaffs_VerifyBlock(dev,bi,i);
6280 + if(bi->gcPrioritise) {
6281 + pendingPrioritisedExist = 1;
6282 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6283 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
6284 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6287 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6292 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
6293 + dev->hasPendingPrioritisedGCs = 0;
6296 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6298 + * else (we're doing a leasurely gc), then we only bother to do this if the
6299 + * block has only a few pages in use.
6302 + dev->nonAggressiveSkip--;
6304 + if (!aggressive && (dev->nonAggressiveSkip > 0)) {
6310 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6314 + dev->internalEndBlock - dev->internalStartBlock + 1;
6317 + dev->internalEndBlock - dev->internalStartBlock + 1;
6318 + iterations = iterations / 16;
6319 + if (iterations > 200) {
6324 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6326 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6327 + b = dev->internalStartBlock;
6330 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6331 + T(YAFFS_TRACE_ERROR,
6332 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6336 + bi = yaffs_GetBlockInfo(dev, b);
6339 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
6346 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6347 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6348 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6350 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6354 + dev->currentDirtyChecker = b;
6356 + if (dirtiest > 0) {
6358 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6359 + dev->nChunksPerBlock - pagesInUse,prioritised));
6362 + dev->oldestDirtySequence = 0;
6364 + if (dirtiest > 0) {
6365 + dev->nonAggressiveSkip = 4;
6371 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
6373 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6377 + /* If the block is still healthy erase it and mark as clean.
6378 + * If the block has had a data failure, then retire it.
6381 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6382 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6383 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6385 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6387 + if (!bi->needsRetiring) {
6388 + yaffs_InvalidateCheckpoint(dev);
6389 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6391 + dev->nErasureFailures++;
6392 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6393 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6398 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6400 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6401 + if (!yaffs_CheckChunkErased
6402 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6403 + T(YAFFS_TRACE_ERROR,
6405 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6406 + TENDSTR), blockNo, i));
6412 + /* Clean it up... */
6413 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6414 + dev->nErasedBlocks++;
6415 + bi->pagesInUse = 0;
6416 + bi->softDeletions = 0;
6417 + bi->hasShrinkHeader = 0;
6418 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6419 + bi->gcPrioritise = 0;
6420 + yaffs_ClearChunkBits(dev, blockNo);
6422 + T(YAFFS_TRACE_ERASE,
6423 + (TSTR("Erased block %d" TENDSTR), blockNo));
6425 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6427 + yaffs_RetireBlock(dev, blockNo);
6428 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6429 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6433 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
6437 + yaffs_BlockInfo *bi;
6439 + if (dev->nErasedBlocks < 1) {
6440 + /* Hoosterman we've got a problem.
6441 + * Can't get space to gc
6443 + T(YAFFS_TRACE_ERROR,
6444 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
6449 + /* Find an empty block. */
6451 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6452 + dev->allocationBlockFinder++;
6453 + if (dev->allocationBlockFinder < dev->internalStartBlock
6454 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6455 + dev->allocationBlockFinder = dev->internalStartBlock;
6458 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6460 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6461 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6462 + dev->sequenceNumber++;
6463 + bi->sequenceNumber = dev->sequenceNumber;
6464 + dev->nErasedBlocks--;
6465 + T(YAFFS_TRACE_ALLOCATE,
6466 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6467 + dev->allocationBlockFinder, dev->sequenceNumber,
6468 + dev->nErasedBlocks));
6469 + return dev->allocationBlockFinder;
6473 + T(YAFFS_TRACE_ALWAYS,
6475 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
6476 + TENDSTR), dev->nErasedBlocks));
6482 +// Check if there's space to allocate...
6483 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6484 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
6486 + int reservedChunks;
6487 + int reservedBlocks = dev->nReservedBlocks;
6488 + int checkpointBlocks;
6490 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
6491 + if(checkpointBlocks < 0)
6492 + checkpointBlocks = 0;
6494 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6496 + return (dev->nFreeChunks > reservedChunks);
6499 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
6502 + yaffs_BlockInfo *bi;
6504 + if (dev->allocationBlock < 0) {
6505 + /* Get next block to allocate off */
6506 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6507 + dev->allocationPage = 0;
6510 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6511 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6515 + if (dev->nErasedBlocks < dev->nReservedBlocks
6516 + && dev->allocationPage == 0) {
6517 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6520 + /* Next page please.... */
6521 + if (dev->allocationBlock >= 0) {
6522 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6524 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6525 + dev->allocationPage;
6527 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6528 + dev->allocationPage);
6530 + dev->allocationPage++;
6532 + dev->nFreeChunks--;
6534 + /* If the block is full set the state to full */
6535 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6536 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6537 + dev->allocationBlock = -1;
6541 + *blockUsedPtr = bi;
6546 + T(YAFFS_TRACE_ERROR,
6547 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6552 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
6556 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6558 + if (dev->allocationBlock > 0) {
6559 + n += (dev->nChunksPerBlock - dev->allocationPage);
6566 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
6572 + int retVal = YAFFS_OK;
6575 + int isCheckpointBlock;
6576 + int matchingChunk;
6578 + int chunksBefore = yaffs_GetErasedChunks(dev);
6581 + yaffs_ExtendedTags tags;
6583 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6585 + yaffs_Object *object;
6587 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6589 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6591 + T(YAFFS_TRACE_TRACING,
6592 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
6593 + bi->pagesInUse, bi->hasShrinkHeader));
6595 + /*yaffs_VerifyFreeChunks(dev); */
6597 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6599 + /* Take off the number of soft deleted entries because
6600 + * they're going to get really deleted during GC.
6602 + dev->nFreeChunks -= bi->softDeletions;
6604 + dev->isDoingGC = 1;
6606 + if (isCheckpointBlock ||
6607 + !yaffs_StillSomeChunkBits(dev, block)) {
6608 + T(YAFFS_TRACE_TRACING,
6610 + ("Collecting block %d that has no chunks in use" TENDSTR),
6612 + yaffs_BlockBecameDirty(dev, block);
6615 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6617 + yaffs_VerifyBlock(dev,bi,block);
6619 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
6620 + chunkInBlock < dev->nChunksPerBlock
6621 + && yaffs_StillSomeChunkBits(dev, block);
6622 + chunkInBlock++, oldChunk++) {
6623 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
6625 + /* This page is in use and might need to be copied off */
6629 + yaffs_InitialiseTags(&tags);
6631 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
6635 + yaffs_FindObjectByNumber(dev,
6638 + T(YAFFS_TRACE_GC_DETAIL,
6640 + ("Collecting page %d, %d %d %d " TENDSTR),
6641 + chunkInBlock, tags.objectId, tags.chunkId,
6644 + if(object && !yaffs_SkipVerification(dev)){
6645 + if(tags.chunkId == 0)
6646 + matchingChunk = object->chunkId;
6647 + else if(object->softDeleted)
6648 + matchingChunk = oldChunk; /* Defeat the test */
6650 + matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
6652 + if(oldChunk != matchingChunk)
6653 + T(YAFFS_TRACE_ERROR,
6654 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
6655 + oldChunk,matchingChunk,tags.objectId, tags.chunkId));
6660 + T(YAFFS_TRACE_ERROR,
6662 + ("page %d in gc has no object: %d %d %d "
6663 + TENDSTR), oldChunk,
6664 + tags.objectId, tags.chunkId, tags.byteCount));
6667 + if (object && object->deleted
6668 + && tags.chunkId != 0) {
6669 + /* Data chunk in a deleted file, throw it away
6670 + * It's a soft deleted data chunk,
6671 + * No need to copy this, just forget about it and
6672 + * fix up the object.
6675 + object->nDataChunks--;
6677 + if (object->nDataChunks <= 0) {
6678 + /* remeber to clean up the object */
6679 + dev->gcCleanupList[cleanups] =
6685 + /* Todo object && object->deleted && object->nDataChunks == 0 */
6687 + /* Deleted object header with no data chunks.
6688 + * Can be discarded and the file deleted.
6690 + object->chunkId = 0;
6691 + yaffs_FreeTnode(object->myDev,
6694 + object->variant.fileVariant.top = NULL;
6695 + yaffs_DoGenericObjectDeletion(object);
6697 + } else if (object) {
6698 + /* It's either a data chunk in a live file or
6699 + * an ObjectHeader, so we're interested in it.
6700 + * NB Need to keep the ObjectHeaders of deleted files
6701 + * until the whole file has been deleted off
6703 + tags.serialNumber++;
6707 + if (tags.chunkId == 0) {
6708 + /* It is an object Id,
6709 + * We need to nuke the shrinkheader flags first
6710 + * We no longer want the shrinkHeader flag since its work is done
6711 + * and if it is left in place it will mess up scanning.
6712 + * Also, clear out any shadowing stuff
6715 + yaffs_ObjectHeader *oh;
6716 + oh = (yaffs_ObjectHeader *)buffer;
6718 + oh->shadowsObject = -1;
6719 + tags.extraShadows = 0;
6720 + tags.extraIsShrinkHeader = 0;
6722 + yaffs_VerifyObjectHeader(object,oh,&tags,1);
6726 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
6728 + if (newChunk < 0) {
6729 + retVal = YAFFS_FAIL;
6732 + /* Ok, now fix up the Tnodes etc. */
6734 + if (tags.chunkId == 0) {
6735 + /* It's a header */
6736 + object->chunkId = newChunk;
6737 + object->serial = tags.serialNumber;
6739 + /* It's a data chunk */
6740 + yaffs_PutChunkIntoFile
6748 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
6753 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6756 + /* Do any required cleanups */
6757 + for (i = 0; i < cleanups; i++) {
6758 + /* Time to delete the file too */
6760 + yaffs_FindObjectByNumber(dev,
6761 + dev->gcCleanupList[i]);
6763 + yaffs_FreeTnode(dev,
6764 + object->variant.fileVariant.
6766 + object->variant.fileVariant.top = NULL;
6769 + ("yaffs: About to finally delete object %d"
6770 + TENDSTR), object->objectId));
6771 + yaffs_DoGenericObjectDeletion(object);
6772 + object->myDev->nDeletedFiles--;
6779 + yaffs_VerifyCollectedBlock(dev,bi,block);
6781 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
6784 + ("gc did not increase free chunks before %d after %d"
6785 + TENDSTR), chunksBefore, chunksAfter));
6788 + dev->isDoingGC = 0;
6793 +/* New garbage collector
6794 + * If we're very low on erased blocks then we do aggressive garbage collection
6795 + * otherwise we do "leasurely" garbage collection.
6796 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
6797 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
6799 + * The idea is to help clear out space in a more spread-out manner.
6800 + * Dunno if it really does anything useful.
6802 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
6806 + int gcOk = YAFFS_OK;
6809 + int checkpointBlockAdjust;
6811 + if (dev->isDoingGC) {
6812 + /* Bail out so we don't get recursive gc */
6816 + /* This loop should pass the first time.
6817 + * We'll only see looping here if the erase of the collected block fails.
6823 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
6824 + if(checkpointBlockAdjust < 0)
6825 + checkpointBlockAdjust = 0;
6827 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
6828 + /* We need a block soon...*/
6831 + /* We're in no hurry */
6835 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6838 + dev->garbageCollections++;
6839 + if (!aggressive) {
6840 + dev->passiveGarbageCollections++;
6845 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6846 + dev->nErasedBlocks, aggressive));
6848 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6851 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6854 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6855 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6857 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6858 + && (maxTries < 2));
6860 + return aggressive ? gcOk : YAFFS_OK;
6863 +/*------------------------- TAGS --------------------------------*/
6865 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6866 + int chunkInObject)
6868 + return (tags->chunkId == chunkInObject &&
6869 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6874 +/*-------------------- Data file manipulation -----------------*/
6876 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6877 + yaffs_ExtendedTags * tags)
6879 + /*Get the Tnode, then get the level 0 offset chunk offset */
6881 + int theChunk = -1;
6882 + yaffs_ExtendedTags localTags;
6885 + yaffs_Device *dev = in->myDev;
6888 + /* Passed a NULL, so use our own tags space */
6889 + tags = &localTags;
6892 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6895 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6898 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6904 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6905 + yaffs_ExtendedTags * tags)
6907 + /* Get the Tnode, then get the level 0 offset chunk offset */
6909 + int theChunk = -1;
6910 + yaffs_ExtendedTags localTags;
6912 + yaffs_Device *dev = in->myDev;
6916 + /* Passed a NULL, so use our own tags space */
6917 + tags = &localTags;
6920 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6924 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6927 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6930 + /* Delete the entry in the filestructure (if found) */
6931 + if (retVal != -1) {
6932 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6935 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6938 + if (retVal == -1) {
6939 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6944 +#ifdef YAFFS_PARANOID
6946 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6954 + yaffs_Tags localTags;
6955 + yaffs_Tags *tags = &localTags;
6959 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6960 + /* T(("Object not a file\n")); */
6961 + return YAFFS_FAIL;
6964 + objId = in->objectId;
6965 + fSize = in->variant.fileVariant.fileSize;
6967 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6969 + for (chunk = 1; chunk <= nChunks; chunk++) {
6970 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6975 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6977 + if (yaffs_CheckChunkBits
6978 + (dev, theChunk / dev->nChunksPerBlock,
6979 + theChunk % dev->nChunksPerBlock)) {
6981 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6984 + if (yaffs_TagsMatch
6985 + (tags, in->objectId, chunk, chunkDeleted)) {
6995 + /* T(("No level 0 found for %d\n", chunk)); */
6999 + return failed ? YAFFS_FAIL : YAFFS_OK;
7004 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
7005 + int chunkInNAND, int inScan)
7007 + /* NB inScan is zero unless scanning.
7008 + * For forward scanning, inScan is > 0;
7009 + * for backward scanning inScan is < 0
7013 + yaffs_Device *dev = in->myDev;
7014 + int existingChunk;
7015 + yaffs_ExtendedTags existingTags;
7016 + yaffs_ExtendedTags newTags;
7017 + unsigned existingSerial, newSerial;
7019 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
7020 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
7021 + * If it is not during Scanning then something went wrong!
7024 + T(YAFFS_TRACE_ERROR,
7026 + ("yaffs tragedy:attempt to put data chunk into a non-file"
7031 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7035 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7036 + &in->variant.fileVariant,
7040 + return YAFFS_FAIL;
7043 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
7045 + if (inScan != 0) {
7046 + /* If we're scanning then we need to test for duplicates
7047 + * NB This does not need to be efficient since it should only ever
7048 + * happen when the power fails during a write, then only one
7049 + * chunk should ever be affected.
7051 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7052 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7055 + if (existingChunk != 0) {
7056 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7057 + * thus we have to do a FindChunkInFile to get the real chunk id.
7059 + * We have a duplicate now we need to decide which one to use:
7061 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7062 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7063 + * YAFFS1: Get both sets of tags and compare serial numbers.
7067 + /* Only do this for forward scanning */
7068 + yaffs_ReadChunkWithTagsFromNAND(dev,
7072 + /* Do a proper find */
7074 + yaffs_FindChunkInFile(in, chunkInInode,
7078 + if (existingChunk <= 0) {
7079 + /*Hoosterman - how did this happen? */
7081 + T(YAFFS_TRACE_ERROR,
7083 + ("yaffs tragedy: existing chunk < 0 in scan"
7088 + /* NB The deleted flags should be false, otherwise the chunks will
7089 + * not be loaded during a scan
7092 + newSerial = newTags.serialNumber;
7093 + existingSerial = existingTags.serialNumber;
7095 + if ((inScan > 0) &&
7096 + (in->myDev->isYaffs2 ||
7097 + existingChunk <= 0 ||
7098 + ((existingSerial + 1) & 3) == newSerial)) {
7099 + /* Forward scanning.
7101 + * Delete the old one and drop through to update the tnode
7103 + yaffs_DeleteChunk(dev, existingChunk, 1,
7106 + /* Backward scanning or we want to use the existing one
7108 + * Delete the new one and return early so that the tnode isn't changed
7110 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7118 + if (existingChunk == 0) {
7119 + in->nDataChunks++;
7122 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
7127 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
7130 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7132 + if (chunkInNAND >= 0) {
7133 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7136 + T(YAFFS_TRACE_NANDACCESS,
7137 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7139 + /* get sane (zero) data if you read a hole */
7140 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7146 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
7150 + yaffs_ExtendedTags tags;
7151 + yaffs_BlockInfo *bi;
7157 + dev->nDeletions++;
7158 + block = chunkId / dev->nChunksPerBlock;
7159 + page = chunkId % dev->nChunksPerBlock;
7162 + if(!yaffs_CheckChunkBit(dev,block,page))
7163 + T(YAFFS_TRACE_VERIFY,
7164 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7167 + bi = yaffs_GetBlockInfo(dev, block);
7169 + T(YAFFS_TRACE_DELETION,
7170 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7173 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7175 + yaffs_InitialiseTags(&tags);
7177 + tags.chunkDeleted = 1;
7179 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7180 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7182 + dev->nUnmarkedDeletions++;
7185 + /* Pull out of the management area.
7186 + * If the whole block became dirty, this will kick off an erasure.
7188 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7189 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7190 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7191 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7192 + dev->nFreeChunks++;
7194 + yaffs_ClearChunkBit(dev, block, page);
7198 + if (bi->pagesInUse == 0 &&
7199 + !bi->hasShrinkHeader &&
7200 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7201 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7202 + yaffs_BlockBecameDirty(dev, block);
7206 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
7211 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
7212 + const __u8 * buffer, int nBytes,
7215 + /* Find old chunk Need to do this to get serial number
7216 + * Write new one and patch into tree.
7217 + * Invalidate old tags.
7221 + yaffs_ExtendedTags prevTags;
7224 + yaffs_ExtendedTags newTags;
7226 + yaffs_Device *dev = in->myDev;
7228 + yaffs_CheckGarbageCollection(dev);
7230 + /* Get the previous chunk at this location in the file if it exists */
7231 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7233 + /* Set up new tags */
7234 + yaffs_InitialiseTags(&newTags);
7236 + newTags.chunkId = chunkInInode;
7237 + newTags.objectId = in->objectId;
7238 + newTags.serialNumber =
7239 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7240 + newTags.byteCount = nBytes;
7243 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7246 + if (newChunkId >= 0) {
7247 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7249 + if (prevChunkId >= 0) {
7250 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7254 + yaffs_CheckFileSanity(in);
7256 + return newChunkId;
7260 +/* UpdateObjectHeader updates the header on NAND for an object.
7261 + * If name is not NULL, then that new name is used.
7263 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
7264 + int isShrink, int shadows)
7267 + yaffs_BlockInfo *bi;
7269 + yaffs_Device *dev = in->myDev;
7276 + yaffs_ExtendedTags newTags;
7277 + yaffs_ExtendedTags oldTags;
7279 + __u8 *buffer = NULL;
7280 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7282 + yaffs_ObjectHeader *oh = NULL;
7284 + yaffs_strcpy(oldName,"silly old name");
7286 + if (!in->fake || force) {
7288 + yaffs_CheckGarbageCollection(dev);
7289 + yaffs_CheckObjectDetailsLoaded(in);
7291 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7292 + oh = (yaffs_ObjectHeader *) buffer;
7294 + prevChunkId = in->chunkId;
7296 + if (prevChunkId >= 0) {
7297 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7298 + buffer, &oldTags);
7300 + yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
7302 + memcpy(oldName, oh->name, sizeof(oh->name));
7305 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7307 + oh->type = in->variantType;
7308 + oh->yst_mode = in->yst_mode;
7309 + oh->shadowsObject = shadows;
7311 +#ifdef CONFIG_YAFFS_WINCE
7312 + oh->win_atime[0] = in->win_atime[0];
7313 + oh->win_ctime[0] = in->win_ctime[0];
7314 + oh->win_mtime[0] = in->win_mtime[0];
7315 + oh->win_atime[1] = in->win_atime[1];
7316 + oh->win_ctime[1] = in->win_ctime[1];
7317 + oh->win_mtime[1] = in->win_mtime[1];
7319 + oh->yst_uid = in->yst_uid;
7320 + oh->yst_gid = in->yst_gid;
7321 + oh->yst_atime = in->yst_atime;
7322 + oh->yst_mtime = in->yst_mtime;
7323 + oh->yst_ctime = in->yst_ctime;
7324 + oh->yst_rdev = in->yst_rdev;
7327 + oh->parentObjectId = in->parent->objectId;
7329 + oh->parentObjectId = 0;
7332 + if (name && *name) {
7333 + memset(oh->name, 0, sizeof(oh->name));
7334 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7335 + } else if (prevChunkId>=0) {
7336 + memcpy(oh->name, oldName, sizeof(oh->name));
7338 + memset(oh->name, 0, sizeof(oh->name));
7341 + oh->isShrink = isShrink;
7343 + switch (in->variantType) {
7344 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7345 + /* Should not happen */
7347 + case YAFFS_OBJECT_TYPE_FILE:
7349 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7350 + || oh->parentObjectId ==
7351 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7352 + fileVariant.fileSize;
7354 + case YAFFS_OBJECT_TYPE_HARDLINK:
7355 + oh->equivalentObjectId =
7356 + in->variant.hardLinkVariant.equivalentObjectId;
7358 + case YAFFS_OBJECT_TYPE_SPECIAL:
7361 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7364 + case YAFFS_OBJECT_TYPE_SYMLINK:
7365 + yaffs_strncpy(oh->alias,
7366 + in->variant.symLinkVariant.alias,
7367 + YAFFS_MAX_ALIAS_LENGTH);
7368 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7373 + yaffs_InitialiseTags(&newTags);
7375 + newTags.chunkId = 0;
7376 + newTags.objectId = in->objectId;
7377 + newTags.serialNumber = in->serial;
7379 + /* Add extra info for file header */
7381 + newTags.extraHeaderInfoAvailable = 1;
7382 + newTags.extraParentObjectId = oh->parentObjectId;
7383 + newTags.extraFileLength = oh->fileSize;
7384 + newTags.extraIsShrinkHeader = oh->isShrink;
7385 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7386 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7387 + newTags.extraObjectType = in->variantType;
7389 + yaffs_VerifyObjectHeader(in,oh,&newTags,1);
7391 + /* Create new chunk in NAND */
7393 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7394 + (prevChunkId >= 0) ? 1 : 0);
7396 + if (newChunkId >= 0) {
7398 + in->chunkId = newChunkId;
7400 + if (prevChunkId >= 0) {
7401 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7405 + if(!yaffs_ObjectHasCachedWriteData(in))
7408 + /* If this was a shrink, then mark the block that the chunk lives on */
7410 + bi = yaffs_GetBlockInfo(in->myDev,
7411 + newChunkId /in->myDev-> nChunksPerBlock);
7412 + bi->hasShrinkHeader = 1;
7417 + retVal = newChunkId;
7422 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7427 +/*------------------------ Short Operations Cache ----------------------------------------
7428 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7429 + * reads might be short sequential reads, and a lot of writes may be short
7430 + * sequential writes. eg. scanning/writing a jpeg file.
7431 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7432 + * with dumb-as-a-rock code.
7433 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7436 + * There are a limited number (~10) of cache chunks per device so that we don't
7437 + * need a very intelligent search.
7440 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7442 + yaffs_Device *dev = obj->myDev;
7444 + yaffs_ChunkCache *cache;
7445 + int nCaches = obj->myDev->nShortOpCaches;
7447 + for(i = 0; i < nCaches; i++){
7448 + cache = &dev->srCache[i];
7449 + if (cache->object == obj &&
7458 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
7460 + yaffs_Device *dev = obj->myDev;
7461 + int lowest = -99; /* Stop compiler whining. */
7463 + yaffs_ChunkCache *cache;
7464 + int chunkWritten = 0;
7465 + int nCaches = obj->myDev->nShortOpCaches;
7467 + if (nCaches > 0) {
7471 + /* Find the dirty cache for this object with the lowest chunk id. */
7472 + for (i = 0; i < nCaches; i++) {
7473 + if (dev->srCache[i].object == obj &&
7474 + dev->srCache[i].dirty) {
7476 + || dev->srCache[i].chunkId <
7478 + cache = &dev->srCache[i];
7479 + lowest = cache->chunkId;
7484 + if (cache && !cache->locked) {
7485 + /* Write it out and free it up */
7488 + yaffs_WriteChunkDataToObject(cache->object,
7494 + cache->object = NULL;
7497 + } while (cache && chunkWritten > 0);
7500 + /* Hoosterman, disk full while writing cache out. */
7501 + T(YAFFS_TRACE_ERROR,
7502 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7509 +/*yaffs_FlushEntireDeviceCache(dev)
7514 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7516 + yaffs_Object *obj;
7517 + int nCaches = dev->nShortOpCaches;
7520 + /* Find a dirty object in the cache and flush it...
7521 + * until there are no further dirty objects.
7525 + for( i = 0; i < nCaches && !obj; i++) {
7526 + if (dev->srCache[i].object &&
7527 + dev->srCache[i].dirty)
7528 + obj = dev->srCache[i].object;
7532 + yaffs_FlushFilesChunkCache(obj);
7539 +/* Grab us a cache chunk for use.
7540 + * First look for an empty one.
7541 + * Then look for the least recently used non-dirty one.
7542 + * Then look for the least recently used dirty one...., flush and look again.
7544 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
7550 + if (dev->nShortOpCaches > 0) {
7551 + for (i = 0; i < dev->nShortOpCaches; i++) {
7552 + if (!dev->srCache[i].object)
7553 + return &dev->srCache[i];
7559 + usage = 0; /* just to stop the compiler grizzling */
7561 + for (i = 0; i < dev->nShortOpCaches; i++) {
7562 + if (!dev->srCache[i].dirty &&
7563 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
7565 + usage = dev->srCache[i].lastUse;
7571 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
7578 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
7580 + yaffs_ChunkCache *cache;
7581 + yaffs_Object *theObj;
7586 + if (dev->nShortOpCaches > 0) {
7587 + /* Try find a non-dirty one... */
7589 + cache = yaffs_GrabChunkCacheWorker(dev);
7592 + /* They were all dirty, find the last recently used object and flush
7593 + * its cache, then find again.
7594 + * NB what's here is not very accurate, we actually flush the object
7595 + * the last recently used page.
7598 + /* With locking we can't assume we can use entry zero */
7605 + for (i = 0; i < dev->nShortOpCaches; i++) {
7606 + if (dev->srCache[i].object &&
7607 + !dev->srCache[i].locked &&
7608 + (dev->srCache[i].lastUse < usage || !cache))
7610 + usage = dev->srCache[i].lastUse;
7611 + theObj = dev->srCache[i].object;
7612 + cache = &dev->srCache[i];
7617 + if (!cache || cache->dirty) {
7618 + /* Flush and try again */
7619 + yaffs_FlushFilesChunkCache(theObj);
7620 + cache = yaffs_GrabChunkCacheWorker(dev);
7630 +/* Find a cached chunk */
7631 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
7634 + yaffs_Device *dev = obj->myDev;
7636 + if (dev->nShortOpCaches > 0) {
7637 + for (i = 0; i < dev->nShortOpCaches; i++) {
7638 + if (dev->srCache[i].object == obj &&
7639 + dev->srCache[i].chunkId == chunkId) {
7642 + return &dev->srCache[i];
7649 +/* Mark the chunk for the least recently used algorithym */
7650 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
7654 + if (dev->nShortOpCaches > 0) {
7655 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
7656 + /* Reset the cache usages */
7658 + for (i = 1; i < dev->nShortOpCaches; i++) {
7659 + dev->srCache[i].lastUse = 0;
7661 + dev->srLastUse = 0;
7666 + cache->lastUse = dev->srLastUse;
7674 +/* Invalidate a single cache page.
7675 + * Do this when a whole page gets written,
7676 + * ie the short cache for this page is no longer valid.
7678 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
7680 + if (object->myDev->nShortOpCaches > 0) {
7681 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
7684 + cache->object = NULL;
7689 +/* Invalidate all the cache pages associated with this object
7690 + * Do this whenever ther file is deleted or resized.
7692 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
7695 + yaffs_Device *dev = in->myDev;
7697 + if (dev->nShortOpCaches > 0) {
7698 + /* Invalidate it. */
7699 + for (i = 0; i < dev->nShortOpCaches; i++) {
7700 + if (dev->srCache[i].object == in) {
7701 + dev->srCache[i].object = NULL;
7707 +/*--------------------- Checkpointing --------------------*/
7710 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
7712 + yaffs_CheckpointValidity cp;
7714 + memset(&cp,0,sizeof(cp));
7716 + cp.structType = sizeof(cp);
7717 + cp.magic = YAFFS_MAGIC;
7718 + cp.version = YAFFS_CHECKPOINT_VERSION;
7719 + cp.head = (head) ? 1 : 0;
7721 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
7725 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
7727 + yaffs_CheckpointValidity cp;
7730 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7733 + ok = (cp.structType == sizeof(cp)) &&
7734 + (cp.magic == YAFFS_MAGIC) &&
7735 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
7736 + (cp.head == ((head) ? 1 : 0));
7737 + return ok ? 1 : 0;
7740 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
7741 + yaffs_Device *dev)
7743 + cp->nErasedBlocks = dev->nErasedBlocks;
7744 + cp->allocationBlock = dev->allocationBlock;
7745 + cp->allocationPage = dev->allocationPage;
7746 + cp->nFreeChunks = dev->nFreeChunks;
7748 + cp->nDeletedFiles = dev->nDeletedFiles;
7749 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
7750 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
7751 + cp->sequenceNumber = dev->sequenceNumber;
7752 + cp->oldestDirtySequence = dev->oldestDirtySequence;
7756 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
7757 + yaffs_CheckpointDevice *cp)
7759 + dev->nErasedBlocks = cp->nErasedBlocks;
7760 + dev->allocationBlock = cp->allocationBlock;
7761 + dev->allocationPage = cp->allocationPage;
7762 + dev->nFreeChunks = cp->nFreeChunks;
7764 + dev->nDeletedFiles = cp->nDeletedFiles;
7765 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
7766 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
7767 + dev->sequenceNumber = cp->sequenceNumber;
7768 + dev->oldestDirtySequence = cp->oldestDirtySequence;
7772 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
7774 + yaffs_CheckpointDevice cp;
7776 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7780 + /* Write device runtime values*/
7781 + yaffs_DeviceToCheckpointDevice(&cp,dev);
7782 + cp.structType = sizeof(cp);
7784 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7786 + /* Write block info */
7788 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7789 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
7792 + /* Write chunk bits */
7794 + nBytes = nBlocks * dev->chunkBitmapStride;
7795 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
7797 + return ok ? 1 : 0;
7801 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
7803 + yaffs_CheckpointDevice cp;
7805 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7809 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7813 + if(cp.structType != sizeof(cp))
7817 + yaffs_CheckpointDeviceToDevice(dev,&cp);
7819 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7821 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
7825 + nBytes = nBlocks * dev->chunkBitmapStride;
7827 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
7829 + return ok ? 1 : 0;
7832 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
7833 + yaffs_Object *obj)
7836 + cp->objectId = obj->objectId;
7837 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
7838 + cp->chunkId = obj->chunkId;
7839 + cp->variantType = obj->variantType;
7840 + cp->deleted = obj->deleted;
7841 + cp->softDeleted = obj->softDeleted;
7842 + cp->unlinked = obj->unlinked;
7843 + cp->fake = obj->fake;
7844 + cp->renameAllowed = obj->renameAllowed;
7845 + cp->unlinkAllowed = obj->unlinkAllowed;
7846 + cp->serial = obj->serial;
7847 + cp->nDataChunks = obj->nDataChunks;
7849 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7850 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7851 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7852 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7855 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7858 + yaffs_Object *parent;
7860 + obj->objectId = cp->objectId;
7863 + parent = yaffs_FindOrCreateObjectByNumber(
7866 + YAFFS_OBJECT_TYPE_DIRECTORY);
7871 + yaffs_AddObjectToDirectory(parent, obj);
7873 + obj->chunkId = cp->chunkId;
7874 + obj->variantType = cp->variantType;
7875 + obj->deleted = cp->deleted;
7876 + obj->softDeleted = cp->softDeleted;
7877 + obj->unlinked = cp->unlinked;
7878 + obj->fake = cp->fake;
7879 + obj->renameAllowed = cp->renameAllowed;
7880 + obj->unlinkAllowed = cp->unlinkAllowed;
7881 + obj->serial = cp->serial;
7882 + obj->nDataChunks = cp->nDataChunks;
7884 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7885 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7886 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7887 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7889 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7890 + obj->lazyLoaded = 1;
7895 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7896 + __u32 level, int chunkOffset)
7899 + yaffs_Device *dev = in->myDev;
7901 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7906 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7907 + if (tn->internal[i]) {
7908 + ok = yaffs_CheckpointTnodeWorker(in,
7911 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7914 + } else if (level == 0) {
7915 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7916 + /* printf("write tnode at %d\n",baseOffset); */
7917 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7919 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7927 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7929 + __u32 endMarker = ~0;
7932 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7933 + ok = yaffs_CheckpointTnodeWorker(obj,
7934 + obj->variant.fileVariant.top,
7935 + obj->variant.fileVariant.topLevel,
7938 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7939 + sizeof(endMarker));
7942 + return ok ? 1 : 0;
7945 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7949 + yaffs_Device *dev = obj->myDev;
7950 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7954 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7956 + while(ok && (~baseChunk)){
7958 + /* Read level 0 tnode */
7961 + /* printf("read tnode at %d\n",baseChunk); */
7962 + tn = yaffs_GetTnodeRaw(dev);
7964 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7965 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7970 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7978 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7982 + T(YAFFS_TRACE_CHECKPOINT,(
7983 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
7984 + nread,baseChunk,ok));
7986 + return ok ? 1 : 0;
7990 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7992 + yaffs_Object *obj;
7993 + yaffs_CheckpointObject cp;
7996 + struct list_head *lh;
7999 + /* Iterate through the objects in each hash entry,
8000 + * dumping them to the checkpointing stream.
8003 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
8004 + list_for_each(lh, &dev->objectBucket[i].list) {
8006 + obj = list_entry(lh, yaffs_Object, hashLink);
8007 + if (!obj->deferedFree) {
8008 + yaffs_ObjectToCheckpointObject(&cp,obj);
8009 + cp.structType = sizeof(cp);
8011 + T(YAFFS_TRACE_CHECKPOINT,(
8012 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
8013 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
8015 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8017 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
8018 + ok = yaffs_WriteCheckpointTnodes(obj);
8025 + /* Dump end of list */
8026 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
8027 + cp.structType = sizeof(cp);
8030 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8032 + return ok ? 1 : 0;
8035 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8037 + yaffs_Object *obj;
8038 + yaffs_CheckpointObject cp;
8041 + yaffs_Object *hardList = NULL;
8043 + while(ok && !done) {
8044 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
8045 + if(cp.structType != sizeof(cp)) {
8046 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
8047 + cp.structType,sizeof(cp),ok));
8051 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8052 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
8054 + if(ok && cp.objectId == ~0)
8057 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
8059 + yaffs_CheckpointObjectToObject(obj,&cp);
8060 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8061 + ok = yaffs_ReadCheckpointTnodes(obj);
8062 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8063 + obj->hardLinks.next =
8064 + (struct list_head *)
8074 + yaffs_HardlinkFixup(dev,hardList);
8076 + return ok ? 1 : 0;
8079 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8081 + __u32 checkpointSum;
8084 + yaffs_GetCheckpointSum(dev,&checkpointSum);
8086 + ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
8094 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8096 + __u32 checkpointSum0;
8097 + __u32 checkpointSum1;
8100 + yaffs_GetCheckpointSum(dev,&checkpointSum0);
8102 + ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8107 + if(checkpointSum0 != checkpointSum1)
8114 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8119 + if(dev->skipCheckpointWrite || !dev->isYaffs2){
8120 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
8125 + ok = yaffs_CheckpointOpen(dev,1);
8128 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8129 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
8132 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
8133 + ok = yaffs_WriteCheckpointDevice(dev);
8136 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
8137 + ok = yaffs_WriteCheckpointObjects(dev);
8140 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8141 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
8145 + ok = yaffs_WriteCheckpointSum(dev);
8149 + if(!yaffs_CheckpointClose(dev))
8153 + dev->isCheckpointed = 1;
8155 + dev->isCheckpointed = 0;
8157 + return dev->isCheckpointed;
8160 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8164 + if(dev->skipCheckpointRead || !dev->isYaffs2){
8165 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
8170 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
8173 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8174 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
8177 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
8178 + ok = yaffs_ReadCheckpointDevice(dev);
8181 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
8182 + ok = yaffs_ReadCheckpointObjects(dev);
8185 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8186 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
8190 + ok = yaffs_ReadCheckpointSum(dev);
8191 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
8194 + if(!yaffs_CheckpointClose(dev))
8198 + dev->isCheckpointed = 1;
8200 + dev->isCheckpointed = 0;
8202 + return ok ? 1 : 0;
8206 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8208 + if(dev->isCheckpointed ||
8209 + dev->blocksInCheckpoint > 0){
8210 + dev->isCheckpointed = 0;
8211 + yaffs_CheckpointInvalidateStream(dev);
8212 + if(dev->superBlock && dev->markSuperBlockDirty)
8213 + dev->markSuperBlockDirty(dev->superBlock);
8218 +int yaffs_CheckpointSave(yaffs_Device *dev)
8221 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8223 + yaffs_VerifyObjects(dev);
8224 + yaffs_VerifyBlocks(dev);
8225 + yaffs_VerifyFreeChunks(dev);
8227 + if(!dev->isCheckpointed) {
8228 + yaffs_InvalidateCheckpoint(dev);
8229 + yaffs_WriteCheckpointData(dev);
8232 + T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8234 + return dev->isCheckpointed;
8237 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8240 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8242 + retval = yaffs_ReadCheckpointData(dev);
8244 + if(dev->isCheckpointed){
8245 + yaffs_VerifyObjects(dev);
8246 + yaffs_VerifyBlocks(dev);
8247 + yaffs_VerifyFreeChunks(dev);
8250 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8255 +/*--------------------- File read/write ------------------------
8256 + * Read and write have very similar structures.
8257 + * In general the read/write has three parts to it
8258 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8259 + * Some complete chunks
8260 + * An incomplete chunk to end off with
8262 + * Curve-balls: the first chunk might also be the last chunk.
8265 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
8274 + yaffs_ChunkCache *cache;
8276 + yaffs_Device *dev;
8281 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8282 + //start = offset % dev->nDataBytesPerChunk;
8283 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8286 + /* OK now check for the curveball where the start and end are in
8289 + if ((start + n) < dev->nDataBytesPerChunk) {
8292 + nToCopy = dev->nDataBytesPerChunk - start;
8295 + cache = yaffs_FindChunkCache(in, chunk);
8297 + /* If the chunk is already in the cache or it is less than a whole chunk
8298 + * then use the cache (if there is caching)
8299 + * else bypass the cache.
8301 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
8302 + if (dev->nShortOpCaches > 0) {
8304 + /* If we can't find the data in the cache, then load it up. */
8307 + cache = yaffs_GrabChunkCache(in->myDev);
8308 + cache->object = in;
8309 + cache->chunkId = chunk;
8311 + cache->locked = 0;
8312 + yaffs_ReadChunkDataFromObject(in, chunk,
8315 + cache->nBytes = 0;
8318 + yaffs_UseChunkCache(dev, cache, 0);
8320 + cache->locked = 1;
8322 +#ifdef CONFIG_YAFFS_WINCE
8323 + yfsd_UnlockYAFFS(TRUE);
8325 + memcpy(buffer, &cache->data[start], nToCopy);
8327 +#ifdef CONFIG_YAFFS_WINCE
8328 + yfsd_LockYAFFS(TRUE);
8330 + cache->locked = 0;
8332 + /* Read into the local buffer then copy..*/
8334 + __u8 *localBuffer =
8335 + yaffs_GetTempBuffer(dev, __LINE__);
8336 + yaffs_ReadChunkDataFromObject(in, chunk,
8338 +#ifdef CONFIG_YAFFS_WINCE
8339 + yfsd_UnlockYAFFS(TRUE);
8341 + memcpy(buffer, &localBuffer[start], nToCopy);
8343 +#ifdef CONFIG_YAFFS_WINCE
8344 + yfsd_LockYAFFS(TRUE);
8346 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8351 +#ifdef CONFIG_YAFFS_WINCE
8352 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8354 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8355 + * This is because we otherwise screw up WinCE's memory mapper
8357 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
8359 +#ifdef CONFIG_YAFFS_WINCE
8360 + yfsd_UnlockYAFFS(TRUE);
8362 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
8364 +#ifdef CONFIG_YAFFS_WINCE
8365 + yfsd_LockYAFFS(TRUE);
8366 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8370 + /* A full chunk. Read directly into the supplied buffer. */
8371 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8376 + offset += nToCopy;
8377 + buffer += nToCopy;
8385 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
8386 + int nBytes, int writeThrough)
8395 + int startOfWrite = offset;
8396 + int chunkWritten = 0;
8399 + yaffs_Device *dev;
8403 + while (n > 0 && chunkWritten >= 0) {
8404 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8405 + //start = offset % dev->nDataBytesPerChunk;
8406 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8409 + /* OK now check for the curveball where the start and end are in
8413 + if ((start + n) < dev->nDataBytesPerChunk) {
8416 + /* Now folks, to calculate how many bytes to write back....
8417 + * If we're overwriting and not writing to then end of file then
8418 + * we need to write back as much as was there before.
8422 + in->variant.fileVariant.fileSize -
8423 + ((chunk - 1) * dev->nDataBytesPerChunk);
8425 + if (nBytesRead > dev->nDataBytesPerChunk) {
8426 + nBytesRead = dev->nDataBytesPerChunk;
8431 + (start + n)) ? nBytesRead : (start + n);
8434 + nToCopy = dev->nDataBytesPerChunk - start;
8435 + nToWriteBack = dev->nDataBytesPerChunk;
8438 + if (nToCopy != dev->nDataBytesPerChunk) {
8439 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
8440 + if (dev->nShortOpCaches > 0) {
8441 + yaffs_ChunkCache *cache;
8442 + /* If we can't find the data in the cache, then load the cache */
8443 + cache = yaffs_FindChunkCache(in, chunk);
8446 + && yaffs_CheckSpaceForAllocation(in->
8448 + cache = yaffs_GrabChunkCache(in->myDev);
8449 + cache->object = in;
8450 + cache->chunkId = chunk;
8452 + cache->locked = 0;
8453 + yaffs_ReadChunkDataFromObject(in, chunk,
8459 + !yaffs_CheckSpaceForAllocation(in->myDev)){
8460 + /* Drop the cache if it was a read cache item and
8461 + * no space check has been made for it.
8467 + yaffs_UseChunkCache(dev, cache, 1);
8468 + cache->locked = 1;
8469 +#ifdef CONFIG_YAFFS_WINCE
8470 + yfsd_UnlockYAFFS(TRUE);
8473 + memcpy(&cache->data[start], buffer,
8476 +#ifdef CONFIG_YAFFS_WINCE
8477 + yfsd_LockYAFFS(TRUE);
8479 + cache->locked = 0;
8480 + cache->nBytes = nToWriteBack;
8482 + if (writeThrough) {
8484 + yaffs_WriteChunkDataToObject
8487 + cache->data, cache->nBytes,
8493 + chunkWritten = -1; /* fail the write */
8496 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8497 + * Read into the local buffer then copy, then copy over and write back.
8500 + __u8 *localBuffer =
8501 + yaffs_GetTempBuffer(dev, __LINE__);
8503 + yaffs_ReadChunkDataFromObject(in, chunk,
8506 +#ifdef CONFIG_YAFFS_WINCE
8507 + yfsd_UnlockYAFFS(TRUE);
8510 + memcpy(&localBuffer[start], buffer, nToCopy);
8512 +#ifdef CONFIG_YAFFS_WINCE
8513 + yfsd_LockYAFFS(TRUE);
8516 + yaffs_WriteChunkDataToObject(in, chunk,
8521 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8528 +#ifdef CONFIG_YAFFS_WINCE
8529 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8530 + * This is because we otherwise screw up WinCE's memory mapper
8532 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8533 +#ifdef CONFIG_YAFFS_WINCE
8534 + yfsd_UnlockYAFFS(TRUE);
8536 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
8537 +#ifdef CONFIG_YAFFS_WINCE
8538 + yfsd_LockYAFFS(TRUE);
8541 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
8542 + dev->nDataBytesPerChunk,
8544 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8546 + /* A full chunk. Write directly from the supplied buffer. */
8548 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8549 + dev->nDataBytesPerChunk,
8552 + /* Since we've overwritten the cached data, we better invalidate it. */
8553 + yaffs_InvalidateChunkCache(in, chunk);
8556 + if (chunkWritten >= 0) {
8558 + offset += nToCopy;
8559 + buffer += nToCopy;
8565 + /* Update file object */
8567 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
8568 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8577 +/* ---------------------- File resizing stuff ------------------ */
8579 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
8582 + yaffs_Device *dev = in->myDev;
8583 + int oldFileSize = in->variant.fileVariant.fileSize;
8585 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8587 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8588 + dev->nDataBytesPerChunk;
8592 + /* Delete backwards so that we don't end up with holes if
8593 + * power is lost part-way through the operation.
8595 + for (i = lastDel; i >= startDel; i--) {
8596 + /* NB this could be optimised somewhat,
8597 + * eg. could retrieve the tags and write them without
8598 + * using yaffs_DeleteChunk
8601 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8602 + if (chunkId > 0) {
8604 + (dev->internalStartBlock * dev->nChunksPerBlock)
8606 + ((dev->internalEndBlock +
8607 + 1) * dev->nChunksPerBlock)) {
8608 + T(YAFFS_TRACE_ALWAYS,
8609 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8612 + in->nDataChunks--;
8613 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8620 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
8623 + int oldFileSize = in->variant.fileVariant.fileSize;
8624 + int newSizeOfPartialChunk;
8625 + int newFullChunks;
8627 + yaffs_Device *dev = in->myDev;
8629 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8631 + yaffs_FlushFilesChunkCache(in);
8632 + yaffs_InvalidateWholeChunkCache(in);
8634 + yaffs_CheckGarbageCollection(dev);
8636 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8637 + return yaffs_GetFileSize(in);
8640 + if (newSize == oldFileSize) {
8641 + return oldFileSize;
8644 + if (newSize < oldFileSize) {
8646 + yaffs_PruneResizedChunks(in, newSize);
8648 + if (newSizeOfPartialChunk != 0) {
8649 + int lastChunk = 1 + newFullChunks;
8651 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8653 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8654 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8657 + memset(localBuffer + newSizeOfPartialChunk, 0,
8658 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8660 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8661 + newSizeOfPartialChunk, 1);
8663 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8666 + in->variant.fileVariant.fileSize = newSize;
8668 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
8670 + /* newsSize > oldFileSize */
8671 + in->variant.fileVariant.fileSize = newSize;
8676 + /* Write a new object header.
8677 + * show we've shrunk the file, if need be
8678 + * Do this only if the file is not in the deleted directories.
8680 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
8681 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
8682 + yaffs_UpdateObjectHeader(in, NULL, 0,
8683 + (newSize < oldFileSize) ? 1 : 0, 0);
8689 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
8691 + obj = yaffs_GetEquivalentObject(obj);
8693 + switch (obj->variantType) {
8694 + case YAFFS_OBJECT_TYPE_FILE:
8695 + return obj->variant.fileVariant.fileSize;
8696 + case YAFFS_OBJECT_TYPE_SYMLINK:
8697 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
8705 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
8709 + yaffs_FlushFilesChunkCache(in);
8711 +#ifdef CONFIG_YAFFS_WINCE
8712 + yfsd_WinFileTimeNow(in->win_mtime);
8715 + in->yst_mtime = Y_CURRENT_TIME;
8721 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
8722 + 0) ? YAFFS_OK : YAFFS_FAIL;
8724 + retVal = YAFFS_OK;
8731 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
8734 + /* First off, invalidate the file's data in the cache, without flushing. */
8735 + yaffs_InvalidateWholeChunkCache(in);
8737 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
8738 + /* Move to the unlinked directory so we have a record that it was deleted. */
8739 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
8743 + yaffs_RemoveObjectFromDirectory(in);
8744 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
8747 + yaffs_FreeObject(in);
8752 +/* yaffs_DeleteFile deletes the whole file data
8753 + * and the inode associated with the file.
8754 + * It does not delete the links associated with the file.
8756 +static int yaffs_UnlinkFile(yaffs_Object * in)
8760 + int immediateDeletion = 0;
8764 + if (!in->myInode) {
8765 + immediateDeletion = 1;
8769 + if (in->inUse <= 0) {
8770 + immediateDeletion = 1;
8774 + if (immediateDeletion) {
8776 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
8778 + T(YAFFS_TRACE_TRACING,
8779 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
8782 + in->myDev->nDeletedFiles++;
8783 + if (0 && in->myDev->isYaffs2) {
8784 + yaffs_ResizeFile(in, 0);
8786 + yaffs_SoftDeleteFile(in);
8789 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
8790 + "unlinked", 0, 0);
8797 +int yaffs_DeleteFile(yaffs_Object * in)
8799 + int retVal = YAFFS_OK;
8801 + if (in->nDataChunks > 0) {
8802 + /* Use soft deletion if there is data in the file */
8803 + if (!in->unlinked) {
8804 + retVal = yaffs_UnlinkFile(in);
8806 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
8808 + in->myDev->nDeletedFiles++;
8809 + yaffs_SoftDeleteFile(in);
8811 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
8813 + /* The file has no data chunks so we toss it immediately */
8814 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
8815 + in->variant.fileVariant.top = NULL;
8816 + yaffs_DoGenericObjectDeletion(in);
8822 +static int yaffs_DeleteDirectory(yaffs_Object * in)
8824 + /* First check that the directory is empty. */
8825 + if (list_empty(&in->variant.directoryVariant.children)) {
8826 + return yaffs_DoGenericObjectDeletion(in);
8829 + return YAFFS_FAIL;
8833 +static int yaffs_DeleteSymLink(yaffs_Object * in)
8835 + YFREE(in->variant.symLinkVariant.alias);
8837 + return yaffs_DoGenericObjectDeletion(in);
8840 +static int yaffs_DeleteHardLink(yaffs_Object * in)
8842 + /* remove this hardlink from the list assocaited with the equivalent
8845 + list_del(&in->hardLinks);
8846 + return yaffs_DoGenericObjectDeletion(in);
8849 +static void yaffs_DestroyObject(yaffs_Object * obj)
8851 + switch (obj->variantType) {
8852 + case YAFFS_OBJECT_TYPE_FILE:
8853 + yaffs_DeleteFile(obj);
8855 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8856 + yaffs_DeleteDirectory(obj);
8858 + case YAFFS_OBJECT_TYPE_SYMLINK:
8859 + yaffs_DeleteSymLink(obj);
8861 + case YAFFS_OBJECT_TYPE_HARDLINK:
8862 + yaffs_DeleteHardLink(obj);
8864 + case YAFFS_OBJECT_TYPE_SPECIAL:
8865 + yaffs_DoGenericObjectDeletion(obj);
8867 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8868 + break; /* should not happen. */
8872 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
8875 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8876 + return yaffs_DeleteHardLink(obj);
8877 + } else if (!list_empty(&obj->hardLinks)) {
8878 + /* Curve ball: We're unlinking an object that has a hardlink.
8880 + * This problem arises because we are not strictly following
8881 + * The Linux link/inode model.
8883 + * We can't really delete the object.
8884 + * Instead, we do the following:
8885 + * - Select a hardlink.
8886 + * - Unhook it from the hard links
8887 + * - Unhook it from its parent directory (so that the rename can work)
8888 + * - Rename the object to the hardlink's name.
8889 + * - Delete the hardlink
8894 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
8896 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
8898 + list_del_init(&hl->hardLinks);
8899 + list_del_init(&hl->siblings);
8901 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
8903 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
8905 + if (retVal == YAFFS_OK) {
8906 + retVal = yaffs_DoGenericObjectDeletion(hl);
8911 + switch (obj->variantType) {
8912 + case YAFFS_OBJECT_TYPE_FILE:
8913 + return yaffs_UnlinkFile(obj);
8915 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8916 + return yaffs_DeleteDirectory(obj);
8918 + case YAFFS_OBJECT_TYPE_SYMLINK:
8919 + return yaffs_DeleteSymLink(obj);
8921 + case YAFFS_OBJECT_TYPE_SPECIAL:
8922 + return yaffs_DoGenericObjectDeletion(obj);
8924 + case YAFFS_OBJECT_TYPE_HARDLINK:
8925 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8927 + return YAFFS_FAIL;
8933 +static int yaffs_UnlinkObject( yaffs_Object *obj)
8936 + if (obj && obj->unlinkAllowed) {
8937 + return yaffs_UnlinkWorker(obj);
8940 + return YAFFS_FAIL;
8943 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8945 + yaffs_Object *obj;
8947 + obj = yaffs_FindObjectByName(dir, name);
8948 + return yaffs_UnlinkObject(obj);
8951 +/*----------------------- Initialisation Scanning ---------------------- */
8953 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8954 + int backwardScanning)
8956 + yaffs_Object *obj;
8958 + if (!backwardScanning) {
8959 + /* Handle YAFFS1 forward scanning case
8960 + * For YAFFS1 we always do the deletion
8964 + /* Handle YAFFS2 case (backward scanning)
8965 + * If the shadowed object exists then ignore.
8967 + if (yaffs_FindObjectByNumber(dev, objId)) {
8972 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8973 + * We put it in unlinked dir to be cleaned up after the scanning
8976 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8977 + YAFFS_OBJECT_TYPE_FILE);
8978 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8979 + obj->variant.fileVariant.shrinkSize = 0;
8980 + obj->valid = 1; /* So that we don't read any other info for this file */
8987 +} yaffs_BlockIndex;
8990 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8995 + while (hardList) {
8997 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8999 + in = yaffs_FindObjectByNumber(dev,
9000 + hl->variant.hardLinkVariant.
9001 + equivalentObjectId);
9004 + /* Add the hardlink pointers */
9005 + hl->variant.hardLinkVariant.equivalentObject = in;
9006 + list_add(&hl->hardLinks, &in->hardLinks);
9008 + /* Todo Need to report/handle this better.
9009 + * Got a problem... hardlink to a non-existant object
9011 + hl->variant.hardLinkVariant.equivalentObject = NULL;
9012 + INIT_LIST_HEAD(&hl->hardLinks);
9024 +static int ybicmp(const void *a, const void *b){
9025 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
9026 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
9027 + register int ablock = ((yaffs_BlockIndex *)a)->block;
9028 + register int bblock = ((yaffs_BlockIndex *)b)->block;
9029 + if( aseq == bseq )
9030 + return ablock - bblock;
9032 + return aseq - bseq;
9036 +static int yaffs_Scan(yaffs_Device * dev)
9038 + yaffs_ExtendedTags tags;
9040 + int blockIterator;
9041 + int startIterator;
9043 + int nBlocksToScan = 0;
9049 + yaffs_BlockState state;
9050 + yaffs_Object *hardList = NULL;
9051 + yaffs_BlockInfo *bi;
9052 + int sequenceNumber;
9053 + yaffs_ObjectHeader *oh;
9055 + yaffs_Object *parent;
9056 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9058 + int alloc_failed = 0;
9063 + yaffs_BlockIndex *blockIndex = NULL;
9065 + if (dev->isYaffs2) {
9066 + T(YAFFS_TRACE_SCAN,
9067 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
9068 + return YAFFS_FAIL;
9071 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
9073 + T(YAFFS_TRACE_SCAN,
9074 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9075 + dev->internalStartBlock, dev->internalEndBlock));
9077 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9079 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9081 + if (dev->isYaffs2) {
9082 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9084 + return YAFFS_FAIL;
9087 + /* Scan all the blocks to determine their state */
9088 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9089 + bi = yaffs_GetBlockInfo(dev, blk);
9090 + yaffs_ClearChunkBits(dev, blk);
9091 + bi->pagesInUse = 0;
9092 + bi->softDeletions = 0;
9094 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9096 + bi->blockState = state;
9097 + bi->sequenceNumber = sequenceNumber;
9099 + T(YAFFS_TRACE_SCAN_DEBUG,
9100 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9101 + state, sequenceNumber));
9103 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9104 + T(YAFFS_TRACE_BAD_BLOCKS,
9105 + (TSTR("block %d is bad" TENDSTR), blk));
9106 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9107 + T(YAFFS_TRACE_SCAN_DEBUG,
9108 + (TSTR("Block empty " TENDSTR)));
9109 + dev->nErasedBlocks++;
9110 + dev->nFreeChunks += dev->nChunksPerBlock;
9111 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9113 + /* Determine the highest sequence number */
9114 + if (dev->isYaffs2 &&
9115 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9116 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9118 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9119 + blockIndex[nBlocksToScan].block = blk;
9123 + if (sequenceNumber >= dev->sequenceNumber) {
9124 + dev->sequenceNumber = sequenceNumber;
9126 + } else if (dev->isYaffs2) {
9127 + /* TODO: Nasty sequence number! */
9128 + T(YAFFS_TRACE_SCAN,
9130 + ("Block scanning block %d has bad sequence number %d"
9131 + TENDSTR), blk, sequenceNumber));
9137 + /* Sort the blocks
9138 + * Dungy old bubble sort for now...
9140 + if (dev->isYaffs2) {
9141 + yaffs_BlockIndex temp;
9145 + for (i = 0; i < nBlocksToScan; i++)
9146 + for (j = i + 1; j < nBlocksToScan; j++)
9147 + if (blockIndex[i].seq > blockIndex[j].seq) {
9148 + temp = blockIndex[j];
9149 + blockIndex[j] = blockIndex[i];
9150 + blockIndex[i] = temp;
9154 + /* Now scan the blocks looking at the data. */
9155 + if (dev->isYaffs2) {
9156 + startIterator = 0;
9157 + endIterator = nBlocksToScan - 1;
9158 + T(YAFFS_TRACE_SCAN_DEBUG,
9159 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9161 + startIterator = dev->internalStartBlock;
9162 + endIterator = dev->internalEndBlock;
9165 + /* For each block.... */
9166 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9167 + blockIterator++) {
9169 + if (dev->isYaffs2) {
9170 + /* get the block to scan in the correct order */
9171 + blk = blockIndex[blockIterator].block;
9173 + blk = blockIterator;
9176 + bi = yaffs_GetBlockInfo(dev, blk);
9177 + state = bi->blockState;
9181 + /* For each chunk in each block that needs scanning....*/
9182 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9183 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9184 + /* Read the tags and decide what to do */
9185 + chunk = blk * dev->nChunksPerBlock + c;
9187 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9190 + /* Let's have a good look at this chunk... */
9192 + if (!dev->isYaffs2 && tags.chunkDeleted) {
9197 + dev->nFreeChunks++;
9198 + /*T((" %d %d deleted\n",blk,c)); */
9199 + } else if (!tags.chunkUsed) {
9200 + /* An unassigned chunk in the block
9201 + * This means that either the block is empty or
9202 + * this is the one being allocated from
9206 + /* We're looking at the first chunk in the block so the block is unused */
9207 + state = YAFFS_BLOCK_STATE_EMPTY;
9208 + dev->nErasedBlocks++;
9210 + /* this is the block being allocated from */
9211 + T(YAFFS_TRACE_SCAN,
9213 + (" Allocating from %d %d" TENDSTR),
9215 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9216 + dev->allocationBlock = blk;
9217 + dev->allocationPage = c;
9218 + dev->allocationBlockFinder = blk;
9219 + /* Set it to here to encourage the allocator to go forth from here. */
9221 + /* Yaffs2 sanity check:
9222 + * This should be the one with the highest sequence number
9225 + && (dev->sequenceNumber !=
9226 + bi->sequenceNumber)) {
9227 + T(YAFFS_TRACE_ALWAYS,
9229 + ("yaffs: Allocation block %d was not highest sequence id:"
9230 + " block seq = %d, dev seq = %d"
9231 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
9235 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9236 + } else if (tags.chunkId > 0) {
9237 + /* chunkId > 0 so it is a data chunk... */
9238 + unsigned int endpos;
9240 + yaffs_SetChunkBit(dev, blk, c);
9243 + in = yaffs_FindOrCreateObjectByNumber(dev,
9246 + YAFFS_OBJECT_TYPE_FILE);
9247 + /* PutChunkIntoFile checks for a clash (two data chunks with
9248 + * the same chunkId).
9255 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
9260 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9263 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9264 + && in->variant.fileVariant.scannedFileSize <
9266 + in->variant.fileVariant.
9267 + scannedFileSize = endpos;
9268 + if (!dev->useHeaderFileSize) {
9269 + in->variant.fileVariant.
9271 + in->variant.fileVariant.
9276 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9278 + /* chunkId == 0, so it is an ObjectHeader.
9279 + * Thus, we read in the object header and make the object
9281 + yaffs_SetChunkBit(dev, blk, c);
9284 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9288 + oh = (yaffs_ObjectHeader *) chunkData;
9290 + in = yaffs_FindObjectByNumber(dev,
9292 + if (in && in->variantType != oh->type) {
9293 + /* This should not happen, but somehow
9294 + * Wev'e ended up with an objectId that has been reused but not yet
9295 + * deleted, and worse still it has changed type. Delete the old object.
9298 + yaffs_DestroyObject(in);
9303 + in = yaffs_FindOrCreateObjectByNumber(dev,
9311 + if (in && oh->shadowsObject > 0) {
9312 + yaffs_HandleShadowedObject(dev,
9318 + if (in && in->valid) {
9319 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9321 + unsigned existingSerial = in->serial;
9322 + unsigned newSerial = tags.serialNumber;
9324 + if (dev->isYaffs2 ||
9325 + ((existingSerial + 1) & 3) ==
9327 + /* Use new one - destroy the exisiting one */
9328 + yaffs_DeleteChunk(dev,
9333 + /* Use existing - destroy this one. */
9334 + yaffs_DeleteChunk(dev, chunk, 1,
9339 + if (in && !in->valid &&
9340 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9341 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9342 + /* We only load some info, don't fiddle with directory structure */
9344 + in->variantType = oh->type;
9346 + in->yst_mode = oh->yst_mode;
9347 +#ifdef CONFIG_YAFFS_WINCE
9348 + in->win_atime[0] = oh->win_atime[0];
9349 + in->win_ctime[0] = oh->win_ctime[0];
9350 + in->win_mtime[0] = oh->win_mtime[0];
9351 + in->win_atime[1] = oh->win_atime[1];
9352 + in->win_ctime[1] = oh->win_ctime[1];
9353 + in->win_mtime[1] = oh->win_mtime[1];
9355 + in->yst_uid = oh->yst_uid;
9356 + in->yst_gid = oh->yst_gid;
9357 + in->yst_atime = oh->yst_atime;
9358 + in->yst_mtime = oh->yst_mtime;
9359 + in->yst_ctime = oh->yst_ctime;
9360 + in->yst_rdev = oh->yst_rdev;
9362 + in->chunkId = chunk;
9364 + } else if (in && !in->valid) {
9365 + /* we need to load this info */
9368 + in->variantType = oh->type;
9370 + in->yst_mode = oh->yst_mode;
9371 +#ifdef CONFIG_YAFFS_WINCE
9372 + in->win_atime[0] = oh->win_atime[0];
9373 + in->win_ctime[0] = oh->win_ctime[0];
9374 + in->win_mtime[0] = oh->win_mtime[0];
9375 + in->win_atime[1] = oh->win_atime[1];
9376 + in->win_ctime[1] = oh->win_ctime[1];
9377 + in->win_mtime[1] = oh->win_mtime[1];
9379 + in->yst_uid = oh->yst_uid;
9380 + in->yst_gid = oh->yst_gid;
9381 + in->yst_atime = oh->yst_atime;
9382 + in->yst_mtime = oh->yst_mtime;
9383 + in->yst_ctime = oh->yst_ctime;
9384 + in->yst_rdev = oh->yst_rdev;
9386 + in->chunkId = chunk;
9388 + yaffs_SetObjectName(in, oh->name);
9391 + /* directory stuff...
9392 + * hook up to parent
9396 + yaffs_FindOrCreateObjectByNumber
9397 + (dev, oh->parentObjectId,
9398 + YAFFS_OBJECT_TYPE_DIRECTORY);
9399 + if (parent->variantType ==
9400 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9401 + /* Set up as a directory */
9402 + parent->variantType =
9403 + YAFFS_OBJECT_TYPE_DIRECTORY;
9404 + INIT_LIST_HEAD(&parent->variant.
9407 + } else if (parent->variantType !=
9408 + YAFFS_OBJECT_TYPE_DIRECTORY)
9410 + /* Hoosterman, another problem....
9411 + * We're trying to use a non-directory as a directory
9414 + T(YAFFS_TRACE_ERROR,
9416 + ("yaffs tragedy: attempting to use non-directory as"
9417 + " a directory in scan. Put in lost+found."
9419 + parent = dev->lostNFoundDir;
9422 + yaffs_AddObjectToDirectory(parent, in);
9424 + if (0 && (parent == dev->deletedDir ||
9425 + parent == dev->unlinkedDir)) {
9426 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9427 + dev->nDeletedFiles++;
9429 + /* Note re hardlinks.
9430 + * Since we might scan a hardlink before its equivalent object is scanned
9431 + * we put them all in a list.
9432 + * After scanning is complete, we should have all the objects, so we run through this
9433 + * list and fix up all the chains.
9436 + switch (in->variantType) {
9437 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9438 + /* Todo got a problem */
9440 + case YAFFS_OBJECT_TYPE_FILE:
9442 + && oh->isShrink) {
9443 + /* Prune back the shrunken chunks */
9444 + yaffs_PruneResizedChunks
9445 + (in, oh->fileSize);
9446 + /* Mark the block as having a shrinkHeader */
9447 + bi->hasShrinkHeader = 1;
9450 + if (dev->useHeaderFileSize)
9452 + in->variant.fileVariant.
9457 + case YAFFS_OBJECT_TYPE_HARDLINK:
9458 + in->variant.hardLinkVariant.
9459 + equivalentObjectId =
9460 + oh->equivalentObjectId;
9461 + in->hardLinks.next =
9462 + (struct list_head *)
9466 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9469 + case YAFFS_OBJECT_TYPE_SPECIAL:
9472 + case YAFFS_OBJECT_TYPE_SYMLINK:
9473 + in->variant.symLinkVariant.alias =
9474 + yaffs_CloneString(oh->alias);
9475 + if(!in->variant.symLinkVariant.alias)
9480 + if (parent == dev->deletedDir) {
9481 + yaffs_DestroyObject(in);
9482 + bi->hasShrinkHeader = 1;
9488 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9489 + /* If we got this far while scanning, then the block is fully allocated.*/
9490 + state = YAFFS_BLOCK_STATE_FULL;
9493 + bi->blockState = state;
9495 + /* Now let's see if it was dirty */
9496 + if (bi->pagesInUse == 0 &&
9497 + !bi->hasShrinkHeader &&
9498 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9499 + yaffs_BlockBecameDirty(dev, blk);
9505 + YFREE(blockIndex);
9509 + /* Ok, we've done all the scanning.
9510 + * Fix up the hard link chains.
9511 + * We should now have scanned all the objects, now it's time to add these
9515 + yaffs_HardlinkFixup(dev,hardList);
9517 + /* Handle the unlinked files. Since they were left in an unlinked state we should
9518 + * just delete them.
9521 + struct list_head *i;
9522 + struct list_head *n;
9525 + /* Soft delete all the unlinked files */
9526 + list_for_each_safe(i, n,
9527 + &dev->unlinkedDir->variant.directoryVariant.
9530 + l = list_entry(i, yaffs_Object, siblings);
9531 + yaffs_DestroyObject(l);
9536 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9539 + return YAFFS_FAIL;
9542 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9548 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9551 + yaffs_ObjectHeader *oh;
9552 + yaffs_Device *dev = in->myDev;
9553 + yaffs_ExtendedTags tags;
9555 + int alloc_failed = 0;
9561 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
9563 + in->lazyLoaded ? "not yet" : "already"));
9566 + if(in->lazyLoaded){
9567 + in->lazyLoaded = 0;
9568 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9570 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
9571 + oh = (yaffs_ObjectHeader *) chunkData;
9573 + in->yst_mode = oh->yst_mode;
9574 +#ifdef CONFIG_YAFFS_WINCE
9575 + in->win_atime[0] = oh->win_atime[0];
9576 + in->win_ctime[0] = oh->win_ctime[0];
9577 + in->win_mtime[0] = oh->win_mtime[0];
9578 + in->win_atime[1] = oh->win_atime[1];
9579 + in->win_ctime[1] = oh->win_ctime[1];
9580 + in->win_mtime[1] = oh->win_mtime[1];
9582 + in->yst_uid = oh->yst_uid;
9583 + in->yst_gid = oh->yst_gid;
9584 + in->yst_atime = oh->yst_atime;
9585 + in->yst_mtime = oh->yst_mtime;
9586 + in->yst_ctime = oh->yst_ctime;
9587 + in->yst_rdev = oh->yst_rdev;
9590 + yaffs_SetObjectName(in, oh->name);
9592 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
9593 + in->variant.symLinkVariant.alias =
9594 + yaffs_CloneString(oh->alias);
9595 + if(!in->variant.symLinkVariant.alias)
9596 + alloc_failed = 1; /* Not returned to caller */
9599 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
9603 +static int yaffs_ScanBackwards(yaffs_Device * dev)
9605 + yaffs_ExtendedTags tags;
9607 + int blockIterator;
9608 + int startIterator;
9610 + int nBlocksToScan = 0;
9616 + yaffs_BlockState state;
9617 + yaffs_Object *hardList = NULL;
9618 + yaffs_BlockInfo *bi;
9619 + int sequenceNumber;
9620 + yaffs_ObjectHeader *oh;
9622 + yaffs_Object *parent;
9623 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9629 + int foundChunksInBlock;
9630 + int equivalentObjectId;
9631 + int alloc_failed = 0;
9634 + yaffs_BlockIndex *blockIndex = NULL;
9635 + int altBlockIndex = 0;
9637 + if (!dev->isYaffs2) {
9638 + T(YAFFS_TRACE_SCAN,
9639 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9640 + return YAFFS_FAIL;
9643 + T(YAFFS_TRACE_SCAN,
9645 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9646 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9649 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9651 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9654 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9655 + altBlockIndex = 1;
9659 + T(YAFFS_TRACE_SCAN,
9660 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9661 + return YAFFS_FAIL;
9664 + dev->blocksInCheckpoint = 0;
9666 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9668 + /* Scan all the blocks to determine their state */
9669 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9670 + bi = yaffs_GetBlockInfo(dev, blk);
9671 + yaffs_ClearChunkBits(dev, blk);
9672 + bi->pagesInUse = 0;
9673 + bi->softDeletions = 0;
9675 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9677 + bi->blockState = state;
9678 + bi->sequenceNumber = sequenceNumber;
9680 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9681 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9683 + T(YAFFS_TRACE_SCAN_DEBUG,
9684 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9685 + state, sequenceNumber));
9688 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
9689 + dev->blocksInCheckpoint++;
9691 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
9692 + T(YAFFS_TRACE_BAD_BLOCKS,
9693 + (TSTR("block %d is bad" TENDSTR), blk));
9694 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9695 + T(YAFFS_TRACE_SCAN_DEBUG,
9696 + (TSTR("Block empty " TENDSTR)));
9697 + dev->nErasedBlocks++;
9698 + dev->nFreeChunks += dev->nChunksPerBlock;
9699 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9701 + /* Determine the highest sequence number */
9702 + if (dev->isYaffs2 &&
9703 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9704 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9706 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9707 + blockIndex[nBlocksToScan].block = blk;
9711 + if (sequenceNumber >= dev->sequenceNumber) {
9712 + dev->sequenceNumber = sequenceNumber;
9714 + } else if (dev->isYaffs2) {
9715 + /* TODO: Nasty sequence number! */
9716 + T(YAFFS_TRACE_SCAN,
9718 + ("Block scanning block %d has bad sequence number %d"
9719 + TENDSTR), blk, sequenceNumber));
9725 + T(YAFFS_TRACE_SCAN,
9726 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
9732 + /* Sort the blocks */
9733 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
9734 + yaffs_qsort(blockIndex, nBlocksToScan,
9735 + sizeof(yaffs_BlockIndex), ybicmp);
9738 + /* Dungy old bubble sort... */
9740 + yaffs_BlockIndex temp;
9744 + for (i = 0; i < nBlocksToScan; i++)
9745 + for (j = i + 1; j < nBlocksToScan; j++)
9746 + if (blockIndex[i].seq > blockIndex[j].seq) {
9747 + temp = blockIndex[j];
9748 + blockIndex[j] = blockIndex[i];
9749 + blockIndex[i] = temp;
9756 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
9758 + /* Now scan the blocks looking at the data. */
9759 + startIterator = 0;
9760 + endIterator = nBlocksToScan - 1;
9761 + T(YAFFS_TRACE_SCAN_DEBUG,
9762 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9764 + /* For each block.... backwards */
9765 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
9766 + blockIterator--) {
9767 + /* Cooperative multitasking! This loop can run for so
9768 + long that watchdog timers expire. */
9771 + /* get the block to scan in the correct order */
9772 + blk = blockIndex[blockIterator].block;
9774 + bi = yaffs_GetBlockInfo(dev, blk);
9777 + state = bi->blockState;
9781 + /* For each chunk in each block that needs scanning.... */
9782 + foundChunksInBlock = 0;
9783 + for (c = dev->nChunksPerBlock - 1;
9784 + !alloc_failed && c >= 0 &&
9785 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9786 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
9787 + /* Scan backwards...
9788 + * Read the tags and decide what to do
9791 + chunk = blk * dev->nChunksPerBlock + c;
9793 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9796 + /* Let's have a good look at this chunk... */
9798 + if (!tags.chunkUsed) {
9799 + /* An unassigned chunk in the block.
9800 + * If there are used chunks after this one, then
9801 + * it is a chunk that was skipped due to failing the erased
9802 + * check. Just skip it so that it can be deleted.
9803 + * But, more typically, We get here when this is an unallocated
9804 + * chunk and his means that either the block is empty or
9805 + * this is the one being allocated from
9808 + if(foundChunksInBlock)
9810 + /* This is a chunk that was skipped due to failing the erased check */
9812 + } else if (c == 0) {
9813 + /* We're looking at the first chunk in the block so the block is unused */
9814 + state = YAFFS_BLOCK_STATE_EMPTY;
9815 + dev->nErasedBlocks++;
9817 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9818 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
9819 + if(dev->sequenceNumber == bi->sequenceNumber) {
9820 + /* this is the block being allocated from */
9822 + T(YAFFS_TRACE_SCAN,
9824 + (" Allocating from %d %d"
9825 + TENDSTR), blk, c));
9827 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9828 + dev->allocationBlock = blk;
9829 + dev->allocationPage = c;
9830 + dev->allocationBlockFinder = blk;
9833 + /* This is a partially written block that is not
9834 + * the current allocation block. This block must have
9835 + * had a write failure, so set up for retirement.
9838 + bi->needsRetiring = 1;
9839 + bi->gcPrioritise = 1;
9841 + T(YAFFS_TRACE_ALWAYS,
9842 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
9850 + dev->nFreeChunks++;
9852 + } else if (tags.chunkId > 0) {
9853 + /* chunkId > 0 so it is a data chunk... */
9854 + unsigned int endpos;
9856 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
9858 + foundChunksInBlock = 1;
9861 + yaffs_SetChunkBit(dev, blk, c);
9864 + in = yaffs_FindOrCreateObjectByNumber(dev,
9867 + YAFFS_OBJECT_TYPE_FILE);
9869 + /* Out of memory */
9874 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9876 + in->variant.fileVariant.shrinkSize) {
9877 + /* This has not been invalidated by a resize */
9878 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
9883 + /* File size is calculated by looking at the data chunks if we have not
9884 + * seen an object header yet. Stop this practice once we find an object header.
9888 + 1) * dev->nDataBytesPerChunk +
9891 + if (!in->valid && /* have not got an object header yet */
9892 + in->variant.fileVariant.
9893 + scannedFileSize < endpos) {
9894 + in->variant.fileVariant.
9895 + scannedFileSize = endpos;
9896 + in->variant.fileVariant.
9898 + in->variant.fileVariant.
9903 + /* This chunk has been invalidated by a resize, so delete */
9904 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9908 + /* chunkId == 0, so it is an ObjectHeader.
9909 + * Thus, we read in the object header and make the object
9911 + foundChunksInBlock = 1;
9913 + yaffs_SetChunkBit(dev, blk, c);
9919 + if (tags.extraHeaderInfoAvailable) {
9920 + in = yaffs_FindOrCreateObjectByNumber
9921 + (dev, tags.objectId,
9922 + tags.extraObjectType);
9926 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
9929 + tags.extraShadows ||
9931 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9932 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
9935 + /* If we don't have valid info then we need to read the chunk
9936 + * TODO In future we can probably defer reading the chunk and
9937 + * living with invalid data until needed.
9940 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
9945 + oh = (yaffs_ObjectHeader *) chunkData;
9948 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
9953 + /* TODO Hoosterman we have a problem! */
9954 + T(YAFFS_TRACE_ERROR,
9956 + ("yaffs tragedy: Could not make object for object %d "
9957 + "at chunk %d during scan"
9958 + TENDSTR), tags.objectId, chunk));
9963 + /* We have already filled this one.
9964 + * We have a duplicate that will be discarded, but
9965 + * we first have to suck out resize info if it is a file.
9968 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
9970 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
9971 + (tags.extraHeaderInfoAvailable &&
9972 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
9975 + (oh) ? oh->fileSize : tags.
9977 + __u32 parentObjectId =
9979 + parentObjectId : tags.
9980 + extraParentObjectId;
9981 + unsigned isShrink =
9982 + (oh) ? oh->isShrink : tags.
9983 + extraIsShrinkHeader;
9985 + /* If it is deleted (unlinked at start also means deleted)
9986 + * we treat the file size as being zeroed at this point.
9988 + if (parentObjectId ==
9989 + YAFFS_OBJECTID_DELETED
9990 + || parentObjectId ==
9991 + YAFFS_OBJECTID_UNLINKED) {
9997 + in->variant.fileVariant.
9998 + shrinkSize > thisSize) {
9999 + in->variant.fileVariant.
10005 + bi->hasShrinkHeader = 1;
10009 + /* Use existing - destroy this one. */
10010 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
10014 + if (!in->valid &&
10015 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
10017 + YAFFS_OBJECTID_LOSTNFOUND)) {
10018 + /* We only load some info, don't fiddle with directory structure */
10022 + in->variantType = oh->type;
10024 + in->yst_mode = oh->yst_mode;
10025 +#ifdef CONFIG_YAFFS_WINCE
10026 + in->win_atime[0] = oh->win_atime[0];
10027 + in->win_ctime[0] = oh->win_ctime[0];
10028 + in->win_mtime[0] = oh->win_mtime[0];
10029 + in->win_atime[1] = oh->win_atime[1];
10030 + in->win_ctime[1] = oh->win_ctime[1];
10031 + in->win_mtime[1] = oh->win_mtime[1];
10033 + in->yst_uid = oh->yst_uid;
10034 + in->yst_gid = oh->yst_gid;
10035 + in->yst_atime = oh->yst_atime;
10036 + in->yst_mtime = oh->yst_mtime;
10037 + in->yst_ctime = oh->yst_ctime;
10038 + in->yst_rdev = oh->yst_rdev;
10042 + in->variantType = tags.extraObjectType;
10043 + in->lazyLoaded = 1;
10046 + in->chunkId = chunk;
10048 + } else if (!in->valid) {
10049 + /* we need to load this info */
10052 + in->chunkId = chunk;
10055 + in->variantType = oh->type;
10057 + in->yst_mode = oh->yst_mode;
10058 +#ifdef CONFIG_YAFFS_WINCE
10059 + in->win_atime[0] = oh->win_atime[0];
10060 + in->win_ctime[0] = oh->win_ctime[0];
10061 + in->win_mtime[0] = oh->win_mtime[0];
10062 + in->win_atime[1] = oh->win_atime[1];
10063 + in->win_ctime[1] = oh->win_ctime[1];
10064 + in->win_mtime[1] = oh->win_mtime[1];
10066 + in->yst_uid = oh->yst_uid;
10067 + in->yst_gid = oh->yst_gid;
10068 + in->yst_atime = oh->yst_atime;
10069 + in->yst_mtime = oh->yst_mtime;
10070 + in->yst_ctime = oh->yst_ctime;
10071 + in->yst_rdev = oh->yst_rdev;
10074 + if (oh->shadowsObject > 0)
10075 + yaffs_HandleShadowedObject(dev,
10081 + yaffs_SetObjectName(in, oh->name);
10083 + yaffs_FindOrCreateObjectByNumber
10084 + (dev, oh->parentObjectId,
10085 + YAFFS_OBJECT_TYPE_DIRECTORY);
10087 + fileSize = oh->fileSize;
10088 + isShrink = oh->isShrink;
10089 + equivalentObjectId = oh->equivalentObjectId;
10093 + in->variantType = tags.extraObjectType;
10095 + yaffs_FindOrCreateObjectByNumber
10096 + (dev, tags.extraParentObjectId,
10097 + YAFFS_OBJECT_TYPE_DIRECTORY);
10098 + fileSize = tags.extraFileLength;
10099 + isShrink = tags.extraIsShrinkHeader;
10100 + equivalentObjectId = tags.extraEquivalentObjectId;
10101 + in->lazyLoaded = 1;
10106 + /* directory stuff...
10107 + * hook up to parent
10110 + if (parent->variantType ==
10111 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10112 + /* Set up as a directory */
10113 + parent->variantType =
10114 + YAFFS_OBJECT_TYPE_DIRECTORY;
10115 + INIT_LIST_HEAD(&parent->variant.
10116 + directoryVariant.
10118 + } else if (parent->variantType !=
10119 + YAFFS_OBJECT_TYPE_DIRECTORY)
10121 + /* Hoosterman, another problem....
10122 + * We're trying to use a non-directory as a directory
10125 + T(YAFFS_TRACE_ERROR,
10127 + ("yaffs tragedy: attempting to use non-directory as"
10128 + " a directory in scan. Put in lost+found."
10130 + parent = dev->lostNFoundDir;
10133 + yaffs_AddObjectToDirectory(parent, in);
10135 + itsUnlinked = (parent == dev->deletedDir) ||
10136 + (parent == dev->unlinkedDir);
10139 + /* Mark the block as having a shrinkHeader */
10140 + bi->hasShrinkHeader = 1;
10143 + /* Note re hardlinks.
10144 + * Since we might scan a hardlink before its equivalent object is scanned
10145 + * we put them all in a list.
10146 + * After scanning is complete, we should have all the objects, so we run
10147 + * through this list and fix up all the chains.
10150 + switch (in->variantType) {
10151 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10152 + /* Todo got a problem */
10154 + case YAFFS_OBJECT_TYPE_FILE:
10156 + if (in->variant.fileVariant.
10157 + scannedFileSize < fileSize) {
10158 + /* This covers the case where the file size is greater
10159 + * than where the data is
10160 + * This will happen if the file is resized to be larger
10161 + * than its current data extents.
10163 + in->variant.fileVariant.fileSize = fileSize;
10164 + in->variant.fileVariant.scannedFileSize =
10165 + in->variant.fileVariant.fileSize;
10169 + in->variant.fileVariant.shrinkSize > fileSize) {
10170 + in->variant.fileVariant.shrinkSize = fileSize;
10174 + case YAFFS_OBJECT_TYPE_HARDLINK:
10175 + if(!itsUnlinked) {
10176 + in->variant.hardLinkVariant.equivalentObjectId =
10177 + equivalentObjectId;
10178 + in->hardLinks.next =
10179 + (struct list_head *) hardList;
10183 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10186 + case YAFFS_OBJECT_TYPE_SPECIAL:
10189 + case YAFFS_OBJECT_TYPE_SYMLINK:
10191 + in->variant.symLinkVariant.alias =
10192 + yaffs_CloneString(oh->
10194 + if(!in->variant.symLinkVariant.alias)
10195 + alloc_failed = 1;
10204 + } /* End of scanning for each chunk */
10206 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10207 + /* If we got this far while scanning, then the block is fully allocated. */
10208 + state = YAFFS_BLOCK_STATE_FULL;
10211 + bi->blockState = state;
10213 + /* Now let's see if it was dirty */
10214 + if (bi->pagesInUse == 0 &&
10215 + !bi->hasShrinkHeader &&
10216 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10217 + yaffs_BlockBecameDirty(dev, blk);
10222 + if (altBlockIndex)
10223 + YFREE_ALT(blockIndex);
10225 + YFREE(blockIndex);
10227 + /* Ok, we've done all the scanning.
10228 + * Fix up the hard link chains.
10229 + * We should now have scanned all the objects, now it's time to add these
10232 + yaffs_HardlinkFixup(dev,hardList);
10236 + * Sort out state of unlinked and deleted objects.
10239 + struct list_head *i;
10240 + struct list_head *n;
10244 + /* Soft delete all the unlinked files */
10245 + list_for_each_safe(i, n,
10246 + &dev->unlinkedDir->variant.directoryVariant.
10249 + l = list_entry(i, yaffs_Object, siblings);
10250 + yaffs_DestroyObject(l);
10254 + /* Soft delete all the deletedDir files */
10255 + list_for_each_safe(i, n,
10256 + &dev->deletedDir->variant.directoryVariant.
10259 + l = list_entry(i, yaffs_Object, siblings);
10260 + yaffs_DestroyObject(l);
10266 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10268 + if(alloc_failed){
10269 + return YAFFS_FAIL;
10272 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10277 +/*------------------------------ Directory Functions ----------------------------- */
10279 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
10281 + yaffs_Device *dev = obj->myDev;
10283 + if(dev && dev->removeObjectCallback)
10284 + dev->removeObjectCallback(obj);
10286 + list_del_init(&obj->siblings);
10287 + obj->parent = NULL;
10291 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
10292 + yaffs_Object * obj)
10295 + if (!directory) {
10296 + T(YAFFS_TRACE_ALWAYS,
10298 + ("tragedy: Trying to add an object to a null pointer directory"
10302 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10303 + T(YAFFS_TRACE_ALWAYS,
10305 + ("tragedy: Trying to add an object to a non-directory"
10310 + if (obj->siblings.prev == NULL) {
10311 + /* Not initialised */
10312 + INIT_LIST_HEAD(&obj->siblings);
10314 + } else if (!list_empty(&obj->siblings)) {
10315 + /* If it is holed up somewhere else, un hook it */
10316 + yaffs_RemoveObjectFromDirectory(obj);
10319 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
10320 + obj->parent = directory;
10322 + if (directory == obj->myDev->unlinkedDir
10323 + || directory == obj->myDev->deletedDir) {
10324 + obj->unlinked = 1;
10325 + obj->myDev->nUnlinkedFiles++;
10326 + obj->renameAllowed = 0;
10330 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
10331 + const YCHAR * name)
10335 + struct list_head *i;
10336 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10344 + if (!directory) {
10345 + T(YAFFS_TRACE_ALWAYS,
10347 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10351 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10352 + T(YAFFS_TRACE_ALWAYS,
10354 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10358 + sum = yaffs_CalcNameSum(name);
10360 + list_for_each(i, &directory->variant.directoryVariant.children) {
10362 + l = list_entry(i, yaffs_Object, siblings);
10364 + yaffs_CheckObjectDetailsLoaded(l);
10366 + /* Special case for lost-n-found */
10367 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10368 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
10371 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
10373 + /* LostnFound cunk called Objxxx
10374 + * Do a real check
10376 + yaffs_GetObjectName(l, buffer,
10377 + YAFFS_MAX_NAME_LENGTH);
10378 + if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
10391 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10392 + int (*fn) (yaffs_Object *))
10394 + struct list_head *i;
10398 + T(YAFFS_TRACE_ALWAYS,
10400 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10404 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10405 + T(YAFFS_TRACE_ALWAYS,
10407 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10411 + list_for_each(i, &theDir->variant.directoryVariant.children) {
10413 + l = list_entry(i, yaffs_Object, siblings);
10414 + if (l && !fn(l)) {
10415 + return YAFFS_FAIL;
10425 +/* GetEquivalentObject dereferences any hard links to get to the
10429 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
10431 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10432 + /* We want the object id of the equivalent object, not this one */
10433 + obj = obj->variant.hardLinkVariant.equivalentObject;
10434 + yaffs_CheckObjectDetailsLoaded(obj);
10440 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
10442 + memset(name, 0, buffSize * sizeof(YCHAR));
10444 + yaffs_CheckObjectDetailsLoaded(obj);
10446 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10447 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10448 + } else if (obj->chunkId <= 0) {
10449 + YCHAR locName[20];
10450 + /* make up a name */
10451 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
10453 + yaffs_strncpy(name, locName, buffSize - 1);
10456 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10457 + else if (obj->shortName[0]) {
10458 + yaffs_strcpy(name, obj->shortName);
10463 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10465 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10467 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10469 + if (obj->chunkId >= 0) {
10470 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10471 + obj->chunkId, buffer,
10474 + yaffs_strncpy(name, oh->name, buffSize - 1);
10476 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10479 + return yaffs_strlen(name);
10482 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
10485 + /* Dereference any hard linking */
10486 + obj = yaffs_GetEquivalentObject(obj);
10488 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10489 + return obj->variant.fileVariant.fileSize;
10491 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10492 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10494 + /* Only a directory should drop through to here */
10495 + return obj->myDev->nDataBytesPerChunk;
10499 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
10502 + struct list_head *i;
10504 + if (!obj->unlinked) {
10505 + count++; /* the object itself */
10507 + list_for_each(i, &obj->hardLinks) {
10508 + count++; /* add the hard links; */
10514 +int yaffs_GetObjectInode(yaffs_Object * obj)
10516 + obj = yaffs_GetEquivalentObject(obj);
10518 + return obj->objectId;
10521 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
10523 + obj = yaffs_GetEquivalentObject(obj);
10525 + switch (obj->variantType) {
10526 + case YAFFS_OBJECT_TYPE_FILE:
10529 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10532 + case YAFFS_OBJECT_TYPE_SYMLINK:
10535 + case YAFFS_OBJECT_TYPE_HARDLINK:
10538 + case YAFFS_OBJECT_TYPE_SPECIAL:
10539 + if (S_ISFIFO(obj->yst_mode))
10541 + if (S_ISCHR(obj->yst_mode))
10543 + if (S_ISBLK(obj->yst_mode))
10545 + if (S_ISSOCK(obj->yst_mode))
10553 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
10555 + obj = yaffs_GetEquivalentObject(obj);
10556 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10557 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10559 + return yaffs_CloneString(_Y(""));
10563 +#ifndef CONFIG_YAFFS_WINCE
10565 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
10567 + unsigned int valid = attr->ia_valid;
10569 + if (valid & ATTR_MODE)
10570 + obj->yst_mode = attr->ia_mode;
10571 + if (valid & ATTR_UID)
10572 + obj->yst_uid = attr->ia_uid;
10573 + if (valid & ATTR_GID)
10574 + obj->yst_gid = attr->ia_gid;
10576 + if (valid & ATTR_ATIME)
10577 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10578 + if (valid & ATTR_CTIME)
10579 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10580 + if (valid & ATTR_MTIME)
10581 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10583 + if (valid & ATTR_SIZE)
10584 + yaffs_ResizeFile(obj, attr->ia_size);
10586 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10591 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
10593 + unsigned int valid = 0;
10595 + attr->ia_mode = obj->yst_mode;
10596 + valid |= ATTR_MODE;
10597 + attr->ia_uid = obj->yst_uid;
10598 + valid |= ATTR_UID;
10599 + attr->ia_gid = obj->yst_gid;
10600 + valid |= ATTR_GID;
10602 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10603 + valid |= ATTR_ATIME;
10604 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10605 + valid |= ATTR_CTIME;
10606 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
10607 + valid |= ATTR_MTIME;
10609 + attr->ia_size = yaffs_GetFileSize(obj);
10610 + valid |= ATTR_SIZE;
10612 + attr->ia_valid = valid;
10621 +int yaffs_DumpObject(yaffs_Object * obj)
10625 + yaffs_GetObjectName(obj, name, 256);
10627 + T(YAFFS_TRACE_ALWAYS,
10629 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
10630 + " chunk %d type %d size %d\n"
10631 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
10632 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
10633 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
10639 +/*---------------------------- Initialisation code -------------------------------------- */
10641 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
10644 + /* Common functions, gotta have */
10645 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
10648 +#ifdef CONFIG_YAFFS_YAFFS2
10650 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
10651 + if (dev->writeChunkWithTagsToNAND &&
10652 + dev->readChunkWithTagsFromNAND &&
10653 + !dev->writeChunkToNAND &&
10654 + !dev->readChunkFromNAND &&
10655 + dev->markNANDBlockBad && dev->queryNANDBlock)
10659 + /* Can use the "spare" style interface for yaffs1 */
10660 + if (!dev->isYaffs2 &&
10661 + !dev->writeChunkWithTagsToNAND &&
10662 + !dev->readChunkWithTagsFromNAND &&
10663 + dev->writeChunkToNAND &&
10664 + dev->readChunkFromNAND &&
10665 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
10668 + return 0; /* bad */
10672 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
10674 + /* Initialise the unlinked, deleted, root and lost and found directories */
10676 + dev->lostNFoundDir = dev->rootDir = NULL;
10677 + dev->unlinkedDir = dev->deletedDir = NULL;
10679 + dev->unlinkedDir =
10680 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
10682 + dev->deletedDir =
10683 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
10686 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
10687 + YAFFS_ROOT_MODE | S_IFDIR);
10688 + dev->lostNFoundDir =
10689 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
10690 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
10692 + if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
10693 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
10697 + return YAFFS_FAIL;
10700 +int yaffs_GutsInitialise(yaffs_Device * dev)
10702 + int init_failed = 0;
10706 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
10708 + /* Check stuff that must be set */
10711 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
10712 + return YAFFS_FAIL;
10715 + dev->internalStartBlock = dev->startBlock;
10716 + dev->internalEndBlock = dev->endBlock;
10717 + dev->blockOffset = 0;
10718 + dev->chunkOffset = 0;
10719 + dev->nFreeChunks = 0;
10721 + if (dev->startBlock == 0) {
10722 + dev->internalStartBlock = dev->startBlock + 1;
10723 + dev->internalEndBlock = dev->endBlock + 1;
10724 + dev->blockOffset = 1;
10725 + dev->chunkOffset = dev->nChunksPerBlock;
10728 + /* Check geometry parameters. */
10730 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
10731 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
10732 + dev->nChunksPerBlock < 2 ||
10733 + dev->nReservedBlocks < 2 ||
10734 + dev->internalStartBlock <= 0 ||
10735 + dev->internalEndBlock <= 0 ||
10736 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
10738 + T(YAFFS_TRACE_ALWAYS,
10740 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
10741 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
10742 + return YAFFS_FAIL;
10745 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
10746 + T(YAFFS_TRACE_ALWAYS,
10747 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
10748 + return YAFFS_FAIL;
10751 + /* Got the right mix of functions? */
10752 + if (!yaffs_CheckDevFunctions(dev)) {
10753 + /* Function missing */
10754 + T(YAFFS_TRACE_ALWAYS,
10756 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
10758 + return YAFFS_FAIL;
10761 + /* This is really a compilation check. */
10762 + if (!yaffs_CheckStructures()) {
10763 + T(YAFFS_TRACE_ALWAYS,
10764 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
10765 + return YAFFS_FAIL;
10768 + if (dev->isMounted) {
10769 + T(YAFFS_TRACE_ALWAYS,
10770 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
10771 + return YAFFS_FAIL;
10774 + /* Finished with most checks. One or two more checks happen later on too. */
10776 + dev->isMounted = 1;
10780 + /* OK now calculate a few things for the device */
10783 + * Calculate all the chunk size manipulation numbers:
10785 + /* Start off assuming it is a power of 2 */
10786 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
10787 + dev->chunkMask = (1<<dev->chunkShift) - 1;
10789 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
10790 + /* Yes it is a power of 2, disable crumbs */
10791 + dev->crumbMask = 0;
10792 + dev->crumbShift = 0;
10793 + dev->crumbsPerChunk = 0;
10795 + /* Not a power of 2, use crumbs instead */
10796 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
10797 + dev->crumbMask = (1<<dev->crumbShift)-1;
10798 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
10799 + dev->chunkShift = 0;
10800 + dev->chunkMask = 0;
10805 + * Calculate chunkGroupBits.
10806 + * We need to find the next power of 2 > than internalEndBlock
10809 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
10811 + bits = ShiftsGE(x);
10813 + /* Set up tnode width if wide tnodes are enabled. */
10814 + if(!dev->wideTnodesDisabled){
10815 + /* bits must be even so that we end up with 32-bit words */
10819 + dev->tnodeWidth = 16;
10821 + dev->tnodeWidth = bits;
10824 + dev->tnodeWidth = 16;
10826 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
10828 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
10829 + * so if the bitwidth of the
10830 + * chunk range we're using is greater than 16 we need
10831 + * to figure out chunk shift and chunkGroupSize
10834 + if (bits <= dev->tnodeWidth)
10835 + dev->chunkGroupBits = 0;
10837 + dev->chunkGroupBits = bits - dev->tnodeWidth;
10840 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
10842 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
10843 + /* We have a problem because the soft delete won't work if
10844 + * the chunk group size > chunks per block.
10845 + * This can be remedied by using larger "virtual blocks".
10847 + T(YAFFS_TRACE_ALWAYS,
10848 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
10850 + return YAFFS_FAIL;
10853 + /* OK, we've finished verifying the device, lets continue with initialisation */
10855 + /* More device initialisation */
10856 + dev->garbageCollections = 0;
10857 + dev->passiveGarbageCollections = 0;
10858 + dev->currentDirtyChecker = 0;
10859 + dev->bufferedBlock = -1;
10860 + dev->doingBufferedBlockRewrite = 0;
10861 + dev->nDeletedFiles = 0;
10862 + dev->nBackgroundDeletions = 0;
10863 + dev->nUnlinkedFiles = 0;
10864 + dev->eccFixed = 0;
10865 + dev->eccUnfixed = 0;
10866 + dev->tagsEccFixed = 0;
10867 + dev->tagsEccUnfixed = 0;
10868 + dev->nErasureFailures = 0;
10869 + dev->nErasedBlocks = 0;
10870 + dev->isDoingGC = 0;
10871 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
10873 + /* Initialise temporary buffers and caches. */
10874 + if(!yaffs_InitialiseTempBuffers(dev))
10877 + dev->srCache = NULL;
10878 + dev->gcCleanupList = NULL;
10881 + if (!init_failed &&
10882 + dev->nShortOpCaches > 0) {
10885 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
10887 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
10888 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
10891 + buf = dev->srCache = YMALLOC(srCacheBytes);
10894 + memset(dev->srCache,0,srCacheBytes);
10896 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
10897 + dev->srCache[i].object = NULL;
10898 + dev->srCache[i].lastUse = 0;
10899 + dev->srCache[i].dirty = 0;
10900 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
10905 + dev->srLastUse = 0;
10908 + dev->cacheHits = 0;
10910 + if(!init_failed){
10911 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
10912 + if(!dev->gcCleanupList)
10916 + if (dev->isYaffs2) {
10917 + dev->useHeaderFileSize = 1;
10919 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10922 + yaffs_InitialiseTnodes(dev);
10923 + yaffs_InitialiseObjects(dev);
10925 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10929 + if(!init_failed){
10930 + /* Now scan the flash. */
10931 + if (dev->isYaffs2) {
10932 + if(yaffs_CheckpointRestore(dev)) {
10933 + T(YAFFS_TRACE_ALWAYS,
10934 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
10937 + /* Clean up the mess caused by an aborted checkpoint load
10938 + * and scan backwards.
10940 + yaffs_DeinitialiseBlocks(dev);
10941 + yaffs_DeinitialiseTnodes(dev);
10942 + yaffs_DeinitialiseObjects(dev);
10945 + dev->nErasedBlocks = 0;
10946 + dev->nFreeChunks = 0;
10947 + dev->allocationBlock = -1;
10948 + dev->allocationPage = -1;
10949 + dev->nDeletedFiles = 0;
10950 + dev->nUnlinkedFiles = 0;
10951 + dev->nBackgroundDeletions = 0;
10952 + dev->oldestDirtySequence = 0;
10954 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10957 + yaffs_InitialiseTnodes(dev);
10958 + yaffs_InitialiseObjects(dev);
10960 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10963 + if(!init_failed && !yaffs_ScanBackwards(dev))
10967 + if(!yaffs_Scan(dev))
10972 + /* Clean up the mess */
10973 + T(YAFFS_TRACE_TRACING,
10974 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
10976 + yaffs_Deinitialise(dev);
10977 + return YAFFS_FAIL;
10980 + /* Zero out stats */
10981 + dev->nPageReads = 0;
10982 + dev->nPageWrites = 0;
10983 + dev->nBlockErasures = 0;
10984 + dev->nGCCopies = 0;
10985 + dev->nRetriedWrites = 0;
10987 + dev->nRetiredBlocks = 0;
10989 + yaffs_VerifyFreeChunks(dev);
10990 + yaffs_VerifyBlocks(dev);
10993 + T(YAFFS_TRACE_TRACING,
10994 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
10999 +void yaffs_Deinitialise(yaffs_Device * dev)
11001 + if (dev->isMounted) {
11004 + yaffs_DeinitialiseBlocks(dev);
11005 + yaffs_DeinitialiseTnodes(dev);
11006 + yaffs_DeinitialiseObjects(dev);
11007 + if (dev->nShortOpCaches > 0 &&
11010 + for (i = 0; i < dev->nShortOpCaches; i++) {
11011 + if(dev->srCache[i].data)
11012 + YFREE(dev->srCache[i].data);
11013 + dev->srCache[i].data = NULL;
11016 + YFREE(dev->srCache);
11017 + dev->srCache = NULL;
11020 + YFREE(dev->gcCleanupList);
11022 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
11023 + YFREE(dev->tempBuffer[i].buffer);
11026 + dev->isMounted = 0;
11031 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
11036 + yaffs_BlockInfo *blk;
11038 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11040 + blk = yaffs_GetBlockInfo(dev, b);
11042 + switch (blk->blockState) {
11043 + case YAFFS_BLOCK_STATE_EMPTY:
11044 + case YAFFS_BLOCK_STATE_ALLOCATING:
11045 + case YAFFS_BLOCK_STATE_COLLECTING:
11046 + case YAFFS_BLOCK_STATE_FULL:
11048 + (dev->nChunksPerBlock - blk->pagesInUse +
11049 + blk->softDeletions);
11060 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
11062 + /* This is what we report to the outside world */
11065 + int nDirtyCacheChunks;
11066 + int blocksForCheckpoint;
11069 + nFree = dev->nFreeChunks;
11071 + nFree = yaffs_CountFreeChunks(dev);
11074 + nFree += dev->nDeletedFiles;
11076 + /* Now count the number of dirty chunks in the cache and subtract those */
11080 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11081 + if (dev->srCache[i].dirty)
11082 + nDirtyCacheChunks++;
11086 + nFree -= nDirtyCacheChunks;
11088 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11090 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11091 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
11092 + if(blocksForCheckpoint < 0)
11093 + blocksForCheckpoint = 0;
11095 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11104 +static int yaffs_freeVerificationFailures;
11106 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
11111 + if(yaffs_SkipVerification(dev))
11114 + counted = yaffs_CountFreeChunks(dev);
11116 + difference = dev->nFreeChunks - counted;
11118 + if (difference) {
11119 + T(YAFFS_TRACE_ALWAYS,
11120 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11121 + dev->nFreeChunks, counted, difference));
11122 + yaffs_freeVerificationFailures++;
11126 +/*---------------------------------------- YAFFS test code ----------------------*/
11128 +#define yaffs_CheckStruct(structure,syze, name) \
11129 + if(sizeof(structure) != syze) \
11131 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
11132 + name,syze,sizeof(structure))); \
11133 + return YAFFS_FAIL; \
11136 +static int yaffs_CheckStructures(void)
11138 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
11139 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
11140 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
11141 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11142 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
11144 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
11148 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.h
11149 ===================================================================
11150 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
11151 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_guts.h 2007-09-21 16:24:08.000000000 +0800
11154 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11156 + * Copyright (C) 2002-2007 Aleph One Ltd.
11157 + * for Toby Churchill Ltd and Brightstar Engineering
11159 + * Created by Charles Manning <charles@aleph1.co.uk>
11161 + * This program is free software; you can redistribute it and/or modify
11162 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11163 + * published by the Free Software Foundation.
11165 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11168 +#ifndef __YAFFS_GUTS_H__
11169 +#define __YAFFS_GUTS_H__
11171 +#include "devextras.h"
11172 +#include "yportenv.h"
11174 +#define YAFFS_OK 1
11175 +#define YAFFS_FAIL 0
11177 +/* Give us a Y=0x59,
11178 + * Give us an A=0x41,
11179 + * Give us an FF=0xFF
11180 + * Give us an S=0x53
11181 + * And what have we got...
11183 +#define YAFFS_MAGIC 0x5941FF53
11185 +#define YAFFS_NTNODES_LEVEL0 16
11186 +#define YAFFS_TNODES_LEVEL0_BITS 4
11187 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11189 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11190 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11191 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11192 +#define YAFFS_TNODES_MAX_LEVEL 6
11194 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11195 +#define YAFFS_BYTES_PER_SPARE 16
11196 +#define YAFFS_BYTES_PER_CHUNK 512
11197 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11198 +#define YAFFS_CHUNKS_PER_BLOCK 32
11199 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11202 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11203 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11205 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11207 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11209 +#define YAFFS_ALLOCATION_NOBJECTS 100
11210 +#define YAFFS_ALLOCATION_NTNODES 100
11211 +#define YAFFS_ALLOCATION_NLINKS 100
11213 +#define YAFFS_NOBJECT_BUCKETS 256
11216 +#define YAFFS_OBJECT_SPACE 0x40000
11218 +#define YAFFS_CHECKPOINT_VERSION 3
11220 +#ifdef CONFIG_YAFFS_UNICODE
11221 +#define YAFFS_MAX_NAME_LENGTH 127
11222 +#define YAFFS_MAX_ALIAS_LENGTH 79
11224 +#define YAFFS_MAX_NAME_LENGTH 255
11225 +#define YAFFS_MAX_ALIAS_LENGTH 159
11228 +#define YAFFS_SHORT_NAME_LENGTH 15
11230 +/* Some special object ids for pseudo objects */
11231 +#define YAFFS_OBJECTID_ROOT 1
11232 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11233 +#define YAFFS_OBJECTID_UNLINKED 3
11234 +#define YAFFS_OBJECTID_DELETED 4
11236 +/* Sseudo object ids for checkpointing */
11237 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11238 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11239 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11243 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11245 +#define YAFFS_N_TEMP_BUFFERS 4
11247 +/* We limit the number attempts at sucessfully saving a chunk of data.
11248 + * Small-page devices have 32 pages per block; large-page devices have 64.
11249 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11251 +#define YAFFS_WR_ATTEMPTS (5*64)
11253 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11254 + * The range is limited slightly to help distinguish bad numbers from good.
11255 + * This also allows us to perhaps in the future use special numbers for
11256 + * special purposes.
11257 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11258 + * and is a larger number than the lifetime of a 2GB device.
11260 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11261 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11263 +/* ChunkCache is used for short read/write operations.*/
11265 + struct yaffs_ObjectStruct *object;
11269 + int nBytes; /* Only valid if the cache is dirty */
11270 + int locked; /* Can't push out or flush while locked. */
11271 +#ifdef CONFIG_YAFFS_YAFFS2
11274 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11276 +} yaffs_ChunkCache;
11280 +/* Tags structures in RAM
11281 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11282 + * the structure size will get blown out.
11285 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11287 + unsigned chunkId:20;
11288 + unsigned serialNumber:2;
11289 + unsigned byteCount:10;
11290 + unsigned objectId:18;
11292 + unsigned unusedStuff:2;
11297 + yaffs_Tags asTags;
11299 +} yaffs_TagsUnion;
11303 +/* Stuff used for extended tags in YAFFS2 */
11306 + YAFFS_ECC_RESULT_UNKNOWN,
11307 + YAFFS_ECC_RESULT_NO_ERROR,
11308 + YAFFS_ECC_RESULT_FIXED,
11309 + YAFFS_ECC_RESULT_UNFIXED
11310 +} yaffs_ECCResult;
11313 + YAFFS_OBJECT_TYPE_UNKNOWN,
11314 + YAFFS_OBJECT_TYPE_FILE,
11315 + YAFFS_OBJECT_TYPE_SYMLINK,
11316 + YAFFS_OBJECT_TYPE_DIRECTORY,
11317 + YAFFS_OBJECT_TYPE_HARDLINK,
11318 + YAFFS_OBJECT_TYPE_SPECIAL
11319 +} yaffs_ObjectType;
11321 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11325 + unsigned validMarker0;
11326 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11327 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11328 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11329 + unsigned byteCount; /* Only valid for data chunks */
11331 + /* The following stuff only has meaning when we read */
11332 + yaffs_ECCResult eccResult;
11333 + unsigned blockBad;
11335 + /* YAFFS 1 stuff */
11336 + unsigned chunkDeleted; /* The chunk is marked deleted */
11337 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11339 + /* YAFFS2 stuff */
11340 + unsigned sequenceNumber; /* The sequence number of this block */
11342 + /* Extra info if this is an object header (YAFFS2 only) */
11344 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11345 + unsigned extraParentObjectId; /* The parent object */
11346 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11347 + unsigned extraShadows; /* Does this shadow another object? */
11349 + yaffs_ObjectType extraObjectType; /* What object type? */
11351 + unsigned extraFileLength; /* Length if it is a file */
11352 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11354 + unsigned validMarker1;
11356 +} yaffs_ExtendedTags;
11358 +/* Spare structure for YAFFS1 */
11364 + __u8 pageStatus; /* set to 0 to delete the chunk */
11365 + __u8 blockStatus;
11374 +/*Special structure for passing through to mtd */
11375 +struct yaffs_NANDSpare {
11376 + yaffs_Spare spare;
11381 +/* Block data in RAM */
11384 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11386 + YAFFS_BLOCK_STATE_SCANNING,
11387 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11388 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11389 + * but it needs to be scanned to determine its true state.
11390 + * This state is only valid during yaffs_Scan.
11391 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11392 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11395 + YAFFS_BLOCK_STATE_EMPTY,
11396 + /* This block is empty */
11398 + YAFFS_BLOCK_STATE_ALLOCATING,
11399 + /* This block is partially allocated.
11400 + * At least one page holds valid data.
11401 + * This is the one currently being used for page
11402 + * allocation. Should never be more than one of these
11405 + YAFFS_BLOCK_STATE_FULL,
11406 + /* All the pages in this block have been allocated.
11409 + YAFFS_BLOCK_STATE_DIRTY,
11410 + /* All pages have been allocated and deleted.
11411 + * Erase me, reuse me.
11414 + YAFFS_BLOCK_STATE_CHECKPOINT,
11415 + /* This block is assigned to holding checkpoint data.
11418 + YAFFS_BLOCK_STATE_COLLECTING,
11419 + /* This block is being garbage collected */
11421 + YAFFS_BLOCK_STATE_DEAD
11422 + /* This block has failed and is not in use */
11423 +} yaffs_BlockState;
11425 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11430 + int softDeletions:10; /* number of soft deleted pages */
11431 + int pagesInUse:10; /* number of pages in use */
11432 + yaffs_BlockState blockState:4; /* One of the above block states */
11433 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11434 + /* and retire the block. */
11435 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
11436 + __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
11437 + It should be prioritised for GC */
11438 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11440 +#ifdef CONFIG_YAFFS_YAFFS2
11441 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11442 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11445 +} yaffs_BlockInfo;
11447 +/* -------------------------- Object structure -------------------------------*/
11448 +/* This is the object structure as stored on NAND */
11451 + yaffs_ObjectType type;
11453 + /* Apply to everything */
11454 + int parentObjectId;
11455 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11456 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11458 + /* Thes following apply to directories, files, symlinks - not hard links */
11459 + __u32 yst_mode; /* protection */
11461 +#ifdef CONFIG_YAFFS_WINCE
11462 + __u32 notForWinCE[5];
11471 + /* File size applies to files only */
11474 + /* Equivalent object id applies to hard links only. */
11475 + int equivalentObjectId;
11477 + /* Alias is for symlinks only. */
11478 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11480 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11482 +#ifdef CONFIG_YAFFS_WINCE
11483 + __u32 win_ctime[2];
11484 + __u32 win_atime[2];
11485 + __u32 win_mtime[2];
11486 + __u32 roomToGrow[4];
11488 + __u32 roomToGrow[10];
11491 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11493 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11496 +} yaffs_ObjectHeader;
11498 +/*--------------------------- Tnode -------------------------- */
11500 +union yaffs_Tnode_union {
11501 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11502 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11504 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11506 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11510 +typedef union yaffs_Tnode_union yaffs_Tnode;
11512 +struct yaffs_TnodeList_struct {
11513 + struct yaffs_TnodeList_struct *next;
11514 + yaffs_Tnode *tnodes;
11517 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11519 +/*------------------------ Object -----------------------------*/
11520 +/* An object can be one of:
11521 + * - a directory (no data, has children links
11522 + * - a regular file (data.... not prunes :->).
11523 + * - a symlink [symbolic link] (the alias).
11529 + __u32 scannedFileSize;
11530 + __u32 shrinkSize;
11532 + yaffs_Tnode *top;
11533 +} yaffs_FileStructure;
11536 + struct list_head children; /* list of child links */
11537 +} yaffs_DirectoryStructure;
11541 +} yaffs_SymLinkStructure;
11544 + struct yaffs_ObjectStruct *equivalentObject;
11545 + __u32 equivalentObjectId;
11546 +} yaffs_HardLinkStructure;
11549 + yaffs_FileStructure fileVariant;
11550 + yaffs_DirectoryStructure directoryVariant;
11551 + yaffs_SymLinkStructure symLinkVariant;
11552 + yaffs_HardLinkStructure hardLinkVariant;
11553 +} yaffs_ObjectVariant;
11555 +struct yaffs_ObjectStruct {
11556 + __u8 deleted:1; /* This should only apply to unlinked files. */
11557 + __u8 softDeleted:1; /* it has also been soft deleted */
11558 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11559 + __u8 fake:1; /* A fake object has no presence on NAND. */
11560 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11561 + __u8 unlinkAllowed:1;
11562 + __u8 dirty:1; /* the object needs to be written to flash */
11563 + __u8 valid:1; /* When the file system is being loaded up, this
11564 + * object might be created before the data
11565 + * is available (ie. file data records appear before the header).
11567 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11569 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11570 + * still in the inode cache. Free of object is defered.
11571 + * until the inode is released.
11574 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11575 + __u16 sum; /* sum of the name to speed searching */
11577 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11579 + struct list_head hashLink; /* list of objects in this hash bucket */
11581 + struct list_head hardLinks; /* all the equivalent hard linked objects */
11583 + /* directory structure stuff */
11584 + /* also used for linking up the free list */
11585 + struct yaffs_ObjectStruct *parent;
11586 + struct list_head siblings;
11588 + /* Where's my object header in NAND? */
11591 + int nDataChunks; /* Number of data chunks attached to the file. */
11593 + __u32 objectId; /* the object id value */
11597 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11598 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11601 +#ifndef __KERNEL__
11605 +#ifdef CONFIG_YAFFS_WINCE
11606 + __u32 win_ctime[2];
11607 + __u32 win_mtime[2];
11608 + __u32 win_atime[2];
11620 + struct inode *myInode;
11624 + yaffs_ObjectType variantType;
11626 + yaffs_ObjectVariant variant;
11630 +typedef struct yaffs_ObjectStruct yaffs_Object;
11632 +struct yaffs_ObjectList_struct {
11633 + yaffs_Object *objects;
11634 + struct yaffs_ObjectList_struct *next;
11637 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
11640 + struct list_head list;
11642 +} yaffs_ObjectBucket;
11645 +/* yaffs_CheckpointObject holds the definition of an object as dumped
11646 + * by checkpointing.
11655 + yaffs_ObjectType variantType:3;
11657 + __u8 softDeleted:1;
11660 + __u8 renameAllowed:1;
11661 + __u8 unlinkAllowed:1;
11665 + __u32 fileSizeOrEquivalentObjectId;
11667 +}yaffs_CheckpointObject;
11669 +/*--------------------- Temporary buffers ----------------
11671 + * These are chunk-sized working buffers. Each device has a few
11676 + int line; /* track from whence this buffer was allocated */
11678 +} yaffs_TempBuffer;
11680 +/*----------------- Device ---------------------------------*/
11682 +struct yaffs_DeviceStruct {
11683 + struct list_head devList;
11684 + const char *name;
11686 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
11687 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
11688 + int nChunksPerBlock; /* does not need to be a power of 2 */
11689 + int nBytesPerSpare; /* spare area size */
11690 + int startBlock; /* Start block we're allowed to use */
11691 + int endBlock; /* End block we're allowed to use */
11692 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
11693 + /* reserved blocks on NOR and RAM. */
11696 + /* Stuff used by the shared space checkpointing mechanism */
11697 + /* If this value is zero, then this mechanism is disabled */
11699 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
11704 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
11705 + * the number of short op caches (don't use too many)
11708 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
11710 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
11712 + void *genericDevice; /* Pointer to device context
11713 + * On an mtd this holds the mtd pointer.
11715 + void *superBlock;
11717 + /* NAND access functions (Must be set before calling YAFFS)*/
11719 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
11720 + int chunkInNAND, const __u8 * data,
11721 + const yaffs_Spare * spare);
11722 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
11723 + int chunkInNAND, __u8 * data,
11724 + yaffs_Spare * spare);
11725 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
11726 + int blockInNAND);
11727 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
11729 +#ifdef CONFIG_YAFFS_YAFFS2
11730 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
11731 + int chunkInNAND, const __u8 * data,
11732 + const yaffs_ExtendedTags * tags);
11733 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
11734 + int chunkInNAND, __u8 * data,
11735 + yaffs_ExtendedTags * tags);
11736 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
11737 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
11738 + yaffs_BlockState * state, int *sequenceNumber);
11743 + /* The removeObjectCallback function must be supplied by OS flavours that
11744 + * need it. The Linux kernel does not use this, but yaffs direct does use
11745 + * it to implement the faster readdir
11747 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
11749 + /* Callback to mark the superblock dirsty */
11750 + void (*markSuperBlockDirty)(void * superblock);
11752 + int wideTnodesDisabled; /* Set to disable wide tnodes */
11755 + /* End of stuff that must be set before initialisation. */
11757 + /* Checkpoint control. Can be set before or after initialisation */
11758 + __u8 skipCheckpointRead;
11759 + __u8 skipCheckpointWrite;
11761 + /* Runtime parameters. Set up by YAFFS. */
11763 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
11764 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
11766 + /* Stuff to support wide tnodes */
11767 + __u32 tnodeWidth;
11770 + /* Stuff to support various file offses to chunk/offset translations */
11771 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
11773 + __u32 crumbShift;
11774 + __u32 crumbsPerChunk;
11776 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
11777 + __u32 chunkShift;
11783 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
11784 + struct semaphore grossLock; /* Gross locking semaphore */
11785 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
11786 + * at compile time so we have to allocate it.
11788 + void (*putSuperFunc) (struct super_block * sb);
11793 + int isCheckpointed;
11796 + /* Stuff to support block offsetting to support start block zero */
11797 + int internalStartBlock;
11798 + int internalEndBlock;
11803 + /* Runtime checkpointing stuff */
11804 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
11805 + int checkpointByteCount;
11806 + int checkpointByteOffset;
11807 + __u8 *checkpointBuffer;
11808 + int checkpointOpenForWrite;
11809 + int blocksInCheckpoint;
11810 + int checkpointCurrentChunk;
11811 + int checkpointCurrentBlock;
11812 + int checkpointNextBlock;
11813 + int *checkpointBlockList;
11814 + int checkpointMaxBlocks;
11815 + __u32 checkpointSum;
11816 + __u32 checkpointXor;
11819 + yaffs_BlockInfo *blockInfo;
11820 + __u8 *chunkBits; /* bitmap of chunks in use */
11821 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
11822 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
11823 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
11824 + * Must be consistent with nChunksPerBlock.
11827 + int nErasedBlocks;
11828 + int allocationBlock; /* Current block being allocated off */
11829 + __u32 allocationPage;
11830 + int allocationBlockFinder; /* Used to search for next allocation block */
11832 + /* Runtime state */
11833 + int nTnodesCreated;
11834 + yaffs_Tnode *freeTnodes;
11836 + yaffs_TnodeList *allocatedTnodeList;
11840 + int nObjectsCreated;
11841 + yaffs_Object *freeObjects;
11842 + int nFreeObjects;
11844 + yaffs_ObjectList *allocatedObjectList;
11846 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
11850 + int currentDirtyChecker; /* Used to find current dirtiest block */
11852 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
11853 + int nonAggressiveSkip; /* GC state/mode */
11858 + int nBlockErasures;
11859 + int nErasureFailures;
11861 + int garbageCollections;
11862 + int passiveGarbageCollections;
11863 + int nRetriedWrites;
11864 + int nRetiredBlocks;
11867 + int tagsEccFixed;
11868 + int tagsEccUnfixed;
11870 + int nUnmarkedDeletions;
11872 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
11874 + /* Special directories */
11875 + yaffs_Object *rootDir;
11876 + yaffs_Object *lostNFoundDir;
11878 + /* Buffer areas for storing data to recover from write failures TODO
11879 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
11880 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
11883 + int bufferedBlock; /* Which block is buffered here? */
11884 + int doingBufferedBlockRewrite;
11886 + yaffs_ChunkCache *srCache;
11891 + /* Stuff for background deletion and unlinked files.*/
11892 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
11893 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
11894 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
11895 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11896 + int nUnlinkedFiles; /* Count of unlinked files. */
11897 + int nBackgroundDeletions; /* Count of background deletions. */
11900 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
11902 + int unmanagedTempAllocations;
11903 + int unmanagedTempDeallocations;
11905 + /* yaffs2 runtime stuff */
11906 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11907 + unsigned oldestDirtySequence;
11911 +typedef struct yaffs_DeviceStruct yaffs_Device;
11913 +/* The static layout of bllock usage etc is stored in the super block header */
11917 + int checkpointStartBlock;
11918 + int checkpointEndBlock;
11922 +} yaffs_SuperBlockHeader;
11924 +/* The CheckpointDevice structure holds the device information that changes at runtime and
11925 + * must be preserved over unmount/mount cycles.
11929 + int nErasedBlocks;
11930 + int allocationBlock; /* Current block being allocated off */
11931 + __u32 allocationPage;
11934 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11935 + int nUnlinkedFiles; /* Count of unlinked files. */
11936 + int nBackgroundDeletions; /* Count of background deletions. */
11938 + /* yaffs2 runtime stuff */
11939 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11940 + unsigned oldestDirtySequence;
11942 +} yaffs_CheckpointDevice;
11950 +} yaffs_CheckpointValidity;
11952 +/* Function to manipulate block info */
11953 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
11955 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
11956 + T(YAFFS_TRACE_ERROR,
11958 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
11962 + return &dev->blockInfo[blk - dev->internalStartBlock];
11965 +/*----------------------- YAFFS Functions -----------------------*/
11967 +int yaffs_GutsInitialise(yaffs_Device * dev);
11968 +void yaffs_Deinitialise(yaffs_Device * dev);
11970 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
11972 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
11973 + yaffs_Object * newDir, const YCHAR * newName);
11975 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
11976 +int yaffs_DeleteFile(yaffs_Object * obj);
11978 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
11979 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
11980 +int yaffs_GetObjectInode(yaffs_Object * obj);
11981 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
11982 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
11984 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
11985 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
11987 +/* File operations */
11988 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
11990 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
11991 + int nBytes, int writeThrough);
11992 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
11994 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
11995 + __u32 mode, __u32 uid, __u32 gid);
11996 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
11998 +/* Flushing and checkpointing */
11999 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
12001 +int yaffs_CheckpointSave(yaffs_Device *dev);
12002 +int yaffs_CheckpointRestore(yaffs_Device *dev);
12004 +/* Directory operations */
12005 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
12006 + __u32 mode, __u32 uid, __u32 gid);
12007 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
12008 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
12009 + int (*fn) (yaffs_Object *));
12011 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
12013 +/* Link operations */
12014 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
12015 + yaffs_Object * equivalentObject);
12017 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
12019 +/* Symlink operations */
12020 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
12021 + __u32 mode, __u32 uid, __u32 gid,
12022 + const YCHAR * alias);
12023 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
12025 +/* Special inodes (fifos, sockets and devices) */
12026 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
12027 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
12029 +/* Special directories */
12030 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
12031 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
12033 +#ifdef CONFIG_YAFFS_WINCE
12034 +/* CONFIG_YAFFS_WINCE special stuff */
12035 +void yfsd_WinFileTimeNow(__u32 target[2]);
12040 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
12044 +int yaffs_DumpObject(yaffs_Object * obj);
12046 +void yaffs_GutsTest(yaffs_Device * dev);
12048 +/* A few useful functions */
12049 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12050 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
12051 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
12052 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12055 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.c
12056 ===================================================================
12057 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12058 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.c 2007-09-21 16:24:08.000000000 +0800
12061 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12063 + * Copyright (C) 2002-2007 Aleph One Ltd.
12064 + * for Toby Churchill Ltd and Brightstar Engineering
12066 + * Created by Charles Manning <charles@aleph1.co.uk>
12068 + * This program is free software; you can redistribute it and/or modify
12069 + * it under the terms of the GNU General Public License version 2 as
12070 + * published by the Free Software Foundation.
12073 +const char *yaffs_mtdif_c_version =
12074 + "$Id: yaffs_mtdif.c,v 1.19 2007-02-14 01:09:06 wookey Exp $";
12076 +#include "yportenv.h"
12079 +#include "yaffs_mtdif.h"
12081 +#include "linux/mtd/mtd.h"
12082 +#include "linux/types.h"
12083 +#include "linux/time.h"
12084 +#include "linux/mtd/nand.h"
12086 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
12087 +static struct nand_oobinfo yaffs_oobinfo = {
12090 + .eccpos = {8, 9, 10, 13, 14, 15}
12093 +static struct nand_oobinfo yaffs_noeccinfo = {
12098 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12099 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
12101 + oob[0] = spare->tagByte0;
12102 + oob[1] = spare->tagByte1;
12103 + oob[2] = spare->tagByte2;
12104 + oob[3] = spare->tagByte3;
12105 + oob[4] = spare->tagByte4;
12106 + oob[5] = spare->tagByte5 & 0x3f;
12107 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
12108 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
12109 + oob[6] = spare->tagByte6;
12110 + oob[7] = spare->tagByte7;
12113 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
12115 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
12116 + spare->tagByte0 = oob[0];
12117 + spare->tagByte1 = oob[1];
12118 + spare->tagByte2 = oob[2];
12119 + spare->tagByte3 = oob[3];
12120 + spare->tagByte4 = oob[4];
12121 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
12122 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
12123 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
12124 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
12125 + spare->tagByte6 = oob[6];
12126 + spare->tagByte7 = oob[7];
12127 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
12129 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
12133 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12134 + const __u8 * data, const yaffs_Spare * spare)
12136 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12137 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12138 + struct mtd_oob_ops ops;
12143 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12144 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12145 + __u8 spareAsBytes[8]; /* OOB */
12147 + if (data && !spare)
12148 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
12150 + else if (spare) {
12151 + if (dev->useNANDECC) {
12152 + translate_spare2oob(spare, spareAsBytes);
12153 + ops.mode = MTD_OOB_AUTO;
12154 + ops.ooblen = 8; /* temp hack */
12156 + ops.mode = MTD_OOB_RAW;
12157 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12159 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12160 + ops.datbuf = (u8 *)data;
12162 + ops.oobbuf = spareAsBytes;
12163 + retval = mtd->write_oob(mtd, addr, &ops);
12166 + __u8 *spareAsBytes = (__u8 *) spare;
12168 + if (data && spare) {
12169 + if (dev->useNANDECC)
12171 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12172 + &dummy, data, spareAsBytes,
12176 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12177 + &dummy, data, spareAsBytes,
12178 + &yaffs_noeccinfo);
12182 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12186 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12187 + &dummy, spareAsBytes);
12194 + return YAFFS_FAIL;
12197 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12198 + yaffs_Spare * spare)
12200 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12201 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12202 + struct mtd_oob_ops ops;
12207 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12208 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12209 + __u8 spareAsBytes[8]; /* OOB */
12211 + if (data && !spare)
12212 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
12214 + else if (spare) {
12215 + if (dev->useNANDECC) {
12216 + ops.mode = MTD_OOB_AUTO;
12217 + ops.ooblen = 8; /* temp hack */
12219 + ops.mode = MTD_OOB_RAW;
12220 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12222 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12223 + ops.datbuf = data;
12225 + ops.oobbuf = spareAsBytes;
12226 + retval = mtd->read_oob(mtd, addr, &ops);
12227 + if (dev->useNANDECC)
12228 + translate_oob2spare(spare, spareAsBytes);
12231 + __u8 *spareAsBytes = (__u8 *) spare;
12233 + if (data && spare) {
12234 + if (dev->useNANDECC) {
12235 + /* Careful, this call adds 2 ints */
12236 + /* to the end of the spare data. Calling function */
12237 + /* should allocate enough memory for spare, */
12238 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
12240 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12241 + &dummy, data, spareAsBytes,
12245 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12246 + &dummy, data, spareAsBytes,
12247 + &yaffs_noeccinfo);
12252 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12256 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12257 + &dummy, spareAsBytes);
12264 + return YAFFS_FAIL;
12267 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
12269 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12271 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
12272 + * dev->nChunksPerBlock;
12273 + struct erase_info ei;
12278 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
12281 + ei.callback = NULL;
12282 + ei.priv = (u_long) dev;
12284 + /* Todo finish off the ei if required */
12286 + sema_init(&dev->sem, 0);
12288 + retval = mtd->erase(mtd, &ei);
12293 + return YAFFS_FAIL;
12296 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
12301 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.h
12302 ===================================================================
12303 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12304 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif.h 2007-09-21 16:24:08.000000000 +0800
12307 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12309 + * Copyright (C) 2002-2007 Aleph One Ltd.
12310 + * for Toby Churchill Ltd and Brightstar Engineering
12312 + * Created by Charles Manning <charles@aleph1.co.uk>
12314 + * This program is free software; you can redistribute it and/or modify
12315 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12316 + * published by the Free Software Foundation.
12318 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12321 +#ifndef __YAFFS_MTDIF_H__
12322 +#define __YAFFS_MTDIF_H__
12324 +#include "yaffs_guts.h"
12326 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12327 + const __u8 * data, const yaffs_Spare * spare);
12328 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12329 + yaffs_Spare * spare);
12330 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
12331 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
12333 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1-compat.c
12334 ===================================================================
12335 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12336 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1-compat.c 2007-09-21 16:24:08.000000000 +0800
12338 +From ian@brightstareng.com Fri May 18 15:06:49 2007
12339 +From ian@brightstareng.com Fri May 18 15:08:21 2007
12340 +Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
12341 + by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
12342 + (envelope-from <ian@brightstareng.com>)
12343 + id 1Hp380-00011e-T6
12344 + for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
12345 +Received: from localhost (localhost.localdomain [127.0.0.1])
12346 + by zebra.brightstareng.com (Postfix) with ESMTP
12347 + id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
12348 +Received: from zebra.brightstareng.com ([127.0.0.1])
12349 + by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
12350 + id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
12351 +Received: from pippin (unknown [192.168.1.25])
12352 + by zebra.brightstareng.com (Postfix) with ESMTP
12353 + id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
12354 +From: Ian McDonnell <ian@brightstareng.com>
12355 +To: David Goodenough <david.goodenough@linkchoose.co.uk>
12356 +Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
12357 +Date: Fri, 18 May 2007 10:06:49 -0400
12358 +User-Agent: KMail/1.9.1
12359 +References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
12360 +In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
12361 +Cc: Andrea Conti <alyf@alyf.net>,
12362 + Charles Manning <manningc2@actrix.gen.nz>
12364 +Content-Type: Multipart/Mixed;
12365 + boundary="Boundary-00=_5LbTGmt62YoutxM"
12366 +Message-Id: <200705181006.49860.ian@brightstareng.com>
12367 +X-Virus-Scanned: by amavisd-new at brightstareng.com
12370 +X-KMail-EncryptionState:
12371 +X-KMail-SignatureState:
12374 +--Boundary-00=_5LbTGmt62YoutxM
12375 +Content-Type: text/plain;
12376 + charset="iso-8859-15"
12377 +Content-Transfer-Encoding: 7bit
12378 +Content-Disposition: inline
12382 +On Friday 18 May 2007 08:34, you wrote:
12383 +> Yea team. With this fix in place (I put it in the wrong place
12384 +> at first) I can now mount and ls the Yaffs partition without
12385 +> an error messages!
12389 +Attached is a newer yaffs_mtdif1.c with a bandaid to help the
12390 +2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
12391 +See the LINUX_VERSION_CODE conditional in
12392 +nandmtd1_ReadChunkWithTagsFromNAND.
12396 +--Boundary-00=_5LbTGmt62YoutxM
12397 +Content-Type: text/x-csrc;
12398 + charset="iso-8859-15";
12399 + name="yaffs_mtdif1.c"
12400 +Content-Transfer-Encoding: 7bit
12401 +Content-Disposition: attachment;
12402 + filename="yaffs_mtdif1.c"
12405 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12406 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12408 + * Copyright (C) 2002 Aleph One Ltd.
12409 + * for Toby Churchill Ltd and Brightstar Engineering
12411 + * This program is free software; you can redistribute it and/or modify
12412 + * it under the terms of the GNU General Public License version 2 as
12413 + * published by the Free Software Foundation.
12417 + * This module provides the interface between yaffs_nand.c and the
12418 + * MTD API. This version is used when the MTD interface supports the
12419 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12420 + * and we have small-page NAND device.
12422 + * These functions are invoked via function pointers in yaffs_nand.c.
12423 + * This replaces functionality provided by functions in yaffs_mtdif.c
12424 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12425 + * called in yaffs_mtdif.c when the function pointers are NULL.
12426 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12429 +#include "yportenv.h"
12430 +#include "yaffs_guts.h"
12431 +#include "yaffs_packedtags1.h"
12432 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12434 +#include "linux/kernel.h"
12435 +#include "linux/version.h"
12436 +#include "linux/types.h"
12437 +#include "linux/mtd/mtd.h"
12439 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12440 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12442 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12444 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12445 +# define YTAG1_SIZE 8
12447 +# define YTAG1_SIZE 9
12451 +/* Use the following nand_ecclayout with MTD when using
12452 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12453 + * If you have existing Yaffs images and the byte order differs from this,
12454 + * adjust 'oobfree' to match your existing Yaffs data.
12456 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12457 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12460 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12461 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12462 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12463 + * byte and B is the small-page bad-block indicator byte.
12465 +static struct nand_ecclayout nand_oob_16 = {
12467 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12469 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12473 +/* Write a chunk (page) of data to NAND.
12475 + * Caller always provides ExtendedTags data which are converted to a more
12476 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12477 + * contents of the tags meta-data; used to valid the tags when read.
12479 + * - Pack ExtendedTags to PackedTags1 form
12480 + * - Compute mini-ECC for PackedTags1
12481 + * - Write data and packed tags to NAND.
12483 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12484 + * a full sequence number (as found in the larger PackedTags2 form) it is
12485 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12486 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12487 + * to be written just once. When Yaffs performs this operation, this
12488 + * function is called with a NULL data pointer -- calling MTD write_oob
12489 + * without data is valid usage (2.6.17).
12491 + * Any underlying MTD error results in YAFFS_FAIL.
12492 + * Returns YAFFS_OK or YAFFS_FAIL.
12494 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12495 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12497 + struct mtd_info * mtd = dev->genericDevice;
12498 + int chunkBytes = dev->nDataBytesPerChunk;
12499 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12500 + struct mtd_oob_ops ops;
12501 + yaffs_PackedTags1 pt1;
12504 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12505 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12506 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12508 + yaffs_PackTags1(&pt1, etags);
12509 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12511 + /* When deleting a chunk, the upper layer provides only skeletal
12512 + * etags, one with chunkDeleted set. However, we need to update the
12513 + * tags, not erase them completely. So we use the NAND write property
12514 + * that only zeroed-bits stick and set tag bytes to all-ones and
12515 + * zero just the (not) deleted bit.
12517 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12518 + if (etags->chunkDeleted) {
12519 + memset(&pt1, 0xff, 8);
12520 + /* clear delete status bit to indicate deleted */
12524 + ((__u8 *)&pt1)[8] = 0xff;
12525 + if (etags->chunkDeleted) {
12526 + memset(&pt1, 0xff, 8);
12527 + /* zero pageStatus byte to indicate deleted */
12528 + ((__u8 *)&pt1)[8] = 0;
12532 + memset(&ops, 0, sizeof(ops));
12533 + ops.mode = MTD_OOB_AUTO;
12534 + ops.len = (data) ? chunkBytes : 0;
12535 + ops.ooblen = YTAG1_SIZE;
12536 + ops.datbuf = (__u8 *)data;
12537 + ops.oobbuf = (__u8 *)&pt1;
12539 + retval = mtd->write_oob(mtd, addr, &ops);
12541 + yaffs_trace(YAFFS_TRACE_MTD,
12542 + "write_oob failed, chunk %d, mtd error %d\n",
12543 + chunkInNAND, retval);
12545 + return retval ? YAFFS_FAIL : YAFFS_OK;
12548 +/* Return with empty ExtendedTags but add eccResult.
12550 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12553 + memset(etags, 0, sizeof(*etags));
12554 + etags->eccResult = eccResult;
12559 +/* Read a chunk (page) from NAND.
12561 + * Caller expects ExtendedTags data to be usable even on error; that is,
12562 + * all members except eccResult and blockBad are zeroed.
12564 + * - Check ECC results for data (if applicable)
12565 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12566 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12567 + * - Convert PackedTags1 to ExtendedTags
12568 + * - Update eccResult and blockBad members to refect state.
12570 + * Returns YAFFS_OK or YAFFS_FAIL.
12572 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12573 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12575 + struct mtd_info * mtd = dev->genericDevice;
12576 + int chunkBytes = dev->nDataBytesPerChunk;
12577 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12578 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12579 + struct mtd_oob_ops ops;
12580 + yaffs_PackedTags1 pt1;
12584 + memset(&ops, 0, sizeof(ops));
12585 + ops.mode = MTD_OOB_AUTO;
12586 + ops.len = (data) ? chunkBytes : 0;
12587 + ops.ooblen = YTAG1_SIZE;
12588 + ops.datbuf = data;
12589 + ops.oobbuf = (__u8 *)&pt1;
12591 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12592 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12593 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12595 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12597 + /* Read page and oob using MTD.
12598 + * Check status and determine ECC result.
12600 + retval = mtd->read_oob(mtd, addr, &ops);
12602 + yaffs_trace(YAFFS_TRACE_MTD,
12603 + "read_oob failed, chunk %d, mtd error %d\n",
12604 + chunkInNAND, retval);
12607 + switch (retval) {
12613 + /* MTD's ECC fixed the data */
12614 + eccres = YAFFS_ECC_RESULT_FIXED;
12619 + /* MTD's ECC could not fix the data */
12620 + dev->eccUnfixed++;
12621 + /* fall into... */
12623 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12624 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12625 + return YAFFS_FAIL;
12628 + /* Check for a blank/erased chunk.
12630 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12631 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12632 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12635 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12636 + /* Read deleted status (bit) then return it to it's non-deleted
12637 + * state before performing tags mini-ECC check. pt1.deleted is
12640 + deleted = !pt1.deleted;
12643 + (void) deleted; /* not used */
12646 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12648 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12649 + switch (retval) {
12651 + /* no tags error, use MTD result */
12654 + /* recovered tags-ECC error */
12655 + dev->tagsEccFixed++;
12656 + eccres = YAFFS_ECC_RESULT_FIXED;
12659 + /* unrecovered tags-ECC error */
12660 + dev->tagsEccUnfixed++;
12661 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12664 + /* Unpack the tags to extended form and set ECC result.
12665 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12667 + pt1.shouldBeFF = 0xFFFFFFFF;
12668 + yaffs_UnpackTags1(etags, &pt1);
12669 + etags->eccResult = eccres;
12671 + /* Set deleted state.
12673 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12674 + etags->chunkDeleted = deleted;
12676 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12681 +/* Mark a block bad.
12683 + * This is a persistant state.
12684 + * Use of this function should be rare.
12686 + * Returns YAFFS_OK or YAFFS_FAIL.
12688 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12690 + struct mtd_info * mtd = dev->genericDevice;
12691 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12694 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
12696 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12697 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12700 +/* Check any MTD prerequists.
12702 + * Returns YAFFS_OK or YAFFS_FAIL.
12704 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
12706 + /* 2.6.18 has mtd->ecclayout->oobavail */
12707 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12708 + int oobavail = mtd->ecclayout->oobavail;
12710 + if (oobavail < YTAG1_SIZE) {
12711 + yaffs_trace(YAFFS_TRACE_ERROR,
12712 + "mtd device has only %d bytes for tags, need %d",
12713 + oobavail, YTAG1_SIZE);
12714 + return YAFFS_FAIL;
12719 +/* Query for the current state of a specific block.
12721 + * Examine the tags of the first chunk of the block and return the state:
12722 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12723 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12724 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12726 + * Always returns YAFFS_OK.
12728 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12729 + yaffs_BlockState * pState, int *pSequenceNumber)
12731 + struct mtd_info * mtd = dev->genericDevice;
12732 + int chunkNo = blockNo * dev->nChunksPerBlock;
12733 + yaffs_ExtendedTags etags;
12734 + int state = YAFFS_BLOCK_STATE_DEAD;
12738 + /* We don't yet have a good place to test for MTD config prerequists.
12739 + * Do it here as we are called during the initial scan.
12741 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
12742 + return YAFFS_FAIL;
12745 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12746 + if (etags.blockBad) {
12747 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12748 + "block %d is marked bad", blockNo);
12749 + state = YAFFS_BLOCK_STATE_DEAD;
12751 + else if (etags.chunkUsed) {
12752 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12753 + seqnum = etags.sequenceNumber;
12756 + state = YAFFS_BLOCK_STATE_EMPTY;
12760 + *pSequenceNumber = seqnum;
12762 + /* query always succeeds */
12766 +#endif /*KERNEL_VERSION*/
12768 +--Boundary-00=_5LbTGmt62YoutxM--
12772 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.c
12773 ===================================================================
12774 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
12775 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.c 2007-09-21 16:24:08.000000000 +0800
12778 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12779 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12781 + * Copyright (C) 2002 Aleph One Ltd.
12782 + * for Toby Churchill Ltd and Brightstar Engineering
12784 + * This program is free software; you can redistribute it and/or modify
12785 + * it under the terms of the GNU General Public License version 2 as
12786 + * published by the Free Software Foundation.
12790 + * This module provides the interface between yaffs_nand.c and the
12791 + * MTD API. This version is used when the MTD interface supports the
12792 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12793 + * and we have small-page NAND device.
12795 + * These functions are invoked via function pointers in yaffs_nand.c.
12796 + * This replaces functionality provided by functions in yaffs_mtdif.c
12797 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12798 + * called in yaffs_mtdif.c when the function pointers are NULL.
12799 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12802 +#include "yportenv.h"
12803 +#include "yaffs_guts.h"
12804 +#include "yaffs_packedtags1.h"
12805 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12807 +#include "linux/kernel.h"
12808 +#include "linux/version.h"
12809 +#include "linux/types.h"
12810 +#include "linux/mtd/mtd.h"
12812 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12813 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12815 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12817 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12818 +# define YTAG1_SIZE 8
12820 +# define YTAG1_SIZE 9
12824 +/* Use the following nand_ecclayout with MTD when using
12825 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12826 + * If you have existing Yaffs images and the byte order differs from this,
12827 + * adjust 'oobfree' to match your existing Yaffs data.
12829 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12830 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12833 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12834 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12835 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12836 + * byte and B is the small-page bad-block indicator byte.
12838 +static struct nand_ecclayout nand_oob_16 = {
12840 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12842 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12846 +/* Write a chunk (page) of data to NAND.
12848 + * Caller always provides ExtendedTags data which are converted to a more
12849 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12850 + * contents of the tags meta-data; used to valid the tags when read.
12852 + * - Pack ExtendedTags to PackedTags1 form
12853 + * - Compute mini-ECC for PackedTags1
12854 + * - Write data and packed tags to NAND.
12856 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12857 + * a full sequence number (as found in the larger PackedTags2 form) it is
12858 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12859 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12860 + * to be written just once. When Yaffs performs this operation, this
12861 + * function is called with a NULL data pointer -- calling MTD write_oob
12862 + * without data is valid usage (2.6.17).
12864 + * Any underlying MTD error results in YAFFS_FAIL.
12865 + * Returns YAFFS_OK or YAFFS_FAIL.
12867 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12868 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12870 + struct mtd_info * mtd = dev->genericDevice;
12871 + int chunkBytes = dev->nDataBytesPerChunk;
12872 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12873 + struct mtd_oob_ops ops;
12874 + yaffs_PackedTags1 pt1;
12877 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12878 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12879 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12881 + dev->nPageWrites++;
12883 + yaffs_PackTags1(&pt1, etags);
12884 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12886 + /* When deleting a chunk, the upper layer provides only skeletal
12887 + * etags, one with chunkDeleted set. However, we need to update the
12888 + * tags, not erase them completely. So we use the NAND write property
12889 + * that only zeroed-bits stick and set tag bytes to all-ones and
12890 + * zero just the (not) deleted bit.
12892 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12893 + if (etags->chunkDeleted) {
12894 + memset(&pt1, 0xff, 8);
12895 + /* clear delete status bit to indicate deleted */
12899 + ((__u8 *)&pt1)[8] = 0xff;
12900 + if (etags->chunkDeleted) {
12901 + memset(&pt1, 0xff, 8);
12902 + /* zero pageStatus byte to indicate deleted */
12903 + ((__u8 *)&pt1)[8] = 0;
12907 + memset(&ops, 0, sizeof(ops));
12908 + ops.mode = MTD_OOB_AUTO;
12909 + ops.len = (data) ? chunkBytes : 0;
12910 + ops.ooblen = YTAG1_SIZE;
12911 + ops.datbuf = (__u8 *)data;
12912 + ops.oobbuf = (__u8 *)&pt1;
12914 + retval = mtd->write_oob(mtd, addr, &ops);
12916 + yaffs_trace(YAFFS_TRACE_MTD,
12917 + "write_oob failed, chunk %d, mtd error %d\n",
12918 + chunkInNAND, retval);
12920 + return retval ? YAFFS_FAIL : YAFFS_OK;
12923 +/* Return with empty ExtendedTags but add eccResult.
12925 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12928 + memset(etags, 0, sizeof(*etags));
12929 + etags->eccResult = eccResult;
12934 +/* Read a chunk (page) from NAND.
12936 + * Caller expects ExtendedTags data to be usable even on error; that is,
12937 + * all members except eccResult and blockBad are zeroed.
12939 + * - Check ECC results for data (if applicable)
12940 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12941 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12942 + * - Convert PackedTags1 to ExtendedTags
12943 + * - Update eccResult and blockBad members to refect state.
12945 + * Returns YAFFS_OK or YAFFS_FAIL.
12947 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12948 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12950 + struct mtd_info * mtd = dev->genericDevice;
12951 + int chunkBytes = dev->nDataBytesPerChunk;
12952 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12953 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12954 + struct mtd_oob_ops ops;
12955 + yaffs_PackedTags1 pt1;
12959 + dev->nPageReads++;
12961 + memset(&ops, 0, sizeof(ops));
12962 + ops.mode = MTD_OOB_AUTO;
12963 + ops.len = (data) ? chunkBytes : 0;
12964 + ops.ooblen = YTAG1_SIZE;
12965 + ops.datbuf = data;
12966 + ops.oobbuf = (__u8 *)&pt1;
12968 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12969 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12970 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12972 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12974 + /* Read page and oob using MTD.
12975 + * Check status and determine ECC result.
12977 + retval = mtd->read_oob(mtd, addr, &ops);
12979 + yaffs_trace(YAFFS_TRACE_MTD,
12980 + "read_oob failed, chunk %d, mtd error %d\n",
12981 + chunkInNAND, retval);
12984 + switch (retval) {
12990 + /* MTD's ECC fixed the data */
12991 + eccres = YAFFS_ECC_RESULT_FIXED;
12996 + /* MTD's ECC could not fix the data */
12997 + dev->eccUnfixed++;
12998 + /* fall into... */
13000 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
13001 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
13002 + return YAFFS_FAIL;
13005 + /* Check for a blank/erased chunk.
13007 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
13008 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
13009 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
13012 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
13013 + /* Read deleted status (bit) then return it to it's non-deleted
13014 + * state before performing tags mini-ECC check. pt1.deleted is
13017 + deleted = !pt1.deleted;
13020 + deleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
13023 + /* Check the packed tags mini-ECC and correct if necessary/possible.
13025 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
13026 + switch (retval) {
13028 + /* no tags error, use MTD result */
13031 + /* recovered tags-ECC error */
13032 + dev->tagsEccFixed++;
13033 + if (eccres == YAFFS_ECC_RESULT_NO_ERROR)
13034 + eccres = YAFFS_ECC_RESULT_FIXED;
13037 + /* unrecovered tags-ECC error */
13038 + dev->tagsEccUnfixed++;
13039 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
13042 + /* Unpack the tags to extended form and set ECC result.
13043 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
13045 + pt1.shouldBeFF = 0xFFFFFFFF;
13046 + yaffs_UnpackTags1(etags, &pt1);
13047 + etags->eccResult = eccres;
13049 + /* Set deleted state */
13050 + etags->chunkDeleted = deleted;
13054 +/* Mark a block bad.
13056 + * This is a persistant state.
13057 + * Use of this function should be rare.
13059 + * Returns YAFFS_OK or YAFFS_FAIL.
13061 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13063 + struct mtd_info * mtd = dev->genericDevice;
13064 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
13067 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
13069 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
13070 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
13073 +/* Check any MTD prerequists.
13075 + * Returns YAFFS_OK or YAFFS_FAIL.
13077 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
13079 + /* 2.6.18 has mtd->ecclayout->oobavail */
13080 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
13081 + int oobavail = mtd->ecclayout->oobavail;
13083 + if (oobavail < YTAG1_SIZE) {
13084 + yaffs_trace(YAFFS_TRACE_ERROR,
13085 + "mtd device has only %d bytes for tags, need %d\n",
13086 + oobavail, YTAG1_SIZE);
13087 + return YAFFS_FAIL;
13092 +/* Query for the current state of a specific block.
13094 + * Examine the tags of the first chunk of the block and return the state:
13095 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
13096 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
13097 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
13099 + * Always returns YAFFS_OK.
13101 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13102 + yaffs_BlockState * pState, int *pSequenceNumber)
13104 + struct mtd_info * mtd = dev->genericDevice;
13105 + int chunkNo = blockNo * dev->nChunksPerBlock;
13106 + yaffs_ExtendedTags etags;
13107 + int state = YAFFS_BLOCK_STATE_DEAD;
13111 + /* We don't yet have a good place to test for MTD config prerequists.
13112 + * Do it here as we are called during the initial scan.
13114 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
13115 + return YAFFS_FAIL;
13118 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
13119 + if (etags.blockBad) {
13120 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
13121 + "block %d is marked bad", blockNo);
13122 + state = YAFFS_BLOCK_STATE_DEAD;
13124 + else if (etags.chunkUsed) {
13125 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13126 + seqnum = etags.sequenceNumber;
13129 + state = YAFFS_BLOCK_STATE_EMPTY;
13133 + *pSequenceNumber = seqnum;
13135 + /* query always succeeds */
13139 +#endif /*KERNEL_VERSION*/
13140 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.h
13141 ===================================================================
13142 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13143 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif1.h 2007-09-21 16:24:08.000000000 +0800
13146 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
13148 + * Copyright (C) 2002-2007 Aleph One Ltd.
13149 + * for Toby Churchill Ltd and Brightstar Engineering
13151 + * This program is free software; you can redistribute it and/or modify
13152 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13153 + * published by the Free Software Foundation.
13155 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13158 +#ifndef __YAFFS_MTDIF1_H__
13159 +#define __YAFFS_MTDIF1_H__
13161 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13162 + const __u8 * data, const yaffs_ExtendedTags * tags);
13164 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13165 + __u8 * data, yaffs_ExtendedTags * tags);
13167 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13169 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13170 + yaffs_BlockState * state, int *sequenceNumber);
13173 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.c
13174 ===================================================================
13175 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13176 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.c 2007-09-21 16:24:08.000000000 +0800
13179 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13181 + * Copyright (C) 2002-2007 Aleph One Ltd.
13182 + * for Toby Churchill Ltd and Brightstar Engineering
13184 + * Created by Charles Manning <charles@aleph1.co.uk>
13186 + * This program is free software; you can redistribute it and/or modify
13187 + * it under the terms of the GNU General Public License version 2 as
13188 + * published by the Free Software Foundation.
13191 +/* mtd interface for YAFFS2 */
13193 +const char *yaffs_mtdif2_c_version =
13194 + "$Id: yaffs_mtdif2.c,v 1.17 2007-02-14 01:09:06 wookey Exp $";
13196 +#include "yportenv.h"
13199 +#include "yaffs_mtdif2.h"
13201 +#include "linux/mtd/mtd.h"
13202 +#include "linux/types.h"
13203 +#include "linux/time.h"
13205 +#include "yaffs_packedtags2.h"
13207 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13208 + const __u8 * data,
13209 + const yaffs_ExtendedTags * tags)
13211 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13212 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13213 + struct mtd_oob_ops ops;
13219 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13221 + yaffs_PackedTags2 pt;
13223 + T(YAFFS_TRACE_MTD,
13225 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
13226 + TENDSTR), chunkInNAND, data, tags));
13228 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13230 + yaffs_PackTags2(&pt, tags);
13232 + BUG(); /* both tags and data should always be present */
13235 + ops.mode = MTD_OOB_AUTO;
13236 + ops.ooblen = sizeof(pt);
13237 + ops.len = dev->nDataBytesPerChunk;
13239 + ops.datbuf = (__u8 *)data;
13240 + ops.oobbuf = (void *)&pt;
13241 + retval = mtd->write_oob(mtd, addr, &ops);
13243 + BUG(); /* both tags and data should always be present */
13246 + yaffs_PackTags2(&pt, tags);
13249 + if (data && tags) {
13250 + if (dev->useNANDECC)
13252 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13253 + &dummy, data, (__u8 *) & pt, NULL);
13256 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13257 + &dummy, data, (__u8 *) & pt, NULL);
13261 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13265 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
13274 + return YAFFS_FAIL;
13277 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13278 + __u8 * data, yaffs_ExtendedTags * tags)
13280 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13281 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13282 + struct mtd_oob_ops ops;
13287 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13289 + yaffs_PackedTags2 pt;
13291 + T(YAFFS_TRACE_MTD,
13293 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
13294 + TENDSTR), chunkInNAND, data, tags));
13296 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13297 + if (data && !tags)
13298 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13301 + ops.mode = MTD_OOB_AUTO;
13302 + ops.ooblen = sizeof(pt);
13303 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13305 + ops.datbuf = data;
13306 + ops.oobbuf = dev->spareBuffer;
13307 + retval = mtd->read_oob(mtd, addr, &ops);
13310 + if (data && tags) {
13311 + if (dev->useNANDECC) {
13313 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13314 + &dummy, data, dev->spareBuffer,
13318 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13319 + &dummy, data, dev->spareBuffer,
13325 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13329 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13330 + dev->spareBuffer);
13334 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13337 + yaffs_UnpackTags2(tags, &pt);
13339 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13340 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13345 + return YAFFS_FAIL;
13348 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13350 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13352 + T(YAFFS_TRACE_MTD,
13353 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13356 + mtd->block_markbad(mtd,
13357 + blockNo * dev->nChunksPerBlock *
13358 + dev->nDataBytesPerChunk);
13363 + return YAFFS_FAIL;
13367 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13368 + yaffs_BlockState * state, int *sequenceNumber)
13370 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13373 + T(YAFFS_TRACE_MTD,
13374 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13376 + mtd->block_isbad(mtd,
13377 + blockNo * dev->nChunksPerBlock *
13378 + dev->nDataBytesPerChunk);
13381 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13383 + *state = YAFFS_BLOCK_STATE_DEAD;
13384 + *sequenceNumber = 0;
13386 + yaffs_ExtendedTags t;
13387 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13389 + dev->nChunksPerBlock, NULL,
13392 + if (t.chunkUsed) {
13393 + *sequenceNumber = t.sequenceNumber;
13394 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13396 + *sequenceNumber = 0;
13397 + *state = YAFFS_BLOCK_STATE_EMPTY;
13400 + T(YAFFS_TRACE_MTD,
13401 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13407 + return YAFFS_FAIL;
13410 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.h
13411 ===================================================================
13412 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13413 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_mtdif2.h 2007-09-21 16:24:08.000000000 +0800
13416 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13418 + * Copyright (C) 2002-2007 Aleph One Ltd.
13419 + * for Toby Churchill Ltd and Brightstar Engineering
13421 + * Created by Charles Manning <charles@aleph1.co.uk>
13423 + * This program is free software; you can redistribute it and/or modify
13424 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13425 + * published by the Free Software Foundation.
13427 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13430 +#ifndef __YAFFS_MTDIF2_H__
13431 +#define __YAFFS_MTDIF2_H__
13433 +#include "yaffs_guts.h"
13434 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13435 + const __u8 * data,
13436 + const yaffs_ExtendedTags * tags);
13437 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13438 + __u8 * data, yaffs_ExtendedTags * tags);
13439 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13440 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13441 + yaffs_BlockState * state, int *sequenceNumber);
13444 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.c
13445 ===================================================================
13446 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13447 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.c 2007-09-21 16:24:08.000000000 +0800
13450 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13452 + * Copyright (C) 2002-2007 Aleph One Ltd.
13453 + * for Toby Churchill Ltd and Brightstar Engineering
13455 + * Created by Charles Manning <charles@aleph1.co.uk>
13457 + * This program is free software; you can redistribute it and/or modify
13458 + * it under the terms of the GNU General Public License version 2 as
13459 + * published by the Free Software Foundation.
13462 +const char *yaffs_nand_c_version =
13463 + "$Id: yaffs_nand.c,v 1.7 2007-02-14 01:09:06 wookey Exp $";
13465 +#include "yaffs_nand.h"
13466 +#include "yaffs_tagscompat.h"
13467 +#include "yaffs_tagsvalidity.h"
13470 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13472 + yaffs_ExtendedTags * tags)
13475 + yaffs_ExtendedTags localTags;
13477 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13479 + /* If there are no tags provided, use local tags to get prioritised gc working */
13481 + tags = &localTags;
13483 + if (dev->readChunkWithTagsFromNAND)
13484 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13487 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13488 + realignedChunkInNAND,
13492 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
13494 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13495 + yaffs_HandleChunkError(dev,bi);
13501 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13503 + const __u8 * buffer,
13504 + yaffs_ExtendedTags * tags)
13506 + chunkInNAND -= dev->chunkOffset;
13510 + tags->sequenceNumber = dev->sequenceNumber;
13511 + tags->chunkUsed = 1;
13512 + if (!yaffs_ValidateTags(tags)) {
13513 + T(YAFFS_TRACE_ERROR,
13514 + (TSTR("Writing uninitialised tags" TENDSTR)));
13517 + T(YAFFS_TRACE_WRITE,
13518 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13519 + tags->objectId, tags->chunkId));
13521 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13525 + if (dev->writeChunkWithTagsToNAND)
13526 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13529 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13535 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
13537 + blockNo -= dev->blockOffset;
13540 + if (dev->markNANDBlockBad)
13541 + return dev->markNANDBlockBad(dev, blockNo);
13543 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13546 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13548 + yaffs_BlockState * state,
13549 + unsigned *sequenceNumber)
13551 + blockNo -= dev->blockOffset;
13553 + if (dev->queryNANDBlock)
13554 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13556 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13562 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13567 + blockInNAND -= dev->blockOffset;
13570 + dev->nBlockErasures++;
13571 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13576 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13578 + return dev->initialiseNAND(dev);
13583 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.h
13584 ===================================================================
13585 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13586 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nand.h 2007-09-21 16:24:08.000000000 +0800
13589 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13591 + * Copyright (C) 2002-2007 Aleph One Ltd.
13592 + * for Toby Churchill Ltd and Brightstar Engineering
13594 + * Created by Charles Manning <charles@aleph1.co.uk>
13596 + * This program is free software; you can redistribute it and/or modify
13597 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13598 + * published by the Free Software Foundation.
13600 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13603 +#ifndef __YAFFS_NAND_H__
13604 +#define __YAFFS_NAND_H__
13605 +#include "yaffs_guts.h"
13609 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13611 + yaffs_ExtendedTags * tags);
13613 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13615 + const __u8 * buffer,
13616 + yaffs_ExtendedTags * tags);
13618 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
13620 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13622 + yaffs_BlockState * state,
13623 + unsigned *sequenceNumber);
13625 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13626 + int blockInNAND);
13628 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13632 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_nandemul2k.h
13633 ===================================================================
13634 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13635 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_nandemul2k.h 2007-09-21 16:24:08.000000000 +0800
13638 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13640 + * Copyright (C) 2002-2007 Aleph One Ltd.
13641 + * for Toby Churchill Ltd and Brightstar Engineering
13643 + * Created by Charles Manning <charles@aleph1.co.uk>
13645 + * This program is free software; you can redistribute it and/or modify
13646 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13647 + * published by the Free Software Foundation.
13649 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13652 +/* Interface to emulated NAND functions (2k page size) */
13654 +#ifndef __YAFFS_NANDEMUL2K_H__
13655 +#define __YAFFS_NANDEMUL2K_H__
13657 +#include "yaffs_guts.h"
13659 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13660 + int chunkInNAND, const __u8 * data,
13661 + yaffs_ExtendedTags * tags);
13662 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13663 + int chunkInNAND, __u8 * data,
13664 + yaffs_ExtendedTags * tags);
13665 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13666 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13667 + yaffs_BlockState * state, int *sequenceNumber);
13668 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13669 + int blockInNAND);
13670 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13671 +int nandemul2k_GetBytesPerChunk(void);
13672 +int nandemul2k_GetChunksPerBlock(void);
13673 +int nandemul2k_GetNumberOfBlocks(void);
13676 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.c
13677 ===================================================================
13678 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13679 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.c 2007-09-21 16:24:08.000000000 +0800
13682 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13684 + * Copyright (C) 2002-2007 Aleph One Ltd.
13685 + * for Toby Churchill Ltd and Brightstar Engineering
13687 + * Created by Charles Manning <charles@aleph1.co.uk>
13689 + * This program is free software; you can redistribute it and/or modify
13690 + * it under the terms of the GNU General Public License version 2 as
13691 + * published by the Free Software Foundation.
13694 +#include "yaffs_packedtags1.h"
13695 +#include "yportenv.h"
13697 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
13699 + pt->chunkId = t->chunkId;
13700 + pt->serialNumber = t->serialNumber;
13701 + pt->byteCount = t->byteCount;
13702 + pt->objectId = t->objectId;
13704 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13705 + pt->unusedStuff = 0;
13706 + pt->shouldBeFF = 0xFFFFFFFF;
13710 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
13712 + static const __u8 allFF[] =
13713 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13716 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13718 + if (pt->shouldBeFF != 0xFFFFFFFF) {
13721 + t->chunkUsed = 1;
13722 + t->objectId = pt->objectId;
13723 + t->chunkId = pt->chunkId;
13724 + t->byteCount = pt->byteCount;
13725 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13726 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13727 + t->serialNumber = pt->serialNumber;
13729 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13733 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.h
13734 ===================================================================
13735 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13736 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags1.h 2007-09-21 16:24:08.000000000 +0800
13739 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13741 + * Copyright (C) 2002-2007 Aleph One Ltd.
13742 + * for Toby Churchill Ltd and Brightstar Engineering
13744 + * Created by Charles Manning <charles@aleph1.co.uk>
13746 + * This program is free software; you can redistribute it and/or modify
13747 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13748 + * published by the Free Software Foundation.
13750 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13753 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13755 +#ifndef __YAFFS_PACKEDTAGS1_H__
13756 +#define __YAFFS_PACKEDTAGS1_H__
13758 +#include "yaffs_guts.h"
13761 + unsigned chunkId:20;
13762 + unsigned serialNumber:2;
13763 + unsigned byteCount:10;
13764 + unsigned objectId:18;
13766 + unsigned deleted:1;
13767 + unsigned unusedStuff:1;
13768 + unsigned shouldBeFF;
13770 +} yaffs_PackedTags1;
13772 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
13773 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
13775 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.c
13776 ===================================================================
13777 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13778 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.c 2007-09-21 16:24:08.000000000 +0800
13781 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13783 + * Copyright (C) 2002-2007 Aleph One Ltd.
13784 + * for Toby Churchill Ltd and Brightstar Engineering
13786 + * Created by Charles Manning <charles@aleph1.co.uk>
13788 + * This program is free software; you can redistribute it and/or modify
13789 + * it under the terms of the GNU General Public License version 2 as
13790 + * published by the Free Software Foundation.
13793 +#include "yaffs_packedtags2.h"
13794 +#include "yportenv.h"
13795 +#include "yaffs_tagsvalidity.h"
13797 +/* This code packs a set of extended tags into a binary structure for
13801 +/* Some of the information is "extra" struff which can be packed in to
13803 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13806 +/* Extra flags applied to chunkId */
13808 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13809 +#define EXTRA_SHRINK_FLAG 0x40000000
13810 +#define EXTRA_SHADOWS_FLAG 0x20000000
13811 +#define EXTRA_SPARE_FLAGS 0x10000000
13813 +#define ALL_EXTRA_FLAGS 0xF0000000
13815 +/* Also, the top 4 bits of the object Id are set to the object type. */
13816 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13817 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13819 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
13821 + T(YAFFS_TRACE_MTD,
13822 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13823 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
13824 + pt->t.sequenceNumber));
13827 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
13829 + T(YAFFS_TRACE_MTD,
13831 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
13832 + "%d del %d ser %d seq %d"
13833 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13834 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13835 + t->sequenceNumber));
13839 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
13841 + pt->t.chunkId = t->chunkId;
13842 + pt->t.sequenceNumber = t->sequenceNumber;
13843 + pt->t.byteCount = t->byteCount;
13844 + pt->t.objectId = t->objectId;
13846 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13847 + /* Store the extra header info instead */
13848 + /* We save the parent object in the chunkId */
13849 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
13850 + | t->extraParentObjectId;
13851 + if (t->extraIsShrinkHeader) {
13852 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
13854 + if (t->extraShadows) {
13855 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
13858 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13859 + pt->t.objectId |=
13860 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13862 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13863 + pt->t.byteCount = t->extraEquivalentObjectId;
13864 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
13865 + pt->t.byteCount = t->extraFileLength;
13867 + pt->t.byteCount = 0;
13871 + yaffs_DumpPackedTags2(pt);
13872 + yaffs_DumpTags2(t);
13874 +#ifndef YAFFS_IGNORE_TAGS_ECC
13876 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13877 + sizeof(yaffs_PackedTags2TagsPart),
13883 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
13886 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13888 + yaffs_InitialiseTags(t);
13890 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13891 + /* Page is in use */
13892 +#ifdef YAFFS_IGNORE_TAGS_ECC
13894 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13898 + yaffs_ECCOther ecc;
13900 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13902 + (yaffs_PackedTags2TagsPart),
13905 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13907 + (yaffs_PackedTags2TagsPart),
13911 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13914 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
13917 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13920 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13925 + t->chunkUsed = 1;
13926 + t->objectId = pt->t.objectId;
13927 + t->chunkId = pt->t.chunkId;
13928 + t->byteCount = pt->t.byteCount;
13929 + t->chunkDeleted = 0;
13930 + t->serialNumber = 0;
13931 + t->sequenceNumber = pt->t.sequenceNumber;
13933 + /* Do extra header info stuff */
13935 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
13937 + t->byteCount = 0;
13939 + t->extraHeaderInfoAvailable = 1;
13940 + t->extraParentObjectId =
13941 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
13942 + t->extraIsShrinkHeader =
13943 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13944 + t->extraShadows =
13945 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13946 + t->extraObjectType =
13947 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13948 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13950 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13951 + t->extraEquivalentObjectId = pt->t.byteCount;
13953 + t->extraFileLength = pt->t.byteCount;
13958 + yaffs_DumpPackedTags2(pt);
13959 + yaffs_DumpTags2(t);
13962 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.h
13963 ===================================================================
13964 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
13965 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_packedtags2.h 2007-09-21 16:24:08.000000000 +0800
13968 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13970 + * Copyright (C) 2002-2007 Aleph One Ltd.
13971 + * for Toby Churchill Ltd and Brightstar Engineering
13973 + * Created by Charles Manning <charles@aleph1.co.uk>
13975 + * This program is free software; you can redistribute it and/or modify
13976 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13977 + * published by the Free Software Foundation.
13979 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13982 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13984 +#ifndef __YAFFS_PACKEDTAGS2_H__
13985 +#define __YAFFS_PACKEDTAGS2_H__
13987 +#include "yaffs_guts.h"
13988 +#include "yaffs_ecc.h"
13991 + unsigned sequenceNumber;
13992 + unsigned objectId;
13993 + unsigned chunkId;
13994 + unsigned byteCount;
13995 +} yaffs_PackedTags2TagsPart;
13998 + yaffs_PackedTags2TagsPart t;
13999 + yaffs_ECCOther ecc;
14000 +} yaffs_PackedTags2;
14002 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
14003 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
14005 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.c
14006 ===================================================================
14007 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14008 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.c 2007-09-21 16:24:08.000000000 +0800
14011 + * Copyright (c) 1992, 1993
14012 + * The Regents of the University of California. All rights reserved.
14014 + * Redistribution and use in source and binary forms, with or without
14015 + * modification, are permitted provided that the following conditions
14017 + * 1. Redistributions of source code must retain the above copyright
14018 + * notice, this list of conditions and the following disclaimer.
14019 + * 2. Redistributions in binary form must reproduce the above copyright
14020 + * notice, this list of conditions and the following disclaimer in the
14021 + * documentation and/or other materials provided with the distribution.
14022 + * 3. Neither the name of the University nor the names of its contributors
14023 + * may be used to endorse or promote products derived from this software
14024 + * without specific prior written permission.
14026 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
14027 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
14028 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
14029 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
14030 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
14031 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
14032 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
14033 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
14034 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
14035 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
14039 +#include "yportenv.h"
14040 +//#include <linux/string.h>
14043 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
14045 +#define swapcode(TYPE, parmi, parmj, n) { \
14046 + long i = (n) / sizeof (TYPE); \
14047 + register TYPE *pi = (TYPE *) (parmi); \
14048 + register TYPE *pj = (TYPE *) (parmj); \
14050 + register TYPE t = *pi; \
14053 + } while (--i > 0); \
14056 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
14057 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
14059 +static __inline void
14060 +swapfunc(char *a, char *b, int n, int swaptype)
14062 + if (swaptype <= 1)
14063 + swapcode(long, a, b, n)
14065 + swapcode(char, a, b, n)
14068 +#define swap(a, b) \
14069 + if (swaptype == 0) { \
14070 + long t = *(long *)(a); \
14071 + *(long *)(a) = *(long *)(b); \
14072 + *(long *)(b) = t; \
14074 + swapfunc(a, b, es, swaptype)
14076 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14078 +static __inline char *
14079 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14081 + return cmp(a, b) < 0 ?
14082 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
14083 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
14087 +#define min(a,b) (((a) < (b)) ? (a) : (b))
14091 +yaffs_qsort(void *aa, size_t n, size_t es,
14092 + int (*cmp)(const void *, const void *))
14094 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14095 + int d, r, swaptype, swap_cnt;
14096 + register char *a = aa;
14098 +loop: SWAPINIT(a, es);
14101 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14102 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14104 + swap(pl, pl - es);
14107 + pm = (char *)a + (n / 2) * es;
14110 + pn = (char *)a + (n - 1) * es;
14112 + d = (n / 8) * es;
14113 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14114 + pm = med3(pm - d, pm, pm + d, cmp);
14115 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14117 + pm = med3(pl, pm, pn, cmp);
14120 + pa = pb = (char *)a + es;
14122 + pc = pd = (char *)a + (n - 1) * es;
14124 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14132 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14147 + if (swap_cnt == 0) { /* Switch to insertion sort */
14148 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14149 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14151 + swap(pl, pl - es);
14155 + pn = (char *)a + n * es;
14156 + r = min(pa - (char *)a, pb - pa);
14157 + vecswap(a, pb - r, r);
14158 + r = min((long)(pd - pc), (long)(pn - pd - es));
14159 + vecswap(pb, pn - r, r);
14160 + if ((r = pb - pa) > es)
14161 + yaffs_qsort(a, r / es, es, cmp);
14162 + if ((r = pd - pc) > es) {
14163 + /* Iterate rather than recurse to save stack space */
14168 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14170 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.h
14171 ===================================================================
14172 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14173 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_qsort.h 2007-09-21 16:24:08.000000000 +0800
14176 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14178 + * Copyright (C) 2002-2007 Aleph One Ltd.
14179 + * for Toby Churchill Ltd and Brightstar Engineering
14181 + * Created by Charles Manning <charles@aleph1.co.uk>
14183 + * This program is free software; you can redistribute it and/or modify
14184 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14185 + * published by the Free Software Foundation.
14187 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14191 +#ifndef __YAFFS_QSORT_H__
14192 +#define __YAFFS_QSORT_H__
14194 +extern void yaffs_qsort (void *const base, size_t total_elems, size_t size,
14195 + int (*cmp)(const void *, const void *));
14198 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.c
14199 ===================================================================
14200 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14201 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.c 2007-09-21 16:24:08.000000000 +0800
14204 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14206 + * Copyright (C) 2002-2007 Aleph One Ltd.
14207 + * for Toby Churchill Ltd and Brightstar Engineering
14209 + * Created by Charles Manning <charles@aleph1.co.uk>
14211 + * This program is free software; you can redistribute it and/or modify
14212 + * it under the terms of the GNU General Public License version 2 as
14213 + * published by the Free Software Foundation.
14216 +#include "yaffs_guts.h"
14217 +#include "yaffs_tagscompat.h"
14218 +#include "yaffs_ecc.h"
14220 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
14222 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
14223 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14224 + const __u8 * data,
14225 + const yaffs_Spare * spare);
14226 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14227 + const yaffs_Spare * spare);
14228 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
14231 +static const char yaffs_countBitsTable[256] = {
14232 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14233 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14234 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14235 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14236 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14237 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14238 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14239 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14240 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14241 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14242 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14243 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14244 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14245 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14246 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14247 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14250 +int yaffs_CountBits(__u8 x)
14253 + retVal = yaffs_countBitsTable[x];
14257 +/********** Tags ECC calculations *********/
14259 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
14261 + yaffs_ECCCalculate(data, spare->ecc1);
14262 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14265 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
14267 + /* Calculate an ecc */
14269 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14271 + unsigned ecc = 0;
14272 + unsigned bit = 0;
14276 + for (i = 0; i < 8; i++) {
14277 + for (j = 1; j & 0xff; j <<= 1) {
14289 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
14291 + unsigned ecc = tags->ecc;
14293 + yaffs_CalcTagsECC(tags);
14295 + ecc ^= tags->ecc;
14297 + if (ecc && ecc <= 64) {
14298 + /* TODO: Handle the failure better. Retire? */
14299 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14303 + b[ecc / 8] ^= (1 << (ecc & 7));
14305 + /* Now recvalc the ecc */
14306 + yaffs_CalcTagsECC(tags);
14308 + return 1; /* recovered error */
14309 + } else if (ecc) {
14310 + /* Wierd ecc failure value */
14311 + /* TODO Need to do somethiong here */
14312 + return -1; /* unrecovered error */
14318 +/********** Tags **********/
14320 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
14321 + yaffs_Tags * tagsPtr)
14323 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14325 + yaffs_CalcTagsECC(tagsPtr);
14327 + sparePtr->tagByte0 = tu->asBytes[0];
14328 + sparePtr->tagByte1 = tu->asBytes[1];
14329 + sparePtr->tagByte2 = tu->asBytes[2];
14330 + sparePtr->tagByte3 = tu->asBytes[3];
14331 + sparePtr->tagByte4 = tu->asBytes[4];
14332 + sparePtr->tagByte5 = tu->asBytes[5];
14333 + sparePtr->tagByte6 = tu->asBytes[6];
14334 + sparePtr->tagByte7 = tu->asBytes[7];
14337 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
14338 + yaffs_Tags * tagsPtr)
14340 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14343 + tu->asBytes[0] = sparePtr->tagByte0;
14344 + tu->asBytes[1] = sparePtr->tagByte1;
14345 + tu->asBytes[2] = sparePtr->tagByte2;
14346 + tu->asBytes[3] = sparePtr->tagByte3;
14347 + tu->asBytes[4] = sparePtr->tagByte4;
14348 + tu->asBytes[5] = sparePtr->tagByte5;
14349 + tu->asBytes[6] = sparePtr->tagByte6;
14350 + tu->asBytes[7] = sparePtr->tagByte7;
14352 + result = yaffs_CheckECCOnTags(tagsPtr);
14353 + if (result > 0) {
14354 + dev->tagsEccFixed++;
14355 + } else if (result < 0) {
14356 + dev->tagsEccUnfixed++;
14360 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
14362 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14365 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14366 + int chunkInNAND, const __u8 * data,
14367 + yaffs_Spare * spare)
14369 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14370 + T(YAFFS_TRACE_ERROR,
14371 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14373 + return YAFFS_FAIL;
14376 + dev->nPageWrites++;
14377 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14380 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14383 + yaffs_Spare * spare,
14384 + yaffs_ECCResult * eccResult,
14385 + int doErrorCorrection)
14388 + yaffs_Spare localSpare;
14390 + dev->nPageReads++;
14392 + if (!spare && data) {
14393 + /* If we don't have a real spare, then we use a local one. */
14394 + /* Need this for the calculation of the ecc */
14395 + spare = &localSpare;
14398 + if (!dev->useNANDECC) {
14399 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14400 + if (data && doErrorCorrection) {
14401 + /* Do ECC correction */
14402 + /* Todo handle any errors */
14403 + int eccResult1, eccResult2;
14406 + yaffs_ECCCalculate(data, calcEcc);
14408 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14409 + yaffs_ECCCalculate(&data[256], calcEcc);
14411 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14413 + if (eccResult1 > 0) {
14414 + T(YAFFS_TRACE_ERROR,
14416 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14417 + TENDSTR), chunkInNAND));
14419 + } else if (eccResult1 < 0) {
14420 + T(YAFFS_TRACE_ERROR,
14422 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14423 + TENDSTR), chunkInNAND));
14424 + dev->eccUnfixed++;
14427 + if (eccResult2 > 0) {
14428 + T(YAFFS_TRACE_ERROR,
14430 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14431 + TENDSTR), chunkInNAND));
14433 + } else if (eccResult2 < 0) {
14434 + T(YAFFS_TRACE_ERROR,
14436 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14437 + TENDSTR), chunkInNAND));
14438 + dev->eccUnfixed++;
14441 + if (eccResult1 || eccResult2) {
14442 + /* We had a data problem on this page */
14443 + yaffs_HandleReadDataError(dev, chunkInNAND);
14446 + if (eccResult1 < 0 || eccResult2 < 0)
14447 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14448 + else if (eccResult1 > 0 || eccResult2 > 0)
14449 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14451 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14454 + /* Must allocate enough memory for spare+2*sizeof(int) */
14455 + /* for ecc results from device. */
14456 + struct yaffs_NANDSpare nspare;
14458 + dev->readChunkFromNAND(dev, chunkInNAND, data,
14459 + (yaffs_Spare *) & nspare);
14460 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14461 + if (data && doErrorCorrection) {
14462 + if (nspare.eccres1 > 0) {
14463 + T(YAFFS_TRACE_ERROR,
14465 + ("**>>mtd ecc error fix performed on chunk %d:0"
14466 + TENDSTR), chunkInNAND));
14467 + } else if (nspare.eccres1 < 0) {
14468 + T(YAFFS_TRACE_ERROR,
14470 + ("**>>mtd ecc error unfixed on chunk %d:0"
14471 + TENDSTR), chunkInNAND));
14474 + if (nspare.eccres2 > 0) {
14475 + T(YAFFS_TRACE_ERROR,
14477 + ("**>>mtd ecc error fix performed on chunk %d:1"
14478 + TENDSTR), chunkInNAND));
14479 + } else if (nspare.eccres2 < 0) {
14480 + T(YAFFS_TRACE_ERROR,
14482 + ("**>>mtd ecc error unfixed on chunk %d:1"
14483 + TENDSTR), chunkInNAND));
14486 + if (nspare.eccres1 || nspare.eccres2) {
14487 + /* We had a data problem on this page */
14488 + yaffs_HandleReadDataError(dev, chunkInNAND);
14491 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14492 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14493 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14494 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14496 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14504 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14508 + static int init = 0;
14509 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14510 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14511 + /* Might as well always allocate the larger size for */
14512 + /* dev->useNANDECC == true; */
14513 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14515 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14518 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14522 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14523 + return YAFFS_FAIL;
14524 + if (memcmp(cmpbuf, spare, 16))
14525 + return YAFFS_FAIL;
14533 + * Functions for robustisizing
14536 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
14538 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14540 + /* Mark the block for retirement */
14541 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14542 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14543 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14546 + * Just do a garbage collection on the affected block
14547 + * then retire the block
14553 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
14557 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14558 + const __u8 * data,
14559 + const yaffs_Spare * spare)
14563 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14564 + const yaffs_Spare * spare)
14568 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
14570 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14572 + /* Mark the block for retirement */
14573 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14574 + /* Delete the chunk */
14575 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14578 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
14579 + const yaffs_Spare * s0, const yaffs_Spare * s1)
14582 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14583 + s0->tagByte0 != s1->tagByte0 ||
14584 + s0->tagByte1 != s1->tagByte1 ||
14585 + s0->tagByte2 != s1->tagByte2 ||
14586 + s0->tagByte3 != s1->tagByte3 ||
14587 + s0->tagByte4 != s1->tagByte4 ||
14588 + s0->tagByte5 != s1->tagByte5 ||
14589 + s0->tagByte6 != s1->tagByte6 ||
14590 + s0->tagByte7 != s1->tagByte7 ||
14591 + s0->ecc1[0] != s1->ecc1[0] ||
14592 + s0->ecc1[1] != s1->ecc1[1] ||
14593 + s0->ecc1[2] != s1->ecc1[2] ||
14594 + s0->ecc2[0] != s1->ecc2[0] ||
14595 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14601 +#endif /* NOTYET */
14603 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14605 + const __u8 * data,
14606 + const yaffs_ExtendedTags *
14609 + yaffs_Spare spare;
14612 + yaffs_SpareInitialise(&spare);
14614 + if (eTags->chunkDeleted) {
14615 + spare.pageStatus = 0;
14617 + tags.objectId = eTags->objectId;
14618 + tags.chunkId = eTags->chunkId;
14619 + tags.byteCount = eTags->byteCount;
14620 + tags.serialNumber = eTags->serialNumber;
14622 + if (!dev->useNANDECC && data) {
14623 + yaffs_CalcECC(data, &spare);
14625 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14629 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14632 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14635 + yaffs_ExtendedTags * eTags)
14638 + yaffs_Spare spare;
14640 + yaffs_ECCResult eccResult;
14642 + static yaffs_Spare spareFF;
14646 + memset(&spareFF, 0xFF, sizeof(spareFF));
14650 + if (yaffs_ReadChunkFromNAND
14651 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14652 + /* eTags may be NULL */
14656 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14658 + eTags->chunkDeleted = deleted;
14659 + eTags->eccResult = eccResult;
14660 + eTags->blockBad = 0; /* We're reading it */
14661 + /* therefore it is not a bad block */
14662 + eTags->chunkUsed =
14663 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14666 + if (eTags->chunkUsed) {
14667 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14669 + eTags->objectId = tags.objectId;
14670 + eTags->chunkId = tags.chunkId;
14671 + eTags->byteCount = tags.byteCount;
14672 + eTags->serialNumber = tags.serialNumber;
14678 + return YAFFS_FAIL;
14682 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14686 + yaffs_Spare spare;
14688 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14690 + spare.blockStatus = 'Y';
14692 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14694 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14701 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14702 + int blockNo, yaffs_BlockState *
14704 + int *sequenceNumber)
14707 + yaffs_Spare spare0, spare1;
14708 + static yaffs_Spare spareFF;
14710 + yaffs_ECCResult dummy;
14713 + memset(&spareFF, 0xFF, sizeof(spareFF));
14717 + *sequenceNumber = 0;
14719 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14720 + &spare0, &dummy, 1);
14721 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14722 + &spare1, &dummy, 1);
14724 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14725 + *state = YAFFS_BLOCK_STATE_DEAD;
14726 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14727 + *state = YAFFS_BLOCK_STATE_EMPTY;
14729 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14733 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.h
14734 ===================================================================
14735 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14736 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagscompat.h 2007-09-21 16:24:08.000000000 +0800
14739 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14741 + * Copyright (C) 2002-2007 Aleph One Ltd.
14742 + * for Toby Churchill Ltd and Brightstar Engineering
14744 + * Created by Charles Manning <charles@aleph1.co.uk>
14746 + * This program is free software; you can redistribute it and/or modify
14747 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14748 + * published by the Free Software Foundation.
14750 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14753 +#ifndef __YAFFS_TAGSCOMPAT_H__
14754 +#define __YAFFS_TAGSCOMPAT_H__
14756 +#include "yaffs_guts.h"
14757 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14759 + const __u8 * data,
14760 + const yaffs_ExtendedTags *
14762 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14765 + yaffs_ExtendedTags *
14767 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14769 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14770 + int blockNo, yaffs_BlockState *
14771 + state, int *sequenceNumber);
14773 +void yaffs_CalcTagsECC(yaffs_Tags * tags);
14774 +int yaffs_CheckECCOnTags(yaffs_Tags * tags);
14775 +int yaffs_CountBits(__u8 byte);
14778 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.c
14779 ===================================================================
14780 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14781 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.c 2007-09-21 16:24:08.000000000 +0800
14784 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14786 + * Copyright (C) 2002-2007 Aleph One Ltd.
14787 + * for Toby Churchill Ltd and Brightstar Engineering
14789 + * Created by Charles Manning <charles@aleph1.co.uk>
14791 + * This program is free software; you can redistribute it and/or modify
14792 + * it under the terms of the GNU General Public License version 2 as
14793 + * published by the Free Software Foundation.
14796 +#include "yaffs_tagsvalidity.h"
14798 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
14800 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14801 + tags->validMarker0 = 0xAAAAAAAA;
14802 + tags->validMarker1 = 0x55555555;
14805 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
14807 + return (tags->validMarker0 == 0xAAAAAAAA &&
14808 + tags->validMarker1 == 0x55555555);
14811 Index: linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.h
14812 ===================================================================
14813 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14814 +++ linux-2.6.23-rc6/fs/yaffs2/yaffs_tagsvalidity.h 2007-09-21 16:24:08.000000000 +0800
14817 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14819 + * Copyright (C) 2002-2007 Aleph One Ltd.
14820 + * for Toby Churchill Ltd and Brightstar Engineering
14822 + * Created by Charles Manning <charles@aleph1.co.uk>
14824 + * This program is free software; you can redistribute it and/or modify
14825 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14826 + * published by the Free Software Foundation.
14828 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14832 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14833 +#define __YAFFS_TAGS_VALIDITY_H__
14835 +#include "yaffs_guts.h"
14837 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
14838 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
14840 Index: linux-2.6.23-rc6/fs/yaffs2/yaffsinterface.h
14841 ===================================================================
14842 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14843 +++ linux-2.6.23-rc6/fs/yaffs2/yaffsinterface.h 2007-09-21 16:24:08.000000000 +0800
14846 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14848 + * Copyright (C) 2002-2007 Aleph One Ltd.
14849 + * for Toby Churchill Ltd and Brightstar Engineering
14851 + * Created by Charles Manning <charles@aleph1.co.uk>
14853 + * This program is free software; you can redistribute it and/or modify
14854 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14855 + * published by the Free Software Foundation.
14857 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14860 +#ifndef __YAFFSINTERFACE_H__
14861 +#define __YAFFSINTERFACE_H__
14863 +int yaffs_Initialise(unsigned nBlocks);
14866 Index: linux-2.6.23-rc6/fs/yaffs2/yportenv.h
14867 ===================================================================
14868 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
14869 +++ linux-2.6.23-rc6/fs/yaffs2/yportenv.h 2007-09-21 16:24:08.000000000 +0800
14872 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14874 + * Copyright (C) 2002-2007 Aleph One Ltd.
14875 + * for Toby Churchill Ltd and Brightstar Engineering
14877 + * Created by Charles Manning <charles@aleph1.co.uk>
14879 + * This program is free software; you can redistribute it and/or modify
14880 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14881 + * published by the Free Software Foundation.
14883 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14887 +#ifndef __YPORTENV_H__
14888 +#define __YPORTENV_H__
14890 +#if defined CONFIG_YAFFS_WINCE
14892 +#include "ywinceenv.h"
14894 +#elif defined __KERNEL__
14896 +#include "moduleconfig.h"
14898 +/* Linux kernel */
14899 +#include <linux/version.h>
14900 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
14901 +#include <linux/config.h>
14903 +#include <linux/kernel.h>
14904 +#include <linux/mm.h>
14905 +#include <linux/string.h>
14906 +#include <linux/slab.h>
14907 +#include <linux/vmalloc.h>
14909 +#define YCHAR char
14910 +#define YUCHAR unsigned char
14912 +#define yaffs_strcpy(a,b) strcpy(a,b)
14913 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14914 +#define yaffs_strncmp(a,b,c) strncmp(a,b,c)
14915 +#define yaffs_strlen(s) strlen(s)
14916 +#define yaffs_sprintf sprintf
14917 +#define yaffs_toupper(a) toupper(a)
14919 +#define Y_INLINE inline
14921 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14922 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14924 +/* #define YPRINTF(x) printk x */
14925 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
14926 +#define YFREE(x) kfree(x)
14927 +#define YMALLOC_ALT(x) vmalloc(x)
14928 +#define YFREE_ALT(x) vfree(x)
14929 +#define YMALLOC_DMA(x) YMALLOC(x)
14931 +// KR - added for use in scan so processes aren't blocked indefinitely.
14932 +#define YYIELD() schedule()
14934 +#define YAFFS_ROOT_MODE 0666
14935 +#define YAFFS_LOSTNFOUND_MODE 0666
14937 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
14938 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14939 +#define Y_TIME_CONVERT(x) (x).tv_sec
14941 +#define Y_CURRENT_TIME CURRENT_TIME
14942 +#define Y_TIME_CONVERT(x) (x)
14945 +#define yaffs_SumCompare(x,y) ((x) == (y))
14946 +#define yaffs_strcmp(a,b) strcmp(a,b)
14948 +#define TENDSTR "\n"
14949 +#define TSTR(x) KERN_WARNING x
14950 +#define TOUT(p) printk p
14952 +#define yaffs_trace(mask, fmt, args...) \
14953 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14954 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14957 +#define compile_time_assertion(assertion) \
14958 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14960 +#elif defined CONFIG_YAFFS_DIRECT
14962 +/* Direct interface */
14963 +#include "ydirectenv.h"
14965 +#elif defined CONFIG_YAFFS_UTIL
14967 +/* Stuff for YAFFS utilities */
14969 +#include "stdlib.h"
14970 +#include "stdio.h"
14971 +#include "string.h"
14973 +#include "devextras.h"
14975 +#define YMALLOC(x) malloc(x)
14976 +#define YFREE(x) free(x)
14977 +#define YMALLOC_ALT(x) malloc(x)
14978 +#define YFREE_ALT(x) free(x)
14980 +#define YCHAR char
14981 +#define YUCHAR unsigned char
14983 +#define yaffs_strcpy(a,b) strcpy(a,b)
14984 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14985 +#define yaffs_strlen(s) strlen(s)
14986 +#define yaffs_sprintf sprintf
14987 +#define yaffs_toupper(a) toupper(a)
14989 +#define Y_INLINE inline
14991 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
14992 +/* #define YALERT(s) YINFO(s) */
14994 +#define TENDSTR "\n"
14996 +#define TOUT(p) printf p
14998 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14999 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
15000 +/* #define YPRINTF(x) printf x */
15002 +#define YAFFS_ROOT_MODE 0666
15003 +#define YAFFS_LOSTNFOUND_MODE 0666
15005 +#define yaffs_SumCompare(x,y) ((x) == (y))
15006 +#define yaffs_strcmp(a,b) strcmp(a,b)
15009 +/* Should have specified a configuration type */
15010 +#error Unknown configuration
15014 +/* see yaffs_fs.c */
15015 +extern unsigned int yaffs_traceMask;
15016 +extern unsigned int yaffs_wr_attempts;
15020 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
15023 +#define YAFFS_TRACE_OS 0x00000002
15024 +#define YAFFS_TRACE_ALLOCATE 0x00000004
15025 +#define YAFFS_TRACE_SCAN 0x00000008
15026 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
15027 +#define YAFFS_TRACE_ERASE 0x00000020
15028 +#define YAFFS_TRACE_GC 0x00000040
15029 +#define YAFFS_TRACE_WRITE 0x00000080
15030 +#define YAFFS_TRACE_TRACING 0x00000100
15031 +#define YAFFS_TRACE_DELETION 0x00000200
15032 +#define YAFFS_TRACE_BUFFERS 0x00000400
15033 +#define YAFFS_TRACE_NANDACCESS 0x00000800
15034 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
15035 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
15036 +#define YAFFS_TRACE_MTD 0x00004000
15037 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
15039 +#define YAFFS_TRACE_VERIFY 0x00010000
15040 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
15041 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
15042 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
15045 +#define YAFFS_TRACE_ERROR 0x40000000
15046 +#define YAFFS_TRACE_BUG 0x80000000
15047 +#define YAFFS_TRACE_ALWAYS 0xF0000000
15050 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p);} while(0)
15052 +#ifndef CONFIG_YAFFS_WINCE
15053 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))