1 diff -urN linux-2.6.21.1/fs/yaffs2/Kconfig linux-2.6.21.1.new/fs/yaffs2/Kconfig
2 --- linux-2.6.21.1/fs/yaffs2/Kconfig 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.21.1.new/fs/yaffs2/Kconfig 2007-05-30 13:17:16.000000000 +0200
6 +# YAFFS file system configurations
10 + tristate "YAFFS2 file system support"
16 + YAFFS2, or Yet Another Flash Filing System, is a filing system
17 + optimised for NAND Flash chips.
19 + To compile the YAFFS2 file system support as a module, choose M
20 + here: the module will be called yaffs2.
24 + Further information on YAFFS2 is available at
25 + <http://www.aleph1.co.uk/yaffs/>.
28 + bool "512 byte / page devices"
32 + Enable YAFFS1 support -- yaffs for 512 byte / page devices
34 + Not needed for 2K-page devices.
38 +config YAFFS_9BYTE_TAGS
39 + bool "Use older-style on-NAND data format with pageStatus byte"
40 + depends on YAFFS_YAFFS1
44 + Older-style on-NAND data format has a "pageStatus" byte to record
45 + chunk/page state. This byte is zero when the page is discarded.
46 + Choose this option if you have existing on-NAND data using this
47 + format that you need to continue to support. New data written
48 + also uses the older-style format. Note: Use of this option
49 + generally requires that MTD's oob layout be adjusted to use the
50 + older-style format. See notes on tags formats and MTD versions.
54 +config YAFFS_DOES_ECC
55 + bool "Lets Yaffs do its own ECC"
56 + depends on YAFFS_FS && YAFFS_YAFFS1 && !YAFFS_9BYTE_TAGS
59 + This enables Yaffs to use its own ECC functions instead of using
60 + the ones from the generic MTD-NAND driver.
64 +config YAFFS_ECC_WRONG_ORDER
65 + bool "Use the same ecc byte order as Steven Hill's nand_ecc.c"
66 + depends on YAFFS_FS && YAFFS_DOES_ECC && !YAFFS_9BYTE_TAGS
69 + This makes yaffs_ecc.c use the same ecc byte order as Steven
70 + Hill's nand_ecc.c. If not set, then you get the same ecc byte
71 + order as SmartMedia.
76 + bool "2048 byte (or larger) / page devices"
80 + Enable YAFFS2 support -- yaffs for >= 2K bytes per page devices
84 +config YAFFS_AUTO_YAFFS2
85 + bool "Autoselect yaffs2 format"
86 + depends on YAFFS_YAFFS2
89 + Without this, you need to explicitely use yaffs2 as the file
90 + system type. With this, you can say "yaffs" and yaffs or yaffs2
91 + will be used depending on the device page size (yaffs on
92 + 512-byte page devices, yaffs2 on 2K page devices).
96 +config YAFFS_DISABLE_LAZY_LOAD
97 + bool "Disable lazy loading"
98 + depends on YAFFS_YAFFS2
101 + "Lazy loading" defers loading file details until they are
102 + required. This saves mount time, but makes the first look-up
105 + Lazy loading will only happen if enabled by this option being 'n'
106 + and if the appropriate tags are available, else yaffs2 will
107 + automatically fall back to immediate loading and do the right
110 + Lazy laoding will be required by checkpointing.
112 + Setting this to 'y' will disable lazy loading.
116 +config YAFFS_CHECKPOINT_RESERVED_BLOCKS
117 + int "Reserved blocks for checkpointing"
118 + depends on YAFFS_YAFFS2
121 + Give the number of Blocks to reserve for checkpointing.
122 + Checkpointing saves the state at unmount so that mounting is
123 + much faster as a scan of all the flash to regenerate this state
124 + is not needed. These Blocks are reserved per partition, so if
125 + you have very small partitions the default (10) may be a mess
126 + for you. You can set this value to 0, but that does not mean
127 + checkpointing is disabled at all. There only won't be any
128 + specially reserved blocks for checkpointing, so if there is
129 + enough free space on the filesystem, it will be used for
132 + If unsure, leave at default (10), but don't wonder if there are
133 + always 2MB used on your large page device partition (10 x 2k
134 + pagesize). When using small partitions or when being very small
135 + on space, you probably want to set this to zero.
137 +config YAFFS_DISABLE_WIDE_TNODES
138 + bool "Turn off wide tnodes"
139 + depends on YAFFS_FS
142 + Wide tnodes are only used for NAND arrays >=32MB for 512-byte
143 + page devices and >=128MB for 2k page devices. They use slightly
144 + more RAM but are faster since they eliminate chunk group
147 + Setting this to 'y' will force tnode width to 16 bits and save
148 + memory but make large arrays slower.
152 +config YAFFS_ALWAYS_CHECK_CHUNK_ERASED
153 + bool "Force chunk erase check"
154 + depends on YAFFS_FS
157 + Normally YAFFS only checks chunks before writing until an erased
158 + chunk is found. This helps to detect any partially written
159 + chunks that might have happened due to power loss.
161 + Enabling this forces on the test that chunks are erased in flash
162 + before writing to them. This takes more time but is potentially
165 + Suggest setting Y during development and ironing out driver
166 + issues etc. Suggest setting to N if you want faster writing.
170 +config YAFFS_SHORT_NAMES_IN_RAM
171 + bool "Cache short names in RAM"
172 + depends on YAFFS_FS
175 + If this config is set, then short names are stored with the
176 + yaffs_Object. This costs an extra 16 bytes of RAM per object,
177 + but makes look-ups faster.
180 diff -urN linux-2.6.21.1/fs/yaffs2/Makefile linux-2.6.21.1.new/fs/yaffs2/Makefile
181 --- linux-2.6.21.1/fs/yaffs2/Makefile 1970-01-01 01:00:00.000000000 +0100
182 +++ linux-2.6.21.1.new/fs/yaffs2/Makefile 2007-05-30 13:17:16.000000000 +0200
185 +# Makefile for the linux YAFFS filesystem routines.
188 +obj-$(CONFIG_YAFFS_FS) += yaffs.o
190 +yaffs-y := yaffs_ecc.o yaffs_fs.o yaffs_guts.o yaffs_checkptrw.o
191 +yaffs-y += yaffs_packedtags2.o yaffs_nand.o yaffs_qsort.o
192 +yaffs-y += yaffs_tagscompat.o yaffs_tagsvalidity.o
193 +yaffs-y += yaffs_mtdif1.o yaffs_packedtags1.o
194 +yaffs-y += yaffs_mtdif.o yaffs_mtdif2.o
195 diff -urN linux-2.6.21.1/fs/yaffs2/devextras.h linux-2.6.21.1.new/fs/yaffs2/devextras.h
196 --- linux-2.6.21.1/fs/yaffs2/devextras.h 1970-01-01 01:00:00.000000000 +0100
197 +++ linux-2.6.21.1.new/fs/yaffs2/devextras.h 2007-05-30 13:17:16.000000000 +0200
200 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
202 + * Copyright (C) 2002-2007 Aleph One Ltd.
203 + * for Toby Churchill Ltd and Brightstar Engineering
205 + * Created by Charles Manning <charles@aleph1.co.uk>
207 + * This program is free software; you can redistribute it and/or modify
208 + * it under the terms of the GNU Lesser General Public License version 2.1 as
209 + * published by the Free Software Foundation.
211 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
215 + * This file is just holds extra declarations used during development.
216 + * Most of these are from kernel includes placed here so we can use them in
221 +#ifndef __EXTRAS_H__
222 +#define __EXTRAS_H__
225 +#define __inline__ __inline
229 +#if !(defined __KERNEL__) || (defined WIN32)
231 +/* User space defines */
233 +typedef unsigned char __u8;
234 +typedef unsigned short __u16;
235 +typedef unsigned __u32;
238 + * Simple doubly linked list implementation.
240 + * Some of the internal functions ("__xxx") are useful when
241 + * manipulating whole lists rather than single entries, as
242 + * sometimes we already know the next/prev entries and we can
243 + * generate better code by using them directly rather than
244 + * using the generic single-entry routines.
247 +#define prefetch(x) 1
250 + struct list_head *next, *prev;
253 +#define LIST_HEAD_INIT(name) { &(name), &(name) }
255 +#define LIST_HEAD(name) \
256 + struct list_head name = LIST_HEAD_INIT(name)
258 +#define INIT_LIST_HEAD(ptr) do { \
259 + (ptr)->next = (ptr); (ptr)->prev = (ptr); \
263 + * Insert a new entry between two known consecutive entries.
265 + * This is only for internal list manipulation where we know
266 + * the prev/next entries already!
268 +static __inline__ void __list_add(struct list_head *new,
269 + struct list_head *prev,
270 + struct list_head *next)
279 + * list_add - add a new entry
280 + * @new: new entry to be added
281 + * @head: list head to add it after
283 + * Insert a new entry after the specified head.
284 + * This is good for implementing stacks.
286 +static __inline__ void list_add(struct list_head *new, struct list_head *head)
288 + __list_add(new, head, head->next);
292 + * list_add_tail - add a new entry
293 + * @new: new entry to be added
294 + * @head: list head to add it before
296 + * Insert a new entry before the specified head.
297 + * This is useful for implementing queues.
299 +static __inline__ void list_add_tail(struct list_head *new,
300 + struct list_head *head)
302 + __list_add(new, head->prev, head);
306 + * Delete a list entry by making the prev/next entries
307 + * point to each other.
309 + * This is only for internal list manipulation where we know
310 + * the prev/next entries already!
312 +static __inline__ void __list_del(struct list_head *prev,
313 + struct list_head *next)
320 + * list_del - deletes entry from list.
321 + * @entry: the element to delete from the list.
322 + * Note: list_empty on entry does not return true after this, the entry is
323 + * in an undefined state.
325 +static __inline__ void list_del(struct list_head *entry)
327 + __list_del(entry->prev, entry->next);
331 + * list_del_init - deletes entry from list and reinitialize it.
332 + * @entry: the element to delete from the list.
334 +static __inline__ void list_del_init(struct list_head *entry)
336 + __list_del(entry->prev, entry->next);
337 + INIT_LIST_HEAD(entry);
341 + * list_empty - tests whether a list is empty
342 + * @head: the list to test.
344 +static __inline__ int list_empty(struct list_head *head)
346 + return head->next == head;
350 + * list_splice - join two lists
351 + * @list: the new list to add.
352 + * @head: the place to add it in the first list.
354 +static __inline__ void list_splice(struct list_head *list,
355 + struct list_head *head)
357 + struct list_head *first = list->next;
359 + if (first != list) {
360 + struct list_head *last = list->prev;
361 + struct list_head *at = head->next;
363 + first->prev = head;
364 + head->next = first;
372 + * list_entry - get the struct for this entry
373 + * @ptr: the &struct list_head pointer.
374 + * @type: the type of the struct this is embedded in.
375 + * @member: the name of the list_struct within the struct.
377 +#define list_entry(ptr, type, member) \
378 + ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
381 + * list_for_each - iterate over a list
382 + * @pos: the &struct list_head to use as a loop counter.
383 + * @head: the head for your list.
385 +#define list_for_each(pos, head) \
386 + for (pos = (head)->next, prefetch(pos->next); pos != (head); \
387 + pos = pos->next, prefetch(pos->next))
390 + * list_for_each_safe - iterate over a list safe against removal
392 + * @pos: the &struct list_head to use as a loop counter.
393 + * @n: another &struct list_head to use as temporary storage
394 + * @head: the head for your list.
396 +#define list_for_each_safe(pos, n, head) \
397 + for (pos = (head)->next, n = pos->next; pos != (head); \
398 + pos = n, n = pos->next)
403 +#define DT_UNKNOWN 0
414 +#include <sys/stat.h>
418 + * Attribute flags. These should be or-ed together to figure out what
419 + * has been changed!
425 +#define ATTR_ATIME 16
426 +#define ATTR_MTIME 32
427 +#define ATTR_CTIME 64
428 +#define ATTR_ATIME_SET 128
429 +#define ATTR_MTIME_SET 256
430 +#define ATTR_FORCE 512 /* Not a change, but a change it */
431 +#define ATTR_ATTR_FLAG 1024
434 + unsigned int ia_valid;
442 + unsigned int ia_attr_flags;
450 +#include <linux/types.h>
451 +#include <linux/list.h>
452 +#include <linux/fs.h>
453 +#include <linux/stat.h>
463 diff -urN linux-2.6.21.1/fs/yaffs2/moduleconfig.h linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h
464 --- linux-2.6.21.1/fs/yaffs2/moduleconfig.h 1970-01-01 01:00:00.000000000 +0100
465 +++ linux-2.6.21.1.new/fs/yaffs2/moduleconfig.h 2007-05-30 13:17:16.000000000 +0200
468 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
470 + * Copyright (C) 2002-2007 Aleph One Ltd.
471 + * for Toby Churchill Ltd and Brightstar Engineering
473 + * Created by Martin Fouts <Martin.Fouts@palmsource.com>
475 + * This program is free software; you can redistribute it and/or modify
476 + * it under the terms of the GNU Lesser General Public License version 2.1 as
477 + * published by the Free Software Foundation.
479 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
482 +#ifndef __YAFFS_CONFIG_H__
483 +#define __YAFFS_CONFIG_H__
485 +#ifdef YAFFS_OUT_OF_TREE
487 +/* DO NOT UNSET THESE THREE. YAFFS2 will not compile if you do. */
488 +#define CONFIG_YAFFS_FS
489 +#define CONFIG_YAFFS_YAFFS1
490 +#define CONFIG_YAFFS_YAFFS2
492 +/* These options are independent of each other. Select those that matter. */
494 +/* Default: Not selected */
495 +/* Meaning: Yaffs does its own ECC, rather than using MTD ECC */
496 +//#define CONFIG_YAFFS_DOES_ECC
498 +/* Default: Not selected */
499 +/* Meaning: ECC byte order is 'wrong'. Only meaningful if */
500 +/* CONFIG_YAFFS_DOES_ECC is set */
501 +//#define CONFIG_YAFFS_ECC_WRONG_ORDER
503 +/* Default: Selected */
504 +/* Meaning: Disables testing whether chunks are erased before writing to them*/
505 +#define CONFIG_YAFFS_DISABLE_CHUNK_ERASED_CHECK
507 +/* Default: Selected */
508 +/* Meaning: Cache short names, taking more RAM, but faster look-ups */
509 +#define CONFIG_YAFFS_SHORT_NAMES_IN_RAM
512 +/* Meaning: set the count of blocks to reserve for checkpointing */
513 +#define CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS 10
516 +Older-style on-NAND data format has a "pageStatus" byte to record
517 +chunk/page state. This byte is zeroed when the page is discarded.
518 +Choose this option if you have existing on-NAND data in this format
519 +that you need to continue to support. New data written also uses the
521 +Note: Use of this option generally requires that MTD's oob layout be
522 +adjusted to use the older-style format. See notes on tags formats and
525 +/* Default: Not selected */
526 +/* Meaning: Use older-style on-NAND data format with pageStatus byte */
527 +#define CONFIG_YAFFS_9BYTE_TAGS
529 +#endif /* YAFFS_OUT_OF_TREE */
531 +#endif /* __YAFFS_CONFIG_H__ */
532 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c
533 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.c 1970-01-01 01:00:00.000000000 +0100
534 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.c 2007-05-30 13:17:16.000000000 +0200
537 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
539 + * Copyright (C) 2002-2007 Aleph One Ltd.
540 + * for Toby Churchill Ltd and Brightstar Engineering
542 + * Created by Charles Manning <charles@aleph1.co.uk>
544 + * This program is free software; you can redistribute it and/or modify
545 + * it under the terms of the GNU General Public License version 2 as
546 + * published by the Free Software Foundation.
549 +const char *yaffs_checkptrw_c_version =
550 + "$Id: yaffs_checkptrw.c,v 1.14 2007-05-15 20:07:40 charles Exp $";
553 +#include "yaffs_checkptrw.h"
556 +static int yaffs_CheckpointSpaceOk(yaffs_Device *dev)
559 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
561 + T(YAFFS_TRACE_CHECKPOINT,
562 + (TSTR("checkpt blocks available = %d" TENDSTR),
566 + return (blocksAvailable <= 0) ? 0 : 1;
570 +static int yaffs_CheckpointErase(yaffs_Device *dev)
576 + if(!dev->eraseBlockInNAND)
578 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checking blocks %d to %d"TENDSTR),
579 + dev->internalStartBlock,dev->internalEndBlock));
581 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
582 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
583 + if(bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT){
584 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("erasing checkpt block %d"TENDSTR),i));
585 + if(dev->eraseBlockInNAND(dev,i- dev->blockOffset /* realign */)){
586 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
587 + dev->nErasedBlocks++;
588 + dev->nFreeChunks += dev->nChunksPerBlock;
591 + dev->markNANDBlockBad(dev,i);
592 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
597 + dev->blocksInCheckpoint = 0;
603 +static void yaffs_CheckpointFindNextErasedBlock(yaffs_Device *dev)
606 + int blocksAvailable = dev->nErasedBlocks - dev->nReservedBlocks;
607 + T(YAFFS_TRACE_CHECKPOINT,
608 + (TSTR("allocating checkpt block: erased %d reserved %d avail %d next %d "TENDSTR),
609 + dev->nErasedBlocks,dev->nReservedBlocks,blocksAvailable,dev->checkpointNextBlock));
611 + if(dev->checkpointNextBlock >= 0 &&
612 + dev->checkpointNextBlock <= dev->internalEndBlock &&
613 + blocksAvailable > 0){
615 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
616 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
617 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY){
618 + dev->checkpointNextBlock = i + 1;
619 + dev->checkpointCurrentBlock = i;
620 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("allocating checkpt block %d"TENDSTR),i));
625 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("out of checkpt blocks"TENDSTR)));
627 + dev->checkpointNextBlock = -1;
628 + dev->checkpointCurrentBlock = -1;
631 +static void yaffs_CheckpointFindNextCheckpointBlock(yaffs_Device *dev)
634 + yaffs_ExtendedTags tags;
636 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: start: blocks %d next %d" TENDSTR),
637 + dev->blocksInCheckpoint, dev->checkpointNextBlock));
639 + if(dev->blocksInCheckpoint < dev->checkpointMaxBlocks)
640 + for(i = dev->checkpointNextBlock; i <= dev->internalEndBlock; i++){
641 + int chunk = i * dev->nChunksPerBlock;
642 + int realignedChunk = chunk - dev->chunkOffset;
644 + dev->readChunkWithTagsFromNAND(dev,realignedChunk,NULL,&tags);
645 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("find next checkpt block: search: block %d oid %d seq %d eccr %d" TENDSTR),
646 + i, tags.objectId,tags.sequenceNumber,tags.eccResult));
648 + if(tags.sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA){
649 + /* Right kind of block */
650 + dev->checkpointNextBlock = tags.objectId;
651 + dev->checkpointCurrentBlock = i;
652 + dev->checkpointBlockList[dev->blocksInCheckpoint] = i;
653 + dev->blocksInCheckpoint++;
654 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found checkpt block %d"TENDSTR),i));
659 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("found no more checkpt blocks"TENDSTR)));
661 + dev->checkpointNextBlock = -1;
662 + dev->checkpointCurrentBlock = -1;
666 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting)
669 + /* Got the functions we need? */
670 + if (!dev->writeChunkWithTagsToNAND ||
671 + !dev->readChunkWithTagsFromNAND ||
672 + !dev->eraseBlockInNAND ||
673 + !dev->markNANDBlockBad)
676 + if(forWriting && !yaffs_CheckpointSpaceOk(dev))
679 + if(!dev->checkpointBuffer)
680 + dev->checkpointBuffer = YMALLOC_DMA(dev->nDataBytesPerChunk);
681 + if(!dev->checkpointBuffer)
685 + dev->checkpointPageSequence = 0;
687 + dev->checkpointOpenForWrite = forWriting;
689 + dev->checkpointByteCount = 0;
690 + dev->checkpointSum = 0;
691 + dev->checkpointXor = 0;
692 + dev->checkpointCurrentBlock = -1;
693 + dev->checkpointCurrentChunk = -1;
694 + dev->checkpointNextBlock = dev->internalStartBlock;
696 + /* Erase all the blocks in the checkpoint area */
698 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
699 + dev->checkpointByteOffset = 0;
700 + return yaffs_CheckpointErase(dev);
705 + /* Set to a value that will kick off a read */
706 + dev->checkpointByteOffset = dev->nDataBytesPerChunk;
707 + /* A checkpoint block list of 1 checkpoint block per 16 block is (hopefully)
708 + * going to be way more than we need */
709 + dev->blocksInCheckpoint = 0;
710 + dev->checkpointMaxBlocks = (dev->internalEndBlock - dev->internalStartBlock)/16 + 2;
711 + dev->checkpointBlockList = YMALLOC(sizeof(int) * dev->checkpointMaxBlocks);
712 + for(i = 0; i < dev->checkpointMaxBlocks; i++)
713 + dev->checkpointBlockList[i] = -1;
719 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum)
721 + __u32 compositeSum;
722 + compositeSum = (dev->checkpointSum << 8) | (dev->checkpointXor & 0xFF);
723 + *sum = compositeSum;
727 +static int yaffs_CheckpointFlushBuffer(yaffs_Device *dev)
731 + int realignedChunk;
733 + yaffs_ExtendedTags tags;
735 + if(dev->checkpointCurrentBlock < 0){
736 + yaffs_CheckpointFindNextErasedBlock(dev);
737 + dev->checkpointCurrentChunk = 0;
740 + if(dev->checkpointCurrentBlock < 0)
743 + tags.chunkDeleted = 0;
744 + tags.objectId = dev->checkpointNextBlock; /* Hint to next place to look */
745 + tags.chunkId = dev->checkpointPageSequence + 1;
746 + tags.sequenceNumber = YAFFS_SEQUENCE_CHECKPOINT_DATA;
747 + tags.byteCount = dev->nDataBytesPerChunk;
748 + if(dev->checkpointCurrentChunk == 0){
749 + /* First chunk we write for the block? Set block state to
751 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointCurrentBlock);
752 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
753 + dev->blocksInCheckpoint++;
756 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock + dev->checkpointCurrentChunk;
759 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint wite buffer nand %d(%d:%d) objid %d chId %d" TENDSTR),
760 + chunk, dev->checkpointCurrentBlock, dev->checkpointCurrentChunk,tags.objectId,tags.chunkId));
762 + realignedChunk = chunk - dev->chunkOffset;
764 + dev->writeChunkWithTagsToNAND(dev,realignedChunk,dev->checkpointBuffer,&tags);
765 + dev->checkpointByteOffset = 0;
766 + dev->checkpointPageSequence++;
767 + dev->checkpointCurrentChunk++;
768 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock){
769 + dev->checkpointCurrentChunk = 0;
770 + dev->checkpointCurrentBlock = -1;
772 + memset(dev->checkpointBuffer,0,dev->nDataBytesPerChunk);
778 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes)
784 + __u8 * dataBytes = (__u8 *)data;
788 + if(!dev->checkpointBuffer)
791 + if(!dev->checkpointOpenForWrite)
794 + while(i < nBytes && ok) {
798 + dev->checkpointBuffer[dev->checkpointByteOffset] = *dataBytes ;
799 + dev->checkpointSum += *dataBytes;
800 + dev->checkpointXor ^= *dataBytes;
802 + dev->checkpointByteOffset++;
805 + dev->checkpointByteCount++;
808 + if(dev->checkpointByteOffset < 0 ||
809 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk)
810 + ok = yaffs_CheckpointFlushBuffer(dev);
817 +int yaffs_CheckpointRead(yaffs_Device *dev, void *data, int nBytes)
821 + yaffs_ExtendedTags tags;
825 + int realignedChunk;
827 + __u8 *dataBytes = (__u8 *)data;
829 + if(!dev->checkpointBuffer)
832 + if(dev->checkpointOpenForWrite)
835 + while(i < nBytes && ok) {
838 + if(dev->checkpointByteOffset < 0 ||
839 + dev->checkpointByteOffset >= dev->nDataBytesPerChunk) {
841 + if(dev->checkpointCurrentBlock < 0){
842 + yaffs_CheckpointFindNextCheckpointBlock(dev);
843 + dev->checkpointCurrentChunk = 0;
846 + if(dev->checkpointCurrentBlock < 0)
850 + chunk = dev->checkpointCurrentBlock * dev->nChunksPerBlock +
851 + dev->checkpointCurrentChunk;
853 + realignedChunk = chunk - dev->chunkOffset;
855 + /* read in the next chunk */
856 + /* printf("read checkpoint page %d\n",dev->checkpointPage); */
857 + dev->readChunkWithTagsFromNAND(dev, realignedChunk,
858 + dev->checkpointBuffer,
861 + if(tags.chunkId != (dev->checkpointPageSequence + 1) ||
862 + tags.sequenceNumber != YAFFS_SEQUENCE_CHECKPOINT_DATA)
865 + dev->checkpointByteOffset = 0;
866 + dev->checkpointPageSequence++;
867 + dev->checkpointCurrentChunk++;
869 + if(dev->checkpointCurrentChunk >= dev->nChunksPerBlock)
870 + dev->checkpointCurrentBlock = -1;
875 + *dataBytes = dev->checkpointBuffer[dev->checkpointByteOffset];
876 + dev->checkpointSum += *dataBytes;
877 + dev->checkpointXor ^= *dataBytes;
878 + dev->checkpointByteOffset++;
881 + dev->checkpointByteCount++;
888 +int yaffs_CheckpointClose(yaffs_Device *dev)
891 + if(dev->checkpointOpenForWrite){
892 + if(dev->checkpointByteOffset != 0)
893 + yaffs_CheckpointFlushBuffer(dev);
896 + for(i = 0; i < dev->blocksInCheckpoint && dev->checkpointBlockList[i] >= 0; i++){
897 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,dev->checkpointBlockList[i]);
898 + if(bi->blockState == YAFFS_BLOCK_STATE_EMPTY)
899 + bi->blockState = YAFFS_BLOCK_STATE_CHECKPOINT;
901 + // Todo this looks odd...
904 + YFREE(dev->checkpointBlockList);
905 + dev->checkpointBlockList = NULL;
908 + dev->nFreeChunks -= dev->blocksInCheckpoint * dev->nChunksPerBlock;
909 + dev->nErasedBlocks -= dev->blocksInCheckpoint;
912 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint byte count %d" TENDSTR),
913 + dev->checkpointByteCount));
915 + if(dev->checkpointBuffer){
916 + /* free the buffer */
917 + YFREE(dev->checkpointBuffer);
918 + dev->checkpointBuffer = NULL;
926 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev)
928 + /* Erase the first checksum block */
930 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("checkpoint invalidate"TENDSTR)));
932 + if(!yaffs_CheckpointSpaceOk(dev))
935 + return yaffs_CheckpointErase(dev);
940 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h
941 --- linux-2.6.21.1/fs/yaffs2/yaffs_checkptrw.h 1970-01-01 01:00:00.000000000 +0100
942 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_checkptrw.h 2007-05-30 13:17:16.000000000 +0200
945 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
947 + * Copyright (C) 2002-2007 Aleph One Ltd.
948 + * for Toby Churchill Ltd and Brightstar Engineering
950 + * Created by Charles Manning <charles@aleph1.co.uk>
952 + * This program is free software; you can redistribute it and/or modify
953 + * it under the terms of the GNU Lesser General Public License version 2.1 as
954 + * published by the Free Software Foundation.
956 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
959 +#ifndef __YAFFS_CHECKPTRW_H__
960 +#define __YAFFS_CHECKPTRW_H__
962 +#include "yaffs_guts.h"
964 +int yaffs_CheckpointOpen(yaffs_Device *dev, int forWriting);
966 +int yaffs_CheckpointWrite(yaffs_Device *dev,const void *data, int nBytes);
968 +int yaffs_CheckpointRead(yaffs_Device *dev,void *data, int nBytes);
970 +int yaffs_GetCheckpointSum(yaffs_Device *dev, __u32 *sum);
972 +int yaffs_CheckpointClose(yaffs_Device *dev);
974 +int yaffs_CheckpointInvalidateStream(yaffs_Device *dev);
979 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c
980 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.c 1970-01-01 01:00:00.000000000 +0100
981 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.c 2007-05-30 13:17:16.000000000 +0200
984 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
986 + * Copyright (C) 2002-2007 Aleph One Ltd.
987 + * for Toby Churchill Ltd and Brightstar Engineering
989 + * Created by Charles Manning <charles@aleph1.co.uk>
991 + * This program is free software; you can redistribute it and/or modify
992 + * it under the terms of the GNU General Public License version 2 as
993 + * published by the Free Software Foundation.
997 + * This code implements the ECC algorithm used in SmartMedia.
999 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1000 + * The two unused bit are set to 1.
1001 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1002 + * blocks are used on a 512-byte NAND page.
1006 +/* Table generated by gen-ecc.c
1007 + * Using a table means we do not have to calculate p1..p4 and p1'..p4'
1008 + * for each byte of data. These are instead provided in a table in bits7..2.
1009 + * Bit 0 of each entry indicates whether the entry has an odd or even parity, and therefore
1010 + * this bytes influence on the line parity.
1013 +const char *yaffs_ecc_c_version =
1014 + "$Id: yaffs_ecc.c,v 1.9 2007-02-14 01:09:06 wookey Exp $";
1016 +#include "yportenv.h"
1018 +#include "yaffs_ecc.h"
1020 +static const unsigned char column_parity_table[] = {
1021 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1022 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1023 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1024 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1025 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1026 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1027 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1028 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1029 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1030 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1031 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1032 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1033 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1034 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1035 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1036 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1037 + 0xa9, 0xfc, 0xf0, 0xa5, 0xcc, 0x99, 0x95, 0xc0,
1038 + 0xc0, 0x95, 0x99, 0xcc, 0xa5, 0xf0, 0xfc, 0xa9,
1039 + 0x3c, 0x69, 0x65, 0x30, 0x59, 0x0c, 0x00, 0x55,
1040 + 0x55, 0x00, 0x0c, 0x59, 0x30, 0x65, 0x69, 0x3c,
1041 + 0x30, 0x65, 0x69, 0x3c, 0x55, 0x00, 0x0c, 0x59,
1042 + 0x59, 0x0c, 0x00, 0x55, 0x3c, 0x69, 0x65, 0x30,
1043 + 0xa5, 0xf0, 0xfc, 0xa9, 0xc0, 0x95, 0x99, 0xcc,
1044 + 0xcc, 0x99, 0x95, 0xc0, 0xa9, 0xfc, 0xf0, 0xa5,
1045 + 0x0c, 0x59, 0x55, 0x00, 0x69, 0x3c, 0x30, 0x65,
1046 + 0x65, 0x30, 0x3c, 0x69, 0x00, 0x55, 0x59, 0x0c,
1047 + 0x99, 0xcc, 0xc0, 0x95, 0xfc, 0xa9, 0xa5, 0xf0,
1048 + 0xf0, 0xa5, 0xa9, 0xfc, 0x95, 0xc0, 0xcc, 0x99,
1049 + 0x95, 0xc0, 0xcc, 0x99, 0xf0, 0xa5, 0xa9, 0xfc,
1050 + 0xfc, 0xa9, 0xa5, 0xf0, 0x99, 0xcc, 0xc0, 0x95,
1051 + 0x00, 0x55, 0x59, 0x0c, 0x65, 0x30, 0x3c, 0x69,
1052 + 0x69, 0x3c, 0x30, 0x65, 0x0c, 0x59, 0x55, 0x00,
1055 +/* Count the bits in an unsigned char or a U32 */
1057 +static int yaffs_CountBits(unsigned char x)
1068 +static int yaffs_CountBits32(unsigned x)
1079 +/* Calculate the ECC for a 256-byte block of data */
1080 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc)
1084 + unsigned char col_parity = 0;
1085 + unsigned char line_parity = 0;
1086 + unsigned char line_parity_prime = 0;
1090 + for (i = 0; i < 256; i++) {
1091 + b = column_parity_table[*data++];
1094 + if (b & 0x01) // odd number of bits in the byte
1097 + line_parity_prime ^= ~i;
1102 + ecc[2] = (~col_parity) | 0x03;
1105 + if (line_parity & 0x80)
1107 + if (line_parity_prime & 0x80)
1109 + if (line_parity & 0x40)
1111 + if (line_parity_prime & 0x40)
1113 + if (line_parity & 0x20)
1115 + if (line_parity_prime & 0x20)
1117 + if (line_parity & 0x10)
1119 + if (line_parity_prime & 0x10)
1124 + if (line_parity & 0x08)
1126 + if (line_parity_prime & 0x08)
1128 + if (line_parity & 0x04)
1130 + if (line_parity_prime & 0x04)
1132 + if (line_parity & 0x02)
1134 + if (line_parity_prime & 0x02)
1136 + if (line_parity & 0x01)
1138 + if (line_parity_prime & 0x01)
1142 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1143 + // Swap the bytes into the wrong order
1151 +/* Correct the ECC on a 256 byte block of data */
1153 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1154 + const unsigned char *test_ecc)
1156 + unsigned char d0, d1, d2; /* deltas */
1158 + d0 = read_ecc[0] ^ test_ecc[0];
1159 + d1 = read_ecc[1] ^ test_ecc[1];
1160 + d2 = read_ecc[2] ^ test_ecc[2];
1162 + if ((d0 | d1 | d2) == 0)
1163 + return 0; /* no error */
1165 + if (((d0 ^ (d0 >> 1)) & 0x55) == 0x55 &&
1166 + ((d1 ^ (d1 >> 1)) & 0x55) == 0x55 &&
1167 + ((d2 ^ (d2 >> 1)) & 0x54) == 0x54) {
1168 + /* Single bit (recoverable) error in data */
1173 +#ifdef CONFIG_YAFFS_ECC_WRONG_ORDER
1174 + // swap the bytes to correct for the wrong order
1208 + data[byte] ^= (1 << bit);
1210 + return 1; /* Corrected the error */
1213 + if ((yaffs_CountBits(d0) +
1214 + yaffs_CountBits(d1) +
1215 + yaffs_CountBits(d2)) == 1) {
1216 + /* Reccoverable error in ecc */
1218 + read_ecc[0] = test_ecc[0];
1219 + read_ecc[1] = test_ecc[1];
1220 + read_ecc[2] = test_ecc[2];
1222 + return 1; /* Corrected the error */
1225 + /* Unrecoverable error */
1233 + * ECCxxxOther does ECC calcs on arbitrary n bytes of data
1235 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1236 + yaffs_ECCOther * eccOther)
1240 + unsigned char col_parity = 0;
1241 + unsigned line_parity = 0;
1242 + unsigned line_parity_prime = 0;
1245 + for (i = 0; i < nBytes; i++) {
1246 + b = column_parity_table[*data++];
1250 + /* odd number of bits in the byte */
1252 + line_parity_prime ^= ~i;
1257 + eccOther->colParity = (col_parity >> 2) & 0x3f;
1258 + eccOther->lineParity = line_parity;
1259 + eccOther->lineParityPrime = line_parity_prime;
1262 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1263 + yaffs_ECCOther * read_ecc,
1264 + const yaffs_ECCOther * test_ecc)
1266 + unsigned char cDelta; /* column parity delta */
1267 + unsigned lDelta; /* line parity delta */
1268 + unsigned lDeltaPrime; /* line parity delta */
1271 + cDelta = read_ecc->colParity ^ test_ecc->colParity;
1272 + lDelta = read_ecc->lineParity ^ test_ecc->lineParity;
1273 + lDeltaPrime = read_ecc->lineParityPrime ^ test_ecc->lineParityPrime;
1275 + if ((cDelta | lDelta | lDeltaPrime) == 0)
1276 + return 0; /* no error */
1278 + if (lDelta == ~lDeltaPrime &&
1279 + (((cDelta ^ (cDelta >> 1)) & 0x15) == 0x15))
1281 + /* Single bit (recoverable) error in data */
1285 + if (cDelta & 0x20)
1287 + if (cDelta & 0x08)
1289 + if (cDelta & 0x02)
1292 + if(lDelta >= nBytes)
1295 + data[lDelta] ^= (1 << bit);
1297 + return 1; /* corrected */
1300 + if ((yaffs_CountBits32(lDelta) + yaffs_CountBits32(lDeltaPrime) +
1301 + yaffs_CountBits(cDelta)) == 1) {
1302 + /* Reccoverable error in ecc */
1304 + *read_ecc = *test_ecc;
1305 + return 1; /* corrected */
1308 + /* Unrecoverable error */
1314 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h
1315 --- linux-2.6.21.1/fs/yaffs2/yaffs_ecc.h 1970-01-01 01:00:00.000000000 +0100
1316 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_ecc.h 2007-05-30 13:17:16.000000000 +0200
1319 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
1321 + * Copyright (C) 2002-2007 Aleph One Ltd.
1322 + * for Toby Churchill Ltd and Brightstar Engineering
1324 + * Created by Charles Manning <charles@aleph1.co.uk>
1326 + * This program is free software; you can redistribute it and/or modify
1327 + * it under the terms of the GNU Lesser General Public License version 2.1 as
1328 + * published by the Free Software Foundation.
1330 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
1334 + * This code implements the ECC algorithm used in SmartMedia.
1336 + * The ECC comprises 22 bits of parity information and is stuffed into 3 bytes.
1337 + * The two unused bit are set to 1.
1338 + * The ECC can correct single bit errors in a 256-byte page of data. Thus, two such ECC
1339 + * blocks are used on a 512-byte NAND page.
1343 +#ifndef __YAFFS_ECC_H__
1344 +#define __YAFFS_ECC_H__
1347 + unsigned char colParity;
1348 + unsigned lineParity;
1349 + unsigned lineParityPrime;
1352 +void yaffs_ECCCalculate(const unsigned char *data, unsigned char *ecc);
1353 +int yaffs_ECCCorrect(unsigned char *data, unsigned char *read_ecc,
1354 + const unsigned char *test_ecc);
1356 +void yaffs_ECCCalculateOther(const unsigned char *data, unsigned nBytes,
1357 + yaffs_ECCOther * ecc);
1358 +int yaffs_ECCCorrectOther(unsigned char *data, unsigned nBytes,
1359 + yaffs_ECCOther * read_ecc,
1360 + const yaffs_ECCOther * test_ecc);
1362 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_fs.c linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c
1363 --- linux-2.6.21.1/fs/yaffs2/yaffs_fs.c 1970-01-01 01:00:00.000000000 +0100
1364 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_fs.c 2007-05-30 13:17:16.000000000 +0200
1367 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
1369 + * Copyright (C) 2002-2007 Aleph One Ltd.
1370 + * for Toby Churchill Ltd and Brightstar Engineering
1372 + * Created by Charles Manning <charles@aleph1.co.uk>
1373 + * Acknowledgements:
1374 + * Luc van OostenRyck for numerous patches.
1375 + * Nick Bane for numerous patches.
1376 + * Nick Bane for 2.5/2.6 integration.
1377 + * Andras Toth for mknod rdev issue.
1378 + * Michael Fischer for finding the problem with inode inconsistency.
1379 + * Some code bodily lifted from JFFS
1381 + * This program is free software; you can redistribute it and/or modify
1382 + * it under the terms of the GNU General Public License version 2 as
1383 + * published by the Free Software Foundation.
1388 + * This is the file system front-end to YAFFS that hooks it up to
1392 + * >> 2.4: sb->u.generic_sbp points to the yaffs_Device associated with
1394 + * >> 2.6: sb->s_fs_info points to the yaffs_Device associated with this
1396 + * >> inode->u.generic_ip points to the associated yaffs_Object.
1399 +const char *yaffs_fs_c_version =
1400 + "$Id: yaffs_fs.c,v 1.60 2007-05-15 20:07:40 charles Exp $";
1401 +extern const char *yaffs_guts_c_version;
1403 +#include <linux/version.h>
1404 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
1405 +#include <linux/config.h>
1407 +#include <linux/kernel.h>
1408 +#include <linux/module.h>
1409 +#include <linux/slab.h>
1410 +#include <linux/init.h>
1411 +#include <linux/list.h>
1412 +#include <linux/fs.h>
1413 +#include <linux/proc_fs.h>
1414 +#include <linux/smp_lock.h>
1415 +#include <linux/pagemap.h>
1416 +#include <linux/mtd/mtd.h>
1417 +#include <linux/interrupt.h>
1418 +#include <linux/string.h>
1419 +#include <linux/ctype.h>
1421 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1423 +#include <linux/statfs.h> /* Added NCB 15-8-2003 */
1424 +#include <asm/statfs.h>
1425 +#define UnlockPage(p) unlock_page(p)
1426 +#define Page_Uptodate(page) test_bit(PG_uptodate, &(page)->flags)
1428 +/* FIXME: use sb->s_id instead ? */
1429 +#define yaffs_devname(sb, buf) bdevname(sb->s_bdev, buf)
1433 +#include <linux/locks.h>
1434 +#define BDEVNAME_SIZE 0
1435 +#define yaffs_devname(sb, buf) kdevname(sb->s_dev)
1437 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
1438 +/* added NCB 26/5/2006 for 2.4.25-vrs2-tcl1 kernel */
1444 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1445 +#define WRITE_SIZE_STR "writesize"
1446 +#define WRITE_SIZE(mtd) (mtd)->writesize
1448 +#define WRITE_SIZE_STR "oobblock"
1449 +#define WRITE_SIZE(mtd) (mtd)->oobblock
1452 +#include <asm/uaccess.h>
1454 +#include "yportenv.h"
1455 +#include "yaffs_guts.h"
1457 +#include <linux/mtd/mtd.h>
1458 +#include "yaffs_mtdif.h"
1459 +#include "yaffs_mtdif1.h"
1460 +#include "yaffs_mtdif2.h"
1462 +unsigned int yaffs_traceMask = YAFFS_TRACE_BAD_BLOCKS;
1463 +unsigned int yaffs_wr_attempts = YAFFS_WR_ATTEMPTS;
1465 +/* Module Parameters */
1466 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1467 +module_param(yaffs_traceMask,uint,0644);
1468 +module_param(yaffs_wr_attempts,uint,0644);
1470 +MODULE_PARM(yaffs_traceMask,"i");
1471 +MODULE_PARM(yaffs_wr_attempts,"i");
1474 +/*#define T(x) printk x */
1476 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1477 +#define yaffs_InodeToObjectLV(iptr) (iptr)->i_private
1479 +#define yaffs_InodeToObjectLV(iptr) (iptr)->u.generic_ip
1482 +#define yaffs_InodeToObject(iptr) ((yaffs_Object *)(yaffs_InodeToObjectLV(iptr)))
1483 +#define yaffs_DentryToObject(dptr) yaffs_InodeToObject((dptr)->d_inode)
1485 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1486 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->s_fs_info)
1488 +#define yaffs_SuperToDevice(sb) ((yaffs_Device *)sb->u.generic_sbp)
1491 +static void yaffs_put_super(struct super_block *sb);
1493 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
1496 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1497 +static int yaffs_file_flush(struct file *file, fl_owner_t id);
1499 +static int yaffs_file_flush(struct file *file);
1502 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
1505 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir);
1507 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1508 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
1509 + struct nameidata *n);
1510 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1511 + struct nameidata *n);
1513 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode);
1514 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry);
1516 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
1517 + struct dentry *dentry);
1518 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry);
1519 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
1520 + const char *symname);
1521 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
1523 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1524 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1527 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
1530 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
1531 + struct inode *new_dir, struct dentry *new_dentry);
1532 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr);
1534 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1535 +static int yaffs_sync_fs(struct super_block *sb, int wait);
1536 +static void yaffs_write_super(struct super_block *sb);
1538 +static int yaffs_sync_fs(struct super_block *sb);
1539 +static int yaffs_write_super(struct super_block *sb);
1542 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1543 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf);
1544 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1545 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf);
1547 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf);
1549 +static void yaffs_read_inode(struct inode *inode);
1551 +static void yaffs_put_inode(struct inode *inode);
1552 +static void yaffs_delete_inode(struct inode *);
1553 +static void yaffs_clear_inode(struct inode *);
1555 +static int yaffs_readpage(struct file *file, struct page *page);
1556 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1557 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc);
1559 +static int yaffs_writepage(struct page *page);
1561 +static int yaffs_prepare_write(struct file *f, struct page *pg,
1562 + unsigned offset, unsigned to);
1563 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
1566 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1568 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1569 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1571 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd);
1574 +static struct address_space_operations yaffs_file_address_operations = {
1575 + .readpage = yaffs_readpage,
1576 + .writepage = yaffs_writepage,
1577 + .prepare_write = yaffs_prepare_write,
1578 + .commit_write = yaffs_commit_write,
1581 +static struct file_operations yaffs_file_operations = {
1582 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18))
1583 + .read = do_sync_read,
1584 + .write = do_sync_write,
1585 + .aio_read = generic_file_aio_read,
1586 + .aio_write = generic_file_aio_write,
1588 + .read = generic_file_read,
1589 + .write = generic_file_write,
1591 + .mmap = generic_file_mmap,
1592 + .flush = yaffs_file_flush,
1593 + .fsync = yaffs_sync_object,
1594 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1595 + .sendfile = generic_file_sendfile,
1600 +static struct inode_operations yaffs_file_inode_operations = {
1601 + .setattr = yaffs_setattr,
1604 +static struct inode_operations yaffs_symlink_inode_operations = {
1605 + .readlink = yaffs_readlink,
1606 + .follow_link = yaffs_follow_link,
1607 + .setattr = yaffs_setattr,
1610 +static struct inode_operations yaffs_dir_inode_operations = {
1611 + .create = yaffs_create,
1612 + .lookup = yaffs_lookup,
1613 + .link = yaffs_link,
1614 + .unlink = yaffs_unlink,
1615 + .symlink = yaffs_symlink,
1616 + .mkdir = yaffs_mkdir,
1617 + .rmdir = yaffs_unlink,
1618 + .mknod = yaffs_mknod,
1619 + .rename = yaffs_rename,
1620 + .setattr = yaffs_setattr,
1623 +static struct file_operations yaffs_dir_operations = {
1624 + .read = generic_read_dir,
1625 + .readdir = yaffs_readdir,
1626 + .fsync = yaffs_sync_object,
1629 +static struct super_operations yaffs_super_ops = {
1630 + .statfs = yaffs_statfs,
1631 + .read_inode = yaffs_read_inode,
1632 + .put_inode = yaffs_put_inode,
1633 + .put_super = yaffs_put_super,
1634 + .delete_inode = yaffs_delete_inode,
1635 + .clear_inode = yaffs_clear_inode,
1636 + .sync_fs = yaffs_sync_fs,
1637 + .write_super = yaffs_write_super,
1640 +static void yaffs_GrossLock(yaffs_Device * dev)
1642 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs locking\n"));
1644 + down(&dev->grossLock);
1647 +static void yaffs_GrossUnlock(yaffs_Device * dev)
1649 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs unlocking\n"));
1650 + up(&dev->grossLock);
1654 +static int yaffs_readlink(struct dentry *dentry, char __user * buffer,
1657 + unsigned char *alias;
1660 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1662 + yaffs_GrossLock(dev);
1664 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1666 + yaffs_GrossUnlock(dev);
1671 + ret = vfs_readlink(dentry, buffer, buflen, alias);
1676 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1677 +static void *yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1679 +static int yaffs_follow_link(struct dentry *dentry, struct nameidata *nd)
1682 + unsigned char *alias;
1684 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
1686 + yaffs_GrossLock(dev);
1688 + alias = yaffs_GetSymlinkAlias(yaffs_DentryToObject(dentry));
1690 + yaffs_GrossUnlock(dev);
1698 + ret = vfs_follow_link(nd, alias);
1701 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1702 + return ERR_PTR (ret);
1708 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
1709 + yaffs_Object * obj);
1712 + * Lookup is used to find objects in the fs
1714 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1716 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry,
1717 + struct nameidata *n)
1719 +static struct dentry *yaffs_lookup(struct inode *dir, struct dentry *dentry)
1722 + yaffs_Object *obj;
1723 + struct inode *inode = NULL; /* NCB 2.5/2.6 needs NULL here */
1725 + yaffs_Device *dev = yaffs_InodeToObject(dir)->myDev;
1727 + yaffs_GrossLock(dev);
1730 + (KERN_DEBUG "yaffs_lookup for %d:%s\n",
1731 + yaffs_InodeToObject(dir)->objectId, dentry->d_name.name));
1734 + yaffs_FindObjectByName(yaffs_InodeToObject(dir),
1735 + dentry->d_name.name);
1737 + obj = yaffs_GetEquivalentObject(obj); /* in case it was a hardlink */
1739 + /* Can't hold gross lock when calling yaffs_get_inode() */
1740 + yaffs_GrossUnlock(dev);
1744 + (KERN_DEBUG "yaffs_lookup found %d\n", obj->objectId));
1746 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
1750 + (KERN_DEBUG "yaffs_loookup dentry \n"));
1751 +/* #if 0 asserted by NCB for 2.5/6 compatability - falls through to
1752 + * d_add even if NULL inode */
1754 + /*dget(dentry); // try to solve directory bug */
1755 + d_add(dentry, inode);
1757 + /* return dentry; */
1763 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_lookup not found\n"));
1767 +/* added NCB for 2.5/6 compatability - forces add even if inode is
1768 + * NULL which creates dentry hash */
1769 + d_add(dentry, inode);
1772 + /* return (ERR_PTR(-EIO)); */
1776 +/* For now put inode is just for debugging
1777 + * Put inode is called when the inode **structure** is put.
1779 +static void yaffs_put_inode(struct inode *inode)
1782 + ("yaffs_put_inode: ino %d, count %d\n", (int)inode->i_ino,
1783 + atomic_read(&inode->i_count)));
1787 +/* clear is called to tell the fs to release any per-inode data it holds */
1788 +static void yaffs_clear_inode(struct inode *inode)
1790 + yaffs_Object *obj;
1791 + yaffs_Device *dev;
1793 + obj = yaffs_InodeToObject(inode);
1796 + ("yaffs_clear_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1797 + atomic_read(&inode->i_count),
1798 + obj ? "object exists" : "null object"));
1802 + yaffs_GrossLock(dev);
1804 + /* Clear the association between the inode and
1805 + * the yaffs_Object.
1807 + obj->myInode = NULL;
1808 + yaffs_InodeToObjectLV(inode) = NULL;
1810 + /* If the object freeing was deferred, then the real
1811 + * free happens now.
1812 + * This should fix the inode inconsistency problem.
1815 + yaffs_HandleDeferedFree(obj);
1817 + yaffs_GrossUnlock(dev);
1822 +/* delete is called when the link count is zero and the inode
1823 + * is put (ie. nobody wants to know about it anymore, time to
1824 + * delete the file).
1825 + * NB Must call clear_inode()
1827 +static void yaffs_delete_inode(struct inode *inode)
1829 + yaffs_Object *obj = yaffs_InodeToObject(inode);
1830 + yaffs_Device *dev;
1833 + ("yaffs_delete_inode: ino %d, count %d %s\n", (int)inode->i_ino,
1834 + atomic_read(&inode->i_count),
1835 + obj ? "object exists" : "null object"));
1839 + yaffs_GrossLock(dev);
1840 + yaffs_DeleteFile(obj);
1841 + yaffs_GrossUnlock(dev);
1843 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,13))
1844 + truncate_inode_pages (&inode->i_data, 0);
1846 + clear_inode(inode);
1849 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
1850 +static int yaffs_file_flush(struct file *file, fl_owner_t id)
1852 +static int yaffs_file_flush(struct file *file)
1855 + yaffs_Object *obj = yaffs_DentryToObject(file->f_dentry);
1857 + yaffs_Device *dev = obj->myDev;
1860 + (KERN_DEBUG "yaffs_file_flush object %d (%s)\n", obj->objectId,
1861 + obj->dirty ? "dirty" : "clean"));
1863 + yaffs_GrossLock(dev);
1865 + yaffs_FlushFile(obj, 1);
1867 + yaffs_GrossUnlock(dev);
1872 +static int yaffs_readpage_nolock(struct file *f, struct page *pg)
1874 + /* Lifted from jffs2 */
1876 + yaffs_Object *obj;
1877 + unsigned char *pg_buf;
1880 + yaffs_Device *dev;
1882 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage at %08x, size %08x\n",
1883 + (unsigned)(pg->index << PAGE_CACHE_SHIFT),
1884 + (unsigned)PAGE_CACHE_SIZE));
1886 + obj = yaffs_DentryToObject(f->f_dentry);
1890 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1891 + BUG_ON(!PageLocked(pg));
1893 + if (!PageLocked(pg))
1897 + pg_buf = kmap(pg);
1898 + /* FIXME: Can kmap fail? */
1900 + yaffs_GrossLock(dev);
1903 + yaffs_ReadDataFromFile(obj, pg_buf, pg->index << PAGE_CACHE_SHIFT,
1906 + yaffs_GrossUnlock(dev);
1912 + ClearPageUptodate(pg);
1915 + SetPageUptodate(pg);
1916 + ClearPageError(pg);
1919 + flush_dcache_page(pg);
1922 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_readpage done\n"));
1926 +static int yaffs_readpage_unlock(struct file *f, struct page *pg)
1928 + int ret = yaffs_readpage_nolock(f, pg);
1933 +static int yaffs_readpage(struct file *f, struct page *pg)
1935 + return yaffs_readpage_unlock(f, pg);
1938 +/* writepage inspired by/stolen from smbfs */
1940 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
1941 +static int yaffs_writepage(struct page *page, struct writeback_control *wbc)
1943 +static int yaffs_writepage(struct page *page)
1946 + struct address_space *mapping = page->mapping;
1947 + loff_t offset = (loff_t) page->index << PAGE_CACHE_SHIFT;
1948 + struct inode *inode;
1949 + unsigned long end_index;
1951 + yaffs_Object *obj;
1957 + inode = mapping->host;
1961 + if (offset > inode->i_size) {
1964 + "yaffs_writepage at %08x, inode size = %08x!!!\n",
1965 + (unsigned)(page->index << PAGE_CACHE_SHIFT),
1966 + (unsigned)inode->i_size));
1968 + (KERN_DEBUG " -> don't care!!\n"));
1969 + unlock_page(page);
1973 + end_index = inode->i_size >> PAGE_CACHE_SHIFT;
1976 + if (page->index < end_index) {
1977 + nBytes = PAGE_CACHE_SIZE;
1979 + nBytes = inode->i_size & (PAGE_CACHE_SIZE - 1);
1984 + buffer = kmap(page);
1986 + obj = yaffs_InodeToObject(inode);
1987 + yaffs_GrossLock(obj->myDev);
1990 + (KERN_DEBUG "yaffs_writepage at %08x, size %08x\n",
1991 + (unsigned)(page->index << PAGE_CACHE_SHIFT), nBytes));
1993 + (KERN_DEBUG "writepag0: obj = %05x, ino = %05x\n",
1994 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
1997 + yaffs_WriteDataToFile(obj, buffer, page->index << PAGE_CACHE_SHIFT,
2001 + (KERN_DEBUG "writepag1: obj = %05x, ino = %05x\n",
2002 + (int)obj->variant.fileVariant.fileSize, (int)inode->i_size));
2004 + yaffs_GrossUnlock(obj->myDev);
2007 + SetPageUptodate(page);
2011 + return (nWritten == nBytes) ? 0 : -ENOSPC;
2014 +static int yaffs_prepare_write(struct file *f, struct page *pg,
2015 + unsigned offset, unsigned to)
2018 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_prepair_write\n"));
2019 + if (!Page_Uptodate(pg) && (offset || to < PAGE_CACHE_SIZE))
2020 + return yaffs_readpage_nolock(f, pg);
2026 +static int yaffs_commit_write(struct file *f, struct page *pg, unsigned offset,
2030 + void *addr = page_address(pg) + offset;
2031 + loff_t pos = (((loff_t) pg->index) << PAGE_CACHE_SHIFT) + offset;
2032 + int nBytes = to - offset;
2035 + unsigned spos = pos;
2036 + unsigned saddr = (unsigned)addr;
2039 + (KERN_DEBUG "yaffs_commit_write addr %x pos %x nBytes %d\n", saddr,
2042 + nWritten = yaffs_file_write(f, addr, nBytes, &pos);
2044 + if (nWritten != nBytes) {
2047 + "yaffs_commit_write not same size nWritten %d nBytes %d\n",
2048 + nWritten, nBytes));
2050 + ClearPageUptodate(pg);
2052 + SetPageUptodate(pg);
2056 + (KERN_DEBUG "yaffs_commit_write returning %d\n",
2057 + nWritten == nBytes ? 0 : nWritten));
2059 + return nWritten == nBytes ? 0 : nWritten;
2063 +static void yaffs_FillInodeFromObject(struct inode *inode, yaffs_Object * obj)
2065 + if (inode && obj) {
2068 + /* Check mode against the variant type and attempt to repair if broken. */
2069 + __u32 mode = obj->yst_mode;
2070 + switch( obj->variantType ){
2071 + case YAFFS_OBJECT_TYPE_FILE :
2072 + if( ! S_ISREG(mode) ){
2073 + obj->yst_mode &= ~S_IFMT;
2074 + obj->yst_mode |= S_IFREG;
2078 + case YAFFS_OBJECT_TYPE_SYMLINK :
2079 + if( ! S_ISLNK(mode) ){
2080 + obj->yst_mode &= ~S_IFMT;
2081 + obj->yst_mode |= S_IFLNK;
2085 + case YAFFS_OBJECT_TYPE_DIRECTORY :
2086 + if( ! S_ISDIR(mode) ){
2087 + obj->yst_mode &= ~S_IFMT;
2088 + obj->yst_mode |= S_IFDIR;
2092 + case YAFFS_OBJECT_TYPE_UNKNOWN :
2093 + case YAFFS_OBJECT_TYPE_HARDLINK :
2094 + case YAFFS_OBJECT_TYPE_SPECIAL :
2100 + inode->i_ino = obj->objectId;
2101 + inode->i_mode = obj->yst_mode;
2102 + inode->i_uid = obj->yst_uid;
2103 + inode->i_gid = obj->yst_gid;
2104 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
2105 + inode->i_blksize = inode->i_sb->s_blocksize;
2107 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2109 + inode->i_rdev = old_decode_dev(obj->yst_rdev);
2110 + inode->i_atime.tv_sec = (time_t) (obj->yst_atime);
2111 + inode->i_atime.tv_nsec = 0;
2112 + inode->i_mtime.tv_sec = (time_t) obj->yst_mtime;
2113 + inode->i_mtime.tv_nsec = 0;
2114 + inode->i_ctime.tv_sec = (time_t) obj->yst_ctime;
2115 + inode->i_ctime.tv_nsec = 0;
2117 + inode->i_rdev = obj->yst_rdev;
2118 + inode->i_atime = obj->yst_atime;
2119 + inode->i_mtime = obj->yst_mtime;
2120 + inode->i_ctime = obj->yst_ctime;
2122 + inode->i_size = yaffs_GetObjectFileLength(obj);
2123 + inode->i_blocks = (inode->i_size + 511) >> 9;
2125 + inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2129 + "yaffs_FillInode mode %x uid %d gid %d size %d count %d\n",
2130 + inode->i_mode, inode->i_uid, inode->i_gid,
2131 + (int)inode->i_size, atomic_read(&inode->i_count)));
2133 + switch (obj->yst_mode & S_IFMT) {
2134 + default: /* fifo, device or socket */
2135 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2136 + init_special_inode(inode, obj->yst_mode,
2137 + old_decode_dev(obj->yst_rdev));
2139 + init_special_inode(inode, obj->yst_mode,
2140 + (dev_t) (obj->yst_rdev));
2143 + case S_IFREG: /* file */
2144 + inode->i_op = &yaffs_file_inode_operations;
2145 + inode->i_fop = &yaffs_file_operations;
2146 + inode->i_mapping->a_ops =
2147 + &yaffs_file_address_operations;
2149 + case S_IFDIR: /* directory */
2150 + inode->i_op = &yaffs_dir_inode_operations;
2151 + inode->i_fop = &yaffs_dir_operations;
2153 + case S_IFLNK: /* symlink */
2154 + inode->i_op = &yaffs_symlink_inode_operations;
2158 + yaffs_InodeToObjectLV(inode) = obj;
2160 + obj->myInode = inode;
2164 + (KERN_DEBUG "yaffs_FileInode invalid parameters\n"));
2169 +struct inode *yaffs_get_inode(struct super_block *sb, int mode, int dev,
2170 + yaffs_Object * obj)
2172 + struct inode *inode;
2176 + (KERN_DEBUG "yaffs_get_inode for NULL super_block!!\n"));
2183 + (KERN_DEBUG "yaffs_get_inode for NULL object!!\n"));
2189 + (KERN_DEBUG "yaffs_get_inode for object %d\n", obj->objectId));
2191 + inode = iget(sb, obj->objectId);
2193 + /* NB Side effect: iget calls back to yaffs_read_inode(). */
2194 + /* iget also increments the inode's i_count */
2195 + /* NB You can't be holding grossLock or deadlock will happen! */
2200 +static ssize_t yaffs_file_write(struct file *f, const char *buf, size_t n,
2203 + yaffs_Object *obj;
2204 + int nWritten, ipos;
2205 + struct inode *inode;
2206 + yaffs_Device *dev;
2208 + obj = yaffs_DentryToObject(f->f_dentry);
2212 + yaffs_GrossLock(dev);
2214 + inode = f->f_dentry->d_inode;
2216 + if (!S_ISBLK(inode->i_mode) && f->f_flags & O_APPEND) {
2217 + ipos = inode->i_size;
2224 + (KERN_DEBUG "yaffs_file_write: hey obj is null!\n"));
2228 + "yaffs_file_write about to write writing %d bytes"
2229 + "to object %d at %d\n",
2230 + n, obj->objectId, ipos));
2233 + nWritten = yaffs_WriteDataToFile(obj, buf, ipos, n, 0);
2236 + (KERN_DEBUG "yaffs_file_write writing %d bytes, %d written at %d\n",
2237 + n, nWritten, ipos));
2238 + if (nWritten > 0) {
2241 + if (ipos > inode->i_size) {
2242 + inode->i_size = ipos;
2243 + inode->i_blocks = (ipos + 511) >> 9;
2247 + "yaffs_file_write size updated to %d bytes, "
2249 + ipos, (int)(inode->i_blocks)));
2253 + yaffs_GrossUnlock(dev);
2254 + return nWritten == 0 ? -ENOSPC : nWritten;
2257 +static int yaffs_readdir(struct file *f, void *dirent, filldir_t filldir)
2259 + yaffs_Object *obj;
2260 + yaffs_Device *dev;
2261 + struct inode *inode = f->f_dentry->d_inode;
2262 + unsigned long offset, curoffs;
2263 + struct list_head *i;
2266 + char name[YAFFS_MAX_NAME_LENGTH + 1];
2268 + obj = yaffs_DentryToObject(f->f_dentry);
2271 + yaffs_GrossLock(dev);
2273 + offset = f->f_pos;
2275 + T(YAFFS_TRACE_OS, ("yaffs_readdir: starting at %d\n", (int)offset));
2277 + if (offset == 0) {
2279 + (KERN_DEBUG "yaffs_readdir: entry . ino %d \n",
2280 + (int)inode->i_ino));
2281 + if (filldir(dirent, ".", 1, offset, inode->i_ino, DT_DIR)
2288 + if (offset == 1) {
2290 + (KERN_DEBUG "yaffs_readdir: entry .. ino %d \n",
2291 + (int)f->f_dentry->d_parent->d_inode->i_ino));
2293 + (dirent, "..", 2, offset,
2294 + f->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0) {
2303 + /* If the directory has changed since the open or last call to
2304 + readdir, rewind to after the 2 canned entries. */
2306 + if (f->f_version != inode->i_version) {
2308 + f->f_pos = offset;
2309 + f->f_version = inode->i_version;
2312 + list_for_each(i, &obj->variant.directoryVariant.children) {
2314 + if (curoffs >= offset) {
2315 + l = list_entry(i, yaffs_Object, siblings);
2317 + yaffs_GetObjectName(l, name,
2318 + YAFFS_MAX_NAME_LENGTH + 1);
2320 + (KERN_DEBUG "yaffs_readdir: %s inode %d\n", name,
2321 + yaffs_GetObjectInode(l)));
2323 + if (filldir(dirent,
2327 + yaffs_GetObjectInode(l),
2328 + yaffs_GetObjectType(l))
2341 + yaffs_GrossUnlock(dev);
2347 + * File creation. Allocate an inode, and we're done..
2349 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2350 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2353 +static int yaffs_mknod(struct inode *dir, struct dentry *dentry, int mode,
2357 + struct inode *inode;
2359 + yaffs_Object *obj = NULL;
2360 + yaffs_Device *dev;
2362 + yaffs_Object *parent = yaffs_InodeToObject(dir);
2364 + int error = -ENOSPC;
2365 + uid_t uid = current->fsuid;
2366 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2368 + if((dir->i_mode & S_ISGID) && S_ISDIR(mode))
2373 + (KERN_DEBUG "yaffs_mknod: parent object %d type %d\n",
2374 + parent->objectId, parent->variantType));
2377 + (KERN_DEBUG "yaffs_mknod: could not get parent object\n"));
2381 + T(YAFFS_TRACE_OS, ("yaffs_mknod: making oject for %s, "
2382 + "mode %x dev %x\n",
2383 + dentry->d_name.name, mode, rdev));
2385 + dev = parent->myDev;
2387 + yaffs_GrossLock(dev);
2389 + switch (mode & S_IFMT) {
2391 + /* Special (socket, fifo, device...) */
2392 + T(YAFFS_TRACE_OS, (KERN_DEBUG
2393 + "yaffs_mknod: making special\n"));
2394 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2396 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2397 + gid, old_encode_dev(rdev));
2400 + yaffs_MknodSpecial(parent, dentry->d_name.name, mode, uid,
2404 + case S_IFREG: /* file */
2405 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2407 + yaffs_MknodFile(parent, dentry->d_name.name, mode, uid,
2410 + case S_IFDIR: /* directory */
2412 + (KERN_DEBUG "yaffs_mknod: making directory\n"));
2414 + yaffs_MknodDirectory(parent, dentry->d_name.name, mode,
2417 + case S_IFLNK: /* symlink */
2418 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mknod: making file\n"));
2419 + obj = NULL; /* Do we ever get here? */
2423 + /* Can not call yaffs_get_inode() with gross lock held */
2424 + yaffs_GrossUnlock(dev);
2427 + inode = yaffs_get_inode(dir->i_sb, mode, rdev, obj);
2428 + d_instantiate(dentry, inode);
2430 + (KERN_DEBUG "yaffs_mknod created object %d count = %d\n",
2431 + obj->objectId, atomic_read(&inode->i_count)));
2435 + (KERN_DEBUG "yaffs_mknod failed making object\n"));
2442 +static int yaffs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2445 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_mkdir\n"));
2446 + retVal = yaffs_mknod(dir, dentry, mode | S_IFDIR, 0);
2448 + /* attempt to fix dir bug - didn't work */
2456 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2457 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode,
2458 + struct nameidata *n)
2460 +static int yaffs_create(struct inode *dir, struct dentry *dentry, int mode)
2463 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_create\n"));
2464 + return yaffs_mknod(dir, dentry, mode | S_IFREG, 0);
2467 +static int yaffs_unlink(struct inode *dir, struct dentry *dentry)
2471 + yaffs_Device *dev;
2474 + (KERN_DEBUG "yaffs_unlink %d:%s\n", (int)(dir->i_ino),
2475 + dentry->d_name.name));
2477 + dev = yaffs_InodeToObject(dir)->myDev;
2479 + yaffs_GrossLock(dev);
2481 + retVal = yaffs_Unlink(yaffs_InodeToObject(dir), dentry->d_name.name);
2483 + if (retVal == YAFFS_OK) {
2484 + dentry->d_inode->i_nlink--;
2486 + yaffs_GrossUnlock(dev);
2487 + mark_inode_dirty(dentry->d_inode);
2490 + yaffs_GrossUnlock(dev);
2491 + return -ENOTEMPTY;
2495 + * Create a link...
2497 +static int yaffs_link(struct dentry *old_dentry, struct inode *dir,
2498 + struct dentry *dentry)
2500 + struct inode *inode = old_dentry->d_inode;
2501 + yaffs_Object *obj = NULL;
2502 + yaffs_Object *link = NULL;
2503 + yaffs_Device *dev;
2505 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_link\n"));
2507 + obj = yaffs_InodeToObject(inode);
2510 + yaffs_GrossLock(dev);
2512 + if (!S_ISDIR(inode->i_mode)) /* Don't link directories */
2515 + yaffs_Link(yaffs_InodeToObject(dir), dentry->d_name.name,
2520 + old_dentry->d_inode->i_nlink = yaffs_GetObjectLinkCount(obj);
2521 + d_instantiate(dentry, old_dentry->d_inode);
2522 + atomic_inc(&old_dentry->d_inode->i_count);
2524 + (KERN_DEBUG "yaffs_link link count %d i_count %d\n",
2525 + old_dentry->d_inode->i_nlink,
2526 + atomic_read(&old_dentry->d_inode->i_count)));
2530 + yaffs_GrossUnlock(dev);
2540 +static int yaffs_symlink(struct inode *dir, struct dentry *dentry,
2541 + const char *symname)
2543 + yaffs_Object *obj;
2544 + yaffs_Device *dev;
2545 + uid_t uid = current->fsuid;
2546 + gid_t gid = (dir->i_mode & S_ISGID) ? dir->i_gid : current->fsgid;
2548 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_symlink\n"));
2550 + dev = yaffs_InodeToObject(dir)->myDev;
2551 + yaffs_GrossLock(dev);
2552 + obj = yaffs_MknodSymLink(yaffs_InodeToObject(dir), dentry->d_name.name,
2553 + S_IFLNK | S_IRWXUGO, uid, gid, symname);
2554 + yaffs_GrossUnlock(dev);
2558 + struct inode *inode;
2560 + inode = yaffs_get_inode(dir->i_sb, obj->yst_mode, 0, obj);
2561 + d_instantiate(dentry, inode);
2562 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink created OK\n"));
2565 + T(YAFFS_TRACE_OS, (KERN_DEBUG "symlink not created\n"));
2572 +static int yaffs_sync_object(struct file *file, struct dentry *dentry,
2576 + yaffs_Object *obj;
2577 + yaffs_Device *dev;
2579 + obj = yaffs_DentryToObject(dentry);
2583 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_object\n"));
2584 + yaffs_GrossLock(dev);
2585 + yaffs_FlushFile(obj, 1);
2586 + yaffs_GrossUnlock(dev);
2591 + * The VFS layer already does all the dentry stuff for rename.
2593 + * NB: POSIX says you can rename an object over an old object of the same name
2595 +static int yaffs_rename(struct inode *old_dir, struct dentry *old_dentry,
2596 + struct inode *new_dir, struct dentry *new_dentry)
2598 + yaffs_Device *dev;
2599 + int retVal = YAFFS_FAIL;
2600 + yaffs_Object *target;
2602 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_rename\n"));
2603 + dev = yaffs_InodeToObject(old_dir)->myDev;
2605 + yaffs_GrossLock(dev);
2607 + /* Check if the target is an existing directory that is not empty. */
2609 + yaffs_FindObjectByName(yaffs_InodeToObject(new_dir),
2610 + new_dentry->d_name.name);
2615 + target->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
2616 + !list_empty(&target->variant.directoryVariant.children)) {
2618 + T(YAFFS_TRACE_OS, (KERN_DEBUG "target is non-empty dir\n"));
2620 + retVal = YAFFS_FAIL;
2623 + /* Now does unlinking internally using shadowing mechanism */
2624 + T(YAFFS_TRACE_OS, (KERN_DEBUG "calling yaffs_RenameObject\n"));
2627 + yaffs_RenameObject(yaffs_InodeToObject(old_dir),
2628 + old_dentry->d_name.name,
2629 + yaffs_InodeToObject(new_dir),
2630 + new_dentry->d_name.name);
2633 + yaffs_GrossUnlock(dev);
2635 + if (retVal == YAFFS_OK) {
2637 + new_dentry->d_inode->i_nlink--;
2638 + mark_inode_dirty(new_dentry->d_inode);
2643 + return -ENOTEMPTY;
2648 +static int yaffs_setattr(struct dentry *dentry, struct iattr *attr)
2650 + struct inode *inode = dentry->d_inode;
2652 + yaffs_Device *dev;
2655 + (KERN_DEBUG "yaffs_setattr of object %d\n",
2656 + yaffs_InodeToObject(inode)->objectId));
2658 + if ((error = inode_change_ok(inode, attr)) == 0) {
2660 + dev = yaffs_InodeToObject(inode)->myDev;
2661 + yaffs_GrossLock(dev);
2662 + if (yaffs_SetAttributes(yaffs_InodeToObject(inode), attr) ==
2668 + yaffs_GrossUnlock(dev);
2670 + error = inode_setattr(inode, attr);
2675 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2676 +static int yaffs_statfs(struct dentry *dentry, struct kstatfs *buf)
2678 + yaffs_Device *dev = yaffs_DentryToObject(dentry)->myDev;
2679 + struct super_block *sb = dentry->d_sb;
2680 +#elif (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
2681 +static int yaffs_statfs(struct super_block *sb, struct kstatfs *buf)
2683 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2685 +static int yaffs_statfs(struct super_block *sb, struct statfs *buf)
2687 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2690 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_statfs\n"));
2692 + yaffs_GrossLock(dev);
2694 + buf->f_type = YAFFS_MAGIC;
2695 + buf->f_bsize = sb->s_blocksize;
2696 + buf->f_namelen = 255;
2697 + if (sb->s_blocksize > dev->nDataBytesPerChunk) {
2700 + (dev->endBlock - dev->startBlock +
2701 + 1) * dev->nChunksPerBlock / (sb->s_blocksize /
2702 + dev->nDataBytesPerChunk);
2704 + yaffs_GetNumberOfFreeChunks(dev) / (sb->s_blocksize /
2705 + dev->nDataBytesPerChunk);
2709 + (dev->endBlock - dev->startBlock +
2710 + 1) * dev->nChunksPerBlock * (dev->nDataBytesPerChunk /
2713 + yaffs_GetNumberOfFreeChunks(dev) * (dev->nDataBytesPerChunk /
2718 + buf->f_bavail = buf->f_bfree;
2720 + yaffs_GrossUnlock(dev);
2726 +static int yaffs_do_sync_fs(struct super_block *sb)
2729 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2730 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_do_sync_fs\n"));
2733 + yaffs_GrossLock(dev);
2736 + yaffs_CheckpointSave(dev);
2738 + yaffs_GrossUnlock(dev);
2746 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2747 +static void yaffs_write_super(struct super_block *sb)
2749 +static int yaffs_write_super(struct super_block *sb)
2753 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_write_super\n"));
2754 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
2755 + return 0; /* yaffs_do_sync_fs(sb);*/
2760 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
2761 +static int yaffs_sync_fs(struct super_block *sb, int wait)
2763 +static int yaffs_sync_fs(struct super_block *sb)
2767 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_sync_fs\n"));
2769 + return 0; /* yaffs_do_sync_fs(sb);*/
2774 +static void yaffs_read_inode(struct inode *inode)
2776 + /* NB This is called as a side effect of other functions, but
2777 + * we had to release the lock to prevent deadlocks, so
2778 + * need to lock again.
2781 + yaffs_Object *obj;
2782 + yaffs_Device *dev = yaffs_SuperToDevice(inode->i_sb);
2785 + (KERN_DEBUG "yaffs_read_inode for %d\n", (int)inode->i_ino));
2787 + yaffs_GrossLock(dev);
2789 + obj = yaffs_FindObjectByNumber(dev, inode->i_ino);
2791 + yaffs_FillInodeFromObject(inode, obj);
2793 + yaffs_GrossUnlock(dev);
2796 +static LIST_HEAD(yaffs_dev_list);
2798 +static int yaffs_remount_fs(struct super_block *sb, int *flags, char *data)
2800 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2802 + if( *flags & MS_RDONLY ) {
2803 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2806 + (KERN_DEBUG "yaffs_remount_fs: %s: RO\n", dev->name ));
2808 + yaffs_GrossLock(dev);
2810 + yaffs_FlushEntireDeviceCache(dev);
2812 + yaffs_CheckpointSave(dev);
2817 + yaffs_GrossUnlock(dev);
2821 + (KERN_DEBUG "yaffs_remount_fs: %s: RW\n", dev->name ));
2827 +static void yaffs_put_super(struct super_block *sb)
2829 + yaffs_Device *dev = yaffs_SuperToDevice(sb);
2831 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_put_super\n"));
2833 + yaffs_GrossLock(dev);
2835 + yaffs_FlushEntireDeviceCache(dev);
2837 + yaffs_CheckpointSave(dev);
2839 + if (dev->putSuperFunc) {
2840 + dev->putSuperFunc(sb);
2843 + yaffs_Deinitialise(dev);
2845 + yaffs_GrossUnlock(dev);
2847 + /* we assume this is protected by lock_kernel() in mount/umount */
2848 + list_del(&dev->devList);
2850 + if(dev->spareBuffer){
2851 + YFREE(dev->spareBuffer);
2852 + dev->spareBuffer = NULL;
2859 +static void yaffs_MTDPutSuper(struct super_block *sb)
2862 + struct mtd_info *mtd = yaffs_SuperToDevice(sb)->genericDevice;
2868 + put_mtd_device(mtd);
2872 +static void yaffs_MarkSuperBlockDirty(void *vsb)
2874 + struct super_block *sb = (struct super_block *)vsb;
2876 + T(YAFFS_TRACE_OS, (KERN_DEBUG "yaffs_MarkSuperBlockDirty() sb = %p\n",sb));
2883 + int skip_checkpoint_read;
2884 + int skip_checkpoint_write;
2888 +#define MAX_OPT_LEN 20
2889 +static int yaffs_parse_options(yaffs_options *options, const char *options_str)
2891 + char cur_opt[MAX_OPT_LEN+1];
2895 + /* Parse through the options which is a comma seperated list */
2897 + while(options_str && *options_str && !error){
2898 + memset(cur_opt,0,MAX_OPT_LEN+1);
2901 + while(*options_str && *options_str != ','){
2902 + if(p < MAX_OPT_LEN){
2903 + cur_opt[p] = *options_str;
2909 + if(!strcmp(cur_opt,"inband-tags"))
2910 + options->inband_tags = 1;
2911 + else if(!strcmp(cur_opt,"no-cache"))
2912 + options->no_cache = 1;
2913 + else if(!strcmp(cur_opt,"no-checkpoint-read"))
2914 + options->skip_checkpoint_read = 1;
2915 + else if(!strcmp(cur_opt,"no-checkpoint-write"))
2916 + options->skip_checkpoint_write = 1;
2917 + else if(!strcmp(cur_opt,"no-checkpoint")){
2918 + options->skip_checkpoint_read = 1;
2919 + options->skip_checkpoint_write = 1;
2921 + printk(KERN_INFO "yaffs: Bad mount option \"%s\"\n",cur_opt);
2930 +static struct super_block *yaffs_internal_read_super(int yaffsVersion,
2931 + struct super_block *sb,
2932 + void *data, int silent)
2935 + struct inode *inode = NULL;
2936 + struct dentry *root;
2937 + yaffs_Device *dev = 0;
2938 + char devname_buf[BDEVNAME_SIZE + 1];
2939 + struct mtd_info *mtd;
2941 + char *data_str = (char *)data;
2943 + yaffs_options options;
2945 + sb->s_magic = YAFFS_MAGIC;
2946 + sb->s_op = &yaffs_super_ops;
2949 + printk(KERN_INFO "yaffs: sb is NULL\n");
2950 + else if (!sb->s_dev)
2951 + printk(KERN_INFO "yaffs: sb->s_dev is NULL\n");
2952 + else if (!yaffs_devname(sb, devname_buf))
2953 + printk(KERN_INFO "yaffs: devname is NULL\n");
2955 + printk(KERN_INFO "yaffs: dev is %d name is \"%s\"\n",
2957 + yaffs_devname(sb, devname_buf));
2962 + printk(KERN_INFO "yaffs: passed flags \"%s\"\n",data_str);
2964 + memset(&options,0,sizeof(options));
2966 + if(yaffs_parse_options(&options,data_str)){
2967 + /* Option parsing failed */
2972 + sb->s_blocksize = PAGE_CACHE_SIZE;
2973 + sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
2974 + T(YAFFS_TRACE_OS, ("yaffs_read_super: Using yaffs%d\n", yaffsVersion));
2976 + ("yaffs_read_super: block size %d\n", (int)(sb->s_blocksize)));
2978 +#ifdef CONFIG_YAFFS_DISABLE_WRITE_VERIFY
2980 + ("yaffs: Write verification disabled. All guarantees "
2981 + "null and void\n"));
2984 + T(YAFFS_TRACE_ALWAYS, ("yaffs: Attempting MTD mount on %u.%u, "
2986 + MAJOR(sb->s_dev), MINOR(sb->s_dev),
2987 + yaffs_devname(sb, devname_buf)));
2989 + /* Check it's an mtd device..... */
2990 + if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
2991 + return NULL; /* This isn't an mtd device */
2993 + /* Get the device */
2994 + mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
2996 + T(YAFFS_TRACE_ALWAYS,
2997 + ("yaffs: MTD device #%u doesn't appear to exist\n",
2998 + MINOR(sb->s_dev)));
3001 + /* Check it's NAND */
3002 + if (mtd->type != MTD_NANDFLASH) {
3003 + T(YAFFS_TRACE_ALWAYS,
3004 + ("yaffs: MTD device is not NAND it's type %d\n", mtd->type));
3008 + T(YAFFS_TRACE_OS, (" erase %p\n", mtd->erase));
3009 + T(YAFFS_TRACE_OS, (" read %p\n", mtd->read));
3010 + T(YAFFS_TRACE_OS, (" write %p\n", mtd->write));
3011 + T(YAFFS_TRACE_OS, (" readoob %p\n", mtd->read_oob));
3012 + T(YAFFS_TRACE_OS, (" writeoob %p\n", mtd->write_oob));
3013 + T(YAFFS_TRACE_OS, (" block_isbad %p\n", mtd->block_isbad));
3014 + T(YAFFS_TRACE_OS, (" block_markbad %p\n", mtd->block_markbad));
3015 + T(YAFFS_TRACE_OS, (" %s %d\n", WRITE_SIZE_STR, WRITE_SIZE(mtd)));
3016 + T(YAFFS_TRACE_OS, (" oobsize %d\n", mtd->oobsize));
3017 + T(YAFFS_TRACE_OS, (" erasesize %d\n", mtd->erasesize));
3018 + T(YAFFS_TRACE_OS, (" size %d\n", mtd->size));
3020 +#ifdef CONFIG_YAFFS_AUTO_YAFFS2
3022 + if (yaffsVersion == 1 &&
3023 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3024 + mtd->writesize >= 2048) {
3026 + mtd->oobblock >= 2048) {
3028 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs2\n"));
3032 + /* Added NCB 26/5/2006 for completeness */
3033 + if (yaffsVersion == 2 &&
3034 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3035 + mtd->writesize == 512) {
3037 + mtd->oobblock == 512) {
3039 + T(YAFFS_TRACE_ALWAYS,("yaffs: auto selecting yaffs1\n"));
3045 + if (yaffsVersion == 2) {
3046 + /* Check for version 2 style functions */
3047 + if (!mtd->erase ||
3048 + !mtd->block_isbad ||
3049 + !mtd->block_markbad ||
3052 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3053 + !mtd->read_oob || !mtd->write_oob) {
3055 + !mtd->write_ecc ||
3056 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3058 + T(YAFFS_TRACE_ALWAYS,
3059 + ("yaffs: MTD device does not support required "
3064 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3065 + if (mtd->writesize < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3067 + if (mtd->oobblock < YAFFS_MIN_YAFFS2_CHUNK_SIZE ||
3069 + mtd->oobsize < YAFFS_MIN_YAFFS2_SPARE_SIZE) {
3070 + T(YAFFS_TRACE_ALWAYS,
3071 + ("yaffs: MTD device does not have the "
3072 + "right page sizes\n"));
3076 + /* Check for V1 style functions */
3077 + if (!mtd->erase ||
3080 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3081 + !mtd->read_oob || !mtd->write_oob) {
3083 + !mtd->write_ecc ||
3084 + !mtd->read_ecc || !mtd->read_oob || !mtd->write_oob) {
3086 + T(YAFFS_TRACE_ALWAYS,
3087 + ("yaffs: MTD device does not support required "
3092 + if (WRITE_SIZE(mtd) < YAFFS_BYTES_PER_CHUNK ||
3093 + mtd->oobsize != YAFFS_BYTES_PER_SPARE) {
3094 + T(YAFFS_TRACE_ALWAYS,
3095 + ("yaffs: MTD device does not support have the "
3096 + "right page sizes\n"));
3101 + /* OK, so if we got here, we have an MTD that's NAND and looks
3102 + * like it has the right capabilities
3103 + * Set the yaffs_Device up for mtd
3106 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3107 + sb->s_fs_info = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3109 + sb->u.generic_sbp = dev = kmalloc(sizeof(yaffs_Device), GFP_KERNEL);
3112 + /* Deep shit could not allocate device structure */
3113 + T(YAFFS_TRACE_ALWAYS,
3114 + ("yaffs_read_super: Failed trying to allocate "
3115 + "yaffs_Device. \n"));
3119 + memset(dev, 0, sizeof(yaffs_Device));
3120 + dev->genericDevice = mtd;
3121 + dev->name = mtd->name;
3123 + /* Set up the memory size parameters.... */
3125 + nBlocks = mtd->size / (YAFFS_CHUNKS_PER_BLOCK * YAFFS_BYTES_PER_CHUNK);
3126 + dev->startBlock = 0;
3127 + dev->endBlock = nBlocks - 1;
3128 + dev->nChunksPerBlock = YAFFS_CHUNKS_PER_BLOCK;
3129 + dev->nDataBytesPerChunk = YAFFS_BYTES_PER_CHUNK;
3130 + dev->nReservedBlocks = 5;
3131 + dev->nShortOpCaches = (options.no_cache) ? 0 : 10;
3133 + /* ... and the functions. */
3134 + if (yaffsVersion == 2) {
3135 + dev->writeChunkWithTagsToNAND =
3136 + nandmtd2_WriteChunkWithTagsToNAND;
3137 + dev->readChunkWithTagsFromNAND =
3138 + nandmtd2_ReadChunkWithTagsFromNAND;
3139 + dev->markNANDBlockBad = nandmtd2_MarkNANDBlockBad;
3140 + dev->queryNANDBlock = nandmtd2_QueryNANDBlock;
3141 + dev->spareBuffer = YMALLOC(mtd->oobsize);
3142 + dev->isYaffs2 = 1;
3143 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3144 + dev->nDataBytesPerChunk = mtd->writesize;
3145 + dev->nChunksPerBlock = mtd->erasesize / mtd->writesize;
3147 + dev->nDataBytesPerChunk = mtd->oobblock;
3148 + dev->nChunksPerBlock = mtd->erasesize / mtd->oobblock;
3150 + nBlocks = mtd->size / mtd->erasesize;
3152 + dev->nCheckpointReservedBlocks = CONFIG_YAFFS_CHECKPOINT_RESERVED_BLOCKS;
3153 + dev->startBlock = 0;
3154 + dev->endBlock = nBlocks - 1;
3156 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3157 + /* use the MTD interface in yaffs_mtdif1.c */
3158 + dev->writeChunkWithTagsToNAND =
3159 + nandmtd1_WriteChunkWithTagsToNAND;
3160 + dev->readChunkWithTagsFromNAND =
3161 + nandmtd1_ReadChunkWithTagsFromNAND;
3162 + dev->markNANDBlockBad = nandmtd1_MarkNANDBlockBad;
3163 + dev->queryNANDBlock = nandmtd1_QueryNANDBlock;
3165 + dev->writeChunkToNAND = nandmtd_WriteChunkToNAND;
3166 + dev->readChunkFromNAND = nandmtd_ReadChunkFromNAND;
3168 + dev->isYaffs2 = 0;
3170 + /* ... and common functions */
3171 + dev->eraseBlockInNAND = nandmtd_EraseBlockInNAND;
3172 + dev->initialiseNAND = nandmtd_InitialiseNAND;
3174 + dev->putSuperFunc = yaffs_MTDPutSuper;
3176 + dev->superBlock = (void *)sb;
3177 + dev->markSuperBlockDirty = yaffs_MarkSuperBlockDirty;
3180 +#ifndef CONFIG_YAFFS_DOES_ECC
3181 + dev->useNANDECC = 1;
3184 +#ifdef CONFIG_YAFFS_DISABLE_WIDE_TNODES
3185 + dev->wideTnodesDisabled = 1;
3188 + dev->skipCheckpointRead = options.skip_checkpoint_read;
3189 + dev->skipCheckpointWrite = options.skip_checkpoint_write;
3191 + /* we assume this is protected by lock_kernel() in mount/umount */
3192 + list_add_tail(&dev->devList, &yaffs_dev_list);
3194 + init_MUTEX(&dev->grossLock);
3196 + yaffs_GrossLock(dev);
3198 + err = yaffs_GutsInitialise(dev);
3201 + ("yaffs_read_super: guts initialised %s\n",
3202 + (err == YAFFS_OK) ? "OK" : "FAILED"));
3204 + /* Release lock before yaffs_get_inode() */
3205 + yaffs_GrossUnlock(dev);
3207 + /* Create root inode */
3208 + if (err == YAFFS_OK)
3209 + inode = yaffs_get_inode(sb, S_IFDIR | 0755, 0,
3215 + inode->i_op = &yaffs_dir_inode_operations;
3216 + inode->i_fop = &yaffs_dir_operations;
3218 + T(YAFFS_TRACE_OS, ("yaffs_read_super: got root inode\n"));
3220 + root = d_alloc_root(inode);
3222 + T(YAFFS_TRACE_OS, ("yaffs_read_super: d_alloc_root done\n"));
3228 + sb->s_root = root;
3230 + T(YAFFS_TRACE_OS, ("yaffs_read_super: done\n"));
3235 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3236 +static int yaffs_internal_read_super_mtd(struct super_block *sb, void *data,
3239 + return yaffs_internal_read_super(1, sb, data, silent) ? 0 : -EINVAL;
3242 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3243 +static int yaffs_read_super(struct file_system_type *fs,
3244 + int flags, const char *dev_name,
3245 + void *data, struct vfsmount *mnt)
3248 + return get_sb_bdev(fs, flags, dev_name, data,
3249 + yaffs_internal_read_super_mtd, mnt);
3252 +static struct super_block *yaffs_read_super(struct file_system_type *fs,
3253 + int flags, const char *dev_name,
3257 + return get_sb_bdev(fs, flags, dev_name, data,
3258 + yaffs_internal_read_super_mtd);
3262 +static struct file_system_type yaffs_fs_type = {
3263 + .owner = THIS_MODULE,
3265 + .get_sb = yaffs_read_super,
3266 + .kill_sb = kill_block_super,
3267 + .fs_flags = FS_REQUIRES_DEV,
3270 +static struct super_block *yaffs_read_super(struct super_block *sb, void *data,
3273 + return yaffs_internal_read_super(1, sb, data, silent);
3276 +static DECLARE_FSTYPE(yaffs_fs_type, "yaffs", yaffs_read_super,
3281 +#ifdef CONFIG_YAFFS_YAFFS2
3283 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
3284 +static int yaffs2_internal_read_super_mtd(struct super_block *sb, void *data,
3287 + return yaffs_internal_read_super(2, sb, data, silent) ? 0 : -EINVAL;
3290 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
3291 +static int yaffs2_read_super(struct file_system_type *fs,
3292 + int flags, const char *dev_name, void *data,
3293 + struct vfsmount *mnt)
3295 + return get_sb_bdev(fs, flags, dev_name, data,
3296 + yaffs2_internal_read_super_mtd, mnt);
3299 +static struct super_block *yaffs2_read_super(struct file_system_type *fs,
3300 + int flags, const char *dev_name,
3304 + return get_sb_bdev(fs, flags, dev_name, data,
3305 + yaffs2_internal_read_super_mtd);
3309 +static struct file_system_type yaffs2_fs_type = {
3310 + .owner = THIS_MODULE,
3312 + .get_sb = yaffs2_read_super,
3313 + .kill_sb = kill_block_super,
3314 + .fs_flags = FS_REQUIRES_DEV,
3317 +static struct super_block *yaffs2_read_super(struct super_block *sb,
3318 + void *data, int silent)
3320 + return yaffs_internal_read_super(2, sb, data, silent);
3323 +static DECLARE_FSTYPE(yaffs2_fs_type, "yaffs2", yaffs2_read_super,
3327 +#endif /* CONFIG_YAFFS_YAFFS2 */
3329 +static struct proc_dir_entry *my_proc_entry;
3331 +static char *yaffs_dump_dev(char *buf, yaffs_Device * dev)
3333 + buf += sprintf(buf, "startBlock......... %d\n", dev->startBlock);
3334 + buf += sprintf(buf, "endBlock........... %d\n", dev->endBlock);
3335 + buf += sprintf(buf, "nDataBytesPerChunk. %d\n", dev->nDataBytesPerChunk);
3336 + buf += sprintf(buf, "chunkGroupBits..... %d\n", dev->chunkGroupBits);
3337 + buf += sprintf(buf, "chunkGroupSize..... %d\n", dev->chunkGroupSize);
3338 + buf += sprintf(buf, "nErasedBlocks...... %d\n", dev->nErasedBlocks);
3339 + buf += sprintf(buf, "nReservedBlocks.... %d\n", dev->nReservedBlocks);
3340 + buf += sprintf(buf, "nCheckptResBlocks.. %d\n", dev->nCheckpointReservedBlocks);
3341 + buf += sprintf(buf, "blocksInCheckpoint. %d\n", dev->blocksInCheckpoint);
3342 + buf += sprintf(buf, "nTnodesCreated..... %d\n", dev->nTnodesCreated);
3343 + buf += sprintf(buf, "nFreeTnodes........ %d\n", dev->nFreeTnodes);
3344 + buf += sprintf(buf, "nObjectsCreated.... %d\n", dev->nObjectsCreated);
3345 + buf += sprintf(buf, "nFreeObjects....... %d\n", dev->nFreeObjects);
3346 + buf += sprintf(buf, "nFreeChunks........ %d\n", dev->nFreeChunks);
3347 + buf += sprintf(buf, "nPageWrites........ %d\n", dev->nPageWrites);
3348 + buf += sprintf(buf, "nPageReads......... %d\n", dev->nPageReads);
3349 + buf += sprintf(buf, "nBlockErasures..... %d\n", dev->nBlockErasures);
3350 + buf += sprintf(buf, "nGCCopies.......... %d\n", dev->nGCCopies);
3352 + sprintf(buf, "garbageCollections. %d\n", dev->garbageCollections);
3354 + sprintf(buf, "passiveGCs......... %d\n",
3355 + dev->passiveGarbageCollections);
3356 + buf += sprintf(buf, "nRetriedWrites..... %d\n", dev->nRetriedWrites);
3357 + buf += sprintf(buf, "nShortOpCaches..... %d\n", dev->nShortOpCaches);
3358 + buf += sprintf(buf, "nRetireBlocks...... %d\n", dev->nRetiredBlocks);
3359 + buf += sprintf(buf, "eccFixed........... %d\n", dev->eccFixed);
3360 + buf += sprintf(buf, "eccUnfixed......... %d\n", dev->eccUnfixed);
3361 + buf += sprintf(buf, "tagsEccFixed....... %d\n", dev->tagsEccFixed);
3362 + buf += sprintf(buf, "tagsEccUnfixed..... %d\n", dev->tagsEccUnfixed);
3363 + buf += sprintf(buf, "cacheHits.......... %d\n", dev->cacheHits);
3364 + buf += sprintf(buf, "nDeletedFiles...... %d\n", dev->nDeletedFiles);
3365 + buf += sprintf(buf, "nUnlinkedFiles..... %d\n", dev->nUnlinkedFiles);
3367 + sprintf(buf, "nBackgroudDeletions %d\n", dev->nBackgroundDeletions);
3368 + buf += sprintf(buf, "useNANDECC......... %d\n", dev->useNANDECC);
3369 + buf += sprintf(buf, "isYaffs2........... %d\n", dev->isYaffs2);
3374 +static int yaffs_proc_read(char *page,
3376 + off_t offset, int count, int *eof, void *data)
3378 + struct list_head *item;
3380 + int step = offset;
3383 + /* Get proc_file_read() to step 'offset' by one on each sucessive call.
3384 + * We use 'offset' (*ppos) to indicate where we are in devList.
3385 + * This also assumes the user has posted a read buffer large
3386 + * enough to hold the complete output; but that's life in /proc.
3389 + *(int *)start = 1;
3391 + /* Print header first */
3393 + buf += sprintf(buf, "YAFFS built:" __DATE__ " " __TIME__
3394 + "\n%s\n%s\n", yaffs_fs_c_version,
3395 + yaffs_guts_c_version);
3398 + /* hold lock_kernel while traversing yaffs_dev_list */
3401 + /* Locate and print the Nth entry. Order N-squared but N is small. */
3402 + list_for_each(item, &yaffs_dev_list) {
3403 + yaffs_Device *dev = list_entry(item, yaffs_Device, devList);
3408 + buf += sprintf(buf, "\nDevice %d \"%s\"\n", n, dev->name);
3409 + buf = yaffs_dump_dev(buf, dev);
3414 + return buf - page < count ? buf - page : count;
3418 + * Set the verbosity of the warnings and error messages.
3420 + * Note that the names can only be a..z or _ with the current code.
3425 + unsigned mask_bitfield;
3427 + {"allocate", YAFFS_TRACE_ALLOCATE},
3428 + {"always", YAFFS_TRACE_ALWAYS},
3429 + {"bad_blocks", YAFFS_TRACE_BAD_BLOCKS},
3430 + {"buffers", YAFFS_TRACE_BUFFERS},
3431 + {"bug", YAFFS_TRACE_BUG},
3432 + {"checkpt", YAFFS_TRACE_CHECKPOINT},
3433 + {"deletion", YAFFS_TRACE_DELETION},
3434 + {"erase", YAFFS_TRACE_ERASE},
3435 + {"error", YAFFS_TRACE_ERROR},
3436 + {"gc_detail", YAFFS_TRACE_GC_DETAIL},
3437 + {"gc", YAFFS_TRACE_GC},
3438 + {"mtd", YAFFS_TRACE_MTD},
3439 + {"nandaccess", YAFFS_TRACE_NANDACCESS},
3440 + {"os", YAFFS_TRACE_OS},
3441 + {"scan_debug", YAFFS_TRACE_SCAN_DEBUG},
3442 + {"scan", YAFFS_TRACE_SCAN},
3443 + {"tracing", YAFFS_TRACE_TRACING},
3445 + {"verify", YAFFS_TRACE_VERIFY},
3446 + {"verify_nand", YAFFS_TRACE_VERIFY_NAND},
3447 + {"verify_full", YAFFS_TRACE_VERIFY_FULL},
3448 + {"verify_all", YAFFS_TRACE_VERIFY_ALL},
3450 + {"write", YAFFS_TRACE_WRITE},
3451 + {"all", 0xffffffff},
3456 +#define MAX_MASK_NAME_LENGTH 40
3457 +static int yaffs_proc_write(struct file *file, const char *buf,
3458 + unsigned long count, void *data)
3460 + unsigned rg = 0, mask_bitfield;
3464 + char substring[MAX_MASK_NAME_LENGTH+1];
3470 + rg = yaffs_traceMask;
3472 + while (!done && (pos < count)) {
3474 + while ((pos < count) && isspace(buf[pos])) {
3478 + switch (buf[pos]) {
3492 + mask_bitfield = simple_strtoul(buf + pos, &end, 0);
3493 + if (end > buf + pos) {
3494 + mask_name = "numeral";
3495 + len = end - (buf + pos);
3498 + for(x = buf + pos, i = 0;
3499 + (*x == '_' || (*x >='a' && *x <= 'z')) &&
3500 + i <MAX_MASK_NAME_LENGTH; x++, i++, pos++)
3501 + substring[i] = *x;
3502 + substring[i] = '\0';
3504 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3505 + //len = strlen(mask_flags[i].mask_name);
3506 + //if (strncmp(buf + pos, mask_flags[i].mask_name, len) == 0) {
3507 + if(strcmp(substring,mask_flags[i].mask_name) == 0){
3508 + mask_name = mask_flags[i].mask_name;
3509 + mask_bitfield = mask_flags[i].mask_bitfield;
3516 + if (mask_name != NULL) {
3521 + rg &= ~mask_bitfield;
3524 + rg |= mask_bitfield;
3527 + rg = mask_bitfield;
3530 + rg |= mask_bitfield;
3536 + yaffs_traceMask = rg | YAFFS_TRACE_ALWAYS;
3538 + printk("new trace = 0x%08X\n",yaffs_traceMask);
3540 + if (rg & YAFFS_TRACE_ALWAYS) {
3541 + for (i = 0; mask_flags[i].mask_name != NULL; i++) {
3543 + flag = ((rg & mask_flags[i].mask_bitfield) == mask_flags[i].mask_bitfield) ? '+' : '-';
3544 + printk("%c%s\n", flag, mask_flags[i].mask_name);
3551 +/* Stuff to handle installation of file systems */
3552 +struct file_system_to_install {
3553 + struct file_system_type *fst;
3557 +static struct file_system_to_install fs_to_install[] = {
3558 +//#ifdef CONFIG_YAFFS_YAFFS1
3559 + {&yaffs_fs_type, 0},
3561 +//#ifdef CONFIG_YAFFS_YAFFS2
3562 + {&yaffs2_fs_type, 0},
3567 +static int __init init_yaffs_fs(void)
3570 + struct file_system_to_install *fsinst;
3572 + T(YAFFS_TRACE_ALWAYS,
3573 + ("yaffs " __DATE__ " " __TIME__ " Installing. \n"));
3575 + /* Install the proc_fs entry */
3576 + my_proc_entry = create_proc_entry("yaffs",
3577 + S_IRUGO | S_IFREG,
3580 + if (my_proc_entry) {
3581 + my_proc_entry->write_proc = yaffs_proc_write;
3582 + my_proc_entry->read_proc = yaffs_proc_read;
3583 + my_proc_entry->data = NULL;
3588 + /* Now add the file system entries */
3590 + fsinst = fs_to_install;
3592 + while (fsinst->fst && !error) {
3593 + error = register_filesystem(fsinst->fst);
3595 + fsinst->installed = 1;
3600 + /* Any errors? uninstall */
3602 + fsinst = fs_to_install;
3604 + while (fsinst->fst) {
3605 + if (fsinst->installed) {
3606 + unregister_filesystem(fsinst->fst);
3607 + fsinst->installed = 0;
3616 +static void __exit exit_yaffs_fs(void)
3619 + struct file_system_to_install *fsinst;
3621 + T(YAFFS_TRACE_ALWAYS, ("yaffs " __DATE__ " " __TIME__
3622 + " removing. \n"));
3624 + remove_proc_entry("yaffs", &proc_root);
3626 + fsinst = fs_to_install;
3628 + while (fsinst->fst) {
3629 + if (fsinst->installed) {
3630 + unregister_filesystem(fsinst->fst);
3631 + fsinst->installed = 0;
3638 +module_init(init_yaffs_fs)
3639 +module_exit(exit_yaffs_fs)
3641 +MODULE_DESCRIPTION("YAFFS2 - a NAND specific flash file system");
3642 +MODULE_AUTHOR("Charles Manning, Aleph One Ltd., 2002-2006");
3643 +MODULE_LICENSE("GPL");
3644 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.c linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c
3645 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.c 1970-01-01 01:00:00.000000000 +0100
3646 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.c 2007-05-30 13:17:16.000000000 +0200
3649 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
3651 + * Copyright (C) 2002-2007 Aleph One Ltd.
3652 + * for Toby Churchill Ltd and Brightstar Engineering
3654 + * Created by Charles Manning <charles@aleph1.co.uk>
3656 + * This program is free software; you can redistribute it and/or modify
3657 + * it under the terms of the GNU General Public License version 2 as
3658 + * published by the Free Software Foundation.
3661 +const char *yaffs_guts_c_version =
3662 + "$Id: yaffs_guts.c,v 1.49 2007-05-15 20:07:40 charles Exp $";
3664 +#include "yportenv.h"
3666 +#include "yaffsinterface.h"
3667 +#include "yaffs_guts.h"
3668 +#include "yaffs_tagsvalidity.h"
3670 +#include "yaffs_tagscompat.h"
3671 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
3672 +#include "yaffs_qsort.h"
3674 +#include "yaffs_nand.h"
3676 +#include "yaffs_checkptrw.h"
3678 +#include "yaffs_nand.h"
3679 +#include "yaffs_packedtags2.h"
3682 +#ifdef CONFIG_YAFFS_WINCE
3683 +void yfsd_LockYAFFS(BOOL fsLockOnly);
3684 +void yfsd_UnlockYAFFS(BOOL fsLockOnly);
3687 +#define YAFFS_PASSIVE_GC_CHUNKS 2
3689 +#include "yaffs_ecc.h"
3692 +/* Robustification (if it ever comes about...) */
3693 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND);
3694 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk);
3695 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
3696 + const __u8 * data,
3697 + const yaffs_ExtendedTags * tags);
3698 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
3699 + const yaffs_ExtendedTags * tags);
3701 +/* Other local prototypes */
3702 +static int yaffs_UnlinkObject( yaffs_Object *obj);
3703 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj);
3705 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList);
3707 +static int yaffs_WriteNewChunkWithTagsToNAND(yaffs_Device * dev,
3708 + const __u8 * buffer,
3709 + yaffs_ExtendedTags * tags,
3711 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
3712 + int chunkInNAND, int inScan);
3714 +static yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
3715 + yaffs_ObjectType type);
3716 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
3717 + yaffs_Object * obj);
3718 +static int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name,
3719 + int force, int isShrink, int shadows);
3720 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj);
3721 +static int yaffs_CheckStructures(void);
3722 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
3723 + int chunkOffset, int *limit);
3724 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in);
3726 +static yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blockNo);
3728 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo);
3729 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3732 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
3735 +static int yaffs_UnlinkWorker(yaffs_Object * obj);
3736 +static void yaffs_DestroyObject(yaffs_Object * obj);
3738 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
3739 + int chunkInObject);
3741 +loff_t yaffs_GetFileSize(yaffs_Object * obj);
3743 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr);
3745 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev);
3747 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in);
3749 +#ifdef YAFFS_PARANOID
3750 +static int yaffs_CheckFileSanity(yaffs_Object * in);
3752 +#define yaffs_CheckFileSanity(in)
3755 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in);
3756 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId);
3758 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev);
3760 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
3761 + yaffs_ExtendedTags * tags);
3763 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos);
3764 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
3765 + yaffs_FileStructure * fStruct,
3769 +/* Function to calculate chunk and offset */
3771 +static void yaffs_AddrToChunk(yaffs_Device *dev, loff_t addr, __u32 *chunk, __u32 *offset)
3773 + if(dev->chunkShift){
3774 + /* Easy-peasy power of 2 case */
3775 + *chunk = (__u32)(addr >> dev->chunkShift);
3776 + *offset = (__u32)(addr & dev->chunkMask);
3778 + else if(dev->crumbsPerChunk)
3780 + /* Case where we're using "crumbs" */
3781 + *offset = (__u32)(addr & dev->crumbMask);
3782 + addr >>= dev->crumbShift;
3783 + *chunk = ((__u32)addr)/dev->crumbsPerChunk;
3784 + *offset += ((addr - (*chunk * dev->crumbsPerChunk)) << dev->crumbShift);
3790 +/* Function to return the number of shifts for a power of 2 greater than or equal
3791 + * to the given number
3792 + * Note we don't try to cater for all possible numbers and this does not have to
3793 + * be hellishly efficient.
3796 +static __u32 ShiftsGE(__u32 x)
3801 + nShifts = extraBits = 0;
3804 + if(x & 1) extraBits++;
3815 +/* Function to return the number of shifts to get a 1 in bit 0
3818 +static __u32 ShiftDiv(__u32 x)
3837 + * Temporary buffer manipulations.
3840 +static int yaffs_InitialiseTempBuffers(yaffs_Device *dev)
3843 + __u8 *buf = (__u8 *)1;
3845 + memset(dev->tempBuffer,0,sizeof(dev->tempBuffer));
3847 + for (i = 0; buf && i < YAFFS_N_TEMP_BUFFERS; i++) {
3848 + dev->tempBuffer[i].line = 0; /* not in use */
3849 + dev->tempBuffer[i].buffer = buf =
3850 + YMALLOC_DMA(dev->nDataBytesPerChunk);
3853 + return buf ? YAFFS_OK : YAFFS_FAIL;
3857 +static __u8 *yaffs_GetTempBuffer(yaffs_Device * dev, int lineNo)
3860 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3861 + if (dev->tempBuffer[i].line == 0) {
3862 + dev->tempBuffer[i].line = lineNo;
3863 + if ((i + 1) > dev->maxTemp) {
3864 + dev->maxTemp = i + 1;
3865 + for (j = 0; j <= i; j++)
3866 + dev->tempBuffer[j].maxLine =
3867 + dev->tempBuffer[j].line;
3870 + return dev->tempBuffer[i].buffer;
3874 + T(YAFFS_TRACE_BUFFERS,
3875 + (TSTR("Out of temp buffers at line %d, other held by lines:"),
3877 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3878 + T(YAFFS_TRACE_BUFFERS, (TSTR(" %d "), dev->tempBuffer[i].line));
3880 + T(YAFFS_TRACE_BUFFERS, (TSTR(" " TENDSTR)));
3883 + * If we got here then we have to allocate an unmanaged one
3884 + * This is not good.
3887 + dev->unmanagedTempAllocations++;
3888 + return YMALLOC(dev->nDataBytesPerChunk);
3892 +static void yaffs_ReleaseTempBuffer(yaffs_Device * dev, __u8 * buffer,
3896 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3897 + if (dev->tempBuffer[i].buffer == buffer) {
3898 + dev->tempBuffer[i].line = 0;
3904 + /* assume it is an unmanaged one. */
3905 + T(YAFFS_TRACE_BUFFERS,
3906 + (TSTR("Releasing unmanaged temp buffer in line %d" TENDSTR),
3909 + dev->unmanagedTempDeallocations++;
3915 + * Determine if we have a managed buffer.
3917 +int yaffs_IsManagedTempBuffer(yaffs_Device * dev, const __u8 * buffer)
3920 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
3921 + if (dev->tempBuffer[i].buffer == buffer)
3926 + for (i = 0; i < dev->nShortOpCaches; i++) {
3927 + if( dev->srCache[i].data == buffer )
3932 + if (buffer == dev->checkpointBuffer)
3935 + T(YAFFS_TRACE_ALWAYS,
3936 + (TSTR("yaffs: unmaged buffer detected.\n" TENDSTR)));
3943 + * Chunk bitmap manipulations
3946 +static Y_INLINE __u8 *yaffs_BlockBits(yaffs_Device * dev, int blk)
3948 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
3949 + T(YAFFS_TRACE_ERROR,
3950 + (TSTR("**>> yaffs: BlockBits block %d is not valid" TENDSTR),
3954 + return dev->chunkBits +
3955 + (dev->chunkBitmapStride * (blk - dev->internalStartBlock));
3958 +static Y_INLINE void yaffs_VerifyChunkBitId(yaffs_Device *dev, int blk, int chunk)
3960 + if(blk < dev->internalStartBlock || blk > dev->internalEndBlock ||
3961 + chunk < 0 || chunk >= dev->nChunksPerBlock) {
3962 + T(YAFFS_TRACE_ERROR,
3963 + (TSTR("**>> yaffs: Chunk Id (%d:%d) invalid"TENDSTR),blk,chunk));
3968 +static Y_INLINE void yaffs_ClearChunkBits(yaffs_Device * dev, int blk)
3970 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3972 + memset(blkBits, 0, dev->chunkBitmapStride);
3975 +static Y_INLINE void yaffs_ClearChunkBit(yaffs_Device * dev, int blk, int chunk)
3977 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3979 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3981 + blkBits[chunk / 8] &= ~(1 << (chunk & 7));
3984 +static Y_INLINE void yaffs_SetChunkBit(yaffs_Device * dev, int blk, int chunk)
3986 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3988 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3990 + blkBits[chunk / 8] |= (1 << (chunk & 7));
3993 +static Y_INLINE int yaffs_CheckChunkBit(yaffs_Device * dev, int blk, int chunk)
3995 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
3996 + yaffs_VerifyChunkBitId(dev,blk,chunk);
3998 + return (blkBits[chunk / 8] & (1 << (chunk & 7))) ? 1 : 0;
4001 +static Y_INLINE int yaffs_StillSomeChunkBits(yaffs_Device * dev, int blk)
4003 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4005 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4013 +static int yaffs_CountChunkBits(yaffs_Device * dev, int blk)
4015 + __u8 *blkBits = yaffs_BlockBits(dev, blk);
4018 + for (i = 0; i < dev->chunkBitmapStride; i++) {
4019 + __u8 x = *blkBits;
4032 + * Verification code
4035 +static int yaffs_SkipVerification(yaffs_Device *dev)
4037 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY | YAFFS_TRACE_VERIFY_FULL));
4040 +static int yaffs_SkipFullVerification(yaffs_Device *dev)
4042 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_FULL));
4045 +static int yaffs_SkipNANDVerification(yaffs_Device *dev)
4047 + return !(yaffs_traceMask & (YAFFS_TRACE_VERIFY_NAND));
4050 +static const char * blockStateName[] = {
4063 +static void yaffs_VerifyBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4068 + if(yaffs_SkipVerification(dev))
4071 + /* Report illegal runtime states */
4072 + if(bi->blockState <0 || bi->blockState >= YAFFS_NUMBER_OF_BLOCK_STATES)
4073 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has undefined state %d"TENDSTR),n,bi->blockState));
4075 + switch(bi->blockState){
4076 + case YAFFS_BLOCK_STATE_UNKNOWN:
4077 + case YAFFS_BLOCK_STATE_SCANNING:
4078 + case YAFFS_BLOCK_STATE_NEEDS_SCANNING:
4079 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has bad run-state %s"TENDSTR),
4080 + n,blockStateName[bi->blockState]));
4083 + /* Check pages in use and soft deletions are legal */
4085 + actuallyUsed = bi->pagesInUse - bi->softDeletions;
4087 + if(bi->pagesInUse < 0 || bi->pagesInUse > dev->nChunksPerBlock ||
4088 + bi->softDeletions < 0 || bi->softDeletions > dev->nChunksPerBlock ||
4089 + actuallyUsed < 0 || actuallyUsed > dev->nChunksPerBlock)
4090 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has illegal values pagesInUsed %d softDeletions %d"TENDSTR),
4091 + n,bi->pagesInUse,bi->softDeletions));
4094 + /* Check chunk bitmap legal */
4095 + inUse = yaffs_CountChunkBits(dev,n);
4096 + if(inUse != bi->pagesInUse)
4097 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has inconsistent values pagesInUse %d counted chunk bits %d"TENDSTR),
4098 + n,bi->pagesInUse,inUse));
4100 + /* Check that the sequence number is valid.
4101 + * Ten million is legal, but is very unlikely
4103 + if(dev->isYaffs2 &&
4104 + (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING || bi->blockState == YAFFS_BLOCK_STATE_FULL) &&
4105 + (bi->sequenceNumber < YAFFS_LOWEST_SEQUENCE_NUMBER || bi->sequenceNumber > 10000000 ))
4106 + T(YAFFS_TRACE_VERIFY,(TSTR("Block %d has suspect sequence number of %d"TENDSTR),
4107 + n,bi->sequenceNumber));
4111 +static void yaffs_VerifyCollectedBlock(yaffs_Device *dev,yaffs_BlockInfo *bi,int n)
4113 + yaffs_VerifyBlock(dev,bi,n);
4115 + /* After collection the block should be in the erased state */
4116 + /* TODO: This will need to change if we do partial gc */
4118 + if(bi->blockState != YAFFS_BLOCK_STATE_EMPTY){
4119 + T(YAFFS_TRACE_ERROR,(TSTR("Block %d is in state %d after gc, should be erased"TENDSTR),
4120 + n,bi->blockState));
4124 +static void yaffs_VerifyBlocks(yaffs_Device *dev)
4127 + int nBlocksPerState[YAFFS_NUMBER_OF_BLOCK_STATES];
4128 + int nIllegalBlockStates = 0;
4131 + if(yaffs_SkipVerification(dev))
4134 + memset(nBlocksPerState,0,sizeof(nBlocksPerState));
4137 + for(i = dev->internalStartBlock; i <= dev->internalEndBlock; i++){
4138 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev,i);
4139 + yaffs_VerifyBlock(dev,bi,i);
4141 + if(bi->blockState >=0 && bi->blockState < YAFFS_NUMBER_OF_BLOCK_STATES)
4142 + nBlocksPerState[bi->blockState]++;
4144 + nIllegalBlockStates++;
4148 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4149 + T(YAFFS_TRACE_VERIFY,(TSTR("Block summary"TENDSTR)));
4151 + T(YAFFS_TRACE_VERIFY,(TSTR("%d blocks have illegal states"TENDSTR),nIllegalBlockStates));
4152 + if(nBlocksPerState[YAFFS_BLOCK_STATE_ALLOCATING] > 1)
4153 + T(YAFFS_TRACE_VERIFY,(TSTR("Too many allocating blocks"TENDSTR)));
4155 + for(i = 0; i < YAFFS_NUMBER_OF_BLOCK_STATES; i++)
4156 + T(YAFFS_TRACE_VERIFY,
4157 + (TSTR("%s %d blocks"TENDSTR),
4158 + blockStateName[i],nBlocksPerState[i]));
4160 + if(dev->blocksInCheckpoint != nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT])
4161 + T(YAFFS_TRACE_VERIFY,
4162 + (TSTR("Checkpoint block count wrong dev %d count %d"TENDSTR),
4163 + dev->blocksInCheckpoint, nBlocksPerState[YAFFS_BLOCK_STATE_CHECKPOINT]));
4165 + if(dev->nErasedBlocks != nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY])
4166 + T(YAFFS_TRACE_VERIFY,
4167 + (TSTR("Erased block count wrong dev %d count %d"TENDSTR),
4168 + dev->nErasedBlocks, nBlocksPerState[YAFFS_BLOCK_STATE_EMPTY]));
4170 + if(nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING] > 1)
4171 + T(YAFFS_TRACE_VERIFY,
4172 + (TSTR("Too many collecting blocks %d (max is 1)"TENDSTR),
4173 + nBlocksPerState[YAFFS_BLOCK_STATE_COLLECTING]));
4175 + T(YAFFS_TRACE_VERIFY,(TSTR(""TENDSTR)));
4180 + * Verify the object header. oh must be valid, but obj and tags may be NULL in which
4181 + * case those tests will not be performed.
4183 +static void yaffs_VerifyObjectHeader(yaffs_Object *obj, yaffs_ObjectHeader *oh, yaffs_ExtendedTags *tags, int parentCheck)
4185 + if(yaffs_SkipVerification(obj->myDev))
4188 + if(!(tags && obj && oh)){
4189 + T(YAFFS_TRACE_VERIFY,
4190 + (TSTR("Verifying object header tags %x obj %x oh %x"TENDSTR),
4191 + (__u32)tags,(__u32)obj,(__u32)oh));
4195 + if(oh->type <= YAFFS_OBJECT_TYPE_UNKNOWN ||
4196 + oh->type > YAFFS_OBJECT_TYPE_MAX)
4197 + T(YAFFS_TRACE_VERIFY,
4198 + (TSTR("Obj %d header type is illegal value 0x%x"TENDSTR),
4199 + tags->objectId, oh->type));
4201 + if(tags->objectId != obj->objectId)
4202 + T(YAFFS_TRACE_VERIFY,
4203 + (TSTR("Obj %d header mismatch objectId %d"TENDSTR),
4204 + tags->objectId, obj->objectId));
4208 + * Check that the object's parent ids match if parentCheck requested.
4210 + * Tests do not apply to the root object.
4213 + if(parentCheck && tags->objectId > 1 && !obj->parent)
4214 + T(YAFFS_TRACE_VERIFY,
4215 + (TSTR("Obj %d header mismatch parentId %d obj->parent is NULL"TENDSTR),
4216 + tags->objectId, oh->parentObjectId));
4219 + if(parentCheck && obj->parent &&
4220 + oh->parentObjectId != obj->parent->objectId &&
4221 + (oh->parentObjectId != YAFFS_OBJECTID_UNLINKED ||
4222 + obj->parent->objectId != YAFFS_OBJECTID_DELETED))
4223 + T(YAFFS_TRACE_VERIFY,
4224 + (TSTR("Obj %d header mismatch parentId %d parentObjectId %d"TENDSTR),
4225 + tags->objectId, oh->parentObjectId, obj->parent->objectId));
4228 + if(tags->objectId > 1 && oh->name[0] == 0) /* Null name */
4229 + T(YAFFS_TRACE_VERIFY,
4230 + (TSTR("Obj %d header name is NULL"TENDSTR),
4233 + if(tags->objectId > 1 && ((__u8)(oh->name[0])) == 0xff) /* Trashed name */
4234 + T(YAFFS_TRACE_VERIFY,
4235 + (TSTR("Obj %d header name is 0xFF"TENDSTR),
4241 +static int yaffs_VerifyTnodeWorker(yaffs_Object * obj, yaffs_Tnode * tn,
4242 + __u32 level, int chunkOffset)
4245 + yaffs_Device *dev = obj->myDev;
4247 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4252 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
4253 + if (tn->internal[i]) {
4254 + ok = yaffs_VerifyTnodeWorker(obj,
4257 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
4260 + } else if (level == 0) {
4262 + yaffs_ExtendedTags tags;
4263 + __u32 objectId = obj->objectId;
4265 + chunkOffset <<= YAFFS_TNODES_LEVEL0_BITS;
4267 + for(i = 0; i < YAFFS_NTNODES_LEVEL0; i++){
4268 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4271 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),tags.objectId,tags.chunkId,theChunk)); */
4272 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4273 + if(tags.objectId != objectId || tags.chunkId != chunkOffset){
4274 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4275 + objectId, chunkOffset, theChunk,
4276 + tags.objectId, tags.chunkId));
4289 +static void yaffs_VerifyFile(yaffs_Object *obj)
4291 + int requiredTallness;
4292 + int actualTallness;
4297 + yaffs_Device *dev;
4298 + yaffs_ExtendedTags tags;
4302 + if(obj && yaffs_SkipVerification(obj->myDev))
4306 + objectId = obj->objectId;
4308 + /* Check file size is consistent with tnode depth */
4309 + lastChunk = obj->variant.fileVariant.fileSize / dev->nDataBytesPerChunk + 1;
4310 + x = lastChunk >> YAFFS_TNODES_LEVEL0_BITS;
4311 + requiredTallness = 0;
4313 + x >>= YAFFS_TNODES_INTERNAL_BITS;
4314 + requiredTallness++;
4317 + actualTallness = obj->variant.fileVariant.topLevel;
4319 + if(requiredTallness > actualTallness )
4320 + T(YAFFS_TRACE_VERIFY,
4321 + (TSTR("Obj %d had tnode tallness %d, needs to be %d"TENDSTR),
4322 + obj->objectId,actualTallness, requiredTallness));
4325 + /* Check that the chunks in the tnode tree are all correct.
4326 + * We do this by scanning through the tnode tree and
4327 + * checking the tags for every chunk match.
4330 + if(yaffs_SkipNANDVerification(dev))
4333 + for(i = 1; i <= lastChunk; i++){
4334 + tn = yaffs_FindLevel0Tnode(dev, &obj->variant.fileVariant,i);
4337 + __u32 theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
4339 + /* T(~0,(TSTR("verifying (%d:%d) %d"TENDSTR),objectId,i,theChunk)); */
4340 + yaffs_ReadChunkWithTagsFromNAND(dev,theChunk,NULL, &tags);
4341 + if(tags.objectId != objectId || tags.chunkId != i){
4342 + T(~0,(TSTR("Object %d chunkId %d NAND mismatch chunk %d tags (%d:%d)"TENDSTR),
4343 + objectId, i, theChunk,
4344 + tags.objectId, tags.chunkId));
4353 +static void yaffs_VerifyDirectory(yaffs_Object *obj)
4355 + if(obj && yaffs_SkipVerification(obj->myDev))
4360 +static void yaffs_VerifyHardLink(yaffs_Object *obj)
4362 + if(obj && yaffs_SkipVerification(obj->myDev))
4365 + /* Verify sane equivalent object */
4368 +static void yaffs_VerifySymlink(yaffs_Object *obj)
4370 + if(obj && yaffs_SkipVerification(obj->myDev))
4373 + /* Verify symlink string */
4376 +static void yaffs_VerifySpecial(yaffs_Object *obj)
4378 + if(obj && yaffs_SkipVerification(obj->myDev))
4382 +static void yaffs_VerifyObject(yaffs_Object *obj)
4384 + yaffs_Device *dev;
4390 + __u32 chunkIsLive;
4397 + if(yaffs_SkipVerification(dev))
4400 + /* Check sane object header chunk */
4402 + chunkMin = dev->internalStartBlock * dev->nChunksPerBlock;
4403 + chunkMax = (dev->internalEndBlock+1) * dev->nChunksPerBlock - 1;
4405 + chunkIdOk = (obj->chunkId >= chunkMin && obj->chunkId <= chunkMax);
4406 + chunkIsLive = chunkIdOk &&
4407 + yaffs_CheckChunkBit(dev,
4408 + obj->chunkId / dev->nChunksPerBlock,
4409 + obj->chunkId % dev->nChunksPerBlock);
4411 + (!chunkIdOk || !chunkIsLive)) {
4412 + T(YAFFS_TRACE_VERIFY,
4413 + (TSTR("Obj %d has chunkId %d %s %s"TENDSTR),
4414 + obj->objectId,obj->chunkId,
4415 + chunkIdOk ? "" : ",out of range",
4416 + chunkIsLive || !chunkIdOk ? "" : ",marked as deleted"));
4419 + if(chunkIdOk && chunkIsLive &&!yaffs_SkipNANDVerification(dev)) {
4420 + yaffs_ExtendedTags tags;
4421 + yaffs_ObjectHeader *oh;
4422 + __u8 *buffer = yaffs_GetTempBuffer(dev,__LINE__);
4424 + oh = (yaffs_ObjectHeader *)buffer;
4426 + yaffs_ReadChunkWithTagsFromNAND(dev, obj->chunkId,buffer, &tags);
4428 + yaffs_VerifyObjectHeader(obj,oh,&tags,1);
4430 + yaffs_ReleaseTempBuffer(dev,buffer,__LINE__);
4433 + /* Verify it has a parent */
4434 + if(obj && !obj->fake &&
4435 + (!obj->parent || obj->parent->myDev != dev)){
4436 + T(YAFFS_TRACE_VERIFY,
4437 + (TSTR("Obj %d has parent pointer %p which does not look like an object"TENDSTR),
4438 + obj->objectId,obj->parent));
4441 + /* Verify parent is a directory */
4442 + if(obj->parent && obj->parent->variantType != YAFFS_OBJECT_TYPE_DIRECTORY){
4443 + T(YAFFS_TRACE_VERIFY,
4444 + (TSTR("Obj %d's parent is not a directory (type %d)"TENDSTR),
4445 + obj->objectId,obj->parent->variantType));
4448 + switch(obj->variantType){
4449 + case YAFFS_OBJECT_TYPE_FILE:
4450 + yaffs_VerifyFile(obj);
4452 + case YAFFS_OBJECT_TYPE_SYMLINK:
4453 + yaffs_VerifySymlink(obj);
4455 + case YAFFS_OBJECT_TYPE_DIRECTORY:
4456 + yaffs_VerifyDirectory(obj);
4458 + case YAFFS_OBJECT_TYPE_HARDLINK:
4459 + yaffs_VerifyHardLink(obj);
4461 + case YAFFS_OBJECT_TYPE_SPECIAL:
4462 + yaffs_VerifySpecial(obj);
4464 + case YAFFS_OBJECT_TYPE_UNKNOWN:
4466 + T(YAFFS_TRACE_VERIFY,
4467 + (TSTR("Obj %d has illegaltype %d"TENDSTR),
4468 + obj->objectId,obj->variantType));
4475 +static void yaffs_VerifyObjects(yaffs_Device *dev)
4477 + yaffs_Object *obj;
4479 + struct list_head *lh;
4481 + if(yaffs_SkipVerification(dev))
4484 + /* Iterate through the objects in each hash entry */
4486 + for(i = 0; i < YAFFS_NOBJECT_BUCKETS; i++){
4487 + list_for_each(lh, &dev->objectBucket[i].list) {
4489 + obj = list_entry(lh, yaffs_Object, hashLink);
4490 + yaffs_VerifyObject(obj);
4499 + * Simple hash function. Needs to have a reasonable spread
4502 +static Y_INLINE int yaffs_HashFunction(int n)
4505 + return (n % YAFFS_NOBJECT_BUCKETS);
4509 + * Access functions to useful fake objects
4512 +yaffs_Object *yaffs_Root(yaffs_Device * dev)
4514 + return dev->rootDir;
4517 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev)
4519 + return dev->lostNFoundDir;
4524 + * Erased NAND checking functions
4527 +int yaffs_CheckFF(__u8 * buffer, int nBytes)
4529 + /* Horrible, slow implementation */
4530 + while (nBytes--) {
4531 + if (*buffer != 0xFF)
4538 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
4542 + int retval = YAFFS_OK;
4543 + __u8 *data = yaffs_GetTempBuffer(dev, __LINE__);
4544 + yaffs_ExtendedTags tags;
4547 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunkInNAND, data, &tags);
4549 + if(tags.eccResult > YAFFS_ECC_RESULT_NO_ERROR)
4550 + retval = YAFFS_FAIL;
4553 + if (!yaffs_CheckFF(data, dev->nDataBytesPerChunk) || tags.chunkUsed) {
4554 + T(YAFFS_TRACE_NANDACCESS,
4555 + (TSTR("Chunk %d not erased" TENDSTR), chunkInNAND));
4556 + retval = YAFFS_FAIL;
4559 + yaffs_ReleaseTempBuffer(dev, data, __LINE__);
4566 +static int yaffs_WriteNewChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
4567 + const __u8 * data,
4568 + yaffs_ExtendedTags * tags,
4575 + yaffs_InvalidateCheckpoint(dev);
4578 + yaffs_BlockInfo *bi = 0;
4581 + chunk = yaffs_AllocateChunk(dev, useReserve, &bi);
4587 + /* First check this chunk is erased, if it needs
4588 + * checking. The checking policy (unless forced
4589 + * always on) is as follows:
4591 + * Check the first page we try to write in a block.
4592 + * If the check passes then we don't need to check any
4593 + * more. If the check fails, we check again...
4594 + * If the block has been erased, we don't need to check.
4596 + * However, if the block has been prioritised for gc,
4597 + * then we think there might be something odd about
4598 + * this block and stop using it.
4600 + * Rationale: We should only ever see chunks that have
4601 + * not been erased if there was a partially written
4602 + * chunk due to power loss. This checking policy should
4603 + * catch that case with very few checks and thus save a
4604 + * lot of checks that are most likely not needed.
4606 + if (bi->gcPrioritise) {
4607 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
4608 + /* try another chunk */
4612 + /* let's give it a try */
4615 +#ifdef CONFIG_YAFFS_ALWAYS_CHECK_CHUNK_ERASED
4616 + bi->skipErasedCheck = 0;
4618 + if (!bi->skipErasedCheck) {
4619 + erasedOk = yaffs_CheckChunkErased(dev, chunk);
4620 + if (erasedOk != YAFFS_OK) {
4621 + T(YAFFS_TRACE_ERROR,
4622 + (TSTR ("**>> yaffs chunk %d was not erased"
4623 + TENDSTR), chunk));
4625 + /* try another chunk */
4628 + bi->skipErasedCheck = 1;
4631 + writeOk = yaffs_WriteChunkWithTagsToNAND(dev, chunk,
4633 + if (writeOk != YAFFS_OK) {
4634 + yaffs_HandleWriteChunkError(dev, chunk, erasedOk);
4635 + /* try another chunk */
4639 + /* Copy the data into the robustification buffer */
4640 + yaffs_HandleWriteChunkOk(dev, chunk, data, tags);
4642 + } while (writeOk != YAFFS_OK && attempts < yaffs_wr_attempts);
4644 + if (attempts > 1) {
4645 + T(YAFFS_TRACE_ERROR,
4646 + (TSTR("**>> yaffs write required %d attempts" TENDSTR),
4649 + dev->nRetriedWrites += (attempts - 1);
4656 + * Block retiring for handling a broken block.
4659 +static void yaffs_RetireBlock(yaffs_Device * dev, int blockInNAND)
4661 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4663 + yaffs_InvalidateCheckpoint(dev);
4665 + yaffs_MarkBlockBad(dev, blockInNAND);
4667 + bi->blockState = YAFFS_BLOCK_STATE_DEAD;
4668 + bi->gcPrioritise = 0;
4669 + bi->needsRetiring = 0;
4671 + dev->nRetiredBlocks++;
4675 + * Functions for robustisizing TODO
4679 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
4680 + const __u8 * data,
4681 + const yaffs_ExtendedTags * tags)
4685 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
4686 + const yaffs_ExtendedTags * tags)
4690 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi)
4692 + if(!bi->gcPrioritise){
4693 + bi->gcPrioritise = 1;
4694 + dev->hasPendingPrioritisedGCs = 1;
4695 + bi->chunkErrorStrikes ++;
4697 + if(bi->chunkErrorStrikes > 3){
4698 + bi->needsRetiring = 1; /* Too many stikes, so retire this */
4699 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Block struck out" TENDSTR)));
4706 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND, int erasedOk)
4709 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
4710 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockInNAND);
4712 + yaffs_HandleChunkError(dev,bi);
4716 + /* Was an actual write failure, so mark the block for retirement */
4717 + bi->needsRetiring = 1;
4718 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
4719 + (TSTR("**>> Block %d needs retiring" TENDSTR), blockInNAND));
4724 + /* Delete the chunk */
4725 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
4729 +/*---------------- Name handling functions ------------*/
4731 +static __u16 yaffs_CalcNameSum(const YCHAR * name)
4736 + YUCHAR *bname = (YUCHAR *) name;
4738 + while ((*bname) && (i < (YAFFS_MAX_NAME_LENGTH/2))) {
4740 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
4741 + sum += yaffs_toupper(*bname) * i;
4743 + sum += (*bname) * i;
4752 +static void yaffs_SetObjectName(yaffs_Object * obj, const YCHAR * name)
4754 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
4755 + if (name && yaffs_strlen(name) <= YAFFS_SHORT_NAME_LENGTH) {
4756 + yaffs_strcpy(obj->shortName, name);
4758 + obj->shortName[0] = _Y('\0');
4761 + obj->sum = yaffs_CalcNameSum(name);
4764 +/*-------------------- TNODES -------------------
4766 + * List of spare tnodes
4767 + * The list is hooked together using the first pointer
4771 +/* yaffs_CreateTnodes creates a bunch more tnodes and
4772 + * adds them to the tnode free list.
4773 + * Don't use this function directly
4776 +static int yaffs_CreateTnodes(yaffs_Device * dev, int nTnodes)
4780 + yaffs_Tnode *newTnodes;
4782 + yaffs_Tnode *curr;
4783 + yaffs_Tnode *next;
4784 + yaffs_TnodeList *tnl;
4789 + /* Calculate the tnode size in bytes for variable width tnode support.
4790 + * Must be a multiple of 32-bits */
4791 + tnodeSize = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
4793 + /* make these things */
4795 + newTnodes = YMALLOC(nTnodes * tnodeSize);
4796 + mem = (__u8 *)newTnodes;
4799 + T(YAFFS_TRACE_ERROR,
4800 + (TSTR("yaffs: Could not allocate Tnodes" TENDSTR)));
4801 + return YAFFS_FAIL;
4804 + /* Hook them into the free list */
4806 + for (i = 0; i < nTnodes - 1; i++) {
4807 + newTnodes[i].internal[0] = &newTnodes[i + 1];
4808 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4809 + newTnodes[i].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4813 + newTnodes[nTnodes - 1].internal[0] = dev->freeTnodes;
4814 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4815 + newTnodes[nTnodes - 1].internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4817 + dev->freeTnodes = newTnodes;
4819 + /* New hookup for wide tnodes */
4820 + for(i = 0; i < nTnodes -1; i++) {
4821 + curr = (yaffs_Tnode *) &mem[i * tnodeSize];
4822 + next = (yaffs_Tnode *) &mem[(i+1) * tnodeSize];
4823 + curr->internal[0] = next;
4826 + curr = (yaffs_Tnode *) &mem[(nTnodes - 1) * tnodeSize];
4827 + curr->internal[0] = dev->freeTnodes;
4828 + dev->freeTnodes = (yaffs_Tnode *)mem;
4833 + dev->nFreeTnodes += nTnodes;
4834 + dev->nTnodesCreated += nTnodes;
4836 + /* Now add this bunch of tnodes to a list for freeing up.
4837 + * NB If we can't add this to the management list it isn't fatal
4838 + * but it just means we can't free this bunch of tnodes later.
4841 + tnl = YMALLOC(sizeof(yaffs_TnodeList));
4843 + T(YAFFS_TRACE_ERROR,
4845 + ("yaffs: Could not add tnodes to management list" TENDSTR)));
4846 + return YAFFS_FAIL;
4849 + tnl->tnodes = newTnodes;
4850 + tnl->next = dev->allocatedTnodeList;
4851 + dev->allocatedTnodeList = tnl;
4854 + T(YAFFS_TRACE_ALLOCATE, (TSTR("yaffs: Tnodes added" TENDSTR)));
4859 +/* GetTnode gets us a clean tnode. Tries to make allocate more if we run out */
4861 +static yaffs_Tnode *yaffs_GetTnodeRaw(yaffs_Device * dev)
4863 + yaffs_Tnode *tn = NULL;
4865 + /* If there are none left make more */
4866 + if (!dev->freeTnodes) {
4867 + yaffs_CreateTnodes(dev, YAFFS_ALLOCATION_NTNODES);
4870 + if (dev->freeTnodes) {
4871 + tn = dev->freeTnodes;
4872 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4873 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != (void *)1) {
4874 + /* Hoosterman, this thing looks like it isn't in the list */
4875 + T(YAFFS_TRACE_ALWAYS,
4876 + (TSTR("yaffs: Tnode list bug 1" TENDSTR)));
4879 + dev->freeTnodes = dev->freeTnodes->internal[0];
4880 + dev->nFreeTnodes--;
4886 +static yaffs_Tnode *yaffs_GetTnode(yaffs_Device * dev)
4888 + yaffs_Tnode *tn = yaffs_GetTnodeRaw(dev);
4891 + memset(tn, 0, (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
4896 +/* FreeTnode frees up a tnode and puts it back on the free list */
4897 +static void yaffs_FreeTnode(yaffs_Device * dev, yaffs_Tnode * tn)
4900 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
4901 + if (tn->internal[YAFFS_NTNODES_INTERNAL] != 0) {
4902 + /* Hoosterman, this thing looks like it is already in the list */
4903 + T(YAFFS_TRACE_ALWAYS,
4904 + (TSTR("yaffs: Tnode list bug 2" TENDSTR)));
4906 + tn->internal[YAFFS_NTNODES_INTERNAL] = (void *)1;
4908 + tn->internal[0] = dev->freeTnodes;
4909 + dev->freeTnodes = tn;
4910 + dev->nFreeTnodes++;
4914 +static void yaffs_DeinitialiseTnodes(yaffs_Device * dev)
4916 + /* Free the list of allocated tnodes */
4917 + yaffs_TnodeList *tmp;
4919 + while (dev->allocatedTnodeList) {
4920 + tmp = dev->allocatedTnodeList->next;
4922 + YFREE(dev->allocatedTnodeList->tnodes);
4923 + YFREE(dev->allocatedTnodeList);
4924 + dev->allocatedTnodeList = tmp;
4928 + dev->freeTnodes = NULL;
4929 + dev->nFreeTnodes = 0;
4932 +static void yaffs_InitialiseTnodes(yaffs_Device * dev)
4934 + dev->allocatedTnodeList = NULL;
4935 + dev->freeTnodes = NULL;
4936 + dev->nFreeTnodes = 0;
4937 + dev->nTnodesCreated = 0;
4942 +void yaffs_PutLevel0Tnode(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos, unsigned val)
4944 + __u32 *map = (__u32 *)tn;
4950 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4951 + val >>= dev->chunkGroupBits;
4953 + bitInMap = pos * dev->tnodeWidth;
4954 + wordInMap = bitInMap /32;
4955 + bitInWord = bitInMap & (32 -1);
4957 + mask = dev->tnodeMask << bitInWord;
4959 + map[wordInMap] &= ~mask;
4960 + map[wordInMap] |= (mask & (val << bitInWord));
4962 + if(dev->tnodeWidth > (32-bitInWord)) {
4963 + bitInWord = (32 - bitInWord);
4965 + mask = dev->tnodeMask >> (/*dev->tnodeWidth -*/ bitInWord);
4966 + map[wordInMap] &= ~mask;
4967 + map[wordInMap] |= (mask & (val >> bitInWord));
4971 +static __u32 yaffs_GetChunkGroupBase(yaffs_Device *dev, yaffs_Tnode *tn, unsigned pos)
4973 + __u32 *map = (__u32 *)tn;
4979 + pos &= YAFFS_TNODES_LEVEL0_MASK;
4981 + bitInMap = pos * dev->tnodeWidth;
4982 + wordInMap = bitInMap /32;
4983 + bitInWord = bitInMap & (32 -1);
4985 + val = map[wordInMap] >> bitInWord;
4987 + if(dev->tnodeWidth > (32-bitInWord)) {
4988 + bitInWord = (32 - bitInWord);
4990 + val |= (map[wordInMap] << bitInWord);
4993 + val &= dev->tnodeMask;
4994 + val <<= dev->chunkGroupBits;
4999 +/* ------------------- End of individual tnode manipulation -----------------*/
5001 +/* ---------Functions to manipulate the look-up tree (made up of tnodes) ------
5002 + * The look up tree is represented by the top tnode and the number of topLevel
5003 + * in the tree. 0 means only the level 0 tnode is in the tree.
5006 +/* FindLevel0Tnode finds the level 0 tnode, if one exists. */
5007 +static yaffs_Tnode *yaffs_FindLevel0Tnode(yaffs_Device * dev,
5008 + yaffs_FileStructure * fStruct,
5012 + yaffs_Tnode *tn = fStruct->top;
5014 + int requiredTallness;
5015 + int level = fStruct->topLevel;
5017 + /* Check sane level and chunk Id */
5018 + if (level < 0 || level > YAFFS_TNODES_MAX_LEVEL) {
5022 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5026 + /* First check we're tall enough (ie enough topLevel) */
5028 + i = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5029 + requiredTallness = 0;
5031 + i >>= YAFFS_TNODES_INTERNAL_BITS;
5032 + requiredTallness++;
5035 + if (requiredTallness > fStruct->topLevel) {
5036 + /* Not tall enough, so we can't find it, return NULL. */
5040 + /* Traverse down to level 0 */
5041 + while (level > 0 && tn) {
5043 + internal[(chunkId >>
5044 + ( YAFFS_TNODES_LEVEL0_BITS +
5046 + YAFFS_TNODES_INTERNAL_BITS)
5048 + YAFFS_TNODES_INTERNAL_MASK];
5056 +/* AddOrFindLevel0Tnode finds the level 0 tnode if it exists, otherwise first expands the tree.
5057 + * This happens in two steps:
5058 + * 1. If the tree isn't tall enough, then make it taller.
5059 + * 2. Scan down the tree towards the level 0 tnode adding tnodes if required.
5061 + * Used when modifying the tree.
5063 + * If the tn argument is NULL, then a fresh tnode will be added otherwise the specified tn will
5064 + * be plugged into the ttree.
5067 +static yaffs_Tnode *yaffs_AddOrFindLevel0Tnode(yaffs_Device * dev,
5068 + yaffs_FileStructure * fStruct,
5070 + yaffs_Tnode *passedTn)
5073 + int requiredTallness;
5081 + /* Check sane level and page Id */
5082 + if (fStruct->topLevel < 0 || fStruct->topLevel > YAFFS_TNODES_MAX_LEVEL) {
5086 + if (chunkId > YAFFS_MAX_CHUNK_ID) {
5090 + /* First check we're tall enough (ie enough topLevel) */
5092 + x = chunkId >> YAFFS_TNODES_LEVEL0_BITS;
5093 + requiredTallness = 0;
5095 + x >>= YAFFS_TNODES_INTERNAL_BITS;
5096 + requiredTallness++;
5100 + if (requiredTallness > fStruct->topLevel) {
5101 + /* Not tall enough,gotta make the tree taller */
5102 + for (i = fStruct->topLevel; i < requiredTallness; i++) {
5104 + tn = yaffs_GetTnode(dev);
5107 + tn->internal[0] = fStruct->top;
5108 + fStruct->top = tn;
5110 + T(YAFFS_TRACE_ERROR,
5111 + (TSTR("yaffs: no more tnodes" TENDSTR)));
5115 + fStruct->topLevel = requiredTallness;
5118 + /* Traverse down to level 0, adding anything we need */
5120 + l = fStruct->topLevel;
5121 + tn = fStruct->top;
5124 + while (l > 0 && tn) {
5126 + ( YAFFS_TNODES_LEVEL0_BITS +
5127 + (l - 1) * YAFFS_TNODES_INTERNAL_BITS)) &
5128 + YAFFS_TNODES_INTERNAL_MASK;
5131 + if((l>1) && !tn->internal[x]){
5132 + /* Add missing non-level-zero tnode */
5133 + tn->internal[x] = yaffs_GetTnode(dev);
5135 + } else if(l == 1) {
5136 + /* Looking from level 1 at level 0 */
5138 + /* If we already have one, then release it.*/
5139 + if(tn->internal[x])
5140 + yaffs_FreeTnode(dev,tn->internal[x]);
5141 + tn->internal[x] = passedTn;
5143 + } else if(!tn->internal[x]) {
5144 + /* Don't have one, none passed in */
5145 + tn->internal[x] = yaffs_GetTnode(dev);
5149 + tn = tn->internal[x];
5153 + /* top is level 0 */
5155 + memcpy(tn,passedTn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
5156 + yaffs_FreeTnode(dev,passedTn);
5163 +static int yaffs_FindChunkInGroup(yaffs_Device * dev, int theChunk,
5164 + yaffs_ExtendedTags * tags, int objectId,
5169 + for (j = 0; theChunk && j < dev->chunkGroupSize; j++) {
5170 + if (yaffs_CheckChunkBit
5171 + (dev, theChunk / dev->nChunksPerBlock,
5172 + theChunk % dev->nChunksPerBlock)) {
5173 + yaffs_ReadChunkWithTagsFromNAND(dev, theChunk, NULL,
5175 + if (yaffs_TagsMatch(tags, objectId, chunkInInode)) {
5187 +/* DeleteWorker scans backwards through the tnode tree and deletes all the
5188 + * chunks and tnodes in the file
5189 + * Returns 1 if the tree was deleted.
5190 + * Returns 0 if it stopped early due to hitting the limit and the delete is incomplete.
5193 +static int yaffs_DeleteWorker(yaffs_Object * in, yaffs_Tnode * tn, __u32 level,
5194 + int chunkOffset, int *limit)
5199 + yaffs_ExtendedTags tags;
5201 + yaffs_Device *dev = in->myDev;
5208 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5210 + if (tn->internal[i]) {
5211 + if (limit && (*limit) < 0) {
5215 + yaffs_DeleteWorker(in,
5223 + YAFFS_TNODES_INTERNAL_BITS)
5228 + yaffs_FreeTnode(dev,
5231 + tn->internal[i] = NULL;
5236 + return (allDone) ? 1 : 0;
5237 + } else if (level == 0) {
5240 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0 && !hitLimit;
5242 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5247 + YAFFS_TNODES_LEVEL0_BITS) + i;
5250 + yaffs_FindChunkInGroup(dev,
5256 + if (foundChunk > 0) {
5257 + yaffs_DeleteChunk(dev,
5260 + in->nDataChunks--;
5262 + *limit = *limit - 1;
5263 + if (*limit <= 0) {
5270 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5274 + return (i < 0) ? 1 : 0;
5284 +static void yaffs_SoftDeleteChunk(yaffs_Device * dev, int chunk)
5287 + yaffs_BlockInfo *theBlock;
5289 + T(YAFFS_TRACE_DELETION, (TSTR("soft delete chunk %d" TENDSTR), chunk));
5291 + theBlock = yaffs_GetBlockInfo(dev, chunk / dev->nChunksPerBlock);
5293 + theBlock->softDeletions++;
5294 + dev->nFreeChunks++;
5298 +/* SoftDeleteWorker scans backwards through the tnode tree and soft deletes all the chunks in the file.
5299 + * All soft deleting does is increment the block's softdelete count and pulls the chunk out
5301 + * Thus, essentially this is the same as DeleteWorker except that the chunks are soft deleted.
5304 +static int yaffs_SoftDeleteWorker(yaffs_Object * in, yaffs_Tnode * tn,
5305 + __u32 level, int chunkOffset)
5310 + yaffs_Device *dev = in->myDev;
5315 + for (i = YAFFS_NTNODES_INTERNAL - 1; allDone && i >= 0;
5317 + if (tn->internal[i]) {
5319 + yaffs_SoftDeleteWorker(in,
5325 + YAFFS_TNODES_INTERNAL_BITS)
5328 + yaffs_FreeTnode(dev,
5331 + tn->internal[i] = NULL;
5333 + /* Hoosterman... how could this happen? */
5337 + return (allDone) ? 1 : 0;
5338 + } else if (level == 0) {
5340 + for (i = YAFFS_NTNODES_LEVEL0 - 1; i >= 0; i--) {
5341 + theChunk = yaffs_GetChunkGroupBase(dev,tn,i);
5343 + /* Note this does not find the real chunk, only the chunk group.
5344 + * We make an assumption that a chunk group is not larger than
5347 + yaffs_SoftDeleteChunk(dev, theChunk);
5348 + yaffs_PutLevel0Tnode(dev,tn,i,0);
5362 +static void yaffs_SoftDeleteFile(yaffs_Object * obj)
5364 + if (obj->deleted &&
5365 + obj->variantType == YAFFS_OBJECT_TYPE_FILE && !obj->softDeleted) {
5366 + if (obj->nDataChunks <= 0) {
5367 + /* Empty file with no duplicate object headers, just delete it immediately */
5368 + yaffs_FreeTnode(obj->myDev,
5369 + obj->variant.fileVariant.top);
5370 + obj->variant.fileVariant.top = NULL;
5371 + T(YAFFS_TRACE_TRACING,
5372 + (TSTR("yaffs: Deleting empty file %d" TENDSTR),
5374 + yaffs_DoGenericObjectDeletion(obj);
5376 + yaffs_SoftDeleteWorker(obj,
5377 + obj->variant.fileVariant.top,
5378 + obj->variant.fileVariant.
5380 + obj->softDeleted = 1;
5385 +/* Pruning removes any part of the file structure tree that is beyond the
5386 + * bounds of the file (ie that does not point to chunks).
5388 + * A file should only get pruned when its size is reduced.
5390 + * Before pruning, the chunks must be pulled from the tree and the
5391 + * level 0 tnode entries must be zeroed out.
5392 + * Could also use this for file deletion, but that's probably better handled
5393 + * by a special case.
5396 +static yaffs_Tnode *yaffs_PruneWorker(yaffs_Device * dev, yaffs_Tnode * tn,
5397 + __u32 level, int del0)
5405 + for (i = 0; i < YAFFS_NTNODES_INTERNAL; i++) {
5406 + if (tn->internal[i] && level > 0) {
5408 + yaffs_PruneWorker(dev, tn->internal[i],
5410 + (i == 0) ? del0 : 1);
5413 + if (tn->internal[i]) {
5418 + if (hasData == 0 && del0) {
5419 + /* Free and return NULL */
5421 + yaffs_FreeTnode(dev, tn);
5431 +static int yaffs_PruneFileStructure(yaffs_Device * dev,
5432 + yaffs_FileStructure * fStruct)
5439 + if (fStruct->topLevel > 0) {
5441 + yaffs_PruneWorker(dev, fStruct->top, fStruct->topLevel, 0);
5443 + /* Now we have a tree with all the non-zero branches NULL but the height
5444 + * is the same as it was.
5445 + * Let's see if we can trim internal tnodes to shorten the tree.
5446 + * We can do this if only the 0th element in the tnode is in use
5447 + * (ie all the non-zero are NULL)
5450 + while (fStruct->topLevel && !done) {
5451 + tn = fStruct->top;
5454 + for (i = 1; i < YAFFS_NTNODES_INTERNAL; i++) {
5455 + if (tn->internal[i]) {
5461 + fStruct->top = tn->internal[0];
5462 + fStruct->topLevel--;
5463 + yaffs_FreeTnode(dev, tn);
5473 +/*-------------------- End of File Structure functions.-------------------*/
5475 +/* yaffs_CreateFreeObjects creates a bunch more objects and
5476 + * adds them to the object free list.
5478 +static int yaffs_CreateFreeObjects(yaffs_Device * dev, int nObjects)
5481 + yaffs_Object *newObjects;
5482 + yaffs_ObjectList *list;
5487 + /* make these things */
5488 + newObjects = YMALLOC(nObjects * sizeof(yaffs_Object));
5489 + list = YMALLOC(sizeof(yaffs_ObjectList));
5491 + if (!newObjects || !list) {
5493 + YFREE(newObjects);
5496 + T(YAFFS_TRACE_ALLOCATE,
5497 + (TSTR("yaffs: Could not allocate more objects" TENDSTR)));
5498 + return YAFFS_FAIL;
5501 + /* Hook them into the free list */
5502 + for (i = 0; i < nObjects - 1; i++) {
5503 + newObjects[i].siblings.next =
5504 + (struct list_head *)(&newObjects[i + 1]);
5507 + newObjects[nObjects - 1].siblings.next = (void *)dev->freeObjects;
5508 + dev->freeObjects = newObjects;
5509 + dev->nFreeObjects += nObjects;
5510 + dev->nObjectsCreated += nObjects;
5512 + /* Now add this bunch of Objects to a list for freeing up. */
5514 + list->objects = newObjects;
5515 + list->next = dev->allocatedObjectList;
5516 + dev->allocatedObjectList = list;
5522 +/* AllocateEmptyObject gets us a clean Object. Tries to make allocate more if we run out */
5523 +static yaffs_Object *yaffs_AllocateEmptyObject(yaffs_Device * dev)
5525 + yaffs_Object *tn = NULL;
5527 + /* If there are none left make more */
5528 + if (!dev->freeObjects) {
5529 + yaffs_CreateFreeObjects(dev, YAFFS_ALLOCATION_NOBJECTS);
5532 + if (dev->freeObjects) {
5533 + tn = dev->freeObjects;
5534 + dev->freeObjects =
5535 + (yaffs_Object *) (dev->freeObjects->siblings.next);
5536 + dev->nFreeObjects--;
5538 + /* Now sweeten it up... */
5540 + memset(tn, 0, sizeof(yaffs_Object));
5543 + tn->variantType = YAFFS_OBJECT_TYPE_UNKNOWN;
5544 + INIT_LIST_HEAD(&(tn->hardLinks));
5545 + INIT_LIST_HEAD(&(tn->hashLink));
5546 + INIT_LIST_HEAD(&tn->siblings);
5548 + /* Add it to the lost and found directory.
5549 + * NB Can't put root or lostNFound in lostNFound so
5550 + * check if lostNFound exists first
5552 + if (dev->lostNFoundDir) {
5553 + yaffs_AddObjectToDirectory(dev->lostNFoundDir, tn);
5560 +static yaffs_Object *yaffs_CreateFakeDirectory(yaffs_Device * dev, int number,
5564 + yaffs_Object *obj =
5565 + yaffs_CreateNewObject(dev, number, YAFFS_OBJECT_TYPE_DIRECTORY);
5567 + obj->fake = 1; /* it is fake so it has no NAND presence... */
5568 + obj->renameAllowed = 0; /* ... and we're not allowed to rename it... */
5569 + obj->unlinkAllowed = 0; /* ... or unlink it */
5571 + obj->unlinked = 0;
5572 + obj->yst_mode = mode;
5574 + obj->chunkId = 0; /* Not a valid chunk. */
5581 +static void yaffs_UnhashObject(yaffs_Object * tn)
5584 + yaffs_Device *dev = tn->myDev;
5586 + /* If it is still linked into the bucket list, free from the list */
5587 + if (!list_empty(&tn->hashLink)) {
5588 + list_del_init(&tn->hashLink);
5589 + bucket = yaffs_HashFunction(tn->objectId);
5590 + dev->objectBucket[bucket].count--;
5595 +/* FreeObject frees up a Object and puts it back on the free list */
5596 +static void yaffs_FreeObject(yaffs_Object * tn)
5599 + yaffs_Device *dev = tn->myDev;
5602 + if (tn->myInode) {
5603 + /* We're still hooked up to a cached inode.
5604 + * Don't delete now, but mark for later deletion
5606 + tn->deferedFree = 1;
5611 + yaffs_UnhashObject(tn);
5613 + /* Link into the free list. */
5614 + tn->siblings.next = (struct list_head *)(dev->freeObjects);
5615 + dev->freeObjects = tn;
5616 + dev->nFreeObjects++;
5621 +void yaffs_HandleDeferedFree(yaffs_Object * obj)
5623 + if (obj->deferedFree) {
5624 + yaffs_FreeObject(obj);
5630 +static void yaffs_DeinitialiseObjects(yaffs_Device * dev)
5632 + /* Free the list of allocated Objects */
5634 + yaffs_ObjectList *tmp;
5636 + while (dev->allocatedObjectList) {
5637 + tmp = dev->allocatedObjectList->next;
5638 + YFREE(dev->allocatedObjectList->objects);
5639 + YFREE(dev->allocatedObjectList);
5641 + dev->allocatedObjectList = tmp;
5644 + dev->freeObjects = NULL;
5645 + dev->nFreeObjects = 0;
5648 +static void yaffs_InitialiseObjects(yaffs_Device * dev)
5652 + dev->allocatedObjectList = NULL;
5653 + dev->freeObjects = NULL;
5654 + dev->nFreeObjects = 0;
5656 + for (i = 0; i < YAFFS_NOBJECT_BUCKETS; i++) {
5657 + INIT_LIST_HEAD(&dev->objectBucket[i].list);
5658 + dev->objectBucket[i].count = 0;
5663 +static int yaffs_FindNiceObjectBucket(yaffs_Device * dev)
5668 + int lowest = 999999;
5670 + /* First let's see if we can find one that's empty. */
5672 + for (i = 0; i < 10 && lowest > 0; i++) {
5674 + x %= YAFFS_NOBJECT_BUCKETS;
5675 + if (dev->objectBucket[x].count < lowest) {
5676 + lowest = dev->objectBucket[x].count;
5682 + /* If we didn't find an empty list, then try
5683 + * looking a bit further for a short one
5686 + for (i = 0; i < 10 && lowest > 3; i++) {
5688 + x %= YAFFS_NOBJECT_BUCKETS;
5689 + if (dev->objectBucket[x].count < lowest) {
5690 + lowest = dev->objectBucket[x].count;
5699 +static int yaffs_CreateNewObjectNumber(yaffs_Device * dev)
5701 + int bucket = yaffs_FindNiceObjectBucket(dev);
5703 + /* Now find an object value that has not already been taken
5704 + * by scanning the list.
5708 + struct list_head *i;
5710 + __u32 n = (__u32) bucket;
5712 + /* yaffs_CheckObjectHashSanity(); */
5716 + n += YAFFS_NOBJECT_BUCKETS;
5717 + if (1 || dev->objectBucket[bucket].count > 0) {
5718 + list_for_each(i, &dev->objectBucket[bucket].list) {
5719 + /* If there is already one in the list */
5721 + && list_entry(i, yaffs_Object,
5722 + hashLink)->objectId == n) {
5733 +static void yaffs_HashObject(yaffs_Object * in)
5735 + int bucket = yaffs_HashFunction(in->objectId);
5736 + yaffs_Device *dev = in->myDev;
5738 + list_add(&in->hashLink, &dev->objectBucket[bucket].list);
5739 + dev->objectBucket[bucket].count++;
5743 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number)
5745 + int bucket = yaffs_HashFunction(number);
5746 + struct list_head *i;
5749 + list_for_each(i, &dev->objectBucket[bucket].list) {
5750 + /* Look if it is in the list */
5752 + in = list_entry(i, yaffs_Object, hashLink);
5753 + if (in->objectId == number) {
5755 + /* Don't tell the VFS about this one if it is defered free */
5756 + if (in->deferedFree)
5768 +yaffs_Object *yaffs_CreateNewObject(yaffs_Device * dev, int number,
5769 + yaffs_ObjectType type)
5772 + yaffs_Object *theObject;
5776 + number = yaffs_CreateNewObjectNumber(dev);
5779 + theObject = yaffs_AllocateEmptyObject(dev);
5783 + if(type == YAFFS_OBJECT_TYPE_FILE){
5784 + tn = yaffs_GetTnode(dev);
5786 + yaffs_FreeObject(theObject);
5794 + theObject->fake = 0;
5795 + theObject->renameAllowed = 1;
5796 + theObject->unlinkAllowed = 1;
5797 + theObject->objectId = number;
5798 + yaffs_HashObject(theObject);
5799 + theObject->variantType = type;
5800 +#ifdef CONFIG_YAFFS_WINCE
5801 + yfsd_WinFileTimeNow(theObject->win_atime);
5802 + theObject->win_ctime[0] = theObject->win_mtime[0] =
5803 + theObject->win_atime[0];
5804 + theObject->win_ctime[1] = theObject->win_mtime[1] =
5805 + theObject->win_atime[1];
5809 + theObject->yst_atime = theObject->yst_mtime =
5810 + theObject->yst_ctime = Y_CURRENT_TIME;
5813 + case YAFFS_OBJECT_TYPE_FILE:
5814 + theObject->variant.fileVariant.fileSize = 0;
5815 + theObject->variant.fileVariant.scannedFileSize = 0;
5816 + theObject->variant.fileVariant.shrinkSize = 0xFFFFFFFF; /* max __u32 */
5817 + theObject->variant.fileVariant.topLevel = 0;
5818 + theObject->variant.fileVariant.top = tn;
5820 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5821 + INIT_LIST_HEAD(&theObject->variant.directoryVariant.
5824 + case YAFFS_OBJECT_TYPE_SYMLINK:
5825 + case YAFFS_OBJECT_TYPE_HARDLINK:
5826 + case YAFFS_OBJECT_TYPE_SPECIAL:
5827 + /* No action required */
5829 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5830 + /* todo this should not happen */
5838 +static yaffs_Object *yaffs_FindOrCreateObjectByNumber(yaffs_Device * dev,
5840 + yaffs_ObjectType type)
5842 + yaffs_Object *theObject = NULL;
5845 + theObject = yaffs_FindObjectByNumber(dev, number);
5849 + theObject = yaffs_CreateNewObject(dev, number, type);
5857 +static YCHAR *yaffs_CloneString(const YCHAR * str)
5859 + YCHAR *newStr = NULL;
5861 + if (str && *str) {
5862 + newStr = YMALLOC((yaffs_strlen(str) + 1) * sizeof(YCHAR));
5864 + yaffs_strcpy(newStr, str);
5872 + * Mknod (create) a new object.
5873 + * equivalentObject only has meaning for a hard link;
5874 + * aliasString only has meaning for a sumlink.
5875 + * rdev only has meaning for devices (a subset of special objects)
5878 +static yaffs_Object *yaffs_MknodObject(yaffs_ObjectType type,
5879 + yaffs_Object * parent,
5880 + const YCHAR * name,
5884 + yaffs_Object * equivalentObject,
5885 + const YCHAR * aliasString, __u32 rdev)
5890 + yaffs_Device *dev = parent->myDev;
5892 + /* Check if the entry exists. If it does then fail the call since we don't want a dup.*/
5893 + if (yaffs_FindObjectByName(parent, name)) {
5897 + in = yaffs_CreateNewObject(dev, -1, type);
5899 + if(type == YAFFS_OBJECT_TYPE_SYMLINK){
5900 + str = yaffs_CloneString(aliasString);
5902 + yaffs_FreeObject(in);
5912 + in->variantType = type;
5914 + in->yst_mode = mode;
5916 +#ifdef CONFIG_YAFFS_WINCE
5917 + yfsd_WinFileTimeNow(in->win_atime);
5918 + in->win_ctime[0] = in->win_mtime[0] = in->win_atime[0];
5919 + in->win_ctime[1] = in->win_mtime[1] = in->win_atime[1];
5922 + in->yst_atime = in->yst_mtime = in->yst_ctime = Y_CURRENT_TIME;
5924 + in->yst_rdev = rdev;
5925 + in->yst_uid = uid;
5926 + in->yst_gid = gid;
5928 + in->nDataChunks = 0;
5930 + yaffs_SetObjectName(in, name);
5933 + yaffs_AddObjectToDirectory(parent, in);
5935 + in->myDev = parent->myDev;
5938 + case YAFFS_OBJECT_TYPE_SYMLINK:
5939 + in->variant.symLinkVariant.alias = str;
5941 + case YAFFS_OBJECT_TYPE_HARDLINK:
5942 + in->variant.hardLinkVariant.equivalentObject =
5944 + in->variant.hardLinkVariant.equivalentObjectId =
5945 + equivalentObject->objectId;
5946 + list_add(&in->hardLinks, &equivalentObject->hardLinks);
5948 + case YAFFS_OBJECT_TYPE_FILE:
5949 + case YAFFS_OBJECT_TYPE_DIRECTORY:
5950 + case YAFFS_OBJECT_TYPE_SPECIAL:
5951 + case YAFFS_OBJECT_TYPE_UNKNOWN:
5956 + if (yaffs_UpdateObjectHeader(in, name, 0, 0, 0) < 0) {
5957 + /* Could not create the object header, fail the creation */
5958 + yaffs_DestroyObject(in);
5967 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
5968 + __u32 mode, __u32 uid, __u32 gid)
5970 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_FILE, parent, name, mode,
5971 + uid, gid, NULL, NULL, 0);
5974 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
5975 + __u32 mode, __u32 uid, __u32 gid)
5977 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_DIRECTORY, parent, name,
5978 + mode, uid, gid, NULL, NULL, 0);
5981 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
5982 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev)
5984 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SPECIAL, parent, name, mode,
5985 + uid, gid, NULL, NULL, rdev);
5988 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
5989 + __u32 mode, __u32 uid, __u32 gid,
5990 + const YCHAR * alias)
5992 + return yaffs_MknodObject(YAFFS_OBJECT_TYPE_SYMLINK, parent, name, mode,
5993 + uid, gid, NULL, alias, 0);
5996 +/* yaffs_Link returns the object id of the equivalent object.*/
5997 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
5998 + yaffs_Object * equivalentObject)
6000 + /* Get the real object in case we were fed a hard link as an equivalent object */
6001 + equivalentObject = yaffs_GetEquivalentObject(equivalentObject);
6003 + if (yaffs_MknodObject
6004 + (YAFFS_OBJECT_TYPE_HARDLINK, parent, name, 0, 0, 0,
6005 + equivalentObject, NULL, 0)) {
6006 + return equivalentObject;
6013 +static int yaffs_ChangeObjectName(yaffs_Object * obj, yaffs_Object * newDir,
6014 + const YCHAR * newName, int force, int shadows)
6019 + yaffs_Object *existingTarget;
6021 + if (newDir == NULL) {
6022 + newDir = obj->parent; /* use the old directory */
6025 + if (newDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
6026 + T(YAFFS_TRACE_ALWAYS,
6028 + ("tragendy: yaffs_ChangeObjectName: newDir is not a directory"
6033 + /* TODO: Do we need this different handling for YAFFS2 and YAFFS1?? */
6034 + if (obj->myDev->isYaffs2) {
6035 + unlinkOp = (newDir == obj->myDev->unlinkedDir);
6037 + unlinkOp = (newDir == obj->myDev->unlinkedDir
6038 + && obj->variantType == YAFFS_OBJECT_TYPE_FILE);
6041 + deleteOp = (newDir == obj->myDev->deletedDir);
6043 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6045 + /* If the object is a file going into the unlinked directory,
6046 + * then it is OK to just stuff it in since duplicate names are allowed.
6047 + * else only proceed if the new name does not exist and if we're putting
6048 + * it into a directory.
6054 + !existingTarget) &&
6055 + newDir->variantType == YAFFS_OBJECT_TYPE_DIRECTORY) {
6056 + yaffs_SetObjectName(obj, newName);
6059 + yaffs_AddObjectToDirectory(newDir, obj);
6062 + obj->unlinked = 1;
6064 + /* If it is a deletion then we mark it as a shrink for gc purposes. */
6065 + if (yaffs_UpdateObjectHeader(obj, newName, 0, deleteOp, shadows)>= 0)
6069 + return YAFFS_FAIL;
6072 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
6073 + yaffs_Object * newDir, const YCHAR * newName)
6075 + yaffs_Object *obj;
6076 + yaffs_Object *existingTarget;
6079 +#ifdef CONFIG_YAFFS_CASE_INSENSITIVE
6080 + /* Special case for case insemsitive systems (eg. WinCE).
6081 + * While look-up is case insensitive, the name isn't.
6082 + * Therefore we might want to change x.txt to X.txt
6084 + if (oldDir == newDir && yaffs_strcmp(oldName, newName) == 0) {
6089 + obj = yaffs_FindObjectByName(oldDir, oldName);
6090 + /* Check new name to long. */
6091 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK &&
6092 + yaffs_strlen(newName) > YAFFS_MAX_ALIAS_LENGTH)
6093 + /* ENAMETOOLONG */
6094 + return YAFFS_FAIL;
6095 + else if (obj->variantType != YAFFS_OBJECT_TYPE_SYMLINK &&
6096 + yaffs_strlen(newName) > YAFFS_MAX_NAME_LENGTH)
6097 + /* ENAMETOOLONG */
6098 + return YAFFS_FAIL;
6100 + if (obj && obj->renameAllowed) {
6102 + /* Now do the handling for an existing target, if there is one */
6104 + existingTarget = yaffs_FindObjectByName(newDir, newName);
6105 + if (existingTarget &&
6106 + existingTarget->variantType == YAFFS_OBJECT_TYPE_DIRECTORY &&
6107 + !list_empty(&existingTarget->variant.directoryVariant.children)) {
6108 + /* There is a target that is a non-empty directory, so we fail */
6109 + return YAFFS_FAIL; /* EEXIST or ENOTEMPTY */
6110 + } else if (existingTarget && existingTarget != obj) {
6111 + /* Nuke the target first, using shadowing,
6112 + * but only if it isn't the same object
6114 + yaffs_ChangeObjectName(obj, newDir, newName, force,
6115 + existingTarget->objectId);
6116 + yaffs_UnlinkObject(existingTarget);
6119 + return yaffs_ChangeObjectName(obj, newDir, newName, 1, 0);
6121 + return YAFFS_FAIL;
6124 +/*------------------------- Block Management and Page Allocation ----------------*/
6126 +static int yaffs_InitialiseBlocks(yaffs_Device * dev)
6128 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
6130 + dev->blockInfo = NULL;
6131 + dev->chunkBits = NULL;
6133 + dev->allocationBlock = -1; /* force it to get a new one */
6135 + /* If the first allocation strategy fails, thry the alternate one */
6136 + dev->blockInfo = YMALLOC(nBlocks * sizeof(yaffs_BlockInfo));
6137 + if(!dev->blockInfo){
6138 + dev->blockInfo = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockInfo));
6139 + dev->blockInfoAlt = 1;
6142 + dev->blockInfoAlt = 0;
6144 + if(dev->blockInfo){
6146 + /* Set up dynamic blockinfo stuff. */
6147 + dev->chunkBitmapStride = (dev->nChunksPerBlock + 7) / 8; /* round up bytes */
6148 + dev->chunkBits = YMALLOC(dev->chunkBitmapStride * nBlocks);
6149 + if(!dev->chunkBits){
6150 + dev->chunkBits = YMALLOC_ALT(dev->chunkBitmapStride * nBlocks);
6151 + dev->chunkBitsAlt = 1;
6154 + dev->chunkBitsAlt = 0;
6157 + if (dev->blockInfo && dev->chunkBits) {
6158 + memset(dev->blockInfo, 0, nBlocks * sizeof(yaffs_BlockInfo));
6159 + memset(dev->chunkBits, 0, dev->chunkBitmapStride * nBlocks);
6163 + return YAFFS_FAIL;
6167 +static void yaffs_DeinitialiseBlocks(yaffs_Device * dev)
6169 + if(dev->blockInfoAlt && dev->blockInfo)
6170 + YFREE_ALT(dev->blockInfo);
6171 + else if(dev->blockInfo)
6172 + YFREE(dev->blockInfo);
6174 + dev->blockInfoAlt = 0;
6176 + dev->blockInfo = NULL;
6178 + if(dev->chunkBitsAlt && dev->chunkBits)
6179 + YFREE_ALT(dev->chunkBits);
6180 + else if(dev->chunkBits)
6181 + YFREE(dev->chunkBits);
6182 + dev->chunkBitsAlt = 0;
6183 + dev->chunkBits = NULL;
6186 +static int yaffs_BlockNotDisqualifiedFromGC(yaffs_Device * dev,
6187 + yaffs_BlockInfo * bi)
6191 + yaffs_BlockInfo *b;
6193 + if (!dev->isYaffs2)
6194 + return 1; /* disqualification only applies to yaffs2. */
6196 + if (!bi->hasShrinkHeader)
6197 + return 1; /* can gc */
6199 + /* Find the oldest dirty sequence number if we don't know it and save it
6200 + * so we don't have to keep recomputing it.
6202 + if (!dev->oldestDirtySequence) {
6203 + seq = dev->sequenceNumber;
6205 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock;
6207 + b = yaffs_GetBlockInfo(dev, i);
6208 + if (b->blockState == YAFFS_BLOCK_STATE_FULL &&
6209 + (b->pagesInUse - b->softDeletions) <
6210 + dev->nChunksPerBlock && b->sequenceNumber < seq) {
6211 + seq = b->sequenceNumber;
6214 + dev->oldestDirtySequence = seq;
6217 + /* Can't do gc of this block if there are any blocks older than this one that have
6218 + * discarded pages.
6220 + return (bi->sequenceNumber <= dev->oldestDirtySequence);
6224 +/* FindDiretiestBlock is used to select the dirtiest block (or close enough)
6225 + * for garbage collection.
6228 +static int yaffs_FindBlockForGarbageCollection(yaffs_Device * dev,
6232 + int b = dev->currentDirtyChecker;
6236 + int dirtiest = -1;
6237 + int pagesInUse = 0;
6238 + int prioritised=0;
6239 + yaffs_BlockInfo *bi;
6240 + int pendingPrioritisedExist = 0;
6242 + /* First let's see if we need to grab a prioritised block */
6243 + if(dev->hasPendingPrioritisedGCs){
6244 + for(i = dev->internalStartBlock; i < dev->internalEndBlock && !prioritised; i++){
6246 + bi = yaffs_GetBlockInfo(dev, i);
6247 + //yaffs_VerifyBlock(dev,bi,i);
6249 + if(bi->gcPrioritise) {
6250 + pendingPrioritisedExist = 1;
6251 + if(bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6252 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)){
6253 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6256 + aggressive = 1; /* Fool the non-aggressive skip logiv below */
6261 + if(!pendingPrioritisedExist) /* None found, so we can clear this */
6262 + dev->hasPendingPrioritisedGCs = 0;
6265 + /* If we're doing aggressive GC then we are happy to take a less-dirty block, and
6267 + * else (we're doing a leasurely gc), then we only bother to do this if the
6268 + * block has only a few pages in use.
6271 + dev->nonAggressiveSkip--;
6273 + if (!aggressive && (dev->nonAggressiveSkip > 0)) {
6279 + (aggressive) ? dev->nChunksPerBlock : YAFFS_PASSIVE_GC_CHUNKS + 1;
6283 + dev->internalEndBlock - dev->internalStartBlock + 1;
6286 + dev->internalEndBlock - dev->internalStartBlock + 1;
6287 + iterations = iterations / 16;
6288 + if (iterations > 200) {
6293 + for (i = 0; i <= iterations && pagesInUse > 0 && !prioritised; i++) {
6295 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6296 + b = dev->internalStartBlock;
6299 + if (b < dev->internalStartBlock || b > dev->internalEndBlock) {
6300 + T(YAFFS_TRACE_ERROR,
6301 + (TSTR("**>> Block %d is not valid" TENDSTR), b));
6305 + bi = yaffs_GetBlockInfo(dev, b);
6308 + if (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT) {
6315 + if (bi->blockState == YAFFS_BLOCK_STATE_FULL &&
6316 + (bi->pagesInUse - bi->softDeletions) < pagesInUse &&
6317 + yaffs_BlockNotDisqualifiedFromGC(dev, bi)) {
6319 + pagesInUse = (bi->pagesInUse - bi->softDeletions);
6323 + dev->currentDirtyChecker = b;
6325 + if (dirtiest > 0) {
6327 + (TSTR("GC Selected block %d with %d free, prioritised:%d" TENDSTR), dirtiest,
6328 + dev->nChunksPerBlock - pagesInUse,prioritised));
6331 + dev->oldestDirtySequence = 0;
6333 + if (dirtiest > 0) {
6334 + dev->nonAggressiveSkip = 4;
6340 +static void yaffs_BlockBecameDirty(yaffs_Device * dev, int blockNo)
6342 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, blockNo);
6346 + /* If the block is still healthy erase it and mark as clean.
6347 + * If the block has had a data failure, then retire it.
6350 + T(YAFFS_TRACE_GC | YAFFS_TRACE_ERASE,
6351 + (TSTR("yaffs_BlockBecameDirty block %d state %d %s"TENDSTR),
6352 + blockNo, bi->blockState, (bi->needsRetiring) ? "needs retiring" : ""));
6354 + bi->blockState = YAFFS_BLOCK_STATE_DIRTY;
6356 + if (!bi->needsRetiring) {
6357 + yaffs_InvalidateCheckpoint(dev);
6358 + erasedOk = yaffs_EraseBlockInNAND(dev, blockNo);
6360 + dev->nErasureFailures++;
6361 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6362 + (TSTR("**>> Erasure failed %d" TENDSTR), blockNo));
6367 + ((yaffs_traceMask & YAFFS_TRACE_ERASE) || !yaffs_SkipVerification(dev))) {
6369 + for (i = 0; i < dev->nChunksPerBlock; i++) {
6370 + if (!yaffs_CheckChunkErased
6371 + (dev, blockNo * dev->nChunksPerBlock + i)) {
6372 + T(YAFFS_TRACE_ERROR,
6374 + (">>Block %d erasure supposedly OK, but chunk %d not erased"
6375 + TENDSTR), blockNo, i));
6381 + /* Clean it up... */
6382 + bi->blockState = YAFFS_BLOCK_STATE_EMPTY;
6383 + dev->nErasedBlocks++;
6384 + bi->pagesInUse = 0;
6385 + bi->softDeletions = 0;
6386 + bi->hasShrinkHeader = 0;
6387 + bi->skipErasedCheck = 1; /* This is clean, so no need to check */
6388 + bi->gcPrioritise = 0;
6389 + yaffs_ClearChunkBits(dev, blockNo);
6391 + T(YAFFS_TRACE_ERASE,
6392 + (TSTR("Erased block %d" TENDSTR), blockNo));
6394 + dev->nFreeChunks -= dev->nChunksPerBlock; /* We lost a block of free space */
6396 + yaffs_RetireBlock(dev, blockNo);
6397 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
6398 + (TSTR("**>> Block %d retired" TENDSTR), blockNo));
6402 +static int yaffs_FindBlockForAllocation(yaffs_Device * dev)
6406 + yaffs_BlockInfo *bi;
6408 + if (dev->nErasedBlocks < 1) {
6409 + /* Hoosterman we've got a problem.
6410 + * Can't get space to gc
6412 + T(YAFFS_TRACE_ERROR,
6413 + (TSTR("yaffs tragedy: no more eraased blocks" TENDSTR)));
6418 + /* Find an empty block. */
6420 + for (i = dev->internalStartBlock; i <= dev->internalEndBlock; i++) {
6421 + dev->allocationBlockFinder++;
6422 + if (dev->allocationBlockFinder < dev->internalStartBlock
6423 + || dev->allocationBlockFinder > dev->internalEndBlock) {
6424 + dev->allocationBlockFinder = dev->internalStartBlock;
6427 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlockFinder);
6429 + if (bi->blockState == YAFFS_BLOCK_STATE_EMPTY) {
6430 + bi->blockState = YAFFS_BLOCK_STATE_ALLOCATING;
6431 + dev->sequenceNumber++;
6432 + bi->sequenceNumber = dev->sequenceNumber;
6433 + dev->nErasedBlocks--;
6434 + T(YAFFS_TRACE_ALLOCATE,
6435 + (TSTR("Allocated block %d, seq %d, %d left" TENDSTR),
6436 + dev->allocationBlockFinder, dev->sequenceNumber,
6437 + dev->nErasedBlocks));
6438 + return dev->allocationBlockFinder;
6442 + T(YAFFS_TRACE_ALWAYS,
6444 + ("yaffs tragedy: no more eraased blocks, but there should have been %d"
6445 + TENDSTR), dev->nErasedBlocks));
6451 +// Check if there's space to allocate...
6452 +// Thinks.... do we need top make this ths same as yaffs_GetFreeChunks()?
6453 +static int yaffs_CheckSpaceForAllocation(yaffs_Device * dev)
6455 + int reservedChunks;
6456 + int reservedBlocks = dev->nReservedBlocks;
6457 + int checkpointBlocks;
6459 + checkpointBlocks = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
6460 + if(checkpointBlocks < 0)
6461 + checkpointBlocks = 0;
6463 + reservedChunks = ((reservedBlocks + checkpointBlocks) * dev->nChunksPerBlock);
6465 + return (dev->nFreeChunks > reservedChunks);
6468 +static int yaffs_AllocateChunk(yaffs_Device * dev, int useReserve, yaffs_BlockInfo **blockUsedPtr)
6471 + yaffs_BlockInfo *bi;
6473 + if (dev->allocationBlock < 0) {
6474 + /* Get next block to allocate off */
6475 + dev->allocationBlock = yaffs_FindBlockForAllocation(dev);
6476 + dev->allocationPage = 0;
6479 + if (!useReserve && !yaffs_CheckSpaceForAllocation(dev)) {
6480 + /* Not enough space to allocate unless we're allowed to use the reserve. */
6484 + if (dev->nErasedBlocks < dev->nReservedBlocks
6485 + && dev->allocationPage == 0) {
6486 + T(YAFFS_TRACE_ALLOCATE, (TSTR("Allocating reserve" TENDSTR)));
6489 + /* Next page please.... */
6490 + if (dev->allocationBlock >= 0) {
6491 + bi = yaffs_GetBlockInfo(dev, dev->allocationBlock);
6493 + retVal = (dev->allocationBlock * dev->nChunksPerBlock) +
6494 + dev->allocationPage;
6496 + yaffs_SetChunkBit(dev, dev->allocationBlock,
6497 + dev->allocationPage);
6499 + dev->allocationPage++;
6501 + dev->nFreeChunks--;
6503 + /* If the block is full set the state to full */
6504 + if (dev->allocationPage >= dev->nChunksPerBlock) {
6505 + bi->blockState = YAFFS_BLOCK_STATE_FULL;
6506 + dev->allocationBlock = -1;
6510 + *blockUsedPtr = bi;
6515 + T(YAFFS_TRACE_ERROR,
6516 + (TSTR("!!!!!!!!! Allocator out !!!!!!!!!!!!!!!!!" TENDSTR)));
6521 +static int yaffs_GetErasedChunks(yaffs_Device * dev)
6525 + n = dev->nErasedBlocks * dev->nChunksPerBlock;
6527 + if (dev->allocationBlock > 0) {
6528 + n += (dev->nChunksPerBlock - dev->allocationPage);
6535 +static int yaffs_GarbageCollectBlock(yaffs_Device * dev, int block)
6541 + int retVal = YAFFS_OK;
6544 + int isCheckpointBlock;
6545 + int matchingChunk;
6547 + int chunksBefore = yaffs_GetErasedChunks(dev);
6550 + yaffs_ExtendedTags tags;
6552 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, block);
6554 + yaffs_Object *object;
6556 + isCheckpointBlock = (bi->blockState == YAFFS_BLOCK_STATE_CHECKPOINT);
6558 + bi->blockState = YAFFS_BLOCK_STATE_COLLECTING;
6560 + T(YAFFS_TRACE_TRACING,
6561 + (TSTR("Collecting block %d, in use %d, shrink %d, " TENDSTR), block,
6562 + bi->pagesInUse, bi->hasShrinkHeader));
6564 + /*yaffs_VerifyFreeChunks(dev); */
6566 + bi->hasShrinkHeader = 0; /* clear the flag so that the block can erase */
6568 + /* Take off the number of soft deleted entries because
6569 + * they're going to get really deleted during GC.
6571 + dev->nFreeChunks -= bi->softDeletions;
6573 + dev->isDoingGC = 1;
6575 + if (isCheckpointBlock ||
6576 + !yaffs_StillSomeChunkBits(dev, block)) {
6577 + T(YAFFS_TRACE_TRACING,
6579 + ("Collecting block %d that has no chunks in use" TENDSTR),
6581 + yaffs_BlockBecameDirty(dev, block);
6584 + __u8 *buffer = yaffs_GetTempBuffer(dev, __LINE__);
6586 + yaffs_VerifyBlock(dev,bi,block);
6588 + for (chunkInBlock = 0, oldChunk = block * dev->nChunksPerBlock;
6589 + chunkInBlock < dev->nChunksPerBlock
6590 + && yaffs_StillSomeChunkBits(dev, block);
6591 + chunkInBlock++, oldChunk++) {
6592 + if (yaffs_CheckChunkBit(dev, block, chunkInBlock)) {
6594 + /* This page is in use and might need to be copied off */
6598 + yaffs_InitialiseTags(&tags);
6600 + yaffs_ReadChunkWithTagsFromNAND(dev, oldChunk,
6604 + yaffs_FindObjectByNumber(dev,
6607 + T(YAFFS_TRACE_GC_DETAIL,
6609 + ("Collecting page %d, %d %d %d " TENDSTR),
6610 + chunkInBlock, tags.objectId, tags.chunkId,
6613 + if(object && !yaffs_SkipVerification(dev)){
6614 + if(tags.chunkId == 0)
6615 + matchingChunk = object->chunkId;
6616 + else if(object->softDeleted)
6617 + matchingChunk = oldChunk; /* Defeat the test */
6619 + matchingChunk = yaffs_FindChunkInFile(object,tags.chunkId,NULL);
6621 + if(oldChunk != matchingChunk)
6622 + T(YAFFS_TRACE_ERROR,
6623 + (TSTR("gc: page in gc mismatch: %d %d %d %d"TENDSTR),
6624 + oldChunk,matchingChunk,tags.objectId, tags.chunkId));
6629 + T(YAFFS_TRACE_ERROR,
6631 + ("page %d in gc has no object: %d %d %d "
6632 + TENDSTR), oldChunk,
6633 + tags.objectId, tags.chunkId, tags.byteCount));
6636 + if (object && object->deleted
6637 + && tags.chunkId != 0) {
6638 + /* Data chunk in a deleted file, throw it away
6639 + * It's a soft deleted data chunk,
6640 + * No need to copy this, just forget about it and
6641 + * fix up the object.
6644 + object->nDataChunks--;
6646 + if (object->nDataChunks <= 0) {
6647 + /* remeber to clean up the object */
6648 + dev->gcCleanupList[cleanups] =
6654 + /* Todo object && object->deleted && object->nDataChunks == 0 */
6656 + /* Deleted object header with no data chunks.
6657 + * Can be discarded and the file deleted.
6659 + object->chunkId = 0;
6660 + yaffs_FreeTnode(object->myDev,
6663 + object->variant.fileVariant.top = NULL;
6664 + yaffs_DoGenericObjectDeletion(object);
6666 + } else if (object) {
6667 + /* It's either a data chunk in a live file or
6668 + * an ObjectHeader, so we're interested in it.
6669 + * NB Need to keep the ObjectHeaders of deleted files
6670 + * until the whole file has been deleted off
6672 + tags.serialNumber++;
6676 + if (tags.chunkId == 0) {
6677 + /* It is an object Id,
6678 + * We need to nuke the shrinkheader flags first
6679 + * We no longer want the shrinkHeader flag since its work is done
6680 + * and if it is left in place it will mess up scanning.
6681 + * Also, clear out any shadowing stuff
6684 + yaffs_ObjectHeader *oh;
6685 + oh = (yaffs_ObjectHeader *)buffer;
6687 + oh->shadowsObject = -1;
6688 + tags.extraShadows = 0;
6689 + tags.extraIsShrinkHeader = 0;
6691 + yaffs_VerifyObjectHeader(object,oh,&tags,1);
6695 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &tags, 1);
6697 + if (newChunk < 0) {
6698 + retVal = YAFFS_FAIL;
6701 + /* Ok, now fix up the Tnodes etc. */
6703 + if (tags.chunkId == 0) {
6704 + /* It's a header */
6705 + object->chunkId = newChunk;
6706 + object->serial = tags.serialNumber;
6708 + /* It's a data chunk */
6709 + yaffs_PutChunkIntoFile
6717 + yaffs_DeleteChunk(dev, oldChunk, markNAND, __LINE__);
6722 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
6725 + /* Do any required cleanups */
6726 + for (i = 0; i < cleanups; i++) {
6727 + /* Time to delete the file too */
6729 + yaffs_FindObjectByNumber(dev,
6730 + dev->gcCleanupList[i]);
6732 + yaffs_FreeTnode(dev,
6733 + object->variant.fileVariant.
6735 + object->variant.fileVariant.top = NULL;
6738 + ("yaffs: About to finally delete object %d"
6739 + TENDSTR), object->objectId));
6740 + yaffs_DoGenericObjectDeletion(object);
6741 + object->myDev->nDeletedFiles--;
6748 + yaffs_VerifyCollectedBlock(dev,bi,block);
6750 + if (chunksBefore >= (chunksAfter = yaffs_GetErasedChunks(dev))) {
6753 + ("gc did not increase free chunks before %d after %d"
6754 + TENDSTR), chunksBefore, chunksAfter));
6757 + dev->isDoingGC = 0;
6762 +/* New garbage collector
6763 + * If we're very low on erased blocks then we do aggressive garbage collection
6764 + * otherwise we do "leasurely" garbage collection.
6765 + * Aggressive gc looks further (whole array) and will accept less dirty blocks.
6766 + * Passive gc only inspects smaller areas and will only accept more dirty blocks.
6768 + * The idea is to help clear out space in a more spread-out manner.
6769 + * Dunno if it really does anything useful.
6771 +static int yaffs_CheckGarbageCollection(yaffs_Device * dev)
6775 + int gcOk = YAFFS_OK;
6778 + int checkpointBlockAdjust;
6780 + if (dev->isDoingGC) {
6781 + /* Bail out so we don't get recursive gc */
6785 + /* This loop should pass the first time.
6786 + * We'll only see looping here if the erase of the collected block fails.
6792 + checkpointBlockAdjust = (dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint);
6793 + if(checkpointBlockAdjust < 0)
6794 + checkpointBlockAdjust = 0;
6796 + if (dev->nErasedBlocks < (dev->nReservedBlocks + checkpointBlockAdjust + 2)) {
6797 + /* We need a block soon...*/
6800 + /* We're in no hurry */
6804 + block = yaffs_FindBlockForGarbageCollection(dev, aggressive);
6807 + dev->garbageCollections++;
6808 + if (!aggressive) {
6809 + dev->passiveGarbageCollections++;
6814 + ("yaffs: GC erasedBlocks %d aggressive %d" TENDSTR),
6815 + dev->nErasedBlocks, aggressive));
6817 + gcOk = yaffs_GarbageCollectBlock(dev, block);
6820 + if (dev->nErasedBlocks < (dev->nReservedBlocks) && block > 0) {
6823 + ("yaffs: GC !!!no reclaim!!! erasedBlocks %d after try %d block %d"
6824 + TENDSTR), dev->nErasedBlocks, maxTries, block));
6826 + } while ((dev->nErasedBlocks < dev->nReservedBlocks) && (block > 0)
6827 + && (maxTries < 2));
6829 + return aggressive ? gcOk : YAFFS_OK;
6832 +/*------------------------- TAGS --------------------------------*/
6834 +static int yaffs_TagsMatch(const yaffs_ExtendedTags * tags, int objectId,
6835 + int chunkInObject)
6837 + return (tags->chunkId == chunkInObject &&
6838 + tags->objectId == objectId && !tags->chunkDeleted) ? 1 : 0;
6843 +/*-------------------- Data file manipulation -----------------*/
6845 +static int yaffs_FindChunkInFile(yaffs_Object * in, int chunkInInode,
6846 + yaffs_ExtendedTags * tags)
6848 + /*Get the Tnode, then get the level 0 offset chunk offset */
6850 + int theChunk = -1;
6851 + yaffs_ExtendedTags localTags;
6854 + yaffs_Device *dev = in->myDev;
6857 + /* Passed a NULL, so use our own tags space */
6858 + tags = &localTags;
6861 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6864 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6867 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6873 +static int yaffs_FindAndDeleteChunkInFile(yaffs_Object * in, int chunkInInode,
6874 + yaffs_ExtendedTags * tags)
6876 + /* Get the Tnode, then get the level 0 offset chunk offset */
6878 + int theChunk = -1;
6879 + yaffs_ExtendedTags localTags;
6881 + yaffs_Device *dev = in->myDev;
6885 + /* Passed a NULL, so use our own tags space */
6886 + tags = &localTags;
6889 + tn = yaffs_FindLevel0Tnode(dev, &in->variant.fileVariant, chunkInInode);
6893 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
6896 + yaffs_FindChunkInGroup(dev, theChunk, tags, in->objectId,
6899 + /* Delete the entry in the filestructure (if found) */
6900 + if (retVal != -1) {
6901 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,0);
6904 + /*T(("No level 0 found for %d\n", chunkInInode)); */
6907 + if (retVal == -1) {
6908 + /* T(("Could not find %d to delete\n",chunkInInode)); */
6913 +#ifdef YAFFS_PARANOID
6915 +static int yaffs_CheckFileSanity(yaffs_Object * in)
6923 + yaffs_Tags localTags;
6924 + yaffs_Tags *tags = &localTags;
6928 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6929 + /* T(("Object not a file\n")); */
6930 + return YAFFS_FAIL;
6933 + objId = in->objectId;
6934 + fSize = in->variant.fileVariant.fileSize;
6936 + (fSize + in->myDev->nDataBytesPerChunk - 1) / in->myDev->nDataBytesPerChunk;
6938 + for (chunk = 1; chunk <= nChunks; chunk++) {
6939 + tn = yaffs_FindLevel0Tnode(in->myDev, &in->variant.fileVariant,
6944 + theChunk = yaffs_GetChunkGroupBase(dev,tn,chunk);
6946 + if (yaffs_CheckChunkBits
6947 + (dev, theChunk / dev->nChunksPerBlock,
6948 + theChunk % dev->nChunksPerBlock)) {
6950 + yaffs_ReadChunkTagsFromNAND(in->myDev, theChunk,
6953 + if (yaffs_TagsMatch
6954 + (tags, in->objectId, chunk, chunkDeleted)) {
6964 + /* T(("No level 0 found for %d\n", chunk)); */
6968 + return failed ? YAFFS_FAIL : YAFFS_OK;
6973 +static int yaffs_PutChunkIntoFile(yaffs_Object * in, int chunkInInode,
6974 + int chunkInNAND, int inScan)
6976 + /* NB inScan is zero unless scanning.
6977 + * For forward scanning, inScan is > 0;
6978 + * for backward scanning inScan is < 0
6982 + yaffs_Device *dev = in->myDev;
6983 + int existingChunk;
6984 + yaffs_ExtendedTags existingTags;
6985 + yaffs_ExtendedTags newTags;
6986 + unsigned existingSerial, newSerial;
6988 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
6989 + /* Just ignore an attempt at putting a chunk into a non-file during scanning
6990 + * If it is not during Scanning then something went wrong!
6993 + T(YAFFS_TRACE_ERROR,
6995 + ("yaffs tragedy:attempt to put data chunk into a non-file"
7000 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
7004 + tn = yaffs_AddOrFindLevel0Tnode(dev,
7005 + &in->variant.fileVariant,
7009 + return YAFFS_FAIL;
7012 + existingChunk = yaffs_GetChunkGroupBase(dev,tn,chunkInInode);
7014 + if (inScan != 0) {
7015 + /* If we're scanning then we need to test for duplicates
7016 + * NB This does not need to be efficient since it should only ever
7017 + * happen when the power fails during a write, then only one
7018 + * chunk should ever be affected.
7020 + * Correction for YAFFS2: This could happen quite a lot and we need to think about efficiency! TODO
7021 + * Update: For backward scanning we don't need to re-read tags so this is quite cheap.
7024 + if (existingChunk != 0) {
7025 + /* NB Right now existing chunk will not be real chunkId if the device >= 32MB
7026 + * thus we have to do a FindChunkInFile to get the real chunk id.
7028 + * We have a duplicate now we need to decide which one to use:
7030 + * Backwards scanning YAFFS2: The old one is what we use, dump the new one.
7031 + * Forward scanning YAFFS2: The new one is what we use, dump the old one.
7032 + * YAFFS1: Get both sets of tags and compare serial numbers.
7036 + /* Only do this for forward scanning */
7037 + yaffs_ReadChunkWithTagsFromNAND(dev,
7041 + /* Do a proper find */
7043 + yaffs_FindChunkInFile(in, chunkInInode,
7047 + if (existingChunk <= 0) {
7048 + /*Hoosterman - how did this happen? */
7050 + T(YAFFS_TRACE_ERROR,
7052 + ("yaffs tragedy: existing chunk < 0 in scan"
7057 + /* NB The deleted flags should be false, otherwise the chunks will
7058 + * not be loaded during a scan
7061 + newSerial = newTags.serialNumber;
7062 + existingSerial = existingTags.serialNumber;
7064 + if ((inScan > 0) &&
7065 + (in->myDev->isYaffs2 ||
7066 + existingChunk <= 0 ||
7067 + ((existingSerial + 1) & 3) == newSerial)) {
7068 + /* Forward scanning.
7070 + * Delete the old one and drop through to update the tnode
7072 + yaffs_DeleteChunk(dev, existingChunk, 1,
7075 + /* Backward scanning or we want to use the existing one
7077 + * Delete the new one and return early so that the tnode isn't changed
7079 + yaffs_DeleteChunk(dev, chunkInNAND, 1,
7087 + if (existingChunk == 0) {
7088 + in->nDataChunks++;
7091 + yaffs_PutLevel0Tnode(dev,tn,chunkInInode,chunkInNAND);
7096 +static int yaffs_ReadChunkDataFromObject(yaffs_Object * in, int chunkInInode,
7099 + int chunkInNAND = yaffs_FindChunkInFile(in, chunkInInode, NULL);
7101 + if (chunkInNAND >= 0) {
7102 + return yaffs_ReadChunkWithTagsFromNAND(in->myDev, chunkInNAND,
7105 + T(YAFFS_TRACE_NANDACCESS,
7106 + (TSTR("Chunk %d not found zero instead" TENDSTR),
7108 + /* get sane (zero) data if you read a hole */
7109 + memset(buffer, 0, in->myDev->nDataBytesPerChunk);
7115 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn)
7119 + yaffs_ExtendedTags tags;
7120 + yaffs_BlockInfo *bi;
7126 + dev->nDeletions++;
7127 + block = chunkId / dev->nChunksPerBlock;
7128 + page = chunkId % dev->nChunksPerBlock;
7131 + if(!yaffs_CheckChunkBit(dev,block,page))
7132 + T(YAFFS_TRACE_VERIFY,
7133 + (TSTR("Deleting invalid chunk %d"TENDSTR),
7136 + bi = yaffs_GetBlockInfo(dev, block);
7138 + T(YAFFS_TRACE_DELETION,
7139 + (TSTR("line %d delete of chunk %d" TENDSTR), lyn, chunkId));
7142 + bi->blockState != YAFFS_BLOCK_STATE_COLLECTING && !dev->isYaffs2) {
7144 + yaffs_InitialiseTags(&tags);
7146 + tags.chunkDeleted = 1;
7148 + yaffs_WriteChunkWithTagsToNAND(dev, chunkId, NULL, &tags);
7149 + yaffs_HandleUpdateChunk(dev, chunkId, &tags);
7151 + dev->nUnmarkedDeletions++;
7154 + /* Pull out of the management area.
7155 + * If the whole block became dirty, this will kick off an erasure.
7157 + if (bi->blockState == YAFFS_BLOCK_STATE_ALLOCATING ||
7158 + bi->blockState == YAFFS_BLOCK_STATE_FULL ||
7159 + bi->blockState == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
7160 + bi->blockState == YAFFS_BLOCK_STATE_COLLECTING) {
7161 + dev->nFreeChunks++;
7163 + yaffs_ClearChunkBit(dev, block, page);
7167 + if (bi->pagesInUse == 0 &&
7168 + !bi->hasShrinkHeader &&
7169 + bi->blockState != YAFFS_BLOCK_STATE_ALLOCATING &&
7170 + bi->blockState != YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
7171 + yaffs_BlockBecameDirty(dev, block);
7175 + /* T(("Bad news deleting chunk %d\n",chunkId)); */
7180 +static int yaffs_WriteChunkDataToObject(yaffs_Object * in, int chunkInInode,
7181 + const __u8 * buffer, int nBytes,
7184 + /* Find old chunk Need to do this to get serial number
7185 + * Write new one and patch into tree.
7186 + * Invalidate old tags.
7190 + yaffs_ExtendedTags prevTags;
7193 + yaffs_ExtendedTags newTags;
7195 + yaffs_Device *dev = in->myDev;
7197 + yaffs_CheckGarbageCollection(dev);
7199 + /* Get the previous chunk at this location in the file if it exists */
7200 + prevChunkId = yaffs_FindChunkInFile(in, chunkInInode, &prevTags);
7202 + /* Set up new tags */
7203 + yaffs_InitialiseTags(&newTags);
7205 + newTags.chunkId = chunkInInode;
7206 + newTags.objectId = in->objectId;
7207 + newTags.serialNumber =
7208 + (prevChunkId >= 0) ? prevTags.serialNumber + 1 : 1;
7209 + newTags.byteCount = nBytes;
7212 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7215 + if (newChunkId >= 0) {
7216 + yaffs_PutChunkIntoFile(in, chunkInInode, newChunkId, 0);
7218 + if (prevChunkId >= 0) {
7219 + yaffs_DeleteChunk(dev, prevChunkId, 1, __LINE__);
7223 + yaffs_CheckFileSanity(in);
7225 + return newChunkId;
7229 +/* UpdateObjectHeader updates the header on NAND for an object.
7230 + * If name is not NULL, then that new name is used.
7232 +int yaffs_UpdateObjectHeader(yaffs_Object * in, const YCHAR * name, int force,
7233 + int isShrink, int shadows)
7236 + yaffs_BlockInfo *bi;
7238 + yaffs_Device *dev = in->myDev;
7245 + yaffs_ExtendedTags newTags;
7246 + yaffs_ExtendedTags oldTags;
7248 + __u8 *buffer = NULL;
7249 + YCHAR oldName[YAFFS_MAX_NAME_LENGTH + 1];
7251 + yaffs_ObjectHeader *oh = NULL;
7253 + yaffs_strcpy(oldName,"silly old name");
7255 + if (!in->fake || force) {
7257 + yaffs_CheckGarbageCollection(dev);
7258 + yaffs_CheckObjectDetailsLoaded(in);
7260 + buffer = yaffs_GetTempBuffer(in->myDev, __LINE__);
7261 + oh = (yaffs_ObjectHeader *) buffer;
7263 + prevChunkId = in->chunkId;
7265 + if (prevChunkId >= 0) {
7266 + result = yaffs_ReadChunkWithTagsFromNAND(dev, prevChunkId,
7267 + buffer, &oldTags);
7269 + yaffs_VerifyObjectHeader(in,oh,&oldTags,0);
7271 + memcpy(oldName, oh->name, sizeof(oh->name));
7274 + memset(buffer, 0xFF, dev->nDataBytesPerChunk);
7276 + oh->type = in->variantType;
7277 + oh->yst_mode = in->yst_mode;
7278 + oh->shadowsObject = shadows;
7280 +#ifdef CONFIG_YAFFS_WINCE
7281 + oh->win_atime[0] = in->win_atime[0];
7282 + oh->win_ctime[0] = in->win_ctime[0];
7283 + oh->win_mtime[0] = in->win_mtime[0];
7284 + oh->win_atime[1] = in->win_atime[1];
7285 + oh->win_ctime[1] = in->win_ctime[1];
7286 + oh->win_mtime[1] = in->win_mtime[1];
7288 + oh->yst_uid = in->yst_uid;
7289 + oh->yst_gid = in->yst_gid;
7290 + oh->yst_atime = in->yst_atime;
7291 + oh->yst_mtime = in->yst_mtime;
7292 + oh->yst_ctime = in->yst_ctime;
7293 + oh->yst_rdev = in->yst_rdev;
7296 + oh->parentObjectId = in->parent->objectId;
7298 + oh->parentObjectId = 0;
7301 + if (name && *name) {
7302 + memset(oh->name, 0, sizeof(oh->name));
7303 + yaffs_strncpy(oh->name, name, YAFFS_MAX_NAME_LENGTH);
7304 + } else if (prevChunkId>=0) {
7305 + memcpy(oh->name, oldName, sizeof(oh->name));
7307 + memset(oh->name, 0, sizeof(oh->name));
7310 + oh->isShrink = isShrink;
7312 + switch (in->variantType) {
7313 + case YAFFS_OBJECT_TYPE_UNKNOWN:
7314 + /* Should not happen */
7316 + case YAFFS_OBJECT_TYPE_FILE:
7318 + (oh->parentObjectId == YAFFS_OBJECTID_DELETED
7319 + || oh->parentObjectId ==
7320 + YAFFS_OBJECTID_UNLINKED) ? 0 : in->variant.
7321 + fileVariant.fileSize;
7323 + case YAFFS_OBJECT_TYPE_HARDLINK:
7324 + oh->equivalentObjectId =
7325 + in->variant.hardLinkVariant.equivalentObjectId;
7327 + case YAFFS_OBJECT_TYPE_SPECIAL:
7330 + case YAFFS_OBJECT_TYPE_DIRECTORY:
7333 + case YAFFS_OBJECT_TYPE_SYMLINK:
7334 + yaffs_strncpy(oh->alias,
7335 + in->variant.symLinkVariant.alias,
7336 + YAFFS_MAX_ALIAS_LENGTH);
7337 + oh->alias[YAFFS_MAX_ALIAS_LENGTH] = 0;
7342 + yaffs_InitialiseTags(&newTags);
7344 + newTags.chunkId = 0;
7345 + newTags.objectId = in->objectId;
7346 + newTags.serialNumber = in->serial;
7348 + /* Add extra info for file header */
7350 + newTags.extraHeaderInfoAvailable = 1;
7351 + newTags.extraParentObjectId = oh->parentObjectId;
7352 + newTags.extraFileLength = oh->fileSize;
7353 + newTags.extraIsShrinkHeader = oh->isShrink;
7354 + newTags.extraEquivalentObjectId = oh->equivalentObjectId;
7355 + newTags.extraShadows = (oh->shadowsObject > 0) ? 1 : 0;
7356 + newTags.extraObjectType = in->variantType;
7358 + yaffs_VerifyObjectHeader(in,oh,&newTags,1);
7360 + /* Create new chunk in NAND */
7362 + yaffs_WriteNewChunkWithTagsToNAND(dev, buffer, &newTags,
7363 + (prevChunkId >= 0) ? 1 : 0);
7365 + if (newChunkId >= 0) {
7367 + in->chunkId = newChunkId;
7369 + if (prevChunkId >= 0) {
7370 + yaffs_DeleteChunk(dev, prevChunkId, 1,
7374 + if(!yaffs_ObjectHasCachedWriteData(in))
7377 + /* If this was a shrink, then mark the block that the chunk lives on */
7379 + bi = yaffs_GetBlockInfo(in->myDev,
7380 + newChunkId /in->myDev-> nChunksPerBlock);
7381 + bi->hasShrinkHeader = 1;
7386 + retVal = newChunkId;
7391 + yaffs_ReleaseTempBuffer(dev, buffer, __LINE__);
7396 +/*------------------------ Short Operations Cache ----------------------------------------
7397 + * In many situations where there is no high level buffering (eg WinCE) a lot of
7398 + * reads might be short sequential reads, and a lot of writes may be short
7399 + * sequential writes. eg. scanning/writing a jpeg file.
7400 + * In these cases, a short read/write cache can provide a huge perfomance benefit
7401 + * with dumb-as-a-rock code.
7402 + * In Linux, the page cache provides read buffering aand the short op cache provides write
7405 + * There are a limited number (~10) of cache chunks per device so that we don't
7406 + * need a very intelligent search.
7409 +static int yaffs_ObjectHasCachedWriteData(yaffs_Object *obj)
7411 + yaffs_Device *dev = obj->myDev;
7413 + yaffs_ChunkCache *cache;
7414 + int nCaches = obj->myDev->nShortOpCaches;
7416 + for(i = 0; i < nCaches; i++){
7417 + cache = &dev->srCache[i];
7418 + if (cache->object == obj &&
7427 +static void yaffs_FlushFilesChunkCache(yaffs_Object * obj)
7429 + yaffs_Device *dev = obj->myDev;
7430 + int lowest = -99; /* Stop compiler whining. */
7432 + yaffs_ChunkCache *cache;
7433 + int chunkWritten = 0;
7434 + int nCaches = obj->myDev->nShortOpCaches;
7436 + if (nCaches > 0) {
7440 + /* Find the dirty cache for this object with the lowest chunk id. */
7441 + for (i = 0; i < nCaches; i++) {
7442 + if (dev->srCache[i].object == obj &&
7443 + dev->srCache[i].dirty) {
7445 + || dev->srCache[i].chunkId <
7447 + cache = &dev->srCache[i];
7448 + lowest = cache->chunkId;
7453 + if (cache && !cache->locked) {
7454 + /* Write it out and free it up */
7457 + yaffs_WriteChunkDataToObject(cache->object,
7463 + cache->object = NULL;
7466 + } while (cache && chunkWritten > 0);
7469 + /* Hoosterman, disk full while writing cache out. */
7470 + T(YAFFS_TRACE_ERROR,
7471 + (TSTR("yaffs tragedy: no space during cache write" TENDSTR)));
7478 +/*yaffs_FlushEntireDeviceCache(dev)
7483 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev)
7485 + yaffs_Object *obj;
7486 + int nCaches = dev->nShortOpCaches;
7489 + /* Find a dirty object in the cache and flush it...
7490 + * until there are no further dirty objects.
7494 + for( i = 0; i < nCaches && !obj; i++) {
7495 + if (dev->srCache[i].object &&
7496 + dev->srCache[i].dirty)
7497 + obj = dev->srCache[i].object;
7501 + yaffs_FlushFilesChunkCache(obj);
7508 +/* Grab us a cache chunk for use.
7509 + * First look for an empty one.
7510 + * Then look for the least recently used non-dirty one.
7511 + * Then look for the least recently used dirty one...., flush and look again.
7513 +static yaffs_ChunkCache *yaffs_GrabChunkCacheWorker(yaffs_Device * dev)
7519 + if (dev->nShortOpCaches > 0) {
7520 + for (i = 0; i < dev->nShortOpCaches; i++) {
7521 + if (!dev->srCache[i].object)
7522 + return &dev->srCache[i];
7528 + usage = 0; /* just to stop the compiler grizzling */
7530 + for (i = 0; i < dev->nShortOpCaches; i++) {
7531 + if (!dev->srCache[i].dirty &&
7532 + ((dev->srCache[i].lastUse < usage && theOne >= 0) ||
7534 + usage = dev->srCache[i].lastUse;
7540 + return theOne >= 0 ? &dev->srCache[theOne] : NULL;
7547 +static yaffs_ChunkCache *yaffs_GrabChunkCache(yaffs_Device * dev)
7549 + yaffs_ChunkCache *cache;
7550 + yaffs_Object *theObj;
7555 + if (dev->nShortOpCaches > 0) {
7556 + /* Try find a non-dirty one... */
7558 + cache = yaffs_GrabChunkCacheWorker(dev);
7561 + /* They were all dirty, find the last recently used object and flush
7562 + * its cache, then find again.
7563 + * NB what's here is not very accurate, we actually flush the object
7564 + * the last recently used page.
7567 + /* With locking we can't assume we can use entry zero */
7574 + for (i = 0; i < dev->nShortOpCaches; i++) {
7575 + if (dev->srCache[i].object &&
7576 + !dev->srCache[i].locked &&
7577 + (dev->srCache[i].lastUse < usage || !cache))
7579 + usage = dev->srCache[i].lastUse;
7580 + theObj = dev->srCache[i].object;
7581 + cache = &dev->srCache[i];
7586 + if (!cache || cache->dirty) {
7587 + /* Flush and try again */
7588 + yaffs_FlushFilesChunkCache(theObj);
7589 + cache = yaffs_GrabChunkCacheWorker(dev);
7599 +/* Find a cached chunk */
7600 +static yaffs_ChunkCache *yaffs_FindChunkCache(const yaffs_Object * obj,
7603 + yaffs_Device *dev = obj->myDev;
7605 + if (dev->nShortOpCaches > 0) {
7606 + for (i = 0; i < dev->nShortOpCaches; i++) {
7607 + if (dev->srCache[i].object == obj &&
7608 + dev->srCache[i].chunkId == chunkId) {
7611 + return &dev->srCache[i];
7618 +/* Mark the chunk for the least recently used algorithym */
7619 +static void yaffs_UseChunkCache(yaffs_Device * dev, yaffs_ChunkCache * cache,
7623 + if (dev->nShortOpCaches > 0) {
7624 + if (dev->srLastUse < 0 || dev->srLastUse > 100000000) {
7625 + /* Reset the cache usages */
7627 + for (i = 1; i < dev->nShortOpCaches; i++) {
7628 + dev->srCache[i].lastUse = 0;
7630 + dev->srLastUse = 0;
7635 + cache->lastUse = dev->srLastUse;
7643 +/* Invalidate a single cache page.
7644 + * Do this when a whole page gets written,
7645 + * ie the short cache for this page is no longer valid.
7647 +static void yaffs_InvalidateChunkCache(yaffs_Object * object, int chunkId)
7649 + if (object->myDev->nShortOpCaches > 0) {
7650 + yaffs_ChunkCache *cache = yaffs_FindChunkCache(object, chunkId);
7653 + cache->object = NULL;
7658 +/* Invalidate all the cache pages associated with this object
7659 + * Do this whenever ther file is deleted or resized.
7661 +static void yaffs_InvalidateWholeChunkCache(yaffs_Object * in)
7664 + yaffs_Device *dev = in->myDev;
7666 + if (dev->nShortOpCaches > 0) {
7667 + /* Invalidate it. */
7668 + for (i = 0; i < dev->nShortOpCaches; i++) {
7669 + if (dev->srCache[i].object == in) {
7670 + dev->srCache[i].object = NULL;
7676 +/*--------------------- Checkpointing --------------------*/
7679 +static int yaffs_WriteCheckpointValidityMarker(yaffs_Device *dev,int head)
7681 + yaffs_CheckpointValidity cp;
7683 + memset(&cp,0,sizeof(cp));
7685 + cp.structType = sizeof(cp);
7686 + cp.magic = YAFFS_MAGIC;
7687 + cp.version = YAFFS_CHECKPOINT_VERSION;
7688 + cp.head = (head) ? 1 : 0;
7690 + return (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp))?
7694 +static int yaffs_ReadCheckpointValidityMarker(yaffs_Device *dev, int head)
7696 + yaffs_CheckpointValidity cp;
7699 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7702 + ok = (cp.structType == sizeof(cp)) &&
7703 + (cp.magic == YAFFS_MAGIC) &&
7704 + (cp.version == YAFFS_CHECKPOINT_VERSION) &&
7705 + (cp.head == ((head) ? 1 : 0));
7706 + return ok ? 1 : 0;
7709 +static void yaffs_DeviceToCheckpointDevice(yaffs_CheckpointDevice *cp,
7710 + yaffs_Device *dev)
7712 + cp->nErasedBlocks = dev->nErasedBlocks;
7713 + cp->allocationBlock = dev->allocationBlock;
7714 + cp->allocationPage = dev->allocationPage;
7715 + cp->nFreeChunks = dev->nFreeChunks;
7717 + cp->nDeletedFiles = dev->nDeletedFiles;
7718 + cp->nUnlinkedFiles = dev->nUnlinkedFiles;
7719 + cp->nBackgroundDeletions = dev->nBackgroundDeletions;
7720 + cp->sequenceNumber = dev->sequenceNumber;
7721 + cp->oldestDirtySequence = dev->oldestDirtySequence;
7725 +static void yaffs_CheckpointDeviceToDevice(yaffs_Device *dev,
7726 + yaffs_CheckpointDevice *cp)
7728 + dev->nErasedBlocks = cp->nErasedBlocks;
7729 + dev->allocationBlock = cp->allocationBlock;
7730 + dev->allocationPage = cp->allocationPage;
7731 + dev->nFreeChunks = cp->nFreeChunks;
7733 + dev->nDeletedFiles = cp->nDeletedFiles;
7734 + dev->nUnlinkedFiles = cp->nUnlinkedFiles;
7735 + dev->nBackgroundDeletions = cp->nBackgroundDeletions;
7736 + dev->sequenceNumber = cp->sequenceNumber;
7737 + dev->oldestDirtySequence = cp->oldestDirtySequence;
7741 +static int yaffs_WriteCheckpointDevice(yaffs_Device *dev)
7743 + yaffs_CheckpointDevice cp;
7745 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7749 + /* Write device runtime values*/
7750 + yaffs_DeviceToCheckpointDevice(&cp,dev);
7751 + cp.structType = sizeof(cp);
7753 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7755 + /* Write block info */
7757 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7758 + ok = (yaffs_CheckpointWrite(dev,dev->blockInfo,nBytes) == nBytes);
7761 + /* Write chunk bits */
7763 + nBytes = nBlocks * dev->chunkBitmapStride;
7764 + ok = (yaffs_CheckpointWrite(dev,dev->chunkBits,nBytes) == nBytes);
7766 + return ok ? 1 : 0;
7770 +static int yaffs_ReadCheckpointDevice(yaffs_Device *dev)
7772 + yaffs_CheckpointDevice cp;
7774 + __u32 nBlocks = (dev->internalEndBlock - dev->internalStartBlock + 1);
7778 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
7782 + if(cp.structType != sizeof(cp))
7786 + yaffs_CheckpointDeviceToDevice(dev,&cp);
7788 + nBytes = nBlocks * sizeof(yaffs_BlockInfo);
7790 + ok = (yaffs_CheckpointRead(dev,dev->blockInfo,nBytes) == nBytes);
7794 + nBytes = nBlocks * dev->chunkBitmapStride;
7796 + ok = (yaffs_CheckpointRead(dev,dev->chunkBits,nBytes) == nBytes);
7798 + return ok ? 1 : 0;
7801 +static void yaffs_ObjectToCheckpointObject(yaffs_CheckpointObject *cp,
7802 + yaffs_Object *obj)
7805 + cp->objectId = obj->objectId;
7806 + cp->parentId = (obj->parent) ? obj->parent->objectId : 0;
7807 + cp->chunkId = obj->chunkId;
7808 + cp->variantType = obj->variantType;
7809 + cp->deleted = obj->deleted;
7810 + cp->softDeleted = obj->softDeleted;
7811 + cp->unlinked = obj->unlinked;
7812 + cp->fake = obj->fake;
7813 + cp->renameAllowed = obj->renameAllowed;
7814 + cp->unlinkAllowed = obj->unlinkAllowed;
7815 + cp->serial = obj->serial;
7816 + cp->nDataChunks = obj->nDataChunks;
7818 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7819 + cp->fileSizeOrEquivalentObjectId = obj->variant.fileVariant.fileSize;
7820 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7821 + cp->fileSizeOrEquivalentObjectId = obj->variant.hardLinkVariant.equivalentObjectId;
7824 +static void yaffs_CheckpointObjectToObject( yaffs_Object *obj,yaffs_CheckpointObject *cp)
7827 + yaffs_Object *parent;
7829 + obj->objectId = cp->objectId;
7832 + parent = yaffs_FindOrCreateObjectByNumber(
7835 + YAFFS_OBJECT_TYPE_DIRECTORY);
7840 + yaffs_AddObjectToDirectory(parent, obj);
7842 + obj->chunkId = cp->chunkId;
7843 + obj->variantType = cp->variantType;
7844 + obj->deleted = cp->deleted;
7845 + obj->softDeleted = cp->softDeleted;
7846 + obj->unlinked = cp->unlinked;
7847 + obj->fake = cp->fake;
7848 + obj->renameAllowed = cp->renameAllowed;
7849 + obj->unlinkAllowed = cp->unlinkAllowed;
7850 + obj->serial = cp->serial;
7851 + obj->nDataChunks = cp->nDataChunks;
7853 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE)
7854 + obj->variant.fileVariant.fileSize = cp->fileSizeOrEquivalentObjectId;
7855 + else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK)
7856 + obj->variant.hardLinkVariant.equivalentObjectId = cp->fileSizeOrEquivalentObjectId;
7858 + if(obj->objectId >= YAFFS_NOBJECT_BUCKETS)
7859 + obj->lazyLoaded = 1;
7864 +static int yaffs_CheckpointTnodeWorker(yaffs_Object * in, yaffs_Tnode * tn,
7865 + __u32 level, int chunkOffset)
7868 + yaffs_Device *dev = in->myDev;
7870 + int nTnodeBytes = (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8;
7875 + for (i = 0; i < YAFFS_NTNODES_INTERNAL && ok; i++){
7876 + if (tn->internal[i]) {
7877 + ok = yaffs_CheckpointTnodeWorker(in,
7880 + (chunkOffset<<YAFFS_TNODES_INTERNAL_BITS) + i);
7883 + } else if (level == 0) {
7884 + __u32 baseOffset = chunkOffset << YAFFS_TNODES_LEVEL0_BITS;
7885 + /* printf("write tnode at %d\n",baseOffset); */
7886 + ok = (yaffs_CheckpointWrite(dev,&baseOffset,sizeof(baseOffset)) == sizeof(baseOffset));
7888 + ok = (yaffs_CheckpointWrite(dev,tn,nTnodeBytes) == nTnodeBytes);
7896 +static int yaffs_WriteCheckpointTnodes(yaffs_Object *obj)
7898 + __u32 endMarker = ~0;
7901 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7902 + ok = yaffs_CheckpointTnodeWorker(obj,
7903 + obj->variant.fileVariant.top,
7904 + obj->variant.fileVariant.topLevel,
7907 + ok = (yaffs_CheckpointWrite(obj->myDev,&endMarker,sizeof(endMarker)) ==
7908 + sizeof(endMarker));
7911 + return ok ? 1 : 0;
7914 +static int yaffs_ReadCheckpointTnodes(yaffs_Object *obj)
7918 + yaffs_Device *dev = obj->myDev;
7919 + yaffs_FileStructure *fileStructPtr = &obj->variant.fileVariant;
7923 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7925 + while(ok && (~baseChunk)){
7927 + /* Read level 0 tnode */
7930 + /* printf("read tnode at %d\n",baseChunk); */
7931 + tn = yaffs_GetTnodeRaw(dev);
7933 + ok = (yaffs_CheckpointRead(dev,tn,(dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8) ==
7934 + (dev->tnodeWidth * YAFFS_NTNODES_LEVEL0)/8);
7939 + ok = yaffs_AddOrFindLevel0Tnode(dev,
7947 + ok = (yaffs_CheckpointRead(dev,&baseChunk,sizeof(baseChunk)) == sizeof(baseChunk));
7951 + T(YAFFS_TRACE_CHECKPOINT,(
7952 + TSTR("Checkpoint read tnodes %d records, last %d. ok %d" TENDSTR),
7953 + nread,baseChunk,ok));
7955 + return ok ? 1 : 0;
7959 +static int yaffs_WriteCheckpointObjects(yaffs_Device *dev)
7961 + yaffs_Object *obj;
7962 + yaffs_CheckpointObject cp;
7965 + struct list_head *lh;
7968 + /* Iterate through the objects in each hash entry,
7969 + * dumping them to the checkpointing stream.
7972 + for(i = 0; ok && i < YAFFS_NOBJECT_BUCKETS; i++){
7973 + list_for_each(lh, &dev->objectBucket[i].list) {
7975 + obj = list_entry(lh, yaffs_Object, hashLink);
7976 + if (!obj->deferedFree) {
7977 + yaffs_ObjectToCheckpointObject(&cp,obj);
7978 + cp.structType = sizeof(cp);
7980 + T(YAFFS_TRACE_CHECKPOINT,(
7981 + TSTR("Checkpoint write object %d parent %d type %d chunk %d obj addr %x" TENDSTR),
7982 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId,(unsigned) obj));
7984 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
7986 + if(ok && obj->variantType == YAFFS_OBJECT_TYPE_FILE){
7987 + ok = yaffs_WriteCheckpointTnodes(obj);
7994 + /* Dump end of list */
7995 + memset(&cp,0xFF,sizeof(yaffs_CheckpointObject));
7996 + cp.structType = sizeof(cp);
7999 + ok = (yaffs_CheckpointWrite(dev,&cp,sizeof(cp)) == sizeof(cp));
8001 + return ok ? 1 : 0;
8004 +static int yaffs_ReadCheckpointObjects(yaffs_Device *dev)
8006 + yaffs_Object *obj;
8007 + yaffs_CheckpointObject cp;
8010 + yaffs_Object *hardList = NULL;
8012 + while(ok && !done) {
8013 + ok = (yaffs_CheckpointRead(dev,&cp,sizeof(cp)) == sizeof(cp));
8014 + if(cp.structType != sizeof(cp)) {
8015 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("struct size %d instead of %d ok %d"TENDSTR),
8016 + cp.structType,sizeof(cp),ok));
8020 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("Checkpoint read object %d parent %d type %d chunk %d " TENDSTR),
8021 + cp.objectId,cp.parentId,cp.variantType,cp.chunkId));
8023 + if(ok && cp.objectId == ~0)
8026 + obj = yaffs_FindOrCreateObjectByNumber(dev,cp.objectId, cp.variantType);
8028 + yaffs_CheckpointObjectToObject(obj,&cp);
8029 + if(obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
8030 + ok = yaffs_ReadCheckpointTnodes(obj);
8031 + } else if(obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8032 + obj->hardLinks.next =
8033 + (struct list_head *)
8043 + yaffs_HardlinkFixup(dev,hardList);
8045 + return ok ? 1 : 0;
8048 +static int yaffs_WriteCheckpointSum(yaffs_Device *dev)
8050 + __u32 checkpointSum;
8053 + yaffs_GetCheckpointSum(dev,&checkpointSum);
8055 + ok = (yaffs_CheckpointWrite(dev,&checkpointSum,sizeof(checkpointSum)) == sizeof(checkpointSum));
8063 +static int yaffs_ReadCheckpointSum(yaffs_Device *dev)
8065 + __u32 checkpointSum0;
8066 + __u32 checkpointSum1;
8069 + yaffs_GetCheckpointSum(dev,&checkpointSum0);
8071 + ok = (yaffs_CheckpointRead(dev,&checkpointSum1,sizeof(checkpointSum1)) == sizeof(checkpointSum1));
8076 + if(checkpointSum0 != checkpointSum1)
8083 +static int yaffs_WriteCheckpointData(yaffs_Device *dev)
8088 + if(dev->skipCheckpointWrite || !dev->isYaffs2){
8089 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint write" TENDSTR)));
8094 + ok = yaffs_CheckpointOpen(dev,1);
8097 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8098 + ok = yaffs_WriteCheckpointValidityMarker(dev,1);
8101 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint device" TENDSTR)));
8102 + ok = yaffs_WriteCheckpointDevice(dev);
8105 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint objects" TENDSTR)));
8106 + ok = yaffs_WriteCheckpointObjects(dev);
8109 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("write checkpoint validity" TENDSTR)));
8110 + ok = yaffs_WriteCheckpointValidityMarker(dev,0);
8114 + ok = yaffs_WriteCheckpointSum(dev);
8118 + if(!yaffs_CheckpointClose(dev))
8122 + dev->isCheckpointed = 1;
8124 + dev->isCheckpointed = 0;
8126 + return dev->isCheckpointed;
8129 +static int yaffs_ReadCheckpointData(yaffs_Device *dev)
8133 + if(dev->skipCheckpointRead || !dev->isYaffs2){
8134 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("skipping checkpoint read" TENDSTR)));
8139 + ok = yaffs_CheckpointOpen(dev,0); /* open for read */
8142 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8143 + ok = yaffs_ReadCheckpointValidityMarker(dev,1);
8146 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint device" TENDSTR)));
8147 + ok = yaffs_ReadCheckpointDevice(dev);
8150 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint objects" TENDSTR)));
8151 + ok = yaffs_ReadCheckpointObjects(dev);
8154 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint validity" TENDSTR)));
8155 + ok = yaffs_ReadCheckpointValidityMarker(dev,0);
8159 + ok = yaffs_ReadCheckpointSum(dev);
8160 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("read checkpoint checksum %d" TENDSTR),ok));
8163 + if(!yaffs_CheckpointClose(dev))
8167 + dev->isCheckpointed = 1;
8169 + dev->isCheckpointed = 0;
8171 + return ok ? 1 : 0;
8175 +static void yaffs_InvalidateCheckpoint(yaffs_Device *dev)
8177 + if(dev->isCheckpointed ||
8178 + dev->blocksInCheckpoint > 0){
8179 + dev->isCheckpointed = 0;
8180 + yaffs_CheckpointInvalidateStream(dev);
8181 + if(dev->superBlock && dev->markSuperBlockDirty)
8182 + dev->markSuperBlockDirty(dev->superBlock);
8187 +int yaffs_CheckpointSave(yaffs_Device *dev)
8190 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("save entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8192 + yaffs_VerifyObjects(dev);
8193 + yaffs_VerifyBlocks(dev);
8194 + yaffs_VerifyFreeChunks(dev);
8196 + if(!dev->isCheckpointed) {
8197 + yaffs_InvalidateCheckpoint(dev);
8198 + yaffs_WriteCheckpointData(dev);
8201 + T(YAFFS_TRACE_ALWAYS,(TSTR("save exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8203 + return dev->isCheckpointed;
8206 +int yaffs_CheckpointRestore(yaffs_Device *dev)
8209 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore entry: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8211 + retval = yaffs_ReadCheckpointData(dev);
8213 + if(dev->isCheckpointed){
8214 + yaffs_VerifyObjects(dev);
8215 + yaffs_VerifyBlocks(dev);
8216 + yaffs_VerifyFreeChunks(dev);
8219 + T(YAFFS_TRACE_CHECKPOINT,(TSTR("restore exit: isCheckpointed %d"TENDSTR),dev->isCheckpointed));
8224 +/*--------------------- File read/write ------------------------
8225 + * Read and write have very similar structures.
8226 + * In general the read/write has three parts to it
8227 + * An incomplete chunk to start with (if the read/write is not chunk-aligned)
8228 + * Some complete chunks
8229 + * An incomplete chunk to end off with
8231 + * Curve-balls: the first chunk might also be the last chunk.
8234 +int yaffs_ReadDataFromFile(yaffs_Object * in, __u8 * buffer, loff_t offset,
8243 + yaffs_ChunkCache *cache;
8245 + yaffs_Device *dev;
8250 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8251 + //start = offset % dev->nDataBytesPerChunk;
8252 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8255 + /* OK now check for the curveball where the start and end are in
8258 + if ((start + n) < dev->nDataBytesPerChunk) {
8261 + nToCopy = dev->nDataBytesPerChunk - start;
8264 + cache = yaffs_FindChunkCache(in, chunk);
8266 + /* If the chunk is already in the cache or it is less than a whole chunk
8267 + * then use the cache (if there is caching)
8268 + * else bypass the cache.
8270 + if (cache || nToCopy != dev->nDataBytesPerChunk) {
8271 + if (dev->nShortOpCaches > 0) {
8273 + /* If we can't find the data in the cache, then load it up. */
8276 + cache = yaffs_GrabChunkCache(in->myDev);
8277 + cache->object = in;
8278 + cache->chunkId = chunk;
8280 + cache->locked = 0;
8281 + yaffs_ReadChunkDataFromObject(in, chunk,
8284 + cache->nBytes = 0;
8287 + yaffs_UseChunkCache(dev, cache, 0);
8289 + cache->locked = 1;
8291 +#ifdef CONFIG_YAFFS_WINCE
8292 + yfsd_UnlockYAFFS(TRUE);
8294 + memcpy(buffer, &cache->data[start], nToCopy);
8296 +#ifdef CONFIG_YAFFS_WINCE
8297 + yfsd_LockYAFFS(TRUE);
8299 + cache->locked = 0;
8301 + /* Read into the local buffer then copy..*/
8303 + __u8 *localBuffer =
8304 + yaffs_GetTempBuffer(dev, __LINE__);
8305 + yaffs_ReadChunkDataFromObject(in, chunk,
8307 +#ifdef CONFIG_YAFFS_WINCE
8308 + yfsd_UnlockYAFFS(TRUE);
8310 + memcpy(buffer, &localBuffer[start], nToCopy);
8312 +#ifdef CONFIG_YAFFS_WINCE
8313 + yfsd_LockYAFFS(TRUE);
8315 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8320 +#ifdef CONFIG_YAFFS_WINCE
8321 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8323 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8324 + * This is because we otherwise screw up WinCE's memory mapper
8326 + yaffs_ReadChunkDataFromObject(in, chunk, localBuffer);
8328 +#ifdef CONFIG_YAFFS_WINCE
8329 + yfsd_UnlockYAFFS(TRUE);
8331 + memcpy(buffer, localBuffer, dev->nDataBytesPerChunk);
8333 +#ifdef CONFIG_YAFFS_WINCE
8334 + yfsd_LockYAFFS(TRUE);
8335 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8339 + /* A full chunk. Read directly into the supplied buffer. */
8340 + yaffs_ReadChunkDataFromObject(in, chunk, buffer);
8345 + offset += nToCopy;
8346 + buffer += nToCopy;
8354 +int yaffs_WriteDataToFile(yaffs_Object * in, const __u8 * buffer, loff_t offset,
8355 + int nBytes, int writeThrough)
8364 + int startOfWrite = offset;
8365 + int chunkWritten = 0;
8368 + yaffs_Device *dev;
8372 + while (n > 0 && chunkWritten >= 0) {
8373 + //chunk = offset / dev->nDataBytesPerChunk + 1;
8374 + //start = offset % dev->nDataBytesPerChunk;
8375 + yaffs_AddrToChunk(dev,offset,&chunk,&start);
8378 + /* OK now check for the curveball where the start and end are in
8382 + if ((start + n) < dev->nDataBytesPerChunk) {
8385 + /* Now folks, to calculate how many bytes to write back....
8386 + * If we're overwriting and not writing to then end of file then
8387 + * we need to write back as much as was there before.
8391 + in->variant.fileVariant.fileSize -
8392 + ((chunk - 1) * dev->nDataBytesPerChunk);
8394 + if (nBytesRead > dev->nDataBytesPerChunk) {
8395 + nBytesRead = dev->nDataBytesPerChunk;
8400 + (start + n)) ? nBytesRead : (start + n);
8403 + nToCopy = dev->nDataBytesPerChunk - start;
8404 + nToWriteBack = dev->nDataBytesPerChunk;
8407 + if (nToCopy != dev->nDataBytesPerChunk) {
8408 + /* An incomplete start or end chunk (or maybe both start and end chunk) */
8409 + if (dev->nShortOpCaches > 0) {
8410 + yaffs_ChunkCache *cache;
8411 + /* If we can't find the data in the cache, then load the cache */
8412 + cache = yaffs_FindChunkCache(in, chunk);
8415 + && yaffs_CheckSpaceForAllocation(in->
8417 + cache = yaffs_GrabChunkCache(in->myDev);
8418 + cache->object = in;
8419 + cache->chunkId = chunk;
8421 + cache->locked = 0;
8422 + yaffs_ReadChunkDataFromObject(in, chunk,
8428 + !yaffs_CheckSpaceForAllocation(in->myDev)){
8429 + /* Drop the cache if it was a read cache item and
8430 + * no space check has been made for it.
8436 + yaffs_UseChunkCache(dev, cache, 1);
8437 + cache->locked = 1;
8438 +#ifdef CONFIG_YAFFS_WINCE
8439 + yfsd_UnlockYAFFS(TRUE);
8442 + memcpy(&cache->data[start], buffer,
8445 +#ifdef CONFIG_YAFFS_WINCE
8446 + yfsd_LockYAFFS(TRUE);
8448 + cache->locked = 0;
8449 + cache->nBytes = nToWriteBack;
8451 + if (writeThrough) {
8453 + yaffs_WriteChunkDataToObject
8456 + cache->data, cache->nBytes,
8462 + chunkWritten = -1; /* fail the write */
8465 + /* An incomplete start or end chunk (or maybe both start and end chunk)
8466 + * Read into the local buffer then copy, then copy over and write back.
8469 + __u8 *localBuffer =
8470 + yaffs_GetTempBuffer(dev, __LINE__);
8472 + yaffs_ReadChunkDataFromObject(in, chunk,
8475 +#ifdef CONFIG_YAFFS_WINCE
8476 + yfsd_UnlockYAFFS(TRUE);
8479 + memcpy(&localBuffer[start], buffer, nToCopy);
8481 +#ifdef CONFIG_YAFFS_WINCE
8482 + yfsd_LockYAFFS(TRUE);
8485 + yaffs_WriteChunkDataToObject(in, chunk,
8490 + yaffs_ReleaseTempBuffer(dev, localBuffer,
8497 +#ifdef CONFIG_YAFFS_WINCE
8498 + /* Under WinCE can't do direct transfer. Need to use a local buffer.
8499 + * This is because we otherwise screw up WinCE's memory mapper
8501 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8502 +#ifdef CONFIG_YAFFS_WINCE
8503 + yfsd_UnlockYAFFS(TRUE);
8505 + memcpy(localBuffer, buffer, dev->nDataBytesPerChunk);
8506 +#ifdef CONFIG_YAFFS_WINCE
8507 + yfsd_LockYAFFS(TRUE);
8510 + yaffs_WriteChunkDataToObject(in, chunk, localBuffer,
8511 + dev->nDataBytesPerChunk,
8513 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8515 + /* A full chunk. Write directly from the supplied buffer. */
8517 + yaffs_WriteChunkDataToObject(in, chunk, buffer,
8518 + dev->nDataBytesPerChunk,
8521 + /* Since we've overwritten the cached data, we better invalidate it. */
8522 + yaffs_InvalidateChunkCache(in, chunk);
8525 + if (chunkWritten >= 0) {
8527 + offset += nToCopy;
8528 + buffer += nToCopy;
8534 + /* Update file object */
8536 + if ((startOfWrite + nDone) > in->variant.fileVariant.fileSize) {
8537 + in->variant.fileVariant.fileSize = (startOfWrite + nDone);
8546 +/* ---------------------- File resizing stuff ------------------ */
8548 +static void yaffs_PruneResizedChunks(yaffs_Object * in, int newSize)
8551 + yaffs_Device *dev = in->myDev;
8552 + int oldFileSize = in->variant.fileVariant.fileSize;
8554 + int lastDel = 1 + (oldFileSize - 1) / dev->nDataBytesPerChunk;
8556 + int startDel = 1 + (newSize + dev->nDataBytesPerChunk - 1) /
8557 + dev->nDataBytesPerChunk;
8561 + /* Delete backwards so that we don't end up with holes if
8562 + * power is lost part-way through the operation.
8564 + for (i = lastDel; i >= startDel; i--) {
8565 + /* NB this could be optimised somewhat,
8566 + * eg. could retrieve the tags and write them without
8567 + * using yaffs_DeleteChunk
8570 + chunkId = yaffs_FindAndDeleteChunkInFile(in, i, NULL);
8571 + if (chunkId > 0) {
8573 + (dev->internalStartBlock * dev->nChunksPerBlock)
8575 + ((dev->internalEndBlock +
8576 + 1) * dev->nChunksPerBlock)) {
8577 + T(YAFFS_TRACE_ALWAYS,
8578 + (TSTR("Found daft chunkId %d for %d" TENDSTR),
8581 + in->nDataChunks--;
8582 + yaffs_DeleteChunk(dev, chunkId, 1, __LINE__);
8589 +int yaffs_ResizeFile(yaffs_Object * in, loff_t newSize)
8592 + int oldFileSize = in->variant.fileVariant.fileSize;
8593 + int newSizeOfPartialChunk;
8594 + int newFullChunks;
8596 + yaffs_Device *dev = in->myDev;
8598 + yaffs_AddrToChunk(dev, newSize, &newFullChunks, &newSizeOfPartialChunk);
8600 + yaffs_FlushFilesChunkCache(in);
8601 + yaffs_InvalidateWholeChunkCache(in);
8603 + yaffs_CheckGarbageCollection(dev);
8605 + if (in->variantType != YAFFS_OBJECT_TYPE_FILE) {
8606 + return yaffs_GetFileSize(in);
8609 + if (newSize == oldFileSize) {
8610 + return oldFileSize;
8613 + if (newSize < oldFileSize) {
8615 + yaffs_PruneResizedChunks(in, newSize);
8617 + if (newSizeOfPartialChunk != 0) {
8618 + int lastChunk = 1 + newFullChunks;
8620 + __u8 *localBuffer = yaffs_GetTempBuffer(dev, __LINE__);
8622 + /* Got to read and rewrite the last chunk with its new size and zero pad */
8623 + yaffs_ReadChunkDataFromObject(in, lastChunk,
8626 + memset(localBuffer + newSizeOfPartialChunk, 0,
8627 + dev->nDataBytesPerChunk - newSizeOfPartialChunk);
8629 + yaffs_WriteChunkDataToObject(in, lastChunk, localBuffer,
8630 + newSizeOfPartialChunk, 1);
8632 + yaffs_ReleaseTempBuffer(dev, localBuffer, __LINE__);
8635 + in->variant.fileVariant.fileSize = newSize;
8637 + yaffs_PruneFileStructure(dev, &in->variant.fileVariant);
8639 + /* newsSize > oldFileSize */
8640 + in->variant.fileVariant.fileSize = newSize;
8645 + /* Write a new object header.
8646 + * show we've shrunk the file, if need be
8647 + * Do this only if the file is not in the deleted directories.
8649 + if (in->parent->objectId != YAFFS_OBJECTID_UNLINKED &&
8650 + in->parent->objectId != YAFFS_OBJECTID_DELETED) {
8651 + yaffs_UpdateObjectHeader(in, NULL, 0,
8652 + (newSize < oldFileSize) ? 1 : 0, 0);
8658 +loff_t yaffs_GetFileSize(yaffs_Object * obj)
8660 + obj = yaffs_GetEquivalentObject(obj);
8662 + switch (obj->variantType) {
8663 + case YAFFS_OBJECT_TYPE_FILE:
8664 + return obj->variant.fileVariant.fileSize;
8665 + case YAFFS_OBJECT_TYPE_SYMLINK:
8666 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
8674 +int yaffs_FlushFile(yaffs_Object * in, int updateTime)
8678 + yaffs_FlushFilesChunkCache(in);
8680 +#ifdef CONFIG_YAFFS_WINCE
8681 + yfsd_WinFileTimeNow(in->win_mtime);
8684 + in->yst_mtime = Y_CURRENT_TIME;
8690 + (yaffs_UpdateObjectHeader(in, NULL, 0, 0, 0) >=
8691 + 0) ? YAFFS_OK : YAFFS_FAIL;
8693 + retVal = YAFFS_OK;
8700 +static int yaffs_DoGenericObjectDeletion(yaffs_Object * in)
8703 + /* First off, invalidate the file's data in the cache, without flushing. */
8704 + yaffs_InvalidateWholeChunkCache(in);
8706 + if (in->myDev->isYaffs2 && (in->parent != in->myDev->deletedDir)) {
8707 + /* Move to the unlinked directory so we have a record that it was deleted. */
8708 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,"deleted", 0, 0);
8712 + yaffs_RemoveObjectFromDirectory(in);
8713 + yaffs_DeleteChunk(in->myDev, in->chunkId, 1, __LINE__);
8716 + yaffs_FreeObject(in);
8721 +/* yaffs_DeleteFile deletes the whole file data
8722 + * and the inode associated with the file.
8723 + * It does not delete the links associated with the file.
8725 +static int yaffs_UnlinkFile(yaffs_Object * in)
8729 + int immediateDeletion = 0;
8733 + if (!in->myInode) {
8734 + immediateDeletion = 1;
8738 + if (in->inUse <= 0) {
8739 + immediateDeletion = 1;
8743 + if (immediateDeletion) {
8745 + yaffs_ChangeObjectName(in, in->myDev->deletedDir,
8747 + T(YAFFS_TRACE_TRACING,
8748 + (TSTR("yaffs: immediate deletion of file %d" TENDSTR),
8751 + in->myDev->nDeletedFiles++;
8752 + if (0 && in->myDev->isYaffs2) {
8753 + yaffs_ResizeFile(in, 0);
8755 + yaffs_SoftDeleteFile(in);
8758 + yaffs_ChangeObjectName(in, in->myDev->unlinkedDir,
8759 + "unlinked", 0, 0);
8766 +int yaffs_DeleteFile(yaffs_Object * in)
8768 + int retVal = YAFFS_OK;
8770 + if (in->nDataChunks > 0) {
8771 + /* Use soft deletion if there is data in the file */
8772 + if (!in->unlinked) {
8773 + retVal = yaffs_UnlinkFile(in);
8775 + if (retVal == YAFFS_OK && in->unlinked && !in->deleted) {
8777 + in->myDev->nDeletedFiles++;
8778 + yaffs_SoftDeleteFile(in);
8780 + return in->deleted ? YAFFS_OK : YAFFS_FAIL;
8782 + /* The file has no data chunks so we toss it immediately */
8783 + yaffs_FreeTnode(in->myDev, in->variant.fileVariant.top);
8784 + in->variant.fileVariant.top = NULL;
8785 + yaffs_DoGenericObjectDeletion(in);
8791 +static int yaffs_DeleteDirectory(yaffs_Object * in)
8793 + /* First check that the directory is empty. */
8794 + if (list_empty(&in->variant.directoryVariant.children)) {
8795 + return yaffs_DoGenericObjectDeletion(in);
8798 + return YAFFS_FAIL;
8802 +static int yaffs_DeleteSymLink(yaffs_Object * in)
8804 + YFREE(in->variant.symLinkVariant.alias);
8806 + return yaffs_DoGenericObjectDeletion(in);
8809 +static int yaffs_DeleteHardLink(yaffs_Object * in)
8811 + /* remove this hardlink from the list assocaited with the equivalent
8814 + list_del(&in->hardLinks);
8815 + return yaffs_DoGenericObjectDeletion(in);
8818 +static void yaffs_DestroyObject(yaffs_Object * obj)
8820 + switch (obj->variantType) {
8821 + case YAFFS_OBJECT_TYPE_FILE:
8822 + yaffs_DeleteFile(obj);
8824 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8825 + yaffs_DeleteDirectory(obj);
8827 + case YAFFS_OBJECT_TYPE_SYMLINK:
8828 + yaffs_DeleteSymLink(obj);
8830 + case YAFFS_OBJECT_TYPE_HARDLINK:
8831 + yaffs_DeleteHardLink(obj);
8833 + case YAFFS_OBJECT_TYPE_SPECIAL:
8834 + yaffs_DoGenericObjectDeletion(obj);
8836 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8837 + break; /* should not happen. */
8841 +static int yaffs_UnlinkWorker(yaffs_Object * obj)
8844 + if (obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
8845 + return yaffs_DeleteHardLink(obj);
8846 + } else if (!list_empty(&obj->hardLinks)) {
8847 + /* Curve ball: We're unlinking an object that has a hardlink.
8849 + * This problem arises because we are not strictly following
8850 + * The Linux link/inode model.
8852 + * We can't really delete the object.
8853 + * Instead, we do the following:
8854 + * - Select a hardlink.
8855 + * - Unhook it from the hard links
8856 + * - Unhook it from its parent directory (so that the rename can work)
8857 + * - Rename the object to the hardlink's name.
8858 + * - Delete the hardlink
8863 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
8865 + hl = list_entry(obj->hardLinks.next, yaffs_Object, hardLinks);
8867 + list_del_init(&hl->hardLinks);
8868 + list_del_init(&hl->siblings);
8870 + yaffs_GetObjectName(hl, name, YAFFS_MAX_NAME_LENGTH + 1);
8872 + retVal = yaffs_ChangeObjectName(obj, hl->parent, name, 0, 0);
8874 + if (retVal == YAFFS_OK) {
8875 + retVal = yaffs_DoGenericObjectDeletion(hl);
8880 + switch (obj->variantType) {
8881 + case YAFFS_OBJECT_TYPE_FILE:
8882 + return yaffs_UnlinkFile(obj);
8884 + case YAFFS_OBJECT_TYPE_DIRECTORY:
8885 + return yaffs_DeleteDirectory(obj);
8887 + case YAFFS_OBJECT_TYPE_SYMLINK:
8888 + return yaffs_DeleteSymLink(obj);
8890 + case YAFFS_OBJECT_TYPE_SPECIAL:
8891 + return yaffs_DoGenericObjectDeletion(obj);
8893 + case YAFFS_OBJECT_TYPE_HARDLINK:
8894 + case YAFFS_OBJECT_TYPE_UNKNOWN:
8896 + return YAFFS_FAIL;
8902 +static int yaffs_UnlinkObject( yaffs_Object *obj)
8905 + if (obj && obj->unlinkAllowed) {
8906 + return yaffs_UnlinkWorker(obj);
8909 + return YAFFS_FAIL;
8912 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name)
8914 + yaffs_Object *obj;
8916 + obj = yaffs_FindObjectByName(dir, name);
8917 + return yaffs_UnlinkObject(obj);
8920 +/*----------------------- Initialisation Scanning ---------------------- */
8922 +static void yaffs_HandleShadowedObject(yaffs_Device * dev, int objId,
8923 + int backwardScanning)
8925 + yaffs_Object *obj;
8927 + if (!backwardScanning) {
8928 + /* Handle YAFFS1 forward scanning case
8929 + * For YAFFS1 we always do the deletion
8933 + /* Handle YAFFS2 case (backward scanning)
8934 + * If the shadowed object exists then ignore.
8936 + if (yaffs_FindObjectByNumber(dev, objId)) {
8941 + /* Let's create it (if it does not exist) assuming it is a file so that it can do shrinking etc.
8942 + * We put it in unlinked dir to be cleaned up after the scanning
8945 + yaffs_FindOrCreateObjectByNumber(dev, objId,
8946 + YAFFS_OBJECT_TYPE_FILE);
8947 + yaffs_AddObjectToDirectory(dev->unlinkedDir, obj);
8948 + obj->variant.fileVariant.shrinkSize = 0;
8949 + obj->valid = 1; /* So that we don't read any other info for this file */
8956 +} yaffs_BlockIndex;
8959 +static void yaffs_HardlinkFixup(yaffs_Device *dev, yaffs_Object *hardList)
8964 + while (hardList) {
8966 + hardList = (yaffs_Object *) (hardList->hardLinks.next);
8968 + in = yaffs_FindObjectByNumber(dev,
8969 + hl->variant.hardLinkVariant.
8970 + equivalentObjectId);
8973 + /* Add the hardlink pointers */
8974 + hl->variant.hardLinkVariant.equivalentObject = in;
8975 + list_add(&hl->hardLinks, &in->hardLinks);
8977 + /* Todo Need to report/handle this better.
8978 + * Got a problem... hardlink to a non-existant object
8980 + hl->variant.hardLinkVariant.equivalentObject = NULL;
8981 + INIT_LIST_HEAD(&hl->hardLinks);
8993 +static int ybicmp(const void *a, const void *b){
8994 + register int aseq = ((yaffs_BlockIndex *)a)->seq;
8995 + register int bseq = ((yaffs_BlockIndex *)b)->seq;
8996 + register int ablock = ((yaffs_BlockIndex *)a)->block;
8997 + register int bblock = ((yaffs_BlockIndex *)b)->block;
8998 + if( aseq == bseq )
8999 + return ablock - bblock;
9001 + return aseq - bseq;
9005 +static int yaffs_Scan(yaffs_Device * dev)
9007 + yaffs_ExtendedTags tags;
9009 + int blockIterator;
9010 + int startIterator;
9012 + int nBlocksToScan = 0;
9018 + yaffs_BlockState state;
9019 + yaffs_Object *hardList = NULL;
9020 + yaffs_BlockInfo *bi;
9021 + int sequenceNumber;
9022 + yaffs_ObjectHeader *oh;
9024 + yaffs_Object *parent;
9025 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9027 + int alloc_failed = 0;
9032 + yaffs_BlockIndex *blockIndex = NULL;
9034 + if (dev->isYaffs2) {
9035 + T(YAFFS_TRACE_SCAN,
9036 + (TSTR("yaffs_Scan is not for YAFFS2!" TENDSTR)));
9037 + return YAFFS_FAIL;
9040 + //TODO Throw all the yaffs2 stuuf out of yaffs_Scan since it is only for yaffs1 format.
9042 + T(YAFFS_TRACE_SCAN,
9043 + (TSTR("yaffs_Scan starts intstartblk %d intendblk %d..." TENDSTR),
9044 + dev->internalStartBlock, dev->internalEndBlock));
9046 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9048 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9050 + if (dev->isYaffs2) {
9051 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9053 + return YAFFS_FAIL;
9056 + /* Scan all the blocks to determine their state */
9057 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9058 + bi = yaffs_GetBlockInfo(dev, blk);
9059 + yaffs_ClearChunkBits(dev, blk);
9060 + bi->pagesInUse = 0;
9061 + bi->softDeletions = 0;
9063 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9065 + bi->blockState = state;
9066 + bi->sequenceNumber = sequenceNumber;
9068 + T(YAFFS_TRACE_SCAN_DEBUG,
9069 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9070 + state, sequenceNumber));
9072 + if (state == YAFFS_BLOCK_STATE_DEAD) {
9073 + T(YAFFS_TRACE_BAD_BLOCKS,
9074 + (TSTR("block %d is bad" TENDSTR), blk));
9075 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9076 + T(YAFFS_TRACE_SCAN_DEBUG,
9077 + (TSTR("Block empty " TENDSTR)));
9078 + dev->nErasedBlocks++;
9079 + dev->nFreeChunks += dev->nChunksPerBlock;
9080 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9082 + /* Determine the highest sequence number */
9083 + if (dev->isYaffs2 &&
9084 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9085 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9087 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9088 + blockIndex[nBlocksToScan].block = blk;
9092 + if (sequenceNumber >= dev->sequenceNumber) {
9093 + dev->sequenceNumber = sequenceNumber;
9095 + } else if (dev->isYaffs2) {
9096 + /* TODO: Nasty sequence number! */
9097 + T(YAFFS_TRACE_SCAN,
9099 + ("Block scanning block %d has bad sequence number %d"
9100 + TENDSTR), blk, sequenceNumber));
9106 + /* Sort the blocks
9107 + * Dungy old bubble sort for now...
9109 + if (dev->isYaffs2) {
9110 + yaffs_BlockIndex temp;
9114 + for (i = 0; i < nBlocksToScan; i++)
9115 + for (j = i + 1; j < nBlocksToScan; j++)
9116 + if (blockIndex[i].seq > blockIndex[j].seq) {
9117 + temp = blockIndex[j];
9118 + blockIndex[j] = blockIndex[i];
9119 + blockIndex[i] = temp;
9123 + /* Now scan the blocks looking at the data. */
9124 + if (dev->isYaffs2) {
9125 + startIterator = 0;
9126 + endIterator = nBlocksToScan - 1;
9127 + T(YAFFS_TRACE_SCAN_DEBUG,
9128 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9130 + startIterator = dev->internalStartBlock;
9131 + endIterator = dev->internalEndBlock;
9134 + /* For each block.... */
9135 + for (blockIterator = startIterator; !alloc_failed && blockIterator <= endIterator;
9136 + blockIterator++) {
9138 + if (dev->isYaffs2) {
9139 + /* get the block to scan in the correct order */
9140 + blk = blockIndex[blockIterator].block;
9142 + blk = blockIterator;
9145 + bi = yaffs_GetBlockInfo(dev, blk);
9146 + state = bi->blockState;
9150 + /* For each chunk in each block that needs scanning....*/
9151 + for (c = 0; !alloc_failed && c < dev->nChunksPerBlock &&
9152 + state == YAFFS_BLOCK_STATE_NEEDS_SCANNING; c++) {
9153 + /* Read the tags and decide what to do */
9154 + chunk = blk * dev->nChunksPerBlock + c;
9156 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9159 + /* Let's have a good look at this chunk... */
9161 + if (!dev->isYaffs2 && tags.chunkDeleted) {
9166 + dev->nFreeChunks++;
9167 + /*T((" %d %d deleted\n",blk,c)); */
9168 + } else if (!tags.chunkUsed) {
9169 + /* An unassigned chunk in the block
9170 + * This means that either the block is empty or
9171 + * this is the one being allocated from
9175 + /* We're looking at the first chunk in the block so the block is unused */
9176 + state = YAFFS_BLOCK_STATE_EMPTY;
9177 + dev->nErasedBlocks++;
9179 + /* this is the block being allocated from */
9180 + T(YAFFS_TRACE_SCAN,
9182 + (" Allocating from %d %d" TENDSTR),
9184 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9185 + dev->allocationBlock = blk;
9186 + dev->allocationPage = c;
9187 + dev->allocationBlockFinder = blk;
9188 + /* Set it to here to encourage the allocator to go forth from here. */
9190 + /* Yaffs2 sanity check:
9191 + * This should be the one with the highest sequence number
9194 + && (dev->sequenceNumber !=
9195 + bi->sequenceNumber)) {
9196 + T(YAFFS_TRACE_ALWAYS,
9198 + ("yaffs: Allocation block %d was not highest sequence id:"
9199 + " block seq = %d, dev seq = %d"
9200 + TENDSTR), blk,bi->sequenceNumber,dev->sequenceNumber));
9204 + dev->nFreeChunks += (dev->nChunksPerBlock - c);
9205 + } else if (tags.chunkId > 0) {
9206 + /* chunkId > 0 so it is a data chunk... */
9207 + unsigned int endpos;
9209 + yaffs_SetChunkBit(dev, blk, c);
9212 + in = yaffs_FindOrCreateObjectByNumber(dev,
9215 + YAFFS_OBJECT_TYPE_FILE);
9216 + /* PutChunkIntoFile checks for a clash (two data chunks with
9217 + * the same chunkId).
9224 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId, chunk,1))
9229 + (tags.chunkId - 1) * dev->nDataBytesPerChunk +
9232 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9233 + && in->variant.fileVariant.scannedFileSize <
9235 + in->variant.fileVariant.
9236 + scannedFileSize = endpos;
9237 + if (!dev->useHeaderFileSize) {
9238 + in->variant.fileVariant.
9240 + in->variant.fileVariant.
9245 + /* T((" %d %d data %d %d\n",blk,c,tags.objectId,tags.chunkId)); */
9247 + /* chunkId == 0, so it is an ObjectHeader.
9248 + * Thus, we read in the object header and make the object
9250 + yaffs_SetChunkBit(dev, blk, c);
9253 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk,
9257 + oh = (yaffs_ObjectHeader *) chunkData;
9259 + in = yaffs_FindObjectByNumber(dev,
9261 + if (in && in->variantType != oh->type) {
9262 + /* This should not happen, but somehow
9263 + * Wev'e ended up with an objectId that has been reused but not yet
9264 + * deleted, and worse still it has changed type. Delete the old object.
9267 + yaffs_DestroyObject(in);
9272 + in = yaffs_FindOrCreateObjectByNumber(dev,
9280 + if (in && oh->shadowsObject > 0) {
9281 + yaffs_HandleShadowedObject(dev,
9287 + if (in && in->valid) {
9288 + /* We have already filled this one. We have a duplicate and need to resolve it. */
9290 + unsigned existingSerial = in->serial;
9291 + unsigned newSerial = tags.serialNumber;
9293 + if (dev->isYaffs2 ||
9294 + ((existingSerial + 1) & 3) ==
9296 + /* Use new one - destroy the exisiting one */
9297 + yaffs_DeleteChunk(dev,
9302 + /* Use existing - destroy this one. */
9303 + yaffs_DeleteChunk(dev, chunk, 1,
9308 + if (in && !in->valid &&
9309 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9310 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND)) {
9311 + /* We only load some info, don't fiddle with directory structure */
9313 + in->variantType = oh->type;
9315 + in->yst_mode = oh->yst_mode;
9316 +#ifdef CONFIG_YAFFS_WINCE
9317 + in->win_atime[0] = oh->win_atime[0];
9318 + in->win_ctime[0] = oh->win_ctime[0];
9319 + in->win_mtime[0] = oh->win_mtime[0];
9320 + in->win_atime[1] = oh->win_atime[1];
9321 + in->win_ctime[1] = oh->win_ctime[1];
9322 + in->win_mtime[1] = oh->win_mtime[1];
9324 + in->yst_uid = oh->yst_uid;
9325 + in->yst_gid = oh->yst_gid;
9326 + in->yst_atime = oh->yst_atime;
9327 + in->yst_mtime = oh->yst_mtime;
9328 + in->yst_ctime = oh->yst_ctime;
9329 + in->yst_rdev = oh->yst_rdev;
9331 + in->chunkId = chunk;
9333 + } else if (in && !in->valid) {
9334 + /* we need to load this info */
9337 + in->variantType = oh->type;
9339 + in->yst_mode = oh->yst_mode;
9340 +#ifdef CONFIG_YAFFS_WINCE
9341 + in->win_atime[0] = oh->win_atime[0];
9342 + in->win_ctime[0] = oh->win_ctime[0];
9343 + in->win_mtime[0] = oh->win_mtime[0];
9344 + in->win_atime[1] = oh->win_atime[1];
9345 + in->win_ctime[1] = oh->win_ctime[1];
9346 + in->win_mtime[1] = oh->win_mtime[1];
9348 + in->yst_uid = oh->yst_uid;
9349 + in->yst_gid = oh->yst_gid;
9350 + in->yst_atime = oh->yst_atime;
9351 + in->yst_mtime = oh->yst_mtime;
9352 + in->yst_ctime = oh->yst_ctime;
9353 + in->yst_rdev = oh->yst_rdev;
9355 + in->chunkId = chunk;
9357 + yaffs_SetObjectName(in, oh->name);
9360 + /* directory stuff...
9361 + * hook up to parent
9365 + yaffs_FindOrCreateObjectByNumber
9366 + (dev, oh->parentObjectId,
9367 + YAFFS_OBJECT_TYPE_DIRECTORY);
9368 + if (parent->variantType ==
9369 + YAFFS_OBJECT_TYPE_UNKNOWN) {
9370 + /* Set up as a directory */
9371 + parent->variantType =
9372 + YAFFS_OBJECT_TYPE_DIRECTORY;
9373 + INIT_LIST_HEAD(&parent->variant.
9376 + } else if (parent->variantType !=
9377 + YAFFS_OBJECT_TYPE_DIRECTORY)
9379 + /* Hoosterman, another problem....
9380 + * We're trying to use a non-directory as a directory
9383 + T(YAFFS_TRACE_ERROR,
9385 + ("yaffs tragedy: attempting to use non-directory as"
9386 + " a directory in scan. Put in lost+found."
9388 + parent = dev->lostNFoundDir;
9391 + yaffs_AddObjectToDirectory(parent, in);
9393 + if (0 && (parent == dev->deletedDir ||
9394 + parent == dev->unlinkedDir)) {
9395 + in->deleted = 1; /* If it is unlinked at start up then it wants deleting */
9396 + dev->nDeletedFiles++;
9398 + /* Note re hardlinks.
9399 + * Since we might scan a hardlink before its equivalent object is scanned
9400 + * we put them all in a list.
9401 + * After scanning is complete, we should have all the objects, so we run through this
9402 + * list and fix up all the chains.
9405 + switch (in->variantType) {
9406 + case YAFFS_OBJECT_TYPE_UNKNOWN:
9407 + /* Todo got a problem */
9409 + case YAFFS_OBJECT_TYPE_FILE:
9411 + && oh->isShrink) {
9412 + /* Prune back the shrunken chunks */
9413 + yaffs_PruneResizedChunks
9414 + (in, oh->fileSize);
9415 + /* Mark the block as having a shrinkHeader */
9416 + bi->hasShrinkHeader = 1;
9419 + if (dev->useHeaderFileSize)
9421 + in->variant.fileVariant.
9426 + case YAFFS_OBJECT_TYPE_HARDLINK:
9427 + in->variant.hardLinkVariant.
9428 + equivalentObjectId =
9429 + oh->equivalentObjectId;
9430 + in->hardLinks.next =
9431 + (struct list_head *)
9435 + case YAFFS_OBJECT_TYPE_DIRECTORY:
9438 + case YAFFS_OBJECT_TYPE_SPECIAL:
9441 + case YAFFS_OBJECT_TYPE_SYMLINK:
9442 + in->variant.symLinkVariant.alias =
9443 + yaffs_CloneString(oh->alias);
9444 + if(!in->variant.symLinkVariant.alias)
9449 + if (parent == dev->deletedDir) {
9450 + yaffs_DestroyObject(in);
9451 + bi->hasShrinkHeader = 1;
9457 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9458 + /* If we got this far while scanning, then the block is fully allocated.*/
9459 + state = YAFFS_BLOCK_STATE_FULL;
9462 + bi->blockState = state;
9464 + /* Now let's see if it was dirty */
9465 + if (bi->pagesInUse == 0 &&
9466 + !bi->hasShrinkHeader &&
9467 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
9468 + yaffs_BlockBecameDirty(dev, blk);
9474 + YFREE(blockIndex);
9478 + /* Ok, we've done all the scanning.
9479 + * Fix up the hard link chains.
9480 + * We should now have scanned all the objects, now it's time to add these
9484 + yaffs_HardlinkFixup(dev,hardList);
9486 + /* Handle the unlinked files. Since they were left in an unlinked state we should
9487 + * just delete them.
9490 + struct list_head *i;
9491 + struct list_head *n;
9494 + /* Soft delete all the unlinked files */
9495 + list_for_each_safe(i, n,
9496 + &dev->unlinkedDir->variant.directoryVariant.
9499 + l = list_entry(i, yaffs_Object, siblings);
9500 + yaffs_DestroyObject(l);
9505 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
9508 + return YAFFS_FAIL;
9511 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_Scan ends" TENDSTR)));
9517 +static void yaffs_CheckObjectDetailsLoaded(yaffs_Object *in)
9520 + yaffs_ObjectHeader *oh;
9521 + yaffs_Device *dev = in->myDev;
9522 + yaffs_ExtendedTags tags;
9524 + int alloc_failed = 0;
9530 + T(YAFFS_TRACE_SCAN,(TSTR("details for object %d %s loaded" TENDSTR),
9532 + in->lazyLoaded ? "not yet" : "already"));
9535 + if(in->lazyLoaded){
9536 + in->lazyLoaded = 0;
9537 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9539 + result = yaffs_ReadChunkWithTagsFromNAND(dev,in->chunkId,chunkData,&tags);
9540 + oh = (yaffs_ObjectHeader *) chunkData;
9542 + in->yst_mode = oh->yst_mode;
9543 +#ifdef CONFIG_YAFFS_WINCE
9544 + in->win_atime[0] = oh->win_atime[0];
9545 + in->win_ctime[0] = oh->win_ctime[0];
9546 + in->win_mtime[0] = oh->win_mtime[0];
9547 + in->win_atime[1] = oh->win_atime[1];
9548 + in->win_ctime[1] = oh->win_ctime[1];
9549 + in->win_mtime[1] = oh->win_mtime[1];
9551 + in->yst_uid = oh->yst_uid;
9552 + in->yst_gid = oh->yst_gid;
9553 + in->yst_atime = oh->yst_atime;
9554 + in->yst_mtime = oh->yst_mtime;
9555 + in->yst_ctime = oh->yst_ctime;
9556 + in->yst_rdev = oh->yst_rdev;
9559 + yaffs_SetObjectName(in, oh->name);
9561 + if(in->variantType == YAFFS_OBJECT_TYPE_SYMLINK){
9562 + in->variant.symLinkVariant.alias =
9563 + yaffs_CloneString(oh->alias);
9564 + if(!in->variant.symLinkVariant.alias)
9565 + alloc_failed = 1; /* Not returned to caller */
9568 + yaffs_ReleaseTempBuffer(dev,chunkData, __LINE__);
9572 +static int yaffs_ScanBackwards(yaffs_Device * dev)
9574 + yaffs_ExtendedTags tags;
9576 + int blockIterator;
9577 + int startIterator;
9579 + int nBlocksToScan = 0;
9585 + yaffs_BlockState state;
9586 + yaffs_Object *hardList = NULL;
9587 + yaffs_BlockInfo *bi;
9588 + int sequenceNumber;
9589 + yaffs_ObjectHeader *oh;
9591 + yaffs_Object *parent;
9592 + int nBlocks = dev->internalEndBlock - dev->internalStartBlock + 1;
9598 + int foundChunksInBlock;
9599 + int equivalentObjectId;
9600 + int alloc_failed = 0;
9603 + yaffs_BlockIndex *blockIndex = NULL;
9604 + int altBlockIndex = 0;
9606 + if (!dev->isYaffs2) {
9607 + T(YAFFS_TRACE_SCAN,
9608 + (TSTR("yaffs_ScanBackwards is only for YAFFS2!" TENDSTR)));
9609 + return YAFFS_FAIL;
9612 + T(YAFFS_TRACE_SCAN,
9614 + ("yaffs_ScanBackwards starts intstartblk %d intendblk %d..."
9615 + TENDSTR), dev->internalStartBlock, dev->internalEndBlock));
9618 + dev->sequenceNumber = YAFFS_LOWEST_SEQUENCE_NUMBER;
9620 + blockIndex = YMALLOC(nBlocks * sizeof(yaffs_BlockIndex));
9623 + blockIndex = YMALLOC_ALT(nBlocks * sizeof(yaffs_BlockIndex));
9624 + altBlockIndex = 1;
9628 + T(YAFFS_TRACE_SCAN,
9629 + (TSTR("yaffs_Scan() could not allocate block index!" TENDSTR)));
9630 + return YAFFS_FAIL;
9633 + dev->blocksInCheckpoint = 0;
9635 + chunkData = yaffs_GetTempBuffer(dev, __LINE__);
9637 + /* Scan all the blocks to determine their state */
9638 + for (blk = dev->internalStartBlock; blk <= dev->internalEndBlock; blk++) {
9639 + bi = yaffs_GetBlockInfo(dev, blk);
9640 + yaffs_ClearChunkBits(dev, blk);
9641 + bi->pagesInUse = 0;
9642 + bi->softDeletions = 0;
9644 + yaffs_QueryInitialBlockState(dev, blk, &state, &sequenceNumber);
9646 + bi->blockState = state;
9647 + bi->sequenceNumber = sequenceNumber;
9649 + if(bi->sequenceNumber == YAFFS_SEQUENCE_CHECKPOINT_DATA)
9650 + bi->blockState = state = YAFFS_BLOCK_STATE_CHECKPOINT;
9652 + T(YAFFS_TRACE_SCAN_DEBUG,
9653 + (TSTR("Block scanning block %d state %d seq %d" TENDSTR), blk,
9654 + state, sequenceNumber));
9657 + if(state == YAFFS_BLOCK_STATE_CHECKPOINT){
9658 + dev->blocksInCheckpoint++;
9660 + } else if (state == YAFFS_BLOCK_STATE_DEAD) {
9661 + T(YAFFS_TRACE_BAD_BLOCKS,
9662 + (TSTR("block %d is bad" TENDSTR), blk));
9663 + } else if (state == YAFFS_BLOCK_STATE_EMPTY) {
9664 + T(YAFFS_TRACE_SCAN_DEBUG,
9665 + (TSTR("Block empty " TENDSTR)));
9666 + dev->nErasedBlocks++;
9667 + dev->nFreeChunks += dev->nChunksPerBlock;
9668 + } else if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
9670 + /* Determine the highest sequence number */
9671 + if (dev->isYaffs2 &&
9672 + sequenceNumber >= YAFFS_LOWEST_SEQUENCE_NUMBER &&
9673 + sequenceNumber < YAFFS_HIGHEST_SEQUENCE_NUMBER) {
9675 + blockIndex[nBlocksToScan].seq = sequenceNumber;
9676 + blockIndex[nBlocksToScan].block = blk;
9680 + if (sequenceNumber >= dev->sequenceNumber) {
9681 + dev->sequenceNumber = sequenceNumber;
9683 + } else if (dev->isYaffs2) {
9684 + /* TODO: Nasty sequence number! */
9685 + T(YAFFS_TRACE_SCAN,
9687 + ("Block scanning block %d has bad sequence number %d"
9688 + TENDSTR), blk, sequenceNumber));
9694 + T(YAFFS_TRACE_SCAN,
9695 + (TSTR("%d blocks to be sorted..." TENDSTR), nBlocksToScan));
9701 + /* Sort the blocks */
9702 +#ifndef CONFIG_YAFFS_USE_OWN_SORT
9703 + yaffs_qsort(blockIndex, nBlocksToScan,
9704 + sizeof(yaffs_BlockIndex), ybicmp);
9707 + /* Dungy old bubble sort... */
9709 + yaffs_BlockIndex temp;
9713 + for (i = 0; i < nBlocksToScan; i++)
9714 + for (j = i + 1; j < nBlocksToScan; j++)
9715 + if (blockIndex[i].seq > blockIndex[j].seq) {
9716 + temp = blockIndex[j];
9717 + blockIndex[j] = blockIndex[i];
9718 + blockIndex[i] = temp;
9725 + T(YAFFS_TRACE_SCAN, (TSTR("...done" TENDSTR)));
9727 + /* Now scan the blocks looking at the data. */
9728 + startIterator = 0;
9729 + endIterator = nBlocksToScan - 1;
9730 + T(YAFFS_TRACE_SCAN_DEBUG,
9731 + (TSTR("%d blocks to be scanned" TENDSTR), nBlocksToScan));
9733 + /* For each block.... backwards */
9734 + for (blockIterator = endIterator; !alloc_failed && blockIterator >= startIterator;
9735 + blockIterator--) {
9736 + /* Cooperative multitasking! This loop can run for so
9737 + long that watchdog timers expire. */
9740 + /* get the block to scan in the correct order */
9741 + blk = blockIndex[blockIterator].block;
9743 + bi = yaffs_GetBlockInfo(dev, blk);
9746 + state = bi->blockState;
9750 + /* For each chunk in each block that needs scanning.... */
9751 + foundChunksInBlock = 0;
9752 + for (c = dev->nChunksPerBlock - 1;
9753 + !alloc_failed && c >= 0 &&
9754 + (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9755 + state == YAFFS_BLOCK_STATE_ALLOCATING); c--) {
9756 + /* Scan backwards...
9757 + * Read the tags and decide what to do
9760 + chunk = blk * dev->nChunksPerBlock + c;
9762 + result = yaffs_ReadChunkWithTagsFromNAND(dev, chunk, NULL,
9765 + /* Let's have a good look at this chunk... */
9767 + if (!tags.chunkUsed) {
9768 + /* An unassigned chunk in the block.
9769 + * If there are used chunks after this one, then
9770 + * it is a chunk that was skipped due to failing the erased
9771 + * check. Just skip it so that it can be deleted.
9772 + * But, more typically, We get here when this is an unallocated
9773 + * chunk and his means that either the block is empty or
9774 + * this is the one being allocated from
9777 + if(foundChunksInBlock)
9779 + /* This is a chunk that was skipped due to failing the erased check */
9781 + } else if (c == 0) {
9782 + /* We're looking at the first chunk in the block so the block is unused */
9783 + state = YAFFS_BLOCK_STATE_EMPTY;
9784 + dev->nErasedBlocks++;
9786 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING ||
9787 + state == YAFFS_BLOCK_STATE_ALLOCATING) {
9788 + if(dev->sequenceNumber == bi->sequenceNumber) {
9789 + /* this is the block being allocated from */
9791 + T(YAFFS_TRACE_SCAN,
9793 + (" Allocating from %d %d"
9794 + TENDSTR), blk, c));
9796 + state = YAFFS_BLOCK_STATE_ALLOCATING;
9797 + dev->allocationBlock = blk;
9798 + dev->allocationPage = c;
9799 + dev->allocationBlockFinder = blk;
9802 + /* This is a partially written block that is not
9803 + * the current allocation block. This block must have
9804 + * had a write failure, so set up for retirement.
9807 + bi->needsRetiring = 1;
9808 + bi->gcPrioritise = 1;
9810 + T(YAFFS_TRACE_ALWAYS,
9811 + (TSTR("Partially written block %d being set for retirement" TENDSTR),
9819 + dev->nFreeChunks++;
9821 + } else if (tags.chunkId > 0) {
9822 + /* chunkId > 0 so it is a data chunk... */
9823 + unsigned int endpos;
9825 + (tags.chunkId - 1) * dev->nDataBytesPerChunk;
9827 + foundChunksInBlock = 1;
9830 + yaffs_SetChunkBit(dev, blk, c);
9833 + in = yaffs_FindOrCreateObjectByNumber(dev,
9836 + YAFFS_OBJECT_TYPE_FILE);
9838 + /* Out of memory */
9843 + in->variantType == YAFFS_OBJECT_TYPE_FILE
9845 + in->variant.fileVariant.shrinkSize) {
9846 + /* This has not been invalidated by a resize */
9847 + if(!yaffs_PutChunkIntoFile(in, tags.chunkId,
9852 + /* File size is calculated by looking at the data chunks if we have not
9853 + * seen an object header yet. Stop this practice once we find an object header.
9857 + 1) * dev->nDataBytesPerChunk +
9860 + if (!in->valid && /* have not got an object header yet */
9861 + in->variant.fileVariant.
9862 + scannedFileSize < endpos) {
9863 + in->variant.fileVariant.
9864 + scannedFileSize = endpos;
9865 + in->variant.fileVariant.
9867 + in->variant.fileVariant.
9872 + /* This chunk has been invalidated by a resize, so delete */
9873 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9877 + /* chunkId == 0, so it is an ObjectHeader.
9878 + * Thus, we read in the object header and make the object
9880 + foundChunksInBlock = 1;
9882 + yaffs_SetChunkBit(dev, blk, c);
9888 + if (tags.extraHeaderInfoAvailable) {
9889 + in = yaffs_FindOrCreateObjectByNumber
9890 + (dev, tags.objectId,
9891 + tags.extraObjectType);
9895 +#ifdef CONFIG_YAFFS_DISABLE_LAZY_LOAD
9898 + tags.extraShadows ||
9900 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9901 + tags.objectId == YAFFS_OBJECTID_LOSTNFOUND))
9904 + /* If we don't have valid info then we need to read the chunk
9905 + * TODO In future we can probably defer reading the chunk and
9906 + * living with invalid data until needed.
9909 + result = yaffs_ReadChunkWithTagsFromNAND(dev,
9914 + oh = (yaffs_ObjectHeader *) chunkData;
9917 + in = yaffs_FindOrCreateObjectByNumber(dev, tags.objectId, oh->type);
9922 + /* TODO Hoosterman we have a problem! */
9923 + T(YAFFS_TRACE_ERROR,
9925 + ("yaffs tragedy: Could not make object for object %d "
9926 + "at chunk %d during scan"
9927 + TENDSTR), tags.objectId, chunk));
9932 + /* We have already filled this one.
9933 + * We have a duplicate that will be discarded, but
9934 + * we first have to suck out resize info if it is a file.
9937 + if ((in->variantType == YAFFS_OBJECT_TYPE_FILE) &&
9939 + oh-> type == YAFFS_OBJECT_TYPE_FILE)||
9940 + (tags.extraHeaderInfoAvailable &&
9941 + tags.extraObjectType == YAFFS_OBJECT_TYPE_FILE))
9944 + (oh) ? oh->fileSize : tags.
9946 + __u32 parentObjectId =
9948 + parentObjectId : tags.
9949 + extraParentObjectId;
9950 + unsigned isShrink =
9951 + (oh) ? oh->isShrink : tags.
9952 + extraIsShrinkHeader;
9954 + /* If it is deleted (unlinked at start also means deleted)
9955 + * we treat the file size as being zeroed at this point.
9957 + if (parentObjectId ==
9958 + YAFFS_OBJECTID_DELETED
9959 + || parentObjectId ==
9960 + YAFFS_OBJECTID_UNLINKED) {
9966 + in->variant.fileVariant.
9967 + shrinkSize > thisSize) {
9968 + in->variant.fileVariant.
9974 + bi->hasShrinkHeader = 1;
9978 + /* Use existing - destroy this one. */
9979 + yaffs_DeleteChunk(dev, chunk, 1, __LINE__);
9984 + (tags.objectId == YAFFS_OBJECTID_ROOT ||
9986 + YAFFS_OBJECTID_LOSTNFOUND)) {
9987 + /* We only load some info, don't fiddle with directory structure */
9991 + in->variantType = oh->type;
9993 + in->yst_mode = oh->yst_mode;
9994 +#ifdef CONFIG_YAFFS_WINCE
9995 + in->win_atime[0] = oh->win_atime[0];
9996 + in->win_ctime[0] = oh->win_ctime[0];
9997 + in->win_mtime[0] = oh->win_mtime[0];
9998 + in->win_atime[1] = oh->win_atime[1];
9999 + in->win_ctime[1] = oh->win_ctime[1];
10000 + in->win_mtime[1] = oh->win_mtime[1];
10002 + in->yst_uid = oh->yst_uid;
10003 + in->yst_gid = oh->yst_gid;
10004 + in->yst_atime = oh->yst_atime;
10005 + in->yst_mtime = oh->yst_mtime;
10006 + in->yst_ctime = oh->yst_ctime;
10007 + in->yst_rdev = oh->yst_rdev;
10011 + in->variantType = tags.extraObjectType;
10012 + in->lazyLoaded = 1;
10015 + in->chunkId = chunk;
10017 + } else if (!in->valid) {
10018 + /* we need to load this info */
10021 + in->chunkId = chunk;
10024 + in->variantType = oh->type;
10026 + in->yst_mode = oh->yst_mode;
10027 +#ifdef CONFIG_YAFFS_WINCE
10028 + in->win_atime[0] = oh->win_atime[0];
10029 + in->win_ctime[0] = oh->win_ctime[0];
10030 + in->win_mtime[0] = oh->win_mtime[0];
10031 + in->win_atime[1] = oh->win_atime[1];
10032 + in->win_ctime[1] = oh->win_ctime[1];
10033 + in->win_mtime[1] = oh->win_mtime[1];
10035 + in->yst_uid = oh->yst_uid;
10036 + in->yst_gid = oh->yst_gid;
10037 + in->yst_atime = oh->yst_atime;
10038 + in->yst_mtime = oh->yst_mtime;
10039 + in->yst_ctime = oh->yst_ctime;
10040 + in->yst_rdev = oh->yst_rdev;
10043 + if (oh->shadowsObject > 0)
10044 + yaffs_HandleShadowedObject(dev,
10050 + yaffs_SetObjectName(in, oh->name);
10052 + yaffs_FindOrCreateObjectByNumber
10053 + (dev, oh->parentObjectId,
10054 + YAFFS_OBJECT_TYPE_DIRECTORY);
10056 + fileSize = oh->fileSize;
10057 + isShrink = oh->isShrink;
10058 + equivalentObjectId = oh->equivalentObjectId;
10062 + in->variantType = tags.extraObjectType;
10064 + yaffs_FindOrCreateObjectByNumber
10065 + (dev, tags.extraParentObjectId,
10066 + YAFFS_OBJECT_TYPE_DIRECTORY);
10067 + fileSize = tags.extraFileLength;
10068 + isShrink = tags.extraIsShrinkHeader;
10069 + equivalentObjectId = tags.extraEquivalentObjectId;
10070 + in->lazyLoaded = 1;
10075 + /* directory stuff...
10076 + * hook up to parent
10079 + if (parent->variantType ==
10080 + YAFFS_OBJECT_TYPE_UNKNOWN) {
10081 + /* Set up as a directory */
10082 + parent->variantType =
10083 + YAFFS_OBJECT_TYPE_DIRECTORY;
10084 + INIT_LIST_HEAD(&parent->variant.
10085 + directoryVariant.
10087 + } else if (parent->variantType !=
10088 + YAFFS_OBJECT_TYPE_DIRECTORY)
10090 + /* Hoosterman, another problem....
10091 + * We're trying to use a non-directory as a directory
10094 + T(YAFFS_TRACE_ERROR,
10096 + ("yaffs tragedy: attempting to use non-directory as"
10097 + " a directory in scan. Put in lost+found."
10099 + parent = dev->lostNFoundDir;
10102 + yaffs_AddObjectToDirectory(parent, in);
10104 + itsUnlinked = (parent == dev->deletedDir) ||
10105 + (parent == dev->unlinkedDir);
10108 + /* Mark the block as having a shrinkHeader */
10109 + bi->hasShrinkHeader = 1;
10112 + /* Note re hardlinks.
10113 + * Since we might scan a hardlink before its equivalent object is scanned
10114 + * we put them all in a list.
10115 + * After scanning is complete, we should have all the objects, so we run
10116 + * through this list and fix up all the chains.
10119 + switch (in->variantType) {
10120 + case YAFFS_OBJECT_TYPE_UNKNOWN:
10121 + /* Todo got a problem */
10123 + case YAFFS_OBJECT_TYPE_FILE:
10125 + if (in->variant.fileVariant.
10126 + scannedFileSize < fileSize) {
10127 + /* This covers the case where the file size is greater
10128 + * than where the data is
10129 + * This will happen if the file is resized to be larger
10130 + * than its current data extents.
10132 + in->variant.fileVariant.fileSize = fileSize;
10133 + in->variant.fileVariant.scannedFileSize =
10134 + in->variant.fileVariant.fileSize;
10138 + in->variant.fileVariant.shrinkSize > fileSize) {
10139 + in->variant.fileVariant.shrinkSize = fileSize;
10143 + case YAFFS_OBJECT_TYPE_HARDLINK:
10144 + if(!itsUnlinked) {
10145 + in->variant.hardLinkVariant.equivalentObjectId =
10146 + equivalentObjectId;
10147 + in->hardLinks.next =
10148 + (struct list_head *) hardList;
10152 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10155 + case YAFFS_OBJECT_TYPE_SPECIAL:
10158 + case YAFFS_OBJECT_TYPE_SYMLINK:
10160 + in->variant.symLinkVariant.alias =
10161 + yaffs_CloneString(oh->
10163 + if(!in->variant.symLinkVariant.alias)
10164 + alloc_failed = 1;
10173 + } /* End of scanning for each chunk */
10175 + if (state == YAFFS_BLOCK_STATE_NEEDS_SCANNING) {
10176 + /* If we got this far while scanning, then the block is fully allocated. */
10177 + state = YAFFS_BLOCK_STATE_FULL;
10180 + bi->blockState = state;
10182 + /* Now let's see if it was dirty */
10183 + if (bi->pagesInUse == 0 &&
10184 + !bi->hasShrinkHeader &&
10185 + bi->blockState == YAFFS_BLOCK_STATE_FULL) {
10186 + yaffs_BlockBecameDirty(dev, blk);
10191 + if (altBlockIndex)
10192 + YFREE_ALT(blockIndex);
10194 + YFREE(blockIndex);
10196 + /* Ok, we've done all the scanning.
10197 + * Fix up the hard link chains.
10198 + * We should now have scanned all the objects, now it's time to add these
10201 + yaffs_HardlinkFixup(dev,hardList);
10205 + * Sort out state of unlinked and deleted objects.
10208 + struct list_head *i;
10209 + struct list_head *n;
10213 + /* Soft delete all the unlinked files */
10214 + list_for_each_safe(i, n,
10215 + &dev->unlinkedDir->variant.directoryVariant.
10218 + l = list_entry(i, yaffs_Object, siblings);
10219 + yaffs_DestroyObject(l);
10223 + /* Soft delete all the deletedDir files */
10224 + list_for_each_safe(i, n,
10225 + &dev->deletedDir->variant.directoryVariant.
10228 + l = list_entry(i, yaffs_Object, siblings);
10229 + yaffs_DestroyObject(l);
10235 + yaffs_ReleaseTempBuffer(dev, chunkData, __LINE__);
10237 + if(alloc_failed){
10238 + return YAFFS_FAIL;
10241 + T(YAFFS_TRACE_SCAN, (TSTR("yaffs_ScanBackwards ends" TENDSTR)));
10246 +/*------------------------------ Directory Functions ----------------------------- */
10248 +static void yaffs_RemoveObjectFromDirectory(yaffs_Object * obj)
10250 + yaffs_Device *dev = obj->myDev;
10252 + if(dev && dev->removeObjectCallback)
10253 + dev->removeObjectCallback(obj);
10255 + list_del_init(&obj->siblings);
10256 + obj->parent = NULL;
10260 +static void yaffs_AddObjectToDirectory(yaffs_Object * directory,
10261 + yaffs_Object * obj)
10264 + if (!directory) {
10265 + T(YAFFS_TRACE_ALWAYS,
10267 + ("tragedy: Trying to add an object to a null pointer directory"
10271 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10272 + T(YAFFS_TRACE_ALWAYS,
10274 + ("tragedy: Trying to add an object to a non-directory"
10279 + if (obj->siblings.prev == NULL) {
10280 + /* Not initialised */
10281 + INIT_LIST_HEAD(&obj->siblings);
10283 + } else if (!list_empty(&obj->siblings)) {
10284 + /* If it is holed up somewhere else, un hook it */
10285 + yaffs_RemoveObjectFromDirectory(obj);
10288 + list_add(&obj->siblings, &directory->variant.directoryVariant.children);
10289 + obj->parent = directory;
10291 + if (directory == obj->myDev->unlinkedDir
10292 + || directory == obj->myDev->deletedDir) {
10293 + obj->unlinked = 1;
10294 + obj->myDev->nUnlinkedFiles++;
10295 + obj->renameAllowed = 0;
10299 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * directory,
10300 + const YCHAR * name)
10304 + struct list_head *i;
10305 + YCHAR buffer[YAFFS_MAX_NAME_LENGTH + 1];
10313 + if (!directory) {
10314 + T(YAFFS_TRACE_ALWAYS,
10316 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10320 + if (directory->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10321 + T(YAFFS_TRACE_ALWAYS,
10323 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10327 + sum = yaffs_CalcNameSum(name);
10329 + list_for_each(i, &directory->variant.directoryVariant.children) {
10331 + l = list_entry(i, yaffs_Object, siblings);
10333 + yaffs_CheckObjectDetailsLoaded(l);
10335 + /* Special case for lost-n-found */
10336 + if (l->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10337 + if (yaffs_strcmp(name, YAFFS_LOSTNFOUND_NAME) == 0) {
10340 + } else if (yaffs_SumCompare(l->sum, sum) || l->chunkId <= 0)
10342 + /* LostnFound cunk called Objxxx
10343 + * Do a real check
10345 + yaffs_GetObjectName(l, buffer,
10346 + YAFFS_MAX_NAME_LENGTH);
10347 + if (yaffs_strncmp(name, buffer,YAFFS_MAX_NAME_LENGTH) == 0) {
10360 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
10361 + int (*fn) (yaffs_Object *))
10363 + struct list_head *i;
10367 + T(YAFFS_TRACE_ALWAYS,
10369 + ("tragedy: yaffs_FindObjectByName: null pointer directory"
10373 + if (theDir->variantType != YAFFS_OBJECT_TYPE_DIRECTORY) {
10374 + T(YAFFS_TRACE_ALWAYS,
10376 + ("tragedy: yaffs_FindObjectByName: non-directory" TENDSTR)));
10380 + list_for_each(i, &theDir->variant.directoryVariant.children) {
10382 + l = list_entry(i, yaffs_Object, siblings);
10383 + if (l && !fn(l)) {
10384 + return YAFFS_FAIL;
10394 +/* GetEquivalentObject dereferences any hard links to get to the
10398 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj)
10400 + if (obj && obj->variantType == YAFFS_OBJECT_TYPE_HARDLINK) {
10401 + /* We want the object id of the equivalent object, not this one */
10402 + obj = obj->variant.hardLinkVariant.equivalentObject;
10403 + yaffs_CheckObjectDetailsLoaded(obj);
10409 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize)
10411 + memset(name, 0, buffSize * sizeof(YCHAR));
10413 + yaffs_CheckObjectDetailsLoaded(obj);
10415 + if (obj->objectId == YAFFS_OBJECTID_LOSTNFOUND) {
10416 + yaffs_strncpy(name, YAFFS_LOSTNFOUND_NAME, buffSize - 1);
10417 + } else if (obj->chunkId <= 0) {
10418 + YCHAR locName[20];
10419 + /* make up a name */
10420 + yaffs_sprintf(locName, _Y("%s%d"), YAFFS_LOSTNFOUND_PREFIX,
10422 + yaffs_strncpy(name, locName, buffSize - 1);
10425 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
10426 + else if (obj->shortName[0]) {
10427 + yaffs_strcpy(name, obj->shortName);
10432 + __u8 *buffer = yaffs_GetTempBuffer(obj->myDev, __LINE__);
10434 + yaffs_ObjectHeader *oh = (yaffs_ObjectHeader *) buffer;
10436 + memset(buffer, 0, obj->myDev->nDataBytesPerChunk);
10438 + if (obj->chunkId >= 0) {
10439 + result = yaffs_ReadChunkWithTagsFromNAND(obj->myDev,
10440 + obj->chunkId, buffer,
10443 + yaffs_strncpy(name, oh->name, buffSize - 1);
10445 + yaffs_ReleaseTempBuffer(obj->myDev, buffer, __LINE__);
10448 + return yaffs_strlen(name);
10451 +int yaffs_GetObjectFileLength(yaffs_Object * obj)
10454 + /* Dereference any hard linking */
10455 + obj = yaffs_GetEquivalentObject(obj);
10457 + if (obj->variantType == YAFFS_OBJECT_TYPE_FILE) {
10458 + return obj->variant.fileVariant.fileSize;
10460 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10461 + return yaffs_strlen(obj->variant.symLinkVariant.alias);
10463 + /* Only a directory should drop through to here */
10464 + return obj->myDev->nDataBytesPerChunk;
10468 +int yaffs_GetObjectLinkCount(yaffs_Object * obj)
10471 + struct list_head *i;
10473 + if (!obj->unlinked) {
10474 + count++; /* the object itself */
10476 + list_for_each(i, &obj->hardLinks) {
10477 + count++; /* add the hard links; */
10483 +int yaffs_GetObjectInode(yaffs_Object * obj)
10485 + obj = yaffs_GetEquivalentObject(obj);
10487 + return obj->objectId;
10490 +unsigned yaffs_GetObjectType(yaffs_Object * obj)
10492 + obj = yaffs_GetEquivalentObject(obj);
10494 + switch (obj->variantType) {
10495 + case YAFFS_OBJECT_TYPE_FILE:
10498 + case YAFFS_OBJECT_TYPE_DIRECTORY:
10501 + case YAFFS_OBJECT_TYPE_SYMLINK:
10504 + case YAFFS_OBJECT_TYPE_HARDLINK:
10507 + case YAFFS_OBJECT_TYPE_SPECIAL:
10508 + if (S_ISFIFO(obj->yst_mode))
10510 + if (S_ISCHR(obj->yst_mode))
10512 + if (S_ISBLK(obj->yst_mode))
10514 + if (S_ISSOCK(obj->yst_mode))
10522 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj)
10524 + obj = yaffs_GetEquivalentObject(obj);
10525 + if (obj->variantType == YAFFS_OBJECT_TYPE_SYMLINK) {
10526 + return yaffs_CloneString(obj->variant.symLinkVariant.alias);
10528 + return yaffs_CloneString(_Y(""));
10532 +#ifndef CONFIG_YAFFS_WINCE
10534 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr)
10536 + unsigned int valid = attr->ia_valid;
10538 + if (valid & ATTR_MODE)
10539 + obj->yst_mode = attr->ia_mode;
10540 + if (valid & ATTR_UID)
10541 + obj->yst_uid = attr->ia_uid;
10542 + if (valid & ATTR_GID)
10543 + obj->yst_gid = attr->ia_gid;
10545 + if (valid & ATTR_ATIME)
10546 + obj->yst_atime = Y_TIME_CONVERT(attr->ia_atime);
10547 + if (valid & ATTR_CTIME)
10548 + obj->yst_ctime = Y_TIME_CONVERT(attr->ia_ctime);
10549 + if (valid & ATTR_MTIME)
10550 + obj->yst_mtime = Y_TIME_CONVERT(attr->ia_mtime);
10552 + if (valid & ATTR_SIZE)
10553 + yaffs_ResizeFile(obj, attr->ia_size);
10555 + yaffs_UpdateObjectHeader(obj, NULL, 1, 0, 0);
10560 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr)
10562 + unsigned int valid = 0;
10564 + attr->ia_mode = obj->yst_mode;
10565 + valid |= ATTR_MODE;
10566 + attr->ia_uid = obj->yst_uid;
10567 + valid |= ATTR_UID;
10568 + attr->ia_gid = obj->yst_gid;
10569 + valid |= ATTR_GID;
10571 + Y_TIME_CONVERT(attr->ia_atime) = obj->yst_atime;
10572 + valid |= ATTR_ATIME;
10573 + Y_TIME_CONVERT(attr->ia_ctime) = obj->yst_ctime;
10574 + valid |= ATTR_CTIME;
10575 + Y_TIME_CONVERT(attr->ia_mtime) = obj->yst_mtime;
10576 + valid |= ATTR_MTIME;
10578 + attr->ia_size = yaffs_GetFileSize(obj);
10579 + valid |= ATTR_SIZE;
10581 + attr->ia_valid = valid;
10590 +int yaffs_DumpObject(yaffs_Object * obj)
10594 + yaffs_GetObjectName(obj, name, 256);
10596 + T(YAFFS_TRACE_ALWAYS,
10598 + ("Object %d, inode %d \"%s\"\n dirty %d valid %d serial %d sum %d"
10599 + " chunk %d type %d size %d\n"
10600 + TENDSTR), obj->objectId, yaffs_GetObjectInode(obj), name,
10601 + obj->dirty, obj->valid, obj->serial, obj->sum, obj->chunkId,
10602 + yaffs_GetObjectType(obj), yaffs_GetObjectFileLength(obj)));
10608 +/*---------------------------- Initialisation code -------------------------------------- */
10610 +static int yaffs_CheckDevFunctions(const yaffs_Device * dev)
10613 + /* Common functions, gotta have */
10614 + if (!dev->eraseBlockInNAND || !dev->initialiseNAND)
10617 +#ifdef CONFIG_YAFFS_YAFFS2
10619 + /* Can use the "with tags" style interface for yaffs1 or yaffs2 */
10620 + if (dev->writeChunkWithTagsToNAND &&
10621 + dev->readChunkWithTagsFromNAND &&
10622 + !dev->writeChunkToNAND &&
10623 + !dev->readChunkFromNAND &&
10624 + dev->markNANDBlockBad && dev->queryNANDBlock)
10628 + /* Can use the "spare" style interface for yaffs1 */
10629 + if (!dev->isYaffs2 &&
10630 + !dev->writeChunkWithTagsToNAND &&
10631 + !dev->readChunkWithTagsFromNAND &&
10632 + dev->writeChunkToNAND &&
10633 + dev->readChunkFromNAND &&
10634 + !dev->markNANDBlockBad && !dev->queryNANDBlock)
10637 + return 0; /* bad */
10641 +static int yaffs_CreateInitialDirectories(yaffs_Device *dev)
10643 + /* Initialise the unlinked, deleted, root and lost and found directories */
10645 + dev->lostNFoundDir = dev->rootDir = NULL;
10646 + dev->unlinkedDir = dev->deletedDir = NULL;
10648 + dev->unlinkedDir =
10649 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_UNLINKED, S_IFDIR);
10651 + dev->deletedDir =
10652 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_DELETED, S_IFDIR);
10655 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_ROOT,
10656 + YAFFS_ROOT_MODE | S_IFDIR);
10657 + dev->lostNFoundDir =
10658 + yaffs_CreateFakeDirectory(dev, YAFFS_OBJECTID_LOSTNFOUND,
10659 + YAFFS_LOSTNFOUND_MODE | S_IFDIR);
10661 + if(dev->lostNFoundDir && dev->rootDir && dev->unlinkedDir && dev->deletedDir){
10662 + yaffs_AddObjectToDirectory(dev->rootDir, dev->lostNFoundDir);
10666 + return YAFFS_FAIL;
10669 +int yaffs_GutsInitialise(yaffs_Device * dev)
10671 + int init_failed = 0;
10675 + T(YAFFS_TRACE_TRACING, (TSTR("yaffs: yaffs_GutsInitialise()" TENDSTR)));
10677 + /* Check stuff that must be set */
10680 + T(YAFFS_TRACE_ALWAYS, (TSTR("yaffs: Need a device" TENDSTR)));
10681 + return YAFFS_FAIL;
10684 + dev->internalStartBlock = dev->startBlock;
10685 + dev->internalEndBlock = dev->endBlock;
10686 + dev->blockOffset = 0;
10687 + dev->chunkOffset = 0;
10688 + dev->nFreeChunks = 0;
10690 + if (dev->startBlock == 0) {
10691 + dev->internalStartBlock = dev->startBlock + 1;
10692 + dev->internalEndBlock = dev->endBlock + 1;
10693 + dev->blockOffset = 1;
10694 + dev->chunkOffset = dev->nChunksPerBlock;
10697 + /* Check geometry parameters. */
10699 + if ((dev->isYaffs2 && dev->nDataBytesPerChunk < 1024) ||
10700 + (!dev->isYaffs2 && dev->nDataBytesPerChunk != 512) ||
10701 + dev->nChunksPerBlock < 2 ||
10702 + dev->nReservedBlocks < 2 ||
10703 + dev->internalStartBlock <= 0 ||
10704 + dev->internalEndBlock <= 0 ||
10705 + dev->internalEndBlock <= (dev->internalStartBlock + dev->nReservedBlocks + 2) // otherwise it is too small
10707 + T(YAFFS_TRACE_ALWAYS,
10709 + ("yaffs: NAND geometry problems: chunk size %d, type is yaffs%s "
10710 + TENDSTR), dev->nDataBytesPerChunk, dev->isYaffs2 ? "2" : ""));
10711 + return YAFFS_FAIL;
10714 + if (yaffs_InitialiseNAND(dev) != YAFFS_OK) {
10715 + T(YAFFS_TRACE_ALWAYS,
10716 + (TSTR("yaffs: InitialiseNAND failed" TENDSTR)));
10717 + return YAFFS_FAIL;
10720 + /* Got the right mix of functions? */
10721 + if (!yaffs_CheckDevFunctions(dev)) {
10722 + /* Function missing */
10723 + T(YAFFS_TRACE_ALWAYS,
10725 + ("yaffs: device function(s) missing or wrong\n" TENDSTR)));
10727 + return YAFFS_FAIL;
10730 + /* This is really a compilation check. */
10731 + if (!yaffs_CheckStructures()) {
10732 + T(YAFFS_TRACE_ALWAYS,
10733 + (TSTR("yaffs_CheckStructures failed\n" TENDSTR)));
10734 + return YAFFS_FAIL;
10737 + if (dev->isMounted) {
10738 + T(YAFFS_TRACE_ALWAYS,
10739 + (TSTR("yaffs: device already mounted\n" TENDSTR)));
10740 + return YAFFS_FAIL;
10743 + /* Finished with most checks. One or two more checks happen later on too. */
10745 + dev->isMounted = 1;
10749 + /* OK now calculate a few things for the device */
10752 + * Calculate all the chunk size manipulation numbers:
10754 + /* Start off assuming it is a power of 2 */
10755 + dev->chunkShift = ShiftDiv(dev->nDataBytesPerChunk);
10756 + dev->chunkMask = (1<<dev->chunkShift) - 1;
10758 + if(dev->nDataBytesPerChunk == (dev->chunkMask + 1)){
10759 + /* Yes it is a power of 2, disable crumbs */
10760 + dev->crumbMask = 0;
10761 + dev->crumbShift = 0;
10762 + dev->crumbsPerChunk = 0;
10764 + /* Not a power of 2, use crumbs instead */
10765 + dev->crumbShift = ShiftDiv(sizeof(yaffs_PackedTags2TagsPart));
10766 + dev->crumbMask = (1<<dev->crumbShift)-1;
10767 + dev->crumbsPerChunk = dev->nDataBytesPerChunk/(1 << dev->crumbShift);
10768 + dev->chunkShift = 0;
10769 + dev->chunkMask = 0;
10774 + * Calculate chunkGroupBits.
10775 + * We need to find the next power of 2 > than internalEndBlock
10778 + x = dev->nChunksPerBlock * (dev->internalEndBlock + 1);
10780 + bits = ShiftsGE(x);
10782 + /* Set up tnode width if wide tnodes are enabled. */
10783 + if(!dev->wideTnodesDisabled){
10784 + /* bits must be even so that we end up with 32-bit words */
10788 + dev->tnodeWidth = 16;
10790 + dev->tnodeWidth = bits;
10793 + dev->tnodeWidth = 16;
10795 + dev->tnodeMask = (1<<dev->tnodeWidth)-1;
10797 + /* Level0 Tnodes are 16 bits or wider (if wide tnodes are enabled),
10798 + * so if the bitwidth of the
10799 + * chunk range we're using is greater than 16 we need
10800 + * to figure out chunk shift and chunkGroupSize
10803 + if (bits <= dev->tnodeWidth)
10804 + dev->chunkGroupBits = 0;
10806 + dev->chunkGroupBits = bits - dev->tnodeWidth;
10809 + dev->chunkGroupSize = 1 << dev->chunkGroupBits;
10811 + if (dev->nChunksPerBlock < dev->chunkGroupSize) {
10812 + /* We have a problem because the soft delete won't work if
10813 + * the chunk group size > chunks per block.
10814 + * This can be remedied by using larger "virtual blocks".
10816 + T(YAFFS_TRACE_ALWAYS,
10817 + (TSTR("yaffs: chunk group too large\n" TENDSTR)));
10819 + return YAFFS_FAIL;
10822 + /* OK, we've finished verifying the device, lets continue with initialisation */
10824 + /* More device initialisation */
10825 + dev->garbageCollections = 0;
10826 + dev->passiveGarbageCollections = 0;
10827 + dev->currentDirtyChecker = 0;
10828 + dev->bufferedBlock = -1;
10829 + dev->doingBufferedBlockRewrite = 0;
10830 + dev->nDeletedFiles = 0;
10831 + dev->nBackgroundDeletions = 0;
10832 + dev->nUnlinkedFiles = 0;
10833 + dev->eccFixed = 0;
10834 + dev->eccUnfixed = 0;
10835 + dev->tagsEccFixed = 0;
10836 + dev->tagsEccUnfixed = 0;
10837 + dev->nErasureFailures = 0;
10838 + dev->nErasedBlocks = 0;
10839 + dev->isDoingGC = 0;
10840 + dev->hasPendingPrioritisedGCs = 1; /* Assume the worst for now, will get fixed on first GC */
10842 + /* Initialise temporary buffers and caches. */
10843 + if(!yaffs_InitialiseTempBuffers(dev))
10846 + dev->srCache = NULL;
10847 + dev->gcCleanupList = NULL;
10850 + if (!init_failed &&
10851 + dev->nShortOpCaches > 0) {
10854 + int srCacheBytes = dev->nShortOpCaches * sizeof(yaffs_ChunkCache);
10856 + if (dev->nShortOpCaches > YAFFS_MAX_SHORT_OP_CACHES) {
10857 + dev->nShortOpCaches = YAFFS_MAX_SHORT_OP_CACHES;
10860 + buf = dev->srCache = YMALLOC(srCacheBytes);
10863 + memset(dev->srCache,0,srCacheBytes);
10865 + for (i = 0; i < dev->nShortOpCaches && buf; i++) {
10866 + dev->srCache[i].object = NULL;
10867 + dev->srCache[i].lastUse = 0;
10868 + dev->srCache[i].dirty = 0;
10869 + dev->srCache[i].data = buf = YMALLOC_DMA(dev->nDataBytesPerChunk);
10874 + dev->srLastUse = 0;
10877 + dev->cacheHits = 0;
10879 + if(!init_failed){
10880 + dev->gcCleanupList = YMALLOC(dev->nChunksPerBlock * sizeof(__u32));
10881 + if(!dev->gcCleanupList)
10885 + if (dev->isYaffs2) {
10886 + dev->useHeaderFileSize = 1;
10888 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10891 + yaffs_InitialiseTnodes(dev);
10892 + yaffs_InitialiseObjects(dev);
10894 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10898 + if(!init_failed){
10899 + /* Now scan the flash. */
10900 + if (dev->isYaffs2) {
10901 + if(yaffs_CheckpointRestore(dev)) {
10902 + T(YAFFS_TRACE_ALWAYS,
10903 + (TSTR("yaffs: restored from checkpoint" TENDSTR)));
10906 + /* Clean up the mess caused by an aborted checkpoint load
10907 + * and scan backwards.
10909 + yaffs_DeinitialiseBlocks(dev);
10910 + yaffs_DeinitialiseTnodes(dev);
10911 + yaffs_DeinitialiseObjects(dev);
10914 + dev->nErasedBlocks = 0;
10915 + dev->nFreeChunks = 0;
10916 + dev->allocationBlock = -1;
10917 + dev->allocationPage = -1;
10918 + dev->nDeletedFiles = 0;
10919 + dev->nUnlinkedFiles = 0;
10920 + dev->nBackgroundDeletions = 0;
10921 + dev->oldestDirtySequence = 0;
10923 + if(!init_failed && !yaffs_InitialiseBlocks(dev))
10926 + yaffs_InitialiseTnodes(dev);
10927 + yaffs_InitialiseObjects(dev);
10929 + if(!init_failed && !yaffs_CreateInitialDirectories(dev))
10932 + if(!init_failed && !yaffs_ScanBackwards(dev))
10936 + if(!yaffs_Scan(dev))
10941 + /* Clean up the mess */
10942 + T(YAFFS_TRACE_TRACING,
10943 + (TSTR("yaffs: yaffs_GutsInitialise() aborted.\n" TENDSTR)));
10945 + yaffs_Deinitialise(dev);
10946 + return YAFFS_FAIL;
10949 + /* Zero out stats */
10950 + dev->nPageReads = 0;
10951 + dev->nPageWrites = 0;
10952 + dev->nBlockErasures = 0;
10953 + dev->nGCCopies = 0;
10954 + dev->nRetriedWrites = 0;
10956 + dev->nRetiredBlocks = 0;
10958 + yaffs_VerifyFreeChunks(dev);
10959 + yaffs_VerifyBlocks(dev);
10962 + T(YAFFS_TRACE_TRACING,
10963 + (TSTR("yaffs: yaffs_GutsInitialise() done.\n" TENDSTR)));
10968 +void yaffs_Deinitialise(yaffs_Device * dev)
10970 + if (dev->isMounted) {
10973 + yaffs_DeinitialiseBlocks(dev);
10974 + yaffs_DeinitialiseTnodes(dev);
10975 + yaffs_DeinitialiseObjects(dev);
10976 + if (dev->nShortOpCaches > 0 &&
10979 + for (i = 0; i < dev->nShortOpCaches; i++) {
10980 + if(dev->srCache[i].data)
10981 + YFREE(dev->srCache[i].data);
10982 + dev->srCache[i].data = NULL;
10985 + YFREE(dev->srCache);
10986 + dev->srCache = NULL;
10989 + YFREE(dev->gcCleanupList);
10991 + for (i = 0; i < YAFFS_N_TEMP_BUFFERS; i++) {
10992 + YFREE(dev->tempBuffer[i].buffer);
10995 + dev->isMounted = 0;
11000 +static int yaffs_CountFreeChunks(yaffs_Device * dev)
11005 + yaffs_BlockInfo *blk;
11007 + for (nFree = 0, b = dev->internalStartBlock; b <= dev->internalEndBlock;
11009 + blk = yaffs_GetBlockInfo(dev, b);
11011 + switch (blk->blockState) {
11012 + case YAFFS_BLOCK_STATE_EMPTY:
11013 + case YAFFS_BLOCK_STATE_ALLOCATING:
11014 + case YAFFS_BLOCK_STATE_COLLECTING:
11015 + case YAFFS_BLOCK_STATE_FULL:
11017 + (dev->nChunksPerBlock - blk->pagesInUse +
11018 + blk->softDeletions);
11029 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev)
11031 + /* This is what we report to the outside world */
11034 + int nDirtyCacheChunks;
11035 + int blocksForCheckpoint;
11038 + nFree = dev->nFreeChunks;
11040 + nFree = yaffs_CountFreeChunks(dev);
11043 + nFree += dev->nDeletedFiles;
11045 + /* Now count the number of dirty chunks in the cache and subtract those */
11049 + for (nDirtyCacheChunks = 0, i = 0; i < dev->nShortOpCaches; i++) {
11050 + if (dev->srCache[i].dirty)
11051 + nDirtyCacheChunks++;
11055 + nFree -= nDirtyCacheChunks;
11057 + nFree -= ((dev->nReservedBlocks + 1) * dev->nChunksPerBlock);
11059 + /* Now we figure out how much to reserve for the checkpoint and report that... */
11060 + blocksForCheckpoint = dev->nCheckpointReservedBlocks - dev->blocksInCheckpoint;
11061 + if(blocksForCheckpoint < 0)
11062 + blocksForCheckpoint = 0;
11064 + nFree -= (blocksForCheckpoint * dev->nChunksPerBlock);
11073 +static int yaffs_freeVerificationFailures;
11075 +static void yaffs_VerifyFreeChunks(yaffs_Device * dev)
11080 + if(yaffs_SkipVerification(dev))
11083 + counted = yaffs_CountFreeChunks(dev);
11085 + difference = dev->nFreeChunks - counted;
11087 + if (difference) {
11088 + T(YAFFS_TRACE_ALWAYS,
11089 + (TSTR("Freechunks verification failure %d %d %d" TENDSTR),
11090 + dev->nFreeChunks, counted, difference));
11091 + yaffs_freeVerificationFailures++;
11095 +/*---------------------------------------- YAFFS test code ----------------------*/
11097 +#define yaffs_CheckStruct(structure,syze, name) \
11098 + if(sizeof(structure) != syze) \
11100 + T(YAFFS_TRACE_ALWAYS,(TSTR("%s should be %d but is %d\n" TENDSTR),\
11101 + name,syze,sizeof(structure))); \
11102 + return YAFFS_FAIL; \
11105 +static int yaffs_CheckStructures(void)
11107 +/* yaffs_CheckStruct(yaffs_Tags,8,"yaffs_Tags") */
11108 +/* yaffs_CheckStruct(yaffs_TagsUnion,8,"yaffs_TagsUnion") */
11109 +/* yaffs_CheckStruct(yaffs_Spare,16,"yaffs_Spare") */
11110 +#ifndef CONFIG_YAFFS_TNODE_LIST_DEBUG
11111 + yaffs_CheckStruct(yaffs_Tnode, 2 * YAFFS_NTNODES_LEVEL0, "yaffs_Tnode")
11113 + yaffs_CheckStruct(yaffs_ObjectHeader, 512, "yaffs_ObjectHeader")
11117 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_guts.h linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h
11118 --- linux-2.6.21.1/fs/yaffs2/yaffs_guts.h 1970-01-01 01:00:00.000000000 +0100
11119 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_guts.h 2007-05-30 13:17:16.000000000 +0200
11122 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
11124 + * Copyright (C) 2002-2007 Aleph One Ltd.
11125 + * for Toby Churchill Ltd and Brightstar Engineering
11127 + * Created by Charles Manning <charles@aleph1.co.uk>
11129 + * This program is free software; you can redistribute it and/or modify
11130 + * it under the terms of the GNU Lesser General Public License version 2.1 as
11131 + * published by the Free Software Foundation.
11133 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
11136 +#ifndef __YAFFS_GUTS_H__
11137 +#define __YAFFS_GUTS_H__
11139 +#include "devextras.h"
11140 +#include "yportenv.h"
11142 +#define YAFFS_OK 1
11143 +#define YAFFS_FAIL 0
11145 +/* Give us a Y=0x59,
11146 + * Give us an A=0x41,
11147 + * Give us an FF=0xFF
11148 + * Give us an S=0x53
11149 + * And what have we got...
11151 +#define YAFFS_MAGIC 0x5941FF53
11153 +#define YAFFS_NTNODES_LEVEL0 16
11154 +#define YAFFS_TNODES_LEVEL0_BITS 4
11155 +#define YAFFS_TNODES_LEVEL0_MASK 0xf
11157 +#define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
11158 +#define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
11159 +#define YAFFS_TNODES_INTERNAL_MASK 0x7
11160 +#define YAFFS_TNODES_MAX_LEVEL 6
11162 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11163 +#define YAFFS_BYTES_PER_SPARE 16
11164 +#define YAFFS_BYTES_PER_CHUNK 512
11165 +#define YAFFS_CHUNK_SIZE_SHIFT 9
11166 +#define YAFFS_CHUNKS_PER_BLOCK 32
11167 +#define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
11170 +#define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
11171 +#define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
11173 +#define YAFFS_MAX_CHUNK_ID 0x000FFFFF
11175 +#define YAFFS_UNUSED_OBJECT_ID 0x0003FFFF
11177 +#define YAFFS_ALLOCATION_NOBJECTS 100
11178 +#define YAFFS_ALLOCATION_NTNODES 100
11179 +#define YAFFS_ALLOCATION_NLINKS 100
11181 +#define YAFFS_NOBJECT_BUCKETS 256
11184 +#define YAFFS_OBJECT_SPACE 0x40000
11186 +#define YAFFS_CHECKPOINT_VERSION 3
11188 +#ifdef CONFIG_YAFFS_UNICODE
11189 +#define YAFFS_MAX_NAME_LENGTH 127
11190 +#define YAFFS_MAX_ALIAS_LENGTH 79
11192 +#define YAFFS_MAX_NAME_LENGTH 255
11193 +#define YAFFS_MAX_ALIAS_LENGTH 159
11196 +#define YAFFS_SHORT_NAME_LENGTH 15
11198 +/* Some special object ids for pseudo objects */
11199 +#define YAFFS_OBJECTID_ROOT 1
11200 +#define YAFFS_OBJECTID_LOSTNFOUND 2
11201 +#define YAFFS_OBJECTID_UNLINKED 3
11202 +#define YAFFS_OBJECTID_DELETED 4
11204 +/* Sseudo object ids for checkpointing */
11205 +#define YAFFS_OBJECTID_SB_HEADER 0x10
11206 +#define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
11207 +#define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
11211 +#define YAFFS_MAX_SHORT_OP_CACHES 20
11213 +#define YAFFS_N_TEMP_BUFFERS 4
11215 +/* We limit the number attempts at sucessfully saving a chunk of data.
11216 + * Small-page devices have 32 pages per block; large-page devices have 64.
11217 + * Default to something in the order of 5 to 10 blocks worth of chunks.
11219 +#define YAFFS_WR_ATTEMPTS (5*64)
11221 +/* Sequence numbers are used in YAFFS2 to determine block allocation order.
11222 + * The range is limited slightly to help distinguish bad numbers from good.
11223 + * This also allows us to perhaps in the future use special numbers for
11224 + * special purposes.
11225 + * EFFFFF00 allows the allocation of 8 blocks per second (~1Mbytes) for 15 years,
11226 + * and is a larger number than the lifetime of a 2GB device.
11228 +#define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
11229 +#define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xEFFFFF00
11231 +/* ChunkCache is used for short read/write operations.*/
11233 + struct yaffs_ObjectStruct *object;
11237 + int nBytes; /* Only valid if the cache is dirty */
11238 + int locked; /* Can't push out or flush while locked. */
11239 +#ifdef CONFIG_YAFFS_YAFFS2
11242 + __u8 data[YAFFS_BYTES_PER_CHUNK];
11244 +} yaffs_ChunkCache;
11248 +/* Tags structures in RAM
11249 + * NB This uses bitfield. Bitfields should not straddle a u32 boundary otherwise
11250 + * the structure size will get blown out.
11253 +#ifndef CONFIG_YAFFS_NO_YAFFS1
11255 + unsigned chunkId:20;
11256 + unsigned serialNumber:2;
11257 + unsigned byteCount:10;
11258 + unsigned objectId:18;
11260 + unsigned unusedStuff:2;
11265 + yaffs_Tags asTags;
11267 +} yaffs_TagsUnion;
11271 +/* Stuff used for extended tags in YAFFS2 */
11274 + YAFFS_ECC_RESULT_UNKNOWN,
11275 + YAFFS_ECC_RESULT_NO_ERROR,
11276 + YAFFS_ECC_RESULT_FIXED,
11277 + YAFFS_ECC_RESULT_UNFIXED
11278 +} yaffs_ECCResult;
11281 + YAFFS_OBJECT_TYPE_UNKNOWN,
11282 + YAFFS_OBJECT_TYPE_FILE,
11283 + YAFFS_OBJECT_TYPE_SYMLINK,
11284 + YAFFS_OBJECT_TYPE_DIRECTORY,
11285 + YAFFS_OBJECT_TYPE_HARDLINK,
11286 + YAFFS_OBJECT_TYPE_SPECIAL
11287 +} yaffs_ObjectType;
11289 +#define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
11293 + unsigned validMarker0;
11294 + unsigned chunkUsed; /* Status of the chunk: used or unused */
11295 + unsigned objectId; /* If 0 then this is not part of an object (unused) */
11296 + unsigned chunkId; /* If 0 then this is a header, else a data chunk */
11297 + unsigned byteCount; /* Only valid for data chunks */
11299 + /* The following stuff only has meaning when we read */
11300 + yaffs_ECCResult eccResult;
11301 + unsigned blockBad;
11303 + /* YAFFS 1 stuff */
11304 + unsigned chunkDeleted; /* The chunk is marked deleted */
11305 + unsigned serialNumber; /* Yaffs1 2-bit serial number */
11307 + /* YAFFS2 stuff */
11308 + unsigned sequenceNumber; /* The sequence number of this block */
11310 + /* Extra info if this is an object header (YAFFS2 only) */
11312 + unsigned extraHeaderInfoAvailable; /* There is extra info available if this is not zero */
11313 + unsigned extraParentObjectId; /* The parent object */
11314 + unsigned extraIsShrinkHeader; /* Is it a shrink header? */
11315 + unsigned extraShadows; /* Does this shadow another object? */
11317 + yaffs_ObjectType extraObjectType; /* What object type? */
11319 + unsigned extraFileLength; /* Length if it is a file */
11320 + unsigned extraEquivalentObjectId; /* Equivalent object Id if it is a hard link */
11322 + unsigned validMarker1;
11324 +} yaffs_ExtendedTags;
11326 +/* Spare structure for YAFFS1 */
11332 + __u8 pageStatus; /* set to 0 to delete the chunk */
11333 + __u8 blockStatus;
11342 +/*Special structure for passing through to mtd */
11343 +struct yaffs_NANDSpare {
11344 + yaffs_Spare spare;
11349 +/* Block data in RAM */
11352 + YAFFS_BLOCK_STATE_UNKNOWN = 0,
11354 + YAFFS_BLOCK_STATE_SCANNING,
11355 + YAFFS_BLOCK_STATE_NEEDS_SCANNING,
11356 + /* The block might have something on it (ie it is allocating or full, perhaps empty)
11357 + * but it needs to be scanned to determine its true state.
11358 + * This state is only valid during yaffs_Scan.
11359 + * NB We tolerate empty because the pre-scanner might be incapable of deciding
11360 + * However, if this state is returned on a YAFFS2 device, then we expect a sequence number
11363 + YAFFS_BLOCK_STATE_EMPTY,
11364 + /* This block is empty */
11366 + YAFFS_BLOCK_STATE_ALLOCATING,
11367 + /* This block is partially allocated.
11368 + * At least one page holds valid data.
11369 + * This is the one currently being used for page
11370 + * allocation. Should never be more than one of these
11373 + YAFFS_BLOCK_STATE_FULL,
11374 + /* All the pages in this block have been allocated.
11377 + YAFFS_BLOCK_STATE_DIRTY,
11378 + /* All pages have been allocated and deleted.
11379 + * Erase me, reuse me.
11382 + YAFFS_BLOCK_STATE_CHECKPOINT,
11383 + /* This block is assigned to holding checkpoint data.
11386 + YAFFS_BLOCK_STATE_COLLECTING,
11387 + /* This block is being garbage collected */
11389 + YAFFS_BLOCK_STATE_DEAD
11390 + /* This block has failed and is not in use */
11391 +} yaffs_BlockState;
11393 +#define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
11398 + int softDeletions:10; /* number of soft deleted pages */
11399 + int pagesInUse:10; /* number of pages in use */
11400 + yaffs_BlockState blockState:4; /* One of the above block states */
11401 + __u32 needsRetiring:1; /* Data has failed on this block, need to get valid data off */
11402 + /* and retire the block. */
11403 + __u32 skipErasedCheck: 1; /* If this is set we can skip the erased check on this block */
11404 + __u32 gcPrioritise: 1; /* An ECC check or blank check has failed on this block.
11405 + It should be prioritised for GC */
11406 + __u32 chunkErrorStrikes:3; /* How many times we've had ecc etc failures on this block and tried to reuse it */
11408 +#ifdef CONFIG_YAFFS_YAFFS2
11409 + __u32 hasShrinkHeader:1; /* This block has at least one shrink object header */
11410 + __u32 sequenceNumber; /* block sequence number for yaffs2 */
11413 +} yaffs_BlockInfo;
11415 +/* -------------------------- Object structure -------------------------------*/
11416 +/* This is the object structure as stored on NAND */
11419 + yaffs_ObjectType type;
11421 + /* Apply to everything */
11422 + int parentObjectId;
11423 + __u16 sum__NoLongerUsed; /* checksum of name. No longer used */
11424 + YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
11426 + /* Thes following apply to directories, files, symlinks - not hard links */
11427 + __u32 yst_mode; /* protection */
11429 +#ifdef CONFIG_YAFFS_WINCE
11430 + __u32 notForWinCE[5];
11439 + /* File size applies to files only */
11442 + /* Equivalent object id applies to hard links only. */
11443 + int equivalentObjectId;
11445 + /* Alias is for symlinks only. */
11446 + YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
11448 + __u32 yst_rdev; /* device stuff for block and char devices (major/min) */
11450 +#ifdef CONFIG_YAFFS_WINCE
11451 + __u32 win_ctime[2];
11452 + __u32 win_atime[2];
11453 + __u32 win_mtime[2];
11454 + __u32 roomToGrow[4];
11456 + __u32 roomToGrow[10];
11459 + int shadowsObject; /* This object header shadows the specified object if > 0 */
11461 + /* isShrink applies to object headers written when we shrink the file (ie resize) */
11464 +} yaffs_ObjectHeader;
11466 +/*--------------------------- Tnode -------------------------- */
11468 +union yaffs_Tnode_union {
11469 +#ifdef CONFIG_YAFFS_TNODE_LIST_DEBUG
11470 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL + 1];
11472 + union yaffs_Tnode_union *internal[YAFFS_NTNODES_INTERNAL];
11474 +/* __u16 level0[YAFFS_NTNODES_LEVEL0]; */
11478 +typedef union yaffs_Tnode_union yaffs_Tnode;
11480 +struct yaffs_TnodeList_struct {
11481 + struct yaffs_TnodeList_struct *next;
11482 + yaffs_Tnode *tnodes;
11485 +typedef struct yaffs_TnodeList_struct yaffs_TnodeList;
11487 +/*------------------------ Object -----------------------------*/
11488 +/* An object can be one of:
11489 + * - a directory (no data, has children links
11490 + * - a regular file (data.... not prunes :->).
11491 + * - a symlink [symbolic link] (the alias).
11497 + __u32 scannedFileSize;
11498 + __u32 shrinkSize;
11500 + yaffs_Tnode *top;
11501 +} yaffs_FileStructure;
11504 + struct list_head children; /* list of child links */
11505 +} yaffs_DirectoryStructure;
11509 +} yaffs_SymLinkStructure;
11512 + struct yaffs_ObjectStruct *equivalentObject;
11513 + __u32 equivalentObjectId;
11514 +} yaffs_HardLinkStructure;
11517 + yaffs_FileStructure fileVariant;
11518 + yaffs_DirectoryStructure directoryVariant;
11519 + yaffs_SymLinkStructure symLinkVariant;
11520 + yaffs_HardLinkStructure hardLinkVariant;
11521 +} yaffs_ObjectVariant;
11523 +struct yaffs_ObjectStruct {
11524 + __u8 deleted:1; /* This should only apply to unlinked files. */
11525 + __u8 softDeleted:1; /* it has also been soft deleted */
11526 + __u8 unlinked:1; /* An unlinked file. The file should be in the unlinked directory.*/
11527 + __u8 fake:1; /* A fake object has no presence on NAND. */
11528 + __u8 renameAllowed:1; /* Some objects are not allowed to be renamed. */
11529 + __u8 unlinkAllowed:1;
11530 + __u8 dirty:1; /* the object needs to be written to flash */
11531 + __u8 valid:1; /* When the file system is being loaded up, this
11532 + * object might be created before the data
11533 + * is available (ie. file data records appear before the header).
11535 + __u8 lazyLoaded:1; /* This object has been lazy loaded and is missing some detail */
11537 + __u8 deferedFree:1; /* For Linux kernel. Object is removed from NAND, but is
11538 + * still in the inode cache. Free of object is defered.
11539 + * until the inode is released.
11542 + __u8 serial; /* serial number of chunk in NAND. Cached here */
11543 + __u16 sum; /* sum of the name to speed searching */
11545 + struct yaffs_DeviceStruct *myDev; /* The device I'm on */
11547 + struct list_head hashLink; /* list of objects in this hash bucket */
11549 + struct list_head hardLinks; /* all the equivalent hard linked objects */
11551 + /* directory structure stuff */
11552 + /* also used for linking up the free list */
11553 + struct yaffs_ObjectStruct *parent;
11554 + struct list_head siblings;
11556 + /* Where's my object header in NAND? */
11559 + int nDataChunks; /* Number of data chunks attached to the file. */
11561 + __u32 objectId; /* the object id value */
11565 +#ifdef CONFIG_YAFFS_SHORT_NAMES_IN_RAM
11566 + YCHAR shortName[YAFFS_SHORT_NAME_LENGTH + 1];
11569 +#ifndef __KERNEL__
11573 +#ifdef CONFIG_YAFFS_WINCE
11574 + __u32 win_ctime[2];
11575 + __u32 win_mtime[2];
11576 + __u32 win_atime[2];
11588 + struct inode *myInode;
11592 + yaffs_ObjectType variantType;
11594 + yaffs_ObjectVariant variant;
11598 +typedef struct yaffs_ObjectStruct yaffs_Object;
11600 +struct yaffs_ObjectList_struct {
11601 + yaffs_Object *objects;
11602 + struct yaffs_ObjectList_struct *next;
11605 +typedef struct yaffs_ObjectList_struct yaffs_ObjectList;
11608 + struct list_head list;
11610 +} yaffs_ObjectBucket;
11613 +/* yaffs_CheckpointObject holds the definition of an object as dumped
11614 + * by checkpointing.
11623 + yaffs_ObjectType variantType:3;
11625 + __u8 softDeleted:1;
11628 + __u8 renameAllowed:1;
11629 + __u8 unlinkAllowed:1;
11633 + __u32 fileSizeOrEquivalentObjectId;
11635 +}yaffs_CheckpointObject;
11637 +/*--------------------- Temporary buffers ----------------
11639 + * These are chunk-sized working buffers. Each device has a few
11644 + int line; /* track from whence this buffer was allocated */
11646 +} yaffs_TempBuffer;
11648 +/*----------------- Device ---------------------------------*/
11650 +struct yaffs_DeviceStruct {
11651 + struct list_head devList;
11652 + const char *name;
11654 + /* Entry parameters set up way early. Yaffs sets up the rest.*/
11655 + int nDataBytesPerChunk; /* Should be a power of 2 >= 512 */
11656 + int nChunksPerBlock; /* does not need to be a power of 2 */
11657 + int nBytesPerSpare; /* spare area size */
11658 + int startBlock; /* Start block we're allowed to use */
11659 + int endBlock; /* End block we're allowed to use */
11660 + int nReservedBlocks; /* We want this tuneable so that we can reduce */
11661 + /* reserved blocks on NOR and RAM. */
11664 + /* Stuff used by the shared space checkpointing mechanism */
11665 + /* If this value is zero, then this mechanism is disabled */
11667 + int nCheckpointReservedBlocks; /* Blocks to reserve for checkpoint data */
11672 + int nShortOpCaches; /* If <= 0, then short op caching is disabled, else
11673 + * the number of short op caches (don't use too many)
11676 + int useHeaderFileSize; /* Flag to determine if we should use file sizes from the header */
11678 + int useNANDECC; /* Flag to decide whether or not to use NANDECC */
11680 + void *genericDevice; /* Pointer to device context
11681 + * On an mtd this holds the mtd pointer.
11683 + void *superBlock;
11685 + /* NAND access functions (Must be set before calling YAFFS)*/
11687 + int (*writeChunkToNAND) (struct yaffs_DeviceStruct * dev,
11688 + int chunkInNAND, const __u8 * data,
11689 + const yaffs_Spare * spare);
11690 + int (*readChunkFromNAND) (struct yaffs_DeviceStruct * dev,
11691 + int chunkInNAND, __u8 * data,
11692 + yaffs_Spare * spare);
11693 + int (*eraseBlockInNAND) (struct yaffs_DeviceStruct * dev,
11694 + int blockInNAND);
11695 + int (*initialiseNAND) (struct yaffs_DeviceStruct * dev);
11697 +#ifdef CONFIG_YAFFS_YAFFS2
11698 + int (*writeChunkWithTagsToNAND) (struct yaffs_DeviceStruct * dev,
11699 + int chunkInNAND, const __u8 * data,
11700 + const yaffs_ExtendedTags * tags);
11701 + int (*readChunkWithTagsFromNAND) (struct yaffs_DeviceStruct * dev,
11702 + int chunkInNAND, __u8 * data,
11703 + yaffs_ExtendedTags * tags);
11704 + int (*markNANDBlockBad) (struct yaffs_DeviceStruct * dev, int blockNo);
11705 + int (*queryNANDBlock) (struct yaffs_DeviceStruct * dev, int blockNo,
11706 + yaffs_BlockState * state, int *sequenceNumber);
11711 + /* The removeObjectCallback function must be supplied by OS flavours that
11712 + * need it. The Linux kernel does not use this, but yaffs direct does use
11713 + * it to implement the faster readdir
11715 + void (*removeObjectCallback)(struct yaffs_ObjectStruct *obj);
11717 + /* Callback to mark the superblock dirsty */
11718 + void (*markSuperBlockDirty)(void * superblock);
11720 + int wideTnodesDisabled; /* Set to disable wide tnodes */
11723 + /* End of stuff that must be set before initialisation. */
11725 + /* Checkpoint control. Can be set before or after initialisation */
11726 + __u8 skipCheckpointRead;
11727 + __u8 skipCheckpointWrite;
11729 + /* Runtime parameters. Set up by YAFFS. */
11731 + __u16 chunkGroupBits; /* 0 for devices <= 32MB. else log2(nchunks) - 16 */
11732 + __u16 chunkGroupSize; /* == 2^^chunkGroupBits */
11734 + /* Stuff to support wide tnodes */
11735 + __u32 tnodeWidth;
11738 + /* Stuff to support various file offses to chunk/offset translations */
11739 + /* "Crumbs" for nDataBytesPerChunk not being a power of 2 */
11741 + __u32 crumbShift;
11742 + __u32 crumbsPerChunk;
11744 + /* Straight shifting for nDataBytesPerChunk being a power of 2 */
11745 + __u32 chunkShift;
11751 + struct semaphore sem; /* Semaphore for waiting on erasure.*/
11752 + struct semaphore grossLock; /* Gross locking semaphore */
11753 + __u8 *spareBuffer; /* For mtdif2 use. Don't know the size of the buffer
11754 + * at compile time so we have to allocate it.
11756 + void (*putSuperFunc) (struct super_block * sb);
11761 + int isCheckpointed;
11764 + /* Stuff to support block offsetting to support start block zero */
11765 + int internalStartBlock;
11766 + int internalEndBlock;
11771 + /* Runtime checkpointing stuff */
11772 + int checkpointPageSequence; /* running sequence number of checkpoint pages */
11773 + int checkpointByteCount;
11774 + int checkpointByteOffset;
11775 + __u8 *checkpointBuffer;
11776 + int checkpointOpenForWrite;
11777 + int blocksInCheckpoint;
11778 + int checkpointCurrentChunk;
11779 + int checkpointCurrentBlock;
11780 + int checkpointNextBlock;
11781 + int *checkpointBlockList;
11782 + int checkpointMaxBlocks;
11783 + __u32 checkpointSum;
11784 + __u32 checkpointXor;
11787 + yaffs_BlockInfo *blockInfo;
11788 + __u8 *chunkBits; /* bitmap of chunks in use */
11789 + unsigned blockInfoAlt:1; /* was allocated using alternative strategy */
11790 + unsigned chunkBitsAlt:1; /* was allocated using alternative strategy */
11791 + int chunkBitmapStride; /* Number of bytes of chunkBits per block.
11792 + * Must be consistent with nChunksPerBlock.
11795 + int nErasedBlocks;
11796 + int allocationBlock; /* Current block being allocated off */
11797 + __u32 allocationPage;
11798 + int allocationBlockFinder; /* Used to search for next allocation block */
11800 + /* Runtime state */
11801 + int nTnodesCreated;
11802 + yaffs_Tnode *freeTnodes;
11804 + yaffs_TnodeList *allocatedTnodeList;
11808 + int nObjectsCreated;
11809 + yaffs_Object *freeObjects;
11810 + int nFreeObjects;
11812 + yaffs_ObjectList *allocatedObjectList;
11814 + yaffs_ObjectBucket objectBucket[YAFFS_NOBJECT_BUCKETS];
11818 + int currentDirtyChecker; /* Used to find current dirtiest block */
11820 + __u32 *gcCleanupList; /* objects to delete at the end of a GC. */
11821 + int nonAggressiveSkip; /* GC state/mode */
11826 + int nBlockErasures;
11827 + int nErasureFailures;
11829 + int garbageCollections;
11830 + int passiveGarbageCollections;
11831 + int nRetriedWrites;
11832 + int nRetiredBlocks;
11835 + int tagsEccFixed;
11836 + int tagsEccUnfixed;
11838 + int nUnmarkedDeletions;
11840 + int hasPendingPrioritisedGCs; /* We think this device might have pending prioritised gcs */
11842 + /* Special directories */
11843 + yaffs_Object *rootDir;
11844 + yaffs_Object *lostNFoundDir;
11846 + /* Buffer areas for storing data to recover from write failures TODO
11847 + * __u8 bufferedData[YAFFS_CHUNKS_PER_BLOCK][YAFFS_BYTES_PER_CHUNK];
11848 + * yaffs_Spare bufferedSpare[YAFFS_CHUNKS_PER_BLOCK];
11851 + int bufferedBlock; /* Which block is buffered here? */
11852 + int doingBufferedBlockRewrite;
11854 + yaffs_ChunkCache *srCache;
11859 + /* Stuff for background deletion and unlinked files.*/
11860 + yaffs_Object *unlinkedDir; /* Directory where unlinked and deleted files live. */
11861 + yaffs_Object *deletedDir; /* Directory where deleted objects are sent to disappear. */
11862 + yaffs_Object *unlinkedDeletion; /* Current file being background deleted.*/
11863 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11864 + int nUnlinkedFiles; /* Count of unlinked files. */
11865 + int nBackgroundDeletions; /* Count of background deletions. */
11868 + yaffs_TempBuffer tempBuffer[YAFFS_N_TEMP_BUFFERS];
11870 + int unmanagedTempAllocations;
11871 + int unmanagedTempDeallocations;
11873 + /* yaffs2 runtime stuff */
11874 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11875 + unsigned oldestDirtySequence;
11879 +typedef struct yaffs_DeviceStruct yaffs_Device;
11881 +/* The static layout of bllock usage etc is stored in the super block header */
11885 + int checkpointStartBlock;
11886 + int checkpointEndBlock;
11890 +} yaffs_SuperBlockHeader;
11892 +/* The CheckpointDevice structure holds the device information that changes at runtime and
11893 + * must be preserved over unmount/mount cycles.
11897 + int nErasedBlocks;
11898 + int allocationBlock; /* Current block being allocated off */
11899 + __u32 allocationPage;
11902 + int nDeletedFiles; /* Count of files awaiting deletion;*/
11903 + int nUnlinkedFiles; /* Count of unlinked files. */
11904 + int nBackgroundDeletions; /* Count of background deletions. */
11906 + /* yaffs2 runtime stuff */
11907 + unsigned sequenceNumber; /* Sequence number of currently allocating block */
11908 + unsigned oldestDirtySequence;
11910 +} yaffs_CheckpointDevice;
11918 +} yaffs_CheckpointValidity;
11920 +/* Function to manipulate block info */
11921 +static Y_INLINE yaffs_BlockInfo *yaffs_GetBlockInfo(yaffs_Device * dev, int blk)
11923 + if (blk < dev->internalStartBlock || blk > dev->internalEndBlock) {
11924 + T(YAFFS_TRACE_ERROR,
11926 + ("**>> yaffs: getBlockInfo block %d is not valid" TENDSTR),
11930 + return &dev->blockInfo[blk - dev->internalStartBlock];
11933 +/*----------------------- YAFFS Functions -----------------------*/
11935 +int yaffs_GutsInitialise(yaffs_Device * dev);
11936 +void yaffs_Deinitialise(yaffs_Device * dev);
11938 +int yaffs_GetNumberOfFreeChunks(yaffs_Device * dev);
11940 +int yaffs_RenameObject(yaffs_Object * oldDir, const YCHAR * oldName,
11941 + yaffs_Object * newDir, const YCHAR * newName);
11943 +int yaffs_Unlink(yaffs_Object * dir, const YCHAR * name);
11944 +int yaffs_DeleteFile(yaffs_Object * obj);
11946 +int yaffs_GetObjectName(yaffs_Object * obj, YCHAR * name, int buffSize);
11947 +int yaffs_GetObjectFileLength(yaffs_Object * obj);
11948 +int yaffs_GetObjectInode(yaffs_Object * obj);
11949 +unsigned yaffs_GetObjectType(yaffs_Object * obj);
11950 +int yaffs_GetObjectLinkCount(yaffs_Object * obj);
11952 +int yaffs_SetAttributes(yaffs_Object * obj, struct iattr *attr);
11953 +int yaffs_GetAttributes(yaffs_Object * obj, struct iattr *attr);
11955 +/* File operations */
11956 +int yaffs_ReadDataFromFile(yaffs_Object * obj, __u8 * buffer, loff_t offset,
11958 +int yaffs_WriteDataToFile(yaffs_Object * obj, const __u8 * buffer, loff_t offset,
11959 + int nBytes, int writeThrough);
11960 +int yaffs_ResizeFile(yaffs_Object * obj, loff_t newSize);
11962 +yaffs_Object *yaffs_MknodFile(yaffs_Object * parent, const YCHAR * name,
11963 + __u32 mode, __u32 uid, __u32 gid);
11964 +int yaffs_FlushFile(yaffs_Object * obj, int updateTime);
11966 +/* Flushing and checkpointing */
11967 +void yaffs_FlushEntireDeviceCache(yaffs_Device *dev);
11969 +int yaffs_CheckpointSave(yaffs_Device *dev);
11970 +int yaffs_CheckpointRestore(yaffs_Device *dev);
11972 +/* Directory operations */
11973 +yaffs_Object *yaffs_MknodDirectory(yaffs_Object * parent, const YCHAR * name,
11974 + __u32 mode, __u32 uid, __u32 gid);
11975 +yaffs_Object *yaffs_FindObjectByName(yaffs_Object * theDir, const YCHAR * name);
11976 +int yaffs_ApplyToDirectoryChildren(yaffs_Object * theDir,
11977 + int (*fn) (yaffs_Object *));
11979 +yaffs_Object *yaffs_FindObjectByNumber(yaffs_Device * dev, __u32 number);
11981 +/* Link operations */
11982 +yaffs_Object *yaffs_Link(yaffs_Object * parent, const YCHAR * name,
11983 + yaffs_Object * equivalentObject);
11985 +yaffs_Object *yaffs_GetEquivalentObject(yaffs_Object * obj);
11987 +/* Symlink operations */
11988 +yaffs_Object *yaffs_MknodSymLink(yaffs_Object * parent, const YCHAR * name,
11989 + __u32 mode, __u32 uid, __u32 gid,
11990 + const YCHAR * alias);
11991 +YCHAR *yaffs_GetSymlinkAlias(yaffs_Object * obj);
11993 +/* Special inodes (fifos, sockets and devices) */
11994 +yaffs_Object *yaffs_MknodSpecial(yaffs_Object * parent, const YCHAR * name,
11995 + __u32 mode, __u32 uid, __u32 gid, __u32 rdev);
11997 +/* Special directories */
11998 +yaffs_Object *yaffs_Root(yaffs_Device * dev);
11999 +yaffs_Object *yaffs_LostNFound(yaffs_Device * dev);
12001 +#ifdef CONFIG_YAFFS_WINCE
12002 +/* CONFIG_YAFFS_WINCE special stuff */
12003 +void yfsd_WinFileTimeNow(__u32 target[2]);
12008 +void yaffs_HandleDeferedFree(yaffs_Object * obj);
12012 +int yaffs_DumpObject(yaffs_Object * obj);
12014 +void yaffs_GutsTest(yaffs_Device * dev);
12016 +/* A few useful functions */
12017 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
12018 +void yaffs_DeleteChunk(yaffs_Device * dev, int chunkId, int markNAND, int lyn);
12019 +int yaffs_CheckFF(__u8 * buffer, int nBytes);
12020 +void yaffs_HandleChunkError(yaffs_Device *dev, yaffs_BlockInfo *bi);
12023 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c
12024 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.c 1970-01-01 01:00:00.000000000 +0100
12025 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.c 2007-05-30 13:17:17.000000000 +0200
12028 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
12030 + * Copyright (C) 2002-2007 Aleph One Ltd.
12031 + * for Toby Churchill Ltd and Brightstar Engineering
12033 + * Created by Charles Manning <charles@aleph1.co.uk>
12035 + * This program is free software; you can redistribute it and/or modify
12036 + * it under the terms of the GNU General Public License version 2 as
12037 + * published by the Free Software Foundation.
12040 +const char *yaffs_mtdif_c_version =
12041 + "$Id: yaffs_mtdif.c,v 1.19 2007-02-14 01:09:06 wookey Exp $";
12043 +#include "yportenv.h"
12046 +#include "yaffs_mtdif.h"
12048 +#include "linux/mtd/mtd.h"
12049 +#include "linux/types.h"
12050 +#include "linux/time.h"
12051 +#include "linux/mtd/nand.h"
12053 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18))
12054 +static struct nand_oobinfo yaffs_oobinfo = {
12057 + .eccpos = {8, 9, 10, 13, 14, 15}
12060 +static struct nand_oobinfo yaffs_noeccinfo = {
12065 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12066 +static inline void translate_spare2oob(const yaffs_Spare *spare, __u8 *oob)
12068 + oob[0] = spare->tagByte0;
12069 + oob[1] = spare->tagByte1;
12070 + oob[2] = spare->tagByte2;
12071 + oob[3] = spare->tagByte3;
12072 + oob[4] = spare->tagByte4;
12073 + oob[5] = spare->tagByte5 & 0x3f;
12074 + oob[5] |= spare->blockStatus == 'Y' ? 0: 0x80;
12075 + oob[5] |= spare->pageStatus == 0 ? 0: 0x40;
12076 + oob[6] = spare->tagByte6;
12077 + oob[7] = spare->tagByte7;
12080 +static inline void translate_oob2spare(yaffs_Spare *spare, __u8 *oob)
12082 + struct yaffs_NANDSpare *nspare = (struct yaffs_NANDSpare *)spare;
12083 + spare->tagByte0 = oob[0];
12084 + spare->tagByte1 = oob[1];
12085 + spare->tagByte2 = oob[2];
12086 + spare->tagByte3 = oob[3];
12087 + spare->tagByte4 = oob[4];
12088 + spare->tagByte5 = oob[5] == 0xff ? 0xff : oob[5] & 0x3f;
12089 + spare->blockStatus = oob[5] & 0x80 ? 0xff : 'Y';
12090 + spare->pageStatus = oob[5] & 0x40 ? 0xff : 0;
12091 + spare->ecc1[0] = spare->ecc1[1] = spare->ecc1[2] = 0xff;
12092 + spare->tagByte6 = oob[6];
12093 + spare->tagByte7 = oob[7];
12094 + spare->ecc2[0] = spare->ecc2[1] = spare->ecc2[2] = 0xff;
12096 + nspare->eccres1 = nspare->eccres2 = 0; /* FIXME */
12100 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12101 + const __u8 * data, const yaffs_Spare * spare)
12103 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12104 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12105 + struct mtd_oob_ops ops;
12110 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12111 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12112 + __u8 spareAsBytes[8]; /* OOB */
12114 + if (data && !spare)
12115 + retval = mtd->write(mtd, addr, dev->nDataBytesPerChunk,
12117 + else if (spare) {
12118 + if (dev->useNANDECC) {
12119 + translate_spare2oob(spare, spareAsBytes);
12120 + ops.mode = MTD_OOB_AUTO;
12121 + ops.ooblen = 8; /* temp hack */
12123 + ops.mode = MTD_OOB_RAW;
12124 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12126 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12127 + ops.datbuf = (u8 *)data;
12129 + ops.oobbuf = spareAsBytes;
12130 + retval = mtd->write_oob(mtd, addr, &ops);
12133 + __u8 *spareAsBytes = (__u8 *) spare;
12135 + if (data && spare) {
12136 + if (dev->useNANDECC)
12138 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12139 + &dummy, data, spareAsBytes,
12143 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
12144 + &dummy, data, spareAsBytes,
12145 + &yaffs_noeccinfo);
12149 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12153 + mtd->write_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12154 + &dummy, spareAsBytes);
12161 + return YAFFS_FAIL;
12164 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12165 + yaffs_Spare * spare)
12167 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12168 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12169 + struct mtd_oob_ops ops;
12174 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
12175 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12176 + __u8 spareAsBytes[8]; /* OOB */
12178 + if (data && !spare)
12179 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
12181 + else if (spare) {
12182 + if (dev->useNANDECC) {
12183 + ops.mode = MTD_OOB_AUTO;
12184 + ops.ooblen = 8; /* temp hack */
12186 + ops.mode = MTD_OOB_RAW;
12187 + ops.ooblen = YAFFS_BYTES_PER_SPARE;
12189 + ops.len = data ? dev->nDataBytesPerChunk : ops.ooblen;
12190 + ops.datbuf = data;
12192 + ops.oobbuf = spareAsBytes;
12193 + retval = mtd->read_oob(mtd, addr, &ops);
12194 + if (dev->useNANDECC)
12195 + translate_oob2spare(spare, spareAsBytes);
12198 + __u8 *spareAsBytes = (__u8 *) spare;
12200 + if (data && spare) {
12201 + if (dev->useNANDECC) {
12202 + /* Careful, this call adds 2 ints */
12203 + /* to the end of the spare data. Calling function */
12204 + /* should allocate enough memory for spare, */
12205 + /* i.e. [YAFFS_BYTES_PER_SPARE+2*sizeof(int)]. */
12207 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12208 + &dummy, data, spareAsBytes,
12212 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
12213 + &dummy, data, spareAsBytes,
12214 + &yaffs_noeccinfo);
12219 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
12223 + mtd->read_oob(mtd, addr, YAFFS_BYTES_PER_SPARE,
12224 + &dummy, spareAsBytes);
12231 + return YAFFS_FAIL;
12234 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber)
12236 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
12238 + ((loff_t) blockNumber) * dev->nDataBytesPerChunk
12239 + * dev->nChunksPerBlock;
12240 + struct erase_info ei;
12245 + ei.len = dev->nDataBytesPerChunk * dev->nChunksPerBlock;
12248 + ei.callback = NULL;
12249 + ei.priv = (u_long) dev;
12251 + /* Todo finish off the ei if required */
12253 + sema_init(&dev->sem, 0);
12255 + retval = mtd->erase(mtd, &ei);
12260 + return YAFFS_FAIL;
12263 +int nandmtd_InitialiseNAND(yaffs_Device * dev)
12268 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h
12269 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif.h 1970-01-01 01:00:00.000000000 +0100
12270 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif.h 2007-05-30 13:17:17.000000000 +0200
12273 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
12275 + * Copyright (C) 2002-2007 Aleph One Ltd.
12276 + * for Toby Churchill Ltd and Brightstar Engineering
12278 + * Created by Charles Manning <charles@aleph1.co.uk>
12280 + * This program is free software; you can redistribute it and/or modify
12281 + * it under the terms of the GNU Lesser General Public License version 2.1 as
12282 + * published by the Free Software Foundation.
12284 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
12287 +#ifndef __YAFFS_MTDIF_H__
12288 +#define __YAFFS_MTDIF_H__
12290 +#include "yaffs_guts.h"
12292 +int nandmtd_WriteChunkToNAND(yaffs_Device * dev, int chunkInNAND,
12293 + const __u8 * data, const yaffs_Spare * spare);
12294 +int nandmtd_ReadChunkFromNAND(yaffs_Device * dev, int chunkInNAND, __u8 * data,
12295 + yaffs_Spare * spare);
12296 +int nandmtd_EraseBlockInNAND(yaffs_Device * dev, int blockNumber);
12297 +int nandmtd_InitialiseNAND(yaffs_Device * dev);
12299 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c
12300 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1-compat.c 1970-01-01 01:00:00.000000000 +0100
12301 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1-compat.c 2007-05-30 13:17:16.000000000 +0200
12303 +From ian@brightstareng.com Fri May 18 15:06:49 2007
12304 +From ian@brightstareng.com Fri May 18 15:08:21 2007
12305 +Received: from 206.173.66.57.ptr.us.xo.net ([206.173.66.57] helo=zebra.brightstareng.com)
12306 + by apollo.linkchoose.co.uk with esmtp (Exim 4.60)
12307 + (envelope-from <ian@brightstareng.com>)
12308 + id 1Hp380-00011e-T6
12309 + for david.goodenough@linkchoose.co.uk; Fri, 18 May 2007 15:08:21 +0100
12310 +Received: from localhost (localhost.localdomain [127.0.0.1])
12311 + by zebra.brightstareng.com (Postfix) with ESMTP
12312 + id 4819F28C004; Fri, 18 May 2007 10:07:49 -0400 (EDT)
12313 +Received: from zebra.brightstareng.com ([127.0.0.1])
12314 + by localhost (zebra [127.0.0.1]) (amavisd-new, port 10024) with ESMTP
12315 + id 05328-06; Fri, 18 May 2007 10:07:16 -0400 (EDT)
12316 +Received: from pippin (unknown [192.168.1.25])
12317 + by zebra.brightstareng.com (Postfix) with ESMTP
12318 + id 8BEF528C1BC; Fri, 18 May 2007 10:06:53 -0400 (EDT)
12319 +From: Ian McDonnell <ian@brightstareng.com>
12320 +To: David Goodenough <david.goodenough@linkchoose.co.uk>
12321 +Subject: Re: something tested this time -- yaffs_mtdif1-compat.c
12322 +Date: Fri, 18 May 2007 10:06:49 -0400
12323 +User-Agent: KMail/1.9.1
12324 +References: <200705142207.06909.ian@brightstareng.com> <200705171131.53536.ian@brightstareng.com> <200705181334.32166.david.goodenough@linkchoose.co.uk>
12325 +In-Reply-To: <200705181334.32166.david.goodenough@linkchoose.co.uk>
12326 +Cc: Andrea Conti <alyf@alyf.net>,
12327 + Charles Manning <manningc2@actrix.gen.nz>
12329 +Content-Type: Multipart/Mixed;
12330 + boundary="Boundary-00=_5LbTGmt62YoutxM"
12331 +Message-Id: <200705181006.49860.ian@brightstareng.com>
12332 +X-Virus-Scanned: by amavisd-new at brightstareng.com
12335 +X-KMail-EncryptionState:
12336 +X-KMail-SignatureState:
12339 +--Boundary-00=_5LbTGmt62YoutxM
12340 +Content-Type: text/plain;
12341 + charset="iso-8859-15"
12342 +Content-Transfer-Encoding: 7bit
12343 +Content-Disposition: inline
12347 +On Friday 18 May 2007 08:34, you wrote:
12348 +> Yea team. With this fix in place (I put it in the wrong place
12349 +> at first) I can now mount and ls the Yaffs partition without
12350 +> an error messages!
12354 +Attached is a newer yaffs_mtdif1.c with a bandaid to help the
12355 +2.6.18 and 2.6.19 versions of MTD not trip on the oob read.
12356 +See the LINUX_VERSION_CODE conditional in
12357 +nandmtd1_ReadChunkWithTagsFromNAND.
12361 +--Boundary-00=_5LbTGmt62YoutxM
12362 +Content-Type: text/x-csrc;
12363 + charset="iso-8859-15";
12364 + name="yaffs_mtdif1.c"
12365 +Content-Transfer-Encoding: 7bit
12366 +Content-Disposition: attachment;
12367 + filename="yaffs_mtdif1.c"
12370 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12371 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12373 + * Copyright (C) 2002 Aleph One Ltd.
12374 + * for Toby Churchill Ltd and Brightstar Engineering
12376 + * This program is free software; you can redistribute it and/or modify
12377 + * it under the terms of the GNU General Public License version 2 as
12378 + * published by the Free Software Foundation.
12382 + * This module provides the interface between yaffs_nand.c and the
12383 + * MTD API. This version is used when the MTD interface supports the
12384 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12385 + * and we have small-page NAND device.
12387 + * These functions are invoked via function pointers in yaffs_nand.c.
12388 + * This replaces functionality provided by functions in yaffs_mtdif.c
12389 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12390 + * called in yaffs_mtdif.c when the function pointers are NULL.
12391 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12394 +#include "yportenv.h"
12395 +#include "yaffs_guts.h"
12396 +#include "yaffs_packedtags1.h"
12397 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12399 +#include "linux/kernel.h"
12400 +#include "linux/version.h"
12401 +#include "linux/types.h"
12402 +#include "linux/mtd/mtd.h"
12404 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12405 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12407 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12409 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12410 +# define YTAG1_SIZE 8
12412 +# define YTAG1_SIZE 9
12416 +/* Use the following nand_ecclayout with MTD when using
12417 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12418 + * If you have existing Yaffs images and the byte order differs from this,
12419 + * adjust 'oobfree' to match your existing Yaffs data.
12421 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12422 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12425 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12426 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12427 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12428 + * byte and B is the small-page bad-block indicator byte.
12430 +static struct nand_ecclayout nand_oob_16 = {
12432 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12434 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12438 +/* Write a chunk (page) of data to NAND.
12440 + * Caller always provides ExtendedTags data which are converted to a more
12441 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12442 + * contents of the tags meta-data; used to valid the tags when read.
12444 + * - Pack ExtendedTags to PackedTags1 form
12445 + * - Compute mini-ECC for PackedTags1
12446 + * - Write data and packed tags to NAND.
12448 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12449 + * a full sequence number (as found in the larger PackedTags2 form) it is
12450 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12451 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12452 + * to be written just once. When Yaffs performs this operation, this
12453 + * function is called with a NULL data pointer -- calling MTD write_oob
12454 + * without data is valid usage (2.6.17).
12456 + * Any underlying MTD error results in YAFFS_FAIL.
12457 + * Returns YAFFS_OK or YAFFS_FAIL.
12459 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12460 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12462 + struct mtd_info * mtd = dev->genericDevice;
12463 + int chunkBytes = dev->nDataBytesPerChunk;
12464 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12465 + struct mtd_oob_ops ops;
12466 + yaffs_PackedTags1 pt1;
12469 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12470 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12471 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12473 + yaffs_PackTags1(&pt1, etags);
12474 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12476 + /* When deleting a chunk, the upper layer provides only skeletal
12477 + * etags, one with chunkDeleted set. However, we need to update the
12478 + * tags, not erase them completely. So we use the NAND write property
12479 + * that only zeroed-bits stick and set tag bytes to all-ones and
12480 + * zero just the (not) deleted bit.
12482 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12483 + if (etags->chunkDeleted) {
12484 + memset(&pt1, 0xff, 8);
12485 + /* clear delete status bit to indicate deleted */
12489 + ((__u8 *)&pt1)[8] = 0xff;
12490 + if (etags->chunkDeleted) {
12491 + memset(&pt1, 0xff, 8);
12492 + /* zero pageStatus byte to indicate deleted */
12493 + ((__u8 *)&pt1)[8] = 0;
12497 + memset(&ops, 0, sizeof(ops));
12498 + ops.mode = MTD_OOB_AUTO;
12499 + ops.len = (data) ? chunkBytes : 0;
12500 + ops.ooblen = YTAG1_SIZE;
12501 + ops.datbuf = (__u8 *)data;
12502 + ops.oobbuf = (__u8 *)&pt1;
12504 + retval = mtd->write_oob(mtd, addr, &ops);
12506 + yaffs_trace(YAFFS_TRACE_MTD,
12507 + "write_oob failed, chunk %d, mtd error %d\n",
12508 + chunkInNAND, retval);
12510 + return retval ? YAFFS_FAIL : YAFFS_OK;
12513 +/* Return with empty ExtendedTags but add eccResult.
12515 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12518 + memset(etags, 0, sizeof(*etags));
12519 + etags->eccResult = eccResult;
12524 +/* Read a chunk (page) from NAND.
12526 + * Caller expects ExtendedTags data to be usable even on error; that is,
12527 + * all members except eccResult and blockBad are zeroed.
12529 + * - Check ECC results for data (if applicable)
12530 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12531 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12532 + * - Convert PackedTags1 to ExtendedTags
12533 + * - Update eccResult and blockBad members to refect state.
12535 + * Returns YAFFS_OK or YAFFS_FAIL.
12537 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12538 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12540 + struct mtd_info * mtd = dev->genericDevice;
12541 + int chunkBytes = dev->nDataBytesPerChunk;
12542 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12543 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12544 + struct mtd_oob_ops ops;
12545 + yaffs_PackedTags1 pt1;
12549 + memset(&ops, 0, sizeof(ops));
12550 + ops.mode = MTD_OOB_AUTO;
12551 + ops.len = (data) ? chunkBytes : 0;
12552 + ops.ooblen = YTAG1_SIZE;
12553 + ops.datbuf = data;
12554 + ops.oobbuf = (__u8 *)&pt1;
12556 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20))
12557 + /* In MTD 2.6.18 to 2.6.19 nand_base.c:nand_do_read_oob() has a bug;
12558 + * help it out with ops.len = ops.ooblen when ops.datbuf == NULL.
12560 + ops.len = (ops.datbuf) ? ops.len : ops.ooblen;
12562 + /* Read page and oob using MTD.
12563 + * Check status and determine ECC result.
12565 + retval = mtd->read_oob(mtd, addr, &ops);
12567 + yaffs_trace(YAFFS_TRACE_MTD,
12568 + "read_oob failed, chunk %d, mtd error %d\n",
12569 + chunkInNAND, retval);
12572 + switch (retval) {
12578 + /* MTD's ECC fixed the data */
12579 + eccres = YAFFS_ECC_RESULT_FIXED;
12584 + /* MTD's ECC could not fix the data */
12585 + dev->eccUnfixed++;
12586 + /* fall into... */
12588 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12589 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12590 + return YAFFS_FAIL;
12593 + /* Check for a blank/erased chunk.
12595 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12596 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12597 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12600 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12601 + /* Read deleted status (bit) then return it to it's non-deleted
12602 + * state before performing tags mini-ECC check. pt1.deleted is
12605 + deleted = !pt1.deleted;
12608 + (void) deleted; /* not used */
12611 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12613 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12614 + switch (retval) {
12616 + /* no tags error, use MTD result */
12619 + /* recovered tags-ECC error */
12620 + dev->tagsEccFixed++;
12621 + eccres = YAFFS_ECC_RESULT_FIXED;
12624 + /* unrecovered tags-ECC error */
12625 + dev->tagsEccUnfixed++;
12626 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12629 + /* Unpack the tags to extended form and set ECC result.
12630 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12632 + pt1.shouldBeFF = 0xFFFFFFFF;
12633 + yaffs_UnpackTags1(etags, &pt1);
12634 + etags->eccResult = eccres;
12636 + /* Set deleted state.
12638 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12639 + etags->chunkDeleted = deleted;
12641 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
12646 +/* Mark a block bad.
12648 + * This is a persistant state.
12649 + * Use of this function should be rare.
12651 + * Returns YAFFS_OK or YAFFS_FAIL.
12653 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
12655 + struct mtd_info * mtd = dev->genericDevice;
12656 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
12659 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
12661 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
12662 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
12665 +/* Check any MTD prerequists.
12667 + * Returns YAFFS_OK or YAFFS_FAIL.
12669 +static int nandmtd1_TestPrerequists(struct mtd_info * mtd)
12671 + /* 2.6.18 has mtd->ecclayout->oobavail */
12672 + /* 2.6.21 has mtd->ecclayout->oobavail and mtd->oobavail */
12673 + int oobavail = mtd->ecclayout->oobavail;
12675 + if (oobavail < YTAG1_SIZE) {
12676 + yaffs_trace(YAFFS_TRACE_ERROR,
12677 + "mtd device has only %d bytes for tags, need %d",
12678 + oobavail, YTAG1_SIZE);
12679 + return YAFFS_FAIL;
12684 +/* Query for the current state of a specific block.
12686 + * Examine the tags of the first chunk of the block and return the state:
12687 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
12688 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
12689 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
12691 + * Always returns YAFFS_OK.
12693 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
12694 + yaffs_BlockState * pState, int *pSequenceNumber)
12696 + struct mtd_info * mtd = dev->genericDevice;
12697 + int chunkNo = blockNo * dev->nChunksPerBlock;
12698 + yaffs_ExtendedTags etags;
12699 + int state = YAFFS_BLOCK_STATE_DEAD;
12703 + /* We don't yet have a good place to test for MTD config prerequists.
12704 + * Do it here as we are called during the initial scan.
12706 + if (nandmtd1_TestPrerequists(mtd) != YAFFS_OK) {
12707 + return YAFFS_FAIL;
12710 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
12711 + if (etags.blockBad) {
12712 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
12713 + "block %d is marked bad", blockNo);
12714 + state = YAFFS_BLOCK_STATE_DEAD;
12716 + else if (etags.chunkUsed) {
12717 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
12718 + seqnum = etags.sequenceNumber;
12721 + state = YAFFS_BLOCK_STATE_EMPTY;
12725 + *pSequenceNumber = seqnum;
12727 + /* query always succeeds */
12731 +#endif /*KERNEL_VERSION*/
12733 +--Boundary-00=_5LbTGmt62YoutxM--
12737 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c
12738 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.c 1970-01-01 01:00:00.000000000 +0100
12739 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.c 2007-05-30 13:17:16.000000000 +0200
12742 + * YAFFS: Yet another FFS. A NAND-flash specific file system.
12743 + * yaffs_mtdif1.c NAND mtd interface functions for small-page NAND.
12745 + * Copyright (C) 2002 Aleph One Ltd.
12746 + * for Toby Churchill Ltd and Brightstar Engineering
12748 + * This program is free software; you can redistribute it and/or modify
12749 + * it under the terms of the GNU General Public License version 2 as
12750 + * published by the Free Software Foundation.
12754 + * This module provides the interface between yaffs_nand.c and the
12755 + * MTD API. This version is used when the MTD interface supports the
12756 + * 'mtd_oob_ops' style calls to read_oob and write_oob, circa 2.6.17,
12757 + * and we have small-page NAND device.
12759 + * These functions are invoked via function pointers in yaffs_nand.c.
12760 + * This replaces functionality provided by functions in yaffs_mtdif.c
12761 + * and the yaffs_TagsCompatability functions in yaffs_tagscompat.c that are
12762 + * called in yaffs_mtdif.c when the function pointers are NULL.
12763 + * We assume the MTD layer is performing ECC (useNANDECC is true).
12766 +#include "yportenv.h"
12767 +#include "yaffs_guts.h"
12768 +#include "yaffs_packedtags1.h"
12769 +#include "yaffs_tagscompat.h" // for yaffs_CalcTagsECC
12771 +#include "linux/kernel.h"
12772 +#include "linux/version.h"
12773 +#include "linux/types.h"
12774 +#include "linux/mtd/mtd.h"
12776 +/* Don't compile this module if we don't have MTD's mtd_oob_ops interface */
12777 +/* should really be >= .17, but elsewhere > .17 is used, so be consistent */
12778 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
12780 +const char *yaffs_mtdif1_c_version = "$Id: yaffs_mtdif1.c,v 1.3 2007/05/15 20:16:11 ian Exp $";
12782 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12783 +# define YTAG1_SIZE 8
12785 +# define YTAG1_SIZE 9
12789 +/* Use the following nand_ecclayout with MTD when using
12790 + * CONFIG_YAFFS_9BYTE_TAGS and the older on-NAND tags layout.
12791 + * If you have existing Yaffs images and the byte order differs from this,
12792 + * adjust 'oobfree' to match your existing Yaffs data.
12794 + * This nand_ecclayout scatters/gathers to/from the old-yaffs layout with the
12795 + * pageStatus byte (at NAND spare offset 4) scattered/gathered from/to
12798 + * Old-style on-NAND format: T0,T1,T2,T3,P,B,T4,T5,E0,E1,E2,T6,T7,E3,E4,E5
12799 + * We have/need PackedTags1 plus pageStatus: T0,T1,T2,T3,T4,T5,T6,T7,P
12800 + * where Tn are the tag bytes, En are MTD's ECC bytes, P is the pageStatus
12801 + * byte and B is the small-page bad-block indicator byte.
12803 +static struct nand_ecclayout nand_oob_16 = {
12805 + .eccpos = { 8, 9, 10, 13, 14, 15 },
12807 + .oobfree = { { 0, 4 }, { 6, 2 }, { 11, 2 }, { 4, 1 } }
12811 +/* Write a chunk (page) of data to NAND.
12813 + * Caller always provides ExtendedTags data which are converted to a more
12814 + * compact (packed) form for storage in NAND. A mini-ECC runs over the
12815 + * contents of the tags meta-data; used to valid the tags when read.
12817 + * - Pack ExtendedTags to PackedTags1 form
12818 + * - Compute mini-ECC for PackedTags1
12819 + * - Write data and packed tags to NAND.
12821 + * Note: Due to the use of the PackedTags1 meta-data which does not include
12822 + * a full sequence number (as found in the larger PackedTags2 form) it is
12823 + * necessary for Yaffs to re-write a chunk/page (just once) to mark it as
12824 + * discarded and dirty. This is not ideal: newer NAND parts are supposed
12825 + * to be written just once. When Yaffs performs this operation, this
12826 + * function is called with a NULL data pointer -- calling MTD write_oob
12827 + * without data is valid usage (2.6.17).
12829 + * Any underlying MTD error results in YAFFS_FAIL.
12830 + * Returns YAFFS_OK or YAFFS_FAIL.
12832 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device *dev,
12833 + int chunkInNAND, const __u8 * data, const yaffs_ExtendedTags * etags)
12835 + struct mtd_info * mtd = dev->genericDevice;
12836 + int chunkBytes = dev->nDataBytesPerChunk;
12837 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12838 + struct mtd_oob_ops ops;
12839 + yaffs_PackedTags1 pt1;
12842 + /* we assume that PackedTags1 and yaffs_Tags are compatible */
12843 + compile_time_assertion(sizeof(yaffs_PackedTags1) == 12);
12844 + compile_time_assertion(sizeof(yaffs_Tags) == 8);
12846 + yaffs_PackTags1(&pt1, etags);
12847 + yaffs_CalcTagsECC((yaffs_Tags *)&pt1);
12849 + /* When deleting a chunk, the upper layer provides only skeletal
12850 + * etags, one with chunkDeleted set. However, we need to update the
12851 + * tags, not erase them completely. So we use the NAND write property
12852 + * that only zeroed-bits stick and set tag bytes to all-ones and
12853 + * zero just the (not) deleted bit.
12855 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12856 + if (etags->chunkDeleted) {
12857 + memset(&pt1, 0xff, 8);
12858 + /* clear delete status bit to indicate deleted */
12862 + ((__u8 *)&pt1)[8] = 0xff;
12863 + if (etags->chunkDeleted) {
12864 + memset(&pt1, 0xff, 8);
12865 + /* zero pageStatus byte to indicate deleted */
12866 + ((__u8 *)&pt1)[8] = 0;
12870 + memset(&ops, 0, sizeof(ops));
12871 + ops.mode = MTD_OOB_AUTO;
12872 + ops.len = (data) ? chunkBytes : 0;
12873 + ops.ooblen = YTAG1_SIZE;
12874 + ops.datbuf = (__u8 *)data;
12875 + ops.oobbuf = (__u8 *)&pt1;
12877 + retval = mtd->write_oob(mtd, addr, &ops);
12879 + yaffs_trace(YAFFS_TRACE_MTD,
12880 + "write_oob failed, chunk %d, mtd error %d\n",
12881 + chunkInNAND, retval);
12883 + return retval ? YAFFS_FAIL : YAFFS_OK;
12886 +/* Return with empty ExtendedTags but add eccResult.
12888 +static int rettags(yaffs_ExtendedTags * etags, int eccResult, int retval)
12891 + memset(etags, 0, sizeof(*etags));
12892 + etags->eccResult = eccResult;
12897 +/* Read a chunk (page) from NAND.
12899 + * Caller expects ExtendedTags data to be usable even on error; that is,
12900 + * all members except eccResult and blockBad are zeroed.
12902 + * - Check ECC results for data (if applicable)
12903 + * - Check for blank/erased block (return empty ExtendedTags if blank)
12904 + * - Check the PackedTags1 mini-ECC (correct if necessary/possible)
12905 + * - Convert PackedTags1 to ExtendedTags
12906 + * - Update eccResult and blockBad members to refect state.
12908 + * Returns YAFFS_OK or YAFFS_FAIL.
12910 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device *dev,
12911 + int chunkInNAND, __u8 * data, yaffs_ExtendedTags * etags)
12913 + struct mtd_info * mtd = dev->genericDevice;
12914 + int chunkBytes = dev->nDataBytesPerChunk;
12915 + loff_t addr = ((loff_t)chunkInNAND) * chunkBytes;
12916 + int eccres = YAFFS_ECC_RESULT_NO_ERROR;
12917 + struct mtd_oob_ops ops;
12918 + yaffs_PackedTags1 pt1;
12922 + memset(&ops, 0, sizeof(ops));
12923 + ops.mode = MTD_OOB_AUTO;
12924 + ops.len = (data) ? chunkBytes : 0;
12925 + ops.ooblen = YTAG1_SIZE;
12926 + ops.datbuf = data;
12927 + ops.oobbuf = (__u8 *)&pt1;
12929 + /* Read page and oob using MTD.
12930 + * Check status and determine ECC result.
12932 + retval = mtd->read_oob(mtd, addr, &ops);
12934 + yaffs_trace(YAFFS_TRACE_MTD,
12935 + "read_oob failed, chunk %d, mtd error %d\n",
12936 + chunkInNAND, retval);
12939 + switch (retval) {
12945 + /* MTD's ECC fixed the data */
12946 + eccres = YAFFS_ECC_RESULT_FIXED;
12951 + /* MTD's ECC could not fix the data */
12952 + dev->eccUnfixed++;
12953 + /* fall into... */
12955 + rettags(etags, YAFFS_ECC_RESULT_UNFIXED, 0);
12956 + etags->blockBad = (mtd->block_isbad)(mtd, addr);
12957 + return YAFFS_FAIL;
12960 + /* Check for a blank/erased chunk.
12962 + if (yaffs_CheckFF((__u8 *)&pt1, 8)) {
12963 + /* when blank, upper layers want eccResult to be <= NO_ERROR */
12964 + return rettags(etags, YAFFS_ECC_RESULT_NO_ERROR, YAFFS_OK);
12967 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
12968 + /* Read deleted status (bit) then return it to it's non-deleted
12969 + * state before performing tags mini-ECC check. pt1.deleted is
12972 + deleted = !pt1.deleted;
12975 + (void) deleted; /* not used */
12978 + /* Check the packed tags mini-ECC and correct if necessary/possible.
12980 + retval = yaffs_CheckECCOnTags((yaffs_Tags *)&pt1);
12981 + switch (retval) {
12983 + /* no tags error, use MTD result */
12986 + /* recovered tags-ECC error */
12987 + dev->tagsEccFixed++;
12988 + eccres = YAFFS_ECC_RESULT_FIXED;
12991 + /* unrecovered tags-ECC error */
12992 + dev->tagsEccUnfixed++;
12993 + return rettags(etags, YAFFS_ECC_RESULT_UNFIXED, YAFFS_FAIL);
12996 + /* Unpack the tags to extended form and set ECC result.
12997 + * [set shouldBeFF just to keep yaffs_UnpackTags1 happy]
12999 + pt1.shouldBeFF = 0xFFFFFFFF;
13000 + yaffs_UnpackTags1(etags, &pt1);
13001 + etags->eccResult = eccres;
13003 + /* Set deleted state.
13005 +#ifndef CONFIG_YAFFS_9BYTE_TAGS
13006 + etags->chunkDeleted = deleted;
13008 + etags->chunkDeleted = (yaffs_CountBits(((__u8 *)&pt1)[8]) < 7);
13013 +/* Mark a block bad.
13015 + * This is a persistant state.
13016 + * Use of this function should be rare.
13018 + * Returns YAFFS_OK or YAFFS_FAIL.
13020 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13022 + struct mtd_info * mtd = dev->genericDevice;
13023 + int blocksize = dev->nChunksPerBlock * dev->nDataBytesPerChunk;
13026 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS, "marking block %d bad", blockNo);
13028 + retval = mtd->block_markbad(mtd, (loff_t)blocksize * blockNo);
13029 + return (retval) ? YAFFS_FAIL : YAFFS_OK;
13032 +/* Query for the current state of a specific block.
13034 + * Examine the tags of the first chunk of the block and return the state:
13035 + * - YAFFS_BLOCK_STATE_DEAD, the block is marked bad
13036 + * - YAFFS_BLOCK_STATE_NEEDS_SCANNING, the block is in use
13037 + * - YAFFS_BLOCK_STATE_EMPTY, the block is clean
13039 + * Always returns YAFFS_OK.
13041 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13042 + yaffs_BlockState * pState, int *pSequenceNumber)
13044 + struct mtd_info * mtd = dev->genericDevice;
13045 + int chunkNo = blockNo * dev->nChunksPerBlock;
13046 + yaffs_ExtendedTags etags;
13047 + int state = YAFFS_BLOCK_STATE_DEAD;
13051 + if (mtd->oobavail < YTAG1_SIZE) {
13052 + yaffs_trace(YAFFS_TRACE_ERROR,
13053 + "mtd device has only %d bytes for tags, need %d",
13054 + mtd->oobavail, YTAG1_SIZE);
13055 + return YAFFS_FAIL;
13058 + retval = nandmtd1_ReadChunkWithTagsFromNAND(dev, chunkNo, NULL, &etags);
13059 + if (etags.blockBad) {
13060 + yaffs_trace(YAFFS_TRACE_BAD_BLOCKS,
13061 + "block %d is marked bad", blockNo);
13062 + state = YAFFS_BLOCK_STATE_DEAD;
13064 + else if (etags.chunkUsed) {
13065 + state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13066 + seqnum = etags.sequenceNumber;
13069 + state = YAFFS_BLOCK_STATE_EMPTY;
13073 + *pSequenceNumber = seqnum;
13075 + /* query always succeeds */
13079 +#endif /*KERNEL_VERSION*/
13080 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h
13081 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif1.h 1970-01-01 01:00:00.000000000 +0100
13082 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif1.h 2007-05-30 13:17:17.000000000 +0200
13085 + * YAFFS: Yet another Flash File System. A NAND-flash specific file system.
13087 + * Copyright (C) 2002-2007 Aleph One Ltd.
13088 + * for Toby Churchill Ltd and Brightstar Engineering
13090 + * This program is free software; you can redistribute it and/or modify
13091 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13092 + * published by the Free Software Foundation.
13094 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13097 +#ifndef __YAFFS_MTDIF1_H__
13098 +#define __YAFFS_MTDIF1_H__
13100 +int nandmtd1_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13101 + const __u8 * data, const yaffs_ExtendedTags * tags);
13103 +int nandmtd1_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13104 + __u8 * data, yaffs_ExtendedTags * tags);
13106 +int nandmtd1_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13108 +int nandmtd1_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13109 + yaffs_BlockState * state, int *sequenceNumber);
13112 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c
13113 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.c 1970-01-01 01:00:00.000000000 +0100
13114 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.c 2007-05-30 13:17:17.000000000 +0200
13117 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13119 + * Copyright (C) 2002-2007 Aleph One Ltd.
13120 + * for Toby Churchill Ltd and Brightstar Engineering
13122 + * Created by Charles Manning <charles@aleph1.co.uk>
13124 + * This program is free software; you can redistribute it and/or modify
13125 + * it under the terms of the GNU General Public License version 2 as
13126 + * published by the Free Software Foundation.
13129 +/* mtd interface for YAFFS2 */
13131 +const char *yaffs_mtdif2_c_version =
13132 + "$Id: yaffs_mtdif2.c,v 1.17 2007-02-14 01:09:06 wookey Exp $";
13134 +#include "yportenv.h"
13137 +#include "yaffs_mtdif2.h"
13139 +#include "linux/mtd/mtd.h"
13140 +#include "linux/types.h"
13141 +#include "linux/time.h"
13143 +#include "yaffs_packedtags2.h"
13145 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13146 + const __u8 * data,
13147 + const yaffs_ExtendedTags * tags)
13149 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13150 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13151 + struct mtd_oob_ops ops;
13157 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13159 + yaffs_PackedTags2 pt;
13161 + T(YAFFS_TRACE_MTD,
13163 + ("nandmtd2_WriteChunkWithTagsToNAND chunk %d data %p tags %p"
13164 + TENDSTR), chunkInNAND, data, tags));
13166 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13168 + yaffs_PackTags2(&pt, tags);
13170 + BUG(); /* both tags and data should always be present */
13173 + ops.mode = MTD_OOB_AUTO;
13174 + ops.ooblen = sizeof(pt);
13175 + ops.len = dev->nDataBytesPerChunk;
13177 + ops.datbuf = (__u8 *)data;
13178 + ops.oobbuf = (void *)&pt;
13179 + retval = mtd->write_oob(mtd, addr, &ops);
13181 + BUG(); /* both tags and data should always be present */
13184 + yaffs_PackTags2(&pt, tags);
13187 + if (data && tags) {
13188 + if (dev->useNANDECC)
13190 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13191 + &dummy, data, (__u8 *) & pt, NULL);
13194 + mtd->write_ecc(mtd, addr, dev->nDataBytesPerChunk,
13195 + &dummy, data, (__u8 *) & pt, NULL);
13199 + mtd->write(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13203 + mtd->write_oob(mtd, addr, mtd->oobsize, &dummy,
13212 + return YAFFS_FAIL;
13215 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13216 + __u8 * data, yaffs_ExtendedTags * tags)
13218 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13219 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13220 + struct mtd_oob_ops ops;
13225 + loff_t addr = ((loff_t) chunkInNAND) * dev->nDataBytesPerChunk;
13227 + yaffs_PackedTags2 pt;
13229 + T(YAFFS_TRACE_MTD,
13231 + ("nandmtd2_ReadChunkWithTagsFromNAND chunk %d data %p tags %p"
13232 + TENDSTR), chunkInNAND, data, tags));
13234 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,17))
13235 + if (data && !tags)
13236 + retval = mtd->read(mtd, addr, dev->nDataBytesPerChunk,
13239 + ops.mode = MTD_OOB_AUTO;
13240 + ops.ooblen = sizeof(pt);
13241 + ops.len = data ? dev->nDataBytesPerChunk : sizeof(pt);
13243 + ops.datbuf = data;
13244 + ops.oobbuf = dev->spareBuffer;
13245 + retval = mtd->read_oob(mtd, addr, &ops);
13248 + if (data && tags) {
13249 + if (dev->useNANDECC) {
13251 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13252 + &dummy, data, dev->spareBuffer,
13256 + mtd->read_ecc(mtd, addr, dev->nDataBytesPerChunk,
13257 + &dummy, data, dev->spareBuffer,
13263 + mtd->read(mtd, addr, dev->nDataBytesPerChunk, &dummy,
13267 + mtd->read_oob(mtd, addr, mtd->oobsize, &dummy,
13268 + dev->spareBuffer);
13272 + memcpy(&pt, dev->spareBuffer, sizeof(pt));
13275 + yaffs_UnpackTags2(tags, &pt);
13277 + if(tags && retval == -EBADMSG && tags->eccResult == YAFFS_ECC_RESULT_NO_ERROR)
13278 + tags->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13283 + return YAFFS_FAIL;
13286 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo)
13288 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13290 + T(YAFFS_TRACE_MTD,
13291 + (TSTR("nandmtd2_MarkNANDBlockBad %d" TENDSTR), blockNo));
13294 + mtd->block_markbad(mtd,
13295 + blockNo * dev->nChunksPerBlock *
13296 + dev->nDataBytesPerChunk);
13301 + return YAFFS_FAIL;
13305 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13306 + yaffs_BlockState * state, int *sequenceNumber)
13308 + struct mtd_info *mtd = (struct mtd_info *)(dev->genericDevice);
13311 + T(YAFFS_TRACE_MTD,
13312 + (TSTR("nandmtd2_QueryNANDBlock %d" TENDSTR), blockNo));
13314 + mtd->block_isbad(mtd,
13315 + blockNo * dev->nChunksPerBlock *
13316 + dev->nDataBytesPerChunk);
13319 + T(YAFFS_TRACE_MTD, (TSTR("block is bad" TENDSTR)));
13321 + *state = YAFFS_BLOCK_STATE_DEAD;
13322 + *sequenceNumber = 0;
13324 + yaffs_ExtendedTags t;
13325 + nandmtd2_ReadChunkWithTagsFromNAND(dev,
13327 + dev->nChunksPerBlock, NULL,
13330 + if (t.chunkUsed) {
13331 + *sequenceNumber = t.sequenceNumber;
13332 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
13334 + *sequenceNumber = 0;
13335 + *state = YAFFS_BLOCK_STATE_EMPTY;
13338 + T(YAFFS_TRACE_MTD,
13339 + (TSTR("block is bad seq %d state %d" TENDSTR), *sequenceNumber,
13345 + return YAFFS_FAIL;
13348 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h
13349 --- linux-2.6.21.1/fs/yaffs2/yaffs_mtdif2.h 1970-01-01 01:00:00.000000000 +0100
13350 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_mtdif2.h 2007-05-30 13:17:17.000000000 +0200
13353 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13355 + * Copyright (C) 2002-2007 Aleph One Ltd.
13356 + * for Toby Churchill Ltd and Brightstar Engineering
13358 + * Created by Charles Manning <charles@aleph1.co.uk>
13360 + * This program is free software; you can redistribute it and/or modify
13361 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13362 + * published by the Free Software Foundation.
13364 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13367 +#ifndef __YAFFS_MTDIF2_H__
13368 +#define __YAFFS_MTDIF2_H__
13370 +#include "yaffs_guts.h"
13371 +int nandmtd2_WriteChunkWithTagsToNAND(yaffs_Device * dev, int chunkInNAND,
13372 + const __u8 * data,
13373 + const yaffs_ExtendedTags * tags);
13374 +int nandmtd2_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13375 + __u8 * data, yaffs_ExtendedTags * tags);
13376 +int nandmtd2_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13377 +int nandmtd2_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13378 + yaffs_BlockState * state, int *sequenceNumber);
13381 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.c linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c
13382 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.c 1970-01-01 01:00:00.000000000 +0100
13383 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.c 2007-05-30 13:17:17.000000000 +0200
13386 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13388 + * Copyright (C) 2002-2007 Aleph One Ltd.
13389 + * for Toby Churchill Ltd and Brightstar Engineering
13391 + * Created by Charles Manning <charles@aleph1.co.uk>
13393 + * This program is free software; you can redistribute it and/or modify
13394 + * it under the terms of the GNU General Public License version 2 as
13395 + * published by the Free Software Foundation.
13398 +const char *yaffs_nand_c_version =
13399 + "$Id: yaffs_nand.c,v 1.7 2007-02-14 01:09:06 wookey Exp $";
13401 +#include "yaffs_nand.h"
13402 +#include "yaffs_tagscompat.h"
13403 +#include "yaffs_tagsvalidity.h"
13406 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13408 + yaffs_ExtendedTags * tags)
13411 + yaffs_ExtendedTags localTags;
13413 + int realignedChunkInNAND = chunkInNAND - dev->chunkOffset;
13415 + /* If there are no tags provided, use local tags to get prioritised gc working */
13417 + tags = &localTags;
13419 + if (dev->readChunkWithTagsFromNAND)
13420 + result = dev->readChunkWithTagsFromNAND(dev, realignedChunkInNAND, buffer,
13423 + result = yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(dev,
13424 + realignedChunkInNAND,
13428 + tags->eccResult > YAFFS_ECC_RESULT_NO_ERROR){
13430 + yaffs_BlockInfo *bi = yaffs_GetBlockInfo(dev, chunkInNAND/dev->nChunksPerBlock);
13431 + yaffs_HandleChunkError(dev,bi);
13437 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13439 + const __u8 * buffer,
13440 + yaffs_ExtendedTags * tags)
13442 + chunkInNAND -= dev->chunkOffset;
13446 + tags->sequenceNumber = dev->sequenceNumber;
13447 + tags->chunkUsed = 1;
13448 + if (!yaffs_ValidateTags(tags)) {
13449 + T(YAFFS_TRACE_ERROR,
13450 + (TSTR("Writing uninitialised tags" TENDSTR)));
13453 + T(YAFFS_TRACE_WRITE,
13454 + (TSTR("Writing chunk %d tags %d %d" TENDSTR), chunkInNAND,
13455 + tags->objectId, tags->chunkId));
13457 + T(YAFFS_TRACE_ERROR, (TSTR("Writing with no tags" TENDSTR)));
13461 + if (dev->writeChunkWithTagsToNAND)
13462 + return dev->writeChunkWithTagsToNAND(dev, chunkInNAND, buffer,
13465 + return yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(dev,
13471 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo)
13473 + blockNo -= dev->blockOffset;
13476 + if (dev->markNANDBlockBad)
13477 + return dev->markNANDBlockBad(dev, blockNo);
13479 + return yaffs_TagsCompatabilityMarkNANDBlockBad(dev, blockNo);
13482 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13484 + yaffs_BlockState * state,
13485 + unsigned *sequenceNumber)
13487 + blockNo -= dev->blockOffset;
13489 + if (dev->queryNANDBlock)
13490 + return dev->queryNANDBlock(dev, blockNo, state, sequenceNumber);
13492 + return yaffs_TagsCompatabilityQueryNANDBlock(dev, blockNo,
13498 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13503 + blockInNAND -= dev->blockOffset;
13506 + dev->nBlockErasures++;
13507 + result = dev->eraseBlockInNAND(dev, blockInNAND);
13512 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev)
13514 + return dev->initialiseNAND(dev);
13519 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nand.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h
13520 --- linux-2.6.21.1/fs/yaffs2/yaffs_nand.h 1970-01-01 01:00:00.000000000 +0100
13521 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nand.h 2007-05-30 13:17:17.000000000 +0200
13524 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13526 + * Copyright (C) 2002-2007 Aleph One Ltd.
13527 + * for Toby Churchill Ltd and Brightstar Engineering
13529 + * Created by Charles Manning <charles@aleph1.co.uk>
13531 + * This program is free software; you can redistribute it and/or modify
13532 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13533 + * published by the Free Software Foundation.
13535 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13538 +#ifndef __YAFFS_NAND_H__
13539 +#define __YAFFS_NAND_H__
13540 +#include "yaffs_guts.h"
13544 +int yaffs_ReadChunkWithTagsFromNAND(yaffs_Device * dev, int chunkInNAND,
13546 + yaffs_ExtendedTags * tags);
13548 +int yaffs_WriteChunkWithTagsToNAND(yaffs_Device * dev,
13550 + const __u8 * buffer,
13551 + yaffs_ExtendedTags * tags);
13553 +int yaffs_MarkBlockBad(yaffs_Device * dev, int blockNo);
13555 +int yaffs_QueryInitialBlockState(yaffs_Device * dev,
13557 + yaffs_BlockState * state,
13558 + unsigned *sequenceNumber);
13560 +int yaffs_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13561 + int blockInNAND);
13563 +int yaffs_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13567 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h
13568 --- linux-2.6.21.1/fs/yaffs2/yaffs_nandemul2k.h 1970-01-01 01:00:00.000000000 +0100
13569 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_nandemul2k.h 2007-05-30 13:17:17.000000000 +0200
13572 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13574 + * Copyright (C) 2002-2007 Aleph One Ltd.
13575 + * for Toby Churchill Ltd and Brightstar Engineering
13577 + * Created by Charles Manning <charles@aleph1.co.uk>
13579 + * This program is free software; you can redistribute it and/or modify
13580 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13581 + * published by the Free Software Foundation.
13583 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13586 +/* Interface to emulated NAND functions (2k page size) */
13588 +#ifndef __YAFFS_NANDEMUL2K_H__
13589 +#define __YAFFS_NANDEMUL2K_H__
13591 +#include "yaffs_guts.h"
13593 +int nandemul2k_WriteChunkWithTagsToNAND(struct yaffs_DeviceStruct *dev,
13594 + int chunkInNAND, const __u8 * data,
13595 + yaffs_ExtendedTags * tags);
13596 +int nandemul2k_ReadChunkWithTagsFromNAND(struct yaffs_DeviceStruct *dev,
13597 + int chunkInNAND, __u8 * data,
13598 + yaffs_ExtendedTags * tags);
13599 +int nandemul2k_MarkNANDBlockBad(struct yaffs_DeviceStruct *dev, int blockNo);
13600 +int nandemul2k_QueryNANDBlock(struct yaffs_DeviceStruct *dev, int blockNo,
13601 + yaffs_BlockState * state, int *sequenceNumber);
13602 +int nandemul2k_EraseBlockInNAND(struct yaffs_DeviceStruct *dev,
13603 + int blockInNAND);
13604 +int nandemul2k_InitialiseNAND(struct yaffs_DeviceStruct *dev);
13605 +int nandemul2k_GetBytesPerChunk(void);
13606 +int nandemul2k_GetChunksPerBlock(void);
13607 +int nandemul2k_GetNumberOfBlocks(void);
13610 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c
13611 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.c 1970-01-01 01:00:00.000000000 +0100
13612 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.c 2007-05-30 13:17:17.000000000 +0200
13615 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13617 + * Copyright (C) 2002-2007 Aleph One Ltd.
13618 + * for Toby Churchill Ltd and Brightstar Engineering
13620 + * Created by Charles Manning <charles@aleph1.co.uk>
13622 + * This program is free software; you can redistribute it and/or modify
13623 + * it under the terms of the GNU General Public License version 2 as
13624 + * published by the Free Software Foundation.
13627 +#include "yaffs_packedtags1.h"
13628 +#include "yportenv.h"
13630 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t)
13632 + pt->chunkId = t->chunkId;
13633 + pt->serialNumber = t->serialNumber;
13634 + pt->byteCount = t->byteCount;
13635 + pt->objectId = t->objectId;
13637 + pt->deleted = (t->chunkDeleted) ? 0 : 1;
13638 + pt->unusedStuff = 0;
13639 + pt->shouldBeFF = 0xFFFFFFFF;
13643 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt)
13645 + static const __u8 allFF[] =
13646 + { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
13649 + if (memcmp(allFF, pt, sizeof(yaffs_PackedTags1))) {
13651 + if (pt->shouldBeFF != 0xFFFFFFFF) {
13654 + t->chunkUsed = 1;
13655 + t->objectId = pt->objectId;
13656 + t->chunkId = pt->chunkId;
13657 + t->byteCount = pt->byteCount;
13658 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13659 + t->chunkDeleted = (pt->deleted) ? 0 : 1;
13660 + t->serialNumber = pt->serialNumber;
13662 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13666 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h
13667 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags1.h 1970-01-01 01:00:00.000000000 +0100
13668 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags1.h 2007-05-30 13:17:17.000000000 +0200
13671 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13673 + * Copyright (C) 2002-2007 Aleph One Ltd.
13674 + * for Toby Churchill Ltd and Brightstar Engineering
13676 + * Created by Charles Manning <charles@aleph1.co.uk>
13678 + * This program is free software; you can redistribute it and/or modify
13679 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13680 + * published by the Free Software Foundation.
13682 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13685 +/* This is used to pack YAFFS1 tags, not YAFFS2 tags. */
13687 +#ifndef __YAFFS_PACKEDTAGS1_H__
13688 +#define __YAFFS_PACKEDTAGS1_H__
13690 +#include "yaffs_guts.h"
13693 + unsigned chunkId:20;
13694 + unsigned serialNumber:2;
13695 + unsigned byteCount:10;
13696 + unsigned objectId:18;
13698 + unsigned deleted:1;
13699 + unsigned unusedStuff:1;
13700 + unsigned shouldBeFF;
13702 +} yaffs_PackedTags1;
13704 +void yaffs_PackTags1(yaffs_PackedTags1 * pt, const yaffs_ExtendedTags * t);
13705 +void yaffs_UnpackTags1(yaffs_ExtendedTags * t, const yaffs_PackedTags1 * pt);
13707 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c
13708 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.c 1970-01-01 01:00:00.000000000 +0100
13709 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.c 2007-05-30 13:17:17.000000000 +0200
13712 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
13714 + * Copyright (C) 2002-2007 Aleph One Ltd.
13715 + * for Toby Churchill Ltd and Brightstar Engineering
13717 + * Created by Charles Manning <charles@aleph1.co.uk>
13719 + * This program is free software; you can redistribute it and/or modify
13720 + * it under the terms of the GNU General Public License version 2 as
13721 + * published by the Free Software Foundation.
13724 +#include "yaffs_packedtags2.h"
13725 +#include "yportenv.h"
13726 +#include "yaffs_tagsvalidity.h"
13728 +/* This code packs a set of extended tags into a binary structure for
13732 +/* Some of the information is "extra" struff which can be packed in to
13734 + * This is defined by having the EXTRA_HEADER_INFO_FLAG set.
13737 +/* Extra flags applied to chunkId */
13739 +#define EXTRA_HEADER_INFO_FLAG 0x80000000
13740 +#define EXTRA_SHRINK_FLAG 0x40000000
13741 +#define EXTRA_SHADOWS_FLAG 0x20000000
13742 +#define EXTRA_SPARE_FLAGS 0x10000000
13744 +#define ALL_EXTRA_FLAGS 0xF0000000
13746 +/* Also, the top 4 bits of the object Id are set to the object type. */
13747 +#define EXTRA_OBJECT_TYPE_SHIFT (28)
13748 +#define EXTRA_OBJECT_TYPE_MASK ((0x0F) << EXTRA_OBJECT_TYPE_SHIFT)
13750 +static void yaffs_DumpPackedTags2(const yaffs_PackedTags2 * pt)
13752 + T(YAFFS_TRACE_MTD,
13753 + (TSTR("packed tags obj %d chunk %d byte %d seq %d" TENDSTR),
13754 + pt->t.objectId, pt->t.chunkId, pt->t.byteCount,
13755 + pt->t.sequenceNumber));
13758 +static void yaffs_DumpTags2(const yaffs_ExtendedTags * t)
13760 + T(YAFFS_TRACE_MTD,
13762 + ("ext.tags eccres %d blkbad %d chused %d obj %d chunk%d byte "
13763 + "%d del %d ser %d seq %d"
13764 + TENDSTR), t->eccResult, t->blockBad, t->chunkUsed, t->objectId,
13765 + t->chunkId, t->byteCount, t->chunkDeleted, t->serialNumber,
13766 + t->sequenceNumber));
13770 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t)
13772 + pt->t.chunkId = t->chunkId;
13773 + pt->t.sequenceNumber = t->sequenceNumber;
13774 + pt->t.byteCount = t->byteCount;
13775 + pt->t.objectId = t->objectId;
13777 + if (t->chunkId == 0 && t->extraHeaderInfoAvailable) {
13778 + /* Store the extra header info instead */
13779 + /* We save the parent object in the chunkId */
13780 + pt->t.chunkId = EXTRA_HEADER_INFO_FLAG
13781 + | t->extraParentObjectId;
13782 + if (t->extraIsShrinkHeader) {
13783 + pt->t.chunkId |= EXTRA_SHRINK_FLAG;
13785 + if (t->extraShadows) {
13786 + pt->t.chunkId |= EXTRA_SHADOWS_FLAG;
13789 + pt->t.objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13790 + pt->t.objectId |=
13791 + (t->extraObjectType << EXTRA_OBJECT_TYPE_SHIFT);
13793 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13794 + pt->t.byteCount = t->extraEquivalentObjectId;
13795 + } else if (t->extraObjectType == YAFFS_OBJECT_TYPE_FILE) {
13796 + pt->t.byteCount = t->extraFileLength;
13798 + pt->t.byteCount = 0;
13802 + yaffs_DumpPackedTags2(pt);
13803 + yaffs_DumpTags2(t);
13805 +#ifndef YAFFS_IGNORE_TAGS_ECC
13807 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13808 + sizeof(yaffs_PackedTags2TagsPart),
13814 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt)
13817 + memset(t, 0, sizeof(yaffs_ExtendedTags));
13819 + yaffs_InitialiseTags(t);
13821 + if (pt->t.sequenceNumber != 0xFFFFFFFF) {
13822 + /* Page is in use */
13823 +#ifdef YAFFS_IGNORE_TAGS_ECC
13825 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13829 + yaffs_ECCOther ecc;
13831 + yaffs_ECCCalculateOther((unsigned char *)&pt->t,
13833 + (yaffs_PackedTags2TagsPart),
13836 + yaffs_ECCCorrectOther((unsigned char *)&pt->t,
13838 + (yaffs_PackedTags2TagsPart),
13842 + t->eccResult = YAFFS_ECC_RESULT_NO_ERROR;
13845 + t->eccResult = YAFFS_ECC_RESULT_FIXED;
13848 + t->eccResult = YAFFS_ECC_RESULT_UNFIXED;
13851 + t->eccResult = YAFFS_ECC_RESULT_UNKNOWN;
13856 + t->chunkUsed = 1;
13857 + t->objectId = pt->t.objectId;
13858 + t->chunkId = pt->t.chunkId;
13859 + t->byteCount = pt->t.byteCount;
13860 + t->chunkDeleted = 0;
13861 + t->serialNumber = 0;
13862 + t->sequenceNumber = pt->t.sequenceNumber;
13864 + /* Do extra header info stuff */
13866 + if (pt->t.chunkId & EXTRA_HEADER_INFO_FLAG) {
13868 + t->byteCount = 0;
13870 + t->extraHeaderInfoAvailable = 1;
13871 + t->extraParentObjectId =
13872 + pt->t.chunkId & (~(ALL_EXTRA_FLAGS));
13873 + t->extraIsShrinkHeader =
13874 + (pt->t.chunkId & EXTRA_SHRINK_FLAG) ? 1 : 0;
13875 + t->extraShadows =
13876 + (pt->t.chunkId & EXTRA_SHADOWS_FLAG) ? 1 : 0;
13877 + t->extraObjectType =
13878 + pt->t.objectId >> EXTRA_OBJECT_TYPE_SHIFT;
13879 + t->objectId &= ~EXTRA_OBJECT_TYPE_MASK;
13881 + if (t->extraObjectType == YAFFS_OBJECT_TYPE_HARDLINK) {
13882 + t->extraEquivalentObjectId = pt->t.byteCount;
13884 + t->extraFileLength = pt->t.byteCount;
13889 + yaffs_DumpPackedTags2(pt);
13890 + yaffs_DumpTags2(t);
13893 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h
13894 --- linux-2.6.21.1/fs/yaffs2/yaffs_packedtags2.h 1970-01-01 01:00:00.000000000 +0100
13895 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_packedtags2.h 2007-05-30 13:17:17.000000000 +0200
13898 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
13900 + * Copyright (C) 2002-2007 Aleph One Ltd.
13901 + * for Toby Churchill Ltd and Brightstar Engineering
13903 + * Created by Charles Manning <charles@aleph1.co.uk>
13905 + * This program is free software; you can redistribute it and/or modify
13906 + * it under the terms of the GNU Lesser General Public License version 2.1 as
13907 + * published by the Free Software Foundation.
13909 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
13912 +/* This is used to pack YAFFS2 tags, not YAFFS1tags. */
13914 +#ifndef __YAFFS_PACKEDTAGS2_H__
13915 +#define __YAFFS_PACKEDTAGS2_H__
13917 +#include "yaffs_guts.h"
13918 +#include "yaffs_ecc.h"
13921 + unsigned sequenceNumber;
13922 + unsigned objectId;
13923 + unsigned chunkId;
13924 + unsigned byteCount;
13925 +} yaffs_PackedTags2TagsPart;
13928 + yaffs_PackedTags2TagsPart t;
13929 + yaffs_ECCOther ecc;
13930 +} yaffs_PackedTags2;
13932 +void yaffs_PackTags2(yaffs_PackedTags2 * pt, const yaffs_ExtendedTags * t);
13933 +void yaffs_UnpackTags2(yaffs_ExtendedTags * t, yaffs_PackedTags2 * pt);
13935 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c
13936 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.c 1970-01-01 01:00:00.000000000 +0100
13937 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.c 2007-05-30 13:17:17.000000000 +0200
13940 + * Copyright (c) 1992, 1993
13941 + * The Regents of the University of California. All rights reserved.
13943 + * Redistribution and use in source and binary forms, with or without
13944 + * modification, are permitted provided that the following conditions
13946 + * 1. Redistributions of source code must retain the above copyright
13947 + * notice, this list of conditions and the following disclaimer.
13948 + * 2. Redistributions in binary form must reproduce the above copyright
13949 + * notice, this list of conditions and the following disclaimer in the
13950 + * documentation and/or other materials provided with the distribution.
13951 + * 3. Neither the name of the University nor the names of its contributors
13952 + * may be used to endorse or promote products derived from this software
13953 + * without specific prior written permission.
13955 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
13956 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
13957 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
13958 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
13959 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
13960 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
13961 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
13962 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
13963 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
13964 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
13968 +#include "yportenv.h"
13969 +//#include <linux/string.h>
13972 + * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
13974 +#define swapcode(TYPE, parmi, parmj, n) { \
13975 + long i = (n) / sizeof (TYPE); \
13976 + register TYPE *pi = (TYPE *) (parmi); \
13977 + register TYPE *pj = (TYPE *) (parmj); \
13979 + register TYPE t = *pi; \
13982 + } while (--i > 0); \
13985 +#define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
13986 + es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
13988 +static __inline void
13989 +swapfunc(char *a, char *b, int n, int swaptype)
13991 + if (swaptype <= 1)
13992 + swapcode(long, a, b, n)
13994 + swapcode(char, a, b, n)
13997 +#define swap(a, b) \
13998 + if (swaptype == 0) { \
13999 + long t = *(long *)(a); \
14000 + *(long *)(a) = *(long *)(b); \
14001 + *(long *)(b) = t; \
14003 + swapfunc(a, b, es, swaptype)
14005 +#define vecswap(a, b, n) if ((n) > 0) swapfunc(a, b, n, swaptype)
14007 +static __inline char *
14008 +med3(char *a, char *b, char *c, int (*cmp)(const void *, const void *))
14010 + return cmp(a, b) < 0 ?
14011 + (cmp(b, c) < 0 ? b : (cmp(a, c) < 0 ? c : a ))
14012 + :(cmp(b, c) > 0 ? b : (cmp(a, c) < 0 ? a : c ));
14016 +#define min(a,b) (((a) < (b)) ? (a) : (b))
14020 +yaffs_qsort(void *aa, size_t n, size_t es,
14021 + int (*cmp)(const void *, const void *))
14023 + char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
14024 + int d, r, swaptype, swap_cnt;
14025 + register char *a = aa;
14027 +loop: SWAPINIT(a, es);
14030 + for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es)
14031 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14033 + swap(pl, pl - es);
14036 + pm = (char *)a + (n / 2) * es;
14039 + pn = (char *)a + (n - 1) * es;
14041 + d = (n / 8) * es;
14042 + pl = med3(pl, pl + d, pl + 2 * d, cmp);
14043 + pm = med3(pm - d, pm, pm + d, cmp);
14044 + pn = med3(pn - 2 * d, pn - d, pn, cmp);
14046 + pm = med3(pl, pm, pn, cmp);
14049 + pa = pb = (char *)a + es;
14051 + pc = pd = (char *)a + (n - 1) * es;
14053 + while (pb <= pc && (r = cmp(pb, a)) <= 0) {
14061 + while (pb <= pc && (r = cmp(pc, a)) >= 0) {
14076 + if (swap_cnt == 0) { /* Switch to insertion sort */
14077 + for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es)
14078 + for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0;
14080 + swap(pl, pl - es);
14084 + pn = (char *)a + n * es;
14085 + r = min(pa - (char *)a, pb - pa);
14086 + vecswap(a, pb - r, r);
14087 + r = min((long)(pd - pc), (long)(pn - pd - es));
14088 + vecswap(pb, pn - r, r);
14089 + if ((r = pb - pa) > es)
14090 + yaffs_qsort(a, r / es, es, cmp);
14091 + if ((r = pd - pc) > es) {
14092 + /* Iterate rather than recurse to save stack space */
14097 +/* yaffs_qsort(pn - r, r / es, es, cmp);*/
14099 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h
14100 --- linux-2.6.21.1/fs/yaffs2/yaffs_qsort.h 1970-01-01 01:00:00.000000000 +0100
14101 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_qsort.h 2007-05-30 13:17:17.000000000 +0200
14104 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14106 + * Copyright (C) 2002-2007 Aleph One Ltd.
14107 + * for Toby Churchill Ltd and Brightstar Engineering
14109 + * Created by Charles Manning <charles@aleph1.co.uk>
14111 + * This program is free software; you can redistribute it and/or modify
14112 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14113 + * published by the Free Software Foundation.
14115 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14119 +#ifndef __YAFFS_QSORT_H__
14120 +#define __YAFFS_QSORT_H__
14122 +extern void yaffs_qsort (void *const base, size_t total_elems, size_t size,
14123 + int (*cmp)(const void *, const void *));
14126 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c
14127 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.c 1970-01-01 01:00:00.000000000 +0100
14128 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.c 2007-05-30 13:17:17.000000000 +0200
14131 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14133 + * Copyright (C) 2002-2007 Aleph One Ltd.
14134 + * for Toby Churchill Ltd and Brightstar Engineering
14136 + * Created by Charles Manning <charles@aleph1.co.uk>
14138 + * This program is free software; you can redistribute it and/or modify
14139 + * it under the terms of the GNU General Public License version 2 as
14140 + * published by the Free Software Foundation.
14143 +#include "yaffs_guts.h"
14144 +#include "yaffs_tagscompat.h"
14145 +#include "yaffs_ecc.h"
14147 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND);
14149 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND);
14150 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14151 + const __u8 * data,
14152 + const yaffs_Spare * spare);
14153 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14154 + const yaffs_Spare * spare);
14155 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND);
14158 +static const char yaffs_countBitsTable[256] = {
14159 + 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
14160 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14161 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14162 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14163 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14164 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14165 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14166 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14167 + 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
14168 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14169 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14170 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14171 + 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
14172 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14173 + 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
14174 + 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
14177 +int yaffs_CountBits(__u8 x)
14180 + retVal = yaffs_countBitsTable[x];
14184 +/********** Tags ECC calculations *********/
14186 +void yaffs_CalcECC(const __u8 * data, yaffs_Spare * spare)
14188 + yaffs_ECCCalculate(data, spare->ecc1);
14189 + yaffs_ECCCalculate(&data[256], spare->ecc2);
14192 +void yaffs_CalcTagsECC(yaffs_Tags * tags)
14194 + /* Calculate an ecc */
14196 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14198 + unsigned ecc = 0;
14199 + unsigned bit = 0;
14203 + for (i = 0; i < 8; i++) {
14204 + for (j = 1; j & 0xff; j <<= 1) {
14216 +int yaffs_CheckECCOnTags(yaffs_Tags * tags)
14218 + unsigned ecc = tags->ecc;
14220 + yaffs_CalcTagsECC(tags);
14222 + ecc ^= tags->ecc;
14224 + if (ecc && ecc <= 64) {
14225 + /* TODO: Handle the failure better. Retire? */
14226 + unsigned char *b = ((yaffs_TagsUnion *) tags)->asBytes;
14230 + b[ecc / 8] ^= (1 << (ecc & 7));
14232 + /* Now recvalc the ecc */
14233 + yaffs_CalcTagsECC(tags);
14235 + return 1; /* recovered error */
14236 + } else if (ecc) {
14237 + /* Wierd ecc failure value */
14238 + /* TODO Need to do somethiong here */
14239 + return -1; /* unrecovered error */
14245 +/********** Tags **********/
14247 +static void yaffs_LoadTagsIntoSpare(yaffs_Spare * sparePtr,
14248 + yaffs_Tags * tagsPtr)
14250 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14252 + yaffs_CalcTagsECC(tagsPtr);
14254 + sparePtr->tagByte0 = tu->asBytes[0];
14255 + sparePtr->tagByte1 = tu->asBytes[1];
14256 + sparePtr->tagByte2 = tu->asBytes[2];
14257 + sparePtr->tagByte3 = tu->asBytes[3];
14258 + sparePtr->tagByte4 = tu->asBytes[4];
14259 + sparePtr->tagByte5 = tu->asBytes[5];
14260 + sparePtr->tagByte6 = tu->asBytes[6];
14261 + sparePtr->tagByte7 = tu->asBytes[7];
14264 +static void yaffs_GetTagsFromSpare(yaffs_Device * dev, yaffs_Spare * sparePtr,
14265 + yaffs_Tags * tagsPtr)
14267 + yaffs_TagsUnion *tu = (yaffs_TagsUnion *) tagsPtr;
14270 + tu->asBytes[0] = sparePtr->tagByte0;
14271 + tu->asBytes[1] = sparePtr->tagByte1;
14272 + tu->asBytes[2] = sparePtr->tagByte2;
14273 + tu->asBytes[3] = sparePtr->tagByte3;
14274 + tu->asBytes[4] = sparePtr->tagByte4;
14275 + tu->asBytes[5] = sparePtr->tagByte5;
14276 + tu->asBytes[6] = sparePtr->tagByte6;
14277 + tu->asBytes[7] = sparePtr->tagByte7;
14279 + result = yaffs_CheckECCOnTags(tagsPtr);
14280 + if (result > 0) {
14281 + dev->tagsEccFixed++;
14282 + } else if (result < 0) {
14283 + dev->tagsEccUnfixed++;
14287 +static void yaffs_SpareInitialise(yaffs_Spare * spare)
14289 + memset(spare, 0xFF, sizeof(yaffs_Spare));
14292 +static int yaffs_WriteChunkToNAND(struct yaffs_DeviceStruct *dev,
14293 + int chunkInNAND, const __u8 * data,
14294 + yaffs_Spare * spare)
14296 + if (chunkInNAND < dev->startBlock * dev->nChunksPerBlock) {
14297 + T(YAFFS_TRACE_ERROR,
14298 + (TSTR("**>> yaffs chunk %d is not valid" TENDSTR),
14300 + return YAFFS_FAIL;
14303 + dev->nPageWrites++;
14304 + return dev->writeChunkToNAND(dev, chunkInNAND, data, spare);
14307 +static int yaffs_ReadChunkFromNAND(struct yaffs_DeviceStruct *dev,
14310 + yaffs_Spare * spare,
14311 + yaffs_ECCResult * eccResult,
14312 + int doErrorCorrection)
14315 + yaffs_Spare localSpare;
14317 + dev->nPageReads++;
14319 + if (!spare && data) {
14320 + /* If we don't have a real spare, then we use a local one. */
14321 + /* Need this for the calculation of the ecc */
14322 + spare = &localSpare;
14325 + if (!dev->useNANDECC) {
14326 + retVal = dev->readChunkFromNAND(dev, chunkInNAND, data, spare);
14327 + if (data && doErrorCorrection) {
14328 + /* Do ECC correction */
14329 + /* Todo handle any errors */
14330 + int eccResult1, eccResult2;
14333 + yaffs_ECCCalculate(data, calcEcc);
14335 + yaffs_ECCCorrect(data, spare->ecc1, calcEcc);
14336 + yaffs_ECCCalculate(&data[256], calcEcc);
14338 + yaffs_ECCCorrect(&data[256], spare->ecc2, calcEcc);
14340 + if (eccResult1 > 0) {
14341 + T(YAFFS_TRACE_ERROR,
14343 + ("**>>yaffs ecc error fix performed on chunk %d:0"
14344 + TENDSTR), chunkInNAND));
14346 + } else if (eccResult1 < 0) {
14347 + T(YAFFS_TRACE_ERROR,
14349 + ("**>>yaffs ecc error unfixed on chunk %d:0"
14350 + TENDSTR), chunkInNAND));
14351 + dev->eccUnfixed++;
14354 + if (eccResult2 > 0) {
14355 + T(YAFFS_TRACE_ERROR,
14357 + ("**>>yaffs ecc error fix performed on chunk %d:1"
14358 + TENDSTR), chunkInNAND));
14360 + } else if (eccResult2 < 0) {
14361 + T(YAFFS_TRACE_ERROR,
14363 + ("**>>yaffs ecc error unfixed on chunk %d:1"
14364 + TENDSTR), chunkInNAND));
14365 + dev->eccUnfixed++;
14368 + if (eccResult1 || eccResult2) {
14369 + /* We had a data problem on this page */
14370 + yaffs_HandleReadDataError(dev, chunkInNAND);
14373 + if (eccResult1 < 0 || eccResult2 < 0)
14374 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14375 + else if (eccResult1 > 0 || eccResult2 > 0)
14376 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14378 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14381 + /* Must allocate enough memory for spare+2*sizeof(int) */
14382 + /* for ecc results from device. */
14383 + struct yaffs_NANDSpare nspare;
14385 + dev->readChunkFromNAND(dev, chunkInNAND, data,
14386 + (yaffs_Spare *) & nspare);
14387 + memcpy(spare, &nspare, sizeof(yaffs_Spare));
14388 + if (data && doErrorCorrection) {
14389 + if (nspare.eccres1 > 0) {
14390 + T(YAFFS_TRACE_ERROR,
14392 + ("**>>mtd ecc error fix performed on chunk %d:0"
14393 + TENDSTR), chunkInNAND));
14394 + } else if (nspare.eccres1 < 0) {
14395 + T(YAFFS_TRACE_ERROR,
14397 + ("**>>mtd ecc error unfixed on chunk %d:0"
14398 + TENDSTR), chunkInNAND));
14401 + if (nspare.eccres2 > 0) {
14402 + T(YAFFS_TRACE_ERROR,
14404 + ("**>>mtd ecc error fix performed on chunk %d:1"
14405 + TENDSTR), chunkInNAND));
14406 + } else if (nspare.eccres2 < 0) {
14407 + T(YAFFS_TRACE_ERROR,
14409 + ("**>>mtd ecc error unfixed on chunk %d:1"
14410 + TENDSTR), chunkInNAND));
14413 + if (nspare.eccres1 || nspare.eccres2) {
14414 + /* We had a data problem on this page */
14415 + yaffs_HandleReadDataError(dev, chunkInNAND);
14418 + if (nspare.eccres1 < 0 || nspare.eccres2 < 0)
14419 + *eccResult = YAFFS_ECC_RESULT_UNFIXED;
14420 + else if (nspare.eccres1 > 0 || nspare.eccres2 > 0)
14421 + *eccResult = YAFFS_ECC_RESULT_FIXED;
14423 + *eccResult = YAFFS_ECC_RESULT_NO_ERROR;
14431 +static int yaffs_CheckChunkErased(struct yaffs_DeviceStruct *dev,
14435 + static int init = 0;
14436 + static __u8 cmpbuf[YAFFS_BYTES_PER_CHUNK];
14437 + static __u8 data[YAFFS_BYTES_PER_CHUNK];
14438 + /* Might as well always allocate the larger size for */
14439 + /* dev->useNANDECC == true; */
14440 + static __u8 spare[sizeof(struct yaffs_NANDSpare)];
14442 + dev->readChunkFromNAND(dev, chunkInNAND, data, (yaffs_Spare *) spare);
14445 + memset(cmpbuf, 0xff, YAFFS_BYTES_PER_CHUNK);
14449 + if (memcmp(cmpbuf, data, YAFFS_BYTES_PER_CHUNK))
14450 + return YAFFS_FAIL;
14451 + if (memcmp(cmpbuf, spare, 16))
14452 + return YAFFS_FAIL;
14460 + * Functions for robustisizing
14463 +static void yaffs_HandleReadDataError(yaffs_Device * dev, int chunkInNAND)
14465 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14467 + /* Mark the block for retirement */
14468 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14469 + T(YAFFS_TRACE_ERROR | YAFFS_TRACE_BAD_BLOCKS,
14470 + (TSTR("**>>Block %d marked for retirement" TENDSTR), blockInNAND));
14473 + * Just do a garbage collection on the affected block
14474 + * then retire the block
14480 +static void yaffs_CheckWrittenBlock(yaffs_Device * dev, int chunkInNAND)
14484 +static void yaffs_HandleWriteChunkOk(yaffs_Device * dev, int chunkInNAND,
14485 + const __u8 * data,
14486 + const yaffs_Spare * spare)
14490 +static void yaffs_HandleUpdateChunk(yaffs_Device * dev, int chunkInNAND,
14491 + const yaffs_Spare * spare)
14495 +static void yaffs_HandleWriteChunkError(yaffs_Device * dev, int chunkInNAND)
14497 + int blockInNAND = chunkInNAND / dev->nChunksPerBlock;
14499 + /* Mark the block for retirement */
14500 + yaffs_GetBlockInfo(dev, blockInNAND)->needsRetiring = 1;
14501 + /* Delete the chunk */
14502 + yaffs_DeleteChunk(dev, chunkInNAND, 1, __LINE__);
14505 +static int yaffs_VerifyCompare(const __u8 * d0, const __u8 * d1,
14506 + const yaffs_Spare * s0, const yaffs_Spare * s1)
14509 + if (memcmp(d0, d1, YAFFS_BYTES_PER_CHUNK) != 0 ||
14510 + s0->tagByte0 != s1->tagByte0 ||
14511 + s0->tagByte1 != s1->tagByte1 ||
14512 + s0->tagByte2 != s1->tagByte2 ||
14513 + s0->tagByte3 != s1->tagByte3 ||
14514 + s0->tagByte4 != s1->tagByte4 ||
14515 + s0->tagByte5 != s1->tagByte5 ||
14516 + s0->tagByte6 != s1->tagByte6 ||
14517 + s0->tagByte7 != s1->tagByte7 ||
14518 + s0->ecc1[0] != s1->ecc1[0] ||
14519 + s0->ecc1[1] != s1->ecc1[1] ||
14520 + s0->ecc1[2] != s1->ecc1[2] ||
14521 + s0->ecc2[0] != s1->ecc2[0] ||
14522 + s0->ecc2[1] != s1->ecc2[1] || s0->ecc2[2] != s1->ecc2[2]) {
14528 +#endif /* NOTYET */
14530 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14532 + const __u8 * data,
14533 + const yaffs_ExtendedTags *
14536 + yaffs_Spare spare;
14539 + yaffs_SpareInitialise(&spare);
14541 + if (eTags->chunkDeleted) {
14542 + spare.pageStatus = 0;
14544 + tags.objectId = eTags->objectId;
14545 + tags.chunkId = eTags->chunkId;
14546 + tags.byteCount = eTags->byteCount;
14547 + tags.serialNumber = eTags->serialNumber;
14549 + if (!dev->useNANDECC && data) {
14550 + yaffs_CalcECC(data, &spare);
14552 + yaffs_LoadTagsIntoSpare(&spare, &tags);
14556 + return yaffs_WriteChunkToNAND(dev, chunkInNAND, data, &spare);
14559 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14562 + yaffs_ExtendedTags * eTags)
14565 + yaffs_Spare spare;
14567 + yaffs_ECCResult eccResult;
14569 + static yaffs_Spare spareFF;
14573 + memset(&spareFF, 0xFF, sizeof(spareFF));
14577 + if (yaffs_ReadChunkFromNAND
14578 + (dev, chunkInNAND, data, &spare, &eccResult, 1)) {
14579 + /* eTags may be NULL */
14583 + (yaffs_CountBits(spare.pageStatus) < 7) ? 1 : 0;
14585 + eTags->chunkDeleted = deleted;
14586 + eTags->eccResult = eccResult;
14587 + eTags->blockBad = 0; /* We're reading it */
14588 + /* therefore it is not a bad block */
14589 + eTags->chunkUsed =
14590 + (memcmp(&spareFF, &spare, sizeof(spareFF)) !=
14593 + if (eTags->chunkUsed) {
14594 + yaffs_GetTagsFromSpare(dev, &spare, &tags);
14596 + eTags->objectId = tags.objectId;
14597 + eTags->chunkId = tags.chunkId;
14598 + eTags->byteCount = tags.byteCount;
14599 + eTags->serialNumber = tags.serialNumber;
14605 + return YAFFS_FAIL;
14609 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14613 + yaffs_Spare spare;
14615 + memset(&spare, 0xff, sizeof(yaffs_Spare));
14617 + spare.blockStatus = 'Y';
14619 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock, NULL,
14621 + yaffs_WriteChunkToNAND(dev, blockInNAND * dev->nChunksPerBlock + 1,
14628 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14629 + int blockNo, yaffs_BlockState *
14631 + int *sequenceNumber)
14634 + yaffs_Spare spare0, spare1;
14635 + static yaffs_Spare spareFF;
14637 + yaffs_ECCResult dummy;
14640 + memset(&spareFF, 0xFF, sizeof(spareFF));
14644 + *sequenceNumber = 0;
14646 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock, NULL,
14647 + &spare0, &dummy, 1);
14648 + yaffs_ReadChunkFromNAND(dev, blockNo * dev->nChunksPerBlock + 1, NULL,
14649 + &spare1, &dummy, 1);
14651 + if (yaffs_CountBits(spare0.blockStatus & spare1.blockStatus) < 7)
14652 + *state = YAFFS_BLOCK_STATE_DEAD;
14653 + else if (memcmp(&spareFF, &spare0, sizeof(spareFF)) == 0)
14654 + *state = YAFFS_BLOCK_STATE_EMPTY;
14656 + *state = YAFFS_BLOCK_STATE_NEEDS_SCANNING;
14660 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h
14661 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagscompat.h 1970-01-01 01:00:00.000000000 +0100
14662 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagscompat.h 2007-05-30 13:17:17.000000000 +0200
14665 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14667 + * Copyright (C) 2002-2007 Aleph One Ltd.
14668 + * for Toby Churchill Ltd and Brightstar Engineering
14670 + * Created by Charles Manning <charles@aleph1.co.uk>
14672 + * This program is free software; you can redistribute it and/or modify
14673 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14674 + * published by the Free Software Foundation.
14676 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14679 +#ifndef __YAFFS_TAGSCOMPAT_H__
14680 +#define __YAFFS_TAGSCOMPAT_H__
14682 +#include "yaffs_guts.h"
14683 +int yaffs_TagsCompatabilityWriteChunkWithTagsToNAND(yaffs_Device * dev,
14685 + const __u8 * data,
14686 + const yaffs_ExtendedTags *
14688 +int yaffs_TagsCompatabilityReadChunkWithTagsFromNAND(yaffs_Device * dev,
14691 + yaffs_ExtendedTags *
14693 +int yaffs_TagsCompatabilityMarkNANDBlockBad(struct yaffs_DeviceStruct *dev,
14695 +int yaffs_TagsCompatabilityQueryNANDBlock(struct yaffs_DeviceStruct *dev,
14696 + int blockNo, yaffs_BlockState *
14697 + state, int *sequenceNumber);
14699 +void yaffs_CalcTagsECC(yaffs_Tags * tags);
14700 +int yaffs_CheckECCOnTags(yaffs_Tags * tags);
14701 +int yaffs_CountBits(__u8 byte);
14704 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c
14705 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.c 1970-01-01 01:00:00.000000000 +0100
14706 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.c 2007-05-30 13:17:17.000000000 +0200
14709 + * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
14711 + * Copyright (C) 2002-2007 Aleph One Ltd.
14712 + * for Toby Churchill Ltd and Brightstar Engineering
14714 + * Created by Charles Manning <charles@aleph1.co.uk>
14716 + * This program is free software; you can redistribute it and/or modify
14717 + * it under the terms of the GNU General Public License version 2 as
14718 + * published by the Free Software Foundation.
14721 +#include "yaffs_tagsvalidity.h"
14723 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags)
14725 + memset(tags, 0, sizeof(yaffs_ExtendedTags));
14726 + tags->validMarker0 = 0xAAAAAAAA;
14727 + tags->validMarker1 = 0x55555555;
14730 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags)
14732 + return (tags->validMarker0 == 0xAAAAAAAA &&
14733 + tags->validMarker1 == 0x55555555);
14736 diff -urN linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h
14737 --- linux-2.6.21.1/fs/yaffs2/yaffs_tagsvalidity.h 1970-01-01 01:00:00.000000000 +0100
14738 +++ linux-2.6.21.1.new/fs/yaffs2/yaffs_tagsvalidity.h 2007-05-30 13:17:17.000000000 +0200
14741 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14743 + * Copyright (C) 2002-2007 Aleph One Ltd.
14744 + * for Toby Churchill Ltd and Brightstar Engineering
14746 + * Created by Charles Manning <charles@aleph1.co.uk>
14748 + * This program is free software; you can redistribute it and/or modify
14749 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14750 + * published by the Free Software Foundation.
14752 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14756 +#ifndef __YAFFS_TAGS_VALIDITY_H__
14757 +#define __YAFFS_TAGS_VALIDITY_H__
14759 +#include "yaffs_guts.h"
14761 +void yaffs_InitialiseTags(yaffs_ExtendedTags * tags);
14762 +int yaffs_ValidateTags(yaffs_ExtendedTags * tags);
14764 diff -urN linux-2.6.21.1/fs/yaffs2/yaffsinterface.h linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h
14765 --- linux-2.6.21.1/fs/yaffs2/yaffsinterface.h 1970-01-01 01:00:00.000000000 +0100
14766 +++ linux-2.6.21.1.new/fs/yaffs2/yaffsinterface.h 2007-05-30 13:17:16.000000000 +0200
14769 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14771 + * Copyright (C) 2002-2007 Aleph One Ltd.
14772 + * for Toby Churchill Ltd and Brightstar Engineering
14774 + * Created by Charles Manning <charles@aleph1.co.uk>
14776 + * This program is free software; you can redistribute it and/or modify
14777 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14778 + * published by the Free Software Foundation.
14780 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14783 +#ifndef __YAFFSINTERFACE_H__
14784 +#define __YAFFSINTERFACE_H__
14786 +int yaffs_Initialise(unsigned nBlocks);
14789 diff -urN linux-2.6.21.1/fs/yaffs2/yportenv.h linux-2.6.21.1.new/fs/yaffs2/yportenv.h
14790 --- linux-2.6.21.1/fs/yaffs2/yportenv.h 1970-01-01 01:00:00.000000000 +0100
14791 +++ linux-2.6.21.1.new/fs/yaffs2/yportenv.h 2007-05-30 13:17:17.000000000 +0200
14794 + * YAFFS: Yet another Flash File System . A NAND-flash specific file system.
14796 + * Copyright (C) 2002-2007 Aleph One Ltd.
14797 + * for Toby Churchill Ltd and Brightstar Engineering
14799 + * Created by Charles Manning <charles@aleph1.co.uk>
14801 + * This program is free software; you can redistribute it and/or modify
14802 + * it under the terms of the GNU Lesser General Public License version 2.1 as
14803 + * published by the Free Software Foundation.
14805 + * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
14809 +#ifndef __YPORTENV_H__
14810 +#define __YPORTENV_H__
14812 +#if defined CONFIG_YAFFS_WINCE
14814 +#include "ywinceenv.h"
14816 +#elif defined __KERNEL__
14818 +#include "moduleconfig.h"
14820 +/* Linux kernel */
14821 +#include <linux/version.h>
14822 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19))
14823 +#include <linux/config.h>
14825 +#include <linux/kernel.h>
14826 +#include <linux/mm.h>
14827 +#include <linux/string.h>
14828 +#include <linux/slab.h>
14829 +#include <linux/vmalloc.h>
14831 +#define YCHAR char
14832 +#define YUCHAR unsigned char
14834 +#define yaffs_strcpy(a,b) strcpy(a,b)
14835 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14836 +#define yaffs_strncmp(a,b,c) strncmp(a,b,c)
14837 +#define yaffs_strlen(s) strlen(s)
14838 +#define yaffs_sprintf sprintf
14839 +#define yaffs_toupper(a) toupper(a)
14841 +#define Y_INLINE inline
14843 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14844 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14846 +/* #define YPRINTF(x) printk x */
14847 +#define YMALLOC(x) kmalloc(x,GFP_KERNEL)
14848 +#define YFREE(x) kfree(x)
14849 +#define YMALLOC_ALT(x) vmalloc(x)
14850 +#define YFREE_ALT(x) vfree(x)
14851 +#define YMALLOC_DMA(x) YMALLOC(x)
14853 +// KR - added for use in scan so processes aren't blocked indefinitely.
14854 +#define YYIELD() schedule()
14856 +#define YAFFS_ROOT_MODE 0666
14857 +#define YAFFS_LOSTNFOUND_MODE 0666
14859 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
14860 +#define Y_CURRENT_TIME CURRENT_TIME.tv_sec
14861 +#define Y_TIME_CONVERT(x) (x).tv_sec
14863 +#define Y_CURRENT_TIME CURRENT_TIME
14864 +#define Y_TIME_CONVERT(x) (x)
14867 +#define yaffs_SumCompare(x,y) ((x) == (y))
14868 +#define yaffs_strcmp(a,b) strcmp(a,b)
14870 +#define TENDSTR "\n"
14871 +#define TSTR(x) KERN_WARNING x
14872 +#define TOUT(p) printk p
14874 +#define yaffs_trace(mask, fmt, args...) \
14875 + do { if ((mask) & (yaffs_traceMask|YAFFS_TRACE_ERROR)) \
14876 + printk(KERN_WARNING "yaffs: " fmt, ## args); \
14879 +#define compile_time_assertion(assertion) \
14880 + ({ int x = __builtin_choose_expr(assertion, 0, (void)0); (void) x; })
14882 +#elif defined CONFIG_YAFFS_DIRECT
14884 +/* Direct interface */
14885 +#include "ydirectenv.h"
14887 +#elif defined CONFIG_YAFFS_UTIL
14889 +/* Stuff for YAFFS utilities */
14891 +#include "stdlib.h"
14892 +#include "stdio.h"
14893 +#include "string.h"
14895 +#include "devextras.h"
14897 +#define YMALLOC(x) malloc(x)
14898 +#define YFREE(x) free(x)
14899 +#define YMALLOC_ALT(x) malloc(x)
14900 +#define YFREE_ALT(x) free(x)
14902 +#define YCHAR char
14903 +#define YUCHAR unsigned char
14905 +#define yaffs_strcpy(a,b) strcpy(a,b)
14906 +#define yaffs_strncpy(a,b,c) strncpy(a,b,c)
14907 +#define yaffs_strlen(s) strlen(s)
14908 +#define yaffs_sprintf sprintf
14909 +#define yaffs_toupper(a) toupper(a)
14911 +#define Y_INLINE inline
14913 +/* #define YINFO(s) YPRINTF(( __FILE__ " %d %s\n",__LINE__,s)) */
14914 +/* #define YALERT(s) YINFO(s) */
14916 +#define TENDSTR "\n"
14918 +#define TOUT(p) printf p
14920 +#define YAFFS_LOSTNFOUND_NAME "lost+found"
14921 +#define YAFFS_LOSTNFOUND_PREFIX "obj"
14922 +/* #define YPRINTF(x) printf x */
14924 +#define YAFFS_ROOT_MODE 0666
14925 +#define YAFFS_LOSTNFOUND_MODE 0666
14927 +#define yaffs_SumCompare(x,y) ((x) == (y))
14928 +#define yaffs_strcmp(a,b) strcmp(a,b)
14931 +/* Should have specified a configuration type */
14932 +#error Unknown configuration
14936 +/* see yaffs_fs.c */
14937 +extern unsigned int yaffs_traceMask;
14938 +extern unsigned int yaffs_wr_attempts;
14942 + * The flags masked in YAFFS_TRACE_ALWAYS are always traced.
14945 +#define YAFFS_TRACE_OS 0x00000002
14946 +#define YAFFS_TRACE_ALLOCATE 0x00000004
14947 +#define YAFFS_TRACE_SCAN 0x00000008
14948 +#define YAFFS_TRACE_BAD_BLOCKS 0x00000010
14949 +#define YAFFS_TRACE_ERASE 0x00000020
14950 +#define YAFFS_TRACE_GC 0x00000040
14951 +#define YAFFS_TRACE_WRITE 0x00000080
14952 +#define YAFFS_TRACE_TRACING 0x00000100
14953 +#define YAFFS_TRACE_DELETION 0x00000200
14954 +#define YAFFS_TRACE_BUFFERS 0x00000400
14955 +#define YAFFS_TRACE_NANDACCESS 0x00000800
14956 +#define YAFFS_TRACE_GC_DETAIL 0x00001000
14957 +#define YAFFS_TRACE_SCAN_DEBUG 0x00002000
14958 +#define YAFFS_TRACE_MTD 0x00004000
14959 +#define YAFFS_TRACE_CHECKPOINT 0x00008000
14961 +#define YAFFS_TRACE_VERIFY 0x00010000
14962 +#define YAFFS_TRACE_VERIFY_NAND 0x00020000
14963 +#define YAFFS_TRACE_VERIFY_FULL 0x00040000
14964 +#define YAFFS_TRACE_VERIFY_ALL 0x000F0000
14967 +#define YAFFS_TRACE_ERROR 0x40000000
14968 +#define YAFFS_TRACE_BUG 0x80000000
14969 +#define YAFFS_TRACE_ALWAYS 0xF0000000
14972 +#define T(mask,p) do{ if((mask) & (yaffs_traceMask | YAFFS_TRACE_ALWAYS)) TOUT(p);} while(0)
14974 +#ifndef CONFIG_YAFFS_WINCE
14975 +#define YBUG() T(YAFFS_TRACE_BUG,(TSTR("==>> yaffs bug: " __FILE__ " %d" TENDSTR),__LINE__))
14979 --- linux-2.6.21.1/fs/Makefile 2007-05-30 13:16:21.000000000 +0200
14980 +++ linux-2.6.21.1.new/fs/Makefile 2007-05-30 13:27:34.000000000 +0200
14981 @@ -116,3 +116,4 @@
14982 obj-$(CONFIG_DEBUG_FS) += debugfs/
14983 obj-$(CONFIG_OCFS2_FS) += ocfs2/
14984 obj-$(CONFIG_GFS2_FS) += gfs2/
14985 +obj-$(CONFIG_YAFFS_FS) += yaffs2/
14986 --- linux-2.6.21.1/fs/Kconfig 2007-05-30 13:16:21.000000000 +0200
14987 +++ linux-2.6.21.1.new/fs/Kconfig 2007-05-30 13:29:14.000000000 +0200
14988 @@ -419,6 +419,7 @@
14990 source "fs/xfs/Kconfig"
14991 source "fs/gfs2/Kconfig"
14992 +source "fs/yaffs2/Kconfig"
14995 tristate "OCFS2 file system support"